gallium/drivers: Sanitize NULL checks into canonical form
[mesa.git] / src / gallium / drivers / radeon / radeon_setup_tgsi_llvm.c
index 5bc93d7aa0e6cd90bb16504d1ad7636f8b952b52..c94f1093ab7d4cc3c5a9dfe51b84adc943610d9f 100644 (file)
@@ -109,13 +109,6 @@ emit_array_index(
        return LLVMBuildAdd(gallivm->builder, addr, lp_build_const_int32(gallivm, offset), "");
 }
 
-static LLVMValueRef
-emit_fetch(
-       struct lp_build_tgsi_context *bld_base,
-       const struct tgsi_full_src_register *reg,
-       enum tgsi_opcode_type type,
-       unsigned swizzle);
-
 LLVMValueRef
 radeon_llvm_emit_fetch_double(
        struct lp_build_tgsi_context *bld_base,
@@ -158,7 +151,7 @@ emit_array_fetch(
 
        for (i = 0; i < size; ++i) {
                tmp_reg.Register.Index = i + range.First;
-               LLVMValueRef temp = emit_fetch(bld_base, &tmp_reg, type, swizzle);
+               LLVMValueRef temp = radeon_llvm_emit_fetch(bld_base, &tmp_reg, type, swizzle);
                result = LLVMBuildInsertElement(builder, result, temp,
                        lp_build_const_int32(gallivm, i), "");
        }
@@ -172,12 +165,10 @@ static bool uses_temp_indirect_addressing(
        return (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY));
 }
 
-static LLVMValueRef
-emit_fetch(
-       struct lp_build_tgsi_context *bld_base,
-       const struct tgsi_full_src_register *reg,
-       enum tgsi_opcode_type type,
-       unsigned swizzle)
+LLVMValueRef radeon_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
+                                   const struct tgsi_full_src_register *reg,
+                                   enum tgsi_opcode_type type,
+                                   unsigned swizzle)
 {
        struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
        struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
@@ -188,7 +179,7 @@ emit_fetch(
                LLVMValueRef values[TGSI_NUM_CHANNELS];
                unsigned chan;
                for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
-                       values[chan] = emit_fetch(bld_base, reg, type, chan);
+                       values[chan] = radeon_llvm_emit_fetch(bld_base, reg, type, chan);
                }
                return lp_build_gather_values(bld_base->base.gallivm, values,
                                              TGSI_NUM_CHANNELS);
@@ -281,6 +272,15 @@ static LLVMValueRef fetch_system_value(
        return bitcast(bld_base, type, cval);
 }
 
+static LLVMValueRef si_build_alloca_undef(struct gallivm_state *gallivm,
+                                         LLVMTypeRef type,
+                                         const char *name)
+{
+       LLVMValueRef ptr = lp_build_alloca(gallivm, type, name);
+       LLVMBuildStore(gallivm->builder, LLVMGetUndef(type), ptr);
+       return ptr;
+}
+
 static void emit_declaration(
        struct lp_build_tgsi_context * bld_base,
        const struct tgsi_full_declaration *decl)
@@ -294,7 +294,7 @@ static void emit_declaration(
                for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
                        unsigned chan;
                        for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
-                                ctx->soa.addr[idx][chan] = lp_build_alloca(
+                                ctx->soa.addr[idx][chan] = si_build_alloca_undef(
                                        &ctx->gallivm,
                                        ctx->soa.bld_base.uint_bld.elem_type, "");
                        }
@@ -324,8 +324,9 @@ static void emit_declaration(
                for (idx = first; idx <= last; idx++) {
                        for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
                                ctx->temps[idx * TGSI_NUM_CHANNELS + i] =
-                                       lp_build_alloca(bld_base->base.gallivm, bld_base->base.vec_type,
-                                               "temp");
+                                       si_build_alloca_undef(bld_base->base.gallivm,
+                                                             bld_base->base.vec_type,
+                                                             "temp");
                        }
                }
                break;
@@ -356,7 +357,8 @@ static void emit_declaration(
                        unsigned chan;
                        assert(idx < RADEON_LLVM_MAX_OUTPUTS);
                        for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
-                               ctx->soa.outputs[idx][chan] = lp_build_alloca(&ctx->gallivm,
+                               ctx->soa.outputs[idx][chan] = si_build_alloca_undef(
+                                       &ctx->gallivm,
                                        ctx->soa.bld_base.base.elem_type, "");
                        }
                }
@@ -757,34 +759,26 @@ static void kil_emit(
        }
 }
 
-void radeon_llvm_emit_prepare_cube_coords(
-               struct lp_build_tgsi_context * bld_base,
-               struct lp_build_emit_data * emit_data,
-               LLVMValueRef *coords_arg)
+static void radeon_llvm_cube_to_2d_coords(struct lp_build_tgsi_context *bld_base,
+                                         LLVMValueRef *in, LLVMValueRef *out)
 {
-
-       unsigned target = emit_data->inst->Texture.Texture;
-       unsigned opcode = emit_data->inst->Instruction.Opcode;
        struct gallivm_state * gallivm = bld_base->base.gallivm;
        LLVMBuilderRef builder = gallivm->builder;
        LLVMTypeRef type = bld_base->base.elem_type;
        LLVMValueRef coords[4];
        LLVMValueRef mad_args[3];
-       LLVMValueRef idx;
-       struct LLVMOpaqueValue *cube_vec;
-       LLVMValueRef v;
+       LLVMValueRef v, cube_vec;
        unsigned i;
 
-       cube_vec = lp_build_gather_values(bld_base->base.gallivm, coords_arg, 4);
-       v = build_intrinsic(builder, "llvm.AMDGPU.cube", LLVMVectorType(type, 4),
+       cube_vec = lp_build_gather_values(bld_base->base.gallivm, in, 4);
+       v = lp_build_intrinsic(builder, "llvm.AMDGPU.cube", LLVMVectorType(type, 4),
                             &cube_vec, 1, LLVMReadNoneAttribute);
 
-       for (i = 0; i < 4; ++i) {
-               idx = lp_build_const_int32(gallivm, i);
-               coords[i] = LLVMBuildExtractElement(builder, v, idx, "");
-       }
+       for (i = 0; i < 4; ++i)
+               coords[i] = LLVMBuildExtractElement(builder, v,
+                                                   lp_build_const_int32(gallivm, i), "");
 
-       coords[2] = build_intrinsic(builder, "fabs",
+       coords[2] = lp_build_intrinsic(builder, "llvm.fabs.f32",
                        type, &coords[2], 1, LLVMReadNoneAttribute);
        coords[2] = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_RCP, coords[2]);
 
@@ -800,10 +794,60 @@ void radeon_llvm_emit_prepare_cube_coords(
                        mad_args[0], mad_args[1], mad_args[2]);
 
        /* apply xyz = yxw swizzle to cooords */
-       coords[2] = coords[3];
-       coords[3] = coords[1];
-       coords[1] = coords[0];
-       coords[0] = coords[3];
+       out[0] = coords[1];
+       out[1] = coords[0];
+       out[2] = coords[3];
+}
+
+void radeon_llvm_emit_prepare_cube_coords(
+               struct lp_build_tgsi_context * bld_base,
+               struct lp_build_emit_data * emit_data,
+               LLVMValueRef *coords_arg,
+               LLVMValueRef *derivs_arg)
+{
+
+       unsigned target = emit_data->inst->Texture.Texture;
+       unsigned opcode = emit_data->inst->Instruction.Opcode;
+       struct gallivm_state * gallivm = bld_base->base.gallivm;
+       LLVMBuilderRef builder = gallivm->builder;
+       LLVMValueRef coords[4];
+       unsigned i;
+
+       radeon_llvm_cube_to_2d_coords(bld_base, coords_arg, coords);
+
+       if (opcode == TGSI_OPCODE_TXD && derivs_arg) {
+               LLVMValueRef derivs[4];
+               int axis;
+
+               /* Convert cube derivatives to 2D derivatives. */
+               for (axis = 0; axis < 2; axis++) {
+                       LLVMValueRef shifted_cube_coords[4], shifted_coords[4];
+
+                       /* Shift the cube coordinates by the derivatives to get
+                        * the cube coordinates of the "neighboring pixel".
+                        */
+                       for (i = 0; i < 3; i++)
+                               shifted_cube_coords[i] =
+                                       LLVMBuildFAdd(builder, coords_arg[i],
+                                                     derivs_arg[axis*3+i], "");
+                       shifted_cube_coords[3] = LLVMGetUndef(bld_base->base.elem_type);
+
+                       /* Project the shifted cube coordinates onto the face. */
+                       radeon_llvm_cube_to_2d_coords(bld_base, shifted_cube_coords,
+                                                     shifted_coords);
+
+                       /* Subtract both sets of 2D coordinates to get 2D derivatives.
+                        * This won't work if the shifted coordinates ended up
+                        * in a different face.
+                        */
+                       for (i = 0; i < 2; i++)
+                               derivs[axis * 2 + i] =
+                                       LLVMBuildFSub(builder, shifted_coords[i],
+                                                     coords[i], "");
+               }
+
+               memcpy(derivs_arg, derivs, sizeof(derivs));
+       }
 
        if (target == TGSI_TEXTURE_CUBE_ARRAY ||
            target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
@@ -827,140 +871,6 @@ void radeon_llvm_emit_prepare_cube_coords(
        memcpy(coords_arg, coords, sizeof(coords));
 }
 
-static void txd_fetch_args(
-       struct lp_build_tgsi_context * bld_base,
-       struct lp_build_emit_data * emit_data)
-{
-       const struct tgsi_full_instruction * inst = emit_data->inst;
-
-       LLVMValueRef coords[4];
-       unsigned chan, src;
-       for (src = 0; src < 3; src++) {
-               for (chan = 0; chan < 4; chan++)
-                       coords[chan] = lp_build_emit_fetch(bld_base, inst, src, chan);
-
-               emit_data->args[src] = lp_build_gather_values(bld_base->base.gallivm,
-                               coords, 4);
-       }
-       emit_data->arg_count = 3;
-       emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
-}
-
-
-static void txp_fetch_args(
-       struct lp_build_tgsi_context * bld_base,
-       struct lp_build_emit_data * emit_data)
-{
-       const struct tgsi_full_instruction * inst = emit_data->inst;
-       LLVMValueRef src_w;
-       unsigned chan;
-       LLVMValueRef coords[5];
-
-       emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
-       src_w = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
-
-       for (chan = 0; chan < 3; chan++ ) {
-               LLVMValueRef arg = lp_build_emit_fetch(bld_base,
-                                               emit_data->inst, 0, chan);
-               coords[chan] = lp_build_emit_llvm_binary(bld_base,
-                                       TGSI_OPCODE_DIV, arg, src_w);
-       }
-       coords[3] = bld_base->base.one;
-
-       if ((inst->Texture.Texture == TGSI_TEXTURE_CUBE ||
-            inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
-            inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
-            inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) &&
-           inst->Instruction.Opcode != TGSI_OPCODE_TXQ &&
-           inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ) {
-               radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
-       }
-
-       emit_data->args[0] = lp_build_gather_values(bld_base->base.gallivm,
-                                               coords, 4);
-       emit_data->arg_count = 1;
-}
-
-static void tex_fetch_args(
-       struct lp_build_tgsi_context * bld_base,
-       struct lp_build_emit_data * emit_data)
-{
-       /* XXX: lp_build_swizzle_aos() was failing with wrong arg types,
-        * when we used CHAN_ALL.  We should be able to get this to work,
-        * but for now we will swizzle it ourselves
-       emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
-                                                0, CHAN_ALL);
-
-       */
-
-       const struct tgsi_full_instruction * inst = emit_data->inst;
-
-       LLVMValueRef coords[5];
-       unsigned chan;
-       for (chan = 0; chan < 4; chan++) {
-               coords[chan] = lp_build_emit_fetch(bld_base, inst, 0, chan);
-       }
-
-       if (inst->Instruction.Opcode == TGSI_OPCODE_TEX2 ||
-               inst->Instruction.Opcode == TGSI_OPCODE_TXB2 ||
-               inst->Instruction.Opcode == TGSI_OPCODE_TXL2) {
-               /* These instructions have additional operand that should be packed
-                * into the cube coord vector by radeon_llvm_emit_prepare_cube_coords.
-                * That operand should be passed as a float value in the args array
-                * right after the coord vector. After packing it's not used anymore,
-                * that's why arg_count is not increased */
-               coords[4] = lp_build_emit_fetch(bld_base, inst, 1, 0);
-       }
-
-       if ((inst->Texture.Texture == TGSI_TEXTURE_CUBE ||
-            inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
-            inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
-            inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) &&
-           inst->Instruction.Opcode != TGSI_OPCODE_TXQ &&
-           inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ) {
-               radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
-       }
-
-       emit_data->arg_count = 1;
-       emit_data->args[0] = lp_build_gather_values(bld_base->base.gallivm,
-                                               coords, 4);
-       emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
-}
-
-static void txf_fetch_args(
-       struct lp_build_tgsi_context * bld_base,
-       struct lp_build_emit_data * emit_data)
-{
-       const struct tgsi_full_instruction * inst = emit_data->inst;
-       struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
-       const struct tgsi_texture_offset * off = inst->TexOffsets;
-       LLVMTypeRef offset_type = bld_base->int_bld.elem_type;
-
-       /* fetch tex coords */
-       tex_fetch_args(bld_base, emit_data);
-
-       /* fetch tex offsets */
-       if (inst->Texture.NumOffsets) {
-               assert(inst->Texture.NumOffsets == 1);
-
-               emit_data->args[1] = LLVMConstBitCast(
-                       bld->immediates[off->Index][off->SwizzleX],
-                       offset_type);
-               emit_data->args[2] = LLVMConstBitCast(
-                       bld->immediates[off->Index][off->SwizzleY],
-                       offset_type);
-               emit_data->args[3] = LLVMConstBitCast(
-                       bld->immediates[off->Index][off->SwizzleZ],
-                       offset_type);
-       } else {
-               emit_data->args[1] = bld_base->int_bld.zero;
-               emit_data->args[2] = bld_base->int_bld.zero;
-               emit_data->args[3] = bld_base->int_bld.zero;
-       }
-
-       emit_data->arg_count = 4;
-}
-
 static void emit_icmp(
                const struct lp_build_tgsi_action * action,
                struct lp_build_tgsi_context * bld_base,
@@ -1009,7 +919,21 @@ static void emit_ucmp(
                LLVMBuildSelect(builder, v, emit_data->args[1], emit_data->args[2], "");
 }
 
-static void emit_cmp(
+static void emit_cmp(const struct lp_build_tgsi_action *action,
+                    struct lp_build_tgsi_context *bld_base,
+                    struct lp_build_emit_data *emit_data)
+{
+       LLVMBuilderRef builder = bld_base->base.gallivm->builder;
+       LLVMValueRef cond, *args = emit_data->args;
+
+       cond = LLVMBuildFCmp(builder, LLVMRealOLT, args[0],
+                            bld_base->base.zero, "");
+
+       emit_data->output[emit_data->chan] =
+               LLVMBuildSelect(builder, cond, args[1], args[2], "");
+}
+
+static void emit_set_cond(
                const struct lp_build_tgsi_action *action,
                struct lp_build_tgsi_context * bld_base,
                struct lp_build_emit_data * emit_data)
@@ -1271,6 +1195,30 @@ static void emit_dneg(
                        emit_data->args[0], "");
 }
 
+static void emit_frac(
+               const struct lp_build_tgsi_action * action,
+               struct lp_build_tgsi_context * bld_base,
+               struct lp_build_emit_data * emit_data)
+{
+       LLVMBuilderRef builder = bld_base->base.gallivm->builder;
+       char *intr;
+
+       if (emit_data->info->opcode == TGSI_OPCODE_FRC)
+               intr = "llvm.floor.f32";
+       else if (emit_data->info->opcode == TGSI_OPCODE_DFRAC)
+               intr = "llvm.floor.f64";
+       else {
+               assert(0);
+               return;
+       }
+
+       LLVMValueRef floor = lp_build_intrinsic(builder, intr, emit_data->dst_type,
+                                               &emit_data->args[0], 1,
+                                               LLVMReadNoneAttribute);
+       emit_data->output[emit_data->chan] = LLVMBuildFSub(builder,
+                       emit_data->args[0], floor, "");
+}
+
 static void emit_f2i(
                const struct lp_build_tgsi_action * action,
                struct lp_build_tgsi_context * bld_base,
@@ -1325,58 +1273,16 @@ static void emit_immediate(struct lp_build_tgsi_context * bld_base,
        ctx->soa.num_immediates++;
 }
 
-LLVMValueRef
-build_intrinsic(LLVMBuilderRef builder,
-                   const char *name,
-                   LLVMTypeRef ret_type,
-                   LLVMValueRef *args,
-                   unsigned num_args,
-                   LLVMAttribute attr)
-{
-   LLVMModuleRef module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)));
-   LLVMValueRef function;
-
-   function = LLVMGetNamedFunction(module, name);
-   if(!function) {
-      LLVMTypeRef arg_types[LP_MAX_FUNC_ARGS];
-      unsigned i;
-
-      assert(num_args <= LP_MAX_FUNC_ARGS);
-
-      for(i = 0; i < num_args; ++i) {
-         assert(args[i]);
-         arg_types[i] = LLVMTypeOf(args[i]);
-      }
-
-      function = lp_declare_intrinsic(module, name, ret_type, arg_types, num_args);
-
-      if (attr)
-          LLVMAddFunctionAttr(function, attr);
-   }
-
-   return LLVMBuildCall(builder, function, args, num_args, "");
-}
-
-static void build_tgsi_intrinsic(
- const struct lp_build_tgsi_action * action,
- struct lp_build_tgsi_context * bld_base,
- struct lp_build_emit_data * emit_data,
- LLVMAttribute attr)
-{
-   struct lp_build_context * base = &bld_base->base;
-   emit_data->output[emit_data->chan] = build_intrinsic(
-               base->gallivm->builder, action->intr_name,
-               emit_data->dst_type, emit_data->args,
-               emit_data->arg_count, attr);
-}
-
 void
-build_tgsi_intrinsic_nomem(
- const struct lp_build_tgsi_action * action,
- struct lp_build_tgsi_context * bld_base,
- struct lp_build_emit_data * emit_data)
+build_tgsi_intrinsic_nomem(const struct lp_build_tgsi_action *action,
+                          struct lp_build_tgsi_context *bld_base,
+                          struct lp_build_emit_data *emit_data)
 {
-       build_tgsi_intrinsic(action, bld_base, emit_data, LLVMReadNoneAttribute);
+       struct lp_build_context * base = &bld_base->base;
+       emit_data->output[emit_data->chan] =
+               lp_build_intrinsic(base->gallivm->builder, action->intr_name,
+                                  emit_data->dst_type, emit_data->args,
+                                  emit_data->arg_count, LLVMReadNoneAttribute);
 }
 
 static void emit_bfi(const struct lp_build_tgsi_action * action,
@@ -1432,7 +1338,7 @@ static void emit_lsb(const struct lp_build_tgsi_action * action,
        };
 
        emit_data->output[emit_data->chan] =
-               build_intrinsic(gallivm->builder, "llvm.cttz.i32",
+               lp_build_intrinsic(gallivm->builder, "llvm.cttz.i32",
                                emit_data->dst_type, args, Elements(args),
                                LLVMReadNoneAttribute);
 }
@@ -1451,7 +1357,7 @@ static void emit_umsb(const struct lp_build_tgsi_action * action,
        };
 
        LLVMValueRef msb =
-               build_intrinsic(builder, "llvm.ctlz.i32",
+               lp_build_intrinsic(builder, "llvm.ctlz.i32",
                                emit_data->dst_type, args, Elements(args),
                                LLVMReadNoneAttribute);
 
@@ -1478,7 +1384,7 @@ static void emit_imsb(const struct lp_build_tgsi_action * action,
        LLVMValueRef arg = emit_data->args[0];
 
        LLVMValueRef msb =
-               build_intrinsic(builder, "llvm.AMDGPU.flbit.i32",
+               lp_build_intrinsic(builder, "llvm.AMDGPU.flbit.i32",
                                emit_data->dst_type, &arg, 1,
                                LLVMReadNoneAttribute);
 
@@ -1501,6 +1407,51 @@ static void emit_imsb(const struct lp_build_tgsi_action * action,
                LLVMBuildSelect(builder, cond, all_ones, msb, "");
 }
 
+static void emit_iabs(const struct lp_build_tgsi_action *action,
+                     struct lp_build_tgsi_context *bld_base,
+                     struct lp_build_emit_data *emit_data)
+{
+       LLVMBuilderRef builder = bld_base->base.gallivm->builder;
+
+       emit_data->output[emit_data->chan] =
+               lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_IMAX,
+                                         emit_data->args[0],
+                                         LLVMBuildNeg(builder,
+                                                      emit_data->args[0], ""));
+}
+
+static void emit_minmax_int(const struct lp_build_tgsi_action *action,
+                           struct lp_build_tgsi_context *bld_base,
+                           struct lp_build_emit_data *emit_data)
+{
+       LLVMBuilderRef builder = bld_base->base.gallivm->builder;
+       LLVMIntPredicate op;
+
+       switch (emit_data->info->opcode) {
+       default:
+               assert(0);
+       case TGSI_OPCODE_IMAX:
+               op = LLVMIntSGT;
+               break;
+       case TGSI_OPCODE_IMIN:
+               op = LLVMIntSLT;
+               break;
+       case TGSI_OPCODE_UMAX:
+               op = LLVMIntUGT;
+               break;
+       case TGSI_OPCODE_UMIN:
+               op = LLVMIntULT;
+               break;
+       }
+
+       emit_data->output[emit_data->chan] =
+               LLVMBuildSelect(builder,
+                               LLVMBuildICmp(builder, op, emit_data->args[0],
+                                             emit_data->args[1], ""),
+                               emit_data->args[0],
+                               emit_data->args[1], "");
+}
+
 void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
 {
        struct lp_type type;
@@ -1517,12 +1468,8 @@ void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
                                                ctx->gallivm.context);
        ctx->gallivm.builder = LLVMCreateBuilderInContext(ctx->gallivm.context);
 
-       ctx->store_output_intr = "llvm.AMDGPU.store.output.";
-       ctx->swizzle_intr = "llvm.AMDGPU.swizzle";
        struct lp_build_tgsi_context * bld_base = &ctx->soa.bld_base;
 
-       /* XXX: We need to revisit this.I think the correct way to do this is
-        * to use length = 4 here and use the elem_bld for everything. */
        type.floating = TRUE;
        type.fixed = FALSE;
        type.sign = TRUE;
@@ -1546,21 +1493,19 @@ void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
        bld_base->emit_declaration = emit_declaration;
        bld_base->emit_immediate = emit_immediate;
 
-       bld_base->emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = emit_fetch;
-       bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch;
-       bld_base->emit_fetch_funcs[TGSI_FILE_TEMPORARY] = emit_fetch;
-       bld_base->emit_fetch_funcs[TGSI_FILE_OUTPUT] = emit_fetch;
+       bld_base->emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = radeon_llvm_emit_fetch;
+       bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = radeon_llvm_emit_fetch;
+       bld_base->emit_fetch_funcs[TGSI_FILE_TEMPORARY] = radeon_llvm_emit_fetch;
+       bld_base->emit_fetch_funcs[TGSI_FILE_OUTPUT] = radeon_llvm_emit_fetch;
        bld_base->emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = fetch_system_value;
 
        /* Allocate outputs */
        ctx->soa.outputs = ctx->outputs;
 
-       /* XXX: Is there a better way to initialize all this ? */
-
        lp_set_default_actions(bld_base);
 
        bld_base->op_actions[TGSI_OPCODE_ABS].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_ABS].intr_name = "fabs";
+       bld_base->op_actions[TGSI_OPCODE_ABS].intr_name = "llvm.fabs.f32";
        bld_base->op_actions[TGSI_OPCODE_AND].emit = emit_and;
        bld_base->op_actions[TGSI_OPCODE_ARL].emit = emit_arl;
        bld_base->op_actions[TGSI_OPCODE_BFI].emit = emit_bfi;
@@ -1569,29 +1514,23 @@ void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
        bld_base->op_actions[TGSI_OPCODE_BREV].intr_name = "llvm.AMDGPU.brev";
        bld_base->op_actions[TGSI_OPCODE_BRK].emit = brk_emit;
        bld_base->op_actions[TGSI_OPCODE_CEIL].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_CEIL].intr_name = "ceil";
+       bld_base->op_actions[TGSI_OPCODE_CEIL].intr_name = "llvm.ceil.f32";
        bld_base->op_actions[TGSI_OPCODE_CLAMP].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_CLAMP].intr_name = "llvm.AMDIL.clamp.";
-       bld_base->op_actions[TGSI_OPCODE_CMP].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_CMP].intr_name = "llvm.AMDGPU.cndlt";
+       bld_base->op_actions[TGSI_OPCODE_CMP].emit = emit_cmp;
        bld_base->op_actions[TGSI_OPCODE_CONT].emit = cont_emit;
        bld_base->op_actions[TGSI_OPCODE_COS].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_COS].intr_name = "llvm.cos.f32";
        bld_base->op_actions[TGSI_OPCODE_DABS].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_DABS].intr_name = "fabs";
