"Fossies" - the Fresh Open Source Software Archive

Member "mesa-20.1.8/src/gallium/drivers/nouveau/codegen/nv50_ir.h" (16 Sep 2020, 36745 Bytes) of package /linux/misc/mesa-20.1.8.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "nv50_ir.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 20.1.5_vs_20.2.0-rc1.

    1 /*
    2  * Copyright 2011 Christoph Bumiller
    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 shall be included in
   12  * all copies or substantial portions of the Software.
   13  *
   14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
   18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   20  * OTHER DEALINGS IN THE SOFTWARE.
   21  */
   22 
   23 #ifndef __NV50_IR_H__
   24 #define __NV50_IR_H__
   25 
   26 #include <stdio.h>
   27 #include <stdlib.h>
   28 #include <stdint.h>
   29 #include <deque>
   30 #include <list>
   31 #include <vector>
   32 
   33 #include "codegen/unordered_set.h"
   34 #include "codegen/nv50_ir_util.h"
   35 #include "codegen/nv50_ir_graph.h"
   36 
   37 #include "codegen/nv50_ir_driver.h"
   38 
   39 namespace nv50_ir {
   40 
   41 enum operation
   42 {
   43    OP_NOP = 0,
   44    OP_PHI,
   45    OP_UNION, // unify a new definition and several source values
   46    OP_SPLIT, // $r0d -> { $r0, $r1 } ($r0d and $r0/$r1 will be coalesced)
   47    OP_MERGE, // opposite of split, e.g. combine 2 32 bit into a 64 bit value
   48    OP_CONSTRAINT, // copy values into consecutive registers
   49    OP_MOV, // simple copy, no modifiers allowed
   50    OP_LOAD,
   51    OP_STORE,
   52    OP_ADD, // NOTE: add u64 + u32 is legal for targets w/o 64-bit integer adds
   53    OP_SUB,
   54    OP_MUL,
   55    OP_DIV,
   56    OP_MOD,
   57    OP_MAD,
   58    OP_FMA,
   59    OP_SAD, // abs(src0 - src1) + src2
   60    OP_SHLADD,
   61    // extended multiply-add (GM107+), does a lot of things.
   62    // see envytools for detailed documentation
   63    OP_XMAD,
   64    OP_ABS,
   65    OP_NEG,
   66    OP_NOT,
   67    OP_AND,
   68    OP_OR,
   69    OP_XOR,
   70    OP_SHL,
   71    OP_SHR,
   72    OP_MAX,
   73    OP_MIN,
   74    OP_SAT, // CLAMP(f32, 0.0, 1.0)
   75    OP_CEIL,
   76    OP_FLOOR,
   77    OP_TRUNC,
   78    OP_CVT,
   79    OP_SET_AND, // dst = (src0 CMP src1) & src2
   80    OP_SET_OR,
   81    OP_SET_XOR,
   82    OP_SET,
   83    OP_SELP, // dst = src2 ? src0 : src1
   84    OP_SLCT, // dst = (src2 CMP 0) ? src0 : src1
   85    OP_RCP,
   86    OP_RSQ,
   87    OP_LG2,
   88    OP_SIN,
   89    OP_COS,
   90    OP_EX2,
   91    OP_EXP, // exponential (base M_E)
   92    OP_LOG, // natural logarithm
   93    OP_PRESIN,
   94    OP_PREEX2,
   95    OP_SQRT,
   96    OP_POW,
   97    OP_BRA,
   98    OP_CALL,
   99    OP_RET,
  100    OP_CONT,
  101    OP_BREAK,
  102    OP_PRERET,
  103    OP_PRECONT,
  104    OP_PREBREAK,
  105    OP_BRKPT,     // breakpoint (not related to loops)
  106    OP_JOINAT,    // push control flow convergence point
  107    OP_JOIN,      // converge
  108    OP_DISCARD,
  109    OP_EXIT,
  110    OP_MEMBAR, // memory barrier (mfence, lfence, sfence)
  111    OP_VFETCH, // indirection 0 in attribute space, indirection 1 is vertex base
  112    OP_PFETCH, // fetch base address of vertex src0 (immediate) [+ src1]
  113    OP_AFETCH, // fetch base address of shader input (a[%r1+0x10])
  114    OP_EXPORT,
  115    OP_LINTERP,
  116    OP_PINTERP,
  117    OP_EMIT,    // emit vertex
  118    OP_RESTART, // restart primitive
  119    OP_TEX,
  120    OP_TXB, // texture bias
  121    OP_TXL, // texure lod
  122    OP_TXF, // texel fetch
  123    OP_TXQ, // texture size query
  124    OP_TXD, // texture derivatives
  125    OP_TXG, // texture gather
  126    OP_TXLQ, // texture query lod
  127    OP_TEXCSAA, // texture op for coverage sampling
  128    OP_TEXPREP, // turn cube map array into 2d array coordinates
  129    OP_SULDB, // surface load (raw)
  130    OP_SULDP, // surface load (formatted)
  131    OP_SUSTB, // surface store (raw)
  132    OP_SUSTP, // surface store (formatted)
  133    OP_SUREDB,
  134    OP_SUREDP, // surface reduction (atomic op)
  135    OP_SULEA,   // surface load effective address
  136    OP_SUBFM,   // surface bitfield manipulation
  137    OP_SUCLAMP, // clamp surface coordinates
  138    OP_SUEAU,   // surface effective address
  139    OP_SUQ,     // surface query
  140    OP_MADSP,   // special integer multiply-add
  141    OP_TEXBAR, // texture dependency barrier
  142    OP_DFDX,
  143    OP_DFDY,
  144    OP_RDSV, // read system value
  145    OP_WRSV, // write system value
  146    OP_PIXLD, // get info about raster object or surfaces
  147    OP_QUADOP,
  148    OP_QUADON,
  149    OP_QUADPOP,
  150    OP_POPCNT, // bitcount(src0 & src1)
  151    OP_INSBF,  // insert first src1[8:15] bits of src0 into src2 at src1[0:7]
  152    OP_EXTBF,  // place bits [K,K+N) of src0 into dst, src1 = 0xNNKK
  153    OP_BFIND,  // find highest/lowest set bit
  154    OP_PERMT,  // dst = bytes from src2,src0 selected by src1 (nvc0's src order)
  155    OP_ATOM,
  156    OP_BAR,    // execution barrier, sources = { id, thread count, predicate }
  157    OP_VADD,   // byte/word vector operations
  158    OP_VAVG,
  159    OP_VMIN,
  160    OP_VMAX,
  161    OP_VSAD,
  162    OP_VSET,
  163    OP_VSHR,
  164    OP_VSHL,
  165    OP_VSEL,
  166    OP_CCTL, // cache control
  167    OP_SHFL, // warp shuffle
  168    OP_VOTE,
  169    OP_BUFQ, // buffer query
  170    OP_LAST
  171 };
  172 
  173 // various instruction-specific modifier definitions Instruction::subOp
  174 // MOV_FINAL marks a MOV originating from an EXPORT (used for placing TEXBARs)
  175 #define NV50_IR_SUBOP_MUL_HIGH     1
  176 #define NV50_IR_SUBOP_EMIT_RESTART 1
  177 #define NV50_IR_SUBOP_LDC_IL       1
  178 #define NV50_IR_SUBOP_LDC_IS       2
  179 #define NV50_IR_SUBOP_LDC_ISL      3
  180 #define NV50_IR_SUBOP_SHIFT_WRAP   1
  181 #define NV50_IR_SUBOP_SHIFT_HIGH   2
  182 #define NV50_IR_SUBOP_EMU_PRERET   1
  183 #define NV50_IR_SUBOP_TEXBAR(n)    n
  184 #define NV50_IR_SUBOP_MOV_FINAL    1
  185 #define NV50_IR_SUBOP_EXTBF_REV    1
  186 #define NV50_IR_SUBOP_BFIND_SAMT   1
  187 #define NV50_IR_SUBOP_RCPRSQ_64H   1
  188 #define NV50_IR_SUBOP_PERMT_F4E    1
  189 #define NV50_IR_SUBOP_PERMT_B4E    2
  190 #define NV50_IR_SUBOP_PERMT_RC8    3
  191 #define NV50_IR_SUBOP_PERMT_ECL    4
  192 #define NV50_IR_SUBOP_PERMT_ECR    5
  193 #define NV50_IR_SUBOP_PERMT_RC16   6
  194 #define NV50_IR_SUBOP_BAR_SYNC     0
  195 #define NV50_IR_SUBOP_BAR_ARRIVE   1
  196 #define NV50_IR_SUBOP_BAR_RED_AND  2
  197 #define NV50_IR_SUBOP_BAR_RED_OR   3
  198 #define NV50_IR_SUBOP_BAR_RED_POPC 4
  199 #define NV50_IR_SUBOP_MEMBAR_L     1
  200 #define NV50_IR_SUBOP_MEMBAR_S     2
  201 #define NV50_IR_SUBOP_MEMBAR_M     3
  202 #define NV50_IR_SUBOP_MEMBAR_CTA  (0 << 2)
  203 #define NV50_IR_SUBOP_MEMBAR_GL   (1 << 2)
  204 #define NV50_IR_SUBOP_MEMBAR_SYS  (2 << 2)
  205 #define NV50_IR_SUBOP_MEMBAR_DIR(m)   ((m) & 0x3)
  206 #define NV50_IR_SUBOP_MEMBAR_SCOPE(m) ((m) & ~0x3)
  207 #define NV50_IR_SUBOP_MEMBAR(d,s) \
  208    (NV50_IR_SUBOP_MEMBAR_##d | NV50_IR_SUBOP_MEMBAR_##s)
  209 #define NV50_IR_SUBOP_ATOM_ADD      0
  210 #define NV50_IR_SUBOP_ATOM_MIN      1
  211 #define NV50_IR_SUBOP_ATOM_MAX      2
  212 #define NV50_IR_SUBOP_ATOM_INC      3
  213 #define NV50_IR_SUBOP_ATOM_DEC      4
  214 #define NV50_IR_SUBOP_ATOM_AND      5
  215 #define NV50_IR_SUBOP_ATOM_OR       6
  216 #define NV50_IR_SUBOP_ATOM_XOR      7
  217 #define NV50_IR_SUBOP_ATOM_CAS      8
  218 #define NV50_IR_SUBOP_ATOM_EXCH     9
  219 #define NV50_IR_SUBOP_CCTL_IV      5
  220 #define NV50_IR_SUBOP_CCTL_IVALL   6
  221 #define NV50_IR_SUBOP_SUST_IGN     0
  222 #define NV50_IR_SUBOP_SUST_TRAP    1
  223 #define NV50_IR_SUBOP_SUST_SDCL    3
  224 #define NV50_IR_SUBOP_SULD_ZERO    0
  225 #define NV50_IR_SUBOP_SULD_TRAP    1
  226 #define NV50_IR_SUBOP_SULD_SDCL    3
  227 #define NV50_IR_SUBOP_SUBFM_3D     1
  228 #define NV50_IR_SUBOP_SUCLAMP_2D   0x10
  229 #define NV50_IR_SUBOP_SUCLAMP_SD(r, d) (( 0 + (r)) | ((d == 2) ? 0x10 : 0))
  230 #define NV50_IR_SUBOP_SUCLAMP_PL(r, d) (( 5 + (r)) | ((d == 2) ? 0x10 : 0))
  231 #define NV50_IR_SUBOP_SUCLAMP_BL(r, d) ((10 + (r)) | ((d == 2) ? 0x10 : 0))
  232 #define NV50_IR_SUBOP_PIXLD_COUNT       0
  233 #define NV50_IR_SUBOP_PIXLD_COVMASK     1
  234 #define NV50_IR_SUBOP_PIXLD_COVERED     2
  235 #define NV50_IR_SUBOP_PIXLD_OFFSET      3
  236 #define NV50_IR_SUBOP_PIXLD_CENT_OFFSET 4
  237 #define NV50_IR_SUBOP_PIXLD_SAMPLEID    5
  238 #define NV50_IR_SUBOP_SHFL_IDX  0
  239 #define NV50_IR_SUBOP_SHFL_UP   1
  240 #define NV50_IR_SUBOP_SHFL_DOWN 2
  241 #define NV50_IR_SUBOP_SHFL_BFLY 3
  242 #define NV50_IR_SUBOP_LOAD_LOCKED    1
  243 #define NV50_IR_SUBOP_STORE_UNLOCKED 2
  244 #define NV50_IR_SUBOP_MADSP_SD     0xffff
  245 // Yes, we could represent those with DataType.
  246 // Or put the type into operation and have a couple 1000 values in that enum.
  247 // This will have to do for now.
  248 // The bitfields are supposed to correspond to nve4 ISA.
  249 #define NV50_IR_SUBOP_MADSP(a,b,c) (((c) << 8) | ((b) << 4) | (a))
  250 #define NV50_IR_SUBOP_V1(d,a,b)    (((d) << 10) | ((b) << 5) | (a) | 0x0000)
  251 #define NV50_IR_SUBOP_V2(d,a,b)    (((d) << 10) | ((b) << 5) | (a) | 0x4000)
  252 #define NV50_IR_SUBOP_V4(d,a,b)    (((d) << 10) | ((b) << 5) | (a) | 0x8000)
  253 #define NV50_IR_SUBOP_Vn(n)        ((n) >> 14)
  254 #define NV50_IR_SUBOP_VOTE_ALL 0
  255 #define NV50_IR_SUBOP_VOTE_ANY 1
  256 #define NV50_IR_SUBOP_VOTE_UNI 2
  257 
  258 #define NV50_IR_SUBOP_MINMAX_LOW  1
  259 #define NV50_IR_SUBOP_MINMAX_MED  2
  260 #define NV50_IR_SUBOP_MINMAX_HIGH 3
  261 
  262 // xmad(src0, src1, 0) << 16 + src2
  263 #define NV50_IR_SUBOP_XMAD_PSL (1 << 0)
  264 // (xmad(src0, src1, src2) & 0xffff) | (src1 << 16)
  265 #define NV50_IR_SUBOP_XMAD_MRG (1 << 1)
  266 // xmad(src0, src1, src2.lo)
  267 #define NV50_IR_SUBOP_XMAD_CLO (1 << 2)
  268 // xmad(src0, src1, src2.hi)
  269 #define NV50_IR_SUBOP_XMAD_CHI (2 << 2)
  270 // if both operands to the multiplication are non-zero, subtract 65536 for each
  271 // negative operand
  272 #define NV50_IR_SUBOP_XMAD_CSFU (3 << 2)
  273 // xmad(src0, src1, src2) + src1 << 16
  274 #define NV50_IR_SUBOP_XMAD_CBCC (4 << 2)
  275 #define NV50_IR_SUBOP_XMAD_CMODE_SHIFT 2
  276 #define NV50_IR_SUBOP_XMAD_CMODE_MASK (0x7 << NV50_IR_SUBOP_XMAD_CMODE_SHIFT)
  277 
  278 // use the high 16 bits instead of the low 16 bits for the multiplication.
  279 // if the instruction's sType is signed, sign extend the operand from 16 bits
  280 // to 32 before multiplication.
  281 #define NV50_IR_SUBOP_XMAD_H1_SHIFT 5
  282 #define NV50_IR_SUBOP_XMAD_H1(i) (1 << (NV50_IR_SUBOP_XMAD_H1_SHIFT + (i)))
  283 #define NV50_IR_SUBOP_XMAD_H1_MASK (0x3 << NV50_IR_SUBOP_XMAD_H1_SHIFT)
  284 
  285 enum DataType
  286 {
  287    TYPE_NONE,
  288    TYPE_U8,
  289    TYPE_S8,
  290    TYPE_U16,
  291    TYPE_S16,
  292    TYPE_U32,
  293    TYPE_S32,
  294    TYPE_U64, // 64 bit operations are only lowered after register allocation
  295    TYPE_S64,
  296    TYPE_F16,
  297    TYPE_F32,
  298    TYPE_F64,
  299    TYPE_B96,
  300    TYPE_B128
  301 };
  302 
  303 enum CondCode
  304 {
  305    CC_FL = 0,
  306    CC_NEVER = CC_FL, // when used with FILE_FLAGS
  307    CC_LT = 1,
  308    CC_EQ = 2,
  309    CC_NOT_P = CC_EQ, // when used with FILE_PREDICATE
  310    CC_LE = 3,
  311    CC_GT = 4,
  312    CC_NE = 5,
  313    CC_P  = CC_NE,
  314    CC_GE = 6,
  315    CC_TR = 7,
  316    CC_ALWAYS = CC_TR,
  317    CC_U  = 8,
  318    CC_LTU = 9,
  319    CC_EQU = 10,
  320    CC_LEU = 11,
  321    CC_GTU = 12,
  322    CC_NEU = 13,
  323    CC_GEU = 14,
  324    CC_NO = 0x10,
  325    CC_NC = 0x11,
  326    CC_NS = 0x12,
  327    CC_NA = 0x13,
  328    CC_A  = 0x14,
  329    CC_S  = 0x15,
  330    CC_C  = 0x16,
  331    CC_O  = 0x17
  332 };
  333 
  334 enum RoundMode
  335 {
  336    ROUND_N, // nearest
  337    ROUND_M, // towards -inf
  338    ROUND_Z, // towards 0
  339    ROUND_P, // towards +inf
  340    ROUND_NI, // nearest integer
  341    ROUND_MI, // to integer towards -inf
  342    ROUND_ZI, // to integer towards 0
  343    ROUND_PI, // to integer towards +inf
  344 };
  345 
  346 enum CacheMode
  347 {
  348    CACHE_CA,            // cache at all levels
  349    CACHE_WB = CACHE_CA, // cache write back
  350    CACHE_CG,            // cache at global level
  351    CACHE_CS,            // cache streaming
  352    CACHE_CV,            // cache as volatile
  353    CACHE_WT = CACHE_CV  // cache write-through
  354 };
  355 
  356 enum DataFile
  357 {
  358    FILE_NULL = 0,
  359    FILE_GPR,
  360    FILE_PREDICATE,       // boolean predicate
  361    FILE_FLAGS,           // zero/sign/carry/overflow bits
  362    FILE_ADDRESS,
  363    LAST_REGISTER_FILE = FILE_ADDRESS,
  364    FILE_IMMEDIATE,
  365    FILE_MEMORY_CONST,
  366    FILE_SHADER_INPUT,
  367    FILE_SHADER_OUTPUT,
  368    FILE_MEMORY_BUFFER,
  369    FILE_MEMORY_GLOBAL,
  370    FILE_MEMORY_SHARED,
  371    FILE_MEMORY_LOCAL,
  372    FILE_SYSTEM_VALUE,
  373    DATA_FILE_COUNT
  374 };
  375 
  376 enum TexTarget
  377 {
  378    TEX_TARGET_1D,
  379    TEX_TARGET_2D,
  380    TEX_TARGET_2D_MS,
  381    TEX_TARGET_3D,
  382    TEX_TARGET_CUBE,
  383    TEX_TARGET_1D_SHADOW,
  384    TEX_TARGET_2D_SHADOW,
  385    TEX_TARGET_CUBE_SHADOW,
  386    TEX_TARGET_1D_ARRAY,
  387    TEX_TARGET_2D_ARRAY,
  388    TEX_TARGET_2D_MS_ARRAY,
  389    TEX_TARGET_CUBE_ARRAY,
  390    TEX_TARGET_1D_ARRAY_SHADOW,
  391    TEX_TARGET_2D_ARRAY_SHADOW,
  392    TEX_TARGET_RECT,
  393    TEX_TARGET_RECT_SHADOW,
  394    TEX_TARGET_CUBE_ARRAY_SHADOW,
  395    TEX_TARGET_BUFFER,
  396    TEX_TARGET_COUNT
  397 };
  398 
  399 enum ImgFormat
  400 {
  401    FMT_NONE,
  402 
  403    FMT_RGBA32F,
  404    FMT_RGBA16F,
  405    FMT_RG32F,
  406    FMT_RG16F,
  407    FMT_R11G11B10F,
  408    FMT_R32F,
  409    FMT_R16F,
  410 
  411    FMT_RGBA32UI,
  412    FMT_RGBA16UI,
  413    FMT_RGB10A2UI,
  414    FMT_RGBA8UI,
  415    FMT_RG32UI,
  416    FMT_RG16UI,
  417    FMT_RG8UI,
  418    FMT_R32UI,
  419    FMT_R16UI,
  420    FMT_R8UI,
  421 
  422    FMT_RGBA32I,
  423    FMT_RGBA16I,
  424    FMT_RGBA8I,
  425    FMT_RG32I,
  426    FMT_RG16I,
  427    FMT_RG8I,
  428    FMT_R32I,
  429    FMT_R16I,
  430    FMT_R8I,
  431 
  432    FMT_RGBA16,
  433    FMT_RGB10A2,
  434    FMT_RGBA8,
  435    FMT_RG16,
  436    FMT_RG8,
  437    FMT_R16,
  438    FMT_R8,
  439 
  440    FMT_RGBA16_SNORM,
  441    FMT_RGBA8_SNORM,
  442    FMT_RG16_SNORM,
  443    FMT_RG8_SNORM,
  444    FMT_R16_SNORM,
  445    FMT_R8_SNORM,
  446 
  447    FMT_BGRA8,
  448 
  449    IMG_FORMAT_COUNT,
  450 };
  451 
  452 enum ImgType {
  453    UINT,
  454    SINT,
  455    UNORM,
  456    SNORM,
  457    FLOAT,
  458 };
  459 
  460 enum SVSemantic
  461 {
  462    SV_POSITION, // WPOS
  463    SV_VERTEX_ID,
  464    SV_INSTANCE_ID,
  465    SV_INVOCATION_ID,
  466    SV_PRIMITIVE_ID,
  467    SV_VERTEX_COUNT, // gl_PatchVerticesIn
  468    SV_LAYER,
  469    SV_VIEWPORT_INDEX,
  470    SV_VIEWPORT_MASK,
  471    SV_YDIR,
  472    SV_FACE,
  473    SV_POINT_SIZE,
  474    SV_POINT_COORD,
  475    SV_CLIP_DISTANCE,
  476    SV_SAMPLE_INDEX,
  477    SV_SAMPLE_POS,
  478    SV_SAMPLE_MASK,
  479    SV_TESS_OUTER,
  480    SV_TESS_INNER,
  481    SV_TESS_COORD,
  482    SV_TID,
  483    SV_COMBINED_TID,
  484    SV_CTAID,
  485    SV_NTID,
  486    SV_GRIDID,
  487    SV_NCTAID,
  488    SV_LANEID,
  489    SV_PHYSID,
  490    SV_NPHYSID,
  491    SV_CLOCK,
  492    SV_LBASE,
  493    SV_SBASE,
  494    SV_VERTEX_STRIDE,
  495    SV_INVOCATION_INFO,
  496    SV_THREAD_KILL,
  497    SV_BASEVERTEX,
  498    SV_BASEINSTANCE,
  499    SV_DRAWID,
  500    SV_WORK_DIM,
  501    SV_LANEMASK_EQ,
  502    SV_LANEMASK_LT,
  503    SV_LANEMASK_LE,
  504    SV_LANEMASK_GT,
  505    SV_LANEMASK_GE,
  506    SV_UNDEFINED,
  507    SV_LAST
  508 };
  509 
  510 class Program;
  511 class Function;
  512 class BasicBlock;
  513 
  514 class Target;
  515 
  516 class Instruction;
  517 class CmpInstruction;
  518 class TexInstruction;
  519 class FlowInstruction;
  520 
  521 class Value;
  522 class LValue;
  523 class Symbol;
  524 class ImmediateValue;
  525 
  526 struct Storage
  527 {
  528    DataFile file;
  529    int8_t fileIndex; // signed, may be indirect for CONST[]
  530    uint8_t size; // this should match the Instruction type's size
  531    DataType type; // mainly for pretty printing
  532    union {
  533       uint64_t u64;    // immediate values
  534       uint32_t u32;
  535       uint16_t u16;
  536       uint8_t u8;
  537       int64_t s64;
  538       int32_t s32;
  539       int16_t s16;
  540       int8_t s8;
  541       float f32;
  542       double f64;
  543       int32_t offset; // offset from 0 (base of address space)
  544       int32_t id;     // register id (< 0 if virtual/unassigned, in units <= 4)
  545       struct {
  546          SVSemantic sv;
  547          int index;
  548       } sv;
  549    } data;
  550 };
  551 
  552 // precedence: NOT after SAT after NEG after ABS
  553 #define NV50_IR_MOD_ABS (1 << 0)
  554 #define NV50_IR_MOD_NEG (1 << 1)
  555 #define NV50_IR_MOD_SAT (1 << 2)
  556 #define NV50_IR_MOD_NOT (1 << 3)
  557 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
  558 
  559 #define NV50_IR_INTERP_MODE_MASK   0x3
  560 #define NV50_IR_INTERP_LINEAR      (0 << 0)
  561 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
  562 #define NV50_IR_INTERP_FLAT        (2 << 0)
  563 #define NV50_IR_INTERP_SC          (3 << 0) // what exactly is that ?
  564 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
  565 #define NV50_IR_INTERP_DEFAULT     (0 << 2)
  566 #define NV50_IR_INTERP_CENTROID    (1 << 2)
  567 #define NV50_IR_INTERP_OFFSET      (2 << 2)
  568 #define NV50_IR_INTERP_SAMPLEID    (3 << 2)
  569 
  570 // do we really want this to be a class ?
  571 class Modifier
  572 {
  573 public:
  574    Modifier() : bits(0) { }
  575    Modifier(unsigned int m) : bits(m) { }
  576    Modifier(operation op);
  577 
  578    // @return new Modifier applying a after b (asserts if unrepresentable)
  579    Modifier operator*(const Modifier) const;
  580    Modifier operator*=(const Modifier m) { *this = *this * m; return *this; }
  581    Modifier operator==(const Modifier m) const { return m.bits == bits; }
  582    Modifier operator!=(const Modifier m) const { return m.bits != bits; }
  583 
  584    inline Modifier operator&(const Modifier m) const { return bits & m.bits; }
  585    inline Modifier operator|(const Modifier m) const { return bits | m.bits; }
  586    inline Modifier operator^(const Modifier m) const { return bits ^ m.bits; }
  587 
  588    operation getOp() const;
  589 
  590    inline int neg() const { return (bits & NV50_IR_MOD_NEG) ? 1 : 0; }
  591    inline int abs() const { return (bits & NV50_IR_MOD_ABS) ? 1 : 0; }
  592 
  593    inline operator bool() const { return bits ? true : false; }
  594 
  595    void applyTo(ImmediateValue &imm) const;
  596 
  597    int print(char *buf, size_t size) const;
  598 
  599 private:
  600    uint8_t bits;
  601 };
  602 
  603 class ValueRef
  604 {
  605 public:
  606    ValueRef(Value * = NULL);
  607    ValueRef(const ValueRef&);
  608    ~ValueRef();
  609 
  610    inline bool exists() const { return value != NULL; }
  611 
  612    void set(Value *);
  613    void set(const ValueRef&);
  614    inline Value *get() const { return value; }
  615    inline Value *rep() const;
  616 
  617    inline Instruction *getInsn() const { return insn; }
  618    inline void setInsn(Instruction *inst) { insn = inst; }
  619 
  620    inline bool isIndirect(int dim) const { return indirect[dim] >= 0; }
  621    inline const ValueRef *getIndirect(int dim) const;
  622 
  623    inline DataFile getFile() const;
  624    inline unsigned getSize() const;
  625 
  626    // SSA: return eventual (traverse MOVs) literal value, if it exists
  627    bool getImmediate(ImmediateValue&) const;
  628 
  629 public:
  630    Modifier mod;
  631    int8_t indirect[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
  632 
  633    bool usedAsPtr; // for printing
  634 
  635 private:
  636    Value *value;
  637    Instruction *insn;
  638 };
  639 
  640 class ValueDef
  641 {
  642 public:
  643    ValueDef(Value * = NULL);
  644    ValueDef(const ValueDef&);
  645    ~ValueDef();
  646 
  647    inline bool exists() const { return value != NULL; }
  648 
  649    inline Value *get() const { return value; }
  650    inline Value *rep() const;
  651    void set(Value *);
  652    bool mayReplace(const ValueRef &);
  653    void replace(const ValueRef &, bool doSet); // replace all uses of the old value
  654 
  655    inline Instruction *getInsn() const { return insn; }
  656    inline void setInsn(Instruction *inst) { insn = inst; }
  657 
  658    inline DataFile getFile() const;
  659    inline unsigned getSize() const;
  660 
  661    inline void setSSA(LValue *);
  662    inline const LValue *preSSA() const;
  663 
  664 private:
  665    Value *value;   // should make this LValue * ...
  666    LValue *origin; // pre SSA value
  667    Instruction *insn;
  668 };
  669 
  670 class Value
  671 {
  672 public:
  673    Value();
  674    virtual ~Value() { }
  675 
  676    virtual Value *clone(ClonePolicy<Function>&) const = 0;
  677 
  678    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const = 0;
  679 
  680    virtual bool equals(const Value *, bool strict = false) const;
  681    virtual bool interfers(const Value *) const;
  682    virtual bool isUniform() const { return true; }
  683 
  684    inline Value *rep() const { return join; }
  685 
  686    inline Instruction *getUniqueInsn() const;
  687    inline Instruction *getInsn() const; // use when uniqueness is certain
  688 
  689    inline int refCount() { return uses.size(); }
  690 
  691    inline LValue *asLValue();
  692    inline Symbol *asSym();
  693    inline ImmediateValue *asImm();
  694    inline const Symbol *asSym() const;
  695    inline const ImmediateValue *asImm() const;
  696 
  697    inline bool inFile(DataFile f) const { return reg.file == f; }
  698 
  699    static inline Value *get(Iterator&);
  700 
  701    unordered_set<ValueRef *> uses;
  702    std::list<ValueDef *> defs;
  703    typedef unordered_set<ValueRef *>::iterator UseIterator;
  704    typedef unordered_set<ValueRef *>::const_iterator UseCIterator;
  705    typedef std::list<ValueDef *>::iterator DefIterator;
  706    typedef std::list<ValueDef *>::const_iterator DefCIterator;
  707 
  708    int id;
  709    Storage reg;
  710 
  711    // TODO: these should be in LValue:
  712    Interval livei;
  713    Value *join;
  714 };
  715 
  716 class LValue : public Value
  717 {
  718 public:
  719    LValue(Function *, DataFile file);
  720    LValue(Function *, LValue *);
  721    ~LValue() { }
  722 
  723    virtual bool isUniform() const;
  724 
  725    virtual LValue *clone(ClonePolicy<Function>&) const;
  726 
  727    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
  728 
  729 public:
  730    unsigned compMask : 8; // compound/component mask
  731    unsigned compound : 1; // used by RA, value involved in split/merge
  732    unsigned ssa      : 1;
  733    unsigned fixedReg : 1; // set & used by RA, earlier just use (id < 0)
  734    unsigned noSpill  : 1; // do not spill (e.g. if spill temporary already)
  735 };
  736 
  737 class Symbol : public Value
  738 {
  739 public:
  740    Symbol(Program *, DataFile file = FILE_MEMORY_CONST, ubyte fileIdx = 0);
  741    ~Symbol() { }
  742 
  743    virtual Symbol *clone(ClonePolicy<Function>&) const;
  744 
  745    virtual bool equals(const Value *that, bool strict) const;
  746 
  747    virtual bool isUniform() const;
  748 
  749    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
  750 
  751    // print with indirect values
  752    int print(char *, size_t, Value *, Value *, DataType ty = TYPE_NONE) const;
  753 
  754    inline void setFile(DataFile file, ubyte fileIndex = 0)
  755    {
  756       reg.file = file;
  757       reg.fileIndex = fileIndex;
  758    }
  759 
  760    inline void setOffset(int32_t offset);
  761    inline void setAddress(Symbol *base, int32_t offset);
  762    inline void setSV(SVSemantic sv, uint32_t idx = 0);
  763 
  764    inline const Symbol *getBase() const { return baseSym; }
  765 
  766 private:
  767    Symbol *baseSym; // array base for Symbols representing array elements
  768 };
  769 
  770 class ImmediateValue : public Value
  771 {
  772 public:
  773    ImmediateValue() { }
  774    ImmediateValue(Program *, uint32_t);
  775    ImmediateValue(Program *, float);
  776    ImmediateValue(Program *, double);
  777    // NOTE: not added to program with
  778    ImmediateValue(const ImmediateValue *, DataType ty);
  779    ~ImmediateValue() { };
  780 
  781    virtual ImmediateValue *clone(ClonePolicy<Function>&) const;
  782 
  783    virtual bool equals(const Value *that, bool strict) const;
  784 
  785    // these only work if 'type' is valid (we mostly use untyped literals):
  786    bool isInteger(const int ival) const; // ival is cast to this' type
  787    bool isNegative() const;
  788    bool isPow2() const;
  789 
  790    void applyLog2();
  791 
  792    // for constant folding:
  793    ImmediateValue operator+(const ImmediateValue&) const;
  794    ImmediateValue operator-(const ImmediateValue&) const;
  795    ImmediateValue operator*(const ImmediateValue&) const;
  796    ImmediateValue operator/(const ImmediateValue&) const;
  797 
  798    ImmediateValue& operator=(const ImmediateValue&); // only sets value !
  799 
  800    bool compare(CondCode cc, float fval) const;
  801 
  802    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
  803 };
  804 
  805 class Instruction
  806 {
  807 public:
  808    Instruction();
  809    Instruction(Function *, operation, DataType);
  810    virtual ~Instruction();
  811 
  812    virtual Instruction *clone(ClonePolicy<Function>&,
  813                               Instruction * = NULL) const;
  814 
  815    void setDef(int i, Value *);
  816    void setSrc(int s, Value *);
  817    void setSrc(int s, const ValueRef&);
  818    void swapSources(int a, int b);
  819    void moveSources(int s, int delta);
  820    bool setIndirect(int s, int dim, Value *);
  821 
  822    inline ValueRef& src(int s) { return srcs[s]; }
  823    inline ValueDef& def(int s) { return defs[s]; }
  824    inline const ValueRef& src(int s) const { return srcs[s]; }
  825    inline const ValueDef& def(int s) const { return defs[s]; }
  826 
  827    inline Value *getDef(int d) const { return defs[d].get(); }
  828    inline Value *getSrc(int s) const { return srcs[s].get(); }
  829    inline Value *getIndirect(int s, int dim) const;
  830 
  831    inline bool defExists(unsigned d) const
  832    {
  833       return d < defs.size() && defs[d].exists();
  834    }
  835    inline bool srcExists(unsigned s) const
  836    {
  837       return s < srcs.size() && srcs[s].exists();
  838    }
  839 
  840    inline bool constrainedDefs() const;
  841 
  842    bool setPredicate(CondCode ccode, Value *);
  843    inline Value *getPredicate() const;
  844    bool writesPredicate() const;
  845    inline bool isPredicated() const { return predSrc >= 0; }
  846 
  847    inline void setFlagsSrc(int s, Value *);
  848    inline void setFlagsDef(int d, Value *);
  849    inline bool usesFlags() const { return flagsSrc >= 0; }
  850 
  851    unsigned int defCount() const { return defs.size(); };
  852    unsigned int defCount(unsigned int mask, bool singleFile = false) const;
  853    unsigned int srcCount() const { return srcs.size(); };
  854    unsigned int srcCount(unsigned int mask, bool singleFile = false) const;
  855 
  856    // save & remove / set indirect[0,1] and predicate source
  857    void takeExtraSources(int s, Value *[3]);
  858    void putExtraSources(int s, Value *[3]);
  859 
  860    inline void setType(DataType type) { dType = sType = type; }
  861 
  862    inline void setType(DataType dtype, DataType stype)
  863    {
  864       dType = dtype;
  865       sType = stype;
  866    }
  867 
  868    inline bool isPseudo() const { return op < OP_MOV; }
  869    bool isDead() const;
  870    bool isNop() const;
  871    bool isCommutationLegal(const Instruction *) const; // must be adjacent !
  872    bool isActionEqual(const Instruction *) const;
  873    bool isResultEqual(const Instruction *) const;
  874 
  875    // check whether the defs interfere with srcs and defs of another instruction
  876    bool canCommuteDefDef(const Instruction *) const;
  877    bool canCommuteDefSrc(const Instruction *) const;
  878 
  879    void print() const;
  880 
  881    inline CmpInstruction *asCmp();
  882    inline TexInstruction *asTex();
  883    inline FlowInstruction *asFlow();
  884    inline const TexInstruction *asTex() const;
  885    inline const CmpInstruction *asCmp() const;
  886    inline const FlowInstruction *asFlow() const;
  887 
  888 public:
  889    Instruction *next;
  890    Instruction *prev;
  891    int id;
  892    int serial; // CFG order
  893 
  894    operation op;
  895    DataType dType; // destination or defining type
  896    DataType sType; // source or secondary type
  897    CondCode cc;
  898    RoundMode rnd;
  899    CacheMode cache;
  900 
  901    uint16_t subOp; // quadop, 1 for mul-high, etc.
  902 
  903    unsigned encSize    : 4; // encoding size in bytes
  904    unsigned saturate   : 1; // to [0.0f, 1.0f]
  905    unsigned join       : 1; // converge control flow (use OP_JOIN until end)
  906    unsigned fixed      : 1; // prevent dead code elimination
  907    unsigned terminator : 1; // end of basic block
  908    unsigned ftz        : 1; // flush denormal to zero
  909    unsigned dnz        : 1; // denormals, NaN are zero
  910    unsigned ipa        : 4; // interpolation mode
  911    unsigned lanes      : 4;
  912    unsigned perPatch   : 1;
  913    unsigned exit       : 1; // terminate program after insn
  914    unsigned mask       : 4; // for vector ops
  915    // prevent algebraic optimisations that aren't bit-for-bit identical
  916    unsigned precise    : 1;
  917 
  918    int8_t postFactor; // MUL/DIV(if < 0) by 1 << postFactor
  919 
  920    int8_t predSrc;
  921    int8_t flagsDef;
  922    int8_t flagsSrc;
  923 
  924    uint32_t sched; // scheduling data (NOTE: maybe move to separate storage)
  925 
  926    BasicBlock *bb;
  927 
  928 protected:
  929    std::deque<ValueDef> defs; // no gaps !
  930    std::deque<ValueRef> srcs; // no gaps !
  931 
  932    // instruction specific methods:
  933    // (don't want to subclass, would need more constructors and memory pools)
  934 public:
  935    inline void setInterpolate(unsigned int mode) { ipa = mode; }
  936 
  937    unsigned int getInterpMode() const { return ipa & 0x3; }
  938    unsigned int getSampleMode() const { return ipa & 0xc; }
  939 
  940 private:
  941    void init();
  942 };
  943 
  944 enum TexQuery
  945 {
  946    TXQ_DIMS, /* x, y, z, levels */
  947    TXQ_TYPE, /* ?, ?, samples, ? */
  948    TXQ_SAMPLE_POSITION,
  949    TXQ_FILTER,
  950    TXQ_LOD,
  951    TXQ_WRAP,
  952    TXQ_BORDER_COLOUR
  953 };
  954 
  955 class TexInstruction : public Instruction
  956 {
  957 public:
  958    class Target
  959    {
  960    public:
  961       Target(TexTarget targ = TEX_TARGET_2D) : target(targ) { }
  962 
  963       const char *getName() const { return descTable[target].name; }
  964       unsigned int getArgCount() const { return descTable[target].argc; }
  965       unsigned int getDim() const { return descTable[target].dim; }
  966       int isArray() const { return descTable[target].array ? 1 : 0; }
  967       int isCube() const { return descTable[target].cube ? 1 : 0; }
  968       int isShadow() const { return descTable[target].shadow ? 1 : 0; }
  969       int isMS() const {
  970         return target == TEX_TARGET_2D_MS || target == TEX_TARGET_2D_MS_ARRAY; }
  971       void clearMS() {
  972          if (isMS()) {
  973             if (isArray())
  974                target = TEX_TARGET_2D_ARRAY;
  975             else
  976                target = TEX_TARGET_2D;
  977          }
  978       }
  979 
  980       Target& operator=(TexTarget targ)
  981       {
  982          assert(targ < TEX_TARGET_COUNT);
  983          target = targ;
  984          return *this;
  985       }
  986 
  987       inline bool operator==(TexTarget targ) const { return target == targ; }
  988       inline bool operator!=(TexTarget targ) const { return target != targ; }
  989 
  990       enum TexTarget getEnum() const { return target; }
  991 
  992    private:
  993       struct Desc
  994       {
  995          char name[19];
  996          uint8_t dim;
  997          uint8_t argc;
  998          bool array;
  999          bool cube;
 1000          bool shadow;
 1001       };
 1002 
 1003       static const struct Desc descTable[TEX_TARGET_COUNT];
 1004 
 1005    private:
 1006       enum TexTarget target;
 1007    };
 1008 
 1009 public:
 1010    struct ImgFormatDesc
 1011    {
 1012       char name[19];
 1013       uint8_t components;
 1014       uint8_t bits[4];
 1015       ImgType type;
 1016       bool bgra;
 1017    };
 1018 
 1019    static const struct ImgFormatDesc formatTable[IMG_FORMAT_COUNT];
 1020    static const struct ImgFormatDesc *translateImgFormat(
 1021          enum pipe_format format);
 1022 
 1023 public:
 1024    TexInstruction(Function *, operation);
 1025    virtual ~TexInstruction();
 1026 
 1027    virtual TexInstruction *clone(ClonePolicy<Function>&,
 1028                                  Instruction * = NULL) const;
 1029 
 1030    inline void setTexture(Target targ, uint8_t r, uint8_t s)
 1031    {
 1032       tex.r = r;
 1033       tex.s = s;
 1034       tex.target = targ;
 1035    }
 1036 
 1037    void setIndirectR(Value *);
 1038    void setIndirectS(Value *);
 1039    inline Value *getIndirectR() const;
 1040    inline Value *getIndirectS() const;
 1041 
 1042 public:
 1043    struct {
 1044       Target target;
 1045 
 1046       uint16_t r;
 1047       uint16_t s;
 1048       int8_t rIndirectSrc;
 1049       int8_t sIndirectSrc;
 1050 
 1051       uint8_t mask;
 1052       uint8_t gatherComp;
 1053 
 1054       bool liveOnly; // only execute on live pixels of a quad (optimization)
 1055       bool levelZero;
 1056       bool derivAll;
 1057       bool bindless;
 1058 
 1059       int8_t useOffsets; // 0, 1, or 4 for textureGatherOffsets
 1060       int8_t offset[3]; // only used on nv50
 1061 
 1062       enum TexQuery query;
 1063       const struct ImgFormatDesc *format;
 1064 
 1065       bool scalar; // for GM107s TEXS, TLDS, TLD4S
 1066    } tex;
 1067 
 1068    ValueRef dPdx[3];
 1069    ValueRef dPdy[3];
 1070    ValueRef offset[4][3];
 1071 };
 1072 
 1073 class CmpInstruction : public Instruction
 1074 {
 1075 public:
 1076    CmpInstruction(Function *, operation);
 1077 
 1078    virtual CmpInstruction *clone(ClonePolicy<Function>&,
 1079                                  Instruction * = NULL) const;
 1080 
 1081    void setCondition(CondCode cond) { setCond = cond; }
 1082    CondCode getCondition() const { return setCond; }
 1083 
 1084 public:
 1085    CondCode setCond;
 1086 };
 1087 
 1088 class FlowInstruction : public Instruction
 1089 {
 1090 public:
 1091    FlowInstruction(Function *, operation, void *target);
 1092 
 1093    virtual FlowInstruction *clone(ClonePolicy<Function>&,
 1094                                   Instruction * = NULL) const;
 1095 
 1096 public:
 1097    unsigned allWarp  : 1;
 1098    unsigned absolute : 1;
 1099    unsigned limit    : 1;
 1100    unsigned builtin  : 1; // true for calls to emulation code
 1101    unsigned indirect : 1; // target in src(0)
 1102 
 1103    union {
 1104       BasicBlock *bb;
 1105       int builtin;
 1106       Function *fn;
 1107    } target;
 1108 };
 1109 
 1110 class BasicBlock
 1111 {
 1112 public:
 1113    BasicBlock(Function *);
 1114    ~BasicBlock();
 1115 
 1116    BasicBlock *clone(ClonePolicy<Function>&) const;
 1117 
 1118    inline int getId() const { return id; }
 1119    inline unsigned int getInsnCount() const { return numInsns; }
 1120    inline bool isTerminated() const { return exit && exit->terminator; }
 1121 
 1122    bool dominatedBy(BasicBlock *bb);
 1123    inline bool reachableBy(const BasicBlock *by, const BasicBlock *term);
 1124 
 1125    // returns mask of conditional out blocks
 1126    // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
 1127    unsigned int initiatesSimpleConditional() const;
 1128 
 1129 public:
 1130    Function *getFunction() const { return func; }
 1131    Program *getProgram() const { return program; }
 1132 
 1133    Instruction *getEntry() const { return entry; } // first non-phi instruction
 1134    Instruction *getPhi() const { return phi; }
 1135    Instruction *getFirst() const { return phi ? phi : entry; }
 1136    Instruction *getExit() const { return exit; }
 1137 
 1138    void insertHead(Instruction *);
 1139    void insertTail(Instruction *);
 1140    void insertBefore(Instruction *, Instruction *);
 1141    void insertAfter(Instruction *, Instruction *);
 1142    void remove(Instruction *);
 1143    void permuteAdjacent(Instruction *, Instruction *);
 1144 
 1145    BasicBlock *idom() const;
 1146 
 1147    // NOTE: currently does not rebuild the dominator tree
 1148    BasicBlock *splitBefore(Instruction *, bool attach = true);
 1149    BasicBlock *splitAfter(Instruction *, bool attach = true);
 1150 
 1151    DLList& getDF() { return df; }
 1152    DLList::Iterator iterDF() { return df.iterator(); }
 1153 
 1154    static inline BasicBlock *get(Iterator&);
 1155    static inline BasicBlock *get(Graph::Node *);
 1156 
 1157 public:
 1158    Graph::Node cfg; // first edge is branch *taken* (the ELSE branch)
 1159    Graph::Node dom;
 1160 
 1161    BitSet liveSet;
 1162    BitSet defSet;
 1163 
 1164    uint32_t binPos;
 1165    uint32_t binSize;
 1166 
 1167    Instruction *joinAt; // for quick reference
 1168 
 1169    bool explicitCont; // loop headers: true if loop contains continue stmts
 1170 
 1171 private:
 1172    int id;
 1173    DLList df;
 1174 
 1175    Instruction *phi;
 1176    Instruction *entry;
 1177    Instruction *exit;
 1178 
 1179    unsigned int numInsns;
 1180 
 1181 private:
 1182    Function *func;
 1183    Program *program;
 1184 
 1185    void splitCommon(Instruction *, BasicBlock *, bool attach);
 1186 };
 1187 
 1188 class Function
 1189 {
 1190 public:
 1191    Function(Program *, const char *name, uint32_t label);
 1192    ~Function();
 1193 
 1194    static inline Function *get(Graph::Node *node);
 1195 
 1196    inline Program *getProgram() const { return prog; }
 1197    inline const char *getName() const { return name; }
 1198    inline int getId() const { return id; }
 1199    inline uint32_t getLabel() const { return label; }
 1200 
 1201    void print();
 1202    void printLiveIntervals() const;
 1203    void printCFGraph(const char *filePath);
 1204 
 1205    bool setEntry(BasicBlock *);
 1206    bool setExit(BasicBlock *);
 1207 
 1208    unsigned int orderInstructions(ArrayList&);
 1209 
 1210    inline void add(BasicBlock *bb, int& id) { allBBlocks.insert(bb, id); }
 1211    inline void add(Instruction *insn, int& id) { allInsns.insert(insn, id); }
 1212    inline void add(LValue *lval, int& id) { allLValues.insert(lval, id); }
 1213 
 1214    inline LValue *getLValue(int id);
 1215 
 1216    void buildLiveSets();
 1217    void buildDefSets();
 1218    bool convertToSSA();
 1219 
 1220 public:
 1221    std::deque<ValueDef> ins;
 1222    std::deque<ValueRef> outs;
 1223    std::deque<Value *> clobbers;
 1224 
 1225    Graph cfg;
 1226    Graph::Node *cfgExit;
 1227    Graph *domTree;
 1228    Graph::Node call; // node in the call graph
 1229 
 1230    BasicBlock **bbArray; // BBs in emission order
 1231    int bbCount;
 1232 
 1233    unsigned int loopNestingBound;
 1234    int regClobberMax;
 1235 
 1236    uint32_t binPos;
 1237    uint32_t binSize;
 1238 
 1239    Value *stackPtr;
 1240 
 1241    uint32_t tlsBase; // base address for l[] space (if no stack pointer is used)
 1242    uint32_t tlsSize;
 1243 
 1244    ArrayList allBBlocks;
 1245    ArrayList allInsns;
 1246    ArrayList allLValues;
 1247 
 1248 private:
 1249    void buildLiveSetsPreSSA(BasicBlock *, const int sequence);
 1250    void buildDefSetsPreSSA(BasicBlock *bb, const int seq);
 1251 
 1252 private:
 1253    uint32_t label;
 1254    int id;
 1255    const char *const name;
 1256    Program *prog;
 1257 };
 1258 
 1259 enum CGStage
 1260 {
 1261    CG_STAGE_PRE_SSA,
 1262    CG_STAGE_SSA, // expected directly before register allocation
 1263    CG_STAGE_POST_RA
 1264 };
 1265 
 1266 class Program
 1267 {
 1268 public:
 1269    enum Type
 1270    {
 1271       TYPE_VERTEX,
 1272       TYPE_TESSELLATION_CONTROL,
 1273       TYPE_TESSELLATION_EVAL,
 1274       TYPE_GEOMETRY,
 1275       TYPE_FRAGMENT,
 1276       TYPE_COMPUTE
 1277    };
 1278 
 1279    Program(Type type, Target *targ);
 1280    ~Program();
 1281 
 1282    void print();
 1283 
 1284    Type getType() const { return progType; }
 1285 
 1286    inline void add(Function *fn, int& id) { allFuncs.insert(fn, id); }
 1287    inline void del(Function *fn, int& id) { allFuncs.remove(id); }
 1288    inline void add(Value *rval, int& id) { allRValues.insert(rval, id); }
 1289 
 1290    bool makeFromNIR(struct nv50_ir_prog_info *);
 1291    bool makeFromTGSI(struct nv50_ir_prog_info *);
 1292    bool convertToSSA();
 1293    bool optimizeSSA(int level);
 1294    bool optimizePostRA(int level);
 1295    bool registerAllocation();
 1296    bool emitBinary(struct nv50_ir_prog_info *);
 1297 
 1298    const Target *getTarget() const { return target; }
 1299 
 1300 private:
 1301    void emitSymbolTable(struct nv50_ir_prog_info *);
 1302 
 1303    Type progType;
 1304    Target *target;
 1305 
 1306 public:
 1307    Function *main;
 1308    Graph calls;
 1309 
 1310    ArrayList allFuncs;
 1311    ArrayList allRValues;
 1312 
 1313    uint32_t *code;
 1314    uint32_t binSize;
 1315    uint32_t tlsSize; // size required for FILE_MEMORY_LOCAL
 1316 
 1317    int maxGPR;
 1318    bool fp64;
 1319 
 1320    MemoryPool mem_Instruction;
 1321    MemoryPool mem_CmpInstruction;
 1322    MemoryPool mem_TexInstruction;
 1323    MemoryPool mem_FlowInstruction;
 1324    MemoryPool mem_LValue;
 1325    MemoryPool mem_Symbol;
 1326    MemoryPool mem_ImmediateValue;
 1327 
 1328    uint32_t dbgFlags;
 1329    uint8_t  optLevel;
 1330 
 1331    void *targetPriv; // e.g. to carry information between passes
 1332 
 1333    const struct nv50_ir_prog_info *driver; // for driver configuration
 1334 
 1335    void releaseInstruction(Instruction *);
 1336    void releaseValue(Value *);
 1337 };
 1338 
 1339 // TODO: add const version
 1340 class Pass
 1341 {
 1342 public:
 1343    bool run(Program *, bool ordered = false, bool skipPhi = false);
 1344    bool run(Function *, bool ordered = false, bool skipPhi = false);
 1345 
 1346 private:
 1347    // return false to continue with next entity on next higher level
 1348    virtual bool visit(Function *) { return true; }
 1349    virtual bool visit(BasicBlock *) { return true; }
 1350    virtual bool visit(Instruction *) { return false; }
 1351 
 1352    bool doRun(Program *, bool ordered, bool skipPhi);
 1353    bool doRun(Function *, bool ordered, bool skipPhi);
 1354 
 1355 protected:
 1356    bool err;
 1357    Function *func;
 1358    Program *prog;
 1359 };
 1360 
 1361 // =============================================================================
 1362 
 1363 #include "codegen/nv50_ir_inlines.h"
 1364 
 1365 } // namespace nv50_ir
 1366 
 1367 #endif // __NV50_IR_H__