radeonsi/gfx10: use fma for TGSI_OPCODE_FMA
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_tgsi_alu.c
index c410a8b540a0dc1a6d849ef0bd67818423a688b2..4be410ec3317837ec2140646875048a3e1bf12f2 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright 2016 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  */
 
 #include "si_shader_internal.h"
-#include "gallivm/lp_bld_const.h"
-#include "gallivm/lp_bld_intr.h"
-#include "gallivm/lp_bld_gather.h"
-#include "tgsi/tgsi_parse.h"
+#include "si_pipe.h"
+#include "ac_llvm_util.h"
 
-static void kill_if_fetch_args(struct lp_build_tgsi_context *bld_base,
-                              struct lp_build_emit_data *emit_data)
+void si_llvm_emit_kill(struct ac_shader_abi *abi, LLVMValueRef visible)
 {
-       const struct tgsi_full_instruction *inst = emit_data->inst;
-       struct gallivm_state *gallivm = bld_base->base.gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
-       unsigned i;
-       LLVMValueRef conds[TGSI_NUM_CHANNELS];
+       struct si_shader_context *ctx = si_shader_context_from_abi(abi);
+       LLVMBuilderRef builder = ctx->ac.builder;
 
-       for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
-               LLVMValueRef value = lp_build_emit_fetch(bld_base, inst, 0, i);
-               conds[i] = LLVMBuildFCmp(builder, LLVMRealOLT, value,
-                                       bld_base->base.zero, "");
-       }
+       if (ctx->shader->selector->force_correct_derivs_after_kill) {
+               /* Kill immediately while maintaining WQM. */
+               ac_build_kill_if_false(&ctx->ac,
+                                      ac_build_wqm_vote(&ctx->ac, visible));
 
-       /* Or the conditions together */
-       for (i = TGSI_NUM_CHANNELS - 1; i > 0; i--) {
-               conds[i - 1] = LLVMBuildOr(builder, conds[i], conds[i - 1], "");
+               LLVMValueRef mask = LLVMBuildLoad(builder, ctx->postponed_kill, "");
+               mask = LLVMBuildAnd(builder, mask, visible, "");
+               LLVMBuildStore(builder, mask, ctx->postponed_kill);
+               return;
        }
 
-       emit_data->dst_type = LLVMVoidTypeInContext(gallivm->context);
-       emit_data->arg_count = 1;
-       emit_data->args[0] = LLVMBuildSelect(builder, conds[0],
-                                       lp_build_const_float(gallivm, -1.0f),
-                                       bld_base->base.zero, "");
+       ac_build_kill_if_false(&ctx->ac, visible);
 }
 
 static void kil_emit(const struct lp_build_tgsi_action *action,
                     struct lp_build_tgsi_context *bld_base,
                     struct lp_build_emit_data *emit_data)
 {
-       unsigned i;
-       for (i = 0; i < emit_data->arg_count; i++) {
-               emit_data->output[i] = lp_build_intrinsic_unary(
-                       bld_base->base.gallivm->builder,
-                       action->intr_name,
-                       emit_data->dst_type, emit_data->args[i]);
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       LLVMValueRef visible;
+
+       if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_KILL_IF) {
+               const struct tgsi_full_instruction *inst = emit_data->inst;
+               struct si_shader_context *ctx = si_shader_context(bld_base);
+               LLVMBuilderRef builder = ctx->ac.builder;
+               unsigned i;
+               LLVMValueRef conds[TGSI_NUM_CHANNELS];
+
+               for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
+                       LLVMValueRef value = lp_build_emit_fetch(bld_base, inst, 0, i);
+                       /* UGE because NaN shouldn't get killed */
+                       conds[i] = LLVMBuildFCmp(builder, LLVMRealUGE, value,
+                                               ctx->ac.f32_0, "");
+               }
+
+               /* And the conditions together */
+               for (i = TGSI_NUM_CHANNELS - 1; i > 0; i--) {
+                       conds[i - 1] = LLVMBuildAnd(builder, conds[i], conds[i - 1], "");
+               }
+               visible = conds[0];
+       } else {
+               assert(emit_data->inst->Instruction.Opcode == TGSI_OPCODE_KILL);
+               visible = ctx->i1false;
        }