+       bld_base->op_actions[TGSI_OPCODE_DABS].intr_name = "llvm.fabs.f64";
        bld_base->op_actions[TGSI_OPCODE_DFMA].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_DFMA].intr_name = "llvm.fma.f64";
-       bld_base->op_actions[TGSI_OPCODE_DFRAC].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_DFRAC].intr_name = "llvm.AMDIL.fraction.";
+       bld_base->op_actions[TGSI_OPCODE_DFRAC].emit = emit_frac;
        bld_base->op_actions[TGSI_OPCODE_DNEG].emit = emit_dneg;
        bld_base->op_actions[TGSI_OPCODE_DSEQ].emit = emit_dcmp;
        bld_base->op_actions[TGSI_OPCODE_DSGE].emit = emit_dcmp;
        bld_base->op_actions[TGSI_OPCODE_DSLT].emit = emit_dcmp;
        bld_base->op_actions[TGSI_OPCODE_DSNE].emit = emit_dcmp;
-       bld_base->op_actions[TGSI_OPCODE_DDX].intr_name = "llvm.AMDGPU.ddx";
-       bld_base->op_actions[TGSI_OPCODE_DDX].fetch_args = tex_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_DDY].intr_name = "llvm.AMDGPU.ddy";
-       bld_base->op_actions[TGSI_OPCODE_DDY].fetch_args = tex_fetch_args;
        bld_base->op_actions[TGSI_OPCODE_DRSQ].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_DRSQ].intr_name = "llvm.AMDGPU.rsq.f64";
        bld_base->op_actions[TGSI_OPCODE_DSQRT].emit = build_tgsi_intrinsic_nomem;
