r600g: Move fetch shader register setup to r600_state.c / evergreen_state.c.
[mesa.git] / src / gallium / drivers / r600 / r600_asm.c
index e96236e06eed2a7f00a9ab4241477bbfad4820f3..df7c5b3fbbcc1acb75d301c7bfcabec175e657cd 100644 (file)
 #define NUM_OF_CYCLES 3
 #define NUM_OF_COMPONENTS 4
 
-static inline unsigned int r600_bc_get_num_operands(struct r600_bc_alu *alu)
+static inline unsigned int r600_bc_get_num_operands(struct r600_bc *bc, struct r600_bc_alu *alu)
 {
        if(alu->is_op3)
                return 3;
 
-       switch (alu->inst) {
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP:
-               return 0;
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ADD:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLNE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MUL: 
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MAX:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MIN:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETE: 
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETNE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETGT:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETGE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4_IEEE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_CUBE:
-               return 2;
-
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOV: 
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA_FLOOR:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FRACT:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLOOR:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_TRUNC:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_EXP_IEEE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_CLAMPED:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_IEEE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_IEEE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_CLAMPED:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_IEEE:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLT_TO_INT:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SIN:
-       case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_COS:
-               return 1;
-       default: R600_ERR(
-               "Need instruction operand number for 0x%x.\n", alu->inst); 
-       };
+       switch (bc->chiprev) {
+       case CHIPREV_R600:
+       case CHIPREV_R700:
+               switch (alu->inst) {
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP:
+                       return 0;
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ADD:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ADD_INT:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLNE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MUL:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULHI_UINT:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MAX:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MIN:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETNE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETGT:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETGE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4_IEEE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_CUBE:
+                       return 2;
+
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOV:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA_FLOOR:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA_INT:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FRACT:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLOOR:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_TRUNC:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_EXP_IEEE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_CLAMPED:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_IEEE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_CLAMPED:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_IEEE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_CLAMPED:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_IEEE:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLT_TO_INT:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_INT_TO_FLT:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SIN:
+               case V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_COS:
+                       return 1;
+               default: R600_ERR(
+                       "Need instruction operand number for 0x%x.\n", alu->inst);
+               }
+               break;
+       case CHIPREV_EVERGREEN:
+               switch (alu->inst) {
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP:
+                       return 0;
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ADD:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ADD_INT:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLNE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MUL:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULHI_UINT:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MAX:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MIN:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETNE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETGT:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETGE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4_IEEE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_CUBE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INTERP_XY:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INTERP_ZW:
+                       return 2;
+
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOV:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA_INT:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FRACT:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLOOR:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_TRUNC:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_EXP_IEEE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_CLAMPED:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_IEEE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_CLAMPED:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_IEEE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_CLAMPED:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_IEEE:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLT_TO_INT:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLT_TO_INT_FLOOR:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_INT_TO_FLT:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SIN:
+               case EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_COS:
+                       return 1;
+               default: R600_ERR(
+                       "Need instruction operand number for 0x%x.\n", alu->inst);
+               }
+               break;
+       }
 
        return 3;
 }
@@ -190,6 +252,37 @@ int r600_bc_add_output(struct r600_bc *bc, const struct r600_bc_output *output)
 {
        int r;
 
+       if (bc->cf_last && (bc->cf_last->inst == output->inst ||
+               (bc->cf_last->inst == BC_INST(bc, V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT) &&
+               output->inst == BC_INST(bc, V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE))) &&
+               output->type == bc->cf_last->output.type &&
+               output->elem_size == bc->cf_last->output.elem_size &&
+               output->swizzle_x == bc->cf_last->output.swizzle_x &&
+               output->swizzle_y == bc->cf_last->output.swizzle_y &&
+               output->swizzle_z == bc->cf_last->output.swizzle_z &&
+               output->swizzle_w == bc->cf_last->output.swizzle_w &&
+               (output->burst_count + bc->cf_last->output.burst_count) <= 16) {
+
+               if ((output->gpr + output->burst_count) == bc->cf_last->output.gpr &&
+                       (output->array_base + output->burst_count) == bc->cf_last->output.array_base) {
+
+                       bc->cf_last->output.end_of_program |= output->end_of_program;
+                       bc->cf_last->output.inst = output->inst;
+                       bc->cf_last->output.gpr = output->gpr;
+                       bc->cf_last->output.array_base = output->array_base;
+                       bc->cf_last->output.burst_count += output->burst_count;
+                       return 0;
+
+               } else if (output->gpr == (bc->cf_last->output.gpr + bc->cf_last->output.burst_count) &&
+                       output->array_base == (bc->cf_last->output.array_base + bc->cf_last->output.burst_count)) {
+
+                       bc->cf_last->output.end_of_program |= output->end_of_program;
+                       bc->cf_last->output.inst = output->inst;
+                       bc->cf_last->output.burst_count += output->burst_count;
+                       return 0;
+               }
+       }
+
        r = r600_bc_add_cf(bc);
        if (r)
                return r;
@@ -199,112 +292,221 @@ int r600_bc_add_output(struct r600_bc *bc, const struct r600_bc_output *output)
 }
 
 /* alu instructions that can ony exits once per group */
-static int is_alu_once_inst(struct r600_bc_alu *alu)
+static int is_alu_once_inst(struct r600_bc *bc, struct r600_bc_alu *alu)
+{
+       switch (bc->chiprev) {
+       case CHIPREV_R600:
+       case CHIPREV_R700:
+               return !alu->is_op3 && (
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLE ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLNE ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT_UINT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE_UINT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLE_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLNE_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_UINT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_UINT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_INV ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_POP ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_CLR ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_RESTORE ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_PUSH ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_PUSH ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_PUSH ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_PUSH ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_PUSH_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_PUSH_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_PUSH_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_PUSH_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETLT_PUSH_INT ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETLE_PUSH_INT);
+       case CHIPREV_EVERGREEN:
+       default:
+               return !alu->is_op3 && (
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLE ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLNE ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT_UINT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE_UINT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLE_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLNE_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_UINT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_UINT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_INV ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_POP ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_CLR ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_RESTORE ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_PUSH ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_PUSH ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_PUSH ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_PUSH ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_PUSH_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_PUSH_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_PUSH_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_PUSH_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETLT_PUSH_INT ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETLE_PUSH_INT);
+       }
+}
+
+static int is_alu_reduction_inst(struct r600_bc *bc, struct r600_bc_alu *alu)
 {
-       return !alu->is_op3 && (
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLE ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLNE ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT_UINT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE_UINT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLE_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGE_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLNE_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_UINT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_UINT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_INV ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_POP ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_CLR ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SET_RESTORE ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_PUSH ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_PUSH ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_PUSH ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_PUSH ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_PUSH_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGT_PUSH_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETGE_PUSH_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_PUSH_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETLT_PUSH_INT ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETLE_PUSH_INT);
+       switch (bc->chiprev) {
+       case CHIPREV_R600:
+       case CHIPREV_R700:
+               return !alu->is_op3 && (
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_CUBE ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4 ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4_IEEE ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MAX4);
+       case CHIPREV_EVERGREEN:
+       default:
+               return !alu->is_op3 && (
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_CUBE ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4 ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4_IEEE ||
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MAX4);
+       }
 }
 
-static int is_alu_reduction_inst(struct r600_bc_alu *alu)
+static int is_alu_cube_inst(struct r600_bc *bc, struct r600_bc_alu *alu)
 {
-       return !alu->is_op3 && (
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_CUBE ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4 ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_DOT4_IEEE ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MAX4);
+       switch (bc->chiprev) {
+       case CHIPREV_R600:
+       case CHIPREV_R700:
+               return !alu->is_op3 &&
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_CUBE;
+       case CHIPREV_EVERGREEN:
+       default:
+               return !alu->is_op3 &&
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_CUBE;
+       }
 }
 
