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_build_util.h"
26 #include "codegen/nv50_ir_target_nvc0.h"
38 #define QUADOP(q, r, s, t) \
39 ((QOP_##q << 6) | (QOP_##r << 4) | \
40 (QOP_##s << 2) | (QOP_##t << 0))
42 class NVC0LegalizeSSA
: public Pass
45 virtual bool visit(BasicBlock
*);
46 virtual bool visit(Function
*);
48 // we want to insert calls to the builtin library only after optimization
49 void handleDIV(Instruction
*); // integer division, modulus
50 void handleRCPRSQ(Instruction
*); // double precision float recip/rsqrt
57 NVC0LegalizeSSA::handleDIV(Instruction
*i
)
59 FlowInstruction
*call
;
63 bld
.setPosition(i
, false);
64 def
[0] = bld
.mkMovToReg(0, i
->getSrc(0))->getDef(0);
65 def
[1] = bld
.mkMovToReg(1, i
->getSrc(1))->getDef(0);
67 case TYPE_U32
: builtin
= NVC0_BUILTIN_DIV_U32
; break;
68 case TYPE_S32
: builtin
= NVC0_BUILTIN_DIV_S32
; break;
72 call
= bld
.mkFlow(OP_CALL
, NULL
, CC_ALWAYS
, NULL
);
73 bld
.mkMov(i
->getDef(0), def
[(i
->op
== OP_DIV
) ? 0 : 1]);
74 bld
.mkClobber(FILE_GPR
, (i
->op
== OP_DIV
) ? 0xe : 0xd, 2);
75 bld
.mkClobber(FILE_PREDICATE
, (i
->dType
== TYPE_S32
) ? 0xf : 0x3, 0);
78 call
->absolute
= call
->builtin
= 1;
79 call
->target
.builtin
= builtin
;
80 delete_Instruction(prog
, i
);
84 NVC0LegalizeSSA::handleRCPRSQ(Instruction
*i
)
90 NVC0LegalizeSSA::visit(Function
*fn
)
92 bld
.setProgram(fn
->getProgram());
97 NVC0LegalizeSSA::visit(BasicBlock
*bb
)
100 for (Instruction
*i
= bb
->getEntry(); i
; i
= next
) {
102 if (i
->dType
== TYPE_F32
)
111 if (i
->dType
== TYPE_F64
)
121 class NVC0LegalizePostRA
: public Pass
124 NVC0LegalizePostRA(const Program
*);
127 virtual bool visit(Function
*);
128 virtual bool visit(BasicBlock
*);
130 void replaceZero(Instruction
*);
131 bool tryReplaceContWithBra(BasicBlock
*);
132 void propagateJoin(BasicBlock
*);
136 TexUse(Instruction
*use
, const Instruction
*tex
)
137 : insn(use
), tex(tex
), level(-1) { }
139 const Instruction
*tex
; // or split / mov
145 Limits(int min
, int max
) : min(min
), max(max
) { }
148 bool insertTextureBarriers(Function
*);
149 inline bool insnDominatedBy(const Instruction
*, const Instruction
*) const;
150 void findFirstUses(const Instruction
*tex
, const Instruction
*def
,
152 void findOverwritingDefs(const Instruction
*tex
, Instruction
*insn
,
153 const BasicBlock
*term
,
155 void addTexUse(std::list
<TexUse
>&, Instruction
*, const Instruction
*);
156 const Instruction
*recurseDef(const Instruction
*);
161 const bool needTexBar
;
164 NVC0LegalizePostRA::NVC0LegalizePostRA(const Program
*prog
)
167 needTexBar(prog
->getTarget()->getChipset() >= 0xe0)
172 NVC0LegalizePostRA::insnDominatedBy(const Instruction
*later
,
173 const Instruction
*early
) const
175 if (early
->bb
== later
->bb
)
176 return early
->serial
< later
->serial
;
177 return later
->bb
->dominatedBy(early
->bb
);
181 NVC0LegalizePostRA::addTexUse(std::list
<TexUse
> &uses
,
182 Instruction
*usei
, const Instruction
*insn
)
185 for (std::list
<TexUse
>::iterator it
= uses
.begin();
187 if (insnDominatedBy(usei
, it
->insn
)) {
191 if (insnDominatedBy(it
->insn
, usei
))
197 uses
.push_back(TexUse(usei
, insn
));
201 NVC0LegalizePostRA::findOverwritingDefs(const Instruction
*texi
,
203 const BasicBlock
*term
,
204 std::list
<TexUse
> &uses
)
206 while (insn
->op
== OP_MOV
&& insn
->getDef(0)->equals(insn
->getSrc(0)))
207 insn
= insn
->getSrc(0)->getUniqueInsn();
209 if (!insn
|| !insn
->bb
->reachableBy(texi
->bb
, term
))
213 /* Values not connected to the tex's definition through any of these should
214 * not be conflicting.
221 for (int s
= 0; insn
->srcExists(s
); ++s
)
222 findOverwritingDefs(texi
, insn
->getSrc(s
)->getUniqueInsn(), term
,
226 // if (!isTextureOp(insn->op)) // TODO: are TEXes always ordered ?
227 addTexUse(uses
, insn
, texi
);
233 NVC0LegalizePostRA::findFirstUses(const Instruction
*texi
,
234 const Instruction
*insn
,
235 std::list
<TexUse
> &uses
)
237 for (int d
= 0; insn
->defExists(d
); ++d
) {
238 Value
*v
= insn
->getDef(d
);
239 for (Value::UseIterator u
= v
->uses
.begin(); u
!= v
->uses
.end(); ++u
) {
240 Instruction
*usei
= (*u
)->getInsn();
242 if (usei
->op
== OP_PHI
|| usei
->op
== OP_UNION
) {
243 // need a barrier before WAW cases
244 for (int s
= 0; usei
->srcExists(s
); ++s
) {
245 Instruction
*defi
= usei
->getSrc(s
)->getUniqueInsn();
246 if (defi
&& &usei
->src(s
) != *u
)
247 findOverwritingDefs(texi
, defi
, usei
->bb
, uses
);
251 if (usei
->op
== OP_SPLIT
||
252 usei
->op
== OP_MERGE
||
253 usei
->op
== OP_PHI
||
254 usei
->op
== OP_UNION
) {
255 // these uses don't manifest in the machine code
256 findFirstUses(texi
, usei
, uses
);
258 if (usei
->op
== OP_MOV
&& usei
->getDef(0)->equals(usei
->getSrc(0)) &&
259 usei
->subOp
!= NV50_IR_SUBOP_MOV_FINAL
) {
260 findFirstUses(texi
, usei
, uses
);
262 addTexUse(uses
, usei
, insn
);
269 // This pass is a bit long and ugly and can probably be optimized.
271 // 1. obtain a list of TEXes and their outputs' first use(s)
272 // 2. calculate the barrier level of each first use (minimal number of TEXes,
273 // over all paths, between the TEX and the use in question)
274 // 3. for each barrier, if all paths from the source TEX to that barrier
275 // contain a barrier of lesser level, it can be culled
277 NVC0LegalizePostRA::insertTextureBarriers(Function
*fn
)
279 std::list
<TexUse
> *uses
;
280 std::vector
<Instruction
*> texes
;
281 std::vector
<int> bbFirstTex
;
282 std::vector
<int> bbFirstUse
;
283 std::vector
<int> texCounts
;
284 std::vector
<TexUse
> useVec
;
287 fn
->orderInstructions(insns
);
289 texCounts
.resize(fn
->allBBlocks
.getSize(), 0);
290 bbFirstTex
.resize(fn
->allBBlocks
.getSize(), insns
.getSize());
291 bbFirstUse
.resize(fn
->allBBlocks
.getSize(), insns
.getSize());
293 // tag BB CFG nodes by their id for later
294 for (ArrayList::Iterator i
= fn
->allBBlocks
.iterator(); !i
.end(); i
.next()) {
295 BasicBlock
*bb
= reinterpret_cast<BasicBlock
*>(i
.get());
297 bb
->cfg
.tag
= bb
->getId();
300 // gather the first uses for each TEX
301 for (int i
= 0; i
< insns
.getSize(); ++i
) {
302 Instruction
*tex
= reinterpret_cast<Instruction
*>(insns
.get(i
));
303 if (isTextureOp(tex
->op
)) {
304 texes
.push_back(tex
);
305 if (!texCounts
.at(tex
->bb
->getId()))
306 bbFirstTex
[tex
->bb
->getId()] = texes
.size() - 1;
307 texCounts
[tex
->bb
->getId()]++;
313 uses
= new std::list
<TexUse
>[texes
.size()];
316 for (size_t i
= 0; i
< texes
.size(); ++i
)
317 findFirstUses(texes
[i
], texes
[i
], uses
[i
]);
319 // determine the barrier level at each use
320 for (size_t i
= 0; i
< texes
.size(); ++i
) {
321 for (std::list
<TexUse
>::iterator u
= uses
[i
].begin(); u
!= uses
[i
].end();
323 BasicBlock
*tb
= texes
[i
]->bb
;
324 BasicBlock
*ub
= u
->insn
->bb
;
327 for (size_t j
= i
+ 1; j
< texes
.size() &&
328 texes
[j
]->bb
== tb
&& texes
[j
]->serial
< u
->insn
->serial
;
332 u
->level
= fn
->cfg
.findLightestPathWeight(&tb
->cfg
,
333 &ub
->cfg
, texCounts
);
335 WARN("Failed to find path TEX -> TEXBAR\n");
339 // this counted all TEXes in the origin block, correct that
340 u
->level
-= i
- bbFirstTex
.at(tb
->getId()) + 1 /* this TEX */;
341 // and did not count the TEXes in the destination block, add those
342 for (size_t j
= bbFirstTex
.at(ub
->getId()); j
< texes
.size() &&
343 texes
[j
]->bb
== ub
&& texes
[j
]->serial
< u
->insn
->serial
;
347 assert(u
->level
>= 0);
348 useVec
.push_back(*u
);
354 // insert the barriers
355 for (size_t i
= 0; i
< useVec
.size(); ++i
) {
356 Instruction
*prev
= useVec
[i
].insn
->prev
;
357 if (useVec
[i
].level
< 0)
359 if (prev
&& prev
->op
== OP_TEXBAR
) {
360 if (prev
->subOp
> useVec
[i
].level
)
361 prev
->subOp
= useVec
[i
].level
;
362 prev
->setSrc(prev
->srcCount(), useVec
[i
].tex
->getDef(0));
364 Instruction
*bar
= new_Instruction(func
, OP_TEXBAR
, TYPE_NONE
);
366 bar
->subOp
= useVec
[i
].level
;
367 // make use explicit to ease latency calculation
368 bar
->setSrc(bar
->srcCount(), useVec
[i
].tex
->getDef(0));
369 useVec
[i
].insn
->bb
->insertBefore(useVec
[i
].insn
, bar
);
373 if (fn
->getProgram()->optLevel
< 3) {
379 std::vector
<Limits
> limitT
, limitB
, limitS
; // entry, exit, single
381 limitT
.resize(fn
->allBBlocks
.getSize(), Limits(0, 0));
382 limitB
.resize(fn
->allBBlocks
.getSize(), Limits(0, 0));
383 limitS
.resize(fn
->allBBlocks
.getSize());
385 // cull unneeded barriers (should do that earlier, but for simplicity)
386 IteratorRef bi
= fn
->cfg
.iteratorCFG();
387 // first calculate min/max outstanding TEXes for each BB
388 for (bi
->reset(); !bi
->end(); bi
->next()) {
389 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(bi
->get());
390 BasicBlock
*bb
= BasicBlock::get(n
);
392 int max
= std::numeric_limits
<int>::max();
393 for (Instruction
*i
= bb
->getFirst(); i
; i
= i
->next
) {
394 if (isTextureOp(i
->op
)) {
396 if (max
< std::numeric_limits
<int>::max())
399 if (i
->op
== OP_TEXBAR
) {
400 min
= MIN2(min
, i
->subOp
);
401 max
= MIN2(max
, i
->subOp
);
404 // limits when looking at an isolated block
405 limitS
[bb
->getId()].min
= min
;
406 limitS
[bb
->getId()].max
= max
;
408 // propagate the min/max values
409 for (unsigned int l
= 0; l
<= fn
->loopNestingBound
; ++l
) {
410 for (bi
->reset(); !bi
->end(); bi
->next()) {
411 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(bi
->get());
412 BasicBlock
*bb
= BasicBlock::get(n
);
413 const int bbId
= bb
->getId();
414 for (Graph::EdgeIterator ei
= n
->incident(); !ei
.end(); ei
.next()) {
415 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
416 const int inId
= in
->getId();
417 limitT
[bbId
].min
= MAX2(limitT
[bbId
].min
, limitB
[inId
].min
);
418 limitT
[bbId
].max
= MAX2(limitT
[bbId
].max
, limitB
[inId
].max
);
420 // I just hope this is correct ...
421 if (limitS
[bbId
].max
== std::numeric_limits
<int>::max()) {
423 limitB
[bbId
].min
= limitT
[bbId
].min
+ limitS
[bbId
].min
;
424 limitB
[bbId
].max
= limitT
[bbId
].max
+ limitS
[bbId
].min
;
426 // block contained a barrier
427 limitB
[bbId
].min
= MIN2(limitS
[bbId
].max
,
428 limitT
[bbId
].min
+ limitS
[bbId
].min
);
429 limitB
[bbId
].max
= MIN2(limitS
[bbId
].max
,
430 limitT
[bbId
].max
+ limitS
[bbId
].min
);
434 // finally delete unnecessary barriers
435 for (bi
->reset(); !bi
->end(); bi
->next()) {
436 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(bi
->get());
437 BasicBlock
*bb
= BasicBlock::get(n
);
438 Instruction
*prev
= NULL
;
440 int max
= limitT
[bb
->getId()].max
;
441 for (Instruction
*i
= bb
->getFirst(); i
; i
= next
) {
443 if (i
->op
== OP_TEXBAR
) {
444 if (i
->subOp
>= max
) {
445 delete_Instruction(prog
, i
);
449 if (prev
&& prev
->op
== OP_TEXBAR
&& prev
->subOp
>= max
) {
450 delete_Instruction(prog
, prev
);
455 if (isTextureOp(i
->op
)) {
458 if (i
&& !i
->isNop())
468 NVC0LegalizePostRA::visit(Function
*fn
)
471 insertTextureBarriers(fn
);
473 rZero
= new_LValue(fn
, FILE_GPR
);
474 carry
= new_LValue(fn
, FILE_FLAGS
);
476 rZero
->reg
.data
.id
= prog
->getTarget()->getFileSize(FILE_GPR
);
477 carry
->reg
.data
.id
= 0;
483 NVC0LegalizePostRA::replaceZero(Instruction
*i
)
485 for (int s
= 0; i
->srcExists(s
); ++s
) {
486 if (s
== 2 && i
->op
== OP_SUCLAMP
)
488 ImmediateValue
*imm
= i
->getSrc(s
)->asImm();
489 if (imm
&& imm
->reg
.data
.u64
== 0)
494 // replace CONT with BRA for single unconditional continue
496 NVC0LegalizePostRA::tryReplaceContWithBra(BasicBlock
*bb
)
498 if (bb
->cfg
.incidentCount() != 2 || bb
->getEntry()->op
!= OP_PRECONT
)
500 Graph::EdgeIterator ei
= bb
->cfg
.incident();
501 if (ei
.getType() != Graph::Edge::BACK
)
503 if (ei
.getType() != Graph::Edge::BACK
)
505 BasicBlock
*contBB
= BasicBlock::get(ei
.getNode());
507 if (!contBB
->getExit() || contBB
->getExit()->op
!= OP_CONT
||
508 contBB
->getExit()->getPredicate())
510 contBB
->getExit()->op
= OP_BRA
;
511 bb
->remove(bb
->getEntry()); // delete PRECONT
514 assert(ei
.end() || ei
.getType() != Graph::Edge::BACK
);
518 // replace branches to join blocks with join ops
520 NVC0LegalizePostRA::propagateJoin(BasicBlock
*bb
)
522 if (bb
->getEntry()->op
!= OP_JOIN
|| bb
->getEntry()->asFlow()->limit
)
524 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
525 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
526 Instruction
*exit
= in
->getExit();
528 in
->insertTail(new FlowInstruction(func
, OP_JOIN
, bb
));
529 // there should always be a terminator instruction
530 WARN("inserted missing terminator in BB:%i\n", in
->getId());
532 if (exit
->op
== OP_BRA
) {
534 exit
->asFlow()->limit
= 1; // must-not-propagate marker
537 bb
->remove(bb
->getEntry());
541 NVC0LegalizePostRA::visit(BasicBlock
*bb
)
543 Instruction
*i
, *next
;
545 // remove pseudo operations and non-fixed no-ops, split 64 bit operations
546 for (i
= bb
->getFirst(); i
; i
= next
) {
548 if (i
->op
== OP_EMIT
|| i
->op
== OP_RESTART
) {
549 if (!i
->getDef(0)->refCount())
551 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
552 i
->setSrc(0, rZero
); // initial value must be 0
557 // TODO: Move this to before register allocation for operations that
558 // need the $c register !
559 if (typeSizeof(i
->dType
) == 8) {
561 hi
= BuildUtil::split64BitOpPostRA(func
, i
, rZero
, carry
);
566 if (i
->op
!= OP_MOV
&& i
->op
!= OP_PFETCH
)
573 if (!tryReplaceContWithBra(bb
))
579 class NVC0LoweringPass
: public Pass
582 NVC0LoweringPass(Program
*);
585 virtual bool visit(Function
*);
586 virtual bool visit(BasicBlock
*);
587 virtual bool visit(Instruction
*);
589 bool handleRDSV(Instruction
*);
590 bool handleWRSV(Instruction
*);
591 bool handleEXPORT(Instruction
*);
592 bool handleOUT(Instruction
*);
593 bool handleDIV(Instruction
*);
594 bool handleMOD(Instruction
*);
595 bool handleSQRT(Instruction
*);
596 bool handlePOW(Instruction
*);
597 bool handleTEX(TexInstruction
*);
598 bool handleTXD(TexInstruction
*);
599 bool handleTXQ(TexInstruction
*);
600 bool handleManualTXD(TexInstruction
*);
601 bool handleTXLQ(TexInstruction
*);
602 bool handleATOM(Instruction
*);
603 bool handleCasExch(Instruction
*, bool needCctl
);
604 void handleSurfaceOpNVE4(TexInstruction
*);
606 void checkPredicate(Instruction
*);
608 void readTessCoord(LValue
*dst
, int c
);
610 Value
*loadResInfo32(Value
*ptr
, uint32_t off
);
611 Value
*loadMsInfo32(Value
*ptr
, uint32_t off
);
612 Value
*loadTexHandle(Value
*ptr
, unsigned int slot
);
614 void adjustCoordinatesMS(TexInstruction
*);
615 void processSurfaceCoordsNVE4(TexInstruction
*);
618 const Target
*const targ
;
623 LValue
*gpEmitAddress
;
626 NVC0LoweringPass::NVC0LoweringPass(Program
*prog
) : targ(prog
->getTarget())
628 bld
.setProgram(prog
);
633 NVC0LoweringPass::visit(Function
*fn
)
635 if (prog
->getType() == Program::TYPE_GEOMETRY
) {
636 assert(!strncmp(fn
->getName(), "MAIN", 4));
637 // TODO: when we generate actual functions pass this value along somehow
638 bld
.setPosition(BasicBlock::get(fn
->cfg
.getRoot()), false);
639 gpEmitAddress
= bld
.loadImm(NULL
, 0)->asLValue();
641 bld
.setPosition(BasicBlock::get(fn
->cfgExit
)->getExit(), false);
642 bld
.mkMovToReg(0, gpEmitAddress
);
649 NVC0LoweringPass::visit(BasicBlock
*bb
)
655 NVC0LoweringPass::loadTexHandle(Value
*ptr
, unsigned int slot
)
657 uint8_t b
= prog
->driver
->io
.resInfoCBSlot
;
658 uint32_t off
= prog
->driver
->io
.texBindBase
+ slot
* 4;
660 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U32
, off
), ptr
);
663 // move array source to first slot, convert to u16, add indirections
665 NVC0LoweringPass::handleTEX(TexInstruction
*i
)
667 const int dim
= i
->tex
.target
.getDim() + i
->tex
.target
.isCube();
668 const int arg
= i
->tex
.target
.getArgCount();
669 const int lyr
= arg
- (i
->tex
.target
.isMS() ? 2 : 1);
670 const int chipset
= prog
->getTarget()->getChipset();
672 if (chipset
>= NVISA_GK104_CHIPSET
) {
673 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0) {
674 WARN("indirect TEX not implemented\n");
676 if (i
->tex
.r
== i
->tex
.s
) {
677 i
->tex
.r
+= prog
->driver
->io
.texBindBase
/ 4;
678 i
->tex
.s
= 0; // only a single cX[] value possible here
680 Value
*hnd
= bld
.getScratch();
681 Value
*rHnd
= loadTexHandle(NULL
, i
->tex
.r
);
682 Value
*sHnd
= loadTexHandle(NULL
, i
->tex
.s
);
684 bld
.mkOp3(OP_INSBF
, TYPE_U32
, hnd
, rHnd
, bld
.mkImm(0x1400), sHnd
);
686 i
->tex
.r
= 0; // not used for indirect tex
688 i
->setIndirectR(hnd
);
690 if (i
->tex
.target
.isArray()) {
691 LValue
*layer
= new_LValue(func
, FILE_GPR
);
692 Value
*src
= i
->getSrc(lyr
);
693 const int sat
= (i
->op
== OP_TXF
) ? 1 : 0;
694 DataType sTy
= (i
->op
== OP_TXF
) ? TYPE_U32
: TYPE_F32
;
695 bld
.mkCvt(OP_CVT
, TYPE_U16
, layer
, sTy
, src
)->saturate
= sat
;
696 for (int s
= dim
; s
>= 1; --s
)
697 i
->setSrc(s
, i
->getSrc(s
- 1));
701 // (nvc0) generate and move the tsc/tic/array source to the front
702 if (i
->tex
.target
.isArray() || i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0) {
703 LValue
*src
= new_LValue(func
, FILE_GPR
); // 0xttxsaaaa
705 Value
*arrayIndex
= i
->tex
.target
.isArray() ? i
->getSrc(lyr
) : NULL
;
706 for (int s
= dim
; s
>= 1; --s
)
707 i
->setSrc(s
, i
->getSrc(s
- 1));
708 i
->setSrc(0, arrayIndex
);
710 Value
*ticRel
= i
->getIndirectR();
711 Value
*tscRel
= i
->getIndirectS();
714 int sat
= (i
->op
== OP_TXF
) ? 1 : 0;
715 DataType sTy
= (i
->op
== OP_TXF
) ? TYPE_U32
: TYPE_F32
;
716 bld
.mkCvt(OP_CVT
, TYPE_U16
, src
, sTy
, arrayIndex
)->saturate
= sat
;
722 i
->setSrc(i
->tex
.rIndirectSrc
, NULL
);
723 bld
.mkOp3(OP_INSBF
, TYPE_U32
, src
, ticRel
, bld
.mkImm(0x0917), src
);
726 i
->setSrc(i
->tex
.sIndirectSrc
, NULL
);
727 bld
.mkOp3(OP_INSBF
, TYPE_U32
, src
, tscRel
, bld
.mkImm(0x0710), src
);
733 // For nvc0, the sample id has to be in the second operand, as the offset
734 // does. Right now we don't know how to pass both in, and this case can't
735 // happen with OpenGL. On nve0, the sample id is part of the texture
736 // coordinate argument.
737 assert(chipset
>= NVISA_GK104_CHIPSET
||
738 !i
->tex
.useOffsets
|| !i
->tex
.target
.isMS());
740 // offset is last source (lod 1st, dc 2nd)
741 if (i
->tex
.useOffsets
) {
744 int s
= i
->srcCount(0xff, true);
745 if (i
->srcExists(s
)) // move potential predicate out of the way
746 i
->moveSources(s
, 1);
747 if (i
->op
== OP_TXG
) {
748 assert(i
->tex
.useOffsets
== 1);
749 for (c
= 0; c
< 3; ++c
)
750 value
|= (i
->tex
.offset
[0][c
] & 0xff) << (c
* 8);
752 for (n
= 0; n
< i
->tex
.useOffsets
; ++n
)
753 for (c
= 0; c
< 3; ++c
)
754 value
|= (i
->tex
.offset
[n
][c
] & 0xf) << (n
* 12 + c
* 4);
756 i
->setSrc(s
, bld
.loadImm(NULL
, value
));
759 if (chipset
>= NVISA_GK104_CHIPSET
) {
761 // If TEX requires more than 4 sources, the 2nd register tuple must be
762 // aligned to 4, even if it consists of just a single 4-byte register.
764 // XXX HACK: We insert 0 sources to avoid the 5 or 6 regs case.
766 int s
= i
->srcCount(0xff, true);
767 if (s
> 4 && s
< 7) {
768 if (i
->srcExists(s
)) // move potential predicate out of the way
769 i
->moveSources(s
, 7 - s
);
771 i
->setSrc(s
++, bld
.loadImm(NULL
, 0));
779 NVC0LoweringPass::handleManualTXD(TexInstruction
*i
)
781 static const uint8_t qOps
[4][2] =
783 { QUADOP(MOV2
, ADD
, MOV2
, ADD
), QUADOP(MOV2
, MOV2
, ADD
, ADD
) }, // l0
784 { QUADOP(SUBR
, MOV2
, SUBR
, MOV2
), QUADOP(MOV2
, MOV2
, ADD
, ADD
) }, // l1
785 { QUADOP(MOV2
, ADD
, MOV2
, ADD
), QUADOP(SUBR
, SUBR
, MOV2
, MOV2
) }, // l2
786 { QUADOP(SUBR
, MOV2
, SUBR
, MOV2
), QUADOP(SUBR
, SUBR
, MOV2
, MOV2
) }, // l3
791 Value
*zero
= bld
.loadImm(bld
.getSSA(), 0);
793 const int dim
= i
->tex
.target
.getDim();
795 i
->op
= OP_TEX
; // no need to clone dPdx/dPdy later
797 for (c
= 0; c
< dim
; ++c
)
798 crd
[c
] = bld
.getScratch();
800 bld
.mkOp(OP_QUADON
, TYPE_NONE
, NULL
);
801 for (l
= 0; l
< 4; ++l
) {
802 // mov coordinates from lane l to all lanes
803 for (c
= 0; c
< dim
; ++c
)
804 bld
.mkQuadop(0x00, crd
[c
], l
, i
->getSrc(c
), zero
);
805 // add dPdx from lane l to lanes dx
806 for (c
= 0; c
< dim
; ++c
)
807 bld
.mkQuadop(qOps
[l
][0], crd
[c
], l
, i
->dPdx
[c
].get(), crd
[c
]);
808 // add dPdy from lane l to lanes dy
809 for (c
= 0; c
< dim
; ++c
)
810 bld
.mkQuadop(qOps
[l
][1], crd
[c
], l
, i
->dPdy
[c
].get(), crd
[c
]);
812 bld
.insert(tex
= cloneForward(func
, i
));
813 for (c
= 0; c
< dim
; ++c
)
814 tex
->setSrc(c
, crd
[c
]);
816 for (c
= 0; i
->defExists(c
); ++c
) {
818 def
[c
][l
] = bld
.getSSA();
819 mov
= bld
.mkMov(def
[c
][l
], tex
->getDef(c
));
824 bld
.mkOp(OP_QUADPOP
, TYPE_NONE
, NULL
);
826 for (c
= 0; i
->defExists(c
); ++c
) {
827 Instruction
*u
= bld
.mkOp(OP_UNION
, TYPE_U32
, i
->getDef(c
));
828 for (l
= 0; l
< 4; ++l
)
829 u
->setSrc(l
, def
[c
][l
]);
837 NVC0LoweringPass::handleTXD(TexInstruction
*txd
)
839 int dim
= txd
->tex
.target
.getDim();
840 int arg
= txd
->tex
.target
.getArgCount();
843 while (txd
->srcExists(arg
))
846 txd
->tex
.derivAll
= true;
848 txd
->tex
.target
.isCube() ||
850 txd
->tex
.target
.isShadow())
851 return handleManualTXD(txd
);
853 for (int c
= 0; c
< dim
; ++c
) {
854 txd
->setSrc(arg
+ c
* 2 + 0, txd
->dPdx
[c
]);
855 txd
->setSrc(arg
+ c
* 2 + 1, txd
->dPdy
[c
]);
856 txd
->dPdx
[c
].set(NULL
);
857 txd
->dPdy
[c
].set(NULL
);
863 NVC0LoweringPass::handleTXQ(TexInstruction
*txq
)
865 // TODO: indirect resource/sampler index
870 NVC0LoweringPass::handleTXLQ(TexInstruction
*i
)
872 /* The outputs are inverted compared to what the TGSI instruction
873 * expects. Take that into account in the mask.
875 assert((i
->tex
.mask
& ~3) == 0);
876 if (i
->tex
.mask
== 1)
878 else if (i
->tex
.mask
== 2)
881 bld
.setPosition(i
, true);
883 /* The returned values are not quite what we want:
884 * (a) convert from s16/u16 to f32
885 * (b) multiply by 1/256
887 for (int def
= 0; def
< 2; ++def
) {
888 if (!i
->defExists(def
))
890 enum DataType type
= TYPE_S16
;
891 if (i
->tex
.mask
== 2 || def
> 0)
893 bld
.mkCvt(OP_CVT
, TYPE_F32
, i
->getDef(def
), type
, i
->getDef(def
));
894 bld
.mkOp2(OP_MUL
, TYPE_F32
, i
->getDef(def
),
895 i
->getDef(def
), bld
.loadImm(NULL
, 1.0f
/ 256));
897 if (i
->tex
.mask
== 3) {
898 LValue
*t
= new_LValue(func
, FILE_GPR
);
899 bld
.mkMov(t
, i
->getDef(0));
900 bld
.mkMov(i
->getDef(0), i
->getDef(1));
901 bld
.mkMov(i
->getDef(1), t
);
908 NVC0LoweringPass::handleATOM(Instruction
*atom
)
912 switch (atom
->src(0).getFile()) {
913 case FILE_MEMORY_LOCAL
:
916 case FILE_MEMORY_SHARED
:
920 assert(atom
->src(0).getFile() == FILE_MEMORY_GLOBAL
);
924 bld
.mkOp1v(OP_RDSV
, TYPE_U32
, bld
.getScratch(), bld
.mkSysVal(sv
, 0));
925 Value
*ptr
= atom
->getIndirect(0, 0);
927 atom
->setSrc(0, cloneShallow(func
, atom
->getSrc(0)));
928 atom
->getSrc(0)->reg
.file
= FILE_MEMORY_GLOBAL
;
930 base
= bld
.mkOp2v(OP_ADD
, TYPE_U32
, base
, base
, ptr
);
931 atom
->setIndirect(0, 0, base
);
937 NVC0LoweringPass::handleCasExch(Instruction
*cas
, bool needCctl
)
939 if (cas
->subOp
!= NV50_IR_SUBOP_ATOM_CAS
&&
940 cas
->subOp
!= NV50_IR_SUBOP_ATOM_EXCH
)
942 bld
.setPosition(cas
, true);
945 Instruction
*cctl
= bld
.mkOp1(OP_CCTL
, TYPE_NONE
, NULL
, cas
->getSrc(0));
946 cctl
->setIndirect(0, 0, cas
->getIndirect(0, 0));
948 cctl
->subOp
= NV50_IR_SUBOP_CCTL_IV
;
949 if (cas
->isPredicated())
950 cctl
->setPredicate(cas
->cc
, cas
->getPredicate());
953 if (cas
->defExists(0) && cas
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
954 // CAS is crazy. It's 2nd source is a double reg, and the 3rd source
955 // should be set to the high part of the double reg or bad things will
956 // happen elsewhere in the universe.
957 // Also, it sometimes returns the new value instead of the old one
958 // under mysterious circumstances.
959 Value
*dreg
= bld
.getSSA(8);
960 bld
.setPosition(cas
, false);
961 bld
.mkOp2(OP_MERGE
, TYPE_U64
, dreg
, cas
->getSrc(1), cas
->getSrc(2));
962 cas
->setSrc(1, dreg
);
969 NVC0LoweringPass::loadResInfo32(Value
*ptr
, uint32_t off
)
971 uint8_t b
= prog
->driver
->io
.resInfoCBSlot
;
972 off
+= prog
->driver
->io
.suInfoBase
;
974 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U32
, off
), ptr
);
978 NVC0LoweringPass::loadMsInfo32(Value
*ptr
, uint32_t off
)
980 uint8_t b
= prog
->driver
->io
.msInfoCBSlot
;
981 off
+= prog
->driver
->io
.msInfoBase
;
983 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U32
, off
), ptr
);
986 /* On nvc0, surface info is obtained via the surface binding points passed
987 * to the SULD/SUST instructions.
988 * On nve4, surface info is stored in c[] and is used by various special
989 * instructions, e.g. for clamping coordiantes or generating an address.
990 * They couldn't just have added an equivalent to TIC now, couldn't they ?
992 #define NVE4_SU_INFO_ADDR 0x00
993 #define NVE4_SU_INFO_FMT 0x04
994 #define NVE4_SU_INFO_DIM_X 0x08
995 #define NVE4_SU_INFO_PITCH 0x0c
996 #define NVE4_SU_INFO_DIM_Y 0x10
997 #define NVE4_SU_INFO_ARRAY 0x14
998 #define NVE4_SU_INFO_DIM_Z 0x18
999 #define NVE4_SU_INFO_UNK1C 0x1c
1000 #define NVE4_SU_INFO_WIDTH 0x20
1001 #define NVE4_SU_INFO_HEIGHT 0x24
1002 #define NVE4_SU_INFO_DEPTH 0x28
1003 #define NVE4_SU_INFO_TARGET 0x2c
1004 #define NVE4_SU_INFO_CALL 0x30
1005 #define NVE4_SU_INFO_RAW_X 0x34
1006 #define NVE4_SU_INFO_MS_X 0x38
1007 #define NVE4_SU_INFO_MS_Y 0x3c
1009 #define NVE4_SU_INFO__STRIDE 0x40
1011 #define NVE4_SU_INFO_DIM(i) (0x08 + (i) * 8)
1012 #define NVE4_SU_INFO_SIZE(i) (0x20 + (i) * 4)
1013 #define NVE4_SU_INFO_MS(i) (0x38 + (i) * 4)
1015 static inline uint16_t getSuClampSubOp(const TexInstruction
*su
, int c
)
1017 switch (su
->tex
.target
.getEnum()) {
1018 case TEX_TARGET_BUFFER
: return NV50_IR_SUBOP_SUCLAMP_PL(0, 1);
1019 case TEX_TARGET_RECT
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1020 case TEX_TARGET_1D
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1021 case TEX_TARGET_1D_ARRAY
: return (c
== 1) ?
1022 NV50_IR_SUBOP_SUCLAMP_PL(0, 2) :
1023 NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1024 case TEX_TARGET_2D
: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1025 case TEX_TARGET_2D_MS
: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1026 case TEX_TARGET_2D_ARRAY
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1027 case TEX_TARGET_2D_MS_ARRAY
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1028 case TEX_TARGET_3D
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1029 case TEX_TARGET_CUBE
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1030 case TEX_TARGET_CUBE_ARRAY
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1038 NVC0LoweringPass::adjustCoordinatesMS(TexInstruction
*tex
)
1040 const uint16_t base
= tex
->tex
.r
* NVE4_SU_INFO__STRIDE
;
1041 const int arg
= tex
->tex
.target
.getArgCount();
1043 if (tex
->tex
.target
== TEX_TARGET_2D_MS
)
1044 tex
->tex
.target
= TEX_TARGET_2D
;
1046 if (tex
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1047 tex
->tex
.target
= TEX_TARGET_2D_ARRAY
;
1051 Value
*x
= tex
->getSrc(0);
1052 Value
*y
= tex
->getSrc(1);
1053 Value
*s
= tex
->getSrc(arg
- 1);
1055 Value
*tx
= bld
.getSSA(), *ty
= bld
.getSSA(), *ts
= bld
.getSSA();
1057 Value
*ms_x
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_MS(0));
1058 Value
*ms_y
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_MS(1));
1060 bld
.mkOp2(OP_SHL
, TYPE_U32
, tx
, x
, ms_x
);
1061 bld
.mkOp2(OP_SHL
, TYPE_U32
, ty
, y
, ms_y
);
1063 s
= bld
.mkOp2v(OP_AND
, TYPE_U32
, ts
, s
, bld
.loadImm(NULL
, 0x7));
1064 s
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, ts
, ts
, bld
.mkImm(3));
1066 Value
*dx
= loadMsInfo32(ts
, 0x0);
1067 Value
*dy
= loadMsInfo32(ts
, 0x4);
1069 bld
.mkOp2(OP_ADD
, TYPE_U32
, tx
, tx
, dx
);
1070 bld
.mkOp2(OP_ADD
, TYPE_U32
, ty
, ty
, dy
);
1074 tex
->moveSources(arg
, -1);
1077 // Sets 64-bit "generic address", predicate and format sources for SULD/SUST.
1078 // They're computed from the coordinates using the surface info in c[] space.
1080 NVC0LoweringPass::processSurfaceCoordsNVE4(TexInstruction
*su
)
1083 const bool atom
= su
->op
== OP_SUREDB
|| su
->op
== OP_SUREDP
;
1085 su
->op
== OP_SULDB
|| su
->op
== OP_SUSTB
|| su
->op
== OP_SUREDB
;
1086 const int idx
= su
->tex
.r
;
1087 const int dim
= su
->tex
.target
.getDim();
1088 const int arg
= dim
+ (su
->tex
.target
.isArray() ? 1 : 0);
1089 const uint16_t base
= idx
* NVE4_SU_INFO__STRIDE
;
1091 Value
*zero
= bld
.mkImm(0);
1095 Value
*bf
, *eau
, *off
;
1098 off
= bld
.getScratch(4);
1099 bf
= bld
.getScratch(4);
1100 addr
= bld
.getSSA(8);
1101 pred
= bld
.getScratch(1, FILE_PREDICATE
);
1103 bld
.setPosition(su
, false);
1105 adjustCoordinatesMS(su
);
1107 // calculate clamped coordinates
1108 for (c
= 0; c
< arg
; ++c
) {
1109 src
[c
] = bld
.getScratch();
1111 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_RAW_X
);
1113 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_DIM(c
));
1114 bld
.mkOp3(OP_SUCLAMP
, TYPE_S32
, src
[c
], su
->getSrc(c
), v
, zero
)
1115 ->subOp
= getSuClampSubOp(su
, c
);
1120 // set predicate output
1121 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1122 src
[0]->getInsn()->setFlagsDef(1, pred
);
1124 if (su
->tex
.target
.isArray()) {
1125 p1
= bld
.getSSA(1, FILE_PREDICATE
);
1126 src
[dim
]->getInsn()->setFlagsDef(1, p1
);
1129 // calculate pixel offset
1131 if (su
->tex
.target
!= TEX_TARGET_BUFFER
)
1132 bld
.mkOp2(OP_AND
, TYPE_U32
, off
, src
[0], bld
.loadImm(NULL
, 0xffff));
1135 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_UNK1C
);
1136 bld
.mkOp3(OP_MADSP
, TYPE_U32
, off
, src
[2], v
, src
[1])
1137 ->subOp
= NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1139 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_PITCH
);
1140 bld
.mkOp3(OP_MADSP
, TYPE_U32
, off
, off
, v
, src
[0])
1141 ->subOp
= NV50_IR_SUBOP_MADSP(0,2,8); // u32 u16l u16l
1144 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_PITCH
);
1145 bld
.mkOp3(OP_MADSP
, TYPE_U32
, off
, src
[1], v
, src
[0])
1146 ->subOp
= su
->tex
.target
.isArray() ?
1147 NV50_IR_SUBOP_MADSP_SD
: NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1150 // calculate effective address part 1
1151 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1155 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_FMT
);
1156 bld
.mkOp3(OP_VSHL
, TYPE_U32
, bf
, src
[0], v
, zero
)
1157 ->subOp
= NV50_IR_SUBOP_V1(7,6,8|2);
1171 if (!su
->tex
.target
.isArray()) {
1172 z
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_UNK1C
);
1173 subOp
= NV50_IR_SUBOP_SUBFM_3D
;
1177 subOp
= NV50_IR_SUBOP_SUBFM_3D
;
1181 insn
= bld
.mkOp3(OP_SUBFM
, TYPE_U32
, bf
, src
[0], y
, z
);
1182 insn
->subOp
= subOp
;
1183 insn
->setFlagsDef(1, pred
);
1187 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_ADDR
);
1189 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1192 eau
= bld
.mkOp3v(OP_SUEAU
, TYPE_U32
, bld
.getScratch(4), off
, bf
, v
);
1194 // add array layer offset
1195 if (su
->tex
.target
.isArray()) {
1196 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_ARRAY
);
1198 bld
.mkOp3(OP_MADSP
, TYPE_U32
, eau
, src
[1], v
, eau
)
1199 ->subOp
= NV50_IR_SUBOP_MADSP(4,0,0); // u16 u24 u32
1201 bld
.mkOp3(OP_MADSP
, TYPE_U32
, eau
, v
, src
[2], eau
)
1202 ->subOp
= NV50_IR_SUBOP_MADSP(0,0,0); // u32 u24 u32
1203 // combine predicates
1205 bld
.mkOp2(OP_OR
, TYPE_U8
, pred
, pred
, p1
);
1210 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1214 // bf == g[] address & 0xff
1215 // eau == g[] address >> 8
1216 bld
.mkOp3(OP_PERMT
, TYPE_U32
, bf
, lo
, bld
.loadImm(NULL
, 0x6540), eau
);
1217 bld
.mkOp3(OP_PERMT
, TYPE_U32
, eau
, zero
, bld
.loadImm(NULL
, 0x0007), eau
);
1219 if (su
->op
== OP_SULDP
&& su
->tex
.target
== TEX_TARGET_BUFFER
) {
1220 // Convert from u32 to u8 address format, which is what the library code
1221 // doing SULDP currently uses.
1222 // XXX: can SUEAU do this ?
1223 // XXX: does it matter that we don't mask high bytes in bf ?
1225 bld
.mkOp2(OP_SHR
, TYPE_U32
, off
, bf
, bld
.mkImm(8));
1226 bld
.mkOp2(OP_ADD
, TYPE_U32
, eau
, eau
, off
);
1229 bld
.mkOp2(OP_MERGE
, TYPE_U64
, addr
, bf
, eau
);
1231 if (atom
&& su
->tex
.target
== TEX_TARGET_BUFFER
)
1232 bld
.mkOp2(OP_ADD
, TYPE_U64
, addr
, addr
, off
);
1234 // let's just set it 0 for raw access and hope it works
1236 bld
.mkImm(0) : loadResInfo32(NULL
, base
+ NVE4_SU_INFO_FMT
);
1238 // get rid of old coordinate sources, make space for fmt info and predicate
1239 su
->moveSources(arg
, 3 - arg
);
1240 // set 64 bit address and 32-bit format sources
1241 su
->setSrc(0, addr
);
1243 su
->setSrc(2, pred
);
1247 NVC0LoweringPass::handleSurfaceOpNVE4(TexInstruction
*su
)
1249 processSurfaceCoordsNVE4(su
);
1251 // Who do we hate more ? The person who decided that nvc0's SULD doesn't
1252 // have to support conversion or the person who decided that, in OpenCL,
1253 // you don't have to specify the format here like you do in OpenGL ?
1255 if (su
->op
== OP_SULDP
) {
1256 // We don't patch shaders. Ever.
1257 // You get an indirect call to our library blob here.
1258 // But at least it's uniform.
1259 FlowInstruction
*call
;
1262 uint16_t base
= su
->tex
.r
* NVE4_SU_INFO__STRIDE
+ NVE4_SU_INFO_CALL
;
1264 for (int i
= 0; i
< 4; ++i
)
1265 (r
[i
] = bld
.getScratch(4, FILE_GPR
))->reg
.data
.id
= i
;
1266 for (int i
= 0; i
< 3; ++i
)
1267 (p
[i
] = bld
.getScratch(1, FILE_PREDICATE
))->reg
.data
.id
= i
;
1268 (r
[4] = bld
.getScratch(8, FILE_GPR
))->reg
.data
.id
= 4;
1270 bld
.mkMov(p
[1], bld
.mkImm((su
->cache
== CACHE_CA
) ? 1 : 0), TYPE_U8
);
1271 bld
.mkMov(p
[2], bld
.mkImm((su
->cache
== CACHE_CG
) ? 1 : 0), TYPE_U8
);
1272 bld
.mkMov(p
[0], su
->getSrc(2), TYPE_U8
);
1273 bld
.mkMov(r
[4], su
->getSrc(0), TYPE_U64
);
1274 bld
.mkMov(r
[2], su
->getSrc(1), TYPE_U32
);
1276 call
= bld
.mkFlow(OP_CALL
, NULL
, su
->cc
, su
->getPredicate());
1280 call
->setSrc(0, bld
.mkSymbol(FILE_MEMORY_CONST
,
1281 prog
->driver
->io
.resInfoCBSlot
, TYPE_U32
,
1282 prog
->driver
->io
.suInfoBase
+ base
));
1283 call
->setSrc(1, r
[2]);
1284 call
->setSrc(2, r
[4]);
1285 for (int i
= 0; i
< 3; ++i
)
1286 call
->setSrc(3 + i
, p
[i
]);
1287 for (int i
= 0; i
< 4; ++i
) {
1288 call
->setDef(i
, r
[i
]);
1289 bld
.mkMov(su
->getDef(i
), r
[i
]);
1291 call
->setDef(4, p
[1]);
1292 delete_Instruction(bld
.getProgram(), su
);
1295 if (su
->op
== OP_SUREDB
|| su
->op
== OP_SUREDP
) {
1296 // FIXME: for out of bounds access, destination value will be undefined !
1297 Value
*pred
= su
->getSrc(2);
1298 CondCode cc
= CC_NOT_P
;
1299 if (su
->getPredicate()) {
1300 pred
= bld
.getScratch(1, FILE_PREDICATE
);
1302 if (cc
== CC_NOT_P
) {
1303 bld
.mkOp2(OP_OR
, TYPE_U8
, pred
, su
->getPredicate(), su
->getSrc(2));
1305 bld
.mkOp2(OP_AND
, TYPE_U8
, pred
, su
->getPredicate(), su
->getSrc(2));
1306 pred
->getInsn()->src(1).mod
= Modifier(NV50_IR_MOD_NOT
);
1309 Instruction
*red
= bld
.mkOp(OP_ATOM
, su
->dType
, su
->getDef(0));
1310 red
->subOp
= su
->subOp
;
1312 gMemBase
= bld
.mkSymbol(FILE_MEMORY_GLOBAL
, 0, TYPE_U32
, 0);
1313 red
->setSrc(0, gMemBase
);
1314 red
->setSrc(1, su
->getSrc(3));
1315 if (su
->subOp
== NV50_IR_SUBOP_ATOM_CAS
)
1316 red
->setSrc(2, su
->getSrc(4));
1317 red
->setIndirect(0, 0, su
->getSrc(0));
1318 red
->setPredicate(cc
, pred
);
1319 delete_Instruction(bld
.getProgram(), su
);
1320 handleCasExch(red
, true);
1322 su
->sType
= (su
->tex
.target
== TEX_TARGET_BUFFER
) ? TYPE_U32
: TYPE_U8
;
1327 NVC0LoweringPass::handleWRSV(Instruction
*i
)
1333 // must replace, $sreg are not writeable
1334 addr
= targ
->getSVAddress(FILE_SHADER_OUTPUT
, i
->getSrc(0)->asSym());
1337 sym
= bld
.mkSymbol(FILE_SHADER_OUTPUT
, 0, i
->sType
, addr
);
1339 st
= bld
.mkStore(OP_EXPORT
, i
->dType
, sym
, i
->getIndirect(0, 0),
1341 st
->perPatch
= i
->perPatch
;
1343 bld
.getBB()->remove(i
);
1348 NVC0LoweringPass::readTessCoord(LValue
*dst
, int c
)
1350 Value
*laneid
= bld
.getSSA();
1353 bld
.mkOp1(OP_RDSV
, TYPE_U32
, laneid
, bld
.mkSysVal(SV_LANEID
, 0));
1368 bld
.mkFetch(x
, TYPE_F32
, FILE_SHADER_OUTPUT
, 0x2f0, NULL
, laneid
);
1370 bld
.mkFetch(y
, TYPE_F32
, FILE_SHADER_OUTPUT
, 0x2f4, NULL
, laneid
);
1373 bld
.mkOp2(OP_ADD
, TYPE_F32
, dst
, x
, y
);
1374 bld
.mkOp2(OP_SUB
, TYPE_F32
, dst
, bld
.loadImm(NULL
, 1.0f
), dst
);
1379 NVC0LoweringPass::handleRDSV(Instruction
*i
)
1381 Symbol
*sym
= i
->getSrc(0)->asSym();
1382 const SVSemantic sv
= sym
->reg
.data
.sv
.sv
;
1385 uint32_t addr
= targ
->getSVAddress(FILE_SHADER_INPUT
, sym
);
1387 if (addr
>= 0x400) {
1389 if (sym
->reg
.data
.sv
.index
== 3) {
1390 // TGSI backend may use 4th component of TID,NTID,CTAID,NCTAID
1392 i
->setSrc(0, bld
.mkImm((sv
== SV_NTID
|| sv
== SV_NCTAID
) ? 1 : 0));
1399 assert(prog
->getType() == Program::TYPE_FRAGMENT
);
1400 bld
.mkInterp(NV50_IR_INTERP_LINEAR
, i
->getDef(0), addr
, NULL
);
1404 Value
*face
= i
->getDef(0);
1405 bld
.mkInterp(NV50_IR_INTERP_FLAT
, face
, addr
, NULL
);
1406 if (i
->dType
== TYPE_F32
) {
1407 bld
.mkOp2(OP_AND
, TYPE_U32
, face
, face
, bld
.mkImm(0x80000000));
1408 bld
.mkOp2(OP_XOR
, TYPE_U32
, face
, face
, bld
.mkImm(0xbf800000));
1413 assert(prog
->getType() == Program::TYPE_TESSELLATION_EVAL
);
1414 readTessCoord(i
->getDef(0)->asLValue(), i
->getSrc(0)->reg
.data
.sv
.index
);
1419 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
); // mov $sreg otherwise
1420 if (sym
->reg
.data
.sv
.index
== 3) {
1422 i
->setSrc(0, bld
.mkImm(sv
== SV_GRIDID
? 0 : 1));
1425 addr
+= prog
->driver
->prop
.cp
.gridInfoBase
;
1426 bld
.mkLoad(TYPE_U32
, i
->getDef(0),
1427 bld
.mkSymbol(FILE_MEMORY_CONST
, 0, TYPE_U32
, addr
), NULL
);
1429 case SV_SAMPLE_INDEX
:
1430 // TODO: Properly pass source as an address in the PIX address space
1431 // (which can be of the form [r0+offset]). But this is currently
1433 ld
= bld
.mkOp1(OP_PIXLD
, TYPE_U32
, i
->getDef(0), bld
.mkImm(0));
1434 ld
->subOp
= NV50_IR_SUBOP_PIXLD_SAMPLEID
;
1436 case SV_SAMPLE_POS
: {
1437 Value
*off
= new_LValue(func
, FILE_GPR
);
1438 ld
= bld
.mkOp1(OP_PIXLD
, TYPE_U32
, i
->getDef(0), bld
.mkImm(0));
1439 ld
->subOp
= NV50_IR_SUBOP_PIXLD_SAMPLEID
;
1440 bld
.mkOp2(OP_SHL
, TYPE_U32
, off
, i
->getDef(0), bld
.mkImm(3));
1441 bld
.mkLoad(TYPE_F32
,
1444 FILE_MEMORY_CONST
, prog
->driver
->io
.resInfoCBSlot
,
1445 TYPE_U32
, prog
->driver
->io
.sampleInfoBase
+
1446 4 * sym
->reg
.data
.sv
.index
),
1450 case SV_SAMPLE_MASK
:
1451 ld
= bld
.mkOp1(OP_PIXLD
, TYPE_U32
, i
->getDef(0), bld
.mkImm(0));
1452 ld
->subOp
= NV50_IR_SUBOP_PIXLD_COVMASK
;
1455 if (prog
->getType() == Program::TYPE_TESSELLATION_EVAL
)
1456 vtx
= bld
.mkOp1v(OP_PFETCH
, TYPE_U32
, bld
.getSSA(), bld
.mkImm(0));
1457 ld
= bld
.mkFetch(i
->getDef(0), i
->dType
,
1458 FILE_SHADER_INPUT
, addr
, i
->getIndirect(0, 0), vtx
);
1459 ld
->perPatch
= i
->perPatch
;
1462 bld
.getBB()->remove(i
);
1467 NVC0LoweringPass::handleDIV(Instruction
*i
)
1469 if (!isFloatType(i
->dType
))
1471 bld
.setPosition(i
, false);
1472 Instruction
*rcp
= bld
.mkOp1(OP_RCP
, i
->dType
, bld
.getSSA(), i
->getSrc(1));
1474 i
->setSrc(1, rcp
->getDef(0));
1479 NVC0LoweringPass::handleMOD(Instruction
*i
)
1481 if (i
->dType
!= TYPE_F32
)
1483 LValue
*value
= bld
.getScratch();
1484 bld
.mkOp1(OP_RCP
, TYPE_F32
, value
, i
->getSrc(1));
1485 bld
.mkOp2(OP_MUL
, TYPE_F32
, value
, i
->getSrc(0), value
);
1486 bld
.mkOp1(OP_TRUNC
, TYPE_F32
, value
, value
);
1487 bld
.mkOp2(OP_MUL
, TYPE_F32
, value
, i
->getSrc(1), value
);
1489 i
->setSrc(1, value
);
1494 NVC0LoweringPass::handleSQRT(Instruction
*i
)
1496 Instruction
*rsq
= bld
.mkOp1(OP_RSQ
, TYPE_F32
,
1497 bld
.getSSA(), i
->getSrc(0));
1499 i
->setSrc(1, rsq
->getDef(0));
1505 NVC0LoweringPass::handlePOW(Instruction
*i
)
1507 LValue
*val
= bld
.getScratch();
1509 bld
.mkOp1(OP_LG2
, TYPE_F32
, val
, i
->getSrc(0));
1510 bld
.mkOp2(OP_MUL
, TYPE_F32
, val
, i
->getSrc(1), val
)->dnz
= 1;
1511 bld
.mkOp1(OP_PREEX2
, TYPE_F32
, val
, val
);
1521 NVC0LoweringPass::handleEXPORT(Instruction
*i
)
1523 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
1524 int id
= i
->getSrc(0)->reg
.data
.offset
/ 4;
1526 if (i
->src(0).isIndirect(0)) // TODO, ugly
1529 i
->subOp
= NV50_IR_SUBOP_MOV_FINAL
;
1530 i
->src(0).set(i
->src(1));
1532 i
->setDef(0, new_LValue(func
, FILE_GPR
));
1533 i
->getDef(0)->reg
.data
.id
= id
;
1535 prog
->maxGPR
= MAX2(prog
->maxGPR
, id
);
1537 if (prog
->getType() == Program::TYPE_GEOMETRY
) {
1538 i
->setIndirect(0, 1, gpEmitAddress
);
1544 NVC0LoweringPass::handleOUT(Instruction
*i
)
1546 if (i
->op
== OP_RESTART
&& i
->prev
&& i
->prev
->op
== OP_EMIT
) {
1547 i
->prev
->subOp
= NV50_IR_SUBOP_EMIT_RESTART
;
1548 delete_Instruction(prog
, i
);
1550 assert(gpEmitAddress
);
1551 i
->setDef(0, gpEmitAddress
);
1552 if (i
->srcExists(0))
1553 i
->setSrc(1, i
->getSrc(0));
1554 i
->setSrc(0, gpEmitAddress
);
1559 // Generate a binary predicate if an instruction is predicated by
1560 // e.g. an f32 value.
1562 NVC0LoweringPass::checkPredicate(Instruction
*insn
)
1564 Value
*pred
= insn
->getPredicate();
1567 if (!pred
|| pred
->reg
.file
== FILE_PREDICATE
)
1569 pdst
= new_LValue(func
, FILE_PREDICATE
);
1571 // CAUTION: don't use pdst->getInsn, the definition might not be unique,
1572 // delay turning PSET(FSET(x,y),0) into PSET(x,y) to a later pass
1574 bld
.mkCmp(OP_SET
, CC_NEU
, insn
->dType
, pdst
, insn
->dType
, bld
.mkImm(0), pred
);
1576 insn
->setPredicate(insn
->cc
, pdst
);
1580 // - add quadop dance for texturing
1581 // - put FP outputs in GPRs
1582 // - convert instruction sequences
1585 NVC0LoweringPass::visit(Instruction
*i
)
1587 bld
.setPosition(i
, false);
1589 if (i
->cc
!= CC_ALWAYS
)
1598 return handleTEX(i
->asTex());
1600 return handleTXD(i
->asTex());
1602 return handleTXLQ(i
->asTex());
1604 return handleTXQ(i
->asTex());
1606 bld
.mkOp1(OP_PREEX2
, TYPE_F32
, i
->getDef(0), i
->getSrc(0));
1607 i
->setSrc(0, i
->getDef(0));
1610 return handlePOW(i
);
1612 return handleDIV(i
);
1614 return handleMOD(i
);
1616 return handleSQRT(i
);
1618 return handleEXPORT(i
);
1621 return handleOUT(i
);
1623 return handleRDSV(i
);
1625 return handleWRSV(i
);
1627 if (i
->src(0).getFile() == FILE_SHADER_INPUT
) {
1628 if (prog
->getType() == Program::TYPE_COMPUTE
) {
1629 i
->getSrc(0)->reg
.file
= FILE_MEMORY_CONST
;
1630 i
->getSrc(0)->reg
.fileIndex
= 0;
1632 if (prog
->getType() == Program::TYPE_GEOMETRY
&&
1633 i
->src(0).isIndirect(0)) {
1634 // XXX: this assumes vec4 units
1635 Value
*ptr
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(),
1636 i
->getIndirect(0, 0), bld
.mkImm(4));
1637 i
->setIndirect(0, 0, ptr
);
1640 assert(prog
->getType() != Program::TYPE_FRAGMENT
); // INTERP
1646 const bool cctl
= i
->src(0).getFile() == FILE_MEMORY_GLOBAL
;
1648 handleCasExch(i
, cctl
);
1657 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1658 handleSurfaceOpNVE4(i
->asTex());
1667 TargetNVC0::runLegalizePass(Program
*prog
, CGStage stage
) const
1669 if (stage
== CG_STAGE_PRE_SSA
) {
1670 NVC0LoweringPass
pass(prog
);
1671 return pass
.run(prog
, false, true);
1673 if (stage
== CG_STAGE_POST_RA
) {
1674 NVC0LegalizePostRA
pass(prog
);
1675 return pass
.run(prog
, false, true);
1677 if (stage
== CG_STAGE_SSA
) {
1678 NVC0LegalizeSSA pass
;
1679 return pass
.run(prog
, false, true);
1684 } // namespace nv50_ir