@@ -1602,28 +1541,24 @@ void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
        bld_base->op_actions[TGSI_OPCODE_EX2].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_EX2].intr_name = "llvm.AMDIL.exp.";
        bld_base->op_actions[TGSI_OPCODE_FLR].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_FLR].intr_name = "floor";
+       bld_base->op_actions[TGSI_OPCODE_FLR].intr_name = "llvm.floor.f32";
        bld_base->op_actions[TGSI_OPCODE_FMA].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_FMA].intr_name = "llvm.fma.f32";
-       bld_base->op_actions[TGSI_OPCODE_FRC].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_FRC].intr_name = "llvm.AMDIL.fraction.";
+       bld_base->op_actions[TGSI_OPCODE_FRC].emit = emit_frac;
        bld_base->op_actions[TGSI_OPCODE_F2I].emit = emit_f2i;
        bld_base->op_actions[TGSI_OPCODE_F2U].emit = emit_f2u;
        bld_base->op_actions[TGSI_OPCODE_FSEQ].emit = emit_fcmp;
        bld_base->op_actions[TGSI_OPCODE_FSGE].emit = emit_fcmp;
        bld_base->op_actions[TGSI_OPCODE_FSLT].emit = emit_fcmp;
        bld_base->op_actions[TGSI_OPCODE_FSNE].emit = emit_fcmp;
