2 * Copyright 2011 Christoph Bumiller
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #include "codegen/nv50_ir.h"
24 #include "codegen/nv50_ir_target.h"
28 const uint8_t Target::operationSrcNr
[] =
31 0, 0, 0, 0, // UNION, SPLIT, MERGE, CONSTRAINT
32 1, 1, 2, // MOV, LOAD, STORE
33 2, 2, 2, 2, 2, 3, 3, 3, 3, // ADD, SUB, MUL, DIV, MOD, MAD, FMA, SAD, SHLADD
34 1, 1, 1, // ABS, NEG, NOT
35 2, 2, 2, 2, 2, // AND, OR, XOR, SHL, SHR
36 2, 2, 1, // MAX, MIN, SAT
37 1, 1, 1, 1, // CEIL, FLOOR, TRUNC, CVT
38 3, 3, 3, 2, 3, 3, // SET_AND,OR,XOR, SET, SELP, SLCT
39 1, 1, 1, 1, 1, 1, // RCP, RSQ, LG2, SIN, COS, EX2
40 1, 1, 1, 1, 1, 2, // EXP, LOG, PRESIN, PREEX2, SQRT, POW
41 0, 0, 0, 0, 0, // BRA, CALL, RET, CONT, BREAK,
42 0, 0, 0, // PRERET,CONT,BREAK
43 0, 0, 0, 0, 0, 0, // BRKPT, JOINAT, JOIN, DISCARD, EXIT, MEMBAR
44 1, 1, 1, 2, 1, 2, // VFETCH, PFETCH, AFETCH, EXPORT, LINTERP, PINTERP
45 1, 1, // EMIT, RESTART
46 1, 1, 1, // TEX, TXB, TXL,
47 1, 1, 1, 1, 1, 1, 2, // TXF, TXQ, TXD, TXG, TXLQ, TEXCSAA, TEXPREP
48 1, 1, 2, 2, 2, 2, 2, // SULDB, SULDP, SUSTB, SUSTP, SUREDB, SUREDP, SULEA
49 3, 3, 3, 1, 3, // SUBFM, SUCLAMP, SUEAU, SUQ, MADSP
52 1, 2, 1, 2, 0, 0, // RDSV, WRSV, PIXLD, QUADOP, QUADON, QUADPOP
53 2, 3, 2, 1, 3, // POPCNT, INSBF, EXTBF, BFIND, PERMT
55 2, 2, 2, 2, 3, 2, // VADD, VAVG, VMIN, VMAX, VSAD, VSET,
56 2, 2, 2, 1, // VSHR, VSHL, VSEL, CCTL
63 const OpClass
Target::operationClass
[] =
65 // NOP; PHI; UNION, SPLIT, MERGE, CONSTRAINT
68 OPCLASS_PSEUDO
, OPCLASS_PSEUDO
, OPCLASS_PSEUDO
, OPCLASS_PSEUDO
,
73 // ADD, SUB, MUL; DIV, MOD; MAD, FMA, SAD, SHLADD
74 OPCLASS_ARITH
, OPCLASS_ARITH
, OPCLASS_ARITH
,
75 OPCLASS_ARITH
, OPCLASS_ARITH
,
76 OPCLASS_ARITH
, OPCLASS_ARITH
, OPCLASS_ARITH
, OPCLASS_ARITH
,
77 // ABS, NEG; NOT, AND, OR, XOR; SHL, SHR
78 OPCLASS_CONVERT
, OPCLASS_CONVERT
,
79 OPCLASS_LOGIC
, OPCLASS_LOGIC
, OPCLASS_LOGIC
, OPCLASS_LOGIC
,
80 OPCLASS_SHIFT
, OPCLASS_SHIFT
,
82 OPCLASS_COMPARE
, OPCLASS_COMPARE
,
83 // SAT, CEIL, FLOOR, TRUNC; CVT
84 OPCLASS_CONVERT
, OPCLASS_CONVERT
, OPCLASS_CONVERT
, OPCLASS_CONVERT
,
86 // SET(AND,OR,XOR); SELP, SLCT
87 OPCLASS_COMPARE
, OPCLASS_COMPARE
, OPCLASS_COMPARE
, OPCLASS_COMPARE
,
88 OPCLASS_COMPARE
, OPCLASS_COMPARE
,
89 // RCP, RSQ, LG2, SIN, COS; EX2, EXP, LOG, PRESIN, PREEX2; SQRT, POW
90 OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
,
91 OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
,
92 OPCLASS_SFU
, OPCLASS_SFU
,
93 // BRA, CALL, RET; CONT, BREAK, PRE(RET,CONT,BREAK); BRKPT, JOINAT, JOIN
94 OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
,
95 OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
,
96 OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
,
98 OPCLASS_FLOW
, OPCLASS_FLOW
,
101 // VFETCH, PFETCH, AFETCH, EXPORT
102 OPCLASS_LOAD
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_STORE
,
104 OPCLASS_SFU
, OPCLASS_SFU
,
106 OPCLASS_CONTROL
, OPCLASS_CONTROL
,
107 // TEX, TXB, TXL, TXF; TXQ, TXD, TXG, TXLQ; TEXCSAA, TEXPREP
108 OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
,
109 OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
,
110 OPCLASS_TEXTURE
, OPCLASS_TEXTURE
,
111 // SULDB, SULDP, SUSTB, SUSTP; SUREDB, SUREDP, SULEA
112 OPCLASS_SURFACE
, OPCLASS_SURFACE
, OPCLASS_ATOMIC
, OPCLASS_SURFACE
,
113 OPCLASS_SURFACE
, OPCLASS_SURFACE
, OPCLASS_SURFACE
,
114 // SUBFM, SUCLAMP, SUEAU, SUQ, MADSP
115 OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_ARITH
,
118 // DFDX, DFDY, RDSV, WRSV; PIXLD, QUADOP, QUADON, QUADPOP
119 OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
,
120 OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_CONTROL
, OPCLASS_CONTROL
,
121 // POPCNT, INSBF, EXTBF, BFIND; PERMT
122 OPCLASS_BITFIELD
, OPCLASS_BITFIELD
, OPCLASS_BITFIELD
, OPCLASS_BITFIELD
,
125 OPCLASS_ATOMIC
, OPCLASS_CONTROL
,
126 // VADD, VAVG, VMIN, VMAX
127 OPCLASS_VECTOR
, OPCLASS_VECTOR
, OPCLASS_VECTOR
, OPCLASS_VECTOR
,
128 // VSAD, VSET, VSHR, VSHL
129 OPCLASS_VECTOR
, OPCLASS_VECTOR
, OPCLASS_VECTOR
, OPCLASS_VECTOR
,
131 OPCLASS_VECTOR
, OPCLASS_CONTROL
,
138 OPCLASS_PSEUDO
// LAST
142 extern Target
*getTargetGM107(unsigned int chipset
);
143 extern Target
*getTargetNVC0(unsigned int chipset
);
144 extern Target
*getTargetNV50(unsigned int chipset
);
146 Target
*Target::create(unsigned int chipset
)
148 STATIC_ASSERT(ARRAY_SIZE(operationSrcNr
) == OP_LAST
+ 1);
149 STATIC_ASSERT(ARRAY_SIZE(operationClass
) == OP_LAST
+ 1);
150 switch (chipset
& ~0xf) {
154 return getTargetGM107(chipset
);
160 return getTargetNVC0(chipset
);
165 return getTargetNV50(chipset
);
167 ERROR("unsupported target: NV%x\n", chipset
);
172 void Target::destroy(Target
*targ
)
177 CodeEmitter::CodeEmitter(const Target
*target
) : targ(target
), fixupInfo(NULL
)
182 CodeEmitter::setCodeLocation(void *ptr
, uint32_t size
)
184 code
= reinterpret_cast<uint32_t *>(ptr
);
186 codeSizeLimit
= size
;
190 CodeEmitter::printBinary() const
192 uint32_t *bin
= code
- codeSize
/ 4;
193 INFO("program binary (%u bytes)", codeSize
);
194 for (unsigned int pos
= 0; pos
< codeSize
/ 4; ++pos
) {
197 INFO("%08x ", bin
[pos
]);
202 static inline uint32_t sizeToBundlesNVE4(uint32_t size
)
204 return (size
+ 55) / 56;
208 CodeEmitter::prepareEmission(Program
*prog
)
210 for (ArrayList::Iterator fi
= prog
->allFuncs
.iterator();
211 !fi
.end(); fi
.next()) {
212 Function
*func
= reinterpret_cast<Function
*>(fi
.get());
213 func
->binPos
= prog
->binSize
;
214 prepareEmission(func
);
216 // adjust sizes & positions for schedulding info:
217 if (prog
->getTarget()->hasSWSched
) {
218 uint32_t adjPos
= func
->binPos
;
219 BasicBlock
*bb
= NULL
;
220 for (int i
= 0; i
< func
->bbCount
; ++i
) {
221 bb
= func
->bbArray
[i
];
222 int32_t adjSize
= bb
->binSize
;
224 adjSize
-= 64 - adjPos
% 64;
228 adjSize
= bb
->binSize
+ sizeToBundlesNVE4(adjSize
) * 8;
230 bb
->binSize
= adjSize
;
234 func
->binSize
= adjPos
- func
->binPos
;
237 prog
->binSize
+= func
->binSize
;
242 CodeEmitter::prepareEmission(Function
*func
)
245 func
->bbArray
= new BasicBlock
* [func
->cfg
.getSize()];
247 BasicBlock::get(func
->cfg
.getRoot())->binPos
= func
->binPos
;
249 for (IteratorRef it
= func
->cfg
.iteratorCFG(); !it
->end(); it
->next())
250 prepareEmission(BasicBlock::get(*it
));
254 CodeEmitter::prepareEmission(BasicBlock
*bb
)
256 Instruction
*i
, *next
;
257 Function
*func
= bb
->getFunction();
261 for (j
= func
->bbCount
- 1; j
>= 0 && !func
->bbArray
[j
]->binSize
; --j
);
263 for (; j
>= 0; --j
) {
264 BasicBlock
*in
= func
->bbArray
[j
];
265 Instruction
*exit
= in
->getExit();
267 if (exit
&& exit
->op
== OP_BRA
&& exit
->asFlow()->target
.bb
== bb
) {
271 for (++j
; j
< func
->bbCount
; ++j
)
272 func
->bbArray
[j
]->binPos
-= 8;
276 bb
->binPos
= in
->binPos
+ in
->binSize
;
277 if (in
->binSize
) // no more no-op branches to bb
280 func
->bbArray
[func
->bbCount
++] = bb
;
285 // determine encoding size, try to group short instructions
287 for (i
= bb
->getEntry(); i
; i
= next
) {
290 if (i
->op
== OP_MEMBAR
&& !targ
->isOpSupported(OP_MEMBAR
, TYPE_NONE
)) {
295 i
->encSize
= getMinEncodingSize(i
);
296 if (next
&& i
->encSize
< 8)
299 if ((nShort
& 1) && next
&& getMinEncodingSize(next
) == 4) {
300 if (i
->isCommutationLegal(i
->next
)) {
301 bb
->permuteAdjacent(i
, next
);
307 if (i
->isCommutationLegal(i
->prev
) && next
->next
) {
308 bb
->permuteAdjacent(i
->prev
, i
);
315 i
->prev
->encSize
= 8;
322 i
->prev
->encSize
= 8;
327 bb
->binSize
+= i
->encSize
;
330 if (bb
->getExit()->encSize
== 4) {
332 bb
->getExit()->encSize
= 8;
335 if ((bb
->getExit()->prev
->encSize
== 4) && !(nShort
& 1)) {
337 bb
->getExit()->prev
->encSize
= 8;
340 assert(!bb
->getEntry() || (bb
->getExit() && bb
->getExit()->encSize
== 8));
342 func
->binSize
+= bb
->binSize
;
346 Program::emitSymbolTable(struct nv50_ir_prog_info
*info
)
348 unsigned int n
= 0, nMax
= allFuncs
.getSize();
351 (struct nv50_ir_prog_symbol
*)MALLOC(nMax
* sizeof(*info
->bin
.syms
));
353 for (ArrayList::Iterator fi
= allFuncs
.iterator();
356 Function
*f
= (Function
*)fi
.get();
359 info
->bin
.syms
[n
].label
= f
->getLabel();
360 info
->bin
.syms
[n
].offset
= f
->binPos
;
363 info
->bin
.numSyms
= n
;
367 Program::emitBinary(struct nv50_ir_prog_info
*info
)
369 CodeEmitter
*emit
= target
->getCodeEmitter(progType
);
371 emit
->prepareEmission(this);
373 if (dbgFlags
& NV50_IR_DEBUG_BASIC
)
380 code
= reinterpret_cast<uint32_t *>(MALLOC(binSize
));
383 emit
->setCodeLocation(code
, binSize
);
384 info
->bin
.instructions
= 0;
386 for (ArrayList::Iterator fi
= allFuncs
.iterator(); !fi
.end(); fi
.next()) {
387 Function
*fn
= reinterpret_cast<Function
*>(fi
.get());
389 assert(emit
->getCodeSize() == fn
->binPos
);
391 for (int b
= 0; b
< fn
->bbCount
; ++b
) {
392 for (Instruction
*i
= fn
->bbArray
[b
]->getEntry(); i
; i
= i
->next
) {
393 emit
->emitInstruction(i
);
394 info
->bin
.instructions
++;
395 if ((typeSizeof(i
->sType
) == 8 || typeSizeof(i
->dType
) == 8) &&
396 (isFloatType(i
->sType
) || isFloatType(i
->dType
)))
397 info
->io
.fp64
= true;
401 info
->bin
.relocData
= emit
->getRelocInfo();
402 info
->bin
.fixupData
= emit
->getFixupInfo();
404 emitSymbolTable(info
);
406 // the nvc0 driver will print the binary iself together with the header
407 if ((dbgFlags
& NV50_IR_DEBUG_BASIC
) && getTarget()->getChipset() < 0xc0)
414 #define RELOC_ALLOC_INCREMENT 8
417 CodeEmitter::addReloc(RelocEntry::Type ty
, int w
, uint32_t data
, uint32_t m
,
420 unsigned int n
= relocInfo
? relocInfo
->count
: 0;
422 if (!(n
% RELOC_ALLOC_INCREMENT
)) {
423 size_t size
= sizeof(RelocInfo
) + n
* sizeof(RelocEntry
);
424 relocInfo
= reinterpret_cast<RelocInfo
*>(
425 REALLOC(relocInfo
, n
? size
: 0,
426 size
+ RELOC_ALLOC_INCREMENT
* sizeof(RelocEntry
)));
430 memset(relocInfo
, 0, sizeof(RelocInfo
));
434 relocInfo
->entry
[n
].data
= data
;
435 relocInfo
->entry
[n
].mask
= m
;
436 relocInfo
->entry
[n
].offset
= codeSize
+ w
* 4;
437 relocInfo
->entry
[n
].bitPos
= s
;
438 relocInfo
->entry
[n
].type
= ty
;
444 CodeEmitter::addInterp(int ipa
, int reg
, FixupApply apply
)
446 unsigned int n
= fixupInfo
? fixupInfo
->count
: 0;
448 if (!(n
% RELOC_ALLOC_INCREMENT
)) {
449 size_t size
= sizeof(FixupInfo
) + n
* sizeof(FixupEntry
);
450 fixupInfo
= reinterpret_cast<FixupInfo
*>(
451 REALLOC(fixupInfo
, n
? size
: 0,
452 size
+ RELOC_ALLOC_INCREMENT
* sizeof(FixupEntry
)));
456 memset(fixupInfo
, 0, sizeof(FixupInfo
));
460 fixupInfo
->entry
[n
] = FixupEntry(apply
, ipa
, reg
, codeSize
>> 2);
466 RelocEntry::apply(uint32_t *binary
, const RelocInfo
*info
) const
471 case TYPE_CODE
: value
= info
->codePos
; break;
472 case TYPE_BUILTIN
: value
= info
->libPos
; break;
473 case TYPE_DATA
: value
= info
->dataPos
; break;
479 value
= (bitPos
< 0) ? (value
>> -bitPos
) : (value
<< bitPos
);
481 binary
[offset
/ 4] &= ~mask
;
482 binary
[offset
/ 4] |= value
& mask
;
485 } // namespace nv50_ir
488 #include "codegen/nv50_ir_driver.h"
493 nv50_ir_relocate_code(void *relocData
, uint32_t *code
,
498 nv50_ir::RelocInfo
*info
= reinterpret_cast<nv50_ir::RelocInfo
*>(relocData
);
500 info
->codePos
= codePos
;
501 info
->libPos
= libPos
;
502 info
->dataPos
= dataPos
;
504 for (unsigned int i
= 0; i
< info
->count
; ++i
)
505 info
->entry
[i
].apply(code
, info
);
509 nv50_ir_apply_fixups(void *fixupData
, uint32_t *code
,
510 bool force_persample_interp
, bool flatshade
,
513 nv50_ir::FixupInfo
*info
= reinterpret_cast<nv50_ir::FixupInfo
*>(
516 // force_persample_interp: all non-flat -> per-sample
517 // flatshade: all color -> flat
518 // alphatest: PIPE_FUNC_* to use with alphatest
519 nv50_ir::FixupData
data(force_persample_interp
, flatshade
, alphatest
);
520 for (unsigned i
= 0; i
< info
->count
; ++i
)
521 info
->entry
[i
].apply(&info
->entry
[i
], code
, data
);
525 nv50_ir_get_target_library(uint32_t chipset
,
526 const uint32_t **code
, uint32_t *size
)
528 nv50_ir::Target
*targ
= nv50_ir::Target::create(chipset
);
529 targ
->getBuiltinCode(code
, size
);
530 nv50_ir::Target::destroy(targ
);