+
+       si_llvm_emit_kill(&ctx->abi, visible);
 }
 
 static void emit_icmp(const struct lp_build_tgsi_action *action,
@@ -72,8 +84,7 @@ static void emit_icmp(const struct lp_build_tgsi_action *action,
                      struct lp_build_emit_data *emit_data)
 {
        unsigned pred;
-       LLVMBuilderRef builder = bld_base->base.gallivm->builder;
-       LLVMContextRef context = bld_base->base.gallivm->context;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
 
        switch (emit_data->inst->Instruction.Opcode) {
        case TGSI_OPCODE_USEQ:
@@ -94,11 +105,10 @@ static void emit_icmp(const struct lp_build_tgsi_action *action,
                break;
        }
 
-       LLVMValueRef v = LLVMBuildICmp(builder, pred,
+       LLVMValueRef v = LLVMBuildICmp(ctx->ac.builder, pred,
                        emit_data->args[0], emit_data->args[1],"");
 
-       v = LLVMBuildSExtOrBitCast(builder, v,
-                       LLVMInt32TypeInContext(context), "");
+       v = LLVMBuildSExtOrBitCast(ctx->ac.builder, v, ctx->i32, "");
 
        emit_data->output[emit_data->chan] = v;
 }
@@ -107,37 +117,35 @@ static void emit_ucmp(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 arg0 = LLVMBuildBitCast(builder, emit_data->args[0],
-                                            bld_base->uint_bld.elem_type, "");
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       LLVMValueRef arg0 = ac_to_integer(&ctx->ac, emit_data->args[0]);
 
-       LLVMValueRef v = LLVMBuildICmp(builder, LLVMIntNE, arg0,
-                                      bld_base->uint_bld.zero, "");
+       LLVMValueRef v = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, arg0,
+                                      ctx->i32_0, "");
 
        emit_data->output[emit_data->chan] =
-               LLVMBuildSelect(builder, v, emit_data->args[1], emit_data->args[2], "");
+               LLVMBuildSelect(ctx->ac.builder, v, emit_data->args[1], emit_data->args[2], "");
 }
 
 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;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMValueRef cond, *args = emit_data->args;
 
-       cond = LLVMBuildFCmp(builder, LLVMRealOLT, args[0],
-                            bld_base->base.zero, "");
+       cond = LLVMBuildFCmp(ctx->ac.builder, LLVMRealOLT, args[0],
+                            ctx->ac.f32_0, "");
 
        emit_data->output[emit_data->chan] =
-               LLVMBuildSelect(builder, cond, args[1], args[2], "");
+               LLVMBuildSelect(ctx->ac.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)
 {
-       LLVMBuilderRef builder = bld_base->base.gallivm->builder;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMRealPredicate pred;
        LLVMValueRef cond;
 
@@ -154,19 +162,18 @@ static void emit_set_cond(const struct lp_build_tgsi_action *action,
        default: assert(!"unknown instruction"); pred = 0; break;
        }
 
-       cond = LLVMBuildFCmp(builder,
+       cond = LLVMBuildFCmp(ctx->ac.builder,
                pred, emit_data->args[0], emit_data->args[1], "");
 
-       emit_data->output[emit_data->chan] = LLVMBuildSelect(builder,
-               cond, bld_base->base.one, bld_base->base.zero, "");
+       emit_data->output[emit_data->chan] = LLVMBuildSelect(ctx->ac.builder,
+               cond, ctx->ac.f32_1, ctx->ac.f32_0, "");
 }
 
 static void emit_fcmp(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;
-       LLVMContextRef context = bld_base->base.gallivm->context;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMRealPredicate pred;
 
        /* Use ordered for everything but NE (which is usual for
@@ -180,11 +187,10 @@ static void emit_fcmp(const struct lp_build_tgsi_action *action,
        default: assert(!"unknown instruction"); pred = 0; break;
        }
 
-       LLVMValueRef v = LLVMBuildFCmp(builder, pred,
+       LLVMValueRef v = LLVMBuildFCmp(ctx->ac.builder, pred,
                        emit_data->args[0], emit_data->args[1],"");
 
-       v = LLVMBuildSExtOrBitCast(builder, v,
-                       LLVMInt32TypeInContext(context), "");
+       v = LLVMBuildSExtOrBitCast(ctx->ac.builder, v, ctx->i32, "");
 
        emit_data->output[emit_data->chan] = v;
 }
@@ -193,8 +199,7 @@ static void emit_dcmp(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;
-       LLVMContextRef context = bld_base->base.gallivm->context;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMRealPredicate pred;
 
        /* Use ordered for everything but NE (which is usual for
@@ -208,11 +213,10 @@ static void emit_dcmp(const struct lp_build_tgsi_action *action,
        default: assert(!"unknown instruction"); pred = 0; break;
        }
 
-       LLVMValueRef v = LLVMBuildFCmp(builder, pred,
+       LLVMValueRef v = LLVMBuildFCmp(ctx->ac.builder, pred,
                        emit_data->args[0], emit_data->args[1],"");
 
-       v = LLVMBuildSExtOrBitCast(builder, v,
-                       LLVMInt32TypeInContext(context), "");
+       v = LLVMBuildSExtOrBitCast(ctx->ac.builder, v, ctx->i32, "");
 
        emit_data->output[emit_data->chan] = v;
 }
@@ -221,28 +225,29 @@ static void emit_not(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 v = bitcast(bld_base, TGSI_TYPE_UNSIGNED,
-                       emit_data->args[0]);
-       emit_data->output[emit_data->chan] = LLVMBuildNot(builder, v, "");
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       LLVMValueRef v = ac_to_integer(&ctx->ac, emit_data->args[0]);
+       emit_data->output[emit_data->chan] = LLVMBuildNot(ctx->ac.builder, v, "");
 }
 
 static void emit_arl(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 floor_index =  lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_FLR, emit_data->args[0]);
-       emit_data->output[emit_data->chan] = LLVMBuildFPToSI(builder,
-                       floor_index, bld_base->base.int_elem_type , "");
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       LLVMValueRef floor_index =
+               ac_build_intrinsic(&ctx->ac, "llvm.floor.f32", ctx->f32,
+                                  &emit_data->args[0], 1, AC_FUNC_ATTR_READNONE);
+       emit_data->output[emit_data->chan] = LLVMBuildFPToSI(ctx->ac.builder,
+                       floor_index, ctx->i32, "");
 }
 
 static void emit_and(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] = LLVMBuildAnd(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildAnd(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -250,8 +255,8 @@ static void emit_or(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] = LLVMBuildOr(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildOr(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -259,8 +264,8 @@ static void emit_uadd(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] = LLVMBuildAdd(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildAdd(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -268,8 +273,8 @@ static void emit_udiv(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] = LLVMBuildUDiv(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildUDiv(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -277,8 +282,8 @@ static void emit_idiv(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] = LLVMBuildSDiv(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildSDiv(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -286,8 +291,8 @@ static void emit_mod(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] = LLVMBuildSRem(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildSRem(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -295,8 +300,8 @@ static void emit_umod(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] = LLVMBuildURem(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildURem(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -304,8 +309,8 @@ static void emit_shl(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] = LLVMBuildShl(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildShl(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -313,16 +318,16 @@ static void emit_ushr(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] = LLVMBuildLShr(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildLShr(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 static void emit_ishr(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] = LLVMBuildAShr(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildAShr(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -330,8 +335,8 @@ static void emit_xor(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] = LLVMBuildXor(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildXor(ctx->ac.builder,
                        emit_data->args[0], emit_data->args[1], "");
 }
 
@@ -339,25 +344,18 @@ static void emit_ssg(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;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
 
-       LLVMValueRef cmp, val;
+       LLVMValueRef  val;
 
        if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_I64SSG) {
-               cmp = LLVMBuildICmp(builder, LLVMIntSGT, emit_data->args[0], bld_base->int64_bld.zero, "");
-               val = LLVMBuildSelect(builder, cmp, bld_base->int64_bld.one, emit_data->args[0], "");
-               cmp = LLVMBuildICmp(builder, LLVMIntSGE, val, bld_base->int64_bld.zero, "");
-               val = LLVMBuildSelect(builder, cmp, val, LLVMConstInt(bld_base->int64_bld.elem_type, -1, true), "");
+               val = ac_build_isign(&ctx->ac, emit_data->args[0], 64);
        } else if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_ISSG) {
-               cmp = LLVMBuildICmp(builder, LLVMIntSGT, emit_data->args[0], bld_base->int_bld.zero, "");
-               val = LLVMBuildSelect(builder, cmp, bld_base->int_bld.one, emit_data->args[0], "");
-               cmp = LLVMBuildICmp(builder, LLVMIntSGE, val, bld_base->int_bld.zero, "");
-               val = LLVMBuildSelect(builder, cmp, val, LLVMConstInt(bld_base->int_bld.elem_type, -1, true), "");
-       } else { // float SSG
-               cmp = LLVMBuildFCmp(builder, LLVMRealOGT, emit_data->args[0], bld_base->base.zero, "");
-               val = LLVMBuildSelect(builder, cmp, bld_base->base.one, emit_data->args[0], "");
-               cmp = LLVMBuildFCmp(builder, LLVMRealOGE, val, bld_base->base.zero, "");
-               val = LLVMBuildSelect(builder, cmp, val, LLVMConstReal(bld_base->base.elem_type, -1), "");
+               val = ac_build_isign(&ctx->ac, emit_data->args[0], 32);
+       } else if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_DSSG) {
+               val = ac_build_fsign(&ctx->ac, emit_data->args[0], 64);
+       } else {
+               val = ac_build_fsign(&ctx->ac, emit_data->args[0], 32);
        }
 
        emit_data->output[emit_data->chan] = val;
@@ -367,8 +365,8 @@ static void emit_ineg(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] = LLVMBuildNeg(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildNeg(ctx->ac.builder,
                        emit_data->args[0], "");
 }
 
@@ -376,8 +374,8 @@ static void emit_dneg(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] = LLVMBuildFNeg(builder,
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildFNeg(ctx->ac.builder,
                        emit_data->args[0], "");
 }
 
@@ -385,59 +383,56 @@ 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;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       unsigned bitsize;
 
        if (emit_data->info->opcode == TGSI_OPCODE_FRC)
-               intr = "llvm.floor.f32";
+               bitsize = 32;
        else if (emit_data->info->opcode == TGSI_OPCODE_DFRAC)
-               intr = "llvm.floor.f64";
+               bitsize = 64;
        else {
                assert(0);
                return;
        }
 
-       LLVMValueRef floor = lp_build_intrinsic(builder, intr, emit_data->dst_type,
-                                               &emit_data->args[0], 1,
-                                               LP_FUNC_ATTR_READNONE);
-       emit_data->output[emit_data->chan] = LLVMBuildFSub(builder,
-                       emit_data->args[0], floor, "");
+       emit_data->output[emit_data->chan] =
+               ac_build_fract(&ctx->ac, emit_data->args[0], bitsize);
 }
 
 static void emit_f2i(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] = LLVMBuildFPToSI(builder,
-                       emit_data->args[0], bld_base->int_bld.elem_type, "");
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildFPToSI(ctx->ac.builder,
+                       emit_data->args[0], ctx->i32, "");
 }
 
 static void emit_f2u(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] = LLVMBuildFPToUI(builder,
-                       emit_data->args[0], bld_base->uint_bld.elem_type, "");
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildFPToUI(ctx->ac.builder,
+                       emit_data->args[0], ctx->i32, "");
 }
 
 static void emit_i2f(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] = LLVMBuildSIToFP(builder,
-                       emit_data->args[0], bld_base->base.elem_type, "");
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildSIToFP(ctx->ac.builder,
+                       emit_data->args[0], ctx->f32, "");
 }
 
 static void emit_u2f(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] = LLVMBuildUIToFP(builder,
-                       emit_data->args[0], bld_base->base.elem_type, "");
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       emit_data->output[emit_data->chan] = LLVMBuildUIToFP(ctx->ac.builder,
+                       emit_data->args[0], ctx->f32, "");
 }
 
 static void
@@ -445,19 +440,19 @@ 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)
 {
-       struct lp_build_context *base = &bld_base->base;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
        emit_data->output[emit_data->chan] =
-               lp_build_intrinsic(base->gallivm->builder, action->intr_name,
+               ac_build_intrinsic(&ctx->ac, action->intr_name,
                                   emit_data->dst_type, emit_data->args,
-                                  emit_data->arg_count, LP_FUNC_ATTR_READNONE);
+                                  emit_data->arg_count, AC_FUNC_ATTR_READNONE);
 }
 
 static void emit_bfi(const struct lp_build_tgsi_action *action,
                     struct lp_build_tgsi_context *bld_base,
                     struct lp_build_emit_data *emit_data)
 {
-       struct gallivm_state *gallivm = bld_base->base.gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef bfi_args[3];
        LLVMValueRef bfi_sm5;
        LLVMValueRef cond;
@@ -466,9 +461,9 @@ static void emit_bfi(const struct lp_build_tgsi_action *action,
        bfi_args[0] = LLVMBuildShl(builder,
                                   LLVMBuildSub(builder,
                                                LLVMBuildShl(builder,
-                                                            bld_base->int_bld.one,
+                                                            ctx->i32_1,
                                                             emit_data->args[3], ""),
-                                               bld_base->int_bld.one, ""),
+                                               ctx->i32_1, ""),
                                   emit_data->args[2], "");
 
        bfi_args[1] = LLVMBuildShl(builder, emit_data->args[1],
@@ -491,7 +486,7 @@ static void emit_bfi(const struct lp_build_tgsi_action *action,
         * and disagrees with GLSL semantics when bits (src3) is 32.
         */
        cond = LLVMBuildICmp(builder, LLVMIntUGE, emit_data->args[3],
-                            lp_build_const_int32(gallivm, 32), "");
+                            LLVMConstInt(ctx->i32, 32, 0), "");
        emit_data->output[emit_data->chan] =
                LLVMBuildSelect(builder, cond, emit_data->args[1], bfi_sm5, "");
 }
@@ -500,20 +495,25 @@ static void emit_bfe(const struct lp_build_tgsi_action *action,
                     struct lp_build_tgsi_context *bld_base,
                     struct lp_build_emit_data *emit_data)
 {
-       struct gallivm_state *gallivm = bld_base->base.gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
-       LLVMValueRef bfe_sm5;
-       LLVMValueRef cond;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
 
-       bfe_sm5 = lp_build_intrinsic(builder, action->intr_name,
-                                    emit_data->dst_type, emit_data->args,
-                                    emit_data->arg_count, LP_FUNC_ATTR_READNONE);
+       /* FIXME: LLVM 7 returns incorrect result when count is 0.
+        * https://bugs.freedesktop.org/show_bug.cgi?id=107276
+        */
+       LLVMValueRef zero = ctx->i32_0;
+       LLVMValueRef bfe_sm5 =
+               ac_build_bfe(&ctx->ac, emit_data->args[0],
+                            emit_data->args[1], emit_data->args[2],
+                            emit_data->info->opcode == TGSI_OPCODE_IBFE);
 
        /* Correct for GLSL semantics. */
-       cond = LLVMBuildICmp(builder, LLVMIntUGE, emit_data->args[2],
-                            lp_build_const_int32(gallivm, 32), "");
+       LLVMValueRef cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntUGE, emit_data->args[2],
+                                         LLVMConstInt(ctx->i32, 32, 0), "");
+       LLVMValueRef cond2 = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, emit_data->args[2],
+                                          zero, "");
+       bfe_sm5 = LLVMBuildSelect(ctx->ac.builder, cond, emit_data->args[0], bfe_sm5, "");
        emit_data->output[emit_data->chan] =
-               LLVMBuildSelect(builder, cond, emit_data->args[0], bfe_sm5, "");
+               LLVMBuildSelect(ctx->ac.builder, cond2, zero, bfe_sm5, "");
 }
 
 /* this is ffs in C */