-       bld_base->op_actions[TGSI_OPCODE_IABS].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_IABS].intr_name = "llvm.AMDIL.abs.";
+       bld_base->op_actions[TGSI_OPCODE_IABS].emit = emit_iabs;
        bld_base->op_actions[TGSI_OPCODE_IBFE].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_IBFE].intr_name = "llvm.AMDGPU.bfe.i32";
        bld_base->op_actions[TGSI_OPCODE_IDIV].emit = emit_idiv;
        bld_base->op_actions[TGSI_OPCODE_IF].emit = if_emit;
        bld_base->op_actions[TGSI_OPCODE_UIF].emit = uif_emit;
-       bld_base->op_actions[TGSI_OPCODE_IMAX].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_IMAX].intr_name = "llvm.AMDGPU.imax";
-       bld_base->op_actions[TGSI_OPCODE_IMIN].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_IMIN].intr_name = "llvm.AMDGPU.imin";
+       bld_base->op_actions[TGSI_OPCODE_IMAX].emit = emit_minmax_int;
+       bld_base->op_actions[TGSI_OPCODE_IMIN].emit = emit_minmax_int;
        bld_base->op_actions[TGSI_OPCODE_IMSB].emit = emit_imsb;
        bld_base->op_actions[TGSI_OPCODE_INEG].emit = emit_ineg;
        bld_base->op_actions[TGSI_OPCODE_ISHR].emit = emit_ishr;
