nvc0/ir: change textureGrad to always use lane 0 as the tex origin
[mesa.git] / src / gallium / drivers / nouveau / codegen / nv50_ir_lowering_nvc0.cpp
1 /*
2 * Copyright 2011 Christoph Bumiller
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 */
22
23 #include "codegen/nv50_ir.h"
24 #include "codegen/nv50_ir_build_util.h"
25
26 #include "codegen/nv50_ir_target_nvc0.h"
27 #include "codegen/nv50_ir_lowering_nvc0.h"
28
29 #include <limits>
30
31 namespace nv50_ir {
32
33 #define QOP_ADD 0
34 #define QOP_SUBR 1
35 #define QOP_SUB 2
36 #define QOP_MOV2 3
37
38 // UL UR LL LR
39 #define QUADOP(q, r, s, t) \
40 ((QOP_##q << 6) | (QOP_##r << 4) | \
41 (QOP_##s << 2) | (QOP_##t << 0))
42
43 void
44 NVC0LegalizeSSA::handleDIV(Instruction *i)
45 {
46 FlowInstruction *call;
47 int builtin;
48
49 bld.setPosition(i, false);
50
51 // Generate movs to the input regs for the call we want to generate
52 for (int s = 0; i->srcExists(s); ++s) {
53 Instruction *ld = i->getSrc(s)->getInsn();
54 assert(ld->getSrc(0) != NULL);
55 // check if we are moving an immediate, propagate it in that case
56 if (!ld || ld->fixed || (ld->op != OP_LOAD && ld->op != OP_MOV) ||
57 !(ld->src(0).getFile() == FILE_IMMEDIATE))
58 bld.mkMovToReg(s, i->getSrc(s));
59 else {
60 bld.mkMovToReg(s, ld->getSrc(0));
61 // Clear the src, to make code elimination possible here before we
62 // delete the instruction i later
63 i->setSrc(s, NULL);
64 if (ld->isDead())
65 delete_Instruction(prog, ld);
66 }
67 }
68
69 switch (i->dType) {
70 case TYPE_U32: builtin = NVC0_BUILTIN_DIV_U32; break;
71 case TYPE_S32: builtin = NVC0_BUILTIN_DIV_S32; break;
72 default:
73 return;
74 }
75 call = bld.mkFlow(OP_CALL, NULL, CC_ALWAYS, NULL);
76 bld.mkMovFromReg(i->getDef(0), i->op == OP_DIV ? 0 : 1);
77 bld.mkClobber(FILE_GPR, (i->op == OP_DIV) ? 0xe : 0xd, 2);
78 bld.mkClobber(FILE_PREDICATE, (i->dType == TYPE_S32) ? 0xf : 0x3, 0);
79
80 call->fixed = 1;
81 call->absolute = call->builtin = 1;
82 call->target.builtin = builtin;
83 delete_Instruction(prog, i);
84 }
85
86 void
87 NVC0LegalizeSSA::handleRCPRSQ(Instruction *i)
88 {
89 assert(i->dType == TYPE_F64);
90 // There are instructions that will compute the high 32 bits of the 64-bit
91 // float. We will just stick 0 in the bottom 32 bits.
92
93 bld.setPosition(i, false);
94
95 // 1. Take the source and it up.
96 Value *src[2], *dst[2], *def = i->getDef(0);
97 bld.mkSplit(src, 4, i->getSrc(0));
98
99 // 2. We don't care about the low 32 bits of the destination. Stick a 0 in.
100 dst[0] = bld.loadImm(NULL, 0);
101 dst[1] = bld.getSSA();
102
103 // 3. The new version of the instruction takes the high 32 bits of the
104 // source and outputs the high 32 bits of the destination.
105 i->setSrc(0, src[1]);
106 i->setDef(0, dst[1]);
107 i->setType(TYPE_F32);
108 i->subOp = NV50_IR_SUBOP_RCPRSQ_64H;
109
110 // 4. Recombine the two dst pieces back into the original destination.
111 bld.setPosition(i, true);
112 bld.mkOp2(OP_MERGE, TYPE_U64, def, dst[0], dst[1]);
113 }
114
115 void
116 NVC0LegalizeSSA::handleFTZ(Instruction *i)
117 {
118 // Only want to flush float inputs
119 assert(i->sType == TYPE_F32);
120
121 // If we're already flushing denorms (and NaN's) to zero, no need for this.
122 if (i->dnz)
123 return;
124
125 // Only certain classes of operations can flush
126 OpClass cls = prog->getTarget()->getOpClass(i->op);
127 if (cls != OPCLASS_ARITH && cls != OPCLASS_COMPARE &&
128 cls != OPCLASS_CONVERT)
129 return;
130
131 i->ftz = true;
132 }
133
134 void
135 NVC0LegalizeSSA::handleTEXLOD(TexInstruction *i)
136 {
137 if (i->tex.levelZero)
138 return;
139
140 ImmediateValue lod;
141
142 // The LOD argument comes right after the coordinates (before depth bias,
143 // offsets, etc).
144 int arg = i->tex.target.getArgCount();
145
146 // SM30+ stores the indirect handle as a separate arg, which comes before
147 // the LOD.
148 if (prog->getTarget()->getChipset() >= NVISA_GK104_CHIPSET &&
149 i->tex.rIndirectSrc >= 0)
150 arg++;
151 // SM20 stores indirect handle combined with array coordinate
152 if (prog->getTarget()->getChipset() < NVISA_GK104_CHIPSET &&
153 !i->tex.target.isArray() &&
154 i->tex.rIndirectSrc >= 0)
155 arg++;
156
157 if (!i->src(arg).getImmediate(lod) || !lod.isInteger(0))
158 return;
159
160 if (i->op == OP_TXL)
161 i->op = OP_TEX;
162 i->tex.levelZero = true;
163 i->moveSources(arg + 1, -1);
164 }
165
166 void
167 NVC0LegalizeSSA::handleShift(Instruction *lo)
168 {
169 Value *shift = lo->getSrc(1);
170 Value *dst64 = lo->getDef(0);
171 Value *src[2], *dst[2];
172 operation op = lo->op;
173
174 bld.setPosition(lo, false);
175
176 bld.mkSplit(src, 4, lo->getSrc(0));
177
178 // SM30 and prior don't have the fancy new SHF.L/R ops. So the logic has to
179 // be completely emulated. For SM35+, we can use the more directed SHF
180 // operations.
181 if (prog->getTarget()->getChipset() < NVISA_GK20A_CHIPSET) {
182 // The strategy here is to handle shifts >= 32 and less than 32 as
183 // separate parts.
184 //
185 // For SHL:
186 // If the shift is <= 32, then
187 // (HI,LO) << x = (HI << x | (LO >> (32 - x)), LO << x)
188 // If the shift is > 32, then
189 // (HI,LO) << x = (LO << (x - 32), 0)
190 //
191 // For SHR:
192 // If the shift is <= 32, then
193 // (HI,LO) >> x = (HI >> x, (HI << (32 - x)) | LO >> x)
194 // If the shift is > 32, then
195 // (HI,LO) >> x = (0, HI >> (x - 32))
196 //
197 // Note that on NVIDIA hardware, a shift > 32 yields a 0 value, which we
198 // can use to our advantage. Also note the structural similarities
199 // between the right/left cases. The main difference is swapping hi/lo
200 // on input and output.
201
202 Value *x32_minus_shift, *pred, *hi1, *hi2;
203 DataType type = isSignedIntType(lo->dType) ? TYPE_S32 : TYPE_U32;
204 operation antiop = op == OP_SHR ? OP_SHL : OP_SHR;
205 if (op == OP_SHR)
206 std::swap(src[0], src[1]);
207 bld.mkOp2(OP_ADD, TYPE_U32, (x32_minus_shift = bld.getSSA()), shift, bld.mkImm(0x20))
208 ->src(0).mod = Modifier(NV50_IR_MOD_NEG);
209 bld.mkCmp(OP_SET, CC_LE, TYPE_U8, (pred = bld.getSSA(1, FILE_PREDICATE)),
210 TYPE_U32, shift, bld.mkImm(32));
211 // Compute HI (shift <= 32)
212 bld.mkOp2(OP_OR, TYPE_U32, (hi1 = bld.getSSA()),
213 bld.mkOp2v(op, TYPE_U32, bld.getSSA(), src[1], shift),
214 bld.mkOp2v(antiop, TYPE_U32, bld.getSSA(), src[0], x32_minus_shift))
215 ->setPredicate(CC_P, pred);
216 // Compute LO (all shift values)
217 bld.mkOp2(op, type, (dst[0] = bld.getSSA()), src[0], shift);
218 // Compute HI (shift > 32)
219 bld.mkOp2(op, type, (hi2 = bld.getSSA()), src[0],
220 bld.mkOp1v(OP_NEG, TYPE_S32, bld.getSSA(), x32_minus_shift))
221 ->setPredicate(CC_NOT_P, pred);
222 bld.mkOp2(OP_UNION, TYPE_U32, (dst[1] = bld.getSSA()), hi1, hi2);
223 if (op == OP_SHR)
224 std::swap(dst[0], dst[1]);
225 bld.mkOp2(OP_MERGE, TYPE_U64, dst64, dst[0], dst[1]);
226 delete_Instruction(prog, lo);
227 return;
228 }
229
230 Instruction *hi = new_Instruction(func, op, TYPE_U32);
231 lo->bb->insertAfter(lo, hi);
232
233 hi->sType = lo->sType;
234 lo->dType = TYPE_U32;
235
236 hi->setDef(0, (dst[1] = bld.getSSA()));
237 if (lo->op == OP_SHR)
238 hi->subOp |= NV50_IR_SUBOP_SHIFT_HIGH;
239 lo->setDef(0, (dst[0] = bld.getSSA()));
240
241 bld.setPosition(hi, true);
242
243 if (lo->op == OP_SHL)
244 std::swap(hi, lo);
245
246 hi->setSrc(0, new_ImmediateValue(prog, 0u));
247 hi->setSrc(1, shift);
248 hi->setSrc(2, lo->op == OP_SHL ? src[0] : src[1]);
249
250 lo->setSrc(0, src[0]);
251 lo->setSrc(1, shift);
252 lo->setSrc(2, src[1]);
253
254 bld.mkOp2(OP_MERGE, TYPE_U64, dst64, dst[0], dst[1]);
255 }
256
257 void
258 NVC0LegalizeSSA::handleSET(CmpInstruction *cmp)
259 {
260 DataType hTy = cmp->sType == TYPE_S64 ? TYPE_S32 : TYPE_U32;
261 Value *carry;
262 Value *src0[2], *src1[2];
263 bld.setPosition(cmp, false);
264
265 bld.mkSplit(src0, 4, cmp->getSrc(0));
266 bld.mkSplit(src1, 4, cmp->getSrc(1));
267 bld.mkOp2(OP_SUB, hTy, NULL, src0[0], src1[0])
268 ->setFlagsDef(0, (carry = bld.getSSA(1, FILE_FLAGS)));
269 cmp->setFlagsSrc(cmp->srcCount(), carry);
270 cmp->setSrc(0, src0[1]);
271 cmp->setSrc(1, src1[1]);
272 cmp->sType = hTy;
273 }
274
275 bool
276 NVC0LegalizeSSA::visit(Function *fn)
277 {
278 bld.setProgram(fn->getProgram());
279 return true;
280 }
281
282 bool
283 NVC0LegalizeSSA::visit(BasicBlock *bb)
284 {
285 Instruction *next;
286 for (Instruction *i = bb->getEntry(); i; i = next) {
287 next = i->next;
288
289 if (i->sType == TYPE_F32 && prog->getType() != Program::TYPE_COMPUTE)
290 handleFTZ(i);
291
292 switch (i->op) {
293 case OP_DIV:
294 case OP_MOD:
295 if (i->sType != TYPE_F32)
296 handleDIV(i);
297 break;
298 case OP_RCP:
299 case OP_RSQ:
300 if (i->dType == TYPE_F64)
301 handleRCPRSQ(i);
302 break;
303 case OP_TXL:
304 case OP_TXF:
305 handleTEXLOD(i->asTex());
306 break;
307 case OP_SHR:
308 case OP_SHL:
309 if (typeSizeof(i->sType) == 8)
310 handleShift(i);
311 break;
312 case OP_SET:
313 case OP_SET_AND:
314 case OP_SET_OR:
315 case OP_SET_XOR:
316 if (typeSizeof(i->sType) == 8 && i->sType != TYPE_F64)
317 handleSET(i->asCmp());
318 break;
319 default:
320 break;
321 }
322 }
323 return true;
324 }
325
326 NVC0LegalizePostRA::NVC0LegalizePostRA(const Program *prog)
327 : rZero(NULL),
328 carry(NULL),
329 pOne(NULL),
330 needTexBar(prog->getTarget()->getChipset() >= 0xe0 &&
331 prog->getTarget()->getChipset() < 0x110)
332 {
333 }
334
335 bool
336 NVC0LegalizePostRA::insnDominatedBy(const Instruction *later,
337 const Instruction *early) const
338 {
339 if (early->bb == later->bb)
340 return early->serial < later->serial;
341 return later->bb->dominatedBy(early->bb);
342 }
343
344 void
345 NVC0LegalizePostRA::addTexUse(std::list<TexUse> &uses,
346 Instruction *usei, const Instruction *texi)
347 {
348 bool add = true;
349 bool dominated = insnDominatedBy(usei, texi);
350 // Uses before the tex have to all be included. Just because an earlier
351 // instruction dominates another instruction doesn't mean that there's no
352 // way to get from the tex to the later instruction. For example you could
353 // have nested loops, with the tex in the inner loop, and uses before it in
354 // both loops - even though the outer loop's instruction would dominate the
355 // inner's, we still want a texbar before the inner loop's instruction.
356 //
357 // However we can still use the eliding logic between uses dominated by the
358 // tex instruction, as that is unambiguously correct.
359 if (dominated) {
360 for (std::list<TexUse>::iterator it = uses.begin(); it != uses.end();) {
361 if (it->after) {
362 if (insnDominatedBy(usei, it->insn)) {
363 add = false;
364 break;
365 }
366 if (insnDominatedBy(it->insn, usei)) {
367 it = uses.erase(it);
368 continue;
369 }
370 }
371 ++it;
372 }
373 }
374 if (add)
375 uses.push_back(TexUse(usei, texi, dominated));
376 }
377
378 // While it might be tempting to use the an algorithm that just looks at tex
379 // uses, not all texture results are guaranteed to be used on all paths. In
380 // the case where along some control flow path a texture result is never used,
381 // we might reuse that register for something else, creating a
382 // write-after-write hazard. So we have to manually look through all
383 // instructions looking for ones that reference the registers in question.
384 void
385 NVC0LegalizePostRA::findFirstUses(
386 Instruction *texi, std::list<TexUse> &uses)
387 {
388 int minGPR = texi->def(0).rep()->reg.data.id;
389 int maxGPR = minGPR + texi->def(0).rep()->reg.size / 4 - 1;
390
391 unordered_set<const BasicBlock *> visited;
392 findFirstUsesBB(minGPR, maxGPR, texi->next, texi, uses, visited);
393 }
394
395 void
396 NVC0LegalizePostRA::findFirstUsesBB(
397 int minGPR, int maxGPR, Instruction *start,
398 const Instruction *texi, std::list<TexUse> &uses,
399 unordered_set<const BasicBlock *> &visited)
400 {
401 const BasicBlock *bb = start->bb;
402
403 // We don't process the whole bb the first time around. This is correct,
404 // however we might be in a loop and hit this BB again, and need to process
405 // the full thing. So only mark a bb as visited if we processed it from the
406 // beginning.
407 if (start == bb->getEntry()) {
408 if (visited.find(bb) != visited.end())
409 return;
410 visited.insert(bb);
411 }
412
413 for (Instruction *insn = start; insn != bb->getExit(); insn = insn->next) {
414 if (insn->isNop())
415 continue;
416
417 for (int d = 0; insn->defExists(d); ++d) {
418 const Value *def = insn->def(d).rep();
419 if (insn->def(d).getFile() != FILE_GPR ||
420 def->reg.data.id + def->reg.size / 4 - 1 < minGPR ||
421 def->reg.data.id > maxGPR)
422 continue;
423 addTexUse(uses, insn, texi);
424 return;
425 }
426
427 for (int s = 0; insn->srcExists(s); ++s) {
428 const Value *src = insn->src(s).rep();
429 if (insn->src(s).getFile() != FILE_GPR ||
430 src->reg.data.id + src->reg.size / 4 - 1 < minGPR ||
431 src->reg.data.id > maxGPR)
432 continue;
433 addTexUse(uses, insn, texi);
434 return;
435 }
436 }
437
438 for (Graph::EdgeIterator ei = bb->cfg.outgoing(); !ei.end(); ei.next()) {
439 findFirstUsesBB(minGPR, maxGPR, BasicBlock::get(ei.getNode())->getEntry(),
440 texi, uses, visited);
441 }
442 }
443
444 // Texture barriers:
445 // This pass is a bit long and ugly and can probably be optimized.
446 //
447 // 1. obtain a list of TEXes and their outputs' first use(s)
448 // 2. calculate the barrier level of each first use (minimal number of TEXes,
449 // over all paths, between the TEX and the use in question)
450 // 3. for each barrier, if all paths from the source TEX to that barrier
451 // contain a barrier of lesser level, it can be culled
452 bool
453 NVC0LegalizePostRA::insertTextureBarriers(Function *fn)
454 {
455 std::list<TexUse> *uses;
456 std::vector<Instruction *> texes;
457 std::vector<int> bbFirstTex;
458 std::vector<int> bbFirstUse;
459 std::vector<int> texCounts;
460 std::vector<TexUse> useVec;
461 ArrayList insns;
462
463 fn->orderInstructions(insns);
464
465 texCounts.resize(fn->allBBlocks.getSize(), 0);
466 bbFirstTex.resize(fn->allBBlocks.getSize(), insns.getSize());
467 bbFirstUse.resize(fn->allBBlocks.getSize(), insns.getSize());
468
469 // tag BB CFG nodes by their id for later
470 for (ArrayList::Iterator i = fn->allBBlocks.iterator(); !i.end(); i.next()) {
471 BasicBlock *bb = reinterpret_cast<BasicBlock *>(i.get());
472 if (bb)
473 bb->cfg.tag = bb->getId();
474 }
475
476 // gather the first uses for each TEX
477 for (int i = 0; i < insns.getSize(); ++i) {
478 Instruction *tex = reinterpret_cast<Instruction *>(insns.get(i));
479 if (isTextureOp(tex->op)) {
480 texes.push_back(tex);
481 if (!texCounts.at(tex->bb->getId()))
482 bbFirstTex[tex->bb->getId()] = texes.size() - 1;
483 texCounts[tex->bb->getId()]++;
484 }
485 }
486 insns.clear();
487 if (texes.empty())
488 return false;
489 uses = new std::list<TexUse>[texes.size()];
490 if (!uses)
491 return false;
492 for (size_t i = 0; i < texes.size(); ++i) {
493 findFirstUses(texes[i], uses[i]);
494 }
495
496 // determine the barrier level at each use
497 for (size_t i = 0; i < texes.size(); ++i) {
498 for (std::list<TexUse>::iterator u = uses[i].begin(); u != uses[i].end();
499 ++u) {
500 BasicBlock *tb = texes[i]->bb;
501 BasicBlock *ub = u->insn->bb;
502 if (tb == ub) {
503 u->level = 0;
504 for (size_t j = i + 1; j < texes.size() &&
505 texes[j]->bb == tb && texes[j]->serial < u->insn->serial;
506 ++j)
507 u->level++;
508 } else {
509 u->level = fn->cfg.findLightestPathWeight(&tb->cfg,
510 &ub->cfg, texCounts);
511 if (u->level < 0) {
512 WARN("Failed to find path TEX -> TEXBAR\n");
513 u->level = 0;
514 continue;
515 }
516 // this counted all TEXes in the origin block, correct that
517 u->level -= i - bbFirstTex.at(tb->getId()) + 1 /* this TEX */;
518 // and did not count the TEXes in the destination block, add those
519 for (size_t j = bbFirstTex.at(ub->getId()); j < texes.size() &&
520 texes[j]->bb == ub && texes[j]->serial < u->insn->serial;
521 ++j)
522 u->level++;
523 }
524 assert(u->level >= 0);
525 useVec.push_back(*u);
526 }
527 }
528 delete[] uses;
529
530 // insert the barriers
531 for (size_t i = 0; i < useVec.size(); ++i) {
532 Instruction *prev = useVec[i].insn->prev;
533 if (useVec[i].level < 0)
534 continue;
535 if (prev && prev->op == OP_TEXBAR) {
536 if (prev->subOp > useVec[i].level)
537 prev->subOp = useVec[i].level;
538 prev->setSrc(prev->srcCount(), useVec[i].tex->getDef(0));
539 } else {
540 Instruction *bar = new_Instruction(func, OP_TEXBAR, TYPE_NONE);
541 bar->fixed = 1;
542 bar->subOp = useVec[i].level;
543 // make use explicit to ease latency calculation
544 bar->setSrc(bar->srcCount(), useVec[i].tex->getDef(0));
545 useVec[i].insn->bb->insertBefore(useVec[i].insn, bar);
546 }
547 }
548
549 if (fn->getProgram()->optLevel < 3)
550 return true;
551
552 std::vector<Limits> limitT, limitB, limitS; // entry, exit, single
553
554 limitT.resize(fn->allBBlocks.getSize(), Limits(0, 0));
555 limitB.resize(fn->allBBlocks.getSize(), Limits(0, 0));
556 limitS.resize(fn->allBBlocks.getSize());
557
558 // cull unneeded barriers (should do that earlier, but for simplicity)
559 IteratorRef bi = fn->cfg.iteratorCFG();
560 // first calculate min/max outstanding TEXes for each BB
561 for (bi->reset(); !bi->end(); bi->next()) {
562 Graph::Node *n = reinterpret_cast<Graph::Node *>(bi->get());
563 BasicBlock *bb = BasicBlock::get(n);
564 int min = 0;
565 int max = std::numeric_limits<int>::max();
566 for (Instruction *i = bb->getFirst(); i; i = i->next) {
567 if (isTextureOp(i->op)) {
568 min++;
569 if (max < std::numeric_limits<int>::max())
570 max++;
571 } else
572 if (i->op == OP_TEXBAR) {
573 min = MIN2(min, i->subOp);
574 max = MIN2(max, i->subOp);
575 }
576 }
577 // limits when looking at an isolated block
578 limitS[bb->getId()].min = min;
579 limitS[bb->getId()].max = max;
580 }
581 // propagate the min/max values
582 for (unsigned int l = 0; l <= fn->loopNestingBound; ++l) {
583 for (bi->reset(); !bi->end(); bi->next()) {
584 Graph::Node *n = reinterpret_cast<Graph::Node *>(bi->get());
585 BasicBlock *bb = BasicBlock::get(n);
586 const int bbId = bb->getId();
587 for (Graph::EdgeIterator ei = n->incident(); !ei.end(); ei.next()) {
588 BasicBlock *in = BasicBlock::get(ei.getNode());
589 const int inId = in->getId();
590 limitT[bbId].min = MAX2(limitT[bbId].min, limitB[inId].min);
591 limitT[bbId].max = MAX2(limitT[bbId].max, limitB[inId].max);
592 }
593 // I just hope this is correct ...
594 if (limitS[bbId].max == std::numeric_limits<int>::max()) {
595 // no barrier
596 limitB[bbId].min = limitT[bbId].min + limitS[bbId].min;
597 limitB[bbId].max = limitT[bbId].max + limitS[bbId].min;
598 } else {
599 // block contained a barrier
600 limitB[bbId].min = MIN2(limitS[bbId].max,
601 limitT[bbId].min + limitS[bbId].min);
602 limitB[bbId].max = MIN2(limitS[bbId].max,
603 limitT[bbId].max + limitS[bbId].min);
604 }
605 }
606 }
607 // finally delete unnecessary barriers
608 for (bi->reset(); !bi->end(); bi->next()) {
609 Graph::Node *n = reinterpret_cast<Graph::Node *>(bi->get());
610 BasicBlock *bb = BasicBlock::get(n);
611 Instruction *prev = NULL;
612 Instruction *next;
613 int max = limitT[bb->getId()].max;
614 for (Instruction *i = bb->getFirst(); i; i = next) {
615 next = i->next;
616 if (i->op == OP_TEXBAR) {
617 if (i->subOp >= max) {
618 delete_Instruction(prog, i);
619 i = NULL;
620 } else {
621 max = i->subOp;
622 if (prev && prev->op == OP_TEXBAR && prev->subOp >= max) {
623 delete_Instruction(prog, prev);
624 prev = NULL;
625 }
626 }
627 } else
628 if (isTextureOp(i->op)) {
629 max++;
630 }
631 if (i && !i->isNop())
632 prev = i;
633 }
634 }
635 return true;
636 }
637
638 bool
639 NVC0LegalizePostRA::visit(Function *fn)
640 {
641 if (needTexBar)
642 insertTextureBarriers(fn);
643
644 rZero = new_LValue(fn, FILE_GPR);
645 pOne = new_LValue(fn, FILE_PREDICATE);
646 carry = new_LValue(fn, FILE_FLAGS);
647
648 rZero->reg.data.id = (prog->getTarget()->getChipset() >= NVISA_GK20A_CHIPSET) ? 255 : 63;
649 carry->reg.data.id = 0;
650 pOne->reg.data.id = 7;
651
652 return true;
653 }
654
655 void
656 NVC0LegalizePostRA::replaceZero(Instruction *i)
657 {
658 for (int s = 0; i->srcExists(s); ++s) {
659 if (s == 2 && i->op == OP_SUCLAMP)
660 continue;
661 if (s == 1 && i->op == OP_SHLADD)
662 continue;
663 ImmediateValue *imm = i->getSrc(s)->asImm();
664 if (imm) {
665 if (i->op == OP_SELP && s == 2) {
666 i->setSrc(s, pOne);
667 if (imm->reg.data.u64 == 0)
668 i->src(s).mod = i->src(s).mod ^ Modifier(NV50_IR_MOD_NOT);
669 } else if (imm->reg.data.u64 == 0) {
670 i->setSrc(s, rZero);
671 }
672 }
673 }
674 }
675
676 // replace CONT with BRA for single unconditional continue
677 bool
678 NVC0LegalizePostRA::tryReplaceContWithBra(BasicBlock *bb)
679 {
680 if (bb->cfg.incidentCount() != 2 || bb->getEntry()->op != OP_PRECONT)
681 return false;
682 Graph::EdgeIterator ei = bb->cfg.incident();
683 if (ei.getType() != Graph::Edge::BACK)
684 ei.next();
685 if (ei.getType() != Graph::Edge::BACK)
686 return false;
687 BasicBlock *contBB = BasicBlock::get(ei.getNode());
688
689 if (!contBB->getExit() || contBB->getExit()->op != OP_CONT ||
690 contBB->getExit()->getPredicate())
691 return false;
692 contBB->getExit()->op = OP_BRA;
693 bb->remove(bb->getEntry()); // delete PRECONT
694
695 ei.next();
696 assert(ei.end() || ei.getType() != Graph::Edge::BACK);
697 return true;
698 }
699
700 // replace branches to join blocks with join ops
701 void
702 NVC0LegalizePostRA::propagateJoin(BasicBlock *bb)
703 {
704 if (bb->getEntry()->op != OP_JOIN || bb->getEntry()->asFlow()->limit)
705 return;
706 for (Graph::EdgeIterator ei = bb->cfg.incident(); !ei.end(); ei.next()) {
707 BasicBlock *in = BasicBlock::get(ei.getNode());
708 Instruction *exit = in->getExit();
709 if (!exit) {
710 in->insertTail(new FlowInstruction(func, OP_JOIN, bb));
711 // there should always be a terminator instruction
712 WARN("inserted missing terminator in BB:%i\n", in->getId());
713 } else
714 if (exit->op == OP_BRA) {
715 exit->op = OP_JOIN;
716 exit->asFlow()->limit = 1; // must-not-propagate marker
717 }
718 }
719 bb->remove(bb->getEntry());
720 }
721
722 bool
723 NVC0LegalizePostRA::visit(BasicBlock *bb)
724 {
725 Instruction *i, *next;
726
727 // remove pseudo operations and non-fixed no-ops, split 64 bit operations
728 for (i = bb->getFirst(); i; i = next) {
729 next = i->next;
730 if (i->op == OP_EMIT || i->op == OP_RESTART) {
731 if (!i->getDef(0)->refCount())
732 i->setDef(0, NULL);
733 if (i->src(0).getFile() == FILE_IMMEDIATE)
734 i->setSrc(0, rZero); // initial value must be 0
735 replaceZero(i);
736 } else
737 if (i->isNop()) {
738 bb->remove(i);
739 } else
740 if (i->op == OP_BAR && i->subOp == NV50_IR_SUBOP_BAR_SYNC &&
741 prog->getType() != Program::TYPE_COMPUTE) {
742 // It seems like barriers are never required for tessellation since
743 // the warp size is 32, and there are always at most 32 tcs threads.
744 bb->remove(i);
745 } else
746 if (i->op == OP_LOAD && i->subOp == NV50_IR_SUBOP_LDC_IS) {
747 int offset = i->src(0).get()->reg.data.offset;
748 if (abs(offset) > 0x10000)
749 i->src(0).get()->reg.fileIndex += offset >> 16;
750 i->src(0).get()->reg.data.offset = (int)(short)offset;
751 } else {
752 // TODO: Move this to before register allocation for operations that
753 // need the $c register !
754 if (typeSizeof(i->sType) == 8 || typeSizeof(i->dType) == 8) {
755 Instruction *hi;
756 hi = BuildUtil::split64BitOpPostRA(func, i, rZero, carry);
757 if (hi)
758 next = hi;
759 }
760
761 if (i->op != OP_MOV && i->op != OP_PFETCH)
762 replaceZero(i);
763 }
764 }
765 if (!bb->getEntry())
766 return true;
767
768 if (!tryReplaceContWithBra(bb))
769 propagateJoin(bb);
770
771 return true;
772 }
773
774 NVC0LoweringPass::NVC0LoweringPass(Program *prog) : targ(prog->getTarget())
775 {
776 bld.setProgram(prog);
777 }
778
779 bool
780 NVC0LoweringPass::visit(Function *fn)
781 {
782 if (prog->getType() == Program::TYPE_GEOMETRY) {
783 assert(!strncmp(fn->getName(), "MAIN", 4));
784 // TODO: when we generate actual functions pass this value along somehow
785 bld.setPosition(BasicBlock::get(fn->cfg.getRoot()), false);
786 gpEmitAddress = bld.loadImm(NULL, 0)->asLValue();
787 if (fn->cfgExit) {
788 bld.setPosition(BasicBlock::get(fn->cfgExit)->getExit(), false);
789 bld.mkMovToReg(0, gpEmitAddress);
790 }
791 }
792 return true;
793 }
794
795 bool
796 NVC0LoweringPass::visit(BasicBlock *bb)
797 {
798 return true;
799 }
800
801 inline Value *
802 NVC0LoweringPass::loadTexHandle(Value *ptr, unsigned int slot)
803 {
804 uint8_t b = prog->driver->io.auxCBSlot;
805 uint32_t off = prog->driver->io.texBindBase + slot * 4;
806
807 if (ptr)
808 ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(), ptr, bld.mkImm(2));
809
810 return bld.
811 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
812 }
813
814 // move array source to first slot, convert to u16, add indirections
815 bool
816 NVC0LoweringPass::handleTEX(TexInstruction *i)
817 {
818 const int dim = i->tex.target.getDim() + i->tex.target.isCube();
819 const int arg = i->tex.target.getArgCount();
820 const int lyr = arg - (i->tex.target.isMS() ? 2 : 1);
821 const int chipset = prog->getTarget()->getChipset();
822
823 /* Only normalize in the non-explicit derivatives case. For explicit
824 * derivatives, this is handled in handleManualTXD.
825 */
826 if (i->tex.target.isCube() && i->dPdx[0].get() == NULL) {
827 Value *src[3], *val;
828 int c;
829 for (c = 0; c < 3; ++c)
830 src[c] = bld.mkOp1v(OP_ABS, TYPE_F32, bld.getSSA(), i->getSrc(c));
831 val = bld.getScratch();
832 bld.mkOp2(OP_MAX, TYPE_F32, val, src[0], src[1]);
833 bld.mkOp2(OP_MAX, TYPE_F32, val, src[2], val);
834 bld.mkOp1(OP_RCP, TYPE_F32, val, val);
835 for (c = 0; c < 3; ++c) {
836 i->setSrc(c, bld.mkOp2v(OP_MUL, TYPE_F32, bld.getSSA(),
837 i->getSrc(c), val));
838 }
839 }
840
841 // Arguments to the TEX instruction are a little insane. Even though the
842 // encoding is identical between SM20 and SM30, the arguments mean
843 // different things between Fermi and Kepler+. A lot of arguments are
844 // optional based on flags passed to the instruction. This summarizes the
845 // order of things.
846 //
847 // Fermi:
848 // array/indirect
849 // coords
850 // sample
851 // lod bias
852 // depth compare
853 // offsets:
854 // - tg4: 8 bits each, either 2 (1 offset reg) or 8 (2 offset reg)
855 // - other: 4 bits each, single reg
856 //
857 // Kepler+:
858 // indirect handle
859 // array (+ offsets for txd in upper 16 bits)
860 // coords
861 // sample
862 // lod bias
863 // depth compare
864 // offsets (same as fermi, except txd which takes it with array)
865 //
866 // Maxwell (tex):
867 // array
868 // coords
869 // indirect handle
870 // sample
871 // lod bias
872 // depth compare
873 // offsets
874 //
875 // Maxwell (txd):
876 // indirect handle
877 // coords
878 // array + offsets
879 // derivatives
880
881 if (chipset >= NVISA_GK104_CHIPSET) {
882 if (i->tex.rIndirectSrc >= 0 || i->tex.sIndirectSrc >= 0) {
883 // XXX this ignores tsc, and assumes a 1:1 mapping
884 assert(i->tex.rIndirectSrc >= 0);
885 Value *hnd = loadTexHandle(i->getIndirectR(), i->tex.r);
886 i->tex.r = 0xff;
887 i->tex.s = 0x1f;
888 i->setIndirectR(hnd);
889 i->setIndirectS(NULL);
890 } else if (i->tex.r == i->tex.s || i->op == OP_TXF) {
891 if (i->tex.r == 0xffff)
892 i->tex.r = prog->driver->io.fbtexBindBase / 4;
893 else
894 i->tex.r += prog->driver->io.texBindBase / 4;
895 i->tex.s = 0; // only a single cX[] value possible here
896 } else {
897 Value *hnd = bld.getScratch();
898 Value *rHnd = loadTexHandle(NULL, i->tex.r);
899 Value *sHnd = loadTexHandle(NULL, i->tex.s);
900
901 bld.mkOp3(OP_INSBF, TYPE_U32, hnd, rHnd, bld.mkImm(0x1400), sHnd);
902
903 i->tex.r = 0; // not used for indirect tex
904 i->tex.s = 0;
905 i->setIndirectR(hnd);
906 }
907 if (i->tex.target.isArray()) {
908 LValue *layer = new_LValue(func, FILE_GPR);
909 Value *src = i->getSrc(lyr);
910 const int sat = (i->op == OP_TXF) ? 1 : 0;
911 DataType sTy = (i->op == OP_TXF) ? TYPE_U32 : TYPE_F32;
912 bld.mkCvt(OP_CVT, TYPE_U16, layer, sTy, src)->saturate = sat;
913 if (i->op != OP_TXD || chipset < NVISA_GM107_CHIPSET) {
914 for (int s = dim; s >= 1; --s)
915 i->setSrc(s, i->getSrc(s - 1));
916 i->setSrc(0, layer);
917 } else {
918 i->setSrc(dim, layer);
919 }
920 }
921 // Move the indirect reference to the first place
922 if (i->tex.rIndirectSrc >= 0 && (
923 i->op == OP_TXD || chipset < NVISA_GM107_CHIPSET)) {
924 Value *hnd = i->getIndirectR();
925
926 i->setIndirectR(NULL);
927 i->moveSources(0, 1);
928 i->setSrc(0, hnd);
929 i->tex.rIndirectSrc = 0;
930 i->tex.sIndirectSrc = -1;
931 }
932 // Move the indirect reference to right after the coords
933 else if (i->tex.rIndirectSrc >= 0 && chipset >= NVISA_GM107_CHIPSET) {
934 Value *hnd = i->getIndirectR();
935
936 i->setIndirectR(NULL);
937 i->moveSources(arg, 1);
938 i->setSrc(arg, hnd);
939 i->tex.rIndirectSrc = 0;
940 i->tex.sIndirectSrc = -1;
941 }
942 } else
943 // (nvc0) generate and move the tsc/tic/array source to the front
944 if (i->tex.target.isArray() || i->tex.rIndirectSrc >= 0 || i->tex.sIndirectSrc >= 0) {
945 LValue *src = new_LValue(func, FILE_GPR); // 0xttxsaaaa
946
947 Value *ticRel = i->getIndirectR();
948 Value *tscRel = i->getIndirectS();
949
950 if (i->tex.r == 0xffff) {
951 i->tex.r = 0x20;
952 i->tex.s = 0x10;
953 }
954
955 if (ticRel) {
956 i->setSrc(i->tex.rIndirectSrc, NULL);
957 if (i->tex.r)
958 ticRel = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(),
959 ticRel, bld.mkImm(i->tex.r));
960 }
961 if (tscRel) {
962 i->setSrc(i->tex.sIndirectSrc, NULL);
963 if (i->tex.s)
964 tscRel = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(),
965 tscRel, bld.mkImm(i->tex.s));
966 }
967
968 Value *arrayIndex = i->tex.target.isArray() ? i->getSrc(lyr) : NULL;
969 if (arrayIndex) {
970 for (int s = dim; s >= 1; --s)
971 i->setSrc(s, i->getSrc(s - 1));
972 i->setSrc(0, arrayIndex);
973 } else {
974 i->moveSources(0, 1);
975 }
976
977 if (arrayIndex) {
978 int sat = (i->op == OP_TXF) ? 1 : 0;
979 DataType sTy = (i->op == OP_TXF) ? TYPE_U32 : TYPE_F32;
980 bld.mkCvt(OP_CVT, TYPE_U16, src, sTy, arrayIndex)->saturate = sat;
981 } else {
982 bld.loadImm(src, 0);
983 }
984
985 if (ticRel)
986 bld.mkOp3(OP_INSBF, TYPE_U32, src, ticRel, bld.mkImm(0x0917), src);
987 if (tscRel)
988 bld.mkOp3(OP_INSBF, TYPE_U32, src, tscRel, bld.mkImm(0x0710), src);
989
990 i->setSrc(0, src);
991 }
992
993 // For nvc0, the sample id has to be in the second operand, as the offset
994 // does. Right now we don't know how to pass both in, and this case can't
995 // happen with OpenGL. On nve0, the sample id is part of the texture
996 // coordinate argument.
997 assert(chipset >= NVISA_GK104_CHIPSET ||
998 !i->tex.useOffsets || !i->tex.target.isMS());
999
1000 // offset is between lod and dc
1001 if (i->tex.useOffsets) {
1002 int n, c;
1003 int s = i->srcCount(0xff, true);
1004 if (i->op != OP_TXD || chipset < NVISA_GK104_CHIPSET) {
1005 if (i->tex.target.isShadow())
1006 s--;
1007 if (i->srcExists(s)) // move potential predicate out of the way
1008 i->moveSources(s, 1);
1009 if (i->tex.useOffsets == 4 && i->srcExists(s + 1))
1010 i->moveSources(s + 1, 1);
1011 }
1012 if (i->op == OP_TXG) {
1013 // Either there is 1 offset, which goes into the 2 low bytes of the
1014 // first source, or there are 4 offsets, which go into 2 sources (8
1015 // values, 1 byte each).
1016 Value *offs[2] = {NULL, NULL};
1017 for (n = 0; n < i->tex.useOffsets; n++) {
1018 for (c = 0; c < 2; ++c) {
1019 if ((n % 2) == 0 && c == 0)
1020 bld.mkMov(offs[n / 2] = bld.getScratch(), i->offset[n][c].get());
1021 else
1022 bld.mkOp3(OP_INSBF, TYPE_U32,
1023 offs[n / 2],
1024 i->offset[n][c].get(),
1025 bld.mkImm(0x800 | ((n * 16 + c * 8) % 32)),
1026 offs[n / 2]);
1027 }
1028 }
1029 i->setSrc(s, offs[0]);
1030 if (offs[1])
1031 i->setSrc(s + 1, offs[1]);
1032 } else {
1033 unsigned imm = 0;
1034 assert(i->tex.useOffsets == 1);
1035 for (c = 0; c < 3; ++c) {
1036 ImmediateValue val;
1037 if (!i->offset[0][c].getImmediate(val))
1038 assert(!"non-immediate offset passed to non-TXG");
1039 imm |= (val.reg.data.u32 & 0xf) << (c * 4);
1040 }
1041 if (i->op == OP_TXD && chipset >= NVISA_GK104_CHIPSET) {
1042 // The offset goes into the upper 16 bits of the array index. So
1043 // create it if it's not already there, and INSBF it if it already
1044 // is.
1045 s = (i->tex.rIndirectSrc >= 0) ? 1 : 0;
1046 if (chipset >= NVISA_GM107_CHIPSET)
1047 s += dim;
1048 if (i->tex.target.isArray()) {
1049 bld.mkOp3(OP_INSBF, TYPE_U32, i->getSrc(s),
1050 bld.loadImm(NULL, imm), bld.mkImm(0xc10),
1051 i->getSrc(s));
1052 } else {
1053 i->moveSources(s, 1);
1054 i->setSrc(s, bld.loadImm(NULL, imm << 16));
1055 }
1056 } else {
1057 i->setSrc(s, bld.loadImm(NULL, imm));
1058 }
1059 }
1060 }
1061
1062 if (chipset >= NVISA_GK104_CHIPSET) {
1063 //
1064 // If TEX requires more than 4 sources, the 2nd register tuple must be
1065 // aligned to 4, even if it consists of just a single 4-byte register.
1066 //
1067 // XXX HACK: We insert 0 sources to avoid the 5 or 6 regs case.
1068 //
1069 int s = i->srcCount(0xff, true);
1070 if (s > 4 && s < 7) {
1071 if (i->srcExists(s)) // move potential predicate out of the way
1072 i->moveSources(s, 7 - s);
1073 while (s < 7)
1074 i->setSrc(s++, bld.loadImm(NULL, 0));
1075 }
1076 }
1077
1078 return true;
1079 }
1080
1081 bool
1082 NVC0LoweringPass::handleManualTXD(TexInstruction *i)
1083 {
1084 // Always done from the l0 perspective. This is the way that NVIDIA's
1085 // driver does it, and doing it from the "current" lane's perpsective
1086 // doesn't seem to always work for reasons that aren't altogether clear,
1087 // even in frag shaders.
1088 //
1089 // Note that we must move not only the coordinates into lane0, but also all
1090 // ancillary arguments, like array indices and depth compare as they may
1091 // differ between lanes. Offsets for TXD are supposed to be uniform, so we
1092 // leave them alone.
1093 static const uint8_t qOps[2] =
1094 { QUADOP(MOV2, ADD, MOV2, ADD), QUADOP(MOV2, MOV2, ADD, ADD) };
1095
1096 Value *def[4][4];
1097 Value *crd[3], *arr[2], *shadow;
1098 Instruction *tex;
1099 Value *zero = bld.loadImm(bld.getSSA(), 0);
1100 int l, c;
1101 const int dim = i->tex.target.getDim() + i->tex.target.isCube();
1102
1103 // This function is invoked after handleTEX lowering, so we have to expect
1104 // the arguments in the order that the hw wants them. For Fermi, array and
1105 // indirect are both in the leading arg, while for Kepler, array and
1106 // indirect are separate (and both precede the coordinates). Maxwell is
1107 // handled in a separate function.
1108 int array;
1109 if (targ->getChipset() < NVISA_GK104_CHIPSET)
1110 array = i->tex.target.isArray() || i->tex.rIndirectSrc >= 0;
1111 else
1112 array = i->tex.target.isArray() + (i->tex.rIndirectSrc >= 0);
1113
1114 i->op = OP_TEX; // no need to clone dPdx/dPdy later
1115
1116 for (c = 0; c < dim; ++c)
1117 crd[c] = bld.getScratch();
1118 for (c = 0; c < array; ++c)
1119 arr[c] = bld.getScratch();
1120 shadow = bld.getScratch();
1121
1122 for (l = 0; l < 4; ++l) {
1123 Value *src[3], *val;
1124
1125 bld.mkOp(OP_QUADON, TYPE_NONE, NULL);
1126 // we're using the texture result from lane 0 in all cases, so make sure
1127 // that lane 0 is pointing at the proper array index, indirect value,
1128 // and depth compare.
1129 if (l != 0) {
1130 for (c = 0; c < array; ++c)
1131 bld.mkQuadop(0x00, arr[c], l, i->getSrc(c), zero);
1132 if (i->tex.target.isShadow()) {
1133 // The next argument after coords is the depth compare
1134 bld.mkQuadop(0x00, shadow, l, i->getSrc(array + dim), zero);
1135 }
1136 }
1137 // mov position coordinates from lane l to all lanes
1138 for (c = 0; c < dim; ++c)
1139 bld.mkQuadop(0x00, crd[c], l, i->getSrc(c + array), zero);
1140 // add dPdx from lane l to lanes dx
1141 for (c = 0; c < dim; ++c)
1142 bld.mkQuadop(qOps[0], crd[c], l, i->dPdx[c].get(), crd[c]);
1143 // add dPdy from lane l to lanes dy
1144 for (c = 0; c < dim; ++c)
1145 bld.mkQuadop(qOps[1], crd[c], l, i->dPdy[c].get(), crd[c]);
1146 // normalize cube coordinates
1147 if (i->tex.target.isCube()) {
1148 for (c = 0; c < 3; ++c)
1149 src[c] = bld.mkOp1v(OP_ABS, TYPE_F32, bld.getSSA(), crd[c]);
1150 val = bld.getScratch();
1151 bld.mkOp2(OP_MAX, TYPE_F32, val, src[0], src[1]);
1152 bld.mkOp2(OP_MAX, TYPE_F32, val, src[2], val);
1153 bld.mkOp1(OP_RCP, TYPE_F32, val, val);
1154 for (c = 0; c < 3; ++c)
1155 src[c] = bld.mkOp2v(OP_MUL, TYPE_F32, bld.getSSA(), crd[c], val);
1156 } else {
1157 for (c = 0; c < dim; ++c)
1158 src[c] = crd[c];
1159 }
1160 // texture
1161 bld.insert(tex = cloneForward(func, i));
1162 if (l != 0) {
1163 for (c = 0; c < array; ++c)
1164 tex->setSrc(c, arr[c]);
1165 if (i->tex.target.isShadow())
1166 tex->setSrc(array + dim, shadow);
1167 }
1168 for (c = 0; c < dim; ++c)
1169 tex->setSrc(c + array, src[c]);
1170 // broadcast results from lane 0 to all lanes so that the moves *into*
1171 // the target lane pick up the proper value.
1172 if (l != 0)
1173 for (c = 0; i->defExists(c); ++c)
1174 bld.mkQuadop(0x00, tex->getDef(c), 0, tex->getDef(c), zero);
1175 bld.mkOp(OP_QUADPOP, TYPE_NONE, NULL);
1176
1177 // save results
1178 for (c = 0; i->defExists(c); ++c) {
1179 Instruction *mov;
1180 def[c][l] = bld.getSSA();
1181 mov = bld.mkMov(def[c][l], tex->getDef(c));
1182 mov->fixed = 1;
1183 mov->lanes = 1 << l;
1184 }
1185 }
1186
1187 for (c = 0; i->defExists(c); ++c) {
1188 Instruction *u = bld.mkOp(OP_UNION, TYPE_U32, i->getDef(c));
1189 for (l = 0; l < 4; ++l)
1190 u->setSrc(l, def[c][l]);
1191 }
1192
1193 i->bb->remove(i);
1194 return true;
1195 }
1196
1197 bool
1198 NVC0LoweringPass::handleTXD(TexInstruction *txd)
1199 {
1200 int dim = txd->tex.target.getDim() + txd->tex.target.isCube();
1201 unsigned arg = txd->tex.target.getArgCount();
1202 unsigned expected_args = arg;
1203 const int chipset = prog->getTarget()->getChipset();
1204
1205 if (chipset >= NVISA_GK104_CHIPSET) {
1206 if (!txd->tex.target.isArray() && txd->tex.useOffsets)
1207 expected_args++;
1208 if (txd->tex.rIndirectSrc >= 0 || txd->tex.sIndirectSrc >= 0)
1209 expected_args++;
1210 } else {
1211 if (txd->tex.useOffsets)
1212 expected_args++;
1213 if (!txd->tex.target.isArray() && (
1214 txd->tex.rIndirectSrc >= 0 || txd->tex.sIndirectSrc >= 0))
1215 expected_args++;
1216 }
1217
1218 if (expected_args > 4 ||
1219 dim > 2 ||
1220 txd->tex.target.isShadow())
1221 txd->op = OP_TEX;
1222
1223 handleTEX(txd);
1224 while (txd->srcExists(arg))
1225 ++arg;
1226
1227 txd->tex.derivAll = true;
1228 if (txd->op == OP_TEX)
1229 return handleManualTXD(txd);
1230
1231 assert(arg == expected_args);
1232 for (int c = 0; c < dim; ++c) {
1233 txd->setSrc(arg + c * 2 + 0, txd->dPdx[c]);
1234 txd->setSrc(arg + c * 2 + 1, txd->dPdy[c]);
1235 txd->dPdx[c].set(NULL);
1236 txd->dPdy[c].set(NULL);
1237 }
1238
1239 // In this case we have fewer than 4 "real" arguments, which means that
1240 // handleTEX didn't apply any padding. However we have to make sure that
1241 // the second "group" of arguments still gets padded up to 4.
1242 if (chipset >= NVISA_GK104_CHIPSET) {
1243 int s = arg + 2 * dim;
1244 if (s >= 4 && s < 7) {
1245 if (txd->srcExists(s)) // move potential predicate out of the way
1246 txd->moveSources(s, 7 - s);
1247 while (s < 7)
1248 txd->setSrc(s++, bld.loadImm(NULL, 0));
1249 }
1250 }
1251
1252 return true;
1253 }
1254
1255 bool
1256 NVC0LoweringPass::handleTXQ(TexInstruction *txq)
1257 {
1258 const int chipset = prog->getTarget()->getChipset();
1259 if (chipset >= NVISA_GK104_CHIPSET && txq->tex.rIndirectSrc < 0)
1260 txq->tex.r += prog->driver->io.texBindBase / 4;
1261
1262 if (txq->tex.rIndirectSrc < 0)
1263 return true;
1264
1265 Value *ticRel = txq->getIndirectR();
1266
1267 txq->setIndirectS(NULL);
1268 txq->tex.sIndirectSrc = -1;
1269
1270 assert(ticRel);
1271
1272 if (chipset < NVISA_GK104_CHIPSET) {
1273 LValue *src = new_LValue(func, FILE_GPR); // 0xttxsaaaa
1274
1275 txq->setSrc(txq->tex.rIndirectSrc, NULL);
1276 if (txq->tex.r)
1277 ticRel = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(),
1278 ticRel, bld.mkImm(txq->tex.r));
1279
1280 bld.mkOp2(OP_SHL, TYPE_U32, src, ticRel, bld.mkImm(0x17));
1281
1282 txq->moveSources(0, 1);
1283 txq->setSrc(0, src);
1284 } else {
1285 Value *hnd = loadTexHandle(txq->getIndirectR(), txq->tex.r);
1286 txq->tex.r = 0xff;
1287 txq->tex.s = 0x1f;
1288
1289 txq->setIndirectR(NULL);
1290 txq->moveSources(0, 1);
1291 txq->setSrc(0, hnd);
1292 txq->tex.rIndirectSrc = 0;
1293 }
1294
1295 return true;
1296 }
1297
1298 bool
1299 NVC0LoweringPass::handleTXLQ(TexInstruction *i)
1300 {
1301 /* The outputs are inverted compared to what the TGSI instruction
1302 * expects. Take that into account in the mask.
1303 */
1304 assert((i->tex.mask & ~3) == 0);
1305 if (i->tex.mask == 1)
1306 i->tex.mask = 2;
1307 else if (i->tex.mask == 2)
1308 i->tex.mask = 1;
1309 handleTEX(i);
1310 bld.setPosition(i, true);
1311
1312 /* The returned values are not quite what we want:
1313 * (a) convert from s16/u16 to f32
1314 * (b) multiply by 1/256
1315 */
1316 for (int def = 0; def < 2; ++def) {
1317 if (!i->defExists(def))
1318 continue;
1319 enum DataType type = TYPE_S16;
1320 if (i->tex.mask == 2 || def > 0)
1321 type = TYPE_U16;
1322 bld.mkCvt(OP_CVT, TYPE_F32, i->getDef(def), type, i->getDef(def));
1323 bld.mkOp2(OP_MUL, TYPE_F32, i->getDef(def),
1324 i->getDef(def), bld.loadImm(NULL, 1.0f / 256));
1325 }
1326 if (i->tex.mask == 3) {
1327 LValue *t = new_LValue(func, FILE_GPR);
1328 bld.mkMov(t, i->getDef(0));
1329 bld.mkMov(i->getDef(0), i->getDef(1));
1330 bld.mkMov(i->getDef(1), t);
1331 }
1332 return true;
1333 }
1334
1335 bool
1336 NVC0LoweringPass::handleBUFQ(Instruction *bufq)
1337 {
1338 bufq->op = OP_MOV;
1339 bufq->setSrc(0, loadBufLength32(bufq->getIndirect(0, 1),
1340 bufq->getSrc(0)->reg.fileIndex * 16));
1341 bufq->setIndirect(0, 0, NULL);
1342 bufq->setIndirect(0, 1, NULL);
1343 return true;
1344 }
1345
1346 void
1347 NVC0LoweringPass::handleSharedATOMNVE4(Instruction *atom)
1348 {
1349 assert(atom->src(0).getFile() == FILE_MEMORY_SHARED);
1350
1351 BasicBlock *currBB = atom->bb;
1352 BasicBlock *tryLockBB = atom->bb->splitBefore(atom, false);
1353 BasicBlock *joinBB = atom->bb->splitAfter(atom);
1354 BasicBlock *setAndUnlockBB = new BasicBlock(func);
1355 BasicBlock *failLockBB = new BasicBlock(func);
1356
1357 bld.setPosition(currBB, true);
1358 assert(!currBB->joinAt);
1359 currBB->joinAt = bld.mkFlow(OP_JOINAT, joinBB, CC_ALWAYS, NULL);
1360
1361 CmpInstruction *pred =
1362 bld.mkCmp(OP_SET, CC_EQ, TYPE_U32, bld.getSSA(1, FILE_PREDICATE),
1363 TYPE_U32, bld.mkImm(0), bld.mkImm(1));
1364
1365 bld.mkFlow(OP_BRA, tryLockBB, CC_ALWAYS, NULL);
1366 currBB->cfg.attach(&tryLockBB->cfg, Graph::Edge::TREE);
1367
1368 bld.setPosition(tryLockBB, true);
1369
1370 Instruction *ld =
1371 bld.mkLoad(TYPE_U32, atom->getDef(0), atom->getSrc(0)->asSym(),
1372 atom->getIndirect(0, 0));
1373 ld->setDef(1, bld.getSSA(1, FILE_PREDICATE));
1374 ld->subOp = NV50_IR_SUBOP_LOAD_LOCKED;
1375
1376 bld.mkFlow(OP_BRA, setAndUnlockBB, CC_P, ld->getDef(1));
1377 bld.mkFlow(OP_BRA, failLockBB, CC_ALWAYS, NULL);
1378 tryLockBB->cfg.attach(&failLockBB->cfg, Graph::Edge::CROSS);
1379 tryLockBB->cfg.attach(&setAndUnlockBB->cfg, Graph::Edge::TREE);
1380
1381 tryLockBB->cfg.detach(&joinBB->cfg);
1382 bld.remove(atom);
1383
1384 bld.setPosition(setAndUnlockBB, true);
1385 Value *stVal;
1386 if (atom->subOp == NV50_IR_SUBOP_ATOM_EXCH) {
1387 // Read the old value, and write the new one.
1388 stVal = atom->getSrc(1);
1389 } else if (atom->subOp == NV50_IR_SUBOP_ATOM_CAS) {
1390 CmpInstruction *set =
1391 bld.mkCmp(OP_SET, CC_EQ, TYPE_U32, bld.getSSA(),
1392 TYPE_U32, ld->getDef(0), atom->getSrc(1));
1393
1394 bld.mkCmp(OP_SLCT, CC_NE, TYPE_U32, (stVal = bld.getSSA()),
1395 TYPE_U32, atom->getSrc(2), ld->getDef(0), set->getDef(0));
1396 } else {
1397 operation op;
1398
1399 switch (atom->subOp) {
1400 case NV50_IR_SUBOP_ATOM_ADD:
1401 op = OP_ADD;
1402 break;
1403 case NV50_IR_SUBOP_ATOM_AND:
1404 op = OP_AND;
1405 break;
1406 case NV50_IR_SUBOP_ATOM_OR:
1407 op = OP_OR;
1408 break;
1409 case NV50_IR_SUBOP_ATOM_XOR:
1410 op = OP_XOR;
1411 break;
1412 case NV50_IR_SUBOP_ATOM_MIN:
1413 op = OP_MIN;
1414 break;
1415 case NV50_IR_SUBOP_ATOM_MAX:
1416 op = OP_MAX;
1417 break;
1418 default:
1419 assert(0);
1420 return;
1421 }
1422
1423 stVal = bld.mkOp2v(op, atom->dType, bld.getSSA(), ld->getDef(0),
1424 atom->getSrc(1));
1425 }
1426
1427 Instruction *st =
1428 bld.mkStore(OP_STORE, TYPE_U32, atom->getSrc(0)->asSym(),
1429 atom->getIndirect(0, 0), stVal);
1430 st->setDef(0, pred->getDef(0));
1431 st->subOp = NV50_IR_SUBOP_STORE_UNLOCKED;
1432
1433 bld.mkFlow(OP_BRA, failLockBB, CC_ALWAYS, NULL);
1434 setAndUnlockBB->cfg.attach(&failLockBB->cfg, Graph::Edge::TREE);
1435
1436 // Lock until the store has not been performed.
1437 bld.setPosition(failLockBB, true);
1438 bld.mkFlow(OP_BRA, tryLockBB, CC_NOT_P, pred->getDef(0));
1439 bld.mkFlow(OP_BRA, joinBB, CC_ALWAYS, NULL);
1440 failLockBB->cfg.attach(&tryLockBB->cfg, Graph::Edge::BACK);
1441 failLockBB->cfg.attach(&joinBB->cfg, Graph::Edge::TREE);
1442
1443 bld.setPosition(joinBB, false);
1444 bld.mkFlow(OP_JOIN, NULL, CC_ALWAYS, NULL)->fixed = 1;
1445 }
1446
1447 void
1448 NVC0LoweringPass::handleSharedATOM(Instruction *atom)
1449 {
1450 assert(atom->src(0).getFile() == FILE_MEMORY_SHARED);
1451
1452 BasicBlock *currBB = atom->bb;
1453 BasicBlock *tryLockAndSetBB = atom->bb->splitBefore(atom, false);
1454 BasicBlock *joinBB = atom->bb->splitAfter(atom);
1455
1456 bld.setPosition(currBB, true);
1457 assert(!currBB->joinAt);
1458 currBB->joinAt = bld.mkFlow(OP_JOINAT, joinBB, CC_ALWAYS, NULL);
1459
1460 bld.mkFlow(OP_BRA, tryLockAndSetBB, CC_ALWAYS, NULL);
1461 currBB->cfg.attach(&tryLockAndSetBB->cfg, Graph::Edge::TREE);
1462
1463 bld.setPosition(tryLockAndSetBB, true);
1464
1465 Instruction *ld =
1466 bld.mkLoad(TYPE_U32, atom->getDef(0), atom->getSrc(0)->asSym(),
1467 atom->getIndirect(0, 0));
1468 ld->setDef(1, bld.getSSA(1, FILE_PREDICATE));
1469 ld->subOp = NV50_IR_SUBOP_LOAD_LOCKED;
1470
1471 Value *stVal;
1472 if (atom->subOp == NV50_IR_SUBOP_ATOM_EXCH) {
1473 // Read the old value, and write the new one.
1474 stVal = atom->getSrc(1);
1475 } else if (atom->subOp == NV50_IR_SUBOP_ATOM_CAS) {
1476 CmpInstruction *set =
1477 bld.mkCmp(OP_SET, CC_EQ, TYPE_U32, bld.getSSA(1, FILE_PREDICATE),
1478 TYPE_U32, ld->getDef(0), atom->getSrc(1));
1479 set->setPredicate(CC_P, ld->getDef(1));
1480
1481 Instruction *selp =
1482 bld.mkOp3(OP_SELP, TYPE_U32, bld.getSSA(), ld->getDef(0),
1483 atom->getSrc(2), set->getDef(0));
1484 selp->src(2).mod = Modifier(NV50_IR_MOD_NOT);
1485 selp->setPredicate(CC_P, ld->getDef(1));
1486
1487 stVal = selp->getDef(0);
1488 } else {
1489 operation op;
1490
1491 switch (atom->subOp) {
1492 case NV50_IR_SUBOP_ATOM_ADD:
1493 op = OP_ADD;
1494 break;
1495 case NV50_IR_SUBOP_ATOM_AND:
1496 op = OP_AND;
1497 break;
1498 case NV50_IR_SUBOP_ATOM_OR:
1499 op = OP_OR;
1500 break;
1501 case NV50_IR_SUBOP_ATOM_XOR:
1502 op = OP_XOR;
1503 break;
1504 case NV50_IR_SUBOP_ATOM_MIN:
1505 op = OP_MIN;
1506 break;
1507 case NV50_IR_SUBOP_ATOM_MAX:
1508 op = OP_MAX;
1509 break;
1510 default:
1511 assert(0);
1512 return;
1513 }
1514
1515 Instruction *i =
1516 bld.mkOp2(op, atom->dType, bld.getSSA(), ld->getDef(0),
1517 atom->getSrc(1));
1518 i->setPredicate(CC_P, ld->getDef(1));
1519
1520 stVal = i->getDef(0);
1521 }
1522
1523 Instruction *st =
1524 bld.mkStore(OP_STORE, TYPE_U32, atom->getSrc(0)->asSym(),
1525 atom->getIndirect(0, 0), stVal);
1526 st->setPredicate(CC_P, ld->getDef(1));
1527 st->subOp = NV50_IR_SUBOP_STORE_UNLOCKED;
1528
1529 // Loop until the lock is acquired.
1530 bld.mkFlow(OP_BRA, tryLockAndSetBB, CC_NOT_P, ld->getDef(1));
1531 tryLockAndSetBB->cfg.attach(&tryLockAndSetBB->cfg, Graph::Edge::BACK);
1532 tryLockAndSetBB->cfg.attach(&joinBB->cfg, Graph::Edge::CROSS);
1533 bld.mkFlow(OP_BRA, joinBB, CC_ALWAYS, NULL);
1534
1535 bld.remove(atom);
1536
1537 bld.setPosition(joinBB, false);
1538 bld.mkFlow(OP_JOIN, NULL, CC_ALWAYS, NULL)->fixed = 1;
1539 }
1540
1541 bool
1542 NVC0LoweringPass::handleATOM(Instruction *atom)
1543 {
1544 SVSemantic sv;
1545 Value *ptr = atom->getIndirect(0, 0), *ind = atom->getIndirect(0, 1), *base;
1546
1547 switch (atom->src(0).getFile()) {
1548 case FILE_MEMORY_LOCAL:
1549 sv = SV_LBASE;
1550 break;
1551 case FILE_MEMORY_SHARED:
1552 // For Fermi/Kepler, we have to use ld lock/st unlock to perform atomic
1553 // operations on shared memory. For Maxwell, ATOMS is enough.
1554 if (targ->getChipset() < NVISA_GK104_CHIPSET)
1555 handleSharedATOM(atom);
1556 else if (targ->getChipset() < NVISA_GM107_CHIPSET)
1557 handleSharedATOMNVE4(atom);
1558 return true;
1559 default:
1560 assert(atom->src(0).getFile() == FILE_MEMORY_BUFFER);
1561 base = loadBufInfo64(ind, atom->getSrc(0)->reg.fileIndex * 16);
1562 assert(base->reg.size == 8);
1563 if (ptr)
1564 base = bld.mkOp2v(OP_ADD, TYPE_U64, base, base, ptr);
1565 assert(base->reg.size == 8);
1566 atom->setIndirect(0, 0, base);
1567 atom->getSrc(0)->reg.file = FILE_MEMORY_GLOBAL;
1568
1569 // Harden against out-of-bounds accesses
1570 Value *offset = bld.loadImm(NULL, atom->getSrc(0)->reg.data.offset + typeSizeof(atom->sType));
1571 Value *length = loadBufLength32(ind, atom->getSrc(0)->reg.fileIndex * 16);
1572 Value *pred = new_LValue(func, FILE_PREDICATE);
1573 if (ptr)
1574 bld.mkOp2(OP_ADD, TYPE_U32, offset, offset, ptr);
1575 bld.mkCmp(OP_SET, CC_GT, TYPE_U32, pred, TYPE_U32, offset, length);
1576 atom->setPredicate(CC_NOT_P, pred);
1577 if (atom->defExists(0)) {
1578 Value *zero, *dst = atom->getDef(0);
1579 atom->setDef(0, bld.getSSA());
1580
1581 bld.setPosition(atom, true);
1582 bld.mkMov((zero = bld.getSSA()), bld.mkImm(0))
1583 ->setPredicate(CC_P, pred);
1584 bld.mkOp2(OP_UNION, TYPE_U32, dst, atom->getDef(0), zero);
1585 }
1586
1587 return true;
1588 }
1589 base =
1590 bld.mkOp1v(OP_RDSV, TYPE_U32, bld.getScratch(), bld.mkSysVal(sv, 0));
1591
1592 atom->setSrc(0, cloneShallow(func, atom->getSrc(0)));
1593 atom->getSrc(0)->reg.file = FILE_MEMORY_GLOBAL;
1594 if (ptr)
1595 base = bld.mkOp2v(OP_ADD, TYPE_U32, base, base, ptr);
1596 atom->setIndirect(0, 1, NULL);
1597 atom->setIndirect(0, 0, base);
1598
1599 return true;
1600 }
1601
1602 bool
1603 NVC0LoweringPass::handleCasExch(Instruction *cas, bool needCctl)
1604 {
1605 if (targ->getChipset() < NVISA_GM107_CHIPSET) {
1606 if (cas->src(0).getFile() == FILE_MEMORY_SHARED) {
1607 // ATOM_CAS and ATOM_EXCH are handled in handleSharedATOM().
1608 return false;
1609 }
1610 }
1611
1612 if (cas->subOp != NV50_IR_SUBOP_ATOM_CAS &&
1613 cas->subOp != NV50_IR_SUBOP_ATOM_EXCH)
1614 return false;
1615 bld.setPosition(cas, true);
1616
1617 if (needCctl) {
1618 Instruction *cctl = bld.mkOp1(OP_CCTL, TYPE_NONE, NULL, cas->getSrc(0));
1619 cctl->setIndirect(0, 0, cas->getIndirect(0, 0));
1620 cctl->fixed = 1;
1621 cctl->subOp = NV50_IR_SUBOP_CCTL_IV;
1622 if (cas->isPredicated())
1623 cctl->setPredicate(cas->cc, cas->getPredicate());
1624 }
1625
1626 if (cas->subOp == NV50_IR_SUBOP_ATOM_CAS) {
1627 // CAS is crazy. It's 2nd source is a double reg, and the 3rd source
1628 // should be set to the high part of the double reg or bad things will
1629 // happen elsewhere in the universe.
1630 // Also, it sometimes returns the new value instead of the old one
1631 // under mysterious circumstances.
1632 Value *dreg = bld.getSSA(8);
1633 bld.setPosition(cas, false);
1634 bld.mkOp2(OP_MERGE, TYPE_U64, dreg, cas->getSrc(1), cas->getSrc(2));
1635 cas->setSrc(1, dreg);
1636 cas->setSrc(2, dreg);
1637 }
1638
1639 return true;
1640 }
1641
1642 inline Value *
1643 NVC0LoweringPass::loadResInfo32(Value *ptr, uint32_t off, uint16_t base)
1644 {
1645 uint8_t b = prog->driver->io.auxCBSlot;
1646 off += base;
1647
1648 return bld.
1649 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
1650 }
1651
1652 inline Value *
1653 NVC0LoweringPass::loadResInfo64(Value *ptr, uint32_t off, uint16_t base)
1654 {
1655 uint8_t b = prog->driver->io.auxCBSlot;
1656 off += base;
1657
1658 if (ptr)
1659 ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getScratch(), ptr, bld.mkImm(4));
1660
1661 return bld.
1662 mkLoadv(TYPE_U64, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U64, off), ptr);
1663 }
1664
1665 inline Value *
1666 NVC0LoweringPass::loadResLength32(Value *ptr, uint32_t off, uint16_t base)
1667 {
1668 uint8_t b = prog->driver->io.auxCBSlot;
1669 off += base;
1670
1671 if (ptr)
1672 ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getScratch(), ptr, bld.mkImm(4));
1673
1674 return bld.
1675 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U64, off + 8), ptr);
1676 }
1677
1678 inline Value *
1679 NVC0LoweringPass::loadBufInfo64(Value *ptr, uint32_t off)
1680 {
1681 return loadResInfo64(ptr, off, prog->driver->io.bufInfoBase);
1682 }
1683
1684 inline Value *
1685 NVC0LoweringPass::loadBufLength32(Value *ptr, uint32_t off)
1686 {
1687 return loadResLength32(ptr, off, prog->driver->io.bufInfoBase);
1688 }
1689
1690 inline Value *
1691 NVC0LoweringPass::loadUboInfo64(Value *ptr, uint32_t off)
1692 {
1693 return loadResInfo64(ptr, off, prog->driver->io.uboInfoBase);
1694 }
1695
1696 inline Value *
1697 NVC0LoweringPass::loadUboLength32(Value *ptr, uint32_t off)
1698 {
1699 return loadResLength32(ptr, off, prog->driver->io.uboInfoBase);
1700 }
1701
1702 inline Value *
1703 NVC0LoweringPass::loadMsInfo32(Value *ptr, uint32_t off)
1704 {
1705 uint8_t b = prog->driver->io.msInfoCBSlot;
1706 off += prog->driver->io.msInfoBase;
1707 return bld.
1708 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
1709 }
1710
1711 /* On nvc0, surface info is obtained via the surface binding points passed
1712 * to the SULD/SUST instructions.
1713 * On nve4, surface info is stored in c[] and is used by various special
1714 * instructions, e.g. for clamping coordinates or generating an address.
1715 * They couldn't just have added an equivalent to TIC now, couldn't they ?
1716 */
1717 #define NVC0_SU_INFO_ADDR 0x00
1718 #define NVC0_SU_INFO_FMT 0x04
1719 #define NVC0_SU_INFO_DIM_X 0x08
1720 #define NVC0_SU_INFO_PITCH 0x0c
1721 #define NVC0_SU_INFO_DIM_Y 0x10
1722 #define NVC0_SU_INFO_ARRAY 0x14
1723 #define NVC0_SU_INFO_DIM_Z 0x18
1724 #define NVC0_SU_INFO_UNK1C 0x1c
1725 #define NVC0_SU_INFO_WIDTH 0x20
1726 #define NVC0_SU_INFO_HEIGHT 0x24
1727 #define NVC0_SU_INFO_DEPTH 0x28
1728 #define NVC0_SU_INFO_TARGET 0x2c
1729 #define NVC0_SU_INFO_BSIZE 0x30
1730 #define NVC0_SU_INFO_RAW_X 0x34
1731 #define NVC0_SU_INFO_MS_X 0x38
1732 #define NVC0_SU_INFO_MS_Y 0x3c
1733
1734 #define NVC0_SU_INFO__STRIDE 0x40
1735
1736 #define NVC0_SU_INFO_DIM(i) (0x08 + (i) * 8)
1737 #define NVC0_SU_INFO_SIZE(i) (0x20 + (i) * 4)
1738 #define NVC0_SU_INFO_MS(i) (0x38 + (i) * 4)
1739
1740 inline Value *
1741 NVC0LoweringPass::loadSuInfo32(Value *ptr, int slot, uint32_t off)
1742 {
1743 uint32_t base = slot * NVC0_SU_INFO__STRIDE;
1744
1745 if (ptr) {
1746 ptr = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getSSA(), ptr, bld.mkImm(slot));
1747 ptr = bld.mkOp2v(OP_AND, TYPE_U32, bld.getSSA(), ptr, bld.mkImm(7));
1748 ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(), ptr, bld.mkImm(6));
1749 base = 0;
1750 }
1751 off += base;
1752
1753 return loadResInfo32(ptr, off, prog->driver->io.suInfoBase);
1754 }
1755
1756 static inline uint16_t getSuClampSubOp(const TexInstruction *su, int c)
1757 {
1758 switch (su->tex.target.getEnum()) {
1759 case TEX_TARGET_BUFFER: return NV50_IR_SUBOP_SUCLAMP_PL(0, 1);
1760 case TEX_TARGET_RECT: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1761 case TEX_TARGET_1D: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1762 case TEX_TARGET_1D_ARRAY: return (c == 1) ?
1763 NV50_IR_SUBOP_SUCLAMP_PL(0, 2) :
1764 NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1765 case TEX_TARGET_2D: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1766 case TEX_TARGET_2D_MS: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1767 case TEX_TARGET_2D_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1768 case TEX_TARGET_2D_MS_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1769 case TEX_TARGET_3D: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1770 case TEX_TARGET_CUBE: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1771 case TEX_TARGET_CUBE_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1772 default:
1773 assert(0);
1774 return 0;
1775 }
1776 }
1777
1778 bool
1779 NVC0LoweringPass::handleSUQ(TexInstruction *suq)
1780 {
1781 int mask = suq->tex.mask;
1782 int dim = suq->tex.target.getDim();
1783 int arg = dim + (suq->tex.target.isArray() || suq->tex.target.isCube());
1784 Value *ind = suq->getIndirectR();
1785 int slot = suq->tex.r;
1786 int c, d;
1787
1788 for (c = 0, d = 0; c < 3; ++c, mask >>= 1) {
1789 if (c >= arg || !(mask & 1))
1790 continue;
1791
1792 int offset;
1793
1794 if (c == 1 && suq->tex.target == TEX_TARGET_1D_ARRAY) {
1795 offset = NVC0_SU_INFO_SIZE(2);
1796 } else {
1797 offset = NVC0_SU_INFO_SIZE(c);
1798 }
1799 bld.mkMov(suq->getDef(d++), loadSuInfo32(ind, slot, offset));
1800 if (c == 2 && suq->tex.target.isCube())
1801 bld.mkOp2(OP_DIV, TYPE_U32, suq->getDef(d - 1), suq->getDef(d - 1),
1802 bld.loadImm(NULL, 6));
1803 }
1804
1805 if (mask & 1) {
1806 if (suq->tex.target.isMS()) {
1807 Value *ms_x = loadSuInfo32(ind, slot, NVC0_SU_INFO_MS(0));
1808 Value *ms_y = loadSuInfo32(ind, slot, NVC0_SU_INFO_MS(1));
1809 Value *ms = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(), ms_x, ms_y);
1810 bld.mkOp2(OP_SHL, TYPE_U32, suq->getDef(d++), bld.loadImm(NULL, 1), ms);
1811 } else {
1812 bld.mkMov(suq->getDef(d++), bld.loadImm(NULL, 1));
1813 }
1814 }
1815
1816 bld.remove(suq);
1817 return true;
1818 }
1819
1820 void
1821 NVC0LoweringPass::adjustCoordinatesMS(TexInstruction *tex)
1822 {
1823 const int arg = tex->tex.target.getArgCount();
1824 int slot = tex->tex.r;
1825
1826 if (tex->tex.target == TEX_TARGET_2D_MS)
1827 tex->tex.target = TEX_TARGET_2D;
1828 else
1829 if (tex->tex.target == TEX_TARGET_2D_MS_ARRAY)
1830 tex->tex.target = TEX_TARGET_2D_ARRAY;
1831 else
1832 return;
1833
1834 Value *x = tex->getSrc(0);
1835 Value *y = tex->getSrc(1);
1836 Value *s = tex->getSrc(arg - 1);
1837
1838 Value *tx = bld.getSSA(), *ty = bld.getSSA(), *ts = bld.getSSA();
1839 Value *ind = tex->getIndirectR();
1840
1841 Value *ms_x = loadSuInfo32(ind, slot, NVC0_SU_INFO_MS(0));
1842 Value *ms_y = loadSuInfo32(ind, slot, NVC0_SU_INFO_MS(1));
1843
1844 bld.mkOp2(OP_SHL, TYPE_U32, tx, x, ms_x);
1845 bld.mkOp2(OP_SHL, TYPE_U32, ty, y, ms_y);
1846
1847 s = bld.mkOp2v(OP_AND, TYPE_U32, ts, s, bld.loadImm(NULL, 0x7));
1848 s = bld.mkOp2v(OP_SHL, TYPE_U32, ts, ts, bld.mkImm(3));
1849
1850 Value *dx = loadMsInfo32(ts, 0x0);
1851 Value *dy = loadMsInfo32(ts, 0x4);
1852
1853 bld.mkOp2(OP_ADD, TYPE_U32, tx, tx, dx);
1854 bld.mkOp2(OP_ADD, TYPE_U32, ty, ty, dy);
1855
1856 tex->setSrc(0, tx);
1857 tex->setSrc(1, ty);
1858 tex->moveSources(arg, -1);
1859 }
1860
1861 // Sets 64-bit "generic address", predicate and format sources for SULD/SUST.
1862 // They're computed from the coordinates using the surface info in c[] space.
1863 void
1864 NVC0LoweringPass::processSurfaceCoordsNVE4(TexInstruction *su)
1865 {
1866 Instruction *insn;
1867 const bool atom = su->op == OP_SUREDB || su->op == OP_SUREDP;
1868 const bool raw =
1869 su->op == OP_SULDB || su->op == OP_SUSTB || su->op == OP_SUREDB;
1870 const int slot = su->tex.r;
1871 const int dim = su->tex.target.getDim();
1872 const int arg = dim + (su->tex.target.isArray() || su->tex.target.isCube());
1873 int c;
1874 Value *zero = bld.mkImm(0);
1875 Value *p1 = NULL;
1876 Value *v;
1877 Value *src[3];
1878 Value *bf, *eau, *off;
1879 Value *addr, *pred;
1880 Value *ind = su->getIndirectR();
1881
1882 off = bld.getScratch(4);
1883 bf = bld.getScratch(4);
1884 addr = bld.getSSA(8);
1885 pred = bld.getScratch(1, FILE_PREDICATE);
1886
1887 bld.setPosition(su, false);
1888
1889 adjustCoordinatesMS(su);
1890
1891 // calculate clamped coordinates
1892 for (c = 0; c < arg; ++c) {
1893 int dimc = c;
1894
1895 if (c == 1 && su->tex.target == TEX_TARGET_1D_ARRAY) {
1896 // The array index is stored in the Z component for 1D arrays.
1897 dimc = 2;
1898 }
1899
1900 src[c] = bld.getScratch();
1901 if (c == 0 && raw)
1902 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_RAW_X);
1903 else
1904 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_DIM(dimc));
1905 bld.mkOp3(OP_SUCLAMP, TYPE_S32, src[c], su->getSrc(c), v, zero)
1906 ->subOp = getSuClampSubOp(su, dimc);
1907 }
1908 for (; c < 3; ++c)
1909 src[c] = zero;
1910
1911 // set predicate output
1912 if (su->tex.target == TEX_TARGET_BUFFER) {
1913 src[0]->getInsn()->setFlagsDef(1, pred);
1914 } else
1915 if (su->tex.target.isArray() || su->tex.target.isCube()) {
1916 p1 = bld.getSSA(1, FILE_PREDICATE);
1917 src[dim]->getInsn()->setFlagsDef(1, p1);
1918 }
1919
1920 // calculate pixel offset
1921 if (dim == 1) {
1922 if (su->tex.target != TEX_TARGET_BUFFER)
1923 bld.mkOp2(OP_AND, TYPE_U32, off, src[0], bld.loadImm(NULL, 0xffff));
1924 } else
1925 if (dim == 3) {
1926 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_UNK1C);
1927 bld.mkOp3(OP_MADSP, TYPE_U32, off, src[2], v, src[1])
1928 ->subOp = NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1929
1930 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_PITCH);
1931 bld.mkOp3(OP_MADSP, TYPE_U32, off, off, v, src[0])
1932 ->subOp = NV50_IR_SUBOP_MADSP(0,2,8); // u32 u16l u16l
1933 } else {
1934 assert(dim == 2);
1935 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_PITCH);
1936 bld.mkOp3(OP_MADSP, TYPE_U32, off, src[1], v, src[0])
1937 ->subOp = (su->tex.target.isArray() || su->tex.target.isCube()) ?
1938 NV50_IR_SUBOP_MADSP_SD : NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1939 }
1940
1941 // calculate effective address part 1
1942 if (su->tex.target == TEX_TARGET_BUFFER) {
1943 if (raw) {
1944 bf = src[0];
1945 } else {
1946 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_FMT);
1947 bld.mkOp3(OP_VSHL, TYPE_U32, bf, src[0], v, zero)
1948 ->subOp = NV50_IR_SUBOP_V1(7,6,8|2);
1949 }
1950 } else {
1951 Value *y = src[1];
1952 Value *z = src[2];
1953 uint16_t subOp = 0;
1954
1955 switch (dim) {
1956 case 1:
1957 y = zero;
1958 z = zero;
1959 break;
1960 case 2:
1961 z = off;
1962 if (!su->tex.target.isArray() && !su->tex.target.isCube()) {
1963 z = loadSuInfo32(ind, slot, NVC0_SU_INFO_UNK1C);
1964 subOp = NV50_IR_SUBOP_SUBFM_3D;
1965 }
1966 break;
1967 default:
1968 subOp = NV50_IR_SUBOP_SUBFM_3D;
1969 assert(dim == 3);
1970 break;
1971 }
1972 insn = bld.mkOp3(OP_SUBFM, TYPE_U32, bf, src[0], y, z);
1973 insn->subOp = subOp;
1974 insn->setFlagsDef(1, pred);
1975 }
1976
1977 // part 2
1978 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_ADDR);
1979
1980 if (su->tex.target == TEX_TARGET_BUFFER) {
1981 eau = v;
1982 } else {
1983 eau = bld.mkOp3v(OP_SUEAU, TYPE_U32, bld.getScratch(4), off, bf, v);
1984 }
1985 // add array layer offset
1986 if (su->tex.target.isArray() || su->tex.target.isCube()) {
1987 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_ARRAY);
1988 if (dim == 1)
1989 bld.mkOp3(OP_MADSP, TYPE_U32, eau, src[1], v, eau)
1990 ->subOp = NV50_IR_SUBOP_MADSP(4,0,0); // u16 u24 u32
1991 else
1992 bld.mkOp3(OP_MADSP, TYPE_U32, eau, v, src[2], eau)
1993 ->subOp = NV50_IR_SUBOP_MADSP(0,0,0); // u32 u24 u32
1994 // combine predicates
1995 assert(p1);
1996 bld.mkOp2(OP_OR, TYPE_U8, pred, pred, p1);
1997 }
1998
1999 if (atom) {
2000 Value *lo = bf;
2001 if (su->tex.target == TEX_TARGET_BUFFER) {
2002 lo = zero;
2003 bld.mkMov(off, bf);
2004 }
2005 // bf == g[] address & 0xff
2006 // eau == g[] address >> 8
2007 bld.mkOp3(OP_PERMT, TYPE_U32, bf, lo, bld.loadImm(NULL, 0x6540), eau);
2008 bld.mkOp3(OP_PERMT, TYPE_U32, eau, zero, bld.loadImm(NULL, 0x0007), eau);
2009 } else
2010 if (su->op == OP_SULDP && su->tex.target == TEX_TARGET_BUFFER) {
2011 // Convert from u32 to u8 address format, which is what the library code
2012 // doing SULDP currently uses.
2013 // XXX: can SUEAU do this ?
2014 // XXX: does it matter that we don't mask high bytes in bf ?
2015 // Grrr.
2016 bld.mkOp2(OP_SHR, TYPE_U32, off, bf, bld.mkImm(8));
2017 bld.mkOp2(OP_ADD, TYPE_U32, eau, eau, off);
2018 }
2019
2020 bld.mkOp2(OP_MERGE, TYPE_U64, addr, bf, eau);
2021
2022 if (atom && su->tex.target == TEX_TARGET_BUFFER)
2023 bld.mkOp2(OP_ADD, TYPE_U64, addr, addr, off);
2024
2025 // let's just set it 0 for raw access and hope it works
2026 v = raw ?
2027 bld.mkImm(0) : loadSuInfo32(ind, slot, NVC0_SU_INFO_FMT);
2028
2029 // get rid of old coordinate sources, make space for fmt info and predicate
2030 su->moveSources(arg, 3 - arg);
2031 // set 64 bit address and 32-bit format sources
2032 su->setSrc(0, addr);
2033 su->setSrc(1, v);
2034 su->setSrc(2, pred);
2035
2036 // prevent read fault when the image is not actually bound
2037 CmpInstruction *pred1 =
2038 bld.mkCmp(OP_SET, CC_EQ, TYPE_U32, bld.getSSA(1, FILE_PREDICATE),
2039 TYPE_U32, bld.mkImm(0),
2040 loadSuInfo32(ind, slot, NVC0_SU_INFO_ADDR));
2041
2042 if (su->op != OP_SUSTP && su->tex.format) {
2043 const TexInstruction::ImgFormatDesc *format = su->tex.format;
2044 int blockwidth = format->bits[0] + format->bits[1] +
2045 format->bits[2] + format->bits[3];
2046
2047 // make sure that the format doesn't mismatch
2048 assert(format->components != 0);
2049 bld.mkCmp(OP_SET_OR, CC_NE, TYPE_U32, pred1->getDef(0),
2050 TYPE_U32, bld.loadImm(NULL, blockwidth / 8),
2051 loadSuInfo32(ind, slot, NVC0_SU_INFO_BSIZE),
2052 pred1->getDef(0));
2053 }
2054 su->setPredicate(CC_NOT_P, pred1->getDef(0));
2055
2056 // TODO: initialize def values to 0 when the surface operation is not
2057 // performed (not needed for stores). Also, fix the "address bounds test"
2058 // subtests from arb_shader_image_load_store-invalid for buffers, because it
2059 // seems like that the predicate is not correctly set by suclamp.
2060 }
2061
2062 static DataType
2063 getSrcType(const TexInstruction::ImgFormatDesc *t, int c)
2064 {
2065 switch (t->type) {
2066 case FLOAT: return t->bits[c] == 16 ? TYPE_F16 : TYPE_F32;
2067 case UNORM: return t->bits[c] == 8 ? TYPE_U8 : TYPE_U16;
2068 case SNORM: return t->bits[c] == 8 ? TYPE_S8 : TYPE_S16;
2069 case UINT:
2070 return (t->bits[c] == 8 ? TYPE_U8 :
2071 (t->bits[c] == 16 ? TYPE_U16 : TYPE_U32));
2072 case SINT:
2073 return (t->bits[c] == 8 ? TYPE_S8 :
2074 (t->bits[c] == 16 ? TYPE_S16 : TYPE_S32));
2075 }
2076 return TYPE_NONE;
2077 }
2078
2079 static DataType
2080 getDestType(const ImgType type) {
2081 switch (type) {
2082 case FLOAT:
2083 case UNORM:
2084 case SNORM:
2085 return TYPE_F32;
2086 case UINT:
2087 return TYPE_U32;
2088 case SINT:
2089 return TYPE_S32;
2090 default:
2091 assert(!"Impossible type");
2092 return TYPE_NONE;
2093 }
2094 }
2095
2096 void
2097 NVC0LoweringPass::convertSurfaceFormat(TexInstruction *su)
2098 {
2099 const TexInstruction::ImgFormatDesc *format = su->tex.format;
2100 int width = format->bits[0] + format->bits[1] +
2101 format->bits[2] + format->bits[3];
2102 Value *untypedDst[4] = {};
2103 Value *typedDst[4] = {};
2104
2105 // We must convert this to a generic load.
2106 su->op = OP_SULDB;
2107
2108 su->dType = typeOfSize(width / 8);
2109 su->sType = TYPE_U8;
2110
2111 for (int i = 0; i < width / 32; i++)
2112 untypedDst[i] = bld.getSSA();
2113 if (width < 32)
2114 untypedDst[0] = bld.getSSA();
2115
2116 for (int i = 0; i < 4; i++) {
2117 typedDst[i] = su->getDef(i);
2118 }
2119
2120 // Set the untyped dsts as the su's destinations
2121 for (int i = 0; i < 4; i++)
2122 su->setDef(i, untypedDst[i]);
2123
2124 bld.setPosition(su, true);
2125
2126 // Unpack each component into the typed dsts
2127 int bits = 0;
2128 for (int i = 0; i < 4; bits += format->bits[i], i++) {
2129 if (!typedDst[i])
2130 continue;
2131 if (i >= format->components) {
2132 if (format->type == FLOAT ||
2133 format->type == UNORM ||
2134 format->type == SNORM)
2135 bld.loadImm(typedDst[i], i == 3 ? 1.0f : 0.0f);
2136 else
2137 bld.loadImm(typedDst[i], i == 3 ? 1 : 0);
2138 continue;
2139 }
2140
2141 // Get just that component's data into the relevant place
2142 if (format->bits[i] == 32)
2143 bld.mkMov(typedDst[i], untypedDst[i]);
2144 else if (format->bits[i] == 16)
2145 bld.mkCvt(OP_CVT, getDestType(format->type), typedDst[i],
2146 getSrcType(format, i), untypedDst[i / 2])
2147 ->subOp = (i & 1) << (format->type == FLOAT ? 0 : 1);
2148 else if (format->bits[i] == 8)
2149 bld.mkCvt(OP_CVT, getDestType(format->type), typedDst[i],
2150 getSrcType(format, i), untypedDst[0])->subOp = i;
2151 else {
2152 bld.mkOp2(OP_EXTBF, TYPE_U32, typedDst[i], untypedDst[bits / 32],
2153 bld.mkImm((bits % 32) | (format->bits[i] << 8)));
2154 if (format->type == UNORM || format->type == SNORM)
2155 bld.mkCvt(OP_CVT, TYPE_F32, typedDst[i], getSrcType(format, i), typedDst[i]);
2156 }
2157
2158 // Normalize / convert as necessary
2159 if (format->type == UNORM)
2160 bld.mkOp2(OP_MUL, TYPE_F32, typedDst[i], typedDst[i], bld.loadImm(NULL, 1.0f / ((1 << format->bits[i]) - 1)));
2161 else if (format->type == SNORM)
2162 bld.mkOp2(OP_MUL, TYPE_F32, typedDst[i], typedDst[i], bld.loadImm(NULL, 1.0f / ((1 << (format->bits[i] - 1)) - 1)));
2163 else if (format->type == FLOAT && format->bits[i] < 16) {
2164 bld.mkOp2(OP_SHL, TYPE_U32, typedDst[i], typedDst[i], bld.loadImm(NULL, 15 - format->bits[i]));
2165 bld.mkCvt(OP_CVT, TYPE_F32, typedDst[i], TYPE_F16, typedDst[i]);
2166 }
2167 }
2168
2169 if (format->bgra) {
2170 std::swap(typedDst[0], typedDst[2]);
2171 }
2172 }
2173
2174 void
2175 NVC0LoweringPass::handleSurfaceOpNVE4(TexInstruction *su)
2176 {
2177 processSurfaceCoordsNVE4(su);
2178
2179 if (su->op == OP_SULDP)
2180 convertSurfaceFormat(su);
2181
2182 if (su->op == OP_SUREDB || su->op == OP_SUREDP) {
2183 assert(su->getPredicate());
2184 Value *pred =
2185 bld.mkOp2v(OP_OR, TYPE_U8, bld.getScratch(1, FILE_PREDICATE),
2186 su->getPredicate(), su->getSrc(2));
2187
2188 Instruction *red = bld.mkOp(OP_ATOM, su->dType, bld.getSSA());
2189 red->subOp = su->subOp;
2190 red->setSrc(0, bld.mkSymbol(FILE_MEMORY_GLOBAL, 0, TYPE_U32, 0));
2191 red->setSrc(1, su->getSrc(3));
2192 if (su->subOp == NV50_IR_SUBOP_ATOM_CAS)
2193 red->setSrc(2, su->getSrc(4));
2194 red->setIndirect(0, 0, su->getSrc(0));
2195
2196 // make sure to initialize dst value when the atomic operation is not
2197 // performed
2198 Instruction *mov = bld.mkMov(bld.getSSA(), bld.loadImm(NULL, 0));
2199
2200 assert(su->cc == CC_NOT_P);
2201 red->setPredicate(su->cc, pred);
2202 mov->setPredicate(CC_P, pred);
2203
2204 bld.mkOp2(OP_UNION, TYPE_U32, su->getDef(0),
2205 red->getDef(0), mov->getDef(0));
2206
2207 delete_Instruction(bld.getProgram(), su);
2208 handleCasExch(red, true);
2209 }
2210
2211 if (su->op == OP_SUSTB || su->op == OP_SUSTP)
2212 su->sType = (su->tex.target == TEX_TARGET_BUFFER) ? TYPE_U32 : TYPE_U8;
2213 }
2214
2215 void
2216 NVC0LoweringPass::processSurfaceCoordsNVC0(TexInstruction *su)
2217 {
2218 const int slot = su->tex.r;
2219 const int dim = su->tex.target.getDim();
2220 const int arg = dim + (su->tex.target.isArray() || su->tex.target.isCube());
2221 int c;
2222 Value *zero = bld.mkImm(0);
2223 Value *src[3];
2224 Value *v;
2225 Value *ind = su->getIndirectR();
2226
2227 bld.setPosition(su, false);
2228
2229 adjustCoordinatesMS(su);
2230
2231 if (ind) {
2232 Value *ptr;
2233 ptr = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getSSA(), ind, bld.mkImm(su->tex.r));
2234 ptr = bld.mkOp2v(OP_AND, TYPE_U32, bld.getSSA(), ptr, bld.mkImm(7));
2235 su->setIndirectR(ptr);
2236 }
2237
2238 // get surface coordinates
2239 for (c = 0; c < arg; ++c)
2240 src[c] = su->getSrc(c);
2241 for (; c < 3; ++c)
2242 src[c] = zero;
2243
2244 // calculate pixel offset
2245 if (su->op == OP_SULDP || su->op == OP_SUREDP) {
2246 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_BSIZE);
2247 su->setSrc(0, bld.mkOp2v(OP_MUL, TYPE_U32, bld.getSSA(), src[0], v));
2248 }
2249
2250 // add array layer offset
2251 if (su->tex.target.isArray() || su->tex.target.isCube()) {
2252 v = loadSuInfo32(ind, slot, NVC0_SU_INFO_ARRAY);
2253 assert(dim > 1);
2254 su->setSrc(2, bld.mkOp2v(OP_MUL, TYPE_U32, bld.getSSA(), src[2], v));
2255 }
2256
2257 // prevent read fault when the image is not actually bound
2258 CmpInstruction *pred =
2259 bld.mkCmp(OP_SET, CC_EQ, TYPE_U32, bld.getSSA(1, FILE_PREDICATE),
2260 TYPE_U32, bld.mkImm(0),
2261 loadSuInfo32(ind, slot, NVC0_SU_INFO_ADDR));
2262 if (su->op != OP_SUSTP && su->tex.format) {
2263 const TexInstruction::ImgFormatDesc *format = su->tex.format;
2264 int blockwidth = format->bits[0] + format->bits[1] +
2265 format->bits[2] + format->bits[3];
2266
2267 assert(format->components != 0);
2268 // make sure that the format doesn't mismatch when it's not FMT_NONE
2269 bld.mkCmp(OP_SET_OR, CC_NE, TYPE_U32, pred->getDef(0),
2270 TYPE_U32, bld.loadImm(NULL, blockwidth / 8),
2271 loadSuInfo32(ind, slot, NVC0_SU_INFO_BSIZE),
2272 pred->getDef(0));
2273 }
2274 su->setPredicate(CC_NOT_P, pred->getDef(0));
2275 }
2276
2277 void
2278 NVC0LoweringPass::handleSurfaceOpNVC0(TexInstruction *su)
2279 {
2280 if (su->tex.target == TEX_TARGET_1D_ARRAY) {
2281 /* As 1d arrays also need 3 coordinates, switching to TEX_TARGET_2D_ARRAY
2282 * will simplify the lowering pass and the texture constraints. */
2283 su->moveSources(1, 1);
2284 su->setSrc(1, bld.loadImm(NULL, 0));
2285 su->tex.target = TEX_TARGET_2D_ARRAY;
2286 }
2287
2288 processSurfaceCoordsNVC0(su);
2289
2290 if (su->op == OP_SULDP)
2291 convertSurfaceFormat(su);
2292
2293 if (su->op == OP_SUREDB || su->op == OP_SUREDP) {
2294 const int dim = su->tex.target.getDim();
2295 const int arg = dim + (su->tex.target.isArray() || su->tex.target.isCube());
2296 LValue *addr = bld.getSSA(8);
2297 Value *def = su->getDef(0);
2298
2299 su->op = OP_SULEA;
2300
2301 // Set the destination to the address
2302 su->dType = TYPE_U64;
2303 su->setDef(0, addr);
2304 su->setDef(1, su->getPredicate());
2305
2306 bld.setPosition(su, true);
2307
2308 // Perform the atomic op
2309 Instruction *red = bld.mkOp(OP_ATOM, su->sType, bld.getSSA());
2310 red->subOp = su->subOp;
2311 red->setSrc(0, bld.mkSymbol(FILE_MEMORY_GLOBAL, 0, su->sType, 0));
2312 red->setSrc(1, su->getSrc(arg));
2313 if (red->subOp == NV50_IR_SUBOP_ATOM_CAS)
2314 red->setSrc(2, su->getSrc(arg + 1));
2315 red->setIndirect(0, 0, addr);
2316
2317 // make sure to initialize dst value when the atomic operation is not
2318 // performed
2319 Instruction *mov = bld.mkMov(bld.getSSA(), bld.loadImm(NULL, 0));
2320
2321 assert(su->cc == CC_NOT_P);
2322 red->setPredicate(su->cc, su->getPredicate());
2323 mov->setPredicate(CC_P, su->getPredicate());
2324
2325 bld.mkOp2(OP_UNION, TYPE_U32, def, red->getDef(0), mov->getDef(0));
2326
2327 handleCasExch(red, false);
2328 }
2329 }
2330
2331 void
2332 NVC0LoweringPass::processSurfaceCoordsGM107(TexInstruction *su)
2333 {
2334 const int slot = su->tex.r;
2335 const int dim = su->tex.target.getDim();
2336 const int arg = dim + (su->tex.target.isArray() || su->tex.target.isCube());
2337 Value *ind = su->getIndirectR();
2338 int pos = 0;
2339
2340 bld.setPosition(su, false);
2341
2342 // add texture handle
2343 switch (su->op) {
2344 case OP_SUSTP:
2345 pos = 4;
2346 break;
2347 case OP_SUREDP:
2348 pos = (su->subOp == NV50_IR_SUBOP_ATOM_CAS) ? 2 : 1;
2349 break;
2350 default:
2351 assert(pos == 0);
2352 break;
2353 }
2354 su->setSrc(arg + pos, loadTexHandle(ind, slot + 32));
2355
2356 // prevent read fault when the image is not actually bound
2357 CmpInstruction *pred =
2358 bld.mkCmp(OP_SET, CC_EQ, TYPE_U32, bld.getSSA(1, FILE_PREDICATE),
2359 TYPE_U32, bld.mkImm(0),
2360 loadSuInfo32(ind, slot, NVC0_SU_INFO_ADDR));
2361 if (su->op != OP_SUSTP && su->tex.format) {
2362 const TexInstruction::ImgFormatDesc *format = su->tex.format;
2363 int blockwidth = format->bits[0] + format->bits[1] +
2364 format->bits[2] + format->bits[3];
2365
2366 assert(format->components != 0);
2367 // make sure that the format doesn't mismatch when it's not FMT_NONE
2368 bld.mkCmp(OP_SET_OR, CC_NE, TYPE_U32, pred->getDef(0),
2369 TYPE_U32, bld.loadImm(NULL, blockwidth / 8),
2370 loadSuInfo32(ind, slot, NVC0_SU_INFO_BSIZE),
2371 pred->getDef(0));
2372 }
2373 su->setPredicate(CC_NOT_P, pred->getDef(0));
2374 }
2375
2376 void
2377 NVC0LoweringPass::handleSurfaceOpGM107(TexInstruction *su)
2378 {
2379 processSurfaceCoordsGM107(su);
2380
2381 if (su->op == OP_SULDP)
2382 convertSurfaceFormat(su);
2383
2384 if (su->op == OP_SUREDP) {
2385 Value *def = su->getDef(0);
2386
2387 su->op = OP_SUREDB;
2388 su->setDef(0, bld.getSSA());
2389
2390 bld.setPosition(su, true);
2391
2392 // make sure to initialize dst value when the atomic operation is not
2393 // performed
2394 Instruction *mov = bld.mkMov(bld.getSSA(), bld.loadImm(NULL, 0));
2395
2396 assert(su->cc == CC_NOT_P);
2397 mov->setPredicate(CC_P, su->getPredicate());
2398
2399 bld.mkOp2(OP_UNION, TYPE_U32, def, su->getDef(0), mov->getDef(0));
2400 }
2401 }
2402
2403 bool
2404 NVC0LoweringPass::handleWRSV(Instruction *i)
2405 {
2406 Instruction *st;
2407 Symbol *sym;
2408 uint32_t addr;
2409
2410 // must replace, $sreg are not writeable
2411 addr = targ->getSVAddress(FILE_SHADER_OUTPUT, i->getSrc(0)->asSym());
2412 if (addr >= 0x400)
2413 return false;
2414 sym = bld.mkSymbol(FILE_SHADER_OUTPUT, 0, i->sType, addr);
2415
2416 st = bld.mkStore(OP_EXPORT, i->dType, sym, i->getIndirect(0, 0),
2417 i->getSrc(1));
2418 st->perPatch = i->perPatch;
2419
2420 bld.getBB()->remove(i);
2421 return true;
2422 }
2423
2424 void
2425 NVC0LoweringPass::handleLDST(Instruction *i)
2426 {
2427 if (i->src(0).getFile() == FILE_SHADER_INPUT) {
2428 if (prog->getType() == Program::TYPE_COMPUTE) {
2429 i->getSrc(0)->reg.file = FILE_MEMORY_CONST;
2430 i->getSrc(0)->reg.fileIndex = 0;
2431 } else
2432 if (prog->getType() == Program::TYPE_GEOMETRY &&
2433 i->src(0).isIndirect(0)) {
2434 // XXX: this assumes vec4 units
2435 Value *ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
2436 i->getIndirect(0, 0), bld.mkImm(4));
2437 i->setIndirect(0, 0, ptr);
2438 i->op = OP_VFETCH;
2439 } else {
2440 i->op = OP_VFETCH;
2441 assert(prog->getType() != Program::TYPE_FRAGMENT); // INTERP
2442 }
2443 } else if (i->src(0).getFile() == FILE_MEMORY_CONST) {
2444 if (targ->getChipset() >= NVISA_GK104_CHIPSET &&
2445 prog->getType() == Program::TYPE_COMPUTE) {
2446 // The launch descriptor only allows to set up 8 CBs, but OpenGL
2447 // requires at least 12 UBOs. To bypass this limitation, we store the
2448 // addrs into the driver constbuf and we directly load from the global
2449 // memory.
2450 int8_t fileIndex = i->getSrc(0)->reg.fileIndex - 1;
2451 Value *ind = i->getIndirect(0, 1);
2452
2453 if (!ind && fileIndex == -1)
2454 return;
2455
2456 if (ind) {
2457 // Clamp the UBO index when an indirect access is used to avoid
2458 // loading information from the wrong place in the driver cb.
2459 // TODO - synchronize the max with the driver.
2460 ind = bld.mkOp2v(OP_MIN, TYPE_U32, ind,
2461 bld.mkOp2v(OP_ADD, TYPE_U32, bld.getSSA(),
2462 ind, bld.loadImm(NULL, fileIndex)),
2463 bld.loadImm(NULL, 13));
2464 fileIndex = 0;
2465 }
2466
2467 Value *offset = bld.loadImm(NULL, i->getSrc(0)->reg.data.offset + typeSizeof(i->sType));
2468 Value *ptr = loadUboInfo64(ind, fileIndex * 16);
2469 Value *length = loadUboLength32(ind, fileIndex * 16);
2470 Value *pred = new_LValue(func, FILE_PREDICATE);
2471 if (i->src(0).isIndirect(0)) {
2472 bld.mkOp2(OP_ADD, TYPE_U64, ptr, ptr, i->getIndirect(0, 0));
2473 bld.mkOp2(OP_ADD, TYPE_U32, offset, offset, i->getIndirect(0, 0));
2474 }
2475 i->getSrc(0)->reg.file = FILE_MEMORY_GLOBAL;
2476 i->setIndirect(0, 1, NULL);
2477 i->setIndirect(0, 0, ptr);
2478 bld.mkCmp(OP_SET, CC_GT, TYPE_U32, pred, TYPE_U32, offset, length);
2479 i->setPredicate(CC_NOT_P, pred);
2480 Value *zero, *dst = i->getDef(0);
2481 i->setDef(0, bld.getSSA());
2482
2483 bld.setPosition(i, true);
2484 bld.mkMov((zero = bld.getSSA()), bld.mkImm(0))
2485 ->setPredicate(CC_P, pred);
2486 bld.mkOp2(OP_UNION, TYPE_U32, dst, i->getDef(0), zero);
2487 } else if (i->src(0).isIndirect(1)) {
2488 Value *ptr;
2489 if (i->src(0).isIndirect(0))
2490 ptr = bld.mkOp3v(OP_INSBF, TYPE_U32, bld.getSSA(),
2491 i->getIndirect(0, 1), bld.mkImm(0x1010),
2492 i->getIndirect(0, 0));
2493 else
2494 ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
2495 i->getIndirect(0, 1), bld.mkImm(16));
2496 i->setIndirect(0, 1, NULL);
2497 i->setIndirect(0, 0, ptr);
2498 i->subOp = NV50_IR_SUBOP_LDC_IS;
2499 }
2500 } else if (i->src(0).getFile() == FILE_SHADER_OUTPUT) {
2501 assert(prog->getType() == Program::TYPE_TESSELLATION_CONTROL);
2502 i->op = OP_VFETCH;
2503 } else if (i->src(0).getFile() == FILE_MEMORY_BUFFER) {
2504 Value *ind = i->getIndirect(0, 1);
2505 Value *ptr = loadBufInfo64(ind, i->getSrc(0)->reg.fileIndex * 16);
2506 // XXX come up with a way not to do this for EVERY little access but
2507 // rather to batch these up somehow. Unfortunately we've lost the
2508 // information about the field width by the time we get here.
2509 Value *offset = bld.loadImm(NULL, i->getSrc(0)->reg.data.offset + typeSizeof(i->sType));
2510 Value *length = loadBufLength32(ind, i->getSrc(0)->reg.fileIndex * 16);
2511 Value *pred = new_LValue(func, FILE_PREDICATE);
2512 if (i->src(0).isIndirect(0)) {
2513 bld.mkOp2(OP_ADD, TYPE_U64, ptr, ptr, i->getIndirect(0, 0));
2514 bld.mkOp2(OP_ADD, TYPE_U32, offset, offset, i->getIndirect(0, 0));
2515 }
2516 i->setIndirect(0, 1, NULL);
2517 i->setIndirect(0, 0, ptr);
2518 i->getSrc(0)->reg.file = FILE_MEMORY_GLOBAL;
2519 bld.mkCmp(OP_SET, CC_GT, TYPE_U32, pred, TYPE_U32, offset, length);
2520 i->setPredicate(CC_NOT_P, pred);
2521 if (i->defExists(0)) {
2522 Value *zero, *dst = i->getDef(0);
2523 i->setDef(0, bld.getSSA());
2524
2525 bld.setPosition(i, true);
2526 bld.mkMov((zero = bld.getSSA()), bld.mkImm(0))
2527 ->setPredicate(CC_P, pred);
2528 bld.mkOp2(OP_UNION, TYPE_U32, dst, i->getDef(0), zero);
2529 }
2530 }
2531 }
2532
2533 void
2534 NVC0LoweringPass::readTessCoord(LValue *dst, int c)
2535 {
2536 Value *laneid = bld.getSSA();
2537 Value *x, *y;
2538
2539 bld.mkOp1(OP_RDSV, TYPE_U32, laneid, bld.mkSysVal(SV_LANEID, 0));
2540
2541 if (c == 0) {
2542 x = dst;
2543 y = NULL;
2544 } else
2545 if (c == 1) {
2546 x = NULL;
2547 y = dst;
2548 } else {
2549 assert(c == 2);
2550 if (prog->driver->prop.tp.domain != PIPE_PRIM_TRIANGLES) {
2551 bld.mkMov(dst, bld.loadImm(NULL, 0));
2552 return;
2553 }
2554 x = bld.getSSA();
2555 y = bld.getSSA();
2556 }
2557 if (x)
2558 bld.mkFetch(x, TYPE_F32, FILE_SHADER_OUTPUT, 0x2f0, NULL, laneid);
2559 if (y)
2560 bld.mkFetch(y, TYPE_F32, FILE_SHADER_OUTPUT, 0x2f4, NULL, laneid);
2561
2562 if (c == 2) {
2563 bld.mkOp2(OP_ADD, TYPE_F32, dst, x, y);
2564 bld.mkOp2(OP_SUB, TYPE_F32, dst, bld.loadImm(NULL, 1.0f), dst);
2565 }
2566 }
2567
2568 bool
2569 NVC0LoweringPass::handleRDSV(Instruction *i)
2570 {
2571 Symbol *sym = i->getSrc(0)->asSym();
2572 const SVSemantic sv = sym->reg.data.sv.sv;
2573 Value *vtx = NULL;
2574 Instruction *ld;
2575 uint32_t addr = targ->getSVAddress(FILE_SHADER_INPUT, sym);
2576
2577 if (addr >= 0x400) {
2578 // mov $sreg
2579 if (sym->reg.data.sv.index == 3) {
2580 // TGSI backend may use 4th component of TID,NTID,CTAID,NCTAID
2581 i->op = OP_MOV;
2582 i->setSrc(0, bld.mkImm((sv == SV_NTID || sv == SV_NCTAID) ? 1 : 0));
2583 }
2584 if (sv == SV_VERTEX_COUNT) {
2585 bld.setPosition(i, true);
2586 bld.mkOp2(OP_EXTBF, TYPE_U32, i->getDef(0), i->getDef(0), bld.mkImm(0x808));
2587 }
2588 return true;
2589 }
2590
2591 switch (sv) {
2592 case SV_POSITION:
2593 assert(prog->getType() == Program::TYPE_FRAGMENT);
2594 if (i->srcExists(1)) {
2595 // Pass offset through to the interpolation logic
2596 ld = bld.mkInterp(NV50_IR_INTERP_LINEAR | NV50_IR_INTERP_OFFSET,
2597 i->getDef(0), addr, NULL);
2598 ld->setSrc(1, i->getSrc(1));
2599 } else {
2600 bld.mkInterp(NV50_IR_INTERP_LINEAR, i->getDef(0), addr, NULL);
2601 }
2602 break;
2603 case SV_FACE:
2604 {
2605 Value *face = i->getDef(0);
2606 bld.mkInterp(NV50_IR_INTERP_FLAT, face, addr, NULL);
2607 if (i->dType == TYPE_F32) {
2608 bld.mkOp2(OP_OR, TYPE_U32, face, face, bld.mkImm(0x00000001));
2609 bld.mkOp1(OP_NEG, TYPE_S32, face, face);
2610 bld.mkCvt(OP_CVT, TYPE_F32, face, TYPE_S32, face);
2611 }
2612 }
2613 break;
2614 case SV_TESS_COORD:
2615 assert(prog->getType() == Program::TYPE_TESSELLATION_EVAL);
2616 readTessCoord(i->getDef(0)->asLValue(), i->getSrc(0)->reg.data.sv.index);
2617 break;
2618 case SV_NTID:
2619 case SV_NCTAID:
2620 case SV_GRIDID:
2621 assert(targ->getChipset() >= NVISA_GK104_CHIPSET); // mov $sreg otherwise
2622 if (sym->reg.data.sv.index == 3) {
2623 i->op = OP_MOV;
2624 i->setSrc(0, bld.mkImm(sv == SV_GRIDID ? 0 : 1));
2625 return true;
2626 }
2627 // Fallthrough
2628 case SV_WORK_DIM:
2629 addr += prog->driver->prop.cp.gridInfoBase;
2630 bld.mkLoad(TYPE_U32, i->getDef(0),
2631 bld.mkSymbol(FILE_MEMORY_CONST, prog->driver->io.auxCBSlot,
2632 TYPE_U32, addr), NULL);
2633 break;
2634 case SV_SAMPLE_INDEX:
2635 // TODO: Properly pass source as an address in the PIX address space
2636 // (which can be of the form [r0+offset]). But this is currently
2637 // unnecessary.
2638 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
2639 ld->subOp = NV50_IR_SUBOP_PIXLD_SAMPLEID;
2640 break;
2641 case SV_SAMPLE_POS: {
2642 Value *off = new_LValue(func, FILE_GPR);
2643 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
2644 ld->subOp = NV50_IR_SUBOP_PIXLD_SAMPLEID;
2645 bld.mkOp2(OP_SHL, TYPE_U32, off, i->getDef(0), bld.mkImm(3));
2646 bld.mkLoad(TYPE_F32,
2647 i->getDef(0),
2648 bld.mkSymbol(
2649 FILE_MEMORY_CONST, prog->driver->io.auxCBSlot,
2650 TYPE_U32, prog->driver->io.sampleInfoBase +
2651 4 * sym->reg.data.sv.index),
2652 off);
2653 break;
2654 }
2655 case SV_SAMPLE_MASK: {
2656 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
2657 ld->subOp = NV50_IR_SUBOP_PIXLD_COVMASK;
2658 Instruction *sampleid =
2659 bld.mkOp1(OP_PIXLD, TYPE_U32, bld.getSSA(), bld.mkImm(0));
2660 sampleid->subOp = NV50_IR_SUBOP_PIXLD_SAMPLEID;
2661 Value *masked =
2662 bld.mkOp2v(OP_AND, TYPE_U32, bld.getSSA(), ld->getDef(0),
2663 bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
2664 bld.loadImm(NULL, 1), sampleid->getDef(0)));
2665 if (prog->driver->prop.fp.persampleInvocation) {
2666 bld.mkMov(i->getDef(0), masked);
2667 } else {
2668 bld.mkOp3(OP_SELP, TYPE_U32, i->getDef(0), ld->getDef(0), masked,
2669 bld.mkImm(0))
2670 ->subOp = 1;
2671 }
2672 break;
2673 }
2674 case SV_BASEVERTEX:
2675 case SV_BASEINSTANCE:
2676 case SV_DRAWID:
2677 ld = bld.mkLoad(TYPE_U32, i->getDef(0),
2678 bld.mkSymbol(FILE_MEMORY_CONST,
2679 prog->driver->io.auxCBSlot,
2680 TYPE_U32,
2681 prog->driver->io.drawInfoBase +
2682 4 * (sv - SV_BASEVERTEX)),
2683 NULL);
2684 break;
2685 default:
2686 if (prog->getType() == Program::TYPE_TESSELLATION_EVAL && !i->perPatch)
2687 vtx = bld.mkOp1v(OP_PFETCH, TYPE_U32, bld.getSSA(), bld.mkImm(0));
2688 if (prog->getType() == Program::TYPE_FRAGMENT) {
2689 bld.mkInterp(NV50_IR_INTERP_FLAT, i->getDef(0), addr, NULL);
2690 } else {
2691 ld = bld.mkFetch(i->getDef(0), i->dType,
2692 FILE_SHADER_INPUT, addr, i->getIndirect(0, 0), vtx);
2693 ld->perPatch = i->perPatch;
2694 }
2695 break;
2696 }
2697 bld.getBB()->remove(i);
2698 return true;
2699 }
2700
2701 bool
2702 NVC0LoweringPass::handleDIV(Instruction *i)
2703 {
2704 if (!isFloatType(i->dType))
2705 return true;
2706 bld.setPosition(i, false);
2707 Instruction *rcp = bld.mkOp1(OP_RCP, i->dType, bld.getSSA(typeSizeof(i->dType)), i->getSrc(1));
2708 i->op = OP_MUL;
2709 i->setSrc(1, rcp->getDef(0));
2710 return true;
2711 }
2712
2713 bool
2714 NVC0LoweringPass::handleMOD(Instruction *i)
2715 {
2716 if (!isFloatType(i->dType))
2717 return true;
2718 LValue *value = bld.getScratch(typeSizeof(i->dType));
2719 bld.mkOp1(OP_RCP, i->dType, value, i->getSrc(1));
2720 bld.mkOp2(OP_MUL, i->dType, value, i->getSrc(0), value);
2721 bld.mkOp1(OP_TRUNC, i->dType, value, value);
2722 bld.mkOp2(OP_MUL, i->dType, value, i->getSrc(1), value);
2723 i->op = OP_SUB;
2724 i->setSrc(1, value);
2725 return true;
2726 }
2727
2728 bool
2729 NVC0LoweringPass::handleSQRT(Instruction *i)
2730 {
2731 if (i->dType == TYPE_F64) {
2732 Value *pred = bld.getSSA(1, FILE_PREDICATE);
2733 Value *zero = bld.loadImm(NULL, 0.0);
2734 Value *dst = bld.getSSA(8);
2735 bld.mkOp1(OP_RSQ, i->dType, dst, i->getSrc(0));
2736 bld.mkCmp(OP_SET, CC_LE, i->dType, pred, i->dType, i->getSrc(0), zero);
2737 bld.mkOp3(OP_SELP, TYPE_U64, dst, zero, dst, pred);
2738 i->op = OP_MUL;
2739 i->setSrc(1, dst);
2740 // TODO: Handle this properly with a library function
2741 } else {
2742 bld.setPosition(i, true);
2743 i->op = OP_RSQ;
2744 bld.mkOp1(OP_RCP, i->dType, i->getDef(0), i->getDef(0));
2745 }
2746
2747 return true;
2748 }
2749
2750 bool
2751 NVC0LoweringPass::handlePOW(Instruction *i)
2752 {
2753 LValue *val = bld.getScratch();
2754
2755 bld.mkOp1(OP_LG2, TYPE_F32, val, i->getSrc(0));
2756 bld.mkOp2(OP_MUL, TYPE_F32, val, i->getSrc(1), val)->dnz = 1;
2757 bld.mkOp1(OP_PREEX2, TYPE_F32, val, val);
2758
2759 i->op = OP_EX2;
2760 i->setSrc(0, val);
2761 i->setSrc(1, NULL);
2762
2763 return true;
2764 }
2765
2766 bool
2767 NVC0LoweringPass::handleEXPORT(Instruction *i)
2768 {
2769 if (prog->getType() == Program::TYPE_FRAGMENT) {
2770 int id = i->getSrc(0)->reg.data.offset / 4;
2771
2772 if (i->src(0).isIndirect(0)) // TODO, ugly
2773 return false;
2774 i->op = OP_MOV;
2775 i->subOp = NV50_IR_SUBOP_MOV_FINAL;
2776 i->src(0).set(i->src(1));
2777 i->setSrc(1, NULL);
2778 i->setDef(0, new_LValue(func, FILE_GPR));
2779 i->getDef(0)->reg.data.id = id;
2780
2781 prog->maxGPR = MAX2(prog->maxGPR, id);
2782 } else
2783 if (prog->getType() == Program::TYPE_GEOMETRY) {
2784 i->setIndirect(0, 1, gpEmitAddress);
2785 }
2786 return true;
2787 }
2788
2789 bool
2790 NVC0LoweringPass::handleOUT(Instruction *i)
2791 {
2792 Instruction *prev = i->prev;
2793 ImmediateValue stream, prevStream;
2794
2795 // Only merge if the stream ids match. Also, note that the previous
2796 // instruction would have already been lowered, so we take arg1 from it.
2797 if (i->op == OP_RESTART && prev && prev->op == OP_EMIT &&
2798 i->src(0).getImmediate(stream) &&
2799 prev->src(1).getImmediate(prevStream) &&
2800 stream.reg.data.u32 == prevStream.reg.data.u32) {
2801 i->prev->subOp = NV50_IR_SUBOP_EMIT_RESTART;
2802 delete_Instruction(prog, i);
2803 } else {
2804 assert(gpEmitAddress);
2805 i->setDef(0, gpEmitAddress);
2806 i->setSrc(1, i->getSrc(0));
2807 i->setSrc(0, gpEmitAddress);
2808 }
2809 return true;
2810 }
2811
2812 // Generate a binary predicate if an instruction is predicated by
2813 // e.g. an f32 value.
2814 void
2815 NVC0LoweringPass::checkPredicate(Instruction *insn)
2816 {
2817 Value *pred = insn->getPredicate();
2818 Value *pdst;
2819
2820 if (!pred || pred->reg.file == FILE_PREDICATE)
2821 return;
2822 pdst = new_LValue(func, FILE_PREDICATE);
2823
2824 // CAUTION: don't use pdst->getInsn, the definition might not be unique,
2825 // delay turning PSET(FSET(x,y),0) into PSET(x,y) to a later pass
2826
2827 bld.mkCmp(OP_SET, CC_NEU, insn->dType, pdst, insn->dType, bld.mkImm(0), pred);
2828
2829 insn->setPredicate(insn->cc, pdst);
2830 }
2831
2832 //
2833 // - add quadop dance for texturing
2834 // - put FP outputs in GPRs
2835 // - convert instruction sequences
2836 //
2837 bool
2838 NVC0LoweringPass::visit(Instruction *i)
2839 {
2840 bool ret = true;
2841 bld.setPosition(i, false);
2842
2843 if (i->cc != CC_ALWAYS)
2844 checkPredicate(i);
2845
2846 switch (i->op) {
2847 case OP_TEX:
2848 case OP_TXB:
2849 case OP_TXL:
2850 case OP_TXF:
2851 case OP_TXG:
2852 return handleTEX(i->asTex());
2853 case OP_TXD:
2854 return handleTXD(i->asTex());
2855 case OP_TXLQ:
2856 return handleTXLQ(i->asTex());
2857 case OP_TXQ:
2858 return handleTXQ(i->asTex());
2859 case OP_EX2:
2860 bld.mkOp1(OP_PREEX2, TYPE_F32, i->getDef(0), i->getSrc(0));
2861 i->setSrc(0, i->getDef(0));
2862 break;
2863 case OP_POW:
2864 return handlePOW(i);
2865 case OP_DIV:
2866 return handleDIV(i);
2867 case OP_MOD:
2868 return handleMOD(i);
2869 case OP_SQRT:
2870 return handleSQRT(i);
2871 case OP_EXPORT:
2872 ret = handleEXPORT(i);
2873 break;
2874 case OP_EMIT:
2875 case OP_RESTART:
2876 return handleOUT(i);
2877 case OP_RDSV:
2878 return handleRDSV(i);
2879 case OP_WRSV:
2880 return handleWRSV(i);
2881 case OP_STORE:
2882 case OP_LOAD:
2883 handleLDST(i);
2884 break;
2885 case OP_ATOM:
2886 {
2887 const bool cctl = i->src(0).getFile() == FILE_MEMORY_BUFFER;
2888 handleATOM(i);
2889 handleCasExch(i, cctl);
2890 }
2891 break;
2892 case OP_SULDB:
2893 case OP_SULDP:
2894 case OP_SUSTB:
2895 case OP_SUSTP:
2896 case OP_SUREDB:
2897 case OP_SUREDP:
2898 if (targ->getChipset() >= NVISA_GM107_CHIPSET)
2899 handleSurfaceOpGM107(i->asTex());
2900 else if (targ->getChipset() >= NVISA_GK104_CHIPSET)
2901 handleSurfaceOpNVE4(i->asTex());
2902 else
2903 handleSurfaceOpNVC0(i->asTex());
2904 break;
2905 case OP_SUQ:
2906 handleSUQ(i->asTex());
2907 break;
2908 case OP_BUFQ:
2909 handleBUFQ(i);
2910 break;
2911 default:
2912 break;
2913 }
2914
2915 /* Kepler+ has a special opcode to compute a new base address to be used
2916 * for indirect loads.
2917 *
2918 * Maxwell+ has an additional similar requirement for indirect
2919 * interpolation ops in frag shaders.
2920 */
2921 bool doAfetch = false;
2922 if (targ->getChipset() >= NVISA_GK104_CHIPSET &&
2923 !i->perPatch &&
2924 (i->op == OP_VFETCH || i->op == OP_EXPORT) &&
2925 i->src(0).isIndirect(0)) {
2926 doAfetch = true;
2927 }
2928 if (targ->getChipset() >= NVISA_GM107_CHIPSET &&
2929 (i->op == OP_LINTERP || i->op == OP_PINTERP) &&
2930 i->src(0).isIndirect(0)) {
2931 doAfetch = true;
2932 }
2933
2934 if (doAfetch) {
2935 Value *addr = cloneShallow(func, i->getSrc(0));
2936 Instruction *afetch = bld.mkOp1(OP_AFETCH, TYPE_U32, bld.getSSA(),
2937 i->getSrc(0));
2938 afetch->setIndirect(0, 0, i->getIndirect(0, 0));
2939 addr->reg.data.offset = 0;
2940 i->setSrc(0, addr);
2941 i->setIndirect(0, 0, afetch->getDef(0));
2942 }
2943
2944 return ret;
2945 }
2946
2947 bool
2948 TargetNVC0::runLegalizePass(Program *prog, CGStage stage) const
2949 {
2950 if (stage == CG_STAGE_PRE_SSA) {
2951 NVC0LoweringPass pass(prog);
2952 return pass.run(prog, false, true);
2953 } else
2954 if (stage == CG_STAGE_POST_RA) {
2955 NVC0LegalizePostRA pass(prog);
2956 return pass.run(prog, false, true);
2957 } else
2958 if (stage == CG_STAGE_SSA) {
2959 NVC0LegalizeSSA pass;
2960 return pass.run(prog, false, true);
2961 }
2962 return false;
2963 }
2964
2965 } // namespace nv50_ir