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