@@ -1639,8 +1574,6 @@ void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
        bld_base->op_actions[TGSI_OPCODE_LSB].emit = emit_lsb;
        bld_base->op_actions[TGSI_OPCODE_LG2].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_LG2].intr_name = "llvm.log2.f32";
-       bld_base->op_actions[TGSI_OPCODE_LRP].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_LRP].intr_name = "llvm.AMDGPU.lrp";
        bld_base->op_actions[TGSI_OPCODE_MOD].emit = emit_mod;
        bld_base->op_actions[TGSI_OPCODE_UMSB].emit = emit_umsb;
        bld_base->op_actions[TGSI_OPCODE_NOT].emit = emit_not;
@@ -1650,49 +1583,29 @@ void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
        bld_base->op_actions[TGSI_OPCODE_POW].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_POW].intr_name = "llvm.pow.f32";
        bld_base->op_actions[TGSI_OPCODE_ROUND].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_ROUND].intr_name = "llvm.AMDIL.round.nearest.";
-       bld_base->op_actions[TGSI_OPCODE_SGE].emit = emit_cmp;
-       bld_base->op_actions[TGSI_OPCODE_SEQ].emit = emit_cmp;
+       bld_base->op_actions[TGSI_OPCODE_ROUND].intr_name = "llvm.rint.f32";
+       bld_base->op_actions[TGSI_OPCODE_RSQ].intr_name = "llvm.AMDGPU.rsq.clamped.f32";
+       bld_base->op_actions[TGSI_OPCODE_RSQ].emit = build_tgsi_intrinsic_nomem;
+       bld_base->op_actions[TGSI_OPCODE_SGE].emit = emit_set_cond;
+       bld_base->op_actions[TGSI_OPCODE_SEQ].emit = emit_set_cond;
        bld_base->op_actions[TGSI_OPCODE_SHL].emit = emit_shl;