@@ -521,33 +521,9 @@ static void emit_lsb(const struct lp_build_tgsi_action *action,
                     struct lp_build_tgsi_context *bld_base,
                     struct lp_build_emit_data *emit_data)
 {
-       struct gallivm_state *gallivm = bld_base->base.gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
-       LLVMValueRef args[2] = {
-               emit_data->args[0],
-
-               /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
-                * add special code to check for x=0. The reason is that
-                * the LLVM behavior for x=0 is different from what we
-                * need here. However, LLVM also assumes that ffs(x) is
-                * in [0, 31], but GLSL expects that ffs(0) = -1, so
-                * a conditional assignment to handle 0 is still required.
-                */
-               LLVMConstInt(LLVMInt1TypeInContext(gallivm->context), 1, 0)
-       };
-
-       LLVMValueRef lsb =
-               lp_build_intrinsic(gallivm->builder, "llvm.cttz.i32",
-                               emit_data->dst_type, args, ARRAY_SIZE(args),
-                               LP_FUNC_ATTR_READNONE);
+       struct si_shader_context *ctx = si_shader_context(bld_base);
 
-       /* TODO: We need an intrinsic to skip this conditional. */
-       /* Check for zero: */
-       emit_data->output[emit_data->chan] =
-               LLVMBuildSelect(builder,
-                               LLVMBuildICmp(builder, LLVMIntEQ, args[0],
-                                             bld_base->uint_bld.zero, ""),
-                               lp_build_const_int32(gallivm, -1), lsb, "");
+       emit_data->output[emit_data->chan] = ac_find_lsb(&ctx->ac, emit_data->dst_type, emit_data->args[0]);
 }
 
 /* Find the last bit set. */
