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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "nv50/codegen/nv50_ir.h"
24 #include "nv50/codegen/nv50_ir_target.h"
28 const uint8_t Target::operationSrcNr
[OP_LAST
+ 1] =
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
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, 2, 1, 2, // VFETCH, PFETCH, EXPORT, LINTERP, PINTERP
45 1, 1, // EMIT, RESTART
46 1, 1, 1, // TEX, TXB, TXL,
47 1, 1, 1, 1, 1, // TXF, TXQ, TXD, TXG, TEXCSAA
50 1, 2, 2, 2, 0, 0, // RDSV, WRSV, TEXPREP, QUADOP, QUADON, QUADPOP
51 2, 3, 2, 0, // POPCNT, INSBF, EXTBF, TEXBAR
55 const OpClass
Target::operationClass
[OP_LAST
+ 1] =
57 // NOP; PHI; UNION, SPLIT, MERGE, CONSTRAINT
60 OPCLASS_PSEUDO
, OPCLASS_PSEUDO
, OPCLASS_PSEUDO
, OPCLASS_PSEUDO
,
65 // ADD, SUB, MUL; DIV, MOD; MAD, FMA, SAD
66 OPCLASS_ARITH
, OPCLASS_ARITH
, OPCLASS_ARITH
,
67 OPCLASS_ARITH
, OPCLASS_ARITH
,
68 OPCLASS_ARITH
, OPCLASS_ARITH
, OPCLASS_ARITH
,
69 // ABS, NEG; NOT, AND, OR, XOR; SHL, SHR
70 OPCLASS_CONVERT
, OPCLASS_CONVERT
,
71 OPCLASS_LOGIC
, OPCLASS_LOGIC
, OPCLASS_LOGIC
, OPCLASS_LOGIC
,
72 OPCLASS_SHIFT
, OPCLASS_SHIFT
,
74 OPCLASS_COMPARE
, OPCLASS_COMPARE
,
75 // SAT, CEIL, FLOOR, TRUNC; CVT
76 OPCLASS_CONVERT
, OPCLASS_CONVERT
, OPCLASS_CONVERT
, OPCLASS_CONVERT
,
78 // SET(AND,OR,XOR); SELP, SLCT
79 OPCLASS_COMPARE
, OPCLASS_COMPARE
, OPCLASS_COMPARE
, OPCLASS_COMPARE
,
80 OPCLASS_COMPARE
, OPCLASS_COMPARE
,
81 // RCP, RSQ, LG2, SIN, COS; EX2, EXP, LOG, PRESIN, PREEX2; SQRT, POW
82 OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
,
83 OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
, OPCLASS_SFU
,
84 OPCLASS_SFU
, OPCLASS_SFU
,
85 // BRA, CALL, RET; CONT, BREAK, PRE(RET,CONT,BREAK); BRKPT, JOINAT, JOIN
86 OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
,
87 OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
,
88 OPCLASS_FLOW
, OPCLASS_FLOW
, OPCLASS_FLOW
,
90 OPCLASS_FLOW
, OPCLASS_FLOW
,
93 // VFETCH, PFETCH, EXPORT
94 OPCLASS_LOAD
, OPCLASS_OTHER
, OPCLASS_STORE
,
96 OPCLASS_SFU
, OPCLASS_SFU
,
98 OPCLASS_OTHER
, OPCLASS_OTHER
,
99 // TEX, TXB, TXL, TXF; TXQ, TXD, TXG, TEXCSAA
100 OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
,
101 OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
, OPCLASS_TEXTURE
,
103 OPCLASS_SURFACE
, OPCLASS_SURFACE
,
104 // DFDX, DFDY, RDSV, WRSV; TEXPREP, QUADOP, QUADON, QUADPOP
105 OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
,
106 OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
,
107 // POPCNT, INSBF, EXTBF
108 OPCLASS_OTHER
, OPCLASS_OTHER
, OPCLASS_OTHER
,
111 OPCLASS_PSEUDO
// LAST
115 extern Target
*getTargetNVC0(unsigned int chipset
);
116 extern Target
*getTargetNV50(unsigned int chipset
);
118 Target
*Target::create(unsigned int chipset
)
120 switch (chipset
& 0xf0) {
124 case NVISA_GK110_CHIPSET
:
125 return getTargetNVC0(chipset
);
130 return getTargetNV50(chipset
);
132 ERROR("unsupported target: NV%x\n", chipset
);
137 void Target::destroy(Target
*targ
)
142 CodeEmitter::CodeEmitter(const Target
*target
) : targ(target
)
147 CodeEmitter::setCodeLocation(void *ptr
, uint32_t size
)
149 code
= reinterpret_cast<uint32_t *>(ptr
);
151 codeSizeLimit
= size
;
155 CodeEmitter::printBinary() const
157 uint32_t *bin
= code
- codeSize
/ 4;
158 INFO("program binary (%u bytes)", codeSize
);
159 for (unsigned int pos
= 0; pos
< codeSize
/ 4; ++pos
) {
162 INFO("%08x ", bin
[pos
]);
167 static inline uint32_t sizeToBundlesNVE4(uint32_t size
)
169 return (size
+ 55) / 56;
173 CodeEmitter::prepareEmission(Program
*prog
)
175 for (ArrayList::Iterator fi
= prog
->allFuncs
.iterator();
176 !fi
.end(); fi
.next()) {
177 Function
*func
= reinterpret_cast<Function
*>(fi
.get());
178 func
->binPos
= prog
->binSize
;
179 prepareEmission(func
);
181 // adjust sizes & positions for schedulding info:
182 if (prog
->getTarget()->hasSWSched
) {
183 BasicBlock
*bb
= NULL
;
184 for (int i
= 0; i
< func
->bbCount
; ++i
) {
185 bb
= func
->bbArray
[i
];
186 const uint32_t oldPos
= bb
->binPos
;
187 const uint32_t oldEnd
= bb
->binPos
+ bb
->binSize
;
188 uint32_t adjPos
= oldPos
+ sizeToBundlesNVE4(oldPos
) * 8;
189 uint32_t adjEnd
= oldEnd
+ sizeToBundlesNVE4(oldEnd
) * 8;
191 bb
->binSize
= adjEnd
- adjPos
;
194 func
->binSize
= bb
->binPos
+ bb
->binSize
;
197 prog
->binSize
+= func
->binSize
;
202 CodeEmitter::prepareEmission(Function
*func
)
205 func
->bbArray
= new BasicBlock
* [func
->cfg
.getSize()];
207 BasicBlock::get(func
->cfg
.getRoot())->binPos
= func
->binPos
;
209 for (IteratorRef it
= func
->cfg
.iteratorCFG(); !it
->end(); it
->next())
210 prepareEmission(BasicBlock::get(*it
));
214 CodeEmitter::prepareEmission(BasicBlock
*bb
)
216 Instruction
*i
, *next
;
217 Function
*func
= bb
->getFunction();
221 for (j
= func
->bbCount
- 1; j
>= 0 && !func
->bbArray
[j
]->binSize
; --j
);
223 for (; j
>= 0; --j
) {
224 BasicBlock
*in
= func
->bbArray
[j
];
225 Instruction
*exit
= in
->getExit();
227 if (exit
&& exit
->op
== OP_BRA
&& exit
->asFlow()->target
.bb
== bb
) {
231 for (++j
; j
< func
->bbCount
; ++j
)
232 func
->bbArray
[j
]->binPos
-= 8;
236 bb
->binPos
= in
->binPos
+ in
->binSize
;
237 if (in
->binSize
) // no more no-op branches to bb
240 func
->bbArray
[func
->bbCount
++] = bb
;
245 // determine encoding size, try to group short instructions
247 for (i
= bb
->getEntry(); i
; i
= next
) {
250 i
->encSize
= getMinEncodingSize(i
);
251 if (next
&& i
->encSize
< 8)
254 if ((nShort
& 1) && next
&& getMinEncodingSize(next
) == 4) {
255 if (i
->isCommutationLegal(i
->next
)) {
256 bb
->permuteAdjacent(i
, next
);
262 if (i
->isCommutationLegal(i
->prev
) && next
->next
) {
263 bb
->permuteAdjacent(i
->prev
, i
);
270 i
->prev
->encSize
= 8;
277 i
->prev
->encSize
= 8;
282 bb
->binSize
+= i
->encSize
;
285 if (bb
->getExit()->encSize
== 4) {
287 bb
->getExit()->encSize
= 8;
290 if ((bb
->getExit()->prev
->encSize
== 4) && !(nShort
& 1)) {
292 bb
->getExit()->prev
->encSize
= 8;
295 assert(!bb
->getEntry() || (bb
->getExit() && bb
->getExit()->encSize
== 8));
297 func
->binSize
+= bb
->binSize
;
301 Program::emitSymbolTable(struct nv50_ir_prog_info
*info
)
303 unsigned int n
= 0, nMax
= allFuncs
.getSize();
306 (struct nv50_ir_prog_symbol
*)MALLOC(nMax
* sizeof(*info
->bin
.syms
));
308 for (ArrayList::Iterator fi
= allFuncs
.iterator();
311 Function
*f
= (Function
*)fi
.get();
314 info
->bin
.syms
[n
].label
= f
->getLabel();
315 info
->bin
.syms
[n
].offset
= f
->binPos
;
318 info
->bin
.numSyms
= n
;
322 Program::emitBinary(struct nv50_ir_prog_info
*info
)
324 CodeEmitter
*emit
= target
->getCodeEmitter(progType
);
326 emit
->prepareEmission(this);
328 if (dbgFlags
& NV50_IR_DEBUG_BASIC
)
335 code
= reinterpret_cast<uint32_t *>(MALLOC(binSize
));
338 emit
->setCodeLocation(code
, binSize
);
340 for (ArrayList::Iterator fi
= allFuncs
.iterator(); !fi
.end(); fi
.next()) {
341 Function
*fn
= reinterpret_cast<Function
*>(fi
.get());
343 assert(emit
->getCodeSize() == fn
->binPos
);
345 for (int b
= 0; b
< fn
->bbCount
; ++b
)
346 for (Instruction
*i
= fn
->bbArray
[b
]->getEntry(); i
; i
= i
->next
)
347 emit
->emitInstruction(i
);
349 info
->bin
.relocData
= emit
->getRelocInfo();
351 emitSymbolTable(info
);
353 // the nvc0 driver will print the binary iself together with the header
354 if ((dbgFlags
& NV50_IR_DEBUG_BASIC
) && getTarget()->getChipset() < 0xc0)
361 #define RELOC_ALLOC_INCREMENT 8
364 CodeEmitter::addReloc(RelocEntry::Type ty
, int w
, uint32_t data
, uint32_t m
,
367 unsigned int n
= relocInfo
? relocInfo
->count
: 0;
369 if (!(n
% RELOC_ALLOC_INCREMENT
)) {
370 size_t size
= sizeof(RelocInfo
) + n
* sizeof(RelocEntry
);
371 relocInfo
= reinterpret_cast<RelocInfo
*>(
372 REALLOC(relocInfo
, n
? size
: 0,
373 size
+ RELOC_ALLOC_INCREMENT
* sizeof(RelocEntry
)));
377 memset(relocInfo
, 0, sizeof(RelocInfo
));
381 relocInfo
->entry
[n
].data
= data
;
382 relocInfo
->entry
[n
].mask
= m
;
383 relocInfo
->entry
[n
].offset
= codeSize
+ w
* 4;
384 relocInfo
->entry
[n
].bitPos
= s
;
385 relocInfo
->entry
[n
].type
= ty
;
391 RelocEntry::apply(uint32_t *binary
, const RelocInfo
*info
) const
396 case TYPE_CODE
: value
= info
->codePos
; break;
397 case TYPE_BUILTIN
: value
= info
->libPos
; break;
398 case TYPE_DATA
: value
= info
->dataPos
; break;
404 value
= (bitPos
< 0) ? (value
>> -bitPos
) : (value
<< bitPos
);
406 binary
[offset
/ 4] &= ~mask
;
407 binary
[offset
/ 4] |= value
& mask
;
410 } // namespace nv50_ir
413 #include "nv50/codegen/nv50_ir_driver.h"
418 nv50_ir_relocate_code(void *relocData
, uint32_t *code
,
423 nv50_ir::RelocInfo
*info
= reinterpret_cast<nv50_ir::RelocInfo
*>(relocData
);
425 info
->codePos
= codePos
;
426 info
->libPos
= libPos
;
427 info
->dataPos
= dataPos
;
429 for (unsigned int i
= 0; i
< info
->count
; ++i
)
430 info
->entry
[i
].apply(code
, info
);
434 nv50_ir_get_target_library(uint32_t chipset
,
435 const uint32_t **code
, uint32_t *size
)
437 nv50_ir::Target
*targ
= nv50_ir::Target::create(chipset
);
438 targ
->getBuiltinCode(code
, size
);
439 nv50_ir::Target::destroy(targ
);