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