@@ -555,30 +531,10 @@ static void emit_umsb(const struct lp_build_tgsi_action *action,
                      struct lp_build_tgsi_context *bld_base,
                      struct lp_build_emit_data *emit_data)
 {
-       struct gallivm_state *gallivm = bld_base->base.gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
-       LLVMValueRef args[2] = {
-               emit_data->args[0],
-               /* Don't generate code for handling zero: */
-               LLVMConstInt(LLVMInt1TypeInContext(gallivm->context), 1, 0)
-       };
-
-       LLVMValueRef msb =
-               lp_build_intrinsic(builder, "llvm.ctlz.i32",
-                               emit_data->dst_type, args, ARRAY_SIZE(args),
-                               LP_FUNC_ATTR_READNONE);
-
-       /* The HW returns the last bit index from MSB, but TGSI wants
-        * the index from LSB. Invert it by doing "31 - msb". */
-       msb = LLVMBuildSub(builder, lp_build_const_int32(gallivm, 31),
-                          msb, "");
+       struct si_shader_context *ctx = si_shader_context(bld_base);
 
-       /* Check for zero: */
        emit_data->output[emit_data->chan] =
-               LLVMBuildSelect(builder,
-                               LLVMBuildICmp(builder, LLVMIntEQ, args[0],
-                                             bld_base->uint_bld.zero, ""),
-                               lp_build_const_int32(gallivm, -1), msb, "");
+               ac_build_umsb(&ctx->ac, emit_data->args[0], emit_data->dst_type);
 }
 
 /* Find the last bit opposite of the sign bit. */