-static int is_alu_mova_inst(struct r600_bc_alu *alu)
+static int is_alu_mova_inst(struct r600_bc *bc, struct r600_bc_alu *alu)
 {
-       return !alu->is_op3 && (
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA_FLOOR ||
-               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA_INT);
+       switch (bc->chiprev) {
+       case CHIPREV_R600:
+       case CHIPREV_R700:
+               return !alu->is_op3 && (
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA_FLOOR ||
+                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA_INT);
+       case CHIPREV_EVERGREEN:
+       default:
+               return !alu->is_op3 && (
+                       alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MOVA_INT);
+       }
 }
 
 /* alu instructions that can only execute on the vector unit */
-static int is_alu_vec_unit_inst(struct r600_bc_alu *alu)
+static int is_alu_vec_unit_inst(struct r600_bc *bc, struct r600_bc_alu *alu)
 {
-       return is_alu_reduction_inst(alu) ||
-               is_alu_mova_inst(alu);
+       return is_alu_reduction_inst(bc, alu) ||
+               is_alu_mova_inst(bc, alu);
 }
 
 /* alu instructions that can only execute on the trans unit */
-static int is_alu_trans_unit_inst(struct r600_bc_alu *alu)
+static int is_alu_trans_unit_inst(struct r600_bc *bc, struct r600_bc_alu *alu)
 {
-       if(!alu->is_op3)
-               return alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ASHR_INT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLT_TO_INT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_INT_TO_FLT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LSHL_INT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LSHR_INT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULHI_INT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULHI_UINT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULLO_INT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULLO_UINT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_INT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_UINT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_UINT_TO_FLT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_COS ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_EXP_IEEE ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_CLAMPED ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_IEEE ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_CLAMPED ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_FF ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_IEEE ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_CLAMPED ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_FF ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_IEEE ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SIN ||
-                       alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SQRT_IEEE;
-       else
-               return alu->inst == V_SQ_ALU_WORD1_OP3_SQ_OP3_INST_MUL_LIT ||
-                       alu->inst == V_SQ_ALU_WORD1_OP3_SQ_OP3_INST_MUL_LIT_D2 ||
-                       alu->inst == V_SQ_ALU_WORD1_OP3_SQ_OP3_INST_MUL_LIT_M2 ||
-                       alu->inst == V_SQ_ALU_WORD1_OP3_SQ_OP3_INST_MUL_LIT_M4;
+       switch (bc->chiprev) {
+       case CHIPREV_R600:
+       case CHIPREV_R700:
+               if (!alu->is_op3)
+                       return alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ASHR_INT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLT_TO_INT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_INT_TO_FLT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LSHL_INT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LSHR_INT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULHI_INT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULHI_UINT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULLO_INT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULLO_UINT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_INT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_UINT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_UINT_TO_FLT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_COS ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_EXP_IEEE ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_CLAMPED ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_IEEE ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_CLAMPED ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_FF ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_IEEE ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_CLAMPED ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_FF ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_IEEE ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SIN ||
+                               alu->inst == V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SQRT_IEEE;
+               else
+                       return alu->inst == V_SQ_ALU_WORD1_OP3_SQ_OP3_INST_MUL_LIT ||
+                               alu->inst == V_SQ_ALU_WORD1_OP3_SQ_OP3_INST_MUL_LIT_D2 ||
+                               alu->inst == V_SQ_ALU_WORD1_OP3_SQ_OP3_INST_MUL_LIT_M2 ||
+                               alu->inst == V_SQ_ALU_WORD1_OP3_SQ_OP3_INST_MUL_LIT_M4;
+       case CHIPREV_EVERGREEN:
+       default:
+               if (!alu->is_op3)
+                       /* Note that FLT_TO_INT* instructions are vector instructions
+                        * on Evergreen, despite what the documentation says. */
+                       return alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ASHR_INT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_INT_TO_FLT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LSHL_INT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LSHR_INT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULHI_INT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULHI_UINT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULLO_INT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULLO_UINT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_INT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_UINT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_UINT_TO_FLT ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_COS ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_EXP_IEEE ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_CLAMPED ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_LOG_IEEE ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_CLAMPED ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_FF ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIP_IEEE ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_CLAMPED ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_FF ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_RECIPSQRT_IEEE ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SIN ||
+                               alu->inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SQRT_IEEE;
+               else
+                       return alu->inst == EG_V_SQ_ALU_WORD1_OP3_SQ_OP3_INST_MUL_LIT;
+       }
 }
 
 /* alu instructions that can execute on any unit */
-static int is_alu_any_unit_inst(struct r600_bc_alu *alu)
+static int is_alu_any_unit_inst(struct r600_bc *bc, struct r600_bc_alu *alu)
 {
-       return !is_alu_vec_unit_inst(alu) &&
-               !is_alu_trans_unit_inst(alu);
+       return !is_alu_vec_unit_inst(bc, alu) &&
+               !is_alu_trans_unit_inst(bc, alu);
 }
 
