gallium/drivers: Sanitize NULL checks into canonical form
[mesa.git] / src / gallium / drivers / radeon / radeon_setup_tgsi_llvm.c
index 5c08cf5d3399d851dd4688bf4c4899937523f4e9..c94f1093ab7d4cc3c5a9dfe51b84adc943610d9f 100644 (file)
@@ -272,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)
@@ -285,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, "");
                        }
@@ -315,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;
@@ -347,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, "");
                        }
                }
@@ -767,7 +778,7 @@ static void radeon_llvm_cube_to_2d_coords(struct lp_build_tgsi_context *bld_base
                coords[i] = LLVMBuildExtractElement(builder, v,
                                                    lp_build_const_int32(gallivm, i), "");
 
-       coords[2] = lp_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]);
 
@@ -908,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)
@@ -1176,8 +1201,18 @@ static void emit_frac(
                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, "floor", emit_data->dst_type,
+       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,
@@ -1372,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;
@@ -1425,7 +1505,7 @@ void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
        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;
@@ -1434,16 +1514,15 @@ 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 = emit_frac;
@@ -1462,7 +1541,7 @@ 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 = emit_frac;
@@ -1472,17 +1551,14 @@ void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
        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;
@@ -1498,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;
@@ -1509,32 +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_RSQ].intr_name =
-               HAVE_LLVM >= 0x0305 ? "llvm.AMDGPU.rsq.clamped.f32" : "llvm.AMDGPU.rsq";
+       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_cmp;
-       bld_base->op_actions[TGSI_OPCODE_SEQ].emit = emit_cmp;
+       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_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;