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