@@ -586,52 +542,28 @@ static void emit_imsb(const struct lp_build_tgsi_action *action,
                      struct lp_build_tgsi_context *bld_base,
                      struct lp_build_emit_data *emit_data)
 {
-       struct gallivm_state *gallivm = bld_base->base.gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
-       LLVMValueRef arg = emit_data->args[0];
-
-       LLVMValueRef msb =
-               lp_build_intrinsic(builder, "llvm.AMDGPU.flbit.i32",
-                               emit_data->dst_type, &arg, 1,
-                               LP_FUNC_ATTR_READNONE);
-
-       /* The HW returns the last bit index from MSB, but TGSI wants
-        * the index from LSB. Invert it by doing "31 - msb". */
-       msb = LLVMBuildSub(builder, lp_build_const_int32(gallivm, 31),
-                          msb, "");
-
-       /* If arg == 0 || arg == -1 (0xffffffff), return -1. */
-       LLVMValueRef all_ones = lp_build_const_int32(gallivm, -1);
-
-       LLVMValueRef cond =
-               LLVMBuildOr(builder,
-                           LLVMBuildICmp(builder, LLVMIntEQ, arg,
-                                         bld_base->uint_bld.zero, ""),
-                           LLVMBuildICmp(builder, LLVMIntEQ, arg,
-                                         all_ones, ""), "");
-
+       struct si_shader_context *ctx = si_shader_context(bld_base);
        emit_data->output[emit_data->chan] =
-               LLVMBuildSelect(builder, cond, all_ones, msb, "");
+               ac_build_imsb(&ctx->ac, emit_data->args[0],
+                             emit_data->dst_type);
 }
 
 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;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
 
        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], ""));
+               ac_build_imax(&ctx->ac,  emit_data->args[0],
+                             LLVMBuildNeg(ctx->ac.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;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMIntPredicate op;
 
        switch (emit_data->info->opcode) {
@@ -656,78 +588,52 @@ static void emit_minmax_int(const struct lp_build_tgsi_action *action,
        }
 
        emit_data->output[emit_data->chan] =
-               LLVMBuildSelect(builder,
-                               LLVMBuildICmp(builder, op, emit_data->args[0],
+               LLVMBuildSelect(ctx->ac.builder,
+                               LLVMBuildICmp(ctx->ac.builder, op, emit_data->args[0],
                                              emit_data->args[1], ""),
                                emit_data->args[0],
                                emit_data->args[1], "");
 }
 
-static void pk2h_fetch_args(struct lp_build_tgsi_context *bld_base,
-                           struct lp_build_emit_data *emit_data)
-{
-       emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
-                                                0, TGSI_CHAN_X);
-       emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
-                                                0, TGSI_CHAN_Y);
-}
-
 static void emit_pk2h(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;
-       LLVMContextRef context = bld_base->base.gallivm->context;
-       struct lp_build_context *uint_bld = &bld_base->uint_bld;
-       LLVMTypeRef fp16, i16;
-       LLVMValueRef const16, comp[2];
-       unsigned i;
-
-       fp16 = LLVMHalfTypeInContext(context);
-       i16 = LLVMInt16TypeInContext(context);
-       const16 = lp_build_const_int32(uint_bld->gallivm, 16);
-
-       for (i = 0; i < 2; i++) {
-               comp[i] = LLVMBuildFPTrunc(builder, emit_data->args[i], fp16, "");
-               comp[i] = LLVMBuildBitCast(builder, comp[i], i16, "");
-               comp[i] = LLVMBuildZExt(builder, comp[i], uint_bld->elem_type, "");
-       }
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       LLVMValueRef v[] = {
+               lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X),
+               lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_Y),
+       };
 
-       comp[1] = LLVMBuildShl(builder, comp[1], const16, "");
-       comp[0] = LLVMBuildOr(builder, comp[0], comp[1], "");
 
-       emit_data->output[emit_data->chan] = comp[0];
-}
-
-static void up2h_fetch_args(struct lp_build_tgsi_context *bld_base,
-                           struct lp_build_emit_data *emit_data)
-{
-       emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
-                                                0, TGSI_CHAN_X);
+       /* From the GLSL 4.50 spec:
+        *   "The rounding mode cannot be set and is undefined."
+        *
+        * v_cvt_pkrtz_f16 rounds to zero, but it's fastest.
+        */
+       emit_data->output[emit_data->chan] =
+               LLVMBuildBitCast(ctx->ac.builder, ac_build_cvt_pkrtz_f16(&ctx->ac, v),
+                                ctx->i32, "");
 }
 
 static void emit_up2h(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;
-       LLVMContextRef context = bld_base->base.gallivm->context;
-       struct lp_build_context *uint_bld = &bld_base->uint_bld;
-       LLVMTypeRef fp16, i16;
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       LLVMTypeRef i16;
        LLVMValueRef const16, input, val;
        unsigned i;
 
-       fp16 = LLVMHalfTypeInContext(context);
-       i16 = LLVMInt16TypeInContext(context);
-       const16 = lp_build_const_int32(uint_bld->gallivm, 16);
-       input = emit_data->args[0];
+       i16 = LLVMInt16TypeInContext(ctx->ac.context);
+       const16 = LLVMConstInt(ctx->i32, 16, 0);
+       input = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X);
 
        for (i = 0; i < 2; i++) {
-               val = i == 1 ? LLVMBuildLShr(builder, input, const16, "") : input;
-               val = LLVMBuildTrunc(builder, val, i16, "");
-               val = LLVMBuildBitCast(builder, val, fp16, "");
-               emit_data->output[i] =
-                       LLVMBuildFPExt(builder, val, bld_base->base.elem_type, "");
+               val = i == 1 ? LLVMBuildLShr(ctx->ac.builder, input, const16, "") : input;
+               val = LLVMBuildTrunc(ctx->ac.builder, val, i16, "");
+               val = ac_to_float(&ctx->ac, val);
+               emit_data->output[i] = LLVMBuildFPExt(ctx->ac.builder, val, ctx->f32, "");
        }
 }
 
