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"
27 #include "codegen/nv50_ir_lowering_nvc0.h"
39 #define QUADOP(q, r, s, t) \
40 ((QOP_##q << 6) | (QOP_##r << 4) | \
41 (QOP_##s << 2) | (QOP_##t << 0))
44 NVC0LegalizeSSA::handleDIV(Instruction
*i
)
46 FlowInstruction
*call
;
50 bld
.setPosition(i
, false);
51 def
[0] = bld
.mkMovToReg(0, i
->getSrc(0))->getDef(0);
52 def
[1] = bld
.mkMovToReg(1, i
->getSrc(1))->getDef(0);
54 case TYPE_U32
: builtin
= NVC0_BUILTIN_DIV_U32
; break;
55 case TYPE_S32
: builtin
= NVC0_BUILTIN_DIV_S32
; break;
59 call
= bld
.mkFlow(OP_CALL
, NULL
, CC_ALWAYS
, NULL
);
60 bld
.mkMov(i
->getDef(0), def
[(i
->op
== OP_DIV
) ? 0 : 1]);
61 bld
.mkClobber(FILE_GPR
, (i
->op
== OP_DIV
) ? 0xe : 0xd, 2);
62 bld
.mkClobber(FILE_PREDICATE
, (i
->dType
== TYPE_S32
) ? 0xf : 0x3, 0);
65 call
->absolute
= call
->builtin
= 1;
66 call
->target
.builtin
= builtin
;
67 delete_Instruction(prog
, i
);
71 NVC0LegalizeSSA::handleRCPRSQ(Instruction
*i
)
77 NVC0LegalizeSSA::visit(Function
*fn
)
79 bld
.setProgram(fn
->getProgram());
84 NVC0LegalizeSSA::visit(BasicBlock
*bb
)
87 for (Instruction
*i
= bb
->getEntry(); i
; i
= next
) {
89 if (i
->dType
== TYPE_F32
)
98 if (i
->dType
== TYPE_F64
)
108 NVC0LegalizePostRA::NVC0LegalizePostRA(const Program
*prog
)
111 needTexBar(prog
->getTarget()->getChipset() >= 0xe0)
116 NVC0LegalizePostRA::insnDominatedBy(const Instruction
*later
,
117 const Instruction
*early
) const
119 if (early
->bb
== later
->bb
)
120 return early
->serial
< later
->serial
;
121 return later
->bb
->dominatedBy(early
->bb
);
125 NVC0LegalizePostRA::addTexUse(std::list
<TexUse
> &uses
,
126 Instruction
*usei
, const Instruction
*insn
)
129 for (std::list
<TexUse
>::iterator it
= uses
.begin();
131 if (insnDominatedBy(usei
, it
->insn
)) {
135 if (insnDominatedBy(it
->insn
, usei
))
141 uses
.push_back(TexUse(usei
, insn
));
145 NVC0LegalizePostRA::findOverwritingDefs(const Instruction
*texi
,
147 const BasicBlock
*term
,
148 std::list
<TexUse
> &uses
)
150 while (insn
->op
== OP_MOV
&& insn
->getDef(0)->equals(insn
->getSrc(0)))
151 insn
= insn
->getSrc(0)->getUniqueInsn();
153 if (!insn
|| !insn
->bb
->reachableBy(texi
->bb
, term
))
157 /* Values not connected to the tex's definition through any of these should
158 * not be conflicting.
165 for (int s
= 0; insn
->srcExists(s
); ++s
)
166 findOverwritingDefs(texi
, insn
->getSrc(s
)->getUniqueInsn(), term
,
170 // if (!isTextureOp(insn->op)) // TODO: are TEXes always ordered ?
171 addTexUse(uses
, insn
, texi
);
177 NVC0LegalizePostRA::findFirstUses(const Instruction
*texi
,
178 const Instruction
*insn
,
179 std::list
<TexUse
> &uses
)
181 for (int d
= 0; insn
->defExists(d
); ++d
) {
182 Value
*v
= insn
->getDef(d
);
183 for (Value::UseIterator u
= v
->uses
.begin(); u
!= v
->uses
.end(); ++u
) {
184 Instruction
*usei
= (*u
)->getInsn();
186 if (usei
->op
== OP_PHI
|| usei
->op
== OP_UNION
) {
187 // need a barrier before WAW cases
188 for (int s
= 0; usei
->srcExists(s
); ++s
) {
189 Instruction
*defi
= usei
->getSrc(s
)->getUniqueInsn();
190 if (defi
&& &usei
->src(s
) != *u
)
191 findOverwritingDefs(texi
, defi
, usei
->bb
, uses
);
195 if (usei
->op
== OP_SPLIT
||
196 usei
->op
== OP_MERGE
||
197 usei
->op
== OP_PHI
||
198 usei
->op
== OP_UNION
) {
199 // these uses don't manifest in the machine code
200 findFirstUses(texi
, usei
, uses
);
202 if (usei
->op
== OP_MOV
&& usei
->getDef(0)->equals(usei
->getSrc(0)) &&
203 usei
->subOp
!= NV50_IR_SUBOP_MOV_FINAL
) {
204 findFirstUses(texi
, usei
, uses
);
206 addTexUse(uses
, usei
, insn
);
213 // This pass is a bit long and ugly and can probably be optimized.
215 // 1. obtain a list of TEXes and their outputs' first use(s)
216 // 2. calculate the barrier level of each first use (minimal number of TEXes,
217 // over all paths, between the TEX and the use in question)
218 // 3. for each barrier, if all paths from the source TEX to that barrier
219 // contain a barrier of lesser level, it can be culled
221 NVC0LegalizePostRA::insertTextureBarriers(Function
*fn
)
223 std::list
<TexUse
> *uses
;
224 std::vector
<Instruction
*> texes
;
225 std::vector
<int> bbFirstTex
;
226 std::vector
<int> bbFirstUse
;
227 std::vector
<int> texCounts
;
228 std::vector
<TexUse
> useVec
;
231 fn
->orderInstructions(insns
);
233 texCounts
.resize(fn
->allBBlocks
.getSize(), 0);
234 bbFirstTex
.resize(fn
->allBBlocks
.getSize(), insns
.getSize());
235 bbFirstUse
.resize(fn
->allBBlocks
.getSize(), insns
.getSize());
237 // tag BB CFG nodes by their id for later
238 for (ArrayList::Iterator i
= fn
->allBBlocks
.iterator(); !i
.end(); i
.next()) {
239 BasicBlock
*bb
= reinterpret_cast<BasicBlock
*>(i
.get());
241 bb
->cfg
.tag
= bb
->getId();
244 // gather the first uses for each TEX
245 for (int i
= 0; i
< insns
.getSize(); ++i
) {
246 Instruction
*tex
= reinterpret_cast<Instruction
*>(insns
.get(i
));
247 if (isTextureOp(tex
->op
)) {
248 texes
.push_back(tex
);
249 if (!texCounts
.at(tex
->bb
->getId()))
250 bbFirstTex
[tex
->bb
->getId()] = texes
.size() - 1;
251 texCounts
[tex
->bb
->getId()]++;
257 uses
= new std::list
<TexUse
>[texes
.size()];
260 for (size_t i
= 0; i
< texes
.size(); ++i
)
261 findFirstUses(texes
[i
], texes
[i
], uses
[i
]);
263 // determine the barrier level at each use
264 for (size_t i
= 0; i
< texes
.size(); ++i
) {
265 for (std::list
<TexUse
>::iterator u
= uses
[i
].begin(); u
!= uses
[i
].end();
267 BasicBlock
*tb
= texes
[i
]->bb
;
268 BasicBlock
*ub
= u
->insn
->bb
;
271 for (size_t j
= i
+ 1; j
< texes
.size() &&
272 texes
[j
]->bb
== tb
&& texes
[j
]->serial
< u
->insn
->serial
;
276 u
->level
= fn
->cfg
.findLightestPathWeight(&tb
->cfg
,
277 &ub
->cfg
, texCounts
);
279 WARN("Failed to find path TEX -> TEXBAR\n");
283 // this counted all TEXes in the origin block, correct that
284 u
->level
-= i
- bbFirstTex
.at(tb
->getId()) + 1 /* this TEX */;
285 // and did not count the TEXes in the destination block, add those
286 for (size_t j
= bbFirstTex
.at(ub
->getId()); j
< texes
.size() &&
287 texes
[j
]->bb
== ub
&& texes
[j
]->serial
< u
->insn
->serial
;
291 assert(u
->level
>= 0);
292 useVec
.push_back(*u
);
298 // insert the barriers
299 for (size_t i
= 0; i
< useVec
.size(); ++i
) {
300 Instruction
*prev
= useVec
[i
].insn
->prev
;
301 if (useVec
[i
].level
< 0)
303 if (prev
&& prev
->op
== OP_TEXBAR
) {
304 if (prev
->subOp
> useVec
[i
].level
)
305 prev
->subOp
= useVec
[i
].level
;
306 prev
->setSrc(prev
->srcCount(), useVec
[i
].tex
->getDef(0));
308 Instruction
*bar
= new_Instruction(func
, OP_TEXBAR
, TYPE_NONE
);
310 bar
->subOp
= useVec
[i
].level
;
311 // make use explicit to ease latency calculation
312 bar
->setSrc(bar
->srcCount(), useVec
[i
].tex
->getDef(0));
313 useVec
[i
].insn
->bb
->insertBefore(useVec
[i
].insn
, bar
);
317 if (fn
->getProgram()->optLevel
< 3) {
323 std::vector
<Limits
> limitT
, limitB
, limitS
; // entry, exit, single
325 limitT
.resize(fn
->allBBlocks
.getSize(), Limits(0, 0));
326 limitB
.resize(fn
->allBBlocks
.getSize(), Limits(0, 0));
327 limitS
.resize(fn
->allBBlocks
.getSize());
329 // cull unneeded barriers (should do that earlier, but for simplicity)
330 IteratorRef bi
= fn
->cfg
.iteratorCFG();
331 // first calculate min/max outstanding TEXes for each BB
332 for (bi
->reset(); !bi
->end(); bi
->next()) {
333 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(bi
->get());
334 BasicBlock
*bb
= BasicBlock::get(n
);
336 int max
= std::numeric_limits
<int>::max();
337 for (Instruction
*i
= bb
->getFirst(); i
; i
= i
->next
) {
338 if (isTextureOp(i
->op
)) {
340 if (max
< std::numeric_limits
<int>::max())
343 if (i
->op
== OP_TEXBAR
) {
344 min
= MIN2(min
, i
->subOp
);
345 max
= MIN2(max
, i
->subOp
);
348 // limits when looking at an isolated block
349 limitS
[bb
->getId()].min
= min
;
350 limitS
[bb
->getId()].max
= max
;
352 // propagate the min/max values
353 for (unsigned int l
= 0; l
<= fn
->loopNestingBound
; ++l
) {
354 for (bi
->reset(); !bi
->end(); bi
->next()) {
355 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(bi
->get());
356 BasicBlock
*bb
= BasicBlock::get(n
);
357 const int bbId
= bb
->getId();
358 for (Graph::EdgeIterator ei
= n
->incident(); !ei
.end(); ei
.next()) {
359 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
360 const int inId
= in
->getId();
361 limitT
[bbId
].min
= MAX2(limitT
[bbId
].min
, limitB
[inId
].min
);
362 limitT
[bbId
].max
= MAX2(limitT
[bbId
].max
, limitB
[inId
].max
);
364 // I just hope this is correct ...
365 if (limitS
[bbId
].max
== std::numeric_limits
<int>::max()) {
367 limitB
[bbId
].min
= limitT
[bbId
].min
+ limitS
[bbId
].min
;
368 limitB
[bbId
].max
= limitT
[bbId
].max
+ limitS
[bbId
].min
;
370 // block contained a barrier
371 limitB
[bbId
].min
= MIN2(limitS
[bbId
].max
,
372 limitT
[bbId
].min
+ limitS
[bbId
].min
);
373 limitB
[bbId
].max
= MIN2(limitS
[bbId
].max
,
374 limitT
[bbId
].max
+ limitS
[bbId
].min
);
378 // finally delete unnecessary barriers
379 for (bi
->reset(); !bi
->end(); bi
->next()) {
380 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(bi
->get());
381 BasicBlock
*bb
= BasicBlock::get(n
);
382 Instruction
*prev
= NULL
;
384 int max
= limitT
[bb
->getId()].max
;
385 for (Instruction
*i
= bb
->getFirst(); i
; i
= next
) {
387 if (i
->op
== OP_TEXBAR
) {
388 if (i
->subOp
>= max
) {
389 delete_Instruction(prog
, i
);
393 if (prev
&& prev
->op
== OP_TEXBAR
&& prev
->subOp
>= max
) {
394 delete_Instruction(prog
, prev
);
399 if (isTextureOp(i
->op
)) {
402 if (i
&& !i
->isNop())
412 NVC0LegalizePostRA::visit(Function
*fn
)
415 insertTextureBarriers(fn
);
417 rZero
= new_LValue(fn
, FILE_GPR
);
418 carry
= new_LValue(fn
, FILE_FLAGS
);
420 rZero
->reg
.data
.id
= prog
->getTarget()->getFileSize(FILE_GPR
);
421 carry
->reg
.data
.id
= 0;
427 NVC0LegalizePostRA::replaceZero(Instruction
*i
)
429 for (int s
= 0; i
->srcExists(s
); ++s
) {
430 if (s
== 2 && i
->op
== OP_SUCLAMP
)
432 ImmediateValue
*imm
= i
->getSrc(s
)->asImm();
433 if (imm
&& imm
->reg
.data
.u64
== 0)
438 // replace CONT with BRA for single unconditional continue
440 NVC0LegalizePostRA::tryReplaceContWithBra(BasicBlock
*bb
)
442 if (bb
->cfg
.incidentCount() != 2 || bb
->getEntry()->op
!= OP_PRECONT
)
444 Graph::EdgeIterator ei
= bb
->cfg
.incident();
445 if (ei
.getType() != Graph::Edge::BACK
)
447 if (ei
.getType() != Graph::Edge::BACK
)
449 BasicBlock
*contBB
= BasicBlock::get(ei
.getNode());
451 if (!contBB
->getExit() || contBB
->getExit()->op
!= OP_CONT
||
452 contBB
->getExit()->getPredicate())
454 contBB
->getExit()->op
= OP_BRA
;
455 bb
->remove(bb
->getEntry()); // delete PRECONT
458 assert(ei
.end() || ei
.getType() != Graph::Edge::BACK
);
462 // replace branches to join blocks with join ops
464 NVC0LegalizePostRA::propagateJoin(BasicBlock
*bb
)
466 if (bb
->getEntry()->op
!= OP_JOIN
|| bb
->getEntry()->asFlow()->limit
)
468 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
469 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
470 Instruction
*exit
= in
->getExit();
472 in
->insertTail(new FlowInstruction(func
, OP_JOIN
, bb
));
473 // there should always be a terminator instruction
474 WARN("inserted missing terminator in BB:%i\n", in
->getId());
476 if (exit
->op
== OP_BRA
) {
478 exit
->asFlow()->limit
= 1; // must-not-propagate marker
481 bb
->remove(bb
->getEntry());
485 NVC0LegalizePostRA::visit(BasicBlock
*bb
)
487 Instruction
*i
, *next
;
489 // remove pseudo operations and non-fixed no-ops, split 64 bit operations
490 for (i
= bb
->getFirst(); i
; i
= next
) {
492 if (i
->op
== OP_EMIT
|| i
->op
== OP_RESTART
) {
493 if (!i
->getDef(0)->refCount())
495 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
496 i
->setSrc(0, rZero
); // initial value must be 0
502 // TODO: Move this to before register allocation for operations that
503 // need the $c register !
504 if (typeSizeof(i
->dType
) == 8) {
506 hi
= BuildUtil::split64BitOpPostRA(func
, i
, rZero
, carry
);
511 if (i
->op
!= OP_MOV
&& i
->op
!= OP_PFETCH
)
518 if (!tryReplaceContWithBra(bb
))
524 NVC0LoweringPass::NVC0LoweringPass(Program
*prog
) : targ(prog
->getTarget())
526 bld
.setProgram(prog
);
531 NVC0LoweringPass::visit(Function
*fn
)
533 if (prog
->getType() == Program::TYPE_GEOMETRY
) {
534 assert(!strncmp(fn
->getName(), "MAIN", 4));
535 // TODO: when we generate actual functions pass this value along somehow
536 bld
.setPosition(BasicBlock::get(fn
->cfg
.getRoot()), false);
537 gpEmitAddress
= bld
.loadImm(NULL
, 0)->asLValue();
539 bld
.setPosition(BasicBlock::get(fn
->cfgExit
)->getExit(), false);
540 bld
.mkMovToReg(0, gpEmitAddress
);
547 NVC0LoweringPass::visit(BasicBlock
*bb
)
553 NVC0LoweringPass::loadTexHandle(Value
*ptr
, unsigned int slot
)
555 uint8_t b
= prog
->driver
->io
.resInfoCBSlot
;
556 uint32_t off
= prog
->driver
->io
.texBindBase
+ slot
* 4;
558 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U32
, off
), ptr
);
561 // move array source to first slot, convert to u16, add indirections
563 NVC0LoweringPass::handleTEX(TexInstruction
*i
)
565 const int dim
= i
->tex
.target
.getDim() + i
->tex
.target
.isCube();
566 const int arg
= i
->tex
.target
.getArgCount();
567 const int lyr
= arg
- (i
->tex
.target
.isMS() ? 2 : 1);
568 const int chipset
= prog
->getTarget()->getChipset();
570 if (chipset
>= NVISA_GK104_CHIPSET
) {
571 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0) {
572 WARN("indirect TEX not implemented\n");
574 if (i
->tex
.r
== i
->tex
.s
) {
575 i
->tex
.r
+= prog
->driver
->io
.texBindBase
/ 4;
576 i
->tex
.s
= 0; // only a single cX[] value possible here
578 Value
*hnd
= bld
.getScratch();
579 Value
*rHnd
= loadTexHandle(NULL
, i
->tex
.r
);
580 Value
*sHnd
= loadTexHandle(NULL
, i
->tex
.s
);
582 bld
.mkOp3(OP_INSBF
, TYPE_U32
, hnd
, rHnd
, bld
.mkImm(0x1400), sHnd
);
584 i
->tex
.r
= 0; // not used for indirect tex
586 i
->setIndirectR(hnd
);
588 if (i
->tex
.target
.isArray()) {
589 LValue
*layer
= new_LValue(func
, FILE_GPR
);
590 Value
*src
= i
->getSrc(lyr
);
591 const int sat
= (i
->op
== OP_TXF
) ? 1 : 0;
592 DataType sTy
= (i
->op
== OP_TXF
) ? TYPE_U32
: TYPE_F32
;
593 bld
.mkCvt(OP_CVT
, TYPE_U16
, layer
, sTy
, src
)->saturate
= sat
;
594 for (int s
= dim
; s
>= 1; --s
)
595 i
->setSrc(s
, i
->getSrc(s
- 1));
599 // (nvc0) generate and move the tsc/tic/array source to the front
600 if (i
->tex
.target
.isArray() || i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0) {
601 LValue
*src
= new_LValue(func
, FILE_GPR
); // 0xttxsaaaa
603 Value
*ticRel
= i
->getIndirectR();
604 Value
*tscRel
= i
->getIndirectS();
607 i
->setSrc(i
->tex
.rIndirectSrc
, NULL
);
609 i
->setSrc(i
->tex
.sIndirectSrc
, NULL
);
611 Value
*arrayIndex
= i
->tex
.target
.isArray() ? i
->getSrc(lyr
) : NULL
;
612 for (int s
= dim
; s
>= 1; --s
)
613 i
->setSrc(s
, i
->getSrc(s
- 1));
614 i
->setSrc(0, arrayIndex
);
617 int sat
= (i
->op
== OP_TXF
) ? 1 : 0;
618 DataType sTy
= (i
->op
== OP_TXF
) ? TYPE_U32
: TYPE_F32
;
619 bld
.mkCvt(OP_CVT
, TYPE_U16
, src
, sTy
, arrayIndex
)->saturate
= sat
;
625 bld
.mkOp3(OP_INSBF
, TYPE_U32
, src
, ticRel
, bld
.mkImm(0x0917), src
);
627 bld
.mkOp3(OP_INSBF
, TYPE_U32
, src
, tscRel
, bld
.mkImm(0x0710), src
);
632 // For nvc0, the sample id has to be in the second operand, as the offset
633 // does. Right now we don't know how to pass both in, and this case can't
634 // happen with OpenGL. On nve0, the sample id is part of the texture
635 // coordinate argument.
636 assert(chipset
>= NVISA_GK104_CHIPSET
||
637 !i
->tex
.useOffsets
|| !i
->tex
.target
.isMS());
639 // offset is between lod and dc
640 if (i
->tex
.useOffsets
) {
642 int s
= i
->srcCount(0xff, true);
643 if (i
->op
!= OP_TXD
|| chipset
< NVISA_GK104_CHIPSET
) {
644 if (i
->tex
.target
.isShadow())
646 if (i
->srcExists(s
)) // move potential predicate out of the way
647 i
->moveSources(s
, 1);
648 if (i
->tex
.useOffsets
== 4 && i
->srcExists(s
+ 1))
649 i
->moveSources(s
+ 1, 1);
651 if (i
->op
== OP_TXG
) {
652 // Either there is 1 offset, which goes into the 2 low bytes of the
653 // first source, or there are 4 offsets, which go into 2 sources (8
654 // values, 1 byte each).
655 Value
*offs
[2] = {NULL
, NULL
};
656 for (n
= 0; n
< i
->tex
.useOffsets
; n
++) {
657 for (c
= 0; c
< 2; ++c
) {
658 if ((n
% 2) == 0 && c
== 0)
659 offs
[n
/ 2] = i
->offset
[n
][c
].get();
661 bld
.mkOp3(OP_INSBF
, TYPE_U32
,
663 i
->offset
[n
][c
].get(),
664 bld
.mkImm(0x800 | ((n
* 16 + c
* 8) % 32)),
668 i
->setSrc(s
, offs
[0]);
670 i
->setSrc(s
+ 1, offs
[1]);
673 assert(i
->tex
.useOffsets
== 1);
674 for (c
= 0; c
< 3; ++c
) {
676 assert(i
->offset
[0][c
].getImmediate(val
));
677 imm
|= (val
.reg
.data
.u32
& 0xf) << (c
* 4);
679 if (i
->op
== OP_TXD
&& chipset
>= NVISA_GK104_CHIPSET
) {
680 // The offset goes into the upper 16 bits of the array index. So
681 // create it if it's not already there, and INSBF it if it already
683 if (i
->tex
.target
.isArray()) {
684 bld
.mkOp3(OP_INSBF
, TYPE_U32
, i
->getSrc(0),
685 bld
.loadImm(NULL
, imm
), bld
.mkImm(0xc10),
688 for (int s
= dim
; s
>= 1; --s
)
689 i
->setSrc(s
, i
->getSrc(s
- 1));
690 i
->setSrc(0, bld
.loadImm(NULL
, imm
<< 16));
693 i
->setSrc(s
, bld
.loadImm(NULL
, imm
));
698 if (chipset
>= NVISA_GK104_CHIPSET
) {
700 // If TEX requires more than 4 sources, the 2nd register tuple must be
701 // aligned to 4, even if it consists of just a single 4-byte register.
703 // XXX HACK: We insert 0 sources to avoid the 5 or 6 regs case.
705 int s
= i
->srcCount(0xff, true);
706 if (s
> 4 && s
< 7) {
707 if (i
->srcExists(s
)) // move potential predicate out of the way
708 i
->moveSources(s
, 7 - s
);
710 i
->setSrc(s
++, bld
.loadImm(NULL
, 0));
718 NVC0LoweringPass::handleManualTXD(TexInstruction
*i
)
720 static const uint8_t qOps
[4][2] =
722 { QUADOP(MOV2
, ADD
, MOV2
, ADD
), QUADOP(MOV2
, MOV2
, ADD
, ADD
) }, // l0
723 { QUADOP(SUBR
, MOV2
, SUBR
, MOV2
), QUADOP(MOV2
, MOV2
, ADD
, ADD
) }, // l1
724 { QUADOP(MOV2
, ADD
, MOV2
, ADD
), QUADOP(SUBR
, SUBR
, MOV2
, MOV2
) }, // l2
725 { QUADOP(SUBR
, MOV2
, SUBR
, MOV2
), QUADOP(SUBR
, SUBR
, MOV2
, MOV2
) }, // l3
730 Value
*zero
= bld
.loadImm(bld
.getSSA(), 0);
732 const int dim
= i
->tex
.target
.getDim();
733 const int array
= i
->tex
.target
.isArray();
735 i
->op
= OP_TEX
; // no need to clone dPdx/dPdy later
737 for (c
= 0; c
< dim
; ++c
)
738 crd
[c
] = bld
.getScratch();
740 bld
.mkOp(OP_QUADON
, TYPE_NONE
, NULL
);
741 for (l
= 0; l
< 4; ++l
) {
742 // mov coordinates from lane l to all lanes
743 for (c
= 0; c
< dim
; ++c
)
744 bld
.mkQuadop(0x00, crd
[c
], l
, i
->getSrc(c
+ array
), zero
);
745 // add dPdx from lane l to lanes dx
746 for (c
= 0; c
< dim
; ++c
)
747 bld
.mkQuadop(qOps
[l
][0], crd
[c
], l
, i
->dPdx
[c
].get(), crd
[c
]);
748 // add dPdy from lane l to lanes dy
749 for (c
= 0; c
< dim
; ++c
)
750 bld
.mkQuadop(qOps
[l
][1], crd
[c
], l
, i
->dPdy
[c
].get(), crd
[c
]);
752 bld
.insert(tex
= cloneForward(func
, i
));
753 for (c
= 0; c
< dim
; ++c
)
754 tex
->setSrc(c
+ array
, crd
[c
]);
756 for (c
= 0; i
->defExists(c
); ++c
) {
758 def
[c
][l
] = bld
.getSSA();
759 mov
= bld
.mkMov(def
[c
][l
], tex
->getDef(c
));
764 bld
.mkOp(OP_QUADPOP
, TYPE_NONE
, NULL
);
766 for (c
= 0; i
->defExists(c
); ++c
) {
767 Instruction
*u
= bld
.mkOp(OP_UNION
, TYPE_U32
, i
->getDef(c
));
768 for (l
= 0; l
< 4; ++l
)
769 u
->setSrc(l
, def
[c
][l
]);
777 NVC0LoweringPass::handleTXD(TexInstruction
*txd
)
779 int dim
= txd
->tex
.target
.getDim();
780 unsigned arg
= txd
->tex
.target
.getArgCount();
781 unsigned expected_args
= arg
;
782 const int chipset
= prog
->getTarget()->getChipset();
784 if (chipset
>= NVISA_GK104_CHIPSET
) {
785 if (!txd
->tex
.target
.isArray() && txd
->tex
.useOffsets
)
788 if (txd
->tex
.useOffsets
)
790 if (!txd
->tex
.target
.isArray() && (
791 txd
->tex
.rIndirectSrc
>= 0 || txd
->tex
.sIndirectSrc
>= 0))
795 if (expected_args
> 4 ||
797 txd
->tex
.target
.isShadow() ||
798 txd
->tex
.target
.isCube())
802 while (txd
->srcExists(arg
))
805 txd
->tex
.derivAll
= true;
806 if (txd
->op
== OP_TEX
)
807 return handleManualTXD(txd
);
809 assert(arg
== expected_args
);
810 for (int c
= 0; c
< dim
; ++c
) {
811 txd
->setSrc(arg
+ c
* 2 + 0, txd
->dPdx
[c
]);
812 txd
->setSrc(arg
+ c
* 2 + 1, txd
->dPdy
[c
]);
813 txd
->dPdx
[c
].set(NULL
);
814 txd
->dPdy
[c
].set(NULL
);
820 NVC0LoweringPass::handleTXQ(TexInstruction
*txq
)
822 // TODO: indirect resource/sampler index
827 NVC0LoweringPass::handleTXLQ(TexInstruction
*i
)
829 /* The outputs are inverted compared to what the TGSI instruction
830 * expects. Take that into account in the mask.
832 assert((i
->tex
.mask
& ~3) == 0);
833 if (i
->tex
.mask
== 1)
835 else if (i
->tex
.mask
== 2)
838 bld
.setPosition(i
, true);
840 /* The returned values are not quite what we want:
841 * (a) convert from s16/u16 to f32
842 * (b) multiply by 1/256
844 for (int def
= 0; def
< 2; ++def
) {
845 if (!i
->defExists(def
))
847 enum DataType type
= TYPE_S16
;
848 if (i
->tex
.mask
== 2 || def
> 0)
850 bld
.mkCvt(OP_CVT
, TYPE_F32
, i
->getDef(def
), type
, i
->getDef(def
));
851 bld
.mkOp2(OP_MUL
, TYPE_F32
, i
->getDef(def
),
852 i
->getDef(def
), bld
.loadImm(NULL
, 1.0f
/ 256));
854 if (i
->tex
.mask
== 3) {
855 LValue
*t
= new_LValue(func
, FILE_GPR
);
856 bld
.mkMov(t
, i
->getDef(0));
857 bld
.mkMov(i
->getDef(0), i
->getDef(1));
858 bld
.mkMov(i
->getDef(1), t
);
865 NVC0LoweringPass::handleATOM(Instruction
*atom
)
869 switch (atom
->src(0).getFile()) {
870 case FILE_MEMORY_LOCAL
:
873 case FILE_MEMORY_SHARED
:
877 assert(atom
->src(0).getFile() == FILE_MEMORY_GLOBAL
);
881 bld
.mkOp1v(OP_RDSV
, TYPE_U32
, bld
.getScratch(), bld
.mkSysVal(sv
, 0));
882 Value
*ptr
= atom
->getIndirect(0, 0);
884 atom
->setSrc(0, cloneShallow(func
, atom
->getSrc(0)));
885 atom
->getSrc(0)->reg
.file
= FILE_MEMORY_GLOBAL
;
887 base
= bld
.mkOp2v(OP_ADD
, TYPE_U32
, base
, base
, ptr
);
888 atom
->setIndirect(0, 0, base
);
894 NVC0LoweringPass::handleCasExch(Instruction
*cas
, bool needCctl
)
896 if (cas
->subOp
!= NV50_IR_SUBOP_ATOM_CAS
&&
897 cas
->subOp
!= NV50_IR_SUBOP_ATOM_EXCH
)
899 bld
.setPosition(cas
, true);
902 Instruction
*cctl
= bld
.mkOp1(OP_CCTL
, TYPE_NONE
, NULL
, cas
->getSrc(0));
903 cctl
->setIndirect(0, 0, cas
->getIndirect(0, 0));
905 cctl
->subOp
= NV50_IR_SUBOP_CCTL_IV
;
906 if (cas
->isPredicated())
907 cctl
->setPredicate(cas
->cc
, cas
->getPredicate());
910 if (cas
->defExists(0) && cas
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
911 // CAS is crazy. It's 2nd source is a double reg, and the 3rd source
912 // should be set to the high part of the double reg or bad things will
913 // happen elsewhere in the universe.
914 // Also, it sometimes returns the new value instead of the old one
915 // under mysterious circumstances.
916 Value
*dreg
= bld
.getSSA(8);
917 bld
.setPosition(cas
, false);
918 bld
.mkOp2(OP_MERGE
, TYPE_U64
, dreg
, cas
->getSrc(1), cas
->getSrc(2));
919 cas
->setSrc(1, dreg
);
926 NVC0LoweringPass::loadResInfo32(Value
*ptr
, uint32_t off
)
928 uint8_t b
= prog
->driver
->io
.resInfoCBSlot
;
929 off
+= prog
->driver
->io
.suInfoBase
;
931 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U32
, off
), ptr
);
935 NVC0LoweringPass::loadMsInfo32(Value
*ptr
, uint32_t off
)
937 uint8_t b
= prog
->driver
->io
.msInfoCBSlot
;
938 off
+= prog
->driver
->io
.msInfoBase
;
940 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U32
, off
), ptr
);
943 /* On nvc0, surface info is obtained via the surface binding points passed
944 * to the SULD/SUST instructions.
945 * On nve4, surface info is stored in c[] and is used by various special
946 * instructions, e.g. for clamping coordiantes or generating an address.
947 * They couldn't just have added an equivalent to TIC now, couldn't they ?
949 #define NVE4_SU_INFO_ADDR 0x00
950 #define NVE4_SU_INFO_FMT 0x04
951 #define NVE4_SU_INFO_DIM_X 0x08
952 #define NVE4_SU_INFO_PITCH 0x0c
953 #define NVE4_SU_INFO_DIM_Y 0x10
954 #define NVE4_SU_INFO_ARRAY 0x14
955 #define NVE4_SU_INFO_DIM_Z 0x18
956 #define NVE4_SU_INFO_UNK1C 0x1c
957 #define NVE4_SU_INFO_WIDTH 0x20
958 #define NVE4_SU_INFO_HEIGHT 0x24
959 #define NVE4_SU_INFO_DEPTH 0x28
960 #define NVE4_SU_INFO_TARGET 0x2c
961 #define NVE4_SU_INFO_CALL 0x30
962 #define NVE4_SU_INFO_RAW_X 0x34
963 #define NVE4_SU_INFO_MS_X 0x38
964 #define NVE4_SU_INFO_MS_Y 0x3c
966 #define NVE4_SU_INFO__STRIDE 0x40
968 #define NVE4_SU_INFO_DIM(i) (0x08 + (i) * 8)
969 #define NVE4_SU_INFO_SIZE(i) (0x20 + (i) * 4)
970 #define NVE4_SU_INFO_MS(i) (0x38 + (i) * 4)
972 static inline uint16_t getSuClampSubOp(const TexInstruction
*su
, int c
)
974 switch (su
->tex
.target
.getEnum()) {
975 case TEX_TARGET_BUFFER
: return NV50_IR_SUBOP_SUCLAMP_PL(0, 1);
976 case TEX_TARGET_RECT
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
977 case TEX_TARGET_1D
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
978 case TEX_TARGET_1D_ARRAY
: return (c
== 1) ?
979 NV50_IR_SUBOP_SUCLAMP_PL(0, 2) :
980 NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
981 case TEX_TARGET_2D
: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
982 case TEX_TARGET_2D_MS
: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
983 case TEX_TARGET_2D_ARRAY
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
984 case TEX_TARGET_2D_MS_ARRAY
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
985 case TEX_TARGET_3D
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
986 case TEX_TARGET_CUBE
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
987 case TEX_TARGET_CUBE_ARRAY
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
995 NVC0LoweringPass::adjustCoordinatesMS(TexInstruction
*tex
)
997 const uint16_t base
= tex
->tex
.r
* NVE4_SU_INFO__STRIDE
;
998 const int arg
= tex
->tex
.target
.getArgCount();
1000 if (tex
->tex
.target
== TEX_TARGET_2D_MS
)
1001 tex
->tex
.target
= TEX_TARGET_2D
;
1003 if (tex
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1004 tex
->tex
.target
= TEX_TARGET_2D_ARRAY
;
1008 Value
*x
= tex
->getSrc(0);
1009 Value
*y
= tex
->getSrc(1);
1010 Value
*s
= tex
->getSrc(arg
- 1);
1012 Value
*tx
= bld
.getSSA(), *ty
= bld
.getSSA(), *ts
= bld
.getSSA();
1014 Value
*ms_x
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_MS(0));
1015 Value
*ms_y
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_MS(1));
1017 bld
.mkOp2(OP_SHL
, TYPE_U32
, tx
, x
, ms_x
);
1018 bld
.mkOp2(OP_SHL
, TYPE_U32
, ty
, y
, ms_y
);
1020 s
= bld
.mkOp2v(OP_AND
, TYPE_U32
, ts
, s
, bld
.loadImm(NULL
, 0x7));
1021 s
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, ts
, ts
, bld
.mkImm(3));
1023 Value
*dx
= loadMsInfo32(ts
, 0x0);
1024 Value
*dy
= loadMsInfo32(ts
, 0x4);
1026 bld
.mkOp2(OP_ADD
, TYPE_U32
, tx
, tx
, dx
);
1027 bld
.mkOp2(OP_ADD
, TYPE_U32
, ty
, ty
, dy
);
1031 tex
->moveSources(arg
, -1);
1034 // Sets 64-bit "generic address", predicate and format sources for SULD/SUST.
1035 // They're computed from the coordinates using the surface info in c[] space.
1037 NVC0LoweringPass::processSurfaceCoordsNVE4(TexInstruction
*su
)
1040 const bool atom
= su
->op
== OP_SUREDB
|| su
->op
== OP_SUREDP
;
1042 su
->op
== OP_SULDB
|| su
->op
== OP_SUSTB
|| su
->op
== OP_SUREDB
;
1043 const int idx
= su
->tex
.r
;
1044 const int dim
= su
->tex
.target
.getDim();
1045 const int arg
= dim
+ (su
->tex
.target
.isArray() ? 1 : 0);
1046 const uint16_t base
= idx
* NVE4_SU_INFO__STRIDE
;
1048 Value
*zero
= bld
.mkImm(0);
1052 Value
*bf
, *eau
, *off
;
1055 off
= bld
.getScratch(4);
1056 bf
= bld
.getScratch(4);
1057 addr
= bld
.getSSA(8);
1058 pred
= bld
.getScratch(1, FILE_PREDICATE
);
1060 bld
.setPosition(su
, false);
1062 adjustCoordinatesMS(su
);
1064 // calculate clamped coordinates
1065 for (c
= 0; c
< arg
; ++c
) {
1066 src
[c
] = bld
.getScratch();
1068 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_RAW_X
);
1070 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_DIM(c
));
1071 bld
.mkOp3(OP_SUCLAMP
, TYPE_S32
, src
[c
], su
->getSrc(c
), v
, zero
)
1072 ->subOp
= getSuClampSubOp(su
, c
);
1077 // set predicate output
1078 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1079 src
[0]->getInsn()->setFlagsDef(1, pred
);
1081 if (su
->tex
.target
.isArray()) {
1082 p1
= bld
.getSSA(1, FILE_PREDICATE
);
1083 src
[dim
]->getInsn()->setFlagsDef(1, p1
);
1086 // calculate pixel offset
1088 if (su
->tex
.target
!= TEX_TARGET_BUFFER
)
1089 bld
.mkOp2(OP_AND
, TYPE_U32
, off
, src
[0], bld
.loadImm(NULL
, 0xffff));
1092 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_UNK1C
);
1093 bld
.mkOp3(OP_MADSP
, TYPE_U32
, off
, src
[2], v
, src
[1])
1094 ->subOp
= NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1096 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_PITCH
);
1097 bld
.mkOp3(OP_MADSP
, TYPE_U32
, off
, off
, v
, src
[0])
1098 ->subOp
= NV50_IR_SUBOP_MADSP(0,2,8); // u32 u16l u16l
1101 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_PITCH
);
1102 bld
.mkOp3(OP_MADSP
, TYPE_U32
, off
, src
[1], v
, src
[0])
1103 ->subOp
= su
->tex
.target
.isArray() ?
1104 NV50_IR_SUBOP_MADSP_SD
: NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1107 // calculate effective address part 1
1108 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1112 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_FMT
);
1113 bld
.mkOp3(OP_VSHL
, TYPE_U32
, bf
, src
[0], v
, zero
)
1114 ->subOp
= NV50_IR_SUBOP_V1(7,6,8|2);
1128 if (!su
->tex
.target
.isArray()) {
1129 z
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_UNK1C
);
1130 subOp
= NV50_IR_SUBOP_SUBFM_3D
;
1134 subOp
= NV50_IR_SUBOP_SUBFM_3D
;
1138 insn
= bld
.mkOp3(OP_SUBFM
, TYPE_U32
, bf
, src
[0], y
, z
);
1139 insn
->subOp
= subOp
;
1140 insn
->setFlagsDef(1, pred
);
1144 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_ADDR
);
1146 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1149 eau
= bld
.mkOp3v(OP_SUEAU
, TYPE_U32
, bld
.getScratch(4), off
, bf
, v
);
1151 // add array layer offset
1152 if (su
->tex
.target
.isArray()) {
1153 v
= loadResInfo32(NULL
, base
+ NVE4_SU_INFO_ARRAY
);
1155 bld
.mkOp3(OP_MADSP
, TYPE_U32
, eau
, src
[1], v
, eau
)
1156 ->subOp
= NV50_IR_SUBOP_MADSP(4,0,0); // u16 u24 u32
1158 bld
.mkOp3(OP_MADSP
, TYPE_U32
, eau
, v
, src
[2], eau
)
1159 ->subOp
= NV50_IR_SUBOP_MADSP(0,0,0); // u32 u24 u32
1160 // combine predicates
1162 bld
.mkOp2(OP_OR
, TYPE_U8
, pred
, pred
, p1
);
1167 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1171 // bf == g[] address & 0xff
1172 // eau == g[] address >> 8
1173 bld
.mkOp3(OP_PERMT
, TYPE_U32
, bf
, lo
, bld
.loadImm(NULL
, 0x6540), eau
);
1174 bld
.mkOp3(OP_PERMT
, TYPE_U32
, eau
, zero
, bld
.loadImm(NULL
, 0x0007), eau
);
1176 if (su
->op
== OP_SULDP
&& su
->tex
.target
== TEX_TARGET_BUFFER
) {
1177 // Convert from u32 to u8 address format, which is what the library code
1178 // doing SULDP currently uses.
1179 // XXX: can SUEAU do this ?
1180 // XXX: does it matter that we don't mask high bytes in bf ?
1182 bld
.mkOp2(OP_SHR
, TYPE_U32
, off
, bf
, bld
.mkImm(8));
1183 bld
.mkOp2(OP_ADD
, TYPE_U32
, eau
, eau
, off
);
1186 bld
.mkOp2(OP_MERGE
, TYPE_U64
, addr
, bf
, eau
);
1188 if (atom
&& su
->tex
.target
== TEX_TARGET_BUFFER
)
1189 bld
.mkOp2(OP_ADD
, TYPE_U64
, addr
, addr
, off
);
1191 // let's just set it 0 for raw access and hope it works
1193 bld
.mkImm(0) : loadResInfo32(NULL
, base
+ NVE4_SU_INFO_FMT
);
1195 // get rid of old coordinate sources, make space for fmt info and predicate
1196 su
->moveSources(arg
, 3 - arg
);
1197 // set 64 bit address and 32-bit format sources
1198 su
->setSrc(0, addr
);
1200 su
->setSrc(2, pred
);
1204 NVC0LoweringPass::handleSurfaceOpNVE4(TexInstruction
*su
)
1206 processSurfaceCoordsNVE4(su
);
1208 // Who do we hate more ? The person who decided that nvc0's SULD doesn't
1209 // have to support conversion or the person who decided that, in OpenCL,
1210 // you don't have to specify the format here like you do in OpenGL ?
1212 if (su
->op
== OP_SULDP
) {
1213 // We don't patch shaders. Ever.
1214 // You get an indirect call to our library blob here.
1215 // But at least it's uniform.
1216 FlowInstruction
*call
;
1219 uint16_t base
= su
->tex
.r
* NVE4_SU_INFO__STRIDE
+ NVE4_SU_INFO_CALL
;
1221 for (int i
= 0; i
< 4; ++i
)
1222 (r
[i
] = bld
.getScratch(4, FILE_GPR
))->reg
.data
.id
= i
;
1223 for (int i
= 0; i
< 3; ++i
)
1224 (p
[i
] = bld
.getScratch(1, FILE_PREDICATE
))->reg
.data
.id
= i
;
1225 (r
[4] = bld
.getScratch(8, FILE_GPR
))->reg
.data
.id
= 4;
1227 bld
.mkMov(p
[1], bld
.mkImm((su
->cache
== CACHE_CA
) ? 1 : 0), TYPE_U8
);
1228 bld
.mkMov(p
[2], bld
.mkImm((su
->cache
== CACHE_CG
) ? 1 : 0), TYPE_U8
);
1229 bld
.mkMov(p
[0], su
->getSrc(2), TYPE_U8
);
1230 bld
.mkMov(r
[4], su
->getSrc(0), TYPE_U64
);
1231 bld
.mkMov(r
[2], su
->getSrc(1), TYPE_U32
);
1233 call
= bld
.mkFlow(OP_CALL
, NULL
, su
->cc
, su
->getPredicate());
1237 call
->setSrc(0, bld
.mkSymbol(FILE_MEMORY_CONST
,
1238 prog
->driver
->io
.resInfoCBSlot
, TYPE_U32
,
1239 prog
->driver
->io
.suInfoBase
+ base
));
1240 call
->setSrc(1, r
[2]);
1241 call
->setSrc(2, r
[4]);
1242 for (int i
= 0; i
< 3; ++i
)
1243 call
->setSrc(3 + i
, p
[i
]);
1244 for (int i
= 0; i
< 4; ++i
) {
1245 call
->setDef(i
, r
[i
]);
1246 bld
.mkMov(su
->getDef(i
), r
[i
]);
1248 call
->setDef(4, p
[1]);
1249 delete_Instruction(bld
.getProgram(), su
);
1252 if (su
->op
== OP_SUREDB
|| su
->op
== OP_SUREDP
) {
1253 // FIXME: for out of bounds access, destination value will be undefined !
1254 Value
*pred
= su
->getSrc(2);
1255 CondCode cc
= CC_NOT_P
;
1256 if (su
->getPredicate()) {
1257 pred
= bld
.getScratch(1, FILE_PREDICATE
);
1259 if (cc
== CC_NOT_P
) {
1260 bld
.mkOp2(OP_OR
, TYPE_U8
, pred
, su
->getPredicate(), su
->getSrc(2));
1262 bld
.mkOp2(OP_AND
, TYPE_U8
, pred
, su
->getPredicate(), su
->getSrc(2));
1263 pred
->getInsn()->src(1).mod
= Modifier(NV50_IR_MOD_NOT
);
1266 Instruction
*red
= bld
.mkOp(OP_ATOM
, su
->dType
, su
->getDef(0));
1267 red
->subOp
= su
->subOp
;
1269 gMemBase
= bld
.mkSymbol(FILE_MEMORY_GLOBAL
, 0, TYPE_U32
, 0);
1270 red
->setSrc(0, gMemBase
);
1271 red
->setSrc(1, su
->getSrc(3));
1272 if (su
->subOp
== NV50_IR_SUBOP_ATOM_CAS
)
1273 red
->setSrc(2, su
->getSrc(4));
1274 red
->setIndirect(0, 0, su
->getSrc(0));
1275 red
->setPredicate(cc
, pred
);
1276 delete_Instruction(bld
.getProgram(), su
);
1277 handleCasExch(red
, true);
1279 su
->sType
= (su
->tex
.target
== TEX_TARGET_BUFFER
) ? TYPE_U32
: TYPE_U8
;
1284 NVC0LoweringPass::handleWRSV(Instruction
*i
)
1290 // must replace, $sreg are not writeable
1291 addr
= targ
->getSVAddress(FILE_SHADER_OUTPUT
, i
->getSrc(0)->asSym());
1294 sym
= bld
.mkSymbol(FILE_SHADER_OUTPUT
, 0, i
->sType
, addr
);
1296 st
= bld
.mkStore(OP_EXPORT
, i
->dType
, sym
, i
->getIndirect(0, 0),
1298 st
->perPatch
= i
->perPatch
;
1300 bld
.getBB()->remove(i
);
1305 NVC0LoweringPass::readTessCoord(LValue
*dst
, int c
)
1307 Value
*laneid
= bld
.getSSA();
1310 bld
.mkOp1(OP_RDSV
, TYPE_U32
, laneid
, bld
.mkSysVal(SV_LANEID
, 0));
1325 bld
.mkFetch(x
, TYPE_F32
, FILE_SHADER_OUTPUT
, 0x2f0, NULL
, laneid
);
1327 bld
.mkFetch(y
, TYPE_F32
, FILE_SHADER_OUTPUT
, 0x2f4, NULL
, laneid
);
1330 bld
.mkOp2(OP_ADD
, TYPE_F32
, dst
, x
, y
);
1331 bld
.mkOp2(OP_SUB
, TYPE_F32
, dst
, bld
.loadImm(NULL
, 1.0f
), dst
);
1336 NVC0LoweringPass::handleRDSV(Instruction
*i
)
1338 Symbol
*sym
= i
->getSrc(0)->asSym();
1339 const SVSemantic sv
= sym
->reg
.data
.sv
.sv
;
1342 uint32_t addr
= targ
->getSVAddress(FILE_SHADER_INPUT
, sym
);
1344 if (addr
>= 0x400) {
1346 if (sym
->reg
.data
.sv
.index
== 3) {
1347 // TGSI backend may use 4th component of TID,NTID,CTAID,NCTAID
1349 i
->setSrc(0, bld
.mkImm((sv
== SV_NTID
|| sv
== SV_NCTAID
) ? 1 : 0));
1356 assert(prog
->getType() == Program::TYPE_FRAGMENT
);
1357 if (i
->srcExists(1)) {
1358 // Pass offset through to the interpolation logic
1359 ld
= bld
.mkInterp(NV50_IR_INTERP_LINEAR
| NV50_IR_INTERP_OFFSET
,
1360 i
->getDef(0), addr
, NULL
);
1361 ld
->setSrc(1, i
->getSrc(1));
1363 bld
.mkInterp(NV50_IR_INTERP_LINEAR
, i
->getDef(0), addr
, NULL
);
1368 Value
*face
= i
->getDef(0);
1369 bld
.mkInterp(NV50_IR_INTERP_FLAT
, face
, addr
, NULL
);
1370 if (i
->dType
== TYPE_F32
) {
1371 bld
.mkOp2(OP_AND
, TYPE_U32
, face
, face
, bld
.mkImm(0x80000000));
1372 bld
.mkOp2(OP_XOR
, TYPE_U32
, face
, face
, bld
.mkImm(0xbf800000));
1377 assert(prog
->getType() == Program::TYPE_TESSELLATION_EVAL
);
1378 readTessCoord(i
->getDef(0)->asLValue(), i
->getSrc(0)->reg
.data
.sv
.index
);
1383 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
); // mov $sreg otherwise
1384 if (sym
->reg
.data
.sv
.index
== 3) {
1386 i
->setSrc(0, bld
.mkImm(sv
== SV_GRIDID
? 0 : 1));
1389 addr
+= prog
->driver
->prop
.cp
.gridInfoBase
;
1390 bld
.mkLoad(TYPE_U32
, i
->getDef(0),
1391 bld
.mkSymbol(FILE_MEMORY_CONST
, 0, TYPE_U32
, addr
), NULL
);
1393 case SV_SAMPLE_INDEX
:
1394 // TODO: Properly pass source as an address in the PIX address space
1395 // (which can be of the form [r0+offset]). But this is currently
1397 ld
= bld
.mkOp1(OP_PIXLD
, TYPE_U32
, i
->getDef(0), bld
.mkImm(0));
1398 ld
->subOp
= NV50_IR_SUBOP_PIXLD_SAMPLEID
;
1400 case SV_SAMPLE_POS
: {
1401 Value
*off
= new_LValue(func
, FILE_GPR
);
1402 ld
= bld
.mkOp1(OP_PIXLD
, TYPE_U32
, i
->getDef(0), bld
.mkImm(0));
1403 ld
->subOp
= NV50_IR_SUBOP_PIXLD_SAMPLEID
;
1404 bld
.mkOp2(OP_SHL
, TYPE_U32
, off
, i
->getDef(0), bld
.mkImm(3));
1405 bld
.mkLoad(TYPE_F32
,
1408 FILE_MEMORY_CONST
, prog
->driver
->io
.resInfoCBSlot
,
1409 TYPE_U32
, prog
->driver
->io
.sampleInfoBase
+
1410 4 * sym
->reg
.data
.sv
.index
),
1414 case SV_SAMPLE_MASK
:
1415 ld
= bld
.mkOp1(OP_PIXLD
, TYPE_U32
, i
->getDef(0), bld
.mkImm(0));
1416 ld
->subOp
= NV50_IR_SUBOP_PIXLD_COVMASK
;
1419 if (prog
->getType() == Program::TYPE_TESSELLATION_EVAL
)
1420 vtx
= bld
.mkOp1v(OP_PFETCH
, TYPE_U32
, bld
.getSSA(), bld
.mkImm(0));
1421 ld
= bld
.mkFetch(i
->getDef(0), i
->dType
,
1422 FILE_SHADER_INPUT
, addr
, i
->getIndirect(0, 0), vtx
);
1423 ld
->perPatch
= i
->perPatch
;
1426 bld
.getBB()->remove(i
);
1431 NVC0LoweringPass::handleDIV(Instruction
*i
)
1433 if (!isFloatType(i
->dType
))
1435 bld
.setPosition(i
, false);
1436 Instruction
*rcp
= bld
.mkOp1(OP_RCP
, i
->dType
, bld
.getSSA(), i
->getSrc(1));
1438 i
->setSrc(1, rcp
->getDef(0));
1443 NVC0LoweringPass::handleMOD(Instruction
*i
)
1445 if (i
->dType
!= TYPE_F32
)
1447 LValue
*value
= bld
.getScratch();
1448 bld
.mkOp1(OP_RCP
, TYPE_F32
, value
, i
->getSrc(1));
1449 bld
.mkOp2(OP_MUL
, TYPE_F32
, value
, i
->getSrc(0), value
);
1450 bld
.mkOp1(OP_TRUNC
, TYPE_F32
, value
, value
);
1451 bld
.mkOp2(OP_MUL
, TYPE_F32
, value
, i
->getSrc(1), value
);
1453 i
->setSrc(1, value
);
1458 NVC0LoweringPass::handleSQRT(Instruction
*i
)
1460 Instruction
*rsq
= bld
.mkOp1(OP_RSQ
, TYPE_F32
,
1461 bld
.getSSA(), i
->getSrc(0));
1463 i
->setSrc(1, rsq
->getDef(0));
1469 NVC0LoweringPass::handlePOW(Instruction
*i
)
1471 LValue
*val
= bld
.getScratch();
1473 bld
.mkOp1(OP_LG2
, TYPE_F32
, val
, i
->getSrc(0));
1474 bld
.mkOp2(OP_MUL
, TYPE_F32
, val
, i
->getSrc(1), val
)->dnz
= 1;
1475 bld
.mkOp1(OP_PREEX2
, TYPE_F32
, val
, val
);
1485 NVC0LoweringPass::handleEXPORT(Instruction
*i
)
1487 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
1488 int id
= i
->getSrc(0)->reg
.data
.offset
/ 4;
1490 if (i
->src(0).isIndirect(0)) // TODO, ugly
1493 i
->subOp
= NV50_IR_SUBOP_MOV_FINAL
;
1494 i
->src(0).set(i
->src(1));
1496 i
->setDef(0, new_LValue(func
, FILE_GPR
));
1497 i
->getDef(0)->reg
.data
.id
= id
;
1499 prog
->maxGPR
= MAX2(prog
->maxGPR
, id
);
1501 if (prog
->getType() == Program::TYPE_GEOMETRY
) {
1502 i
->setIndirect(0, 1, gpEmitAddress
);
1508 NVC0LoweringPass::handleOUT(Instruction
*i
)
1510 Instruction
*prev
= i
->prev
;
1511 ImmediateValue stream
, prevStream
;
1513 // Only merge if the stream ids match. Also, note that the previous
1514 // instruction would have already been lowered, so we take arg1 from it.
1515 if (i
->op
== OP_RESTART
&& prev
&& prev
->op
== OP_EMIT
&&
1516 i
->src(0).getImmediate(stream
) &&
1517 prev
->src(1).getImmediate(prevStream
) &&
1518 stream
.reg
.data
.u32
== prevStream
.reg
.data
.u32
) {
1519 i
->prev
->subOp
= NV50_IR_SUBOP_EMIT_RESTART
;
1520 delete_Instruction(prog
, i
);
1522 assert(gpEmitAddress
);
1523 i
->setDef(0, gpEmitAddress
);
1524 i
->setSrc(1, i
->getSrc(0));
1525 i
->setSrc(0, gpEmitAddress
);
1530 // Generate a binary predicate if an instruction is predicated by
1531 // e.g. an f32 value.
1533 NVC0LoweringPass::checkPredicate(Instruction
*insn
)
1535 Value
*pred
= insn
->getPredicate();
1538 if (!pred
|| pred
->reg
.file
== FILE_PREDICATE
)
1540 pdst
= new_LValue(func
, FILE_PREDICATE
);
1542 // CAUTION: don't use pdst->getInsn, the definition might not be unique,
1543 // delay turning PSET(FSET(x,y),0) into PSET(x,y) to a later pass
1545 bld
.mkCmp(OP_SET
, CC_NEU
, insn
->dType
, pdst
, insn
->dType
, bld
.mkImm(0), pred
);
1547 insn
->setPredicate(insn
->cc
, pdst
);
1551 // - add quadop dance for texturing
1552 // - put FP outputs in GPRs
1553 // - convert instruction sequences
1556 NVC0LoweringPass::visit(Instruction
*i
)
1558 bld
.setPosition(i
, false);
1560 if (i
->cc
!= CC_ALWAYS
)
1569 return handleTEX(i
->asTex());
1571 return handleTXD(i
->asTex());
1573 return handleTXLQ(i
->asTex());
1575 return handleTXQ(i
->asTex());
1577 bld
.mkOp1(OP_PREEX2
, TYPE_F32
, i
->getDef(0), i
->getSrc(0));
1578 i
->setSrc(0, i
->getDef(0));
1581 return handlePOW(i
);
1583 return handleDIV(i
);
1585 return handleMOD(i
);
1587 return handleSQRT(i
);
1589 return handleEXPORT(i
);
1592 return handleOUT(i
);
1594 return handleRDSV(i
);
1596 return handleWRSV(i
);
1598 if (i
->src(0).getFile() == FILE_SHADER_INPUT
) {
1599 if (prog
->getType() == Program::TYPE_COMPUTE
) {
1600 i
->getSrc(0)->reg
.file
= FILE_MEMORY_CONST
;
1601 i
->getSrc(0)->reg
.fileIndex
= 0;
1603 if (prog
->getType() == Program::TYPE_GEOMETRY
&&
1604 i
->src(0).isIndirect(0)) {
1605 // XXX: this assumes vec4 units
1606 Value
*ptr
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(),
1607 i
->getIndirect(0, 0), bld
.mkImm(4));
1608 i
->setIndirect(0, 0, ptr
);
1611 assert(prog
->getType() != Program::TYPE_FRAGMENT
); // INTERP
1613 } else if (i
->src(0).getFile() == FILE_MEMORY_CONST
) {
1614 if (i
->src(0).isIndirect(1)) {
1616 if (i
->src(0).isIndirect(0))
1617 ptr
= bld
.mkOp3v(OP_INSBF
, TYPE_U32
, bld
.getSSA(),
1618 i
->getIndirect(0, 1), bld
.mkImm(0x1010),
1619 i
->getIndirect(0, 0));
1621 ptr
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(),
1622 i
->getIndirect(0, 1), bld
.mkImm(16));
1623 i
->setIndirect(0, 1, NULL
);
1624 i
->setIndirect(0, 0, ptr
);
1625 i
->subOp
= NV50_IR_SUBOP_LDC_IS
;
1631 const bool cctl
= i
->src(0).getFile() == FILE_MEMORY_GLOBAL
;
1633 handleCasExch(i
, cctl
);
1642 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1643 handleSurfaceOpNVE4(i
->asTex());
1652 TargetNVC0::runLegalizePass(Program
*prog
, CGStage stage
) const
1654 if (stage
== CG_STAGE_PRE_SSA
) {
1655 NVC0LoweringPass
pass(prog
);
1656 return pass
.run(prog
, false, true);
1658 if (stage
== CG_STAGE_POST_RA
) {
1659 NVC0LegalizePostRA
pass(prog
);
1660 return pass
.run(prog
, false, true);
1662 if (stage
== CG_STAGE_SSA
) {
1663 NVC0LegalizeSSA pass
;
1664 return pass
.run(prog
, false, true);
1669 } // namespace nv50_ir