-       bld_base->op_actions[TGSI_OPCODE_SLE].emit = emit_cmp;
-       bld_base->op_actions[TGSI_OPCODE_SLT].emit = emit_cmp;
-       bld_base->op_actions[TGSI_OPCODE_SNE].emit = emit_cmp;
-       bld_base->op_actions[TGSI_OPCODE_SGT].emit = emit_cmp;
+       bld_base->op_actions[TGSI_OPCODE_SLE].emit = emit_set_cond;
+       bld_base->op_actions[TGSI_OPCODE_SLT].emit = emit_set_cond;
+       bld_base->op_actions[TGSI_OPCODE_SNE].emit = emit_set_cond;
+       bld_base->op_actions[TGSI_OPCODE_SGT].emit = emit_set_cond;
        bld_base->op_actions[TGSI_OPCODE_SIN].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_SIN].intr_name = "llvm.sin.f32";
        bld_base->op_actions[TGSI_OPCODE_SQRT].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_SQRT].intr_name = "llvm.sqrt.f32";
        bld_base->op_actions[TGSI_OPCODE_SSG].emit = emit_ssg;
-       bld_base->op_actions[TGSI_OPCODE_TEX].fetch_args = tex_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TEX].intr_name = "llvm.AMDGPU.tex";
-       bld_base->op_actions[TGSI_OPCODE_TEX2].fetch_args = tex_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TEX2].intr_name = "llvm.AMDGPU.tex";
-       bld_base->op_actions[TGSI_OPCODE_TXB].fetch_args = tex_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TXB].intr_name = "llvm.AMDGPU.txb";
-       bld_base->op_actions[TGSI_OPCODE_TXB2].fetch_args = tex_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TXB2].intr_name = "llvm.AMDGPU.txb";
-       bld_base->op_actions[TGSI_OPCODE_TXD].fetch_args = txd_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TXD].intr_name = "llvm.AMDGPU.txd";
-       bld_base->op_actions[TGSI_OPCODE_TXF].fetch_args = txf_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TXF].intr_name = "llvm.AMDGPU.txf";
-       bld_base->op_actions[TGSI_OPCODE_TXL].fetch_args = tex_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TXL].intr_name = "llvm.AMDGPU.txl";
-       bld_base->op_actions[TGSI_OPCODE_TXL2].fetch_args = tex_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TXL2].intr_name = "llvm.AMDGPU.txl";
-       bld_base->op_actions[TGSI_OPCODE_TXP].fetch_args = txp_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TXP].intr_name = "llvm.AMDGPU.tex";
-       bld_base->op_actions[TGSI_OPCODE_TXQ].fetch_args = tex_fetch_args;
-       bld_base->op_actions[TGSI_OPCODE_TXQ].intr_name = "llvm.AMDGPU.txq";
        bld_base->op_actions[TGSI_OPCODE_TRUNC].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_TRUNC].intr_name = "llvm.AMDGPU.trunc";