@@ -738,14 +644,7 @@ static void emit_fdiv(const struct lp_build_tgsi_action *action,
        struct si_shader_context *ctx = si_shader_context(bld_base);
 
        emit_data->output[emit_data->chan] =
-               LLVMBuildFDiv(bld_base->base.gallivm->builder,
-                             emit_data->args[0], emit_data->args[1], "");
-
-       /* Use v_rcp_f32 instead of precise division. */
-       if (HAVE_LLVM >= 0x0309 &&
-           !LLVMIsConstant(emit_data->output[emit_data->chan]))
-               LLVMSetMetadata(emit_data->output[emit_data->chan],
-                               ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
+               ac_build_fdiv(&ctx->ac, emit_data->args[0], emit_data->args[1]);
 }
 
 /* 1/sqrt is translated to rsq for f32 if fp32 denormals are not enabled in
@@ -754,56 +653,88 @@ static void emit_rsq(const struct lp_build_tgsi_action *action,
                     struct lp_build_tgsi_context *bld_base,
                     struct lp_build_emit_data *emit_data)
 {
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+
        LLVMValueRef sqrt =
-               lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_SQRT,
-                                        emit_data->args[0]);
+               ac_build_intrinsic(&ctx->ac, "llvm.sqrt.f32", ctx->f32,
+                                  &emit_data->args[0], 1, AC_FUNC_ATTR_READNONE);
+
+       emit_data->output[emit_data->chan] =
+               ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, sqrt);
+}
+
+static void dfracexp_emit(const struct lp_build_tgsi_action *action,
+                         struct lp_build_tgsi_context *bld_base,
+                         struct lp_build_emit_data *emit_data)
+{
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       LLVMValueRef in = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X);
 
        emit_data->output[emit_data->chan] =
-               lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_DIV,
-                                         bld_base->base.one, sqrt);
+               ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.frexp.mant.f64",
+                                  ctx->ac.f64, &in, 1, 0);
+       emit_data->output1[emit_data->chan] =
+               ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.frexp.exp.i32.f64",
+                                  ctx->ac.i32, &in, 1, 0);
 }
 
-void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base)
+void si_shader_context_init_alu(struct si_shader_context *ctx)
 {
+       struct lp_build_tgsi_context *bld_base = &ctx->bld_base;
+
        lp_set_default_actions(bld_base);
 
        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;
        bld_base->op_actions[TGSI_OPCODE_BREV].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_BREV].intr_name =
-               HAVE_LLVM >= 0x0308 ? "llvm.bitreverse.i32" : "llvm.AMDGPU.brev";
+       bld_base->op_actions[TGSI_OPCODE_BREV].intr_name = "llvm.bitreverse.i32";
        bld_base->op_actions[TGSI_OPCODE_CEIL].emit = build_tgsi_intrinsic_nomem;
        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 =
-               HAVE_LLVM >= 0x0308 ? "llvm.AMDGPU.clamp." : "llvm.AMDIL.clamp.";
        bld_base->op_actions[TGSI_OPCODE_CMP].emit = emit_cmp;
        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 = "llvm.fabs.f64";
+       bld_base->op_actions[TGSI_OPCODE_DCEIL].emit = build_tgsi_intrinsic_nomem;
+       bld_base->op_actions[TGSI_OPCODE_DCEIL].intr_name = "llvm.ceil.f64";
+       bld_base->op_actions[TGSI_OPCODE_DFLR].emit = build_tgsi_intrinsic_nomem;
+       bld_base->op_actions[TGSI_OPCODE_DFLR].intr_name = "llvm.floor.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;
        bld_base->op_actions[TGSI_OPCODE_DIV].emit = emit_fdiv;
        bld_base->op_actions[TGSI_OPCODE_DNEG].emit = emit_dneg;
+       bld_base->op_actions[TGSI_OPCODE_DROUND].emit = build_tgsi_intrinsic_nomem;
+       bld_base->op_actions[TGSI_OPCODE_DROUND].intr_name = "llvm.rint.f64";
        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_DSSG].emit = emit_ssg;
        bld_base->op_actions[TGSI_OPCODE_DRSQ].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_DRSQ].intr_name =
-               HAVE_LLVM >= 0x0309 ? "llvm.amdgcn.rsq.f64" : "llvm.AMDGPU.rsq.f64";
+       bld_base->op_actions[TGSI_OPCODE_DRSQ].intr_name = "llvm.amdgcn.rsq.f64";
        bld_base->op_actions[TGSI_OPCODE_DSQRT].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_DSQRT].intr_name = "llvm.sqrt.f64";
+       bld_base->op_actions[TGSI_OPCODE_DTRUNC].emit = build_tgsi_intrinsic_nomem;
+       bld_base->op_actions[TGSI_OPCODE_DTRUNC].intr_name = "llvm.trunc.f64";
+       bld_base->op_actions[TGSI_OPCODE_DFRACEXP].emit = dfracexp_emit;
+       bld_base->op_actions[TGSI_OPCODE_DLDEXP].emit = build_tgsi_intrinsic_nomem;
+       bld_base->op_actions[TGSI_OPCODE_DLDEXP].intr_name = "llvm.amdgcn.ldexp.f64";
        bld_base->op_actions[TGSI_OPCODE_EX2].emit = build_tgsi_intrinsic_nomem;
-       bld_base->op_actions[TGSI_OPCODE_EX2].intr_name =
-               HAVE_LLVM >= 0x0308 ? "llvm.exp2.f32" : "llvm.AMDIL.exp.";
+       bld_base->op_actions[TGSI_OPCODE_EX2].intr_name = "llvm.exp2.f32";
        bld_base->op_actions[TGSI_OPCODE_FLR].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_FLR].intr_name = "llvm.floor.f32";
-       bld_base->op_actions[TGSI_OPCODE_FMA].emit =
-               bld_base->op_actions[TGSI_OPCODE_MAD].emit;
+
+       /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
+       if (ctx->screen->info.chip_class >= GFX10) {
+               bld_base->op_actions[TGSI_OPCODE_FMA].emit = build_tgsi_intrinsic_nomem;
+               bld_base->op_actions[TGSI_OPCODE_FMA].intr_name = "llvm.fma.f32";
+       } else {
+               bld_base->op_actions[TGSI_OPCODE_FMA].emit =
+                       bld_base->op_actions[TGSI_OPCODE_MAD].emit;
+       }
+
        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;
@@ -813,7 +744,6 @@ void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base)
        bld_base->op_actions[TGSI_OPCODE_FSNE].emit = emit_fcmp;
        bld_base->op_actions[TGSI_OPCODE_IABS].emit = emit_iabs;
        bld_base->op_actions[TGSI_OPCODE_IBFE].emit = emit_bfe;
-       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_IMAX].emit = emit_minmax_int;
        bld_base->op_actions[TGSI_OPCODE_IMIN].emit = emit_minmax_int;
@@ -824,11 +754,10 @@ void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base)
        bld_base->op_actions[TGSI_OPCODE_ISLT].emit = emit_icmp;
        bld_base->op_actions[TGSI_OPCODE_ISSG].emit = emit_ssg;
        bld_base->op_actions[TGSI_OPCODE_I2F].emit = emit_i2f;
-       bld_base->op_actions[TGSI_OPCODE_KILL_IF].fetch_args = kill_if_fetch_args;
        bld_base->op_actions[TGSI_OPCODE_KILL_IF].emit = kil_emit;
-       bld_base->op_actions[TGSI_OPCODE_KILL_IF].intr_name = "llvm.AMDGPU.kill";
-       bld_base->op_actions[TGSI_OPCODE_KILL].emit = lp_build_tgsi_intrinsic;
-       bld_base->op_actions[TGSI_OPCODE_KILL].intr_name = "llvm.AMDGPU.kilp";
+       bld_base->op_actions[TGSI_OPCODE_KILL].emit = kil_emit;
+       bld_base->op_actions[TGSI_OPCODE_LDEXP].emit = build_tgsi_intrinsic_nomem;
+       bld_base->op_actions[TGSI_OPCODE_LDEXP].intr_name = "llvm.amdgcn.ldexp.f32";
        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";
@@ -840,7 +769,6 @@ void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base)
        bld_base->op_actions[TGSI_OPCODE_UMSB].emit = emit_umsb;
        bld_base->op_actions[TGSI_OPCODE_NOT].emit = emit_not;
        bld_base->op_actions[TGSI_OPCODE_OR].emit = emit_or;
-       bld_base->op_actions[TGSI_OPCODE_PK2H].fetch_args = pk2h_fetch_args;
        bld_base->op_actions[TGSI_OPCODE_PK2H].emit = emit_pk2h;
        bld_base->op_actions[TGSI_OPCODE_POPC].emit = build_tgsi_intrinsic_nomem;
        bld_base->op_actions[TGSI_OPCODE_POPC].intr_name = "llvm.ctpop.i32";
@@ -865,7 +793,6 @@ void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base)
        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 = emit_bfe;
-       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 = emit_minmax_int;
        bld_base->op_actions[TGSI_OPCODE_UMIN].emit = emit_minmax_int;
@@ -878,7 +805,6 @@ void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base)
        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->op_actions[TGSI_OPCODE_UP2H].fetch_args = up2h_fetch_args;
        bld_base->op_actions[TGSI_OPCODE_UP2H].emit = emit_up2h;
 
        bld_base->op_actions[TGSI_OPCODE_I64MAX].emit = emit_minmax_int;
@@ -906,210 +832,3 @@ void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base)
        bld_base->op_actions[TGSI_OPCODE_U64DIV].emit = emit_udiv;
        bld_base->op_actions[TGSI_OPCODE_I64DIV].emit = emit_idiv;
 }
-
-/* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
- * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
- * already multiplied by two. id is the cube face number.
- */
-struct cube_selection_coords {
-       LLVMValueRef stc[2];
-       LLVMValueRef ma;
-       LLVMValueRef id;
-};
-
-static void build_cube_intrinsic(struct gallivm_state *gallivm,
-                                LLVMValueRef in[3],
-                                struct cube_selection_coords *out)
-{
-       LLVMBuilderRef builder = gallivm->builder;
-
-       if (HAVE_LLVM >= 0x0309) {
-               LLVMTypeRef f32 = LLVMTypeOf(in[0]);
-
-               out->stc[1] = lp_build_intrinsic(builder, "llvm.amdgcn.cubetc",
-                                           f32, in, 3, LP_FUNC_ATTR_READNONE);
-               out->stc[0] = lp_build_intrinsic(builder, "llvm.amdgcn.cubesc",
-                                           f32, in, 3, LP_FUNC_ATTR_READNONE);
-               out->ma = lp_build_intrinsic(builder, "llvm.amdgcn.cubema",
-                                           f32, in, 3, LP_FUNC_ATTR_READNONE);
-               out->id = lp_build_intrinsic(builder, "llvm.amdgcn.cubeid",
-                                           f32, in, 3, LP_FUNC_ATTR_READNONE);
-       } else {
-               LLVMValueRef c[4] = {
-                       in[0],
-                       in[1],
-                       in[2],
-                       LLVMGetUndef(LLVMTypeOf(in[0]))
-               };
-               LLVMValueRef vec = lp_build_gather_values(gallivm, c, 4);
-
-               LLVMValueRef tmp =
-                       lp_build_intrinsic(builder, "llvm.AMDGPU.cube",
-                                         LLVMTypeOf(vec), &vec, 1,
-                                         LP_FUNC_ATTR_READNONE);
-
-               out->stc[1] = LLVMBuildExtractElement(builder, tmp,
-                               lp_build_const_int32(gallivm, 0), "");
-               out->stc[0] = LLVMBuildExtractElement(builder, tmp,
-                               lp_build_const_int32(gallivm, 1), "");
-               out->ma = LLVMBuildExtractElement(builder, tmp,
-                               lp_build_const_int32(gallivm, 2), "");
-               out->id = LLVMBuildExtractElement(builder, tmp,
-                               lp_build_const_int32(gallivm, 3), "");
-       }
-}
-
-/**
- * Build a manual selection sequence for cube face sc/tc coordinates and
- * major axis vector (multiplied by 2 for consistency) for the given
- * vec3 \p coords, for the face implied by \p selcoords.
- *
- * For the major axis, we always adjust the sign to be in the direction of
- * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
- * the selcoords major axis.
- */
-static void build_cube_select(LLVMBuilderRef builder,
-                             const struct cube_selection_coords *selcoords,
-                             const LLVMValueRef *coords,
-                             LLVMValueRef *out_st,
-                             LLVMValueRef *out_ma)
-{
-       LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
-       LLVMValueRef is_ma_positive;
-       LLVMValueRef sgn_ma;
-       LLVMValueRef is_ma_z, is_not_ma_z;
-       LLVMValueRef is_ma_y;
-       LLVMValueRef is_ma_x;
-       LLVMValueRef sgn;
-       LLVMValueRef tmp;
-
-       is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
-               selcoords->ma, LLVMConstReal(f32, 0.0), "");
-       sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
-               LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
-
-       is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
-       is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
-       is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
-               LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
-       is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
-
-       /* Select sc */
-       tmp = LLVMBuildSelect(builder, is_ma_z, coords[2], coords[0], "");
-       sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
-               LLVMBuildSelect(builder, is_ma_x, sgn_ma,
-                       LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
-       out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
-
-       /* Select tc */
-       tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
-       sgn = LLVMBuildSelect(builder, is_ma_y, LLVMBuildFNeg(builder, sgn_ma, ""),
-               LLVMConstReal(f32, -1.0), "");
-       out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
-
-       /* Select ma */
-       tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
-               LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
-       sgn = LLVMBuildSelect(builder, is_ma_positive,
-               LLVMConstReal(f32, 2.0), LLVMConstReal(f32, -2.0), "");
-       *out_ma = LLVMBuildFMul(builder, tmp, sgn, "");
-}
-
-void si_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;
-       LLVMTypeRef type = bld_base->base.elem_type;
-       struct cube_selection_coords selcoords;
-       LLVMValueRef coords[4];
-       LLVMValueRef invma;
-
-       build_cube_intrinsic(gallivm, coords_arg, &selcoords);
-
-       invma = lp_build_intrinsic(builder, "llvm.fabs.f32",
-                       type, &selcoords.ma, 1, LP_FUNC_ATTR_READNONE);
-       invma = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_RCP, invma);
-
-       for (int i = 0; i < 2; ++i)
-               coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
-
-       coords[2] = selcoords.id;
-
-       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 deriv_st[2];
-                       LLVMValueRef deriv_ma;
-
-                       /* Transform the derivative alongside the texture
-                        * coordinate. Mathematically, the correct formula is
-                        * as follows. Assume we're projecting onto the +Z face
-                        * and denote by dx/dh the derivative of the (original)
-                        * X texture coordinate with respect to horizontal
-                        * window coordinates. The projection onto the +Z face
-                        * plane is:
-                        *
-                        *   f(x,z) = x/z
-                        *
-                        * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
-                        *            = 1/z * dx/dh - x/z * 1/z * dz/dh.
-                        *
-                        * This motivatives the implementation below.
-                        *
-                        * Whether this actually gives the expected results for
-                        * apps that might feed in derivatives obtained via
-                        * finite differences is anyone's guess. The OpenGL spec
-                        * seems awfully quiet about how textureGrad for cube
-                        * maps should be handled.
-                        */
-                       build_cube_select(builder, &selcoords, &derivs_arg[axis * 3],
-                                         deriv_st, &deriv_ma);
-
-                       deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
-
-                       for (int i = 0; i < 2; ++i)
-                               derivs[axis * 2 + i] =
-                                       LLVMBuildFSub(builder,
-                                               LLVMBuildFMul(builder, deriv_st[i], invma, ""),
-                                               LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
-               }
-
-               memcpy(derivs_arg, derivs, sizeof(derivs));
-       }
-
-       /* Shift the texture coordinate. This must be applied after the
-        * derivative calculation.
-        */
-       for (int i = 0; i < 2; ++i)
-               coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(type, 1.5), "");
-
-       if (target == TGSI_TEXTURE_CUBE_ARRAY ||
-           target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
-               /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
-               /* coords_arg.w component - array_index for cube arrays */
-               coords[2] = lp_build_emit_llvm_ternary(bld_base, TGSI_OPCODE_MAD,
-                                                      coords_arg[3], lp_build_const_float(gallivm, 8.0), coords[2]);
-       }
-
-       /* Preserve compare/lod/bias. Put it in coords.w. */
-       if (opcode == TGSI_OPCODE_TEX2 ||
-           opcode == TGSI_OPCODE_TXB2 ||
-           opcode == TGSI_OPCODE_TXL2) {
-               coords[3] = coords_arg[4];
-       } else if (opcode == TGSI_OPCODE_TXB ||
-                  opcode == TGSI_OPCODE_TXL ||
-                  target == TGSI_TEXTURE_SHADOWCUBE) {
-               coords[3] = coords_arg[3];
-       }
-
-       memcpy(coords_arg, coords, sizeof(coords));
-}