-static int assign_alu_units(struct r600_bc_alu *alu_first, struct r600_bc_alu *assignment[5])
+static int assign_alu_units(struct r600_bc *bc, struct r600_bc_alu *alu_first,
+                           struct r600_bc_alu *assignment[5])
 {
        struct r600_bc_alu *alu;
        unsigned i, chan, trans;
@@ -314,9 +516,9 @@ static int assign_alu_units(struct r600_bc_alu *alu_first, struct r600_bc_alu *a
 
        for (alu = alu_first; alu; alu = LIST_ENTRY(struct r600_bc_alu, alu->list.next, list)) {
                chan = alu->dst.chan;
-               if (is_alu_trans_unit_inst(alu))
+               if (is_alu_trans_unit_inst(bc, alu))
                        trans = 1;
-               else if (is_alu_vec_unit_inst(alu))
+               else if (is_alu_vec_unit_inst(bc, alu))
                        trans = 0;
                else if (assignment[chan])
                        trans = 1; // assume ALU_INST_PREFER_VECTOR
@@ -349,7 +551,7 @@ struct alu_bank_swizzle {
        int     hw_cfile_elem[4];
 };
 
-const unsigned cycle_for_bank_swizzle_vec[][3] = {
+static const unsigned cycle_for_bank_swizzle_vec[][3] = {
        [SQ_ALU_VEC_012] = { 0, 1, 2 },
        [SQ_ALU_VEC_021] = { 0, 2, 1 },
        [SQ_ALU_VEC_120] = { 1, 2, 0 },
@@ -358,7 +560,7 @@ const unsigned cycle_for_bank_swizzle_vec[][3] = {
        [SQ_ALU_VEC_210] = { 2, 1, 0 }
 };
 
-const unsigned cycle_for_bank_swizzle_scl[][3] = {
+static const unsigned cycle_for_bank_swizzle_scl[][3] = {
        [SQ_ALU_SCL_210] = { 2, 1, 0 },
        [SQ_ALU_SCL_122] = { 1, 2, 2 },
        [SQ_ALU_SCL_212] = { 2, 1, 2 },
@@ -377,7 +579,7 @@ static void init_bank_swizzle(struct alu_bank_swizzle *bs)
        for (i = 0; i < 4; i++)
                bs->hw_cfile_elem[i] = -1;
 }
+
 static int reserve_gpr(struct alu_bank_swizzle *bs, unsigned sel, unsigned chan, unsigned cycle)
 {
        if (bs->hw_gpr[cycle][chan] == -1)
@@ -388,29 +590,27 @@ static int reserve_gpr(struct alu_bank_swizzle *bs, unsigned sel, unsigned chan,
        }
        return 0;
 }
-static int reserve_cfile(struct alu_bank_swizzle *bs, unsigned sel, unsigned chan)
+
+static int reserve_cfile(struct r600_bc *bc, struct alu_bank_swizzle *bs, unsigned sel, unsigned chan)
 {
-       int res, resmatch = -1, resempty = -1;
-       for (res = 3; res >= 0; --res) {
-               if (bs->hw_cfile_addr[res] == -1)
-                       resempty = res;
-               else if (bs->hw_cfile_addr[res] == sel &&
-                       bs->hw_cfile_elem[res] == chan)
-                       resmatch = res;
+       int res, num_res = 4;
+       if (bc->chiprev >= CHIPREV_R700) {
+               num_res = 2;
+               chan /= 2;
        }
-       if (resmatch != -1)
-               return 0; // Read for this scalar element already reserved, nothing to do here.
-       else if (resempty != -1) {
-               bs->hw_cfile_addr[resempty] = sel;
-               bs->hw_cfile_elem[resempty] = chan;
-       } else {
-               // All cfile read ports are used, cannot reference vector element
-               return -1;
+       for (res = 0; res < num_res; ++res) {
+               if (bs->hw_cfile_addr[res] == -1) {
+                       bs->hw_cfile_addr[res] = sel;
+                       bs->hw_cfile_elem[res] = chan;
+                       return 0;
+               } else if (bs->hw_cfile_addr[res] == sel &&
+                       bs->hw_cfile_elem[res] == chan)
+                       return 0; // Read for this scalar element already reserved, nothing to do here.
        }
-       return 0;       
+       // All cfile read ports are used, cannot reference vector element
+       return -1;
 }
+
 static int is_gpr(unsigned sel)
 {
        return (sel >= 0 && sel <= 127);
@@ -425,26 +625,27 @@ static int is_cfile(unsigned sel)
                (sel > 511 && sel < 4607) || // Kcache before translate
                (sel > 127 && sel < 192); // Kcache after translate
 }
+
 static int is_const(int sel)
 {
        return is_cfile(sel) ||
-               (sel >= V_SQ_ALU_SRC_0 && 
+               (sel >= V_SQ_ALU_SRC_0 &&
                sel <= V_SQ_ALU_SRC_LITERAL);
 }
-static int check_vector(struct r600_bc_alu *alu, struct alu_bank_swizzle *bs, int bank_swizzle)
+
+static int check_vector(struct r600_bc *bc, struct r600_bc_alu *alu,
+                       struct alu_bank_swizzle *bs, int bank_swizzle)
 {
        int r, src, num_src, sel, elem, cycle;
-       num_src = r600_bc_get_num_operands(alu);
+
+       num_src = r600_bc_get_num_operands(bc, alu);
        for (src = 0; src < num_src; src++) {
                sel = alu->src[src].sel;
                elem = alu->src[src].chan;
                if (is_gpr(sel)) {
                        cycle = cycle_for_bank_swizzle_vec[bank_swizzle][src];
                        if (src == 1 && sel == alu->src[0].sel && elem == alu->src[0].chan)
-                               // Nothing to do; special-case optimization, 
+                               // Nothing to do; special-case optimization,
                                // second source uses first source’s reservation
                                continue;
                        else {
@@ -453,7 +654,7 @@ static int check_vector(struct r600_bc_alu *alu, struct alu_bank_swizzle *bs, in
                                        return r;
                        }
                } else if (is_cfile(sel)) {
-                       r = reserve_cfile(bs, sel, elem);
+                       r = reserve_cfile(bc, bs, sel, elem);
                        if (r)
                                return r;
                }
@@ -461,12 +662,13 @@ static int check_vector(struct r600_bc_alu *alu, struct alu_bank_swizzle *bs, in
        }
        return 0;
 }
-static int check_scalar(struct r600_bc_alu *alu, struct alu_bank_swizzle *bs, int bank_swizzle)
+
+static int check_scalar(struct r600_bc *bc, struct r600_bc_alu *alu,
+                       struct alu_bank_swizzle *bs, int bank_swizzle)
 {
        int r, src, num_src, const_count, sel, elem, cycle;
-       num_src = r600_bc_get_num_operands(alu);
+
+       num_src = r600_bc_get_num_operands(bc, alu);
        for (const_count = 0, src = 0; src < num_src; ++src) {
                sel = alu->src[src].sel;
                elem = alu->src[src].chan;
@@ -474,12 +676,12 @@ static int check_scalar(struct r600_bc_alu *alu, struct alu_bank_swizzle *bs, in
                        if (const_count >= 2)
                                // More than two references to a constant in
                                // transcendental operation.
-                               return -1; 
+                               return -1;
                        else
                                const_count++;
                }
                if (is_cfile(sel)) {
-                       r = reserve_cfile(bs, sel, elem);
+                       r = reserve_cfile(bc, bs, sel, elem);
                        if (r)
                                return r;
                }
@@ -503,12 +705,13 @@ static int check_scalar(struct r600_bc_alu *alu, struct alu_bank_swizzle *bs, in
        return 0;
 }
 
-static int check_and_set_bank_swizzle(struct r600_bc_alu *slots[5])
+static int check_and_set_bank_swizzle(struct r600_bc *bc,
+                                     struct r600_bc_alu *slots[5])
 {
        struct alu_bank_swizzle bs;
        int bank_swizzle[5];
        int i, r = 0, forced = 0;
+
        for (i = 0; i < 5; i++)
                if (slots[i] && slots[i]->bank_swizzle_force) {
                        slots[i]->bank_swizzle = slots[i]->bank_swizzle_force;
@@ -527,13 +730,13 @@ static int check_and_set_bank_swizzle(struct r600_bc_alu *slots[5])
                init_bank_swizzle(&bs);
                for (i = 0; i < 4; i++) {
                        if (slots[i]) {
-                               r = check_vector(slots[i], &bs, bank_swizzle[i]);
+                               r = check_vector(bc, slots[i], &bs, bank_swizzle[i]);
                                if (r)
                                        break;
                        }
                }
                if (!r && slots[4]) {
-                       r = check_scalar(slots[4], &bs, bank_swizzle[4]);
+                       r = check_scalar(bc, slots[4], &bs, bank_swizzle[4]);
                }
                if (!r) {
                        for (i = 0; i < 5; i++) {
@@ -556,25 +759,27 @@ static int check_and_set_bank_swizzle(struct r600_bc_alu *slots[5])
        return -1;
 }
 
-static int replace_gpr_with_pv_ps(struct r600_bc_alu *slots[5], struct r600_bc_alu *alu_prev)
+static int replace_gpr_with_pv_ps(struct r600_bc *bc,
+                                 struct r600_bc_alu *slots[5], struct r600_bc_alu *alu_prev)
 {
        struct r600_bc_alu *prev[5];
        int gpr[5], chan[5];
        int i, j, r, src, num_src;
-       
-       r = assign_alu_units(alu_prev, prev);
+
+       r = assign_alu_units(bc, alu_prev, prev);
        if (r)
                return r;
 
        for (i = 0; i < 5; ++i) {
                if(prev[i] && prev[i]->dst.write && !prev[i]->dst.rel) {
                        gpr[i] = prev[i]->dst.sel;
-                       if (is_alu_reduction_inst(prev[i]))
+                       /* cube writes more than PV.X */
+                       if (!is_alu_cube_inst(bc, prev[i]) && is_alu_reduction_inst(bc, prev[i]))
                                chan[i] = 0;
                        else
                                chan[i] = prev[i]->dst.chan;
                } else
-                       gpr[i] = -1;            
+                       gpr[i] = -1;
        }
 
        for (i = 0; i < 5; ++i) {
@@ -582,7 +787,7 @@ static int replace_gpr_with_pv_ps(struct r600_bc_alu *slots[5], struct r600_bc_a
                if(!alu)
                        continue;
 
-               num_src = r600_bc_get_num_operands(alu);
+               num_src = r600_bc_get_num_operands(bc, alu);
                for (src = 0; src < num_src; ++src) {
                        if (!is_gpr(alu->src[src].sel) || alu->src[src].rel)
                                continue;
@@ -641,14 +846,15 @@ void r600_bc_special_constants(u32 value, unsigned *sel, unsigned *neg)
 }
 
 /* compute how many literal are needed */
-static int r600_bc_alu_nliterals(struct r600_bc_alu *alu, uint32_t literal[4], unsigned *nliteral)
+static int r600_bc_alu_nliterals(struct r600_bc *bc, struct r600_bc_alu *alu,
+                                uint32_t literal[4], unsigned *nliteral)
 {
-       unsigned num_src = r600_bc_get_num_operands(alu);
+       unsigned num_src = r600_bc_get_num_operands(bc, alu);
        unsigned i, j;
 
        for (i = 0; i < num_src; ++i) {
                if (alu->src[i].sel == V_SQ_ALU_SRC_LITERAL) {
-                       uint32_t value = alu->src[i].value[alu->src[i].chan];
+                       uint32_t value = alu->src[i].value;
                        unsigned found = 0;
                        for (j = 0; j < *nliteral; ++j) {
                                if (literal[j] == value) {
@@ -666,14 +872,16 @@ static int r600_bc_alu_nliterals(struct r600_bc_alu *alu, uint32_t literal[4], u
        return 0;
 }
 
-static void r600_bc_alu_adjust_literals(struct r600_bc_alu *alu, uint32_t literal[4], unsigned nliteral)
+static void r600_bc_alu_adjust_literals(struct r600_bc *bc,
+                                       struct r600_bc_alu *alu,
+                                       uint32_t literal[4], unsigned nliteral)
 {
-       unsigned num_src = r600_bc_get_num_operands(alu);
+       unsigned num_src = r600_bc_get_num_operands(bc, alu);
        unsigned i, j;
 
        for (i = 0; i < num_src; ++i) {
                if (alu->src[i].sel == V_SQ_ALU_SRC_LITERAL) {
-                       uint32_t value = alu->src[i].value[alu->src[i].chan];
+                       uint32_t value = alu->src[i].value;
                        for (j = 0; j < nliteral; ++j) {
                                if (literal[j] == value) {
                                        alu->src[i].chan = j;
@@ -684,40 +892,53 @@ static void r600_bc_alu_adjust_literals(struct r600_bc_alu *alu, uint32_t litera
        }
 }
 
-static int merge_inst_groups(struct r600_bc *bc, struct r600_bc_alu *slots[5], struct r600_bc_alu *alu_prev)
+static int merge_inst_groups(struct r600_bc *bc, struct r600_bc_alu *slots[5],
+                            struct r600_bc_alu *alu_prev)
 {
        struct r600_bc_alu *prev[5];
        struct r600_bc_alu *result[5] = { NULL };
-       
-       uint32_t literal[4];
-       unsigned nliteral = 0;
+
+       uint32_t literal[4], prev_literal[4];
+       unsigned nliteral = 0, prev_nliteral = 0;
 
        int i, j, r, src, num_src;
        int num_once_inst = 0;
+       int have_mova = 0, have_rel = 0;
 
-       r = assign_alu_units(alu_prev, prev);
+       r = assign_alu_units(bc, alu_prev, prev);
        if (r)
                return r;
 
        for (i = 0; i < 5; ++i) {
+               struct r600_bc_alu *alu;
+
                /* check number of literals */
-               if (prev[i] && r600_bc_alu_nliterals(prev[i], literal, &nliteral))
-                       return 0;
-               if (slots[i] && r600_bc_alu_nliterals(slots[i], literal, &nliteral))
+               if (prev[i]) {
+                       if (r600_bc_alu_nliterals(bc, prev[i], literal, &nliteral))
+                               return 0;
+                       if (r600_bc_alu_nliterals(bc, prev[i], prev_literal, &prev_nliteral))
+                               return 0;
+                       if (is_alu_mova_inst(bc, prev[i])) {
+                               if (have_rel)
+                                       return 0;
+                               have_mova = 1;
+                       }
+                       num_once_inst += is_alu_once_inst(bc, prev[i]);
+               }
+               if (slots[i] && r600_bc_alu_nliterals(bc, slots[i], literal, &nliteral))
                        return 0;
 
                // let's check used slots
                if (prev[i] && !slots[i]) {
                        result[i] = prev[i];
-                       num_once_inst += is_alu_once_inst(prev[i]);
                        continue;
                } else if (prev[i] && slots[i]) {
                        if (result[4] == NULL && prev[4] == NULL && slots[4] == NULL) {
                                // trans unit is still free try to use it
-                               if (is_alu_any_unit_inst(slots[i])) {
+                               if (is_alu_any_unit_inst(bc, slots[i])) {
                                        result[i] = prev[i];
                                        result[4] = slots[i];
-                               } else if (is_alu_any_unit_inst(prev[i])) {
+                               } else if (is_alu_any_unit_inst(bc, prev[i])) {
                                        result[i] = slots[i];
                                        result[4] = prev[i];
                                } else
@@ -726,15 +947,21 @@ static int merge_inst_groups(struct r600_bc *bc, struct r600_bc_alu *slots[5], s
                                return 0;
                } else if(!slots[i]) {
                        continue;
-               } else 
+               } else
                        result[i] = slots[i];
 
                // let's check source gprs
-               struct r600_bc_alu *alu = slots[i];
-               num_once_inst += is_alu_once_inst(alu);
+               alu = slots[i];
+               num_once_inst += is_alu_once_inst(bc, alu);
 
-               num_src = r600_bc_get_num_operands(alu);
+               num_src = r600_bc_get_num_operands(bc, alu);
                for (src = 0; src < num_src; ++src) {
+                       if (alu->src[src].rel) {
+                               if (have_mova)
+                                       return 0;
+                               have_rel = 1;
+                       }
+
                        // constants doesn't matter
                        if (!is_gpr(alu->src[src].sel))
                                continue;
@@ -757,12 +984,15 @@ static int merge_inst_groups(struct r600_bc *bc, struct r600_bc_alu *slots[5], s
                return 0;
 
        /* check if the result can still be swizzlet */
-       r = check_and_set_bank_swizzle(result);
+       r = check_and_set_bank_swizzle(bc, result);
        if (r)
                return 0;
 
        /* looks like everything worked out right, apply the changes */
 
+       /* undo adding previus literals */
+       bc->cf_last->ndw -= align(prev_nliteral, 2);
+
        /* sort instructions */
        for (i = 0; i < 5; ++i) {
                slots[i] = result[i];
@@ -948,19 +1178,13 @@ int r600_bc_add_alu_type(struct r600_bc *bc, const struct r600_bc_alu *alu, int
        if (!bc->cf_last->curr_bs_head) {
                bc->cf_last->curr_bs_head = nalu;
        }
-       /* at most 128 slots, one add alu can add 5 slots + 4 constants(2 slots)
-        * worst case */
-       if (nalu->last && (bc->cf_last->ndw >> 1) >= 120) {
-               bc->force_add_cf = 1;
-       }
        /* number of gpr == the last gpr used in any alu */
        for (i = 0; i < 3; i++) {
                if (nalu->src[i].sel >= bc->ngpr && nalu->src[i].sel < 128) {
                        bc->ngpr = nalu->src[i].sel + 1;
                }
                if (nalu->src[i].sel == V_SQ_ALU_SRC_LITERAL)
-                       r600_bc_special_constants(
-                               nalu->src[i].value[nalu->src[i].chan], 
+                       r600_bc_special_constants(nalu->src[i].value,
                                &nalu->src[i].sel, &nalu->src[i].neg);
        }
        if (nalu->dst.sel >= bc->ngpr) {
@@ -973,8 +1197,10 @@ int r600_bc_add_alu_type(struct r600_bc *bc, const struct r600_bc_alu *alu, int
 
        /* process cur ALU instructions for bank swizzle */
        if (nalu->last) {
+               uint32_t literal[4];
+               unsigned nliteral;
                struct r600_bc_alu *slots[5];
-               r = assign_alu_units(bc->cf_last->curr_bs_head, slots);
+               r = assign_alu_units(bc, bc->cf_last->curr_bs_head, slots);
                if (r)
                        return r;
 
@@ -985,15 +1211,30 @@ int r600_bc_add_alu_type(struct r600_bc *bc, const struct r600_bc_alu *alu, int
                }
 
                if (bc->cf_last->prev_bs_head) {
-                       r = replace_gpr_with_pv_ps(slots, bc->cf_last->prev_bs_head);
+                       r = replace_gpr_with_pv_ps(bc, slots, bc->cf_last->prev_bs_head);
                        if (r)
                                return r;
                }
 
-               r = check_and_set_bank_swizzle(slots);
+               r = check_and_set_bank_swizzle(bc, slots);
                if (r)
                        return r;
 
+               for (i = 0, nliteral = 0; i < 5; i++) {
+                       if (slots[i]) {
+                               r = r600_bc_alu_nliterals(bc, slots[i], literal, &nliteral);
+                               if (r)
+                                       return r;
+                       }
+               }
+               bc->cf_last->ndw += align(nliteral, 2);
+
+               /* at most 128 slots, one add alu can add 5 slots + 4 constants(2 slots)
+                * worst case */
+               if ((bc->cf_last->ndw >> 1) >= 120) {
+                       bc->force_add_cf = 1;
+               }
+
                bc->cf_last->prev2_bs_head = bc->cf_last->prev_bs_head;
                bc->cf_last->prev_bs_head = bc->cf_last->curr_bs_head;
                bc->cf_last->curr_bs_head = NULL;
@@ -1006,6 +1247,24 @@ int r600_bc_add_alu(struct r600_bc *bc, const struct r600_bc_alu *alu)
        return r600_bc_add_alu_type(bc, alu, BC_INST(bc, V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU));
 }
 
+static unsigned r600_bc_num_tex_and_vtx_instructions(const struct r600_bc *bc)
+{
+       switch (bc->chiprev) {
+       case CHIPREV_R600:
+               return 8;
+
+       case CHIPREV_R700:
+               return 16;
+
+       case CHIPREV_EVERGREEN:
+               return 64;
+
+       default:
+               R600_ERR("Unknown chiprev %d.\n", bc->chiprev);
+               return 8;
+       }
+}
+
 int r600_bc_add_vtx(struct r600_bc *bc, const struct r600_bc_vtx *vtx)
 {
        struct r600_bc_vtx *nvtx = r600_bc_vtx();
@@ -1031,7 +1290,7 @@ int r600_bc_add_vtx(struct r600_bc *bc, const struct r600_bc_vtx *vtx)
        /* each fetch use 4 dwords */
        bc->cf_last->ndw += 4;
        bc->ndw += 4;
-       if ((bc->cf_last->ndw / 4) > 7)
+       if ((bc->cf_last->ndw / 4) >= r600_bc_num_tex_and_vtx_instructions(bc))
                bc->force_add_cf = 1;
        return 0;
 }
@@ -1045,6 +1304,18 @@ int r600_bc_add_tex(struct r600_bc *bc, const struct r600_bc_tex *tex)
                return -ENOMEM;
        memcpy(ntex, tex, sizeof(struct r600_bc_tex));
 
+       /* we can't fetch data und use it as texture lookup address in the same TEX clause */
+       if (bc->cf_last != NULL &&
+               bc->cf_last->inst == V_SQ_CF_WORD1_SQ_CF_INST_TEX) {
+               struct r600_bc_tex *ttex;
+               LIST_FOR_EACH_ENTRY(ttex, &bc->cf_last->tex, list) {
+                       if (ttex->dst_gpr == ntex->src_gpr) {
+                               bc->force_add_cf = 1;
+                               break;
+                       }
+               }
+       }
+
        /* cf can contains only alu or only vtx or only tex */
        if (bc->cf_last == NULL ||
                bc->cf_last->inst != V_SQ_CF_WORD1_SQ_CF_INST_TEX ||
@@ -1066,7 +1337,7 @@ int r600_bc_add_tex(struct r600_bc *bc, const struct r600_bc_tex *tex)
        /* each texture fetch use 4 dwords */
        bc->cf_last->ndw += 4;
        bc->ndw += 4;
-       if ((bc->cf_last->ndw / 4) > 7)
+       if ((bc->cf_last->ndw / 4) >= r600_bc_num_tex_and_vtx_instructions(bc))
                bc->force_add_cf = 1;
        return 0;
 }
@@ -1086,31 +1357,8 @@ int r600_bc_add_cfinst(struct r600_bc *bc, int inst)
 /* common to all 3 families */
 static int r600_bc_vtx_build(struct r600_bc *bc, struct r600_bc_vtx *vtx, unsigned id)
 {
-       unsigned fetch_resource_start = 0;
-
-       /* check if we are fetch shader */
-                       /* fetch shader can also access vertex resource,
-                        * first fetch shader resource is at 160
-                        */
-       if (bc->type == -1) {
-               switch (bc->chiprev) {
-               /* r600 */
-               case CHIPREV_R600:
-               /* r700 */
-               case CHIPREV_R700:
-                       fetch_resource_start = 160;
-                       break;
-               /* evergreen */
-               case CHIPREV_EVERGREEN:
-                       fetch_resource_start = 0;
-                       break;
-               default:
-                       fprintf(stderr,  "%s:%s:%d unknown chiprev %d\n",
-                               __FILE__, __func__, __LINE__, bc->chiprev);
-                       break;
-               }
-       }
-       bc->bytecode[id++] = S_SQ_VTX_WORD0_BUFFER_ID(vtx->buffer_id + fetch_resource_start) |
+       bc->bytecode[id++] = S_SQ_VTX_WORD0_BUFFER_ID(vtx->buffer_id) |
+                       S_SQ_VTX_WORD0_FETCH_TYPE(vtx->fetch_type) |
                        S_SQ_VTX_WORD0_SRC_GPR(vtx->src_gpr) |
                        S_SQ_VTX_WORD0_SRC_SEL_X(vtx->src_sel_x) |
                        S_SQ_VTX_WORD0_MEGA_FETCH_COUNT(vtx->mega_fetch_count);
@@ -1124,7 +1372,8 @@ static int r600_bc_vtx_build(struct r600_bc *bc, struct r600_bc_vtx *vtx, unsign
                                S_SQ_VTX_WORD1_FORMAT_COMP_ALL(vtx->format_comp_all) |
                                S_SQ_VTX_WORD1_SRF_MODE_ALL(vtx->srf_mode_all) |
                                S_SQ_VTX_WORD1_GPR_DST_GPR(vtx->dst_gpr);
-       bc->bytecode[id++] = S_SQ_VTX_WORD2_MEGA_FETCH(1);
+       bc->bytecode[id++] = S_SQ_VTX_WORD2_OFFSET(vtx->offset) |
+                               S_SQ_VTX_WORD2_MEGA_FETCH(1);
        bc->bytecode[id++] = 0;
        return 0;
 }
@@ -1201,6 +1450,14 @@ static int r600_bc_alu_build(struct r600_bc *bc, struct r600_bc_alu *alu, unsign
        return 0;
 }
 
+static void r600_bc_cf_vtx_build(uint32_t *bytecode, const struct r600_bc_cf *cf)
+{
+       *bytecode++ = S_SQ_CF_WORD0_ADDR(cf->addr >> 1);
+       *bytecode++ = S_SQ_CF_WORD1_CF_INST(cf->inst) |
+                       S_SQ_CF_WORD1_BARRIER(1) |
+                       S_SQ_CF_WORD1_COUNT((cf->ndw / 4) - 1);
+}
+
 /* common for r600/r700 - eg in eg_asm.c */
 static int r600_bc_cf_build(struct r600_bc *bc, struct r600_bc_cf *cf)
 {
@@ -1227,10 +1484,10 @@ static int r600_bc_cf_build(struct r600_bc *bc, struct r600_bc_cf *cf)
        case V_SQ_CF_WORD1_SQ_CF_INST_TEX:
        case V_SQ_CF_WORD1_SQ_CF_INST_VTX:
        case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC:
-               bc->bytecode[id++] = S_SQ_CF_WORD0_ADDR(cf->addr >> 1);
-               bc->bytecode[id++] = S_SQ_CF_WORD1_CF_INST(cf->inst) |
-                                       S_SQ_CF_WORD1_BARRIER(1) |
-                                       S_SQ_CF_WORD1_COUNT((cf->ndw / 4) - 1);
+               if (bc->chiprev == CHIPREV_R700)
+                       r700_bc_cf_vtx_build(&bc->bytecode[id], cf);
+               else
+                       r600_bc_cf_vtx_build(&bc->bytecode[id], cf);
                break;
        case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT:
        case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE:
@@ -1238,7 +1495,8 @@ static int r600_bc_cf_build(struct r600_bc *bc, struct r600_bc_cf *cf)
                        S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf->output.elem_size) |
                        S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf->output.array_base) |
                        S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf->output.type);
-               bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(cf->output.swizzle_x) |
+               bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(cf->output.burst_count - 1) |
+                       S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(cf->output.swizzle_x) |
                        S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(cf->output.swizzle_y) |
                        S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(cf->output.swizzle_z) |
                        S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(cf->output.swizzle_w) |
@@ -1295,16 +1553,6 @@ int r600_bc_build(struct r600_bc *bc)
                case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU_POP_AFTER << 3):
                case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU_POP2_AFTER << 3):
                case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU_PUSH_BEFORE << 3):
-                       nliteral = 0;
-                       LIST_FOR_EACH_ENTRY(alu, &cf->alu, list) {
-                               r = r600_bc_alu_nliterals(alu, literal, &nliteral);
-                               if (r)
-                                       return r;
-                               if (alu->last) {
-                                       cf->ndw += align(nliteral, 2);
-                                       nliteral = 0;
-                               }
-                       }
                        break;
                case V_SQ_CF_WORD1_SQ_CF_INST_TEX:
                case V_SQ_CF_WORD1_SQ_CF_INST_VTX:
@@ -1354,11 +1602,12 @@ int r600_bc_build(struct r600_bc *bc)
                case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU_POP2_AFTER << 3):
                case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU_PUSH_BEFORE << 3):
                        nliteral = 0;
+                       memset(literal, 0, sizeof(literal));
                        LIST_FOR_EACH_ENTRY(alu, &cf->alu, list) {
-                               r = r600_bc_alu_nliterals(alu, literal, &nliteral);
+                               r = r600_bc_alu_nliterals(bc, alu, literal, &nliteral);
                                if (r)
                                        return r;
-                               r600_bc_alu_adjust_literals(alu, literal, nliteral);
+                               r600_bc_alu_adjust_literals(bc, alu, literal, nliteral);
                                switch(bc->chiprev) {
                                case CHIPREV_R600:
                                        r = r600_bc_alu_build(bc, alu, addr);
@@ -1379,6 +1628,7 @@ int r600_bc_build(struct r600_bc *bc)
                                                bc->bytecode[addr++] = literal[i];
                                        }
                                        nliteral = 0;
+                                       memset(literal, 0, sizeof(literal));
                                }
                        }
                        break;
@@ -1528,9 +1778,9 @@ void r600_bc_dump(struct r600_bc *bc)
                        fprintf(stderr, "SWIZ_Y:%X ", cf->output.swizzle_y);
                        fprintf(stderr, "SWIZ_Z:%X ", cf->output.swizzle_z);
                        fprintf(stderr, "SWIZ_W:%X ", cf->output.swizzle_w);
-                       fprintf(stderr, "SWIZ_W:%X ", cf->output.swizzle_w);
                        fprintf(stderr, "BARRIER:%X ", cf->output.barrier);
                        fprintf(stderr, "INST:%d ", cf->output.inst);
+                       fprintf(stderr, "BURST_COUNT:%d ", cf->output.burst_count);
                        fprintf(stderr, "EOP:%X\n", cf->output.end_of_program);
                        break;
                case V_SQ_CF_WORD1_SQ_CF_INST_JUMP:
@@ -1555,7 +1805,7 @@ void r600_bc_dump(struct r600_bc *bc)
                id = cf->addr;
                nliteral = 0;
                LIST_FOR_EACH_ENTRY(alu, &cf->alu, list) {
-                       r600_bc_alu_nliterals(alu, literal, &nliteral);
+                       r600_bc_alu_nliterals(bc, alu, literal, &nliteral);
 
                        fprintf(stderr, "%04d %08X   ", id, bc->bytecode[id]);
                        fprintf(stderr, "SRC0(SEL:%d ", alu->src[0].sel);
@@ -1601,89 +1851,73 @@ void r600_bc_dump(struct r600_bc *bc)
                }
 
                LIST_FOR_EACH_ENTRY(tex, &cf->tex, list) {
-                       //TODO
+                       fprintf(stderr, "%04d %08X   ", id, bc->bytecode[id]);
+                       fprintf(stderr, "INST:%d ", tex->inst);
+                       fprintf(stderr, "RESOURCE_ID:%d ", tex->resource_id);
+                       fprintf(stderr, "SRC(GPR:%d ", tex->src_gpr);
+                       fprintf(stderr, "REL:%d)\n", tex->src_rel);
+                       id++;
+                       fprintf(stderr, "%04d %08X   ", id, bc->bytecode[id]);
+                       fprintf(stderr, "DST(GPR:%d ", tex->dst_gpr);
+                       fprintf(stderr, "REL:%d ", tex->dst_rel);
+                       fprintf(stderr, "SEL_X:%d ", tex->dst_sel_x);
+                       fprintf(stderr, "SEL_Y:%d ", tex->dst_sel_y);
+                       fprintf(stderr, "SEL_Z:%d ", tex->dst_sel_z);
+                       fprintf(stderr, "SEL_W:%d) ", tex->dst_sel_w);
+                       fprintf(stderr, "LOD_BIAS:%d ", tex->lod_bias);
+                       fprintf(stderr, "COORD_TYPE_X:%d ", tex->coord_type_x);
+                       fprintf(stderr, "COORD_TYPE_Y:%d ", tex->coord_type_y);
+                       fprintf(stderr, "COORD_TYPE_Z:%d ", tex->coord_type_z);
+                       fprintf(stderr, "COORD_TYPE_W:%d\n", tex->coord_type_w);
+                       id++;
+                       fprintf(stderr, "%04d %08X   ", id, bc->bytecode[id]);
+                       fprintf(stderr, "OFFSET_X:%d ", tex->offset_x);
+                       fprintf(stderr, "OFFSET_Y:%d ", tex->offset_y);
+                       fprintf(stderr, "OFFSET_Z:%d ", tex->offset_z);
+                       fprintf(stderr, "SAMPLER_ID:%d ", tex->sampler_id);
+                       fprintf(stderr, "SRC(SEL_X:%d ", tex->src_sel_x);
+                       fprintf(stderr, "SEL_Y:%d ", tex->src_sel_y);
+                       fprintf(stderr, "SEL_Z:%d ", tex->src_sel_z);
+                       fprintf(stderr, "SEL_W:%d)\n", tex->src_sel_w);
+                       id++;
+                       fprintf(stderr, "%04d %08X   \n", id, bc->bytecode[id]);
+                       id++;
                }
 
                LIST_FOR_EACH_ENTRY(vtx, &cf->vtx, list) {
+                       fprintf(stderr, "%04d %08X   ", id, bc->bytecode[id]);
+                       fprintf(stderr, "INST:%d ", vtx->inst);
+                       fprintf(stderr, "FETCH_TYPE:%d ", vtx->fetch_type);
+                       fprintf(stderr, "BUFFER_ID:%d\n", vtx->buffer_id);
+                       id++;
+                       /* This assumes that no semantic fetches exist */
+                       fprintf(stderr, "%04d %08X   ", id, bc->bytecode[id]);
+                       fprintf(stderr, "SRC(GPR:%d ", vtx->src_gpr);
+                       fprintf(stderr, "SEL_X:%d) ", vtx->src_sel_x);
+                       fprintf(stderr, "MEGA_FETCH_COUNT:%d ", vtx->mega_fetch_count);
+                       fprintf(stderr, "DST(GPR:%d ", vtx->dst_gpr);
+                       fprintf(stderr, "SEL_X:%d ", vtx->dst_sel_x);
+                       fprintf(stderr, "SEL_Y:%d ", vtx->dst_sel_y);
+                       fprintf(stderr, "SEL_Z:%d ", vtx->dst_sel_z);
+                       fprintf(stderr, "SEL_W:%d) ", vtx->dst_sel_w);
+                       fprintf(stderr, "USE_CONST_FIELDS:%d ", vtx->use_const_fields);
+                       fprintf(stderr, "FORMAT(DATA:%d ", vtx->data_format);
+                       fprintf(stderr, "NUM:%d ", vtx->num_format_all);
+                       fprintf(stderr, "COMP:%d ", vtx->format_comp_all);
+                       fprintf(stderr, "MODE:%d)\n", vtx->srf_mode_all);
+                       id++;
+                       fprintf(stderr, "%04d %08X   ", id, bc->bytecode[id]);
+                       fprintf(stderr, "OFFSET:%d\n", vtx->offset);
                        //TODO
+                       id++;
+                       fprintf(stderr, "%04d %08X   \n", id, bc->bytecode[id]);
+                       id++;
                }
        }
 
        fprintf(stderr, "--------------------------------------\n");
 }
 
-void r600_cf_vtx(struct r600_vertex_element *ve, u32 *bytecode, unsigned count)
-{
-       struct r600_pipe_state *rstate;
-       unsigned i = 0;
-
-       if (count > 8) {
-               bytecode[i++] = S_SQ_CF_WORD0_ADDR(8 >> 1);
-               bytecode[i++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_VTX) |
-                                               S_SQ_CF_WORD1_BARRIER(1) |
-                                               S_SQ_CF_WORD1_COUNT(8 - 1);
-               bytecode[i++] = S_SQ_CF_WORD0_ADDR(40 >> 1);
-               bytecode[i++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_VTX) |
-                                               S_SQ_CF_WORD1_BARRIER(1) |
-                                               S_SQ_CF_WORD1_COUNT(count - 8 - 1);
-       } else {
-               bytecode[i++] = S_SQ_CF_WORD0_ADDR(8 >> 1);
-               bytecode[i++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_VTX) |
-                                               S_SQ_CF_WORD1_BARRIER(1) |
-                                               S_SQ_CF_WORD1_COUNT(count - 1);
-       }
-       bytecode[i++] = S_SQ_CF_WORD0_ADDR(0);
-       bytecode[i++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_RETURN) |
-                       S_SQ_CF_WORD1_BARRIER(1);
-
-       rstate = &ve->rstate;
-       rstate->id = R600_PIPE_STATE_FETCH_SHADER;
-       rstate->nregs = 0;
-       r600_pipe_state_add_reg(rstate, R_0288A4_SQ_PGM_RESOURCES_FS,
-                               0x00000000, 0xFFFFFFFF, NULL);
-       r600_pipe_state_add_reg(rstate, R_0288DC_SQ_PGM_CF_OFFSET_FS,
-                               0x00000000, 0xFFFFFFFF, NULL);
-       r600_pipe_state_add_reg(rstate, R_028894_SQ_PGM_START_FS,
-                               r600_bo_offset(ve->fetch_shader) >> 8,
-                               0xFFFFFFFF, ve->fetch_shader);
-}
-
-void r600_cf_vtx_tc(struct r600_vertex_element *ve, u32 *bytecode, unsigned count)
-{
-       struct r600_pipe_state *rstate;
-       unsigned i = 0;
-
-       if (count > 8) {
-               bytecode[i++] = S_SQ_CF_WORD0_ADDR(8 >> 1);
-               bytecode[i++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC) |
-                                               S_SQ_CF_WORD1_BARRIER(1) |
-                                               S_SQ_CF_WORD1_COUNT(8 - 1);
-               bytecode[i++] = S_SQ_CF_WORD0_ADDR(40 >> 1);
-               bytecode[i++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC) |
-                                               S_SQ_CF_WORD1_BARRIER(1) |
-                                               S_SQ_CF_WORD1_COUNT((count - 8) - 1);
-       } else {
-               bytecode[i++] = S_SQ_CF_WORD0_ADDR(8 >> 1);
-               bytecode[i++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC) |
-                                               S_SQ_CF_WORD1_BARRIER(1) |
-                                               S_SQ_CF_WORD1_COUNT(count - 1);
-       }
-       bytecode[i++] = S_SQ_CF_WORD0_ADDR(0);
-       bytecode[i++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_RETURN) |
-                       S_SQ_CF_WORD1_BARRIER(1);
-
-       rstate = &ve->rstate;
-       rstate->id = R600_PIPE_STATE_FETCH_SHADER;
-       rstate->nregs = 0;
-       r600_pipe_state_add_reg(rstate, R_0288A4_SQ_PGM_RESOURCES_FS,
-                               0x00000000, 0xFFFFFFFF, NULL);
-       r600_pipe_state_add_reg(rstate, R_0288DC_SQ_PGM_CF_OFFSET_FS,
-                               0x00000000, 0xFFFFFFFF, NULL);
-       r600_pipe_state_add_reg(rstate, R_028894_SQ_PGM_START_FS,
-                               r600_bo_offset(ve->fetch_shader) >> 8,
-                               0xFFFFFFFF, ve->fetch_shader);
-}
-
 static void r600_vertex_data_type(enum pipe_format pformat, unsigned *format,
                                unsigned *num_format, unsigned *format_comp)
 {
@@ -1707,7 +1941,7 @@ static void r600_vertex_data_type(enum pipe_format pformat, unsigned *format,
        }
 
        switch (desc->channel[i].type) {
-               /* Half-floats, floats, doubles */
+       /* Half-floats, floats, ints */
        case UTIL_FORMAT_TYPE_FLOAT:
                switch (desc->channel[i].size) {
                case 16:
@@ -1719,8 +1953,6 @@ static void r600_vertex_data_type(enum pipe_format pformat, unsigned *format,
                                *format = FMT_16_16_FLOAT;
                                break;
                        case 3:
-                               *format = FMT_16_16_16_FLOAT;
-                               break;
                        case 4:
                                *format = FMT_16_16_16_16_FLOAT;
                                break;
@@ -1760,8 +1992,6 @@ static void r600_vertex_data_type(enum pipe_format pformat, unsigned *format,
                                *format = FMT_8_8;
                                break;
                        case 3:
-                       //      *format = FMT_8_8_8; /* fails piglit draw-vertices test */
-                       //      break;
                        case 4:
                                *format = FMT_8_8_8_8;
                                break;
@@ -1776,8 +2006,6 @@ static void r600_vertex_data_type(enum pipe_format pformat, unsigned *format,
                                *format = FMT_16_16;
                                break;
                        case 3:
-                       //      *format = FMT_16_16_16; /* fails piglit draw-vertices test */
-                       //      break;
                        case 4:
                                *format = FMT_16_16_16_16;
                                break;
@@ -1822,37 +2050,19 @@ out_unknown:
 
 int r600_vertex_elements_build_fetch_shader(struct r600_pipe_context *rctx, struct r600_vertex_element *ve)
 {
-       unsigned ndw, i;
-       u32 *bytecode;
-       unsigned fetch_resource_start = 0, format, num_format, format_comp;
+       static int dump_shaders = -1;
+
+       struct r600_bc bc;
+       struct r600_bc_vtx vtx;
        struct pipe_vertex_element *elements = ve->elements;
        const struct util_format_description *desc;
-
-       /* 2 dwords for cf aligned to 4 + 4 dwords per input */
-       ndw = 8 + ve->count * 4;
-       ve->fs_size = ndw * 4;
-
-       /* use PIPE_BIND_VERTEX_BUFFER so we use the cache buffer manager */
-       ve->fetch_shader = r600_bo(rctx->radeon, ndw*4, 256, PIPE_BIND_VERTEX_BUFFER, 0);
-       if (ve->fetch_shader == NULL) {
-               return -ENOMEM;
-       }
-
-       bytecode = r600_bo_map(rctx->radeon, ve->fetch_shader, 0, NULL);
-       if (bytecode == NULL) {
-               r600_bo_reference(rctx->radeon, &ve->fetch_shader, NULL);
-               return -ENOMEM;
-       }
-
-       if (rctx->family >= CHIP_CEDAR) {
-               eg_cf_vtx(ve, &bytecode[0], (ndw - 8) / 4);
-       } else {
-               r600_cf_vtx(ve, &bytecode[0], (ndw - 8) / 4);
-               fetch_resource_start = 160;
-       }
+       unsigned fetch_resource_start = rctx->family >= CHIP_CEDAR ? 0 : 160;
+       unsigned format, num_format, format_comp;
+       u32 *bytecode;
+       int i, r;
 
        /* vertex elements offset need special handling, if offset is bigger
-        * than what we can put in fetch instruction then we need to alterate
+       + * than what we can put in fetch instruction then we need to alterate
         * the vertex resource offset. In such case in order to simplify code
         * we will bound one resource per elements. It's a worst case scenario.
         */
@@ -1863,40 +2073,111 @@ int r600_vertex_elements_build_fetch_shader(struct r600_pipe_context *rctx, stru
                }
        }
 
+       memset(&bc, 0, sizeof(bc));
+       r = r600_bc_init(&bc, r600_get_family(rctx->radeon));
+       if (r)
+               return r;
+
+       for (i = 0; i < ve->count; i++) {
+               if (elements[i].instance_divisor > 1) {
+                       struct r600_bc_alu alu;
+
+                       memset(&alu, 0, sizeof(alu));
+                       alu.inst = BC_INST(&bc, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_MULHI_UINT);
+                       alu.src[0].sel = 0;
+                       alu.src[0].chan = 3;
+
+                       alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
+                       alu.src[1].value = (1ll << 32) / elements[i].instance_divisor + 1;
+
+                       alu.dst.sel = i + 1;
+                       alu.dst.chan = 3;
+                       alu.dst.write = 1;
+                       alu.last = 1;
+
+                       if ((r = r600_bc_add_alu(&bc, &alu))) {
+                               r600_bc_clear(&bc);
+                               return r;
+                       }
+               }
+       }
+
        for (i = 0; i < ve->count; i++) {
                unsigned vbuffer_index;
-               r600_vertex_data_type(ve->hw_format[i], &format, &num_format, &format_comp);
-               desc = util_format_description(ve->hw_format[i]);
+               r600_vertex_data_type(ve->elements[i].src_format, &format, &num_format, &format_comp);
+               desc = util_format_description(ve->elements[i].src_format);
                if (desc == NULL) {
-                       R600_ERR("unknown format %d\n", ve->hw_format[i]);
-                       r600_bo_reference(rctx->radeon, &ve->fetch_shader, NULL);
+                       r600_bc_clear(&bc);
+                       R600_ERR("unknown format %d\n", ve->elements[i].src_format);
                        return -EINVAL;
                }
 
                /* see above for vbuffer_need_offset explanation */
                vbuffer_index = elements[i].vertex_buffer_index;
-               if (ve->vbuffer_need_offset) {
-                       bytecode[8 + i * 4 + 0] = S_SQ_VTX_WORD0_BUFFER_ID(i + fetch_resource_start);
-               } else {
-                       bytecode[8 + i * 4 + 0] = S_SQ_VTX_WORD0_BUFFER_ID(vbuffer_index + fetch_resource_start);
+               memset(&vtx, 0, sizeof(vtx));
+               vtx.buffer_id = (ve->vbuffer_need_offset ? i : vbuffer_index) + fetch_resource_start;
+               vtx.fetch_type = elements[i].instance_divisor ? 1 : 0;
+               vtx.src_gpr = elements[i].instance_divisor > 1 ? i + 1 : 0;
+               vtx.src_sel_x = elements[i].instance_divisor ? 3 : 0;
+               vtx.mega_fetch_count = 0x1F;
+               vtx.dst_gpr = i + 1;
+               vtx.dst_sel_x = desc->swizzle[0];
+               vtx.dst_sel_y = desc->swizzle[1];
+               vtx.dst_sel_z = desc->swizzle[2];
+               vtx.dst_sel_w = desc->swizzle[3];
+               vtx.data_format = format;
+               vtx.num_format_all = num_format;
+               vtx.format_comp_all = format_comp;
+               vtx.srf_mode_all = 1;
+               vtx.offset = elements[i].src_offset;
+
+               if ((r = r600_bc_add_vtx(&bc, &vtx))) {
+                       r600_bc_clear(&bc);
+                       return r;
                }
-               bytecode[8 + i * 4 + 0] |= S_SQ_VTX_WORD0_SRC_GPR(0) |
-                                       S_SQ_VTX_WORD0_SRC_SEL_X(0) |
-                                       S_SQ_VTX_WORD0_MEGA_FETCH_COUNT(0x1F);
-               bytecode[8 + i * 4 + 1] = S_SQ_VTX_WORD1_DST_SEL_X(desc->swizzle[0]) |
-                                       S_SQ_VTX_WORD1_DST_SEL_Y(desc->swizzle[1]) |
-                                       S_SQ_VTX_WORD1_DST_SEL_Z(desc->swizzle[2]) |
-                                       S_SQ_VTX_WORD1_DST_SEL_W(desc->swizzle[3]) |
-                                       S_SQ_VTX_WORD1_USE_CONST_FIELDS(0) |
-                                       S_SQ_VTX_WORD1_DATA_FORMAT(format) |
-                                       S_SQ_VTX_WORD1_NUM_FORMAT_ALL(num_format) |
-                                       S_SQ_VTX_WORD1_FORMAT_COMP_ALL(format_comp) |
-                                       S_SQ_VTX_WORD1_SRF_MODE_ALL(1) |
-                                       S_SQ_VTX_WORD1_GPR_DST_GPR(i + 1);
-               bytecode[8 + i * 4 + 2] = S_SQ_VTX_WORD2_OFFSET(elements[i].src_offset) |
-                                       S_SQ_VTX_WORD2_MEGA_FETCH(1);
-               bytecode[8 + i * 4 + 3] = 0;
        }
+
+       r600_bc_add_cfinst(&bc, BC_INST(&bc, V_SQ_CF_WORD1_SQ_CF_INST_RETURN));
+
+       if ((r = r600_bc_build(&bc))) {
+               r600_bc_clear(&bc);
+               return r;
+       }
+
+       if (dump_shaders == -1)
+               dump_shaders = debug_get_bool_option("R600_DUMP_SHADERS", FALSE);
+
+       if (dump_shaders) {
+               fprintf(stderr, "--------------------------------------------------------------\n");
+               r600_bc_dump(&bc);
+               fprintf(stderr, "______________________________________________________________\n");
+       }
+
+       ve->fs_size = bc.ndw*4;
+
+       /* use PIPE_BIND_VERTEX_BUFFER so we use the cache buffer manager */
+       ve->fetch_shader = r600_bo(rctx->radeon, ve->fs_size, 256, PIPE_BIND_VERTEX_BUFFER, 0);
+       if (ve->fetch_shader == NULL) {
+               r600_bc_clear(&bc);
+               return -ENOMEM;
+       }
+
+       bytecode = r600_bo_map(rctx->radeon, ve->fetch_shader, 0, NULL);
+       if (bytecode == NULL) {
+               r600_bc_clear(&bc);
+               r600_bo_reference(rctx->radeon, &ve->fetch_shader, NULL);
+               return -ENOMEM;
+       }
+
+       memcpy(bytecode, bc.bytecode, ve->fs_size);
+
        r600_bo_unmap(rctx->radeon, ve->fetch_shader);
+       r600_bc_clear(&bc);
+
+       if (rctx->family >= CHIP_CEDAR)
+               evergreen_fetch_shader(ve);
+       else
+               r600_fetch_shader(ve);
+
        return 0;
 }