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, // ADD, SUB, MUL, DIV, MOD, MAD, FMA, SAD
35 1, 1, 1, // ABS, NEG, NOT
36 2, 2, 2, 3, 2, 2, 3, // AND, OR, XOR, LOP3_LUT, SHL, SHR, SHF
37 2, 2, 1, // MAX, MIN, SAT
38 1, 1, 1, 1, // CEIL, FLOOR, TRUNC, CVT
39 3, 3, 3, 2, 3, 3, // SET_AND,OR,XOR, SET, SELP, SLCT
40 1, 1, 1, 1, 1, 1, // RCP, RSQ, LG2, SIN, COS, EX2
41 1, 1, 1, 1, 1, 2, // EXP, LOG, PRESIN, PREEX2, SQRT, POW
42 0, 0, 0, 0, 0, // BRA, CALL, RET, CONT, BREAK,
43 0, 0, 0, // PRERET,CONT,BREAK
44 0, 0, 0, 0, 0, 0, // BRKPT, JOINAT, JOIN, DISCARD, EXIT, MEMBAR
45 1, 1, 1, 2, 1, 2, // VFETCH, PFETCH, AFETCH, EXPORT, LINTERP, PINTERP
46 1, 1, 1, // EMIT, RESTART, FINAL
47 1, 1, 1, // TEX, TXB, TXL,
48 1, 1, 1, 1, 1, 1, 2, // TXF, TXQ, TXD, TXG, TXLQ, TEXCSAA, TEXPREP
49 1, 1, 2, 2, 2, 2, 2, // SULDB, SULDP, SUSTB, SUSTP, SUREDB, SUREDP, SULEA
50 3, 3, 3, 1, 3, // SUBFM, SUCLAMP, SUEAU, SUQ, MADSP
53 1, 2, 1, 2, 0, 0, // RDSV, WRSV, PIXLD, QUADOP, QUADON, QUADPOP
54 2, 3, 2, 1, 1, 2, 3, // POPCNT, INSBF, EXTBF, BFIND, BREV, BMSK, PERMT
57 2, 2, 2, 2, 3, 2, // VADD, VAVG, VMIN, VMAX, VSAD, VSET,
58 2, 2, 2, 1, // VSHR, VSHL, VSEL, CCTL
66 const OpClass
Target::operationClass
[] =
68 // NOP; PHI; UNION, SPLIT, MERGE, CONSTRAINT
71 OPCLASS_PSEUDO
, OPCLASS_PSEUDO
, OPCLASS_PSEUDO
, OPCLASS_PSEUDO
,
76 // ADD, SUB, MUL; DIV, MOD; MAD, FMA, SAD, SHLADD, XMAD
77 OPCLASS_ARITH
, OPCLASS_ARITH
, OPCLASS_ARITH
,
78 OPCLASS_ARITH
, OPCLASS_ARITH
,
79 OPCLASS_ARITH
, OPCLASS_ARITH
, OPCLASS_ARITH
, OPCLASS_ARITH
, OPCLASS_ARITH
,
80 // ABS, NEG; NOT, AND, OR, XOR, LOP3_LUT; SHL, SHR, SHF
81 OPCLASS_CONVERT
, OPCLASS_CONVERT
,
82 OPCLASS_LOGIC
, OPCLASS_LOGIC
, OPCLASS_LOGIC
, OPCLASS_LOGIC
, OPCLASS_LOGIC
,
83 OPCLASS_SHIFT
, OPCLASS_SHIFT
, OPCLASS_SHIFT
,
85 OPCLASS_COMPARE
, OPCLASS_COMPARE
,
86 // SAT, CEIL, FLOOR, TRUNC; CVT
87 OPCLASS_CONVERT
, OPCLASS_CONVERT
, OPCLASS_CONVERT
, OPCLASS_CONVERT
,
89 // SET(AND,OR,XOR); SELP, SLCT
90 OPCLASS_COMPARE
, OPCLASS_COMPARE
, OPCLASS_COMPARE
, OPCLASS_COMPARE
,
91 OPCLASS_COMPARE
, OPCLASS_COMPARE
,
92 // RCP, RSQ, LG2, SIN, COS; EX2, EXP, LOG, PRESIN, PREEX2; SQRT, POW
93 OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
,
94 OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
,
95 OPCLASS_SFU
, OPCLASS_SFU
,
96 // BRA, CALL, RET; CONT, BREAK, PRE(RET,CONT,BREAK); BRKPT, JOINAT, JOIN
97 OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
,
98 OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
,
99 OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
,
101 OPCLASS_FLOW
, OPCLASS_FLOW
,
104 // VFETCH, PFETCH, AFETCH, EXPORT
105 OPCLASS_LOAD
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_STORE
,
107 OPCLASS_SFU
, OPCLASS_SFU
,
108 // EMIT, RESTART, FINAL
109 OPCLASS_CONTROL
, OPCLASS_CONTROL
, OPCLASS_CONTROL
,
110 // TEX, TXB, TXL, TXF; TXQ, TXD, TXG, TXLQ; TEXCSAA, TEXPREP
111 OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
,
112 OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
,
113 OPCLASS_TEXTURE
, OPCLASS_TEXTURE
,
114 // SULDB, SULDP, SUSTB, SUSTP; SUREDB, SUREDP, SULEA
115 OPCLASS_SURFACE
, OPCLASS_SURFACE
, OPCLASS_ATOMIC
, OPCLASS_SURFACE
,
116 OPCLASS_SURFACE
, OPCLASS_SURFACE
, OPCLASS_SURFACE
,
117 // SUBFM, SUCLAMP, SUEAU, SUQ, MADSP
118 OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_ARITH
,
121 // DFDX, DFDY, RDSV, WRSV; PIXLD, QUADOP, QUADON, QUADPOP
122 OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
,
123 OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_CONTROL
, OPCLASS_CONTROL
,
124 // POPCNT, INSBF, EXTBF, BFIND, BREV, BMSK; PERMT, SGXT
125 OPCLASS_BITFIELD
, OPCLASS_BITFIELD
, OPCLASS_BITFIELD
, OPCLASS_BITFIELD
,
126 OPCLASS_BITFIELD
, OPCLASS_BITFIELD
, OPCLASS_BITFIELD
, OPCLASS_BITFIELD
,
128 OPCLASS_ATOMIC
, OPCLASS_CONTROL
,
129 // VADD, VAVG, VMIN, VMAX
130 OPCLASS_VECTOR
, OPCLASS_VECTOR
, OPCLASS_VECTOR
, OPCLASS_VECTOR
,
131 // VSAD, VSET, VSHR, VSHL
132 OPCLASS_VECTOR
, OPCLASS_VECTOR
, OPCLASS_VECTOR
, OPCLASS_VECTOR
,
134 OPCLASS_VECTOR
, OPCLASS_CONTROL
,
143 OPCLASS_PSEUDO
// LAST
147 extern Target
*getTargetGV100(unsigned int chipset
);
148 extern Target
*getTargetGM107(unsigned int chipset
);
149 extern Target
*getTargetNVC0(unsigned int chipset
);
150 extern Target
*getTargetNV50(unsigned int chipset
);
152 Target
*Target::create(unsigned int chipset
)
154 STATIC_ASSERT(ARRAY_SIZE(operationSrcNr
) == OP_LAST
+ 1);
155 STATIC_ASSERT(ARRAY_SIZE(operationClass
) == OP_LAST
+ 1);
156 switch (chipset
& ~0xf) {
159 return getTargetGV100(chipset
);
163 return getTargetGM107(chipset
);
169 return getTargetNVC0(chipset
);
174 return getTargetNV50(chipset
);
176 ERROR("unsupported target: NV%x\n", chipset
);
181 void Target::destroy(Target
*targ
)
186 CodeEmitter::CodeEmitter(const Target
*target
) : targ(target
), fixupInfo(NULL
)
191 CodeEmitter::setCodeLocation(void *ptr
, uint32_t size
)
193 code
= reinterpret_cast<uint32_t *>(ptr
);
195 codeSizeLimit
= size
;
199 CodeEmitter::printBinary() const
201 uint32_t *bin
= code
- codeSize
/ 4;
202 INFO("program binary (%u bytes)", codeSize
);
203 for (unsigned int pos
= 0; pos
< codeSize
/ 4; ++pos
) {
206 INFO("%08x ", bin
[pos
]);
211 static inline uint32_t sizeToBundlesNVE4(uint32_t size
)
213 return (size
+ 55) / 56;
217 CodeEmitter::prepareEmission(Program
*prog
)
219 for (ArrayList::Iterator fi
= prog
->allFuncs
.iterator();
220 !fi
.end(); fi
.next()) {
221 Function
*func
= reinterpret_cast<Function
*>(fi
.get());
222 func
->binPos
= prog
->binSize
;
223 prepareEmission(func
);
225 // adjust sizes & positions for schedulding info:
226 if (prog
->getTarget()->hasSWSched
) {
227 uint32_t adjPos
= func
->binPos
;
228 BasicBlock
*bb
= NULL
;
229 for (int i
= 0; i
< func
->bbCount
; ++i
) {
230 bb
= func
->bbArray
[i
];
231 int32_t adjSize
= bb
->binSize
;
233 adjSize
-= 64 - adjPos
% 64;
237 adjSize
= bb
->binSize
+ sizeToBundlesNVE4(adjSize
) * 8;
239 bb
->binSize
= adjSize
;
243 func
->binSize
= adjPos
- func
->binPos
;
246 prog
->binSize
+= func
->binSize
;
251 CodeEmitter::prepareEmission(Function
*func
)
254 func
->bbArray
= new BasicBlock
* [func
->cfg
.getSize()];
256 BasicBlock::get(func
->cfg
.getRoot())->binPos
= func
->binPos
;
258 for (IteratorRef it
= func
->cfg
.iteratorCFG(); !it
->end(); it
->next())
259 prepareEmission(BasicBlock::get(*it
));
263 CodeEmitter::prepareEmission(BasicBlock
*bb
)
265 Instruction
*i
, *next
;
266 Function
*func
= bb
->getFunction();
270 for (j
= func
->bbCount
- 1; j
>= 0 && !func
->bbArray
[j
]->binSize
; --j
);
272 for (; j
>= 0; --j
) {
273 BasicBlock
*in
= func
->bbArray
[j
];
274 Instruction
*exit
= in
->getExit();
276 if (exit
&& exit
->op
== OP_BRA
&& exit
->asFlow()->target
.bb
== bb
) {
280 for (++j
; j
< func
->bbCount
; ++j
)
281 func
->bbArray
[j
]->binPos
-= 8;
285 bb
->binPos
= in
->binPos
+ in
->binSize
;
286 if (in
->binSize
) // no more no-op branches to bb
289 func
->bbArray
[func
->bbCount
++] = bb
;
294 // determine encoding size, try to group short instructions
296 for (i
= bb
->getEntry(); i
; i
= next
) {
299 if (i
->op
== OP_MEMBAR
&& !targ
->isOpSupported(OP_MEMBAR
, TYPE_NONE
)) {
304 i
->encSize
= getMinEncodingSize(i
);
305 if (next
&& i
->encSize
< 8)
308 if ((nShort
& 1) && next
&& getMinEncodingSize(next
) == 4) {
309 if (i
->isCommutationLegal(i
->next
)) {
310 bb
->permuteAdjacent(i
, next
);
316 if (i
->isCommutationLegal(i
->prev
) && next
->next
) {
317 bb
->permuteAdjacent(i
->prev
, i
);
324 i
->prev
->encSize
= 8;
331 i
->prev
->encSize
= 8;
336 bb
->binSize
+= i
->encSize
;
339 if (bb
->getExit()->encSize
== 4) {
341 bb
->getExit()->encSize
= 8;
344 if ((bb
->getExit()->prev
->encSize
== 4) && !(nShort
& 1)) {
346 bb
->getExit()->prev
->encSize
= 8;
349 assert(!bb
->getEntry() || (bb
->getExit() && bb
->getExit()->encSize
== 8));
351 func
->binSize
+= bb
->binSize
;
355 Program::emitBinary(struct nv50_ir_prog_info_out
*info
)
357 CodeEmitter
*emit
= target
->getCodeEmitter(progType
);
359 emit
->prepareEmission(this);
361 if (dbgFlags
& NV50_IR_DEBUG_BASIC
)
368 code
= reinterpret_cast<uint32_t *>(MALLOC(binSize
));
371 emit
->setCodeLocation(code
, binSize
);
372 info
->bin
.instructions
= 0;
374 for (ArrayList::Iterator fi
= allFuncs
.iterator(); !fi
.end(); fi
.next()) {
375 Function
*fn
= reinterpret_cast<Function
*>(fi
.get());
377 assert(emit
->getCodeSize() == fn
->binPos
);
379 for (int b
= 0; b
< fn
->bbCount
; ++b
) {
380 for (Instruction
*i
= fn
->bbArray
[b
]->getEntry(); i
; i
= i
->next
) {
381 emit
->emitInstruction(i
);
382 info
->bin
.instructions
++;
383 if ((typeSizeof(i
->sType
) == 8 || typeSizeof(i
->dType
) == 8) &&
384 (isFloatType(i
->sType
) || isFloatType(i
->dType
)))
385 info
->io
.fp64
= true;
389 info
->io
.fp64
|= fp64
;
390 info
->bin
.relocData
= emit
->getRelocInfo();
391 info
->bin
.fixupData
= emit
->getFixupInfo();
393 // the nvc0 driver will print the binary iself together with the header
394 if ((dbgFlags
& NV50_IR_DEBUG_BASIC
) && getTarget()->getChipset() < 0xc0)
401 #define RELOC_ALLOC_INCREMENT 8
404 CodeEmitter::addReloc(RelocEntry::Type ty
, int w
, uint32_t data
, uint32_t m
,
407 unsigned int n
= relocInfo
? relocInfo
->count
: 0;
409 if (!(n
% RELOC_ALLOC_INCREMENT
)) {
410 size_t size
= sizeof(RelocInfo
) + n
* sizeof(RelocEntry
);
411 relocInfo
= reinterpret_cast<RelocInfo
*>(
412 REALLOC(relocInfo
, n
? size
: 0,
413 size
+ RELOC_ALLOC_INCREMENT
* sizeof(RelocEntry
)));
417 memset(relocInfo
, 0, sizeof(RelocInfo
));
421 relocInfo
->entry
[n
].data
= data
;
422 relocInfo
->entry
[n
].mask
= m
;
423 relocInfo
->entry
[n
].offset
= codeSize
+ w
* 4;
424 relocInfo
->entry
[n
].bitPos
= s
;
425 relocInfo
->entry
[n
].type
= ty
;
431 CodeEmitter::addInterp(int ipa
, int reg
, FixupApply apply
)
433 unsigned int n
= fixupInfo
? fixupInfo
->count
: 0;
435 if (!(n
% RELOC_ALLOC_INCREMENT
)) {
436 size_t size
= sizeof(FixupInfo
) + n
* sizeof(FixupEntry
);
437 fixupInfo
= reinterpret_cast<FixupInfo
*>(
438 REALLOC(fixupInfo
, n
? size
: 0,
439 size
+ RELOC_ALLOC_INCREMENT
* sizeof(FixupEntry
)));
443 fixupInfo
->count
= 0;
447 fixupInfo
->entry
[n
] = FixupEntry(apply
, ipa
, reg
, codeSize
>> 2);
453 RelocEntry::apply(uint32_t *binary
, const RelocInfo
*info
) const
458 case TYPE_CODE
: value
= info
->codePos
; break;
459 case TYPE_BUILTIN
: value
= info
->libPos
; break;
460 case TYPE_DATA
: value
= info
->dataPos
; break;
466 value
= (bitPos
< 0) ? (value
>> -bitPos
) : (value
<< bitPos
);
468 binary
[offset
/ 4] &= ~mask
;
469 binary
[offset
/ 4] |= value
& mask
;
472 } // namespace nv50_ir
475 #include "codegen/nv50_ir_driver.h"
480 nv50_ir_relocate_code(void *relocData
, uint32_t *code
,
485 nv50_ir::RelocInfo
*info
= reinterpret_cast<nv50_ir::RelocInfo
*>(relocData
);
487 info
->codePos
= codePos
;
488 info
->libPos
= libPos
;
489 info
->dataPos
= dataPos
;
491 for (unsigned int i
= 0; i
< info
->count
; ++i
)
492 info
->entry
[i
].apply(code
, info
);
496 nv50_ir_apply_fixups(void *fixupData
, uint32_t *code
,
497 bool force_persample_interp
, bool flatshade
,
500 nv50_ir::FixupInfo
*info
= reinterpret_cast<nv50_ir::FixupInfo
*>(
503 // force_persample_interp: all non-flat -> per-sample
504 // flatshade: all color -> flat
505 // alphatest: PIPE_FUNC_* to use with alphatest
506 nv50_ir::FixupData
data(force_persample_interp
, flatshade
, alphatest
);
507 for (unsigned i
= 0; i
< info
->count
; ++i
)
508 info
->entry
[i
].apply(&info
->entry
[i
], code
, data
);
512 nv50_ir_get_target_library(uint32_t chipset
,
513 const uint32_t **code
, uint32_t *size
)
515 nv50_ir::Target
*targ
= nv50_ir::Target::create(chipset
);
516 targ
->getBuiltinCode(code
, size
);
517 nv50_ir::Target::destroy(targ
);