Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2011 Advanced Micro Devices, Inc.
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21.  * SOFTWARE.
  22.  *
  23.  * Authors: Tom Stellard <thomas.stellard@amd.com>
  24.  *
  25.  */
  26. #include "radeon_llvm.h"
  27.  
  28. #include "gallivm/lp_bld_const.h"
  29. #include "gallivm/lp_bld_gather.h"
  30. #include "gallivm/lp_bld_flow.h"
  31. #include "gallivm/lp_bld_init.h"
  32. #include "gallivm/lp_bld_intr.h"
  33. #include "gallivm/lp_bld_swizzle.h"
  34. #include "tgsi/tgsi_info.h"
  35. #include "tgsi/tgsi_parse.h"
  36. #include "util/u_math.h"
  37. #include "util/u_memory.h"
  38. #include "util/u_debug.h"
  39.  
  40. #include <llvm-c/Core.h>
  41. #include <llvm-c/Transforms/Scalar.h>
  42.  
  43. static struct radeon_llvm_loop * get_current_loop(struct radeon_llvm_context * ctx)
  44. {
  45.         return ctx->loop_depth > 0 ? ctx->loop + (ctx->loop_depth - 1) : NULL;
  46. }
  47.  
  48. static struct radeon_llvm_branch * get_current_branch(
  49.         struct radeon_llvm_context * ctx)
  50. {
  51.         return ctx->branch_depth > 0 ?
  52.                         ctx->branch + (ctx->branch_depth - 1) : NULL;
  53. }
  54.  
  55. unsigned radeon_llvm_reg_index_soa(unsigned index, unsigned chan)
  56. {
  57.  return (index * 4) + chan;
  58. }
  59.  
  60. static LLVMValueRef emit_swizzle(
  61.         struct lp_build_tgsi_context * bld_base,
  62.         LLVMValueRef value,
  63.         unsigned swizzle_x,
  64.         unsigned swizzle_y,
  65.         unsigned swizzle_z,
  66.         unsigned swizzle_w)
  67. {
  68.         LLVMValueRef swizzles[4];
  69.         LLVMTypeRef i32t =
  70.                 LLVMInt32TypeInContext(bld_base->base.gallivm->context);
  71.  
  72.         swizzles[0] = LLVMConstInt(i32t, swizzle_x, 0);
  73.         swizzles[1] = LLVMConstInt(i32t, swizzle_y, 0);
  74.         swizzles[2] = LLVMConstInt(i32t, swizzle_z, 0);
  75.         swizzles[3] = LLVMConstInt(i32t, swizzle_w, 0);
  76.  
  77.         return LLVMBuildShuffleVector(bld_base->base.gallivm->builder,
  78.                 value,
  79.                 LLVMGetUndef(LLVMTypeOf(value)),
  80.                 LLVMConstVector(swizzles, 4), "");
  81. }
  82.  
  83. static struct tgsi_declaration_range
  84. get_array_range(struct lp_build_tgsi_context *bld_base,
  85.                 unsigned File, const struct tgsi_ind_register *reg)
  86. {
  87.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  88.         if (File != TGSI_FILE_TEMPORARY || reg->ArrayID == 0 ||
  89.             reg->ArrayID > RADEON_LLVM_MAX_ARRAYS) {
  90.                 struct tgsi_declaration_range range;
  91.                 range.First = 0;
  92.                 range.Last = bld_base->info->file_max[File];
  93.                 return range;
  94.         }
  95.  
  96.         return ctx->arrays[reg->ArrayID - 1];
  97. }
  98.  
  99. static LLVMValueRef
  100. emit_array_index(
  101.         struct lp_build_tgsi_soa_context *bld,
  102.         const struct tgsi_ind_register *reg,
  103.         unsigned offset)
  104. {
  105.         struct gallivm_state * gallivm = bld->bld_base.base.gallivm;
  106.  
  107.         LLVMValueRef addr = LLVMBuildLoad(gallivm->builder, bld->addr[reg->Index][reg->Swizzle], "");
  108.         return LLVMBuildAdd(gallivm->builder, addr, lp_build_const_int32(gallivm, offset), "");
  109. }
  110.  
  111. static LLVMValueRef
  112. emit_fetch(
  113.         struct lp_build_tgsi_context *bld_base,
  114.         const struct tgsi_full_src_register *reg,
  115.         enum tgsi_opcode_type type,
  116.         unsigned swizzle);
  117.  
  118. static LLVMValueRef
  119. emit_array_fetch(
  120.         struct lp_build_tgsi_context *bld_base,
  121.         unsigned File, enum tgsi_opcode_type type,
  122.         struct tgsi_declaration_range range,
  123.         unsigned swizzle)
  124. {
  125.         struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
  126.         struct gallivm_state * gallivm = bld->bld_base.base.gallivm;
  127.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  128.  
  129.         unsigned i, size = range.Last - range.First + 1;
  130.         LLVMTypeRef vec = LLVMVectorType(tgsi2llvmtype(bld_base, type), size);
  131.         LLVMValueRef result = LLVMGetUndef(vec);
  132.  
  133.         struct tgsi_full_src_register tmp_reg = {};
  134.         tmp_reg.Register.File = File;
  135.  
  136.         for (i = 0; i < size; ++i) {
  137.                 tmp_reg.Register.Index = i + range.First;
  138.                 LLVMValueRef temp = emit_fetch(bld_base, &tmp_reg, type, swizzle);
  139.                 result = LLVMBuildInsertElement(builder, result, temp,
  140.                         lp_build_const_int32(gallivm, i), "");
  141.         }
  142.         return result;
  143. }
  144.  
  145. static bool uses_temp_indirect_addressing(
  146.         struct lp_build_tgsi_context *bld_base)
  147. {
  148.         struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
  149.         return (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY));
  150. }
  151.  
  152. static LLVMValueRef
  153. emit_fetch(
  154.         struct lp_build_tgsi_context *bld_base,
  155.         const struct tgsi_full_src_register *reg,
  156.         enum tgsi_opcode_type type,
  157.         unsigned swizzle)
  158. {
  159.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  160.         struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
  161.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  162.         LLVMValueRef result = NULL, ptr;
  163.  
  164.         if (swizzle == ~0) {
  165.                 LLVMValueRef values[TGSI_NUM_CHANNELS];
  166.                 unsigned chan;
  167.                 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
  168.                         values[chan] = emit_fetch(bld_base, reg, type, chan);
  169.                 }
  170.                 return lp_build_gather_values(bld_base->base.gallivm, values,
  171.                                               TGSI_NUM_CHANNELS);
  172.         }
  173.  
  174.         if (reg->Register.Indirect) {
  175.                 struct tgsi_declaration_range range = get_array_range(bld_base,
  176.                         reg->Register.File, &reg->Indirect);
  177.                 return LLVMBuildExtractElement(builder,
  178.                         emit_array_fetch(bld_base, reg->Register.File, type, range, swizzle),
  179.                         emit_array_index(bld, &reg->Indirect, reg->Register.Index - range.First),
  180.                         "");
  181.         }
  182.  
  183.         switch(reg->Register.File) {
  184.         case TGSI_FILE_IMMEDIATE: {
  185.                 LLVMTypeRef ctype = tgsi2llvmtype(bld_base, type);
  186.                 return LLVMConstBitCast(bld->immediates[reg->Register.Index][swizzle], ctype);
  187.         }
  188.  
  189.         case TGSI_FILE_INPUT:
  190.                 result = ctx->inputs[radeon_llvm_reg_index_soa(reg->Register.Index, swizzle)];
  191.                 break;
  192.  
  193.         case TGSI_FILE_TEMPORARY:
  194.                 if (reg->Register.Index >= ctx->temps_count)
  195.                         return LLVMGetUndef(tgsi2llvmtype(bld_base, type));
  196.                 if (uses_temp_indirect_addressing(bld_base)) {
  197.                         ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index, swizzle);
  198.                         break;
  199.                 }
  200.                 ptr = ctx->temps[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle];
  201.                 result = LLVMBuildLoad(builder, ptr, "");
  202.                 break;
  203.  
  204.         case TGSI_FILE_OUTPUT:
  205.                 ptr = lp_get_output_ptr(bld, reg->Register.Index, swizzle);
  206.                 result = LLVMBuildLoad(builder, ptr, "");
  207.                 break;
  208.  
  209.         default:
  210.                 return LLVMGetUndef(tgsi2llvmtype(bld_base, type));
  211.         }
  212.  
  213.         return bitcast(bld_base, type, result);
  214. }
  215.  
  216. static LLVMValueRef fetch_system_value(
  217.         struct lp_build_tgsi_context * bld_base,
  218.         const struct tgsi_full_src_register *reg,
  219.         enum tgsi_opcode_type type,
  220.         unsigned swizzle)
  221. {
  222.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  223.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  224.  
  225.         LLVMValueRef cval = ctx->system_values[reg->Register.Index];
  226.         if (LLVMGetTypeKind(LLVMTypeOf(cval)) == LLVMVectorTypeKind) {
  227.                 cval = LLVMBuildExtractElement(gallivm->builder, cval,
  228.                                                lp_build_const_int32(gallivm, swizzle), "");
  229.         }
  230.         return bitcast(bld_base, type, cval);
  231. }
  232.  
  233. static void emit_declaration(
  234.         struct lp_build_tgsi_context * bld_base,
  235.         const struct tgsi_full_declaration *decl)
  236. {
  237.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  238.         unsigned first, last, i, idx;
  239.         switch(decl->Declaration.File) {
  240.         case TGSI_FILE_ADDRESS:
  241.         {
  242.                  unsigned idx;
  243.                 for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
  244.                         unsigned chan;
  245.                         for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
  246.                                  ctx->soa.addr[idx][chan] = lp_build_alloca(
  247.                                         &ctx->gallivm,
  248.                                         ctx->soa.bld_base.uint_bld.elem_type, "");
  249.                         }
  250.                 }
  251.                 break;
  252.         }
  253.  
  254.         case TGSI_FILE_TEMPORARY:
  255.                 if (decl->Declaration.Array && decl->Array.ArrayID <= RADEON_LLVM_MAX_ARRAYS)
  256.                         ctx->arrays[decl->Array.ArrayID - 1] = decl->Range;
  257.                 if (uses_temp_indirect_addressing(bld_base)) {
  258.                         lp_emit_declaration_soa(bld_base, decl);
  259.                         break;
  260.                 }
  261.                 first = decl->Range.First;
  262.                 last = decl->Range.Last;
  263.                 if (!ctx->temps_count) {
  264.                         ctx->temps_count = bld_base->info->file_max[TGSI_FILE_TEMPORARY] + 1;
  265.                         ctx->temps = MALLOC(TGSI_NUM_CHANNELS * ctx->temps_count * sizeof(LLVMValueRef));
  266.                 }
  267.                 for (idx = first; idx <= last; idx++) {
  268.                         for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
  269.                                 ctx->temps[idx * TGSI_NUM_CHANNELS + i] =
  270.                                         lp_build_alloca(bld_base->base.gallivm, bld_base->base.vec_type,
  271.                                                 "temp");
  272.                         }
  273.                 }
  274.                 break;
  275.  
  276.         case TGSI_FILE_INPUT:
  277.         {
  278.                 unsigned idx;
  279.                 for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
  280.                         if (ctx->load_input)
  281.                                 ctx->load_input(ctx, idx, decl);
  282.                 }
  283.         }
  284.         break;
  285.  
  286.         case TGSI_FILE_SYSTEM_VALUE:
  287.         {
  288.                 unsigned idx;
  289.                 for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
  290.                         ctx->load_system_value(ctx, idx, decl);
  291.                 }
  292.         }
  293.         break;
  294.  
  295.         case TGSI_FILE_OUTPUT:
  296.         {
  297.                 unsigned idx;
  298.                 for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
  299.                         unsigned chan;
  300.                         assert(idx < RADEON_LLVM_MAX_OUTPUTS);
  301.                         for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
  302.                                 ctx->soa.outputs[idx][chan] = lp_build_alloca(&ctx->gallivm,
  303.                                         ctx->soa.bld_base.base.elem_type, "");
  304.                         }
  305.                 }
  306.  
  307.                 ctx->output_reg_count = MAX2(ctx->output_reg_count,
  308.                                                          decl->Range.Last + 1);
  309.                 break;
  310.         }
  311.  
  312.         default:
  313.                 break;
  314.         }
  315. }
  316.  
  317. static void
  318. emit_store(
  319.         struct lp_build_tgsi_context * bld_base,
  320.         const struct tgsi_full_instruction * inst,
  321.         const struct tgsi_opcode_info * info,
  322.         LLVMValueRef dst[4])
  323. {
  324.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  325.         struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
  326.         struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
  327.         struct lp_build_context base = bld->bld_base.base;
  328.         const struct tgsi_full_dst_register *reg = &inst->Dst[0];
  329.         LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
  330.         LLVMValueRef temp_ptr;
  331.         unsigned chan, chan_index;
  332.         boolean is_vec_store = FALSE;
  333.  
  334.         if (dst[0]) {
  335.                 LLVMTypeKind k = LLVMGetTypeKind(LLVMTypeOf(dst[0]));
  336.                 is_vec_store = (k == LLVMVectorTypeKind);
  337.         }
  338.  
  339.         if (is_vec_store) {
  340.                 LLVMValueRef values[4] = {};
  341.                 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst, chan) {
  342.                         LLVMValueRef index = lp_build_const_int32(gallivm, chan);
  343.                         values[chan]  = LLVMBuildExtractElement(gallivm->builder,
  344.                                                         dst[0], index, "");
  345.                 }
  346.                 bld_base->emit_store(bld_base, inst, info, values);
  347.                 return;
  348.         }
  349.  
  350.         TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
  351.                 LLVMValueRef value = dst[chan_index];
  352.  
  353.                 if (inst->Instruction.Saturate != TGSI_SAT_NONE) {
  354.                         struct lp_build_emit_data clamp_emit_data;
  355.  
  356.                         memset(&clamp_emit_data, 0, sizeof(clamp_emit_data));
  357.                         clamp_emit_data.arg_count = 3;
  358.                         clamp_emit_data.args[0] = value;
  359.                         clamp_emit_data.args[2] = base.one;
  360.  
  361.                         switch(inst->Instruction.Saturate) {
  362.                         case TGSI_SAT_ZERO_ONE:
  363.                                 clamp_emit_data.args[1] = base.zero;
  364.                                 break;
  365.                         case TGSI_SAT_MINUS_PLUS_ONE:
  366.                                 clamp_emit_data.args[1] = LLVMConstReal(
  367.                                                 base.elem_type, -1.0f);
  368.                                 break;
  369.                         default:
  370.                                 assert(0);
  371.                         }
  372.                         value = lp_build_emit_llvm(bld_base, TGSI_OPCODE_CLAMP,
  373.                                                 &clamp_emit_data);
  374.                 }
  375.  
  376.                 if (reg->Register.File == TGSI_FILE_ADDRESS) {
  377.                         temp_ptr = bld->addr[reg->Register.Index][chan_index];
  378.                         LLVMBuildStore(builder, value, temp_ptr);
  379.                         continue;
  380.                 }
  381.        
  382.                 value = bitcast(bld_base, TGSI_TYPE_FLOAT, value);
  383.  
  384.                 if (reg->Register.Indirect) {
  385.                         struct tgsi_declaration_range range = get_array_range(bld_base,
  386.                                 reg->Register.File, &reg->Indirect);
  387.  
  388.                         unsigned i, size = range.Last - range.First + 1;
  389.                         LLVMValueRef array = LLVMBuildInsertElement(builder,
  390.                                 emit_array_fetch(bld_base, reg->Register.File, TGSI_TYPE_FLOAT, range, chan_index),
  391.                                 value,  emit_array_index(bld, &reg->Indirect, reg->Register.Index - range.First), "");
  392.  
  393.                         for (i = 0; i < size; ++i) {
  394.                                 switch(reg->Register.File) {
  395.                                 case TGSI_FILE_OUTPUT:
  396.                                         temp_ptr = bld->outputs[i + range.First][chan_index];
  397.                                         break;
  398.  
  399.                                 case TGSI_FILE_TEMPORARY:
  400.                                         if (range.First + i >= ctx->temps_count)
  401.                                                 continue;
  402.                                         if (uses_temp_indirect_addressing(bld_base))
  403.                                                 temp_ptr = lp_get_temp_ptr_soa(bld, i + range.First, chan_index);
  404.                                         else
  405.                                                 temp_ptr = ctx->temps[(i + range.First) * TGSI_NUM_CHANNELS + chan_index];
  406.                                         break;
  407.  
  408.                                 default:
  409.                                         return;
  410.                                 }
  411.                                 value = LLVMBuildExtractElement(builder, array,
  412.                                         lp_build_const_int32(gallivm, i), "");
  413.                                 LLVMBuildStore(builder, value, temp_ptr);
  414.                         }
  415.  
  416.                 } else {
  417.                         switch(reg->Register.File) {
  418.                         case TGSI_FILE_OUTPUT:
  419.                                 temp_ptr = bld->outputs[reg->Register.Index][chan_index];
  420.                                 break;
  421.  
  422.                         case TGSI_FILE_TEMPORARY:
  423.                                 if (reg->Register.Index >= ctx->temps_count)
  424.                                         continue;
  425.                                 if (uses_temp_indirect_addressing(bld_base)) {
  426.                                         temp_ptr = NULL;
  427.                                         break;
  428.                                 }
  429.                                 temp_ptr = ctx->temps[ TGSI_NUM_CHANNELS * reg->Register.Index + chan_index];
  430.                                 break;
  431.  
  432.                         default:
  433.                                 return;
  434.                         }
  435.                         LLVMBuildStore(builder, value, temp_ptr);
  436.                 }
  437.         }
  438. }
  439.  
  440. static void bgnloop_emit(
  441.         const struct lp_build_tgsi_action * action,
  442.         struct lp_build_tgsi_context * bld_base,
  443.         struct lp_build_emit_data * emit_data)
  444. {
  445.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  446.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  447.         LLVMBasicBlockRef loop_block;
  448.         LLVMBasicBlockRef endloop_block;
  449.         endloop_block = LLVMAppendBasicBlockInContext(gallivm->context,
  450.                                                 ctx->main_fn, "ENDLOOP");
  451.         loop_block = LLVMInsertBasicBlockInContext(gallivm->context,
  452.                                                 endloop_block, "LOOP");
  453.         LLVMBuildBr(gallivm->builder, loop_block);
  454.         LLVMPositionBuilderAtEnd(gallivm->builder, loop_block);
  455.  
  456.         if (++ctx->loop_depth > ctx->loop_depth_max) {
  457.                 unsigned new_max = ctx->loop_depth_max << 1;
  458.  
  459.                 if (!new_max)
  460.                         new_max = RADEON_LLVM_INITIAL_CF_DEPTH;
  461.  
  462.                 ctx->loop = REALLOC(ctx->loop, ctx->loop_depth_max *
  463.                                     sizeof(ctx->loop[0]),
  464.                                     new_max * sizeof(ctx->loop[0]));
  465.                 ctx->loop_depth_max = new_max;
  466.         }
  467.  
  468.         ctx->loop[ctx->loop_depth - 1].loop_block = loop_block;
  469.         ctx->loop[ctx->loop_depth - 1].endloop_block = endloop_block;
  470. }
  471.  
  472. static void brk_emit(
  473.         const struct lp_build_tgsi_action * action,
  474.         struct lp_build_tgsi_context * bld_base,
  475.         struct lp_build_emit_data * emit_data)
  476. {
  477.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  478.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  479.         struct radeon_llvm_loop * current_loop = get_current_loop(ctx);
  480.  
  481.         LLVMBuildBr(gallivm->builder, current_loop->endloop_block);
  482. }
  483.  
  484. static void cont_emit(
  485.         const struct lp_build_tgsi_action * action,
  486.         struct lp_build_tgsi_context * bld_base,
  487.         struct lp_build_emit_data * emit_data)
  488. {
  489.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  490.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  491.         struct radeon_llvm_loop * current_loop = get_current_loop(ctx);
  492.  
  493.         LLVMBuildBr(gallivm->builder, current_loop->loop_block);
  494. }
  495.  
  496. static void else_emit(
  497.         const struct lp_build_tgsi_action * action,
  498.         struct lp_build_tgsi_context * bld_base,
  499.         struct lp_build_emit_data * emit_data)
  500. {
  501.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  502.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  503.         struct radeon_llvm_branch * current_branch = get_current_branch(ctx);
  504.         LLVMBasicBlockRef current_block = LLVMGetInsertBlock(gallivm->builder);
  505.  
  506.         /* We need to add a terminator to the current block if the previous
  507.          * instruction was an ENDIF.Example:
  508.          * IF
  509.          *   [code]
  510.          *   IF
  511.          *     [code]
  512.          *   ELSE
  513.          *    [code]
  514.          *   ENDIF <--
  515.          * ELSE<--
  516.          *   [code]
  517.          * ENDIF
  518.          */
  519.  
  520.         if (current_block != current_branch->if_block) {
  521.                 LLVMBuildBr(gallivm->builder, current_branch->endif_block);
  522.         }
  523.         if (!LLVMGetBasicBlockTerminator(current_branch->if_block)) {
  524.                 LLVMBuildBr(gallivm->builder, current_branch->endif_block);
  525.         }
  526.         current_branch->has_else = 1;
  527.         LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->else_block);
  528. }
  529.  
  530. static void endif_emit(
  531.         const struct lp_build_tgsi_action * action,
  532.         struct lp_build_tgsi_context * bld_base,
  533.         struct lp_build_emit_data * emit_data)
  534. {
  535.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  536.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  537.         struct radeon_llvm_branch * current_branch = get_current_branch(ctx);
  538.         LLVMBasicBlockRef current_block = LLVMGetInsertBlock(gallivm->builder);
  539.  
  540.         /* If we have consecutive ENDIF instructions, then the first ENDIF
  541.          * will not have a terminator, so we need to add one. */
  542.         if (current_block != current_branch->if_block
  543.                         && current_block != current_branch->else_block
  544.                         && !LLVMGetBasicBlockTerminator(current_block)) {
  545.  
  546.                  LLVMBuildBr(gallivm->builder, current_branch->endif_block);
  547.         }
  548.         if (!LLVMGetBasicBlockTerminator(current_branch->else_block)) {
  549.                 LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->else_block);
  550.                 LLVMBuildBr(gallivm->builder, current_branch->endif_block);
  551.         }
  552.  
  553.         if (!LLVMGetBasicBlockTerminator(current_branch->if_block)) {
  554.                 LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->if_block);
  555.                 LLVMBuildBr(gallivm->builder, current_branch->endif_block);
  556.         }
  557.  
  558.         LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->endif_block);
  559.         ctx->branch_depth--;
  560. }
  561.  
  562. static void endloop_emit(
  563.         const struct lp_build_tgsi_action * action,
  564.         struct lp_build_tgsi_context * bld_base,
  565.         struct lp_build_emit_data * emit_data)
  566. {
  567.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  568.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  569.         struct radeon_llvm_loop * current_loop = get_current_loop(ctx);
  570.  
  571.         if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(gallivm->builder))) {
  572.                  LLVMBuildBr(gallivm->builder, current_loop->loop_block);
  573.         }
  574.  
  575.         LLVMPositionBuilderAtEnd(gallivm->builder, current_loop->endloop_block);
  576.         ctx->loop_depth--;
  577. }
  578.  
  579. static void if_cond_emit(
  580.         const struct lp_build_tgsi_action * action,
  581.         struct lp_build_tgsi_context * bld_base,
  582.         struct lp_build_emit_data * emit_data,
  583.         LLVMValueRef cond)
  584. {
  585.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  586.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  587.         LLVMBasicBlockRef if_block, else_block, endif_block;
  588.  
  589.         endif_block = LLVMAppendBasicBlockInContext(gallivm->context,
  590.                                                 ctx->main_fn, "ENDIF");
  591.         if_block = LLVMInsertBasicBlockInContext(gallivm->context,
  592.                                                 endif_block, "IF");
  593.         else_block = LLVMInsertBasicBlockInContext(gallivm->context,
  594.                                                 endif_block, "ELSE");
  595.         LLVMBuildCondBr(gallivm->builder, cond, if_block, else_block);
  596.         LLVMPositionBuilderAtEnd(gallivm->builder, if_block);
  597.  
  598.         if (++ctx->branch_depth > ctx->branch_depth_max) {
  599.                 unsigned new_max = ctx->branch_depth_max << 1;
  600.  
  601.                 if (!new_max)
  602.                         new_max = RADEON_LLVM_INITIAL_CF_DEPTH;
  603.  
  604.                 ctx->branch = REALLOC(ctx->branch, ctx->branch_depth_max *
  605.                                       sizeof(ctx->branch[0]),
  606.                                       new_max * sizeof(ctx->branch[0]));
  607.                 ctx->branch_depth_max = new_max;
  608.         }
  609.  
  610.         ctx->branch[ctx->branch_depth - 1].endif_block = endif_block;
  611.         ctx->branch[ctx->branch_depth - 1].if_block = if_block;
  612.         ctx->branch[ctx->branch_depth - 1].else_block = else_block;
  613.         ctx->branch[ctx->branch_depth - 1].has_else = 0;
  614. }
  615.  
  616. static void if_emit(
  617.         const struct lp_build_tgsi_action * action,
  618.         struct lp_build_tgsi_context * bld_base,
  619.         struct lp_build_emit_data * emit_data)
  620. {
  621.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  622.         LLVMValueRef cond;
  623.  
  624.         cond = LLVMBuildFCmp(gallivm->builder, LLVMRealUNE,
  625.                         emit_data->args[0],
  626.                         bld_base->base.zero, "");
  627.  
  628.         if_cond_emit(action, bld_base, emit_data, cond);
  629. }
  630.  
  631. static void uif_emit(
  632.         const struct lp_build_tgsi_action * action,
  633.         struct lp_build_tgsi_context * bld_base,
  634.         struct lp_build_emit_data * emit_data)
  635. {
  636.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  637.         LLVMValueRef cond;
  638.  
  639.         cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE,
  640.                 bitcast(bld_base, TGSI_TYPE_UNSIGNED, emit_data->args[0]),
  641.                         bld_base->int_bld.zero, "");
  642.  
  643.         if_cond_emit(action, bld_base, emit_data, cond);
  644. }
  645.  
  646. static void kill_if_fetch_args(
  647.         struct lp_build_tgsi_context * bld_base,
  648.         struct lp_build_emit_data * emit_data)
  649. {
  650.         const struct tgsi_full_instruction * inst = emit_data->inst;
  651.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  652.         LLVMBuilderRef builder = gallivm->builder;
  653.         unsigned i;
  654.         LLVMValueRef conds[TGSI_NUM_CHANNELS];
  655.  
  656.         for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
  657.                 LLVMValueRef value = lp_build_emit_fetch(bld_base, inst, 0, i);
  658.                 conds[i] = LLVMBuildFCmp(builder, LLVMRealOLT, value,
  659.                                         bld_base->base.zero, "");
  660.         }
  661.  
  662.         /* Or the conditions together */
  663.         for (i = TGSI_NUM_CHANNELS - 1; i > 0; i--) {
  664.                 conds[i - 1] = LLVMBuildOr(builder, conds[i], conds[i - 1], "");
  665.         }
  666.  
  667.         emit_data->dst_type = LLVMVoidTypeInContext(gallivm->context);
  668.         emit_data->arg_count = 1;
  669.         emit_data->args[0] = LLVMBuildSelect(builder, conds[0],
  670.                                         lp_build_const_float(gallivm, -1.0f),
  671.                                         bld_base->base.zero, "");
  672. }
  673.  
  674. static void kil_emit(
  675.         const struct lp_build_tgsi_action * action,
  676.         struct lp_build_tgsi_context * bld_base,
  677.         struct lp_build_emit_data * emit_data)
  678. {
  679.         unsigned i;
  680.         for (i = 0; i < emit_data->arg_count; i++) {
  681.                 emit_data->output[i] = lp_build_intrinsic_unary(
  682.                         bld_base->base.gallivm->builder,
  683.                         action->intr_name,
  684.                         emit_data->dst_type, emit_data->args[i]);
  685.         }
  686. }
  687.  
  688. void radeon_llvm_emit_prepare_cube_coords(
  689.                 struct lp_build_tgsi_context * bld_base,
  690.                 struct lp_build_emit_data * emit_data,
  691.                 LLVMValueRef *coords_arg)
  692. {
  693.  
  694.         unsigned target = emit_data->inst->Texture.Texture;
  695.         unsigned opcode = emit_data->inst->Instruction.Opcode;
  696.         struct gallivm_state * gallivm = bld_base->base.gallivm;
  697.         LLVMBuilderRef builder = gallivm->builder;
  698.         LLVMTypeRef type = bld_base->base.elem_type;
  699.         LLVMValueRef coords[4];
  700.         LLVMValueRef mad_args[3];
  701.         LLVMValueRef idx;
  702.         struct LLVMOpaqueValue *cube_vec;
  703.         LLVMValueRef v;
  704.         unsigned i;
  705.  
  706.         cube_vec = lp_build_gather_values(bld_base->base.gallivm, coords_arg, 4);
  707.         v = build_intrinsic(builder, "llvm.AMDGPU.cube", LLVMVectorType(type, 4),
  708.                             &cube_vec, 1, LLVMReadNoneAttribute);
  709.  
  710.         for (i = 0; i < 4; ++i) {
  711.                 idx = lp_build_const_int32(gallivm, i);
  712.                 coords[i] = LLVMBuildExtractElement(builder, v, idx, "");
  713.         }
  714.  
  715.         coords[2] = build_intrinsic(builder, "fabs",
  716.                         type, &coords[2], 1, LLVMReadNoneAttribute);
  717.         coords[2] = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_RCP, coords[2]);
  718.  
  719.         mad_args[1] = coords[2];
  720.         mad_args[2] = LLVMConstReal(type, 1.5);
  721.  
  722.         mad_args[0] = coords[0];
  723.         coords[0] = lp_build_emit_llvm_ternary(bld_base, TGSI_OPCODE_MAD,
  724.                         mad_args[0], mad_args[1], mad_args[2]);
  725.  
  726.         mad_args[0] = coords[1];
  727.         coords[1] = lp_build_emit_llvm_ternary(bld_base, TGSI_OPCODE_MAD,
  728.                         mad_args[0], mad_args[1], mad_args[2]);
  729.  
  730.         /* apply xyz = yxw swizzle to cooords */
  731.         coords[2] = coords[3];
  732.         coords[3] = coords[1];
  733.         coords[1] = coords[0];
  734.         coords[0] = coords[3];
  735.  
  736.         if (target == TGSI_TEXTURE_CUBE_ARRAY ||
  737.             target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
  738.                 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
  739.                 /* coords_arg.w component - array_index for cube arrays */
  740.                 coords[2] = lp_build_emit_llvm_ternary(bld_base, TGSI_OPCODE_MAD,
  741.                                                        coords_arg[3], lp_build_const_float(gallivm, 8.0), coords[2]);
  742.         }
  743.  
  744.         /* Preserve compare/lod/bias. Put it in coords.w. */
  745.         if (opcode == TGSI_OPCODE_TEX2 ||
  746.             opcode == TGSI_OPCODE_TXB2 ||
  747.             opcode == TGSI_OPCODE_TXL2) {
  748.                 coords[3] = coords_arg[4];
  749.         } else if (opcode == TGSI_OPCODE_TXB ||
  750.                    opcode == TGSI_OPCODE_TXL ||
  751.                    target == TGSI_TEXTURE_SHADOWCUBE) {
  752.                 coords[3] = coords_arg[3];
  753.         }
  754.  
  755.         memcpy(coords_arg, coords, sizeof(coords));
  756. }
  757.  
  758. static void txd_fetch_args(
  759.         struct lp_build_tgsi_context * bld_base,
  760.         struct lp_build_emit_data * emit_data)
  761. {
  762.         const struct tgsi_full_instruction * inst = emit_data->inst;
  763.  
  764.         LLVMValueRef coords[4];
  765.         unsigned chan, src;
  766.         for (src = 0; src < 3; src++) {
  767.                 for (chan = 0; chan < 4; chan++)
  768.                         coords[chan] = lp_build_emit_fetch(bld_base, inst, src, chan);
  769.  
  770.                 emit_data->args[src] = lp_build_gather_values(bld_base->base.gallivm,
  771.                                 coords, 4);
  772.         }
  773.         emit_data->arg_count = 3;
  774.         emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
  775. }
  776.  
  777.  
  778. static void txp_fetch_args(
  779.         struct lp_build_tgsi_context * bld_base,
  780.         struct lp_build_emit_data * emit_data)
  781. {
  782.         const struct tgsi_full_instruction * inst = emit_data->inst;
  783.         LLVMValueRef src_w;
  784.         unsigned chan;
  785.         LLVMValueRef coords[5];
  786.  
  787.         emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
  788.         src_w = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
  789.  
  790.         for (chan = 0; chan < 3; chan++ ) {
  791.                 LLVMValueRef arg = lp_build_emit_fetch(bld_base,
  792.                                                 emit_data->inst, 0, chan);
  793.                 coords[chan] = lp_build_emit_llvm_binary(bld_base,
  794.                                         TGSI_OPCODE_DIV, arg, src_w);
  795.         }
  796.         coords[3] = bld_base->base.one;
  797.  
  798.         if ((inst->Texture.Texture == TGSI_TEXTURE_CUBE ||
  799.              inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
  800.              inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
  801.              inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) &&
  802.             inst->Instruction.Opcode != TGSI_OPCODE_TXQ &&
  803.             inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ) {
  804.                 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
  805.         }
  806.  
  807.         emit_data->args[0] = lp_build_gather_values(bld_base->base.gallivm,
  808.                                                 coords, 4);
  809.         emit_data->arg_count = 1;
  810. }
  811.  
  812. static void tex_fetch_args(
  813.         struct lp_build_tgsi_context * bld_base,
  814.         struct lp_build_emit_data * emit_data)
  815. {
  816.         /* XXX: lp_build_swizzle_aos() was failing with wrong arg types,
  817.          * when we used CHAN_ALL.  We should be able to get this to work,
  818.          * but for now we will swizzle it ourselves
  819.         emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
  820.                                                  0, CHAN_ALL);
  821.  
  822.         */
  823.  
  824.         const struct tgsi_full_instruction * inst = emit_data->inst;
  825.  
  826.         LLVMValueRef coords[5];
  827.         unsigned chan;
  828.         for (chan = 0; chan < 4; chan++) {
  829.                 coords[chan] = lp_build_emit_fetch(bld_base, inst, 0, chan);
  830.         }
  831.  
  832.         if (inst->Instruction.Opcode == TGSI_OPCODE_TEX2 ||
  833.                 inst->Instruction.Opcode == TGSI_OPCODE_TXB2 ||
  834.                 inst->Instruction.Opcode == TGSI_OPCODE_TXL2) {
  835.                 /* These instructions have additional operand that should be packed
  836.                  * into the cube coord vector by radeon_llvm_emit_prepare_cube_coords.
  837.                  * That operand should be passed as a float value in the args array
  838.                  * right after the coord vector. After packing it's not used anymore,
  839.                  * that's why arg_count is not increased */
  840.                 coords[4] = lp_build_emit_fetch(bld_base, inst, 1, 0);
  841.         }
  842.  
  843.         if ((inst->Texture.Texture == TGSI_TEXTURE_CUBE ||
  844.              inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
  845.              inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
  846.              inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) &&
  847.             inst->Instruction.Opcode != TGSI_OPCODE_TXQ &&
  848.             inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ) {
  849.                 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
  850.         }
  851.  
  852.         emit_data->arg_count = 1;
  853.         emit_data->args[0] = lp_build_gather_values(bld_base->base.gallivm,
  854.                                                 coords, 4);
  855.         emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
  856. }
  857.  
  858. static void txf_fetch_args(
  859.         struct lp_build_tgsi_context * bld_base,
  860.         struct lp_build_emit_data * emit_data)
  861. {
  862.         const struct tgsi_full_instruction * inst = emit_data->inst;
  863.         struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
  864.         const struct tgsi_texture_offset * off = inst->TexOffsets;
  865.         LLVMTypeRef offset_type = bld_base->int_bld.elem_type;
  866.  
  867.         /* fetch tex coords */
  868.         tex_fetch_args(bld_base, emit_data);
  869.  
  870.         /* fetch tex offsets */
  871.         if (inst->Texture.NumOffsets) {
  872.                 assert(inst->Texture.NumOffsets == 1);
  873.  
  874.                 emit_data->args[1] = LLVMConstBitCast(
  875.                         bld->immediates[off->Index][off->SwizzleX],
  876.                         offset_type);
  877.                 emit_data->args[2] = LLVMConstBitCast(
  878.                         bld->immediates[off->Index][off->SwizzleY],
  879.                         offset_type);
  880.                 emit_data->args[3] = LLVMConstBitCast(
  881.                         bld->immediates[off->Index][off->SwizzleZ],
  882.                         offset_type);
  883.         } else {
  884.                 emit_data->args[1] = bld_base->int_bld.zero;
  885.                 emit_data->args[2] = bld_base->int_bld.zero;
  886.                 emit_data->args[3] = bld_base->int_bld.zero;
  887.         }
  888.  
  889.         emit_data->arg_count = 4;
  890. }
  891.  
  892. static void emit_icmp(
  893.                 const struct lp_build_tgsi_action * action,
  894.                 struct lp_build_tgsi_context * bld_base,
  895.                 struct lp_build_emit_data * emit_data)
  896. {
  897.         unsigned pred;
  898.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  899.         LLVMContextRef context = bld_base->base.gallivm->context;
  900.  
  901.         switch (emit_data->inst->Instruction.Opcode) {
  902.         case TGSI_OPCODE_USEQ: pred = LLVMIntEQ; break;
  903.         case TGSI_OPCODE_USNE: pred = LLVMIntNE; break;
  904.         case TGSI_OPCODE_USGE: pred = LLVMIntUGE; break;
  905.         case TGSI_OPCODE_USLT: pred = LLVMIntULT; break;
  906.         case TGSI_OPCODE_ISGE: pred = LLVMIntSGE; break;
  907.         case TGSI_OPCODE_ISLT: pred = LLVMIntSLT; break;
  908.         default:
  909.                 assert(!"unknown instruction");
  910.                 pred = 0;
  911.                 break;
  912.         }
  913.  
  914.         LLVMValueRef v = LLVMBuildICmp(builder, pred,
  915.                         emit_data->args[0], emit_data->args[1],"");
  916.  
  917.         v = LLVMBuildSExtOrBitCast(builder, v,
  918.                         LLVMInt32TypeInContext(context), "");
  919.  
  920.         emit_data->output[emit_data->chan] = v;
  921. }
  922.  
  923. static void emit_ucmp(
  924.                 const struct lp_build_tgsi_action * action,
  925.                 struct lp_build_tgsi_context * bld_base,
  926.                 struct lp_build_emit_data * emit_data)
  927. {
  928.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  929.  
  930.         LLVMValueRef arg0 = LLVMBuildBitCast(builder, emit_data->args[0],
  931.                                              bld_base->uint_bld.elem_type, "");
  932.  
  933.         LLVMValueRef v = LLVMBuildICmp(builder, LLVMIntNE, arg0,
  934.                                        bld_base->uint_bld.zero, "");
  935.  
  936.         emit_data->output[emit_data->chan] =
  937.                 LLVMBuildSelect(builder, v, emit_data->args[1], emit_data->args[2], "");
  938. }
  939.  
  940. static void emit_cmp(
  941.                 const struct lp_build_tgsi_action *action,
  942.                 struct lp_build_tgsi_context * bld_base,
  943.                 struct lp_build_emit_data * emit_data)
  944. {
  945.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  946.         LLVMRealPredicate pred;
  947.         LLVMValueRef cond;
  948.  
  949.         /* Use ordered for everything but NE (which is usual for
  950.          * float comparisons)
  951.          */
  952.         switch (emit_data->inst->Instruction.Opcode) {
  953.         case TGSI_OPCODE_SGE: pred = LLVMRealOGE; break;
  954.         case TGSI_OPCODE_SEQ: pred = LLVMRealOEQ; break;
  955.         case TGSI_OPCODE_SLE: pred = LLVMRealOLE; break;
  956.         case TGSI_OPCODE_SLT: pred = LLVMRealOLT; break;
  957.         case TGSI_OPCODE_SNE: pred = LLVMRealUNE; break;
  958.         case TGSI_OPCODE_SGT: pred = LLVMRealOGT; break;
  959.         default: assert(!"unknown instruction"); pred = 0; break;
  960.         }
  961.  
  962.         cond = LLVMBuildFCmp(builder,
  963.                 pred, emit_data->args[0], emit_data->args[1], "");
  964.  
  965.         emit_data->output[emit_data->chan] = LLVMBuildSelect(builder,
  966.                 cond, bld_base->base.one, bld_base->base.zero, "");
  967. }
  968.  
  969. static void emit_fcmp(
  970.                 const struct lp_build_tgsi_action *action,
  971.                 struct lp_build_tgsi_context * bld_base,
  972.                 struct lp_build_emit_data * emit_data)
  973. {
  974.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  975.         LLVMContextRef context = bld_base->base.gallivm->context;
  976.         LLVMRealPredicate pred;
  977.  
  978.         /* Use ordered for everything but NE (which is usual for
  979.          * float comparisons)
  980.          */
  981.         switch (emit_data->inst->Instruction.Opcode) {
  982.         case TGSI_OPCODE_FSEQ: pred = LLVMRealOEQ; break;
  983.         case TGSI_OPCODE_FSGE: pred = LLVMRealOGE; break;
  984.         case TGSI_OPCODE_FSLT: pred = LLVMRealOLT; break;
  985.         case TGSI_OPCODE_FSNE: pred = LLVMRealUNE; break;
  986.         default: assert(!"unknown instruction"); pred = 0; break;
  987.         }
  988.  
  989.         LLVMValueRef v = LLVMBuildFCmp(builder, pred,
  990.                         emit_data->args[0], emit_data->args[1],"");
  991.  
  992.         v = LLVMBuildSExtOrBitCast(builder, v,
  993.                         LLVMInt32TypeInContext(context), "");
  994.  
  995.         emit_data->output[emit_data->chan] = v;
  996. }
  997.  
  998. static void emit_not(
  999.                 const struct lp_build_tgsi_action * action,
  1000.                 struct lp_build_tgsi_context * bld_base,
  1001.                 struct lp_build_emit_data * emit_data)
  1002. {
  1003.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1004.         LLVMValueRef v = bitcast(bld_base, TGSI_TYPE_UNSIGNED,
  1005.                         emit_data->args[0]);
  1006.         emit_data->output[emit_data->chan] = LLVMBuildNot(builder, v, "");
  1007. }
  1008.  
  1009. static void emit_arl(
  1010.                 const struct lp_build_tgsi_action * action,
  1011.                 struct lp_build_tgsi_context * bld_base,
  1012.                 struct lp_build_emit_data * emit_data)
  1013. {
  1014.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1015.         LLVMValueRef floor_index =  lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_FLR, emit_data->args[0]);
  1016.         emit_data->output[emit_data->chan] = LLVMBuildFPToSI(builder,
  1017.                         floor_index, bld_base->base.int_elem_type , "");
  1018. }
  1019.  
  1020. static void emit_and(
  1021.                 const struct lp_build_tgsi_action * action,
  1022.                 struct lp_build_tgsi_context * bld_base,
  1023.                 struct lp_build_emit_data * emit_data)
  1024. {
  1025.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1026.         emit_data->output[emit_data->chan] = LLVMBuildAnd(builder,
  1027.                         emit_data->args[0], emit_data->args[1], "");
  1028. }
  1029.  
  1030. static void emit_or(
  1031.                 const struct lp_build_tgsi_action * action,
  1032.                 struct lp_build_tgsi_context * bld_base,
  1033.                 struct lp_build_emit_data * emit_data)
  1034. {
  1035.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1036.         emit_data->output[emit_data->chan] = LLVMBuildOr(builder,
  1037.                         emit_data->args[0], emit_data->args[1], "");
  1038. }
  1039.  
  1040. static void emit_uadd(
  1041.                 const struct lp_build_tgsi_action * action,
  1042.                 struct lp_build_tgsi_context * bld_base,
  1043.                 struct lp_build_emit_data * emit_data)
  1044. {
  1045.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1046.         emit_data->output[emit_data->chan] = LLVMBuildAdd(builder,
  1047.                         emit_data->args[0], emit_data->args[1], "");
  1048. }
  1049.  
  1050. static void emit_udiv(
  1051.                 const struct lp_build_tgsi_action * action,
  1052.                 struct lp_build_tgsi_context * bld_base,
  1053.                 struct lp_build_emit_data * emit_data)
  1054. {
  1055.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1056.         emit_data->output[emit_data->chan] = LLVMBuildUDiv(builder,
  1057.                         emit_data->args[0], emit_data->args[1], "");
  1058. }
  1059.  
  1060. static void emit_idiv(
  1061.                 const struct lp_build_tgsi_action * action,
  1062.                 struct lp_build_tgsi_context * bld_base,
  1063.                 struct lp_build_emit_data * emit_data)
  1064. {
  1065.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1066.         emit_data->output[emit_data->chan] = LLVMBuildSDiv(builder,
  1067.                         emit_data->args[0], emit_data->args[1], "");
  1068. }
  1069.  
  1070. static void emit_mod(
  1071.                 const struct lp_build_tgsi_action * action,
  1072.                 struct lp_build_tgsi_context * bld_base,
  1073.                 struct lp_build_emit_data * emit_data)
  1074. {
  1075.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1076.         emit_data->output[emit_data->chan] = LLVMBuildSRem(builder,
  1077.                         emit_data->args[0], emit_data->args[1], "");
  1078. }
  1079.  
  1080. static void emit_umod(
  1081.                 const struct lp_build_tgsi_action * action,
  1082.                 struct lp_build_tgsi_context * bld_base,
  1083.                 struct lp_build_emit_data * emit_data)
  1084. {
  1085.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1086.         emit_data->output[emit_data->chan] = LLVMBuildURem(builder,
  1087.                         emit_data->args[0], emit_data->args[1], "");
  1088. }
  1089.  
  1090. static void emit_shl(
  1091.                 const struct lp_build_tgsi_action * action,
  1092.                 struct lp_build_tgsi_context * bld_base,
  1093.                 struct lp_build_emit_data * emit_data)
  1094. {
  1095.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1096.         emit_data->output[emit_data->chan] = LLVMBuildShl(builder,
  1097.                         emit_data->args[0], emit_data->args[1], "");
  1098. }
  1099.  
  1100. static void emit_ushr(
  1101.                 const struct lp_build_tgsi_action * action,
  1102.                 struct lp_build_tgsi_context * bld_base,
  1103.                 struct lp_build_emit_data * emit_data)
  1104. {
  1105.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1106.         emit_data->output[emit_data->chan] = LLVMBuildLShr(builder,
  1107.                         emit_data->args[0], emit_data->args[1], "");
  1108. }
  1109. static void emit_ishr(
  1110.                 const struct lp_build_tgsi_action * action,
  1111.                 struct lp_build_tgsi_context * bld_base,
  1112.                 struct lp_build_emit_data * emit_data)
  1113. {
  1114.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1115.         emit_data->output[emit_data->chan] = LLVMBuildAShr(builder,
  1116.                         emit_data->args[0], emit_data->args[1], "");
  1117. }
  1118.  
  1119. static void emit_xor(
  1120.                 const struct lp_build_tgsi_action * action,
  1121.                 struct lp_build_tgsi_context * bld_base,
  1122.                 struct lp_build_emit_data * emit_data)
  1123. {
  1124.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1125.         emit_data->output[emit_data->chan] = LLVMBuildXor(builder,
  1126.                         emit_data->args[0], emit_data->args[1], "");
  1127. }
  1128.  
  1129. static void emit_ssg(
  1130.                 const struct lp_build_tgsi_action * action,
  1131.                 struct lp_build_tgsi_context * bld_base,
  1132.                 struct lp_build_emit_data * emit_data)
  1133. {
  1134.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1135.  
  1136.         LLVMValueRef cmp, val;
  1137.  
  1138.         if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_ISSG) {
  1139.                 cmp = LLVMBuildICmp(builder, LLVMIntSGT, emit_data->args[0], bld_base->int_bld.zero, "");
  1140.                 val = LLVMBuildSelect(builder, cmp, bld_base->int_bld.one, emit_data->args[0], "");
  1141.                 cmp = LLVMBuildICmp(builder, LLVMIntSGE, val, bld_base->int_bld.zero, "");
  1142.                 val = LLVMBuildSelect(builder, cmp, val, LLVMConstInt(bld_base->int_bld.elem_type, -1, true), "");
  1143.         } else { // float SSG
  1144.                 cmp = LLVMBuildFCmp(builder, LLVMRealOGT, emit_data->args[0], bld_base->base.zero, "");
  1145.                 val = LLVMBuildSelect(builder, cmp, bld_base->base.one, emit_data->args[0], "");
  1146.                 cmp = LLVMBuildFCmp(builder, LLVMRealOGE, val, bld_base->base.zero, "");
  1147.                 val = LLVMBuildSelect(builder, cmp, val, LLVMConstReal(bld_base->base.elem_type, -1), "");
  1148.         }
  1149.  
  1150.         emit_data->output[emit_data->chan] = val;
  1151. }
  1152.  
  1153. static void emit_ineg(
  1154.                 const struct lp_build_tgsi_action * action,
  1155.                 struct lp_build_tgsi_context * bld_base,
  1156.                 struct lp_build_emit_data * emit_data)
  1157. {
  1158.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1159.         emit_data->output[emit_data->chan] = LLVMBuildNeg(builder,
  1160.                         emit_data->args[0], "");
  1161. }
  1162.  
  1163. static void emit_f2i(
  1164.                 const struct lp_build_tgsi_action * action,
  1165.                 struct lp_build_tgsi_context * bld_base,
  1166.                 struct lp_build_emit_data * emit_data)
  1167. {
  1168.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1169.         emit_data->output[emit_data->chan] = LLVMBuildFPToSI(builder,
  1170.                         emit_data->args[0], bld_base->int_bld.elem_type, "");
  1171. }
  1172.  
  1173. static void emit_f2u(
  1174.                 const struct lp_build_tgsi_action * action,
  1175.                 struct lp_build_tgsi_context * bld_base,
  1176.                 struct lp_build_emit_data * emit_data)
  1177. {
  1178.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1179.         emit_data->output[emit_data->chan] = LLVMBuildFPToUI(builder,
  1180.                         emit_data->args[0], bld_base->uint_bld.elem_type, "");
  1181. }
  1182.  
  1183. static void emit_i2f(
  1184.                 const struct lp_build_tgsi_action * action,
  1185.                 struct lp_build_tgsi_context * bld_base,
  1186.                 struct lp_build_emit_data * emit_data)
  1187. {
  1188.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1189.         emit_data->output[emit_data->chan] = LLVMBuildSIToFP(builder,
  1190.                         emit_data->args[0], bld_base->base.elem_type, "");
  1191. }
  1192.  
  1193. static void emit_u2f(
  1194.                 const struct lp_build_tgsi_action * action,
  1195.                 struct lp_build_tgsi_context * bld_base,
  1196.                 struct lp_build_emit_data * emit_data)
  1197. {
  1198.         LLVMBuilderRef builder = bld_base->base.gallivm->builder;
  1199.         emit_data->output[emit_data->chan] = LLVMBuildUIToFP(builder,
  1200.                         emit_data->args[0], bld_base->base.elem_type, "");
  1201. }
  1202.  
  1203. static void emit_immediate(struct lp_build_tgsi_context * bld_base,
  1204.                 const struct tgsi_full_immediate *imm)
  1205. {
  1206.         unsigned i;
  1207.         struct radeon_llvm_context * ctx = radeon_llvm_context(bld_base);
  1208.  
  1209.         for (i = 0; i < 4; ++i) {
  1210.                 ctx->soa.immediates[ctx->soa.num_immediates][i] =
  1211.                                 LLVMConstInt(bld_base->uint_bld.elem_type, imm->u[i].Uint, false   );
  1212.         }
  1213.  
  1214.         ctx->soa.num_immediates++;
  1215. }
  1216.  
  1217. LLVMValueRef
  1218. build_intrinsic(LLVMBuilderRef builder,
  1219.                    const char *name,
  1220.                    LLVMTypeRef ret_type,
  1221.                    LLVMValueRef *args,
  1222.                    unsigned num_args,
  1223.                    LLVMAttribute attr)
  1224. {
  1225.    LLVMModuleRef module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)));
  1226.    LLVMValueRef function;
  1227.  
  1228.    function = LLVMGetNamedFunction(module, name);
  1229.    if(!function) {
  1230.       LLVMTypeRef arg_types[LP_MAX_FUNC_ARGS];
  1231.       unsigned i;
  1232.  
  1233.       assert(num_args <= LP_MAX_FUNC_ARGS);
  1234.  
  1235.       for(i = 0; i < num_args; ++i) {
  1236.          assert(args[i]);
  1237.          arg_types[i] = LLVMTypeOf(args[i]);
  1238.       }
  1239.  
  1240.       function = lp_declare_intrinsic(module, name, ret_type, arg_types, num_args);
  1241.  
  1242.       if (attr)
  1243.           LLVMAddFunctionAttr(function, attr);
  1244.    }
  1245.  
  1246.    return LLVMBuildCall(builder, function, args, num_args, "");
  1247. }
  1248.  
  1249. static void build_tgsi_intrinsic(
  1250.  const struct lp_build_tgsi_action * action,
  1251.  struct lp_build_tgsi_context * bld_base,
  1252.  struct lp_build_emit_data * emit_data,
  1253.  LLVMAttribute attr)
  1254. {
  1255.    struct lp_build_context * base = &bld_base->base;
  1256.    emit_data->output[emit_data->chan] = build_intrinsic(
  1257.                base->gallivm->builder, action->intr_name,
  1258.                emit_data->dst_type, emit_data->args,
  1259.                emit_data->arg_count, attr);
  1260. }
  1261.  
  1262. void
  1263. build_tgsi_intrinsic_nomem(
  1264.  const struct lp_build_tgsi_action * action,
  1265.  struct lp_build_tgsi_context * bld_base,
  1266.  struct lp_build_emit_data * emit_data)
  1267. {
  1268.         build_tgsi_intrinsic(action, bld_base, emit_data, LLVMReadNoneAttribute);
  1269. }
  1270.  
  1271. static void emit_bfi(const struct lp_build_tgsi_action * action,
  1272.                      struct lp_build_tgsi_context * bld_base,
  1273.                      struct lp_build_emit_data * emit_data)
  1274. {
  1275.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  1276.         LLVMBuilderRef builder = gallivm->builder;
  1277.         LLVMValueRef bfi_args[3];
  1278.  
  1279.         // Calculate the bitmask: (((1 << src3) - 1) << src2
  1280.         bfi_args[0] = LLVMBuildShl(builder,
  1281.                                    LLVMBuildSub(builder,
  1282.                                                 LLVMBuildShl(builder,
  1283.                                                              bld_base->int_bld.one,
  1284.                                                              emit_data->args[3], ""),
  1285.                                                 bld_base->int_bld.one, ""),
  1286.                                    emit_data->args[2], "");
  1287.  
  1288.         bfi_args[1] = LLVMBuildShl(builder, emit_data->args[1],
  1289.                                    emit_data->args[2], "");
  1290.  
  1291.         bfi_args[2] = emit_data->args[0];
  1292.  
  1293.         /* Calculate:
  1294.          *   (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
  1295.          * Use the right-hand side, which the LLVM backend can convert to V_BFI.
  1296.          */
  1297.         emit_data->output[emit_data->chan] =
  1298.                 LLVMBuildXor(builder, bfi_args[2],
  1299.                         LLVMBuildAnd(builder, bfi_args[0],
  1300.                                 LLVMBuildXor(builder, bfi_args[1], bfi_args[2],
  1301.                                              ""), ""), "");
  1302. }
  1303.  
  1304. /* this is ffs in C */
  1305. static void emit_lsb(const struct lp_build_tgsi_action * action,
  1306.                      struct lp_build_tgsi_context * bld_base,
  1307.                      struct lp_build_emit_data * emit_data)
  1308. {
  1309.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  1310.         LLVMValueRef args[2] = {
  1311.                 emit_data->args[0],
  1312.  
  1313.                 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
  1314.                  * add special code to check for x=0. The reason is that
  1315.                  * the LLVM behavior for x=0 is different from what we
  1316.                  * need here.
  1317.                  *
  1318.                  * The hardware already implements the correct behavior.
  1319.                  */
  1320.                 lp_build_const_int32(gallivm, 1)
  1321.         };
  1322.  
  1323.         emit_data->output[emit_data->chan] =
  1324.                 build_intrinsic(gallivm->builder, "llvm.cttz.i32",
  1325.                                 emit_data->dst_type, args, Elements(args),
  1326.                                 LLVMReadNoneAttribute);
  1327. }
  1328.  
  1329. /* Find the last bit set. */
  1330. static void emit_umsb(const struct lp_build_tgsi_action * action,
  1331.                       struct lp_build_tgsi_context * bld_base,
  1332.                       struct lp_build_emit_data * emit_data)
  1333. {
  1334.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  1335.         LLVMBuilderRef builder = gallivm->builder;
  1336.         LLVMValueRef args[2] = {
  1337.                 emit_data->args[0],
  1338.                 /* Don't generate code for handling zero: */
  1339.                 lp_build_const_int32(gallivm, 1)
  1340.         };
  1341.  
  1342.         LLVMValueRef msb =
  1343.                 build_intrinsic(builder, "llvm.ctlz.i32",
  1344.                                 emit_data->dst_type, args, Elements(args),
  1345.                                 LLVMReadNoneAttribute);
  1346.  
  1347.         /* The HW returns the last bit index from MSB, but TGSI wants
  1348.          * the index from LSB. Invert it by doing "31 - msb". */
  1349.         msb = LLVMBuildSub(builder, lp_build_const_int32(gallivm, 31),
  1350.                            msb, "");
  1351.  
  1352.         /* Check for zero: */
  1353.         emit_data->output[emit_data->chan] =
  1354.                 LLVMBuildSelect(builder,
  1355.                                 LLVMBuildICmp(builder, LLVMIntEQ, args[0],
  1356.                                               bld_base->uint_bld.zero, ""),
  1357.                                 lp_build_const_int32(gallivm, -1), msb, "");
  1358. }
  1359.  
  1360. /* Find the last bit opposite of the sign bit. */
  1361. static void emit_imsb(const struct lp_build_tgsi_action * action,
  1362.                      struct lp_build_tgsi_context * bld_base,
  1363.                      struct lp_build_emit_data * emit_data)
  1364. {
  1365.         struct gallivm_state *gallivm = bld_base->base.gallivm;
  1366.         LLVMBuilderRef builder = gallivm->builder;
  1367.         LLVMValueRef arg = emit_data->args[0];
  1368.  
  1369.         LLVMValueRef msb =
  1370.                 build_intrinsic(builder, "llvm.AMDGPU.flbit.i32",
  1371.                                 emit_data->dst_type, &arg, 1,
  1372.                                 LLVMReadNoneAttribute);
  1373.  
  1374.         /* The HW returns the last bit index from MSB, but TGSI wants
  1375.          * the index from LSB. Invert it by doing "31 - msb". */
  1376.         msb = LLVMBuildSub(builder, lp_build_const_int32(gallivm, 31),
  1377.                            msb, "");
  1378.  
  1379.         /* If arg == 0 || arg == -1 (0xffffffff), return -1. */
  1380.         LLVMValueRef all_ones = lp_build_const_int32(gallivm, -1);
  1381.  
  1382.         LLVMValueRef cond =
  1383.                 LLVMBuildOr(builder,
  1384.                             LLVMBuildICmp(builder, LLVMIntEQ, arg,
  1385.                                           bld_base->uint_bld.zero, ""),
  1386.                             LLVMBuildICmp(builder, LLVMIntEQ, arg,
  1387.                                           all_ones, ""), "");
  1388.  
  1389.         emit_data->output[emit_data->chan] =
  1390.                 LLVMBuildSelect(builder, cond, all_ones, msb, "");
  1391. }
  1392.  
  1393. void radeon_llvm_context_init(struct radeon_llvm_context * ctx)
  1394. {
  1395.         struct lp_type type;
  1396.  
  1397.         /* Initialize the gallivm object:
  1398.          * We are only using the module, context, and builder fields of this struct.
  1399.          * This should be enough for us to be able to pass our gallivm struct to the
  1400.          * helper functions in the gallivm module.
  1401.          */
  1402.         memset(&ctx->gallivm, 0, sizeof (ctx->gallivm));
  1403.         memset(&ctx->soa, 0, sizeof(ctx->soa));
  1404.         ctx->gallivm.context = LLVMContextCreate();
  1405.         ctx->gallivm.module = LLVMModuleCreateWithNameInContext("tgsi",
  1406.                                                 ctx->gallivm.context);
  1407.         ctx->gallivm.builder = LLVMCreateBuilderInContext(ctx->gallivm.context);
  1408.  
  1409.         ctx->store_output_intr = "llvm.AMDGPU.store.output.";
  1410.         ctx->swizzle_intr = "llvm.AMDGPU.swizzle";
  1411.         struct lp_build_tgsi_context * bld_base = &ctx->soa.bld_base;
  1412.  
  1413.         /* XXX: We need to revisit this.I think the correct way to do this is
  1414.          * to use length = 4 here and use the elem_bld for everything. */
  1415.         type.floating = TRUE;
  1416.         type.fixed = FALSE;
  1417.         type.sign = TRUE;
  1418.         type.norm = FALSE;
  1419.         type.width = 32;
  1420.         type.length = 1;
  1421.  
  1422.         lp_build_context_init(&bld_base->base, &ctx->gallivm, type);
  1423.         lp_build_context_init(&ctx->soa.bld_base.uint_bld, &ctx->gallivm, lp_uint_type(type));
  1424.         lp_build_context_init(&ctx->soa.bld_base.int_bld, &ctx->gallivm, lp_int_type(type));
  1425.  
  1426.         bld_base->soa = 1;
  1427.         bld_base->emit_store = emit_store;
  1428.         bld_base->emit_swizzle = emit_swizzle;
  1429.         bld_base->emit_declaration = emit_declaration;
  1430.         bld_base->emit_immediate = emit_immediate;
  1431.  
  1432.         bld_base->emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = emit_fetch;
  1433.         bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch;
  1434.         bld_base->emit_fetch_funcs[TGSI_FILE_TEMPORARY] = emit_fetch;
  1435.         bld_base->emit_fetch_funcs[TGSI_FILE_OUTPUT] = emit_fetch;
  1436.         bld_base->emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = fetch_system_value;
  1437.  
  1438.         /* Allocate outputs */
  1439.         ctx->soa.outputs = ctx->outputs;
  1440.  
  1441.         ctx->num_arrays = 0;
  1442.  
  1443.         /* XXX: Is there a better way to initialize all this ? */
  1444.  
  1445.         lp_set_default_actions(bld_base);
  1446.  
  1447.         bld_base->op_actions[TGSI_OPCODE_ABS].emit = build_tgsi_intrinsic_nomem;
  1448.         bld_base->op_actions[TGSI_OPCODE_ABS].intr_name = "fabs";
  1449.         bld_base->op_actions[TGSI_OPCODE_AND].emit = emit_and;
  1450.         bld_base->op_actions[TGSI_OPCODE_ARL].emit = emit_arl;
  1451.         bld_base->op_actions[TGSI_OPCODE_BFI].emit = emit_bfi;
  1452.         bld_base->op_actions[TGSI_OPCODE_BGNLOOP].emit = bgnloop_emit;
  1453.         bld_base->op_actions[TGSI_OPCODE_BREV].emit = build_tgsi_intrinsic_nomem;
  1454.         bld_base->op_actions[TGSI_OPCODE_BREV].intr_name = "llvm.AMDGPU.brev";
  1455.         bld_base->op_actions[TGSI_OPCODE_BRK].emit = brk_emit;
  1456.         bld_base->op_actions[TGSI_OPCODE_CEIL].emit = build_tgsi_intrinsic_nomem;
  1457.         bld_base->op_actions[TGSI_OPCODE_CEIL].intr_name = "ceil";
  1458.         bld_base->op_actions[TGSI_OPCODE_CLAMP].emit = build_tgsi_intrinsic_nomem;
  1459.         bld_base->op_actions[TGSI_OPCODE_CLAMP].intr_name = "llvm.AMDIL.clamp.";
  1460.         bld_base->op_actions[TGSI_OPCODE_CMP].emit = build_tgsi_intrinsic_nomem;
  1461.         bld_base->op_actions[TGSI_OPCODE_CMP].intr_name = "llvm.AMDGPU.cndlt";
  1462.         bld_base->op_actions[TGSI_OPCODE_CONT].emit = cont_emit;
  1463.         bld_base->op_actions[TGSI_OPCODE_COS].emit = build_tgsi_intrinsic_nomem;
  1464.         bld_base->op_actions[TGSI_OPCODE_COS].intr_name = "llvm.cos.f32";
  1465.         bld_base->op_actions[TGSI_OPCODE_DDX].intr_name = "llvm.AMDGPU.ddx";
  1466.         bld_base->op_actions[TGSI_OPCODE_DDX].fetch_args = tex_fetch_args;
  1467.         bld_base->op_actions[TGSI_OPCODE_DDY].intr_name = "llvm.AMDGPU.ddy";
  1468.         bld_base->op_actions[TGSI_OPCODE_DDY].fetch_args = tex_fetch_args;
  1469.         bld_base->op_actions[TGSI_OPCODE_ELSE].emit = else_emit;
  1470.         bld_base->op_actions[TGSI_OPCODE_ENDIF].emit = endif_emit;
  1471.         bld_base->op_actions[TGSI_OPCODE_ENDLOOP].emit = endloop_emit;
  1472.         bld_base->op_actions[TGSI_OPCODE_EX2].emit = build_tgsi_intrinsic_nomem;
  1473.         bld_base->op_actions[TGSI_OPCODE_EX2].intr_name = "llvm.AMDIL.exp.";
  1474.         bld_base->op_actions[TGSI_OPCODE_FLR].emit = build_tgsi_intrinsic_nomem;
  1475.         bld_base->op_actions[TGSI_OPCODE_FLR].intr_name = "floor";
  1476.         bld_base->op_actions[TGSI_OPCODE_FMA].emit = build_tgsi_intrinsic_nomem;
  1477.         bld_base->op_actions[TGSI_OPCODE_FMA].intr_name = "llvm.fma.f32";
  1478.         bld_base->op_actions[TGSI_OPCODE_FRC].emit = build_tgsi_intrinsic_nomem;
  1479.         bld_base->op_actions[TGSI_OPCODE_FRC].intr_name = "llvm.AMDIL.fraction.";
  1480.         bld_base->op_actions[TGSI_OPCODE_F2I].emit = emit_f2i;
  1481.         bld_base->op_actions[TGSI_OPCODE_F2U].emit = emit_f2u;
  1482.         bld_base->op_actions[TGSI_OPCODE_FSEQ].emit = emit_fcmp;
  1483.         bld_base->op_actions[TGSI_OPCODE_FSGE].emit = emit_fcmp;
  1484.         bld_base->op_actions[TGSI_OPCODE_FSLT].emit = emit_fcmp;
  1485.         bld_base->op_actions[TGSI_OPCODE_FSNE].emit = emit_fcmp;
  1486.         bld_base->op_actions[TGSI_OPCODE_IABS].emit = build_tgsi_intrinsic_nomem;
  1487.         bld_base->op_actions[TGSI_OPCODE_IABS].intr_name = "llvm.AMDIL.abs.";
  1488.         bld_base->op_actions[TGSI_OPCODE_IBFE].emit = build_tgsi_intrinsic_nomem;
  1489.         bld_base->op_actions[TGSI_OPCODE_IBFE].intr_name = "llvm.AMDGPU.bfe.i32";
  1490.         bld_base->op_actions[TGSI_OPCODE_IDIV].emit = emit_idiv;
  1491.         bld_base->op_actions[TGSI_OPCODE_IF].emit = if_emit;
  1492.         bld_base->op_actions[TGSI_OPCODE_UIF].emit = uif_emit;
  1493.         bld_base->op_actions[TGSI_OPCODE_IMAX].emit = build_tgsi_intrinsic_nomem;
  1494.         bld_base->op_actions[TGSI_OPCODE_IMAX].intr_name = "llvm.AMDGPU.imax";
  1495.         bld_base->op_actions[TGSI_OPCODE_IMIN].emit = build_tgsi_intrinsic_nomem;
  1496.         bld_base->op_actions[TGSI_OPCODE_IMIN].intr_name = "llvm.AMDGPU.imin";
  1497.         bld_base->op_actions[TGSI_OPCODE_IMSB].emit = emit_imsb;
  1498.         bld_base->op_actions[TGSI_OPCODE_INEG].emit = emit_ineg;
  1499.         bld_base->op_actions[TGSI_OPCODE_ISHR].emit = emit_ishr;
  1500.         bld_base->op_actions[TGSI_OPCODE_ISGE].emit = emit_icmp;
  1501.         bld_base->op_actions[TGSI_OPCODE_ISLT].emit = emit_icmp;
  1502.         bld_base->op_actions[TGSI_OPCODE_ISSG].emit = emit_ssg;
  1503.         bld_base->op_actions[TGSI_OPCODE_I2F].emit = emit_i2f;
  1504.         bld_base->op_actions[TGSI_OPCODE_KILL_IF].fetch_args = kill_if_fetch_args;
  1505.         bld_base->op_actions[TGSI_OPCODE_KILL_IF].emit = kil_emit;
  1506.         bld_base->op_actions[TGSI_OPCODE_KILL_IF].intr_name = "llvm.AMDGPU.kill";
  1507.         bld_base->op_actions[TGSI_OPCODE_KILL].emit = lp_build_tgsi_intrinsic;
  1508.         bld_base->op_actions[TGSI_OPCODE_KILL].intr_name = "llvm.AMDGPU.kilp";
  1509.         bld_base->op_actions[TGSI_OPCODE_LSB].emit = emit_lsb;
  1510.         bld_base->op_actions[TGSI_OPCODE_LG2].emit = build_tgsi_intrinsic_nomem;
  1511.         bld_base->op_actions[TGSI_OPCODE_LG2].intr_name = "llvm.log2.f32";
  1512.         bld_base->op_actions[TGSI_OPCODE_LRP].emit = build_tgsi_intrinsic_nomem;
  1513.         bld_base->op_actions[TGSI_OPCODE_LRP].intr_name = "llvm.AMDGPU.lrp";
  1514.         bld_base->op_actions[TGSI_OPCODE_MOD].emit = emit_mod;
  1515.         bld_base->op_actions[TGSI_OPCODE_UMSB].emit = emit_umsb;
  1516.         bld_base->op_actions[TGSI_OPCODE_NOT].emit = emit_not;
  1517.         bld_base->op_actions[TGSI_OPCODE_OR].emit = emit_or;
  1518.         bld_base->op_actions[TGSI_OPCODE_POPC].emit = build_tgsi_intrinsic_nomem;
  1519.         bld_base->op_actions[TGSI_OPCODE_POPC].intr_name = "llvm.ctpop.i32";
  1520.         bld_base->op_actions[TGSI_OPCODE_POW].emit = build_tgsi_intrinsic_nomem;
  1521.         bld_base->op_actions[TGSI_OPCODE_POW].intr_name = "llvm.pow.f32";
  1522.         bld_base->op_actions[TGSI_OPCODE_ROUND].emit = build_tgsi_intrinsic_nomem;
  1523.         bld_base->op_actions[TGSI_OPCODE_ROUND].intr_name = "llvm.AMDIL.round.nearest.";
  1524.         bld_base->op_actions[TGSI_OPCODE_SGE].emit = emit_cmp;
  1525.         bld_base->op_actions[TGSI_OPCODE_SEQ].emit = emit_cmp;
  1526.         bld_base->op_actions[TGSI_OPCODE_SHL].emit = emit_shl;
  1527.         bld_base->op_actions[TGSI_OPCODE_SLE].emit = emit_cmp;
  1528.         bld_base->op_actions[TGSI_OPCODE_SLT].emit = emit_cmp;
  1529.         bld_base->op_actions[TGSI_OPCODE_SNE].emit = emit_cmp;
  1530.         bld_base->op_actions[TGSI_OPCODE_SGT].emit = emit_cmp;
  1531.         bld_base->op_actions[TGSI_OPCODE_SIN].emit = build_tgsi_intrinsic_nomem;
  1532.         bld_base->op_actions[TGSI_OPCODE_SIN].intr_name = "llvm.sin.f32";
  1533.         bld_base->op_actions[TGSI_OPCODE_SQRT].emit = build_tgsi_intrinsic_nomem;
  1534.         bld_base->op_actions[TGSI_OPCODE_SQRT].intr_name = "llvm.sqrt.f32";
  1535.         bld_base->op_actions[TGSI_OPCODE_SSG].emit = emit_ssg;
  1536.         bld_base->op_actions[TGSI_OPCODE_TEX].fetch_args = tex_fetch_args;
  1537.         bld_base->op_actions[TGSI_OPCODE_TEX].intr_name = "llvm.AMDGPU.tex";
  1538.         bld_base->op_actions[TGSI_OPCODE_TEX2].fetch_args = tex_fetch_args;
  1539.         bld_base->op_actions[TGSI_OPCODE_TEX2].intr_name = "llvm.AMDGPU.tex";
  1540.         bld_base->op_actions[TGSI_OPCODE_TXB].fetch_args = tex_fetch_args;
  1541.         bld_base->op_actions[TGSI_OPCODE_TXB].intr_name = "llvm.AMDGPU.txb";
  1542.         bld_base->op_actions[TGSI_OPCODE_TXB2].fetch_args = tex_fetch_args;
  1543.         bld_base->op_actions[TGSI_OPCODE_TXB2].intr_name = "llvm.AMDGPU.txb";
  1544.         bld_base->op_actions[TGSI_OPCODE_TXD].fetch_args = txd_fetch_args;
  1545.         bld_base->op_actions[TGSI_OPCODE_TXD].intr_name = "llvm.AMDGPU.txd";
  1546.         bld_base->op_actions[TGSI_OPCODE_TXF].fetch_args = txf_fetch_args;
  1547.         bld_base->op_actions[TGSI_OPCODE_TXF].intr_name = "llvm.AMDGPU.txf";
  1548.         bld_base->op_actions[TGSI_OPCODE_TXL].fetch_args = tex_fetch_args;
  1549.         bld_base->op_actions[TGSI_OPCODE_TXL].intr_name = "llvm.AMDGPU.txl";
  1550.         bld_base->op_actions[TGSI_OPCODE_TXL2].fetch_args = tex_fetch_args;
  1551.         bld_base->op_actions[TGSI_OPCODE_TXL2].intr_name = "llvm.AMDGPU.txl";
  1552.         bld_base->op_actions[TGSI_OPCODE_TXP].fetch_args = txp_fetch_args;
  1553.         bld_base->op_actions[TGSI_OPCODE_TXP].intr_name = "llvm.AMDGPU.tex";
  1554.         bld_base->op_actions[TGSI_OPCODE_TXQ].fetch_args = tex_fetch_args;
  1555.         bld_base->op_actions[TGSI_OPCODE_TXQ].intr_name = "llvm.AMDGPU.txq";
  1556.         bld_base->op_actions[TGSI_OPCODE_TRUNC].emit = build_tgsi_intrinsic_nomem;
  1557.         bld_base->op_actions[TGSI_OPCODE_TRUNC].intr_name = "llvm.AMDGPU.trunc";
  1558.         bld_base->op_actions[TGSI_OPCODE_UADD].emit = emit_uadd;
  1559.         bld_base->op_actions[TGSI_OPCODE_UBFE].emit = build_tgsi_intrinsic_nomem;
  1560.         bld_base->op_actions[TGSI_OPCODE_UBFE].intr_name = "llvm.AMDGPU.bfe.u32";
  1561.         bld_base->op_actions[TGSI_OPCODE_UDIV].emit = emit_udiv;
  1562.         bld_base->op_actions[TGSI_OPCODE_UMAX].emit = build_tgsi_intrinsic_nomem;
  1563.         bld_base->op_actions[TGSI_OPCODE_UMAX].intr_name = "llvm.AMDGPU.umax";
  1564.         bld_base->op_actions[TGSI_OPCODE_UMIN].emit = build_tgsi_intrinsic_nomem;
  1565.         bld_base->op_actions[TGSI_OPCODE_UMIN].intr_name = "llvm.AMDGPU.umin";
  1566.         bld_base->op_actions[TGSI_OPCODE_UMOD].emit = emit_umod;
  1567.         bld_base->op_actions[TGSI_OPCODE_USEQ].emit = emit_icmp;
  1568.         bld_base->op_actions[TGSI_OPCODE_USGE].emit = emit_icmp;
  1569.         bld_base->op_actions[TGSI_OPCODE_USHR].emit = emit_ushr;
  1570.         bld_base->op_actions[TGSI_OPCODE_USLT].emit = emit_icmp;
  1571.         bld_base->op_actions[TGSI_OPCODE_USNE].emit = emit_icmp;
  1572.         bld_base->op_actions[TGSI_OPCODE_U2F].emit = emit_u2f;
  1573.         bld_base->op_actions[TGSI_OPCODE_XOR].emit = emit_xor;
  1574.         bld_base->op_actions[TGSI_OPCODE_UCMP].emit = emit_ucmp;
  1575.  
  1576.         bld_base->rsq_action.emit = build_tgsi_intrinsic_nomem;
  1577. #if HAVE_LLVM >= 0x0305
  1578.         bld_base->rsq_action.intr_name = "llvm.AMDGPU.rsq.clamped.f32";
  1579. #else
  1580.         bld_base->rsq_action.intr_name = "llvm.AMDGPU.rsq";
  1581. #endif
  1582. }
  1583.  
  1584. void radeon_llvm_create_func(struct radeon_llvm_context * ctx,
  1585.                              LLVMTypeRef *ParamTypes, unsigned ParamCount)
  1586. {
  1587.         LLVMTypeRef main_fn_type;
  1588.         LLVMBasicBlockRef main_fn_body;
  1589.  
  1590.         /* Setup the function */
  1591.         main_fn_type = LLVMFunctionType(LLVMVoidTypeInContext(ctx->gallivm.context),
  1592.                                         ParamTypes, ParamCount, 0);
  1593.         ctx->main_fn = LLVMAddFunction(ctx->gallivm.module, "main", main_fn_type);
  1594.         main_fn_body = LLVMAppendBasicBlockInContext(ctx->gallivm.context,
  1595.                         ctx->main_fn, "main_body");
  1596.         LLVMPositionBuilderAtEnd(ctx->gallivm.builder, main_fn_body);
  1597. }
  1598.  
  1599. void radeon_llvm_finalize_module(struct radeon_llvm_context * ctx)
  1600. {
  1601.         struct gallivm_state * gallivm = ctx->soa.bld_base.base.gallivm;
  1602.         /* End the main function with Return*/
  1603.         LLVMBuildRetVoid(gallivm->builder);
  1604.  
  1605.         /* Create the pass manager */
  1606.         ctx->gallivm.passmgr = LLVMCreateFunctionPassManagerForModule(
  1607.                                                         gallivm->module);
  1608.  
  1609.         /* This pass should eliminate all the load and store instructions */
  1610.         LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
  1611.  
  1612.         /* Add some optimization passes */
  1613.         LLVMAddScalarReplAggregatesPass(gallivm->passmgr);
  1614.         LLVMAddLICMPass(gallivm->passmgr);
  1615.         LLVMAddAggressiveDCEPass(gallivm->passmgr);
  1616.         LLVMAddCFGSimplificationPass(gallivm->passmgr);
  1617.         LLVMAddInstructionCombiningPass(gallivm->passmgr);
  1618.  
  1619.         /* Run the pass */
  1620.         LLVMRunFunctionPassManager(gallivm->passmgr, ctx->main_fn);
  1621.  
  1622.         LLVMDisposeBuilder(gallivm->builder);
  1623.         LLVMDisposePassManager(gallivm->passmgr);
  1624.  
  1625. }
  1626.  
  1627. void radeon_llvm_dispose(struct radeon_llvm_context * ctx)
  1628. {
  1629.         LLVMDisposeModule(ctx->soa.bld_base.base.gallivm->module);
  1630.         LLVMContextDispose(ctx->soa.bld_base.base.gallivm->context);
  1631.         FREE(ctx->temps);
  1632.         ctx->temps = NULL;
  1633.         FREE(ctx->loop);
  1634.         ctx->loop = NULL;
  1635.         ctx->loop_depth_max = 0;
  1636.         FREE(ctx->branch);
  1637.         ctx->branch = NULL;
  1638.         ctx->branch_depth_max = 0;
  1639. }
  1640.