+       bld_base->op_actions[TGSI_OPCODE_TRUNC].intr_name = "llvm.trunc.f32";
        bld_base->op_actions[TGSI_OPCODE_UADD].emit = emit_uadd;
        bld_base->op_actions[TGSI_OPCODE_UBFE].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_UBFE].intr_name = "llvm.AMDGPU.bfe.u32";
        bld_base->op_actions[TGSI_OPCODE_UDIV].emit = emit_udiv;
-       bld_base->op_actions[TGSI_OPCODE_UMAX].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_UMAX].intr_name = "llvm.AMDGPU.umax";
-       bld_base->op_actions[TGSI_OPCODE_UMIN].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_UMIN].intr_name = "llvm.AMDGPU.umin";
+       bld_base->op_actions[TGSI_OPCODE_UMAX].emit = emit_minmax_int;
+       bld_base->op_actions[TGSI_OPCODE_UMIN].emit = emit_minmax_int;
        bld_base->op_actions[TGSI_OPCODE_UMOD].emit = emit_umod;
        bld_base->op_actions[TGSI_OPCODE_USEQ].emit = emit_icmp;
        bld_base->op_actions[TGSI_OPCODE_USGE].emit = emit_icmp;
@@ -1702,13 +1615,6 @@ void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
        bld_base->op_actions[TGSI_OPCODE_U2F].emit = emit_u2f;
        bld_base->op_actions[TGSI_OPCODE_XOR].emit = emit_xor;
        bld_base->op_actions[TGSI_OPCODE_UCMP].emit = emit_ucmp;
-
-       bld_base->rsq_action.emit = build_tgsi_intrinsic_nomem;
-#if HAVE_LLVM >= 0x0305
-       bld_base->rsq_action.intr_name = "llvm.AMDGPU.rsq.clamped.f32";
-#else
-       bld_base->rsq_action.intr_name = "llvm.AMDGPU.rsq";
-#endif
 }
 
 void radeon_llvm_create_func(struct radeon_llvm_context * ctx,