Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 void
190 NVC0LegalizePostRA::findOverwritingDefs(const Instruction *texi,
191 Instruction *insn,
192 const BasicBlock *term,
193 std::list<TexUse> &uses)
194 {
195 while (insn->op == OP_MOV && insn->getDef(0)->equals(insn->getSrc(0)))
196 insn = insn->getSrc(0)->getUniqueInsn();
197
198 // NOTE: the tex itself is, of course, not an overwriting definition
199 if (insn == texi || !insn->bb->reachableBy(texi->bb, term))
200 return;
201
202 switch (insn->op) {
203 /* Values not connected to the tex's definition through any of these should
204 * not be conflicting.
205 */
206 case OP_SPLIT:
207 case OP_MERGE:
208 case OP_PHI:
209 case OP_UNION:
210 /* recurse again */
211 for (int s = 0; insn->srcExists(s); ++s)
212 findOverwritingDefs(texi, insn->getSrc(s)->getUniqueInsn(), term,
213 uses);
214 break;
215 default:
216 // if (!isTextureOp(insn->op)) // TODO: are TEXes always ordered ?
217 addTexUse(uses, insn, texi);
218 break;
219 }
220 }
221
222 void
223 NVC0LegalizePostRA::findFirstUses(
224 const Instruction *texi,
225 const Instruction *insn,
226 std::list<TexUse> &uses,
227 unordered_set<const Instruction *>& visited)
228 {
229 for (int d = 0; insn->defExists(d); ++d) {
230 Value *v = insn->getDef(d);
231 for (Value::UseIterator u = v->uses.begin(); u != v->uses.end(); ++u) {
232 Instruction *usei = (*u)->getInsn();
233
234 // NOTE: In case of a loop that overwrites a value but never uses
235 // it, it can happen that we have a cycle of uses that consists only
236 // of phis and no-op moves and will thus cause an infinite loop here
237 // since these are not considered actual uses.
238 // The most obvious (and perhaps the only) way to prevent this is to
239 // remember which instructions we've already visited.
240
241 if (visited.find(usei) != visited.end())
242 continue;
243
244 visited.insert(usei);
245
246 if (usei->op == OP_PHI || usei->op == OP_UNION) {
247 // need a barrier before WAW cases, like:
248 // %r0 = tex
249 // if ...
250 // texbar <- is required or tex might replace x again
251 // %r1 = x <- overwriting def
252 // %r2 = phi %r0, %r1
253 for (int s = 0; usei->srcExists(s); ++s) {
254 Instruction *defi = usei->getSrc(s)->getUniqueInsn();
255 if (defi && &usei->src(s) != *u)
256 findOverwritingDefs(texi, defi, usei->bb, uses);
257 }
258 }
259
260 if (usei->op == OP_SPLIT ||
261 usei->op == OP_MERGE ||
262 usei->op == OP_PHI ||
263 usei->op == OP_UNION) {
264 // these uses don't manifest in the machine code
265 findFirstUses(texi, usei, uses, visited);
266 } else
267 if (usei->op == OP_MOV && usei->getDef(0)->equals(usei->getSrc(0)) &&
268 usei->subOp != NV50_IR_SUBOP_MOV_FINAL) {
269 findFirstUses(texi, usei, uses, visited);
270 } else {
271 addTexUse(uses, usei, texi);
272 }
273 }
274 }
275 }
276
277 // Texture barriers:
278 // This pass is a bit long and ugly and can probably be optimized.
279 //
280 // 1. obtain a list of TEXes and their outputs' first use(s)
281 // 2. calculate the barrier level of each first use (minimal number of TEXes,
282 // over all paths, between the TEX and the use in question)
283 // 3. for each barrier, if all paths from the source TEX to that barrier
284 // contain a barrier of lesser level, it can be culled
285 bool
286 NVC0LegalizePostRA::insertTextureBarriers(Function *fn)
287 {
288 std::list<TexUse> *uses;
289 std::vector<Instruction *> texes;
290 std::vector<int> bbFirstTex;
291 std::vector<int> bbFirstUse;
292 std::vector<int> texCounts;
293 std::vector<TexUse> useVec;
294 ArrayList insns;
295
296 fn->orderInstructions(insns);
297
298 texCounts.resize(fn->allBBlocks.getSize(), 0);
299 bbFirstTex.resize(fn->allBBlocks.getSize(), insns.getSize());
300 bbFirstUse.resize(fn->allBBlocks.getSize(), insns.getSize());
301
302 // tag BB CFG nodes by their id for later
303 for (ArrayList::Iterator i = fn->allBBlocks.iterator(); !i.end(); i.next()) {
304 BasicBlock *bb = reinterpret_cast<BasicBlock *>(i.get());
305 if (bb)
306 bb->cfg.tag = bb->getId();
307 }
308
309 // gather the first uses for each TEX
310 for (int i = 0; i < insns.getSize(); ++i) {
311 Instruction *tex = reinterpret_cast<Instruction *>(insns.get(i));
312 if (isTextureOp(tex->op)) {
313 texes.push_back(tex);
314 if (!texCounts.at(tex->bb->getId()))
315 bbFirstTex[tex->bb->getId()] = texes.size() - 1;
316 texCounts[tex->bb->getId()]++;
317 }
318 }
319 insns.clear();
320 if (texes.empty())
321 return false;
322 uses = new std::list<TexUse>[texes.size()];
323 if (!uses)
324 return false;
325 for (size_t i = 0; i < texes.size(); ++i) {
326 unordered_set<const Instruction *> visited;
327 findFirstUses(texes[i], texes[i], uses[i], visited);
328 }
329
330 // determine the barrier level at each use
331 for (size_t i = 0; i < texes.size(); ++i) {
332 for (std::list<TexUse>::iterator u = uses[i].begin(); u != uses[i].end();
333 ++u) {
334 BasicBlock *tb = texes[i]->bb;
335 BasicBlock *ub = u->insn->bb;
336 if (tb == ub) {
337 u->level = 0;
338 for (size_t j = i + 1; j < texes.size() &&
339 texes[j]->bb == tb && texes[j]->serial < u->insn->serial;
340 ++j)
341 u->level++;
342 } else {
343 u->level = fn->cfg.findLightestPathWeight(&tb->cfg,
344 &ub->cfg, texCounts);
345 if (u->level < 0) {
346 WARN("Failed to find path TEX -> TEXBAR\n");
347 u->level = 0;
348 continue;
349 }
350 // this counted all TEXes in the origin block, correct that
351 u->level -= i - bbFirstTex.at(tb->getId()) + 1 /* this TEX */;
352 // and did not count the TEXes in the destination block, add those
353 for (size_t j = bbFirstTex.at(ub->getId()); j < texes.size() &&
354 texes[j]->bb == ub && texes[j]->serial < u->insn->serial;
355 ++j)
356 u->level++;
357 }
358 assert(u->level >= 0);
359 useVec.push_back(*u);
360 }
361 }
362 delete[] uses;
363
364 // insert the barriers
365 for (size_t i = 0; i < useVec.size(); ++i) {
366 Instruction *prev = useVec[i].insn->prev;
367 if (useVec[i].level < 0)
368 continue;
369 if (prev && prev->op == OP_TEXBAR) {
370 if (prev->subOp > useVec[i].level)
371 prev->subOp = useVec[i].level;
372 prev->setSrc(prev->srcCount(), useVec[i].tex->getDef(0));
373 } else {
374 Instruction *bar = new_Instruction(func, OP_TEXBAR, TYPE_NONE);
375 bar->fixed = 1;
376 bar->subOp = useVec[i].level;
377 // make use explicit to ease latency calculation
378 bar->setSrc(bar->srcCount(), useVec[i].tex->getDef(0));
379 useVec[i].insn->bb->insertBefore(useVec[i].insn, bar);
380 }
381 }
382
383 if (fn->getProgram()->optLevel < 3)
384 return true;
385
386 std::vector<Limits> limitT, limitB, limitS; // entry, exit, single
387
388 limitT.resize(fn->allBBlocks.getSize(), Limits(0, 0));
389 limitB.resize(fn->allBBlocks.getSize(), Limits(0, 0));
390 limitS.resize(fn->allBBlocks.getSize());
391
392 // cull unneeded barriers (should do that earlier, but for simplicity)
393 IteratorRef bi = fn->cfg.iteratorCFG();
394 // first calculate min/max outstanding TEXes for each BB
395 for (bi->reset(); !bi->end(); bi->next()) {
396 Graph::Node *n = reinterpret_cast<Graph::Node *>(bi->get());
397 BasicBlock *bb = BasicBlock::get(n);
398 int min = 0;
399 int max = std::numeric_limits<int>::max();
400 for (Instruction *i = bb->getFirst(); i; i = i->next) {
401 if (isTextureOp(i->op)) {
402 min++;
403 if (max < std::numeric_limits<int>::max())
404 max++;
405 } else
406 if (i->op == OP_TEXBAR) {
407 min = MIN2(min, i->subOp);
408 max = MIN2(max, i->subOp);
409 }
410 }
411 // limits when looking at an isolated block
412 limitS[bb->getId()].min = min;
413 limitS[bb->getId()].max = max;
414 }
415 // propagate the min/max values
416 for (unsigned int l = 0; l <= fn->loopNestingBound; ++l) {
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 const int bbId = bb->getId();
421 for (Graph::EdgeIterator ei = n->incident(); !ei.end(); ei.next()) {
422 BasicBlock *in = BasicBlock::get(ei.getNode());
423 const int inId = in->getId();
424 limitT[bbId].min = MAX2(limitT[bbId].min, limitB[inId].min);
425 limitT[bbId].max = MAX2(limitT[bbId].max, limitB[inId].max);
426 }
427 // I just hope this is correct ...
428 if (limitS[bbId].max == std::numeric_limits<int>::max()) {
429 // no barrier
430 limitB[bbId].min = limitT[bbId].min + limitS[bbId].min;
431 limitB[bbId].max = limitT[bbId].max + limitS[bbId].min;
432 } else {
433 // block contained a barrier
434 limitB[bbId].min = MIN2(limitS[bbId].max,
435 limitT[bbId].min + limitS[bbId].min);
436 limitB[bbId].max = MIN2(limitS[bbId].max,
437 limitT[bbId].max + limitS[bbId].min);
438 }
439 }
440 }
441 // finally delete unnecessary barriers
442 for (bi->reset(); !bi->end(); bi->next()) {
443 Graph::Node *n = reinterpret_cast<Graph::Node *>(bi->get());
444 BasicBlock *bb = BasicBlock::get(n);
445 Instruction *prev = NULL;
446 Instruction *next;
447 int max = limitT[bb->getId()].max;
448 for (Instruction *i = bb->getFirst(); i; i = next) {
449 next = i->next;
450 if (i->op == OP_TEXBAR) {
451 if (i->subOp >= max) {
452 delete_Instruction(prog, i);
453 i = NULL;
454 } else {
455 max = i->subOp;
456 if (prev && prev->op == OP_TEXBAR && prev->subOp >= max) {
457 delete_Instruction(prog, prev);
458 prev = NULL;
459 }
460 }
461 } else
462 if (isTextureOp(i->op)) {
463 max++;
464 }
465 if (i && !i->isNop())
466 prev = i;
467 }
468 }
469 return true;
470 }
471
472 bool
473 NVC0LegalizePostRA::visit(Function *fn)
474 {
475 if (needTexBar)
476 insertTextureBarriers(fn);
477
478 rZero = new_LValue(fn, FILE_GPR);
479 carry = new_LValue(fn, FILE_FLAGS);
480
481 rZero->reg.data.id = prog->getTarget()->getFileSize(FILE_GPR);
482 carry->reg.data.id = 0;
483
484 return true;
485 }
486
487 void
488 NVC0LegalizePostRA::replaceZero(Instruction *i)
489 {
490 for (int s = 0; i->srcExists(s); ++s) {
491 if (s == 2 && i->op == OP_SUCLAMP)
492 continue;
493 ImmediateValue *imm = i->getSrc(s)->asImm();
494 if (imm && imm->reg.data.u64 == 0)
495 i->setSrc(s, rZero);
496 }
497 }
498
499 // replace CONT with BRA for single unconditional continue
500 bool
501 NVC0LegalizePostRA::tryReplaceContWithBra(BasicBlock *bb)
502 {
503 if (bb->cfg.incidentCount() != 2 || bb->getEntry()->op != OP_PRECONT)
504 return false;
505 Graph::EdgeIterator ei = bb->cfg.incident();
506 if (ei.getType() != Graph::Edge::BACK)
507 ei.next();
508 if (ei.getType() != Graph::Edge::BACK)
509 return false;
510 BasicBlock *contBB = BasicBlock::get(ei.getNode());
511
512 if (!contBB->getExit() || contBB->getExit()->op != OP_CONT ||
513 contBB->getExit()->getPredicate())
514 return false;
515 contBB->getExit()->op = OP_BRA;
516 bb->remove(bb->getEntry()); // delete PRECONT
517
518 ei.next();
519 assert(ei.end() || ei.getType() != Graph::Edge::BACK);
520 return true;
521 }
522
523 // replace branches to join blocks with join ops
524 void
525 NVC0LegalizePostRA::propagateJoin(BasicBlock *bb)
526 {
527 if (bb->getEntry()->op != OP_JOIN || bb->getEntry()->asFlow()->limit)
528 return;
529 for (Graph::EdgeIterator ei = bb->cfg.incident(); !ei.end(); ei.next()) {
530 BasicBlock *in = BasicBlock::get(ei.getNode());
531 Instruction *exit = in->getExit();
532 if (!exit) {
533 in->insertTail(new FlowInstruction(func, OP_JOIN, bb));
534 // there should always be a terminator instruction
535 WARN("inserted missing terminator in BB:%i\n", in->getId());
536 } else
537 if (exit->op == OP_BRA) {
538 exit->op = OP_JOIN;
539 exit->asFlow()->limit = 1; // must-not-propagate marker
540 }
541 }
542 bb->remove(bb->getEntry());
543 }
544
545 bool
546 NVC0LegalizePostRA::visit(BasicBlock *bb)
547 {
548 Instruction *i, *next;
549
550 // remove pseudo operations and non-fixed no-ops, split 64 bit operations
551 for (i = bb->getFirst(); i; i = next) {
552 next = i->next;
553 if (i->op == OP_EMIT || i->op == OP_RESTART) {
554 if (!i->getDef(0)->refCount())
555 i->setDef(0, NULL);
556 if (i->src(0).getFile() == FILE_IMMEDIATE)
557 i->setSrc(0, rZero); // initial value must be 0
558 replaceZero(i);
559 } else
560 if (i->isNop()) {
561 bb->remove(i);
562 } else
563 if (i->op == OP_BAR && i->subOp == NV50_IR_SUBOP_BAR_SYNC &&
564 prog->getType() != Program::TYPE_COMPUTE) {
565 // It seems like barriers are never required for tessellation since
566 // the warp size is 32, and there are always at most 32 tcs threads.
567 bb->remove(i);
568 } else {
569 // TODO: Move this to before register allocation for operations that
570 // need the $c register !
571 if (typeSizeof(i->dType) == 8) {
572 Instruction *hi;
573 hi = BuildUtil::split64BitOpPostRA(func, i, rZero, carry);
574 if (hi)
575 next = hi;
576 }
577
578 if (i->op != OP_MOV && i->op != OP_PFETCH)
579 replaceZero(i);
580 }
581 }
582 if (!bb->getEntry())
583 return true;
584
585 if (!tryReplaceContWithBra(bb))
586 propagateJoin(bb);
587
588 return true;
589 }
590
591 NVC0LoweringPass::NVC0LoweringPass(Program *prog) : targ(prog->getTarget())
592 {
593 bld.setProgram(prog);
594 gMemBase = NULL;
595 }
596
597 bool
598 NVC0LoweringPass::visit(Function *fn)
599 {
600 if (prog->getType() == Program::TYPE_GEOMETRY) {
601 assert(!strncmp(fn->getName(), "MAIN", 4));
602 // TODO: when we generate actual functions pass this value along somehow
603 bld.setPosition(BasicBlock::get(fn->cfg.getRoot()), false);
604 gpEmitAddress = bld.loadImm(NULL, 0)->asLValue();
605 if (fn->cfgExit) {
606 bld.setPosition(BasicBlock::get(fn->cfgExit)->getExit(), false);
607 bld.mkMovToReg(0, gpEmitAddress);
608 }
609 }
610 return true;
611 }
612
613 bool
614 NVC0LoweringPass::visit(BasicBlock *bb)
615 {
616 return true;
617 }
618
619 inline Value *
620 NVC0LoweringPass::loadTexHandle(Value *ptr, unsigned int slot)
621 {
622 uint8_t b = prog->driver->io.resInfoCBSlot;
623 uint32_t off = prog->driver->io.texBindBase + slot * 4;
624 return bld.
625 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
626 }
627
628 // move array source to first slot, convert to u16, add indirections
629 bool
630 NVC0LoweringPass::handleTEX(TexInstruction *i)
631 {
632 const int dim = i->tex.target.getDim() + i->tex.target.isCube();
633 const int arg = i->tex.target.getArgCount();
634 const int lyr = arg - (i->tex.target.isMS() ? 2 : 1);
635 const int chipset = prog->getTarget()->getChipset();
636
637 // Arguments to the TEX instruction are a little insane. Even though the
638 // encoding is identical between SM20 and SM30, the arguments mean
639 // different things between Fermi and Kepler+. A lot of arguments are
640 // optional based on flags passed to the instruction. This summarizes the
641 // order of things.
642 //
643 // Fermi:
644 // array/indirect
645 // coords
646 // sample
647 // lod bias
648 // depth compare
649 // offsets:
650 // - tg4: 8 bits each, either 2 (1 offset reg) or 8 (2 offset reg)
651 // - other: 4 bits each, single reg
652 //
653 // Kepler+:
654 // indirect handle
655 // array (+ offsets for txd in upper 16 bits)
656 // coords
657 // sample
658 // lod bias
659 // depth compare
660 // offsets (same as fermi, except txd which takes it with array)
661 //
662 // Maxwell (tex):
663 // array
664 // coords
665 // indirect handle
666 // sample
667 // lod bias
668 // depth compare
669 // offsets
670 //
671 // Maxwell (txd):
672 // indirect handle
673 // coords
674 // array + offsets
675 // derivatives
676
677 if (chipset >= NVISA_GK104_CHIPSET) {
678 if (i->tex.rIndirectSrc >= 0 || i->tex.sIndirectSrc >= 0) {
679 // XXX this ignores tsc, and assumes a 1:1 mapping
680 assert(i->tex.rIndirectSrc >= 0);
681 Value *hnd = loadTexHandle(
682 bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
683 i->getIndirectR(), bld.mkImm(2)),
684 i->tex.r);
685 i->tex.r = 0xff;
686 i->tex.s = 0x1f;
687 i->setIndirectR(hnd);
688 i->setIndirectS(NULL);
689 } else if (i->tex.r == i->tex.s || i->op == OP_TXF) {
690 i->tex.r += prog->driver->io.texBindBase / 4;
691 i->tex.s = 0; // only a single cX[] value possible here
692 } else {
693 Value *hnd = bld.getScratch();
694 Value *rHnd = loadTexHandle(NULL, i->tex.r);
695 Value *sHnd = loadTexHandle(NULL, i->tex.s);
696
697 bld.mkOp3(OP_INSBF, TYPE_U32, hnd, rHnd, bld.mkImm(0x1400), sHnd);
698
699 i->tex.r = 0; // not used for indirect tex
700 i->tex.s = 0;
701 i->setIndirectR(hnd);
702 }
703 if (i->tex.target.isArray()) {
704 LValue *layer = new_LValue(func, FILE_GPR);
705 Value *src = i->getSrc(lyr);
706 const int sat = (i->op == OP_TXF) ? 1 : 0;
707 DataType sTy = (i->op == OP_TXF) ? TYPE_U32 : TYPE_F32;
708 bld.mkCvt(OP_CVT, TYPE_U16, layer, sTy, src)->saturate = sat;
709 if (i->op != OP_TXD || chipset < NVISA_GM107_CHIPSET) {
710 for (int s = dim; s >= 1; --s)
711 i->setSrc(s, i->getSrc(s - 1));
712 i->setSrc(0, layer);
713 } else {
714 i->setSrc(dim, layer);
715 }
716 }
717 // Move the indirect reference to the first place
718 if (i->tex.rIndirectSrc >= 0 && (
719 i->op == OP_TXD || chipset < NVISA_GM107_CHIPSET)) {
720 Value *hnd = i->getIndirectR();
721
722 i->setIndirectR(NULL);
723 i->moveSources(0, 1);
724 i->setSrc(0, hnd);
725 i->tex.rIndirectSrc = 0;
726 i->tex.sIndirectSrc = -1;
727 }
728 } else
729 // (nvc0) generate and move the tsc/tic/array source to the front
730 if (i->tex.target.isArray() || i->tex.rIndirectSrc >= 0 || i->tex.sIndirectSrc >= 0) {
731 LValue *src = new_LValue(func, FILE_GPR); // 0xttxsaaaa
732
733 Value *ticRel = i->getIndirectR();
734 Value *tscRel = i->getIndirectS();
735
736 if (ticRel) {
737 i->setSrc(i->tex.rIndirectSrc, NULL);
738 if (i->tex.r)
739 ticRel = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(),
740 ticRel, bld.mkImm(i->tex.r));
741 }
742 if (tscRel) {
743 i->setSrc(i->tex.sIndirectSrc, NULL);
744 if (i->tex.s)
745 tscRel = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(),
746 tscRel, bld.mkImm(i->tex.s));
747 }
748
749 Value *arrayIndex = i->tex.target.isArray() ? i->getSrc(lyr) : NULL;
750 for (int s = dim; s >= 1; --s)
751 i->setSrc(s, i->getSrc(s - 1));
752 i->setSrc(0, arrayIndex);
753
754 if (arrayIndex) {
755 int sat = (i->op == OP_TXF) ? 1 : 0;
756 DataType sTy = (i->op == OP_TXF) ? TYPE_U32 : TYPE_F32;
757 bld.mkCvt(OP_CVT, TYPE_U16, src, sTy, arrayIndex)->saturate = sat;
758 } else {
759 bld.loadImm(src, 0);
760 }
761
762 if (ticRel)
763 bld.mkOp3(OP_INSBF, TYPE_U32, src, ticRel, bld.mkImm(0x0917), src);
764 if (tscRel)
765 bld.mkOp3(OP_INSBF, TYPE_U32, src, tscRel, bld.mkImm(0x0710), src);
766
767 i->setSrc(0, src);
768 }
769
770 // For nvc0, the sample id has to be in the second operand, as the offset
771 // does. Right now we don't know how to pass both in, and this case can't
772 // happen with OpenGL. On nve0, the sample id is part of the texture
773 // coordinate argument.
774 assert(chipset >= NVISA_GK104_CHIPSET ||
775 !i->tex.useOffsets || !i->tex.target.isMS());
776
777 // offset is between lod and dc
778 if (i->tex.useOffsets) {
779 int n, c;
780 int s = i->srcCount(0xff, true);
781 if (i->op != OP_TXD || chipset < NVISA_GK104_CHIPSET) {
782 if (i->tex.target.isShadow())
783 s--;
784 if (i->srcExists(s)) // move potential predicate out of the way
785 i->moveSources(s, 1);
786 if (i->tex.useOffsets == 4 && i->srcExists(s + 1))
787 i->moveSources(s + 1, 1);
788 }
789 if (i->op == OP_TXG) {
790 // Either there is 1 offset, which goes into the 2 low bytes of the
791 // first source, or there are 4 offsets, which go into 2 sources (8
792 // values, 1 byte each).
793 Value *offs[2] = {NULL, NULL};
794 for (n = 0; n < i->tex.useOffsets; n++) {
795 for (c = 0; c < 2; ++c) {
796 if ((n % 2) == 0 && c == 0)
797 offs[n / 2] = i->offset[n][c].get();
798 else
799 bld.mkOp3(OP_INSBF, TYPE_U32,
800 offs[n / 2],
801 i->offset[n][c].get(),
802 bld.mkImm(0x800 | ((n * 16 + c * 8) % 32)),
803 offs[n / 2]);
804 }
805 }
806 i->setSrc(s, offs[0]);
807 if (offs[1])
808 i->setSrc(s + 1, offs[1]);
809 } else {
810 unsigned imm = 0;
811 assert(i->tex.useOffsets == 1);
812 for (c = 0; c < 3; ++c) {
813 ImmediateValue val;
814 if (!i->offset[0][c].getImmediate(val))
815 assert(!"non-immediate offset passed to non-TXG");
816 imm |= (val.reg.data.u32 & 0xf) << (c * 4);
817 }
818 if (i->op == OP_TXD && chipset >= NVISA_GK104_CHIPSET) {
819 // The offset goes into the upper 16 bits of the array index. So
820 // create it if it's not already there, and INSBF it if it already
821 // is.
822 s = (i->tex.rIndirectSrc >= 0) ? 1 : 0;
823 if (chipset >= NVISA_GM107_CHIPSET)
824 s += dim;
825 if (i->tex.target.isArray()) {
826 bld.mkOp3(OP_INSBF, TYPE_U32, i->getSrc(s),
827 bld.loadImm(NULL, imm), bld.mkImm(0xc10),
828 i->getSrc(s));
829 } else {
830 i->moveSources(s, 1);
831 i->setSrc(s, bld.loadImm(NULL, imm << 16));
832 }
833 } else {
834 i->setSrc(s, bld.loadImm(NULL, imm));
835 }
836 }
837 }
838
839 if (chipset >= NVISA_GK104_CHIPSET) {
840 //
841 // If TEX requires more than 4 sources, the 2nd register tuple must be
842 // aligned to 4, even if it consists of just a single 4-byte register.
843 //
844 // XXX HACK: We insert 0 sources to avoid the 5 or 6 regs case.
845 //
846 int s = i->srcCount(0xff, true);
847 if (s > 4 && s < 7) {
848 if (i->srcExists(s)) // move potential predicate out of the way
849 i->moveSources(s, 7 - s);
850 while (s < 7)
851 i->setSrc(s++, bld.loadImm(NULL, 0));
852 }
853 }
854
855 return true;
856 }
857
858 bool
859 NVC0LoweringPass::handleManualTXD(TexInstruction *i)
860 {
861 static const uint8_t qOps[4][2] =
862 {
863 { QUADOP(MOV2, ADD, MOV2, ADD), QUADOP(MOV2, MOV2, ADD, ADD) }, // l0
864 { QUADOP(SUBR, MOV2, SUBR, MOV2), QUADOP(MOV2, MOV2, ADD, ADD) }, // l1
865 { QUADOP(MOV2, ADD, MOV2, ADD), QUADOP(SUBR, SUBR, MOV2, MOV2) }, // l2
866 { QUADOP(SUBR, MOV2, SUBR, MOV2), QUADOP(SUBR, SUBR, MOV2, MOV2) }, // l3
867 };
868 Value *def[4][4];
869 Value *crd[3];
870 Instruction *tex;
871 Value *zero = bld.loadImm(bld.getSSA(), 0);
872 int l, c;
873 const int dim = i->tex.target.getDim();
874 const int array = i->tex.target.isArray();
875
876 i->op = OP_TEX; // no need to clone dPdx/dPdy later
877
878 for (c = 0; c < dim; ++c)
879 crd[c] = bld.getScratch();
880
881 bld.mkOp(OP_QUADON, TYPE_NONE, NULL);
882 for (l = 0; l < 4; ++l) {
883 // mov coordinates from lane l to all lanes
884 for (c = 0; c < dim; ++c)
885 bld.mkQuadop(0x00, crd[c], l, i->getSrc(c + array), zero);
886 // add dPdx from lane l to lanes dx
887 for (c = 0; c < dim; ++c)
888 bld.mkQuadop(qOps[l][0], crd[c], l, i->dPdx[c].get(), crd[c]);
889 // add dPdy from lane l to lanes dy
890 for (c = 0; c < dim; ++c)
891 bld.mkQuadop(qOps[l][1], crd[c], l, i->dPdy[c].get(), crd[c]);
892 // texture
893 bld.insert(tex = cloneForward(func, i));
894 for (c = 0; c < dim; ++c)
895 tex->setSrc(c + array, crd[c]);
896 // save results
897 for (c = 0; i->defExists(c); ++c) {
898 Instruction *mov;
899 def[c][l] = bld.getSSA();
900 mov = bld.mkMov(def[c][l], tex->getDef(c));
901 mov->fixed = 1;
902 mov->lanes = 1 << l;
903 }
904 }
905 bld.mkOp(OP_QUADPOP, TYPE_NONE, NULL);
906
907 for (c = 0; i->defExists(c); ++c) {
908 Instruction *u = bld.mkOp(OP_UNION, TYPE_U32, i->getDef(c));
909 for (l = 0; l < 4; ++l)
910 u->setSrc(l, def[c][l]);
911 }
912
913 i->bb->remove(i);
914 return true;
915 }
916
917 bool
918 NVC0LoweringPass::handleTXD(TexInstruction *txd)
919 {
920 int dim = txd->tex.target.getDim();
921 unsigned arg = txd->tex.target.getArgCount();
922 unsigned expected_args = arg;
923 const int chipset = prog->getTarget()->getChipset();
924
925 if (chipset >= NVISA_GK104_CHIPSET) {
926 if (!txd->tex.target.isArray() && txd->tex.useOffsets)
927 expected_args++;
928 if (txd->tex.rIndirectSrc >= 0 || txd->tex.sIndirectSrc >= 0)
929 expected_args++;
930 } else {
931 if (txd->tex.useOffsets)
932 expected_args++;
933 if (!txd->tex.target.isArray() && (
934 txd->tex.rIndirectSrc >= 0 || txd->tex.sIndirectSrc >= 0))
935 expected_args++;
936 }
937
938 if (expected_args > 4 ||
939 dim > 2 ||
940 txd->tex.target.isShadow() ||
941 txd->tex.target.isCube())
942 txd->op = OP_TEX;
943
944 handleTEX(txd);
945 while (txd->srcExists(arg))
946 ++arg;
947
948 txd->tex.derivAll = true;
949 if (txd->op == OP_TEX)
950 return handleManualTXD(txd);
951
952 assert(arg == expected_args);
953 for (int c = 0; c < dim; ++c) {
954 txd->setSrc(arg + c * 2 + 0, txd->dPdx[c]);
955 txd->setSrc(arg + c * 2 + 1, txd->dPdy[c]);
956 txd->dPdx[c].set(NULL);
957 txd->dPdy[c].set(NULL);
958 }
959 return true;
960 }
961
962 bool
963 NVC0LoweringPass::handleTXQ(TexInstruction *txq)
964 {
965 const int chipset = prog->getTarget()->getChipset();
966 if (chipset >= NVISA_GK104_CHIPSET && txq->tex.rIndirectSrc < 0)
967 txq->tex.r += prog->driver->io.texBindBase / 4;
968
969 if (txq->tex.rIndirectSrc < 0)
970 return true;
971
972 Value *ticRel = txq->getIndirectR();
973
974 txq->setIndirectS(NULL);
975 txq->tex.sIndirectSrc = -1;
976
977 assert(ticRel);
978
979 if (chipset < NVISA_GK104_CHIPSET) {
980 LValue *src = new_LValue(func, FILE_GPR); // 0xttxsaaaa
981
982 txq->setSrc(txq->tex.rIndirectSrc, NULL);
983 if (txq->tex.r)
984 ticRel = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(),
985 ticRel, bld.mkImm(txq->tex.r));
986
987 bld.mkOp2(OP_SHL, TYPE_U32, src, ticRel, bld.mkImm(0x17));
988
989 txq->moveSources(0, 1);
990 txq->setSrc(0, src);
991 } else {
992 Value *hnd = loadTexHandle(
993 bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
994 txq->getIndirectR(), bld.mkImm(2)),
995 txq->tex.r);
996 txq->tex.r = 0xff;
997 txq->tex.s = 0x1f;
998
999 txq->setIndirectR(NULL);
1000 txq->moveSources(0, 1);
1001 txq->setSrc(0, hnd);
1002 txq->tex.rIndirectSrc = 0;
1003 }
1004
1005 return true;
1006 }
1007
1008 bool
1009 NVC0LoweringPass::handleTXLQ(TexInstruction *i)
1010 {
1011 /* The outputs are inverted compared to what the TGSI instruction
1012 * expects. Take that into account in the mask.
1013 */
1014 assert((i->tex.mask & ~3) == 0);
1015 if (i->tex.mask == 1)
1016 i->tex.mask = 2;
1017 else if (i->tex.mask == 2)
1018 i->tex.mask = 1;
1019 handleTEX(i);
1020 bld.setPosition(i, true);
1021
1022 /* The returned values are not quite what we want:
1023 * (a) convert from s16/u16 to f32
1024 * (b) multiply by 1/256
1025 */
1026 for (int def = 0; def < 2; ++def) {
1027 if (!i->defExists(def))
1028 continue;
1029 enum DataType type = TYPE_S16;
1030 if (i->tex.mask == 2 || def > 0)
1031 type = TYPE_U16;
1032 bld.mkCvt(OP_CVT, TYPE_F32, i->getDef(def), type, i->getDef(def));
1033 bld.mkOp2(OP_MUL, TYPE_F32, i->getDef(def),
1034 i->getDef(def), bld.loadImm(NULL, 1.0f / 256));
1035 }
1036 if (i->tex.mask == 3) {
1037 LValue *t = new_LValue(func, FILE_GPR);
1038 bld.mkMov(t, i->getDef(0));
1039 bld.mkMov(i->getDef(0), i->getDef(1));
1040 bld.mkMov(i->getDef(1), t);
1041 }
1042 return true;
1043 }
1044
1045
1046 bool
1047 NVC0LoweringPass::handleATOM(Instruction *atom)
1048 {
1049 SVSemantic sv;
1050
1051 switch (atom->src(0).getFile()) {
1052 case FILE_MEMORY_LOCAL:
1053 sv = SV_LBASE;
1054 break;
1055 case FILE_MEMORY_SHARED:
1056 sv = SV_SBASE;
1057 break;
1058 default:
1059 assert(atom->src(0).getFile() == FILE_MEMORY_GLOBAL);
1060 return true;
1061 }
1062 Value *base =
1063 bld.mkOp1v(OP_RDSV, TYPE_U32, bld.getScratch(), bld.mkSysVal(sv, 0));
1064 Value *ptr = atom->getIndirect(0, 0);
1065
1066 atom->setSrc(0, cloneShallow(func, atom->getSrc(0)));
1067 atom->getSrc(0)->reg.file = FILE_MEMORY_GLOBAL;
1068 if (ptr)
1069 base = bld.mkOp2v(OP_ADD, TYPE_U32, base, base, ptr);
1070 atom->setIndirect(0, 0, base);
1071
1072 return true;
1073 }
1074
1075 bool
1076 NVC0LoweringPass::handleCasExch(Instruction *cas, bool needCctl)
1077 {
1078 if (cas->subOp != NV50_IR_SUBOP_ATOM_CAS &&
1079 cas->subOp != NV50_IR_SUBOP_ATOM_EXCH)
1080 return false;
1081 bld.setPosition(cas, true);
1082
1083 if (needCctl) {
1084 Instruction *cctl = bld.mkOp1(OP_CCTL, TYPE_NONE, NULL, cas->getSrc(0));
1085 cctl->setIndirect(0, 0, cas->getIndirect(0, 0));
1086 cctl->fixed = 1;
1087 cctl->subOp = NV50_IR_SUBOP_CCTL_IV;
1088 if (cas->isPredicated())
1089 cctl->setPredicate(cas->cc, cas->getPredicate());
1090 }
1091
1092 if (cas->defExists(0) && cas->subOp == NV50_IR_SUBOP_ATOM_CAS) {
1093 // CAS is crazy. It's 2nd source is a double reg, and the 3rd source
1094 // should be set to the high part of the double reg or bad things will
1095 // happen elsewhere in the universe.
1096 // Also, it sometimes returns the new value instead of the old one
1097 // under mysterious circumstances.
1098 Value *dreg = bld.getSSA(8);
1099 bld.setPosition(cas, false);
1100 bld.mkOp2(OP_MERGE, TYPE_U64, dreg, cas->getSrc(1), cas->getSrc(2));
1101 cas->setSrc(1, dreg);
1102 }
1103
1104 return true;
1105 }
1106
1107 inline Value *
1108 NVC0LoweringPass::loadResInfo32(Value *ptr, uint32_t off)
1109 {
1110 uint8_t b = prog->driver->io.resInfoCBSlot;
1111 off += prog->driver->io.suInfoBase;
1112 return bld.
1113 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
1114 }
1115
1116 inline Value *
1117 NVC0LoweringPass::loadMsInfo32(Value *ptr, uint32_t off)
1118 {
1119 uint8_t b = prog->driver->io.msInfoCBSlot;
1120 off += prog->driver->io.msInfoBase;
1121 return bld.
1122 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
1123 }
1124
1125 /* On nvc0, surface info is obtained via the surface binding points passed
1126 * to the SULD/SUST instructions.
1127 * On nve4, surface info is stored in c[] and is used by various special
1128 * instructions, e.g. for clamping coordiantes or generating an address.
1129 * They couldn't just have added an equivalent to TIC now, couldn't they ?
1130 */
1131 #define NVE4_SU_INFO_ADDR 0x00
1132 #define NVE4_SU_INFO_FMT 0x04
1133 #define NVE4_SU_INFO_DIM_X 0x08
1134 #define NVE4_SU_INFO_PITCH 0x0c
1135 #define NVE4_SU_INFO_DIM_Y 0x10
1136 #define NVE4_SU_INFO_ARRAY 0x14
1137 #define NVE4_SU_INFO_DIM_Z 0x18
1138 #define NVE4_SU_INFO_UNK1C 0x1c
1139 #define NVE4_SU_INFO_WIDTH 0x20
1140 #define NVE4_SU_INFO_HEIGHT 0x24
1141 #define NVE4_SU_INFO_DEPTH 0x28
1142 #define NVE4_SU_INFO_TARGET 0x2c
1143 #define NVE4_SU_INFO_CALL 0x30
1144 #define NVE4_SU_INFO_RAW_X 0x34
1145 #define NVE4_SU_INFO_MS_X 0x38
1146 #define NVE4_SU_INFO_MS_Y 0x3c
1147
1148 #define NVE4_SU_INFO__STRIDE 0x40
1149
1150 #define NVE4_SU_INFO_DIM(i) (0x08 + (i) * 8)
1151 #define NVE4_SU_INFO_SIZE(i) (0x20 + (i) * 4)
1152 #define NVE4_SU_INFO_MS(i) (0x38 + (i) * 4)
1153
1154 static inline uint16_t getSuClampSubOp(const TexInstruction *su, int c)
1155 {
1156 switch (su->tex.target.getEnum()) {
1157 case TEX_TARGET_BUFFER: return NV50_IR_SUBOP_SUCLAMP_PL(0, 1);
1158 case TEX_TARGET_RECT: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1159 case TEX_TARGET_1D: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1160 case TEX_TARGET_1D_ARRAY: return (c == 1) ?
1161 NV50_IR_SUBOP_SUCLAMP_PL(0, 2) :
1162 NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1163 case TEX_TARGET_2D: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1164 case TEX_TARGET_2D_MS: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1165 case TEX_TARGET_2D_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1166 case TEX_TARGET_2D_MS_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1167 case TEX_TARGET_3D: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1168 case TEX_TARGET_CUBE: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1169 case TEX_TARGET_CUBE_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1170 default:
1171 assert(0);
1172 return 0;
1173 }
1174 }
1175
1176 void
1177 NVC0LoweringPass::adjustCoordinatesMS(TexInstruction *tex)
1178 {
1179 const uint16_t base = tex->tex.r * NVE4_SU_INFO__STRIDE;
1180 const int arg = tex->tex.target.getArgCount();
1181
1182 if (tex->tex.target == TEX_TARGET_2D_MS)
1183 tex->tex.target = TEX_TARGET_2D;
1184 else
1185 if (tex->tex.target == TEX_TARGET_2D_MS_ARRAY)
1186 tex->tex.target = TEX_TARGET_2D_ARRAY;
1187 else
1188 return;
1189
1190 Value *x = tex->getSrc(0);
1191 Value *y = tex->getSrc(1);
1192 Value *s = tex->getSrc(arg - 1);
1193
1194 Value *tx = bld.getSSA(), *ty = bld.getSSA(), *ts = bld.getSSA();
1195
1196 Value *ms_x = loadResInfo32(NULL, base + NVE4_SU_INFO_MS(0));
1197 Value *ms_y = loadResInfo32(NULL, base + NVE4_SU_INFO_MS(1));
1198
1199 bld.mkOp2(OP_SHL, TYPE_U32, tx, x, ms_x);
1200 bld.mkOp2(OP_SHL, TYPE_U32, ty, y, ms_y);
1201
1202 s = bld.mkOp2v(OP_AND, TYPE_U32, ts, s, bld.loadImm(NULL, 0x7));
1203 s = bld.mkOp2v(OP_SHL, TYPE_U32, ts, ts, bld.mkImm(3));
1204
1205 Value *dx = loadMsInfo32(ts, 0x0);
1206 Value *dy = loadMsInfo32(ts, 0x4);
1207
1208 bld.mkOp2(OP_ADD, TYPE_U32, tx, tx, dx);
1209 bld.mkOp2(OP_ADD, TYPE_U32, ty, ty, dy);
1210
1211 tex->setSrc(0, tx);
1212 tex->setSrc(1, ty);
1213 tex->moveSources(arg, -1);
1214 }
1215
1216 // Sets 64-bit "generic address", predicate and format sources for SULD/SUST.
1217 // They're computed from the coordinates using the surface info in c[] space.
1218 void
1219 NVC0LoweringPass::processSurfaceCoordsNVE4(TexInstruction *su)
1220 {
1221 Instruction *insn;
1222 const bool atom = su->op == OP_SUREDB || su->op == OP_SUREDP;
1223 const bool raw =
1224 su->op == OP_SULDB || su->op == OP_SUSTB || su->op == OP_SUREDB;
1225 const int idx = su->tex.r;
1226 const int dim = su->tex.target.getDim();
1227 const int arg = dim + (su->tex.target.isArray() ? 1 : 0);
1228 const uint16_t base = idx * NVE4_SU_INFO__STRIDE;
1229 int c;
1230 Value *zero = bld.mkImm(0);
1231 Value *p1 = NULL;
1232 Value *v;
1233 Value *src[3];
1234 Value *bf, *eau, *off;
1235 Value *addr, *pred;
1236
1237 off = bld.getScratch(4);
1238 bf = bld.getScratch(4);
1239 addr = bld.getSSA(8);
1240 pred = bld.getScratch(1, FILE_PREDICATE);
1241
1242 bld.setPosition(su, false);
1243
1244 adjustCoordinatesMS(su);
1245
1246 // calculate clamped coordinates
1247 for (c = 0; c < arg; ++c) {
1248 src[c] = bld.getScratch();
1249 if (c == 0 && raw)
1250 v = loadResInfo32(NULL, base + NVE4_SU_INFO_RAW_X);
1251 else
1252 v = loadResInfo32(NULL, base + NVE4_SU_INFO_DIM(c));
1253 bld.mkOp3(OP_SUCLAMP, TYPE_S32, src[c], su->getSrc(c), v, zero)
1254 ->subOp = getSuClampSubOp(su, c);
1255 }
1256 for (; c < 3; ++c)
1257 src[c] = zero;
1258
1259 // set predicate output
1260 if (su->tex.target == TEX_TARGET_BUFFER) {
1261 src[0]->getInsn()->setFlagsDef(1, pred);
1262 } else
1263 if (su->tex.target.isArray()) {
1264 p1 = bld.getSSA(1, FILE_PREDICATE);
1265 src[dim]->getInsn()->setFlagsDef(1, p1);
1266 }
1267
1268 // calculate pixel offset
1269 if (dim == 1) {
1270 if (su->tex.target != TEX_TARGET_BUFFER)
1271 bld.mkOp2(OP_AND, TYPE_U32, off, src[0], bld.loadImm(NULL, 0xffff));
1272 } else
1273 if (dim == 3) {
1274 v = loadResInfo32(NULL, base + NVE4_SU_INFO_UNK1C);
1275 bld.mkOp3(OP_MADSP, TYPE_U32, off, src[2], v, src[1])
1276 ->subOp = NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1277
1278 v = loadResInfo32(NULL, base + NVE4_SU_INFO_PITCH);
1279 bld.mkOp3(OP_MADSP, TYPE_U32, off, off, v, src[0])
1280 ->subOp = NV50_IR_SUBOP_MADSP(0,2,8); // u32 u16l u16l
1281 } else {
1282 assert(dim == 2);
1283 v = loadResInfo32(NULL, base + NVE4_SU_INFO_PITCH);
1284 bld.mkOp3(OP_MADSP, TYPE_U32, off, src[1], v, src[0])
1285 ->subOp = su->tex.target.isArray() ?
1286 NV50_IR_SUBOP_MADSP_SD : NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1287 }
1288
1289 // calculate effective address part 1
1290 if (su->tex.target == TEX_TARGET_BUFFER) {
1291 if (raw) {
1292 bf = src[0];
1293 } else {
1294 v = loadResInfo32(NULL, base + NVE4_SU_INFO_FMT);
1295 bld.mkOp3(OP_VSHL, TYPE_U32, bf, src[0], v, zero)
1296 ->subOp = NV50_IR_SUBOP_V1(7,6,8|2);
1297 }
1298 } else {
1299 Value *y = src[1];
1300 Value *z = src[2];
1301 uint16_t subOp = 0;
1302
1303 switch (dim) {
1304 case 1:
1305 y = zero;
1306 z = zero;
1307 break;
1308 case 2:
1309 z = off;
1310 if (!su->tex.target.isArray()) {
1311 z = loadResInfo32(NULL, base + NVE4_SU_INFO_UNK1C);
1312 subOp = NV50_IR_SUBOP_SUBFM_3D;
1313 }
1314 break;
1315 default:
1316 subOp = NV50_IR_SUBOP_SUBFM_3D;
1317 assert(dim == 3);
1318 break;
1319 }
1320 insn = bld.mkOp3(OP_SUBFM, TYPE_U32, bf, src[0], y, z);
1321 insn->subOp = subOp;
1322 insn->setFlagsDef(1, pred);
1323 }
1324
1325 // part 2
1326 v = loadResInfo32(NULL, base + NVE4_SU_INFO_ADDR);
1327
1328 if (su->tex.target == TEX_TARGET_BUFFER) {
1329 eau = v;
1330 } else {
1331 eau = bld.mkOp3v(OP_SUEAU, TYPE_U32, bld.getScratch(4), off, bf, v);
1332 }
1333 // add array layer offset
1334 if (su->tex.target.isArray()) {
1335 v = loadResInfo32(NULL, base + NVE4_SU_INFO_ARRAY);
1336 if (dim == 1)
1337 bld.mkOp3(OP_MADSP, TYPE_U32, eau, src[1], v, eau)
1338 ->subOp = NV50_IR_SUBOP_MADSP(4,0,0); // u16 u24 u32
1339 else
1340 bld.mkOp3(OP_MADSP, TYPE_U32, eau, v, src[2], eau)
1341 ->subOp = NV50_IR_SUBOP_MADSP(0,0,0); // u32 u24 u32
1342 // combine predicates
1343 assert(p1);
1344 bld.mkOp2(OP_OR, TYPE_U8, pred, pred, p1);
1345 }
1346
1347 if (atom) {
1348 Value *lo = bf;
1349 if (su->tex.target == TEX_TARGET_BUFFER) {
1350 lo = zero;
1351 bld.mkMov(off, bf);
1352 }
1353 // bf == g[] address & 0xff
1354 // eau == g[] address >> 8
1355 bld.mkOp3(OP_PERMT, TYPE_U32, bf, lo, bld.loadImm(NULL, 0x6540), eau);
1356 bld.mkOp3(OP_PERMT, TYPE_U32, eau, zero, bld.loadImm(NULL, 0x0007), eau);
1357 } else
1358 if (su->op == OP_SULDP && su->tex.target == TEX_TARGET_BUFFER) {
1359 // Convert from u32 to u8 address format, which is what the library code
1360 // doing SULDP currently uses.
1361 // XXX: can SUEAU do this ?
1362 // XXX: does it matter that we don't mask high bytes in bf ?
1363 // Grrr.
1364 bld.mkOp2(OP_SHR, TYPE_U32, off, bf, bld.mkImm(8));
1365 bld.mkOp2(OP_ADD, TYPE_U32, eau, eau, off);
1366 }
1367
1368 bld.mkOp2(OP_MERGE, TYPE_U64, addr, bf, eau);
1369
1370 if (atom && su->tex.target == TEX_TARGET_BUFFER)
1371 bld.mkOp2(OP_ADD, TYPE_U64, addr, addr, off);
1372
1373 // let's just set it 0 for raw access and hope it works
1374 v = raw ?
1375 bld.mkImm(0) : loadResInfo32(NULL, base + NVE4_SU_INFO_FMT);
1376
1377 // get rid of old coordinate sources, make space for fmt info and predicate
1378 su->moveSources(arg, 3 - arg);
1379 // set 64 bit address and 32-bit format sources
1380 su->setSrc(0, addr);
1381 su->setSrc(1, v);
1382 su->setSrc(2, pred);
1383 }
1384
1385 void
1386 NVC0LoweringPass::handleSurfaceOpNVE4(TexInstruction *su)
1387 {
1388 processSurfaceCoordsNVE4(su);
1389
1390 // Who do we hate more ? The person who decided that nvc0's SULD doesn't
1391 // have to support conversion or the person who decided that, in OpenCL,
1392 // you don't have to specify the format here like you do in OpenGL ?
1393
1394 if (su->op == OP_SULDP) {
1395 // We don't patch shaders. Ever.
1396 // You get an indirect call to our library blob here.
1397 // But at least it's uniform.
1398 FlowInstruction *call;
1399 LValue *p[3];
1400 LValue *r[5];
1401 uint16_t base = su->tex.r * NVE4_SU_INFO__STRIDE + NVE4_SU_INFO_CALL;
1402
1403 for (int i = 0; i < 4; ++i)
1404 (r[i] = bld.getScratch(4, FILE_GPR))->reg.data.id = i;
1405 for (int i = 0; i < 3; ++i)
1406 (p[i] = bld.getScratch(1, FILE_PREDICATE))->reg.data.id = i;
1407 (r[4] = bld.getScratch(8, FILE_GPR))->reg.data.id = 4;
1408
1409 bld.mkMov(p[1], bld.mkImm((su->cache == CACHE_CA) ? 1 : 0), TYPE_U8);
1410 bld.mkMov(p[2], bld.mkImm((su->cache == CACHE_CG) ? 1 : 0), TYPE_U8);
1411 bld.mkMov(p[0], su->getSrc(2), TYPE_U8);
1412 bld.mkMov(r[4], su->getSrc(0), TYPE_U64);
1413 bld.mkMov(r[2], su->getSrc(1), TYPE_U32);
1414
1415 call = bld.mkFlow(OP_CALL, NULL, su->cc, su->getPredicate());
1416
1417 call->indirect = 1;
1418 call->absolute = 1;
1419 call->setSrc(0, bld.mkSymbol(FILE_MEMORY_CONST,
1420 prog->driver->io.resInfoCBSlot, TYPE_U32,
1421 prog->driver->io.suInfoBase + base));
1422 call->setSrc(1, r[2]);
1423 call->setSrc(2, r[4]);
1424 for (int i = 0; i < 3; ++i)
1425 call->setSrc(3 + i, p[i]);
1426 for (int i = 0; i < 4; ++i) {
1427 call->setDef(i, r[i]);
1428 bld.mkMov(su->getDef(i), r[i]);
1429 }
1430 call->setDef(4, p[1]);
1431 delete_Instruction(bld.getProgram(), su);
1432 }
1433
1434 if (su->op == OP_SUREDB || su->op == OP_SUREDP) {
1435 // FIXME: for out of bounds access, destination value will be undefined !
1436 Value *pred = su->getSrc(2);
1437 CondCode cc = CC_NOT_P;
1438 if (su->getPredicate()) {
1439 pred = bld.getScratch(1, FILE_PREDICATE);
1440 cc = su->cc;
1441 if (cc == CC_NOT_P) {
1442 bld.mkOp2(OP_OR, TYPE_U8, pred, su->getPredicate(), su->getSrc(2));
1443 } else {
1444 bld.mkOp2(OP_AND, TYPE_U8, pred, su->getPredicate(), su->getSrc(2));
1445 pred->getInsn()->src(1).mod = Modifier(NV50_IR_MOD_NOT);
1446 }
1447 }
1448 Instruction *red = bld.mkOp(OP_ATOM, su->dType, su->getDef(0));
1449 red->subOp = su->subOp;
1450 if (!gMemBase)
1451 gMemBase = bld.mkSymbol(FILE_MEMORY_GLOBAL, 0, TYPE_U32, 0);
1452 red->setSrc(0, gMemBase);
1453 red->setSrc(1, su->getSrc(3));
1454 if (su->subOp == NV50_IR_SUBOP_ATOM_CAS)
1455 red->setSrc(2, su->getSrc(4));
1456 red->setIndirect(0, 0, su->getSrc(0));
1457 red->setPredicate(cc, pred);
1458 delete_Instruction(bld.getProgram(), su);
1459 handleCasExch(red, true);
1460 } else {
1461 su->sType = (su->tex.target == TEX_TARGET_BUFFER) ? TYPE_U32 : TYPE_U8;
1462 }
1463 }
1464
1465 bool
1466 NVC0LoweringPass::handleWRSV(Instruction *i)
1467 {
1468 Instruction *st;
1469 Symbol *sym;
1470 uint32_t addr;
1471
1472 // must replace, $sreg are not writeable
1473 addr = targ->getSVAddress(FILE_SHADER_OUTPUT, i->getSrc(0)->asSym());
1474 if (addr >= 0x400)
1475 return false;
1476 sym = bld.mkSymbol(FILE_SHADER_OUTPUT, 0, i->sType, addr);
1477
1478 st = bld.mkStore(OP_EXPORT, i->dType, sym, i->getIndirect(0, 0),
1479 i->getSrc(1));
1480 st->perPatch = i->perPatch;
1481
1482 bld.getBB()->remove(i);
1483 return true;
1484 }
1485
1486 void
1487 NVC0LoweringPass::readTessCoord(LValue *dst, int c)
1488 {
1489 Value *laneid = bld.getSSA();
1490 Value *x, *y;
1491
1492 bld.mkOp1(OP_RDSV, TYPE_U32, laneid, bld.mkSysVal(SV_LANEID, 0));
1493
1494 if (c == 0) {
1495 x = dst;
1496 y = NULL;
1497 } else
1498 if (c == 1) {
1499 x = NULL;
1500 y = dst;
1501 } else {
1502 assert(c == 2);
1503 x = bld.getSSA();
1504 y = bld.getSSA();
1505 }
1506 if (x)
1507 bld.mkFetch(x, TYPE_F32, FILE_SHADER_OUTPUT, 0x2f0, NULL, laneid);
1508 if (y)
1509 bld.mkFetch(y, TYPE_F32, FILE_SHADER_OUTPUT, 0x2f4, NULL, laneid);
1510
1511 if (c == 2) {
1512 bld.mkOp2(OP_ADD, TYPE_F32, dst, x, y);
1513 bld.mkOp2(OP_SUB, TYPE_F32, dst, bld.loadImm(NULL, 1.0f), dst);
1514 }
1515 }
1516
1517 bool
1518 NVC0LoweringPass::handleRDSV(Instruction *i)
1519 {
1520 Symbol *sym = i->getSrc(0)->asSym();
1521 const SVSemantic sv = sym->reg.data.sv.sv;
1522 Value *vtx = NULL;
1523 Instruction *ld;
1524 uint32_t addr = targ->getSVAddress(FILE_SHADER_INPUT, sym);
1525
1526 if (addr >= 0x400) {
1527 // mov $sreg
1528 if (sym->reg.data.sv.index == 3) {
1529 // TGSI backend may use 4th component of TID,NTID,CTAID,NCTAID
1530 i->op = OP_MOV;
1531 i->setSrc(0, bld.mkImm((sv == SV_NTID || sv == SV_NCTAID) ? 1 : 0));
1532 }
1533 if (sv == SV_VERTEX_COUNT) {
1534 bld.setPosition(i, true);
1535 bld.mkOp2(OP_EXTBF, TYPE_U32, i->getDef(0), i->getDef(0), bld.mkImm(0x808));
1536 }
1537 return true;
1538 }
1539
1540 switch (sv) {
1541 case SV_POSITION:
1542 assert(prog->getType() == Program::TYPE_FRAGMENT);
1543 if (i->srcExists(1)) {
1544 // Pass offset through to the interpolation logic
1545 ld = bld.mkInterp(NV50_IR_INTERP_LINEAR | NV50_IR_INTERP_OFFSET,
1546 i->getDef(0), addr, NULL);
1547 ld->setSrc(1, i->getSrc(1));
1548 } else {
1549 bld.mkInterp(NV50_IR_INTERP_LINEAR, i->getDef(0), addr, NULL);
1550 }
1551 break;
1552 case SV_FACE:
1553 {
1554 Value *face = i->getDef(0);
1555 bld.mkInterp(NV50_IR_INTERP_FLAT, face, addr, NULL);
1556 if (i->dType == TYPE_F32) {
1557 bld.mkOp2(OP_OR, TYPE_U32, face, face, bld.mkImm(0x00000001));
1558 bld.mkOp1(OP_NEG, TYPE_S32, face, face);
1559 bld.mkCvt(OP_CVT, TYPE_F32, face, TYPE_S32, face);
1560 }
1561 }
1562 break;
1563 case SV_TESS_COORD:
1564 assert(prog->getType() == Program::TYPE_TESSELLATION_EVAL);
1565 readTessCoord(i->getDef(0)->asLValue(), i->getSrc(0)->reg.data.sv.index);
1566 break;
1567 case SV_NTID:
1568 case SV_NCTAID:
1569 case SV_GRIDID:
1570 assert(targ->getChipset() >= NVISA_GK104_CHIPSET); // mov $sreg otherwise
1571 if (sym->reg.data.sv.index == 3) {
1572 i->op = OP_MOV;
1573 i->setSrc(0, bld.mkImm(sv == SV_GRIDID ? 0 : 1));
1574 return true;
1575 }
1576 addr += prog->driver->prop.cp.gridInfoBase;
1577 bld.mkLoad(TYPE_U32, i->getDef(0),
1578 bld.mkSymbol(FILE_MEMORY_CONST, 0, TYPE_U32, addr), NULL);
1579 break;
1580 case SV_SAMPLE_INDEX:
1581 // TODO: Properly pass source as an address in the PIX address space
1582 // (which can be of the form [r0+offset]). But this is currently
1583 // unnecessary.
1584 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
1585 ld->subOp = NV50_IR_SUBOP_PIXLD_SAMPLEID;
1586 break;
1587 case SV_SAMPLE_POS: {
1588 Value *off = new_LValue(func, FILE_GPR);
1589 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
1590 ld->subOp = NV50_IR_SUBOP_PIXLD_SAMPLEID;
1591 bld.mkOp2(OP_SHL, TYPE_U32, off, i->getDef(0), bld.mkImm(3));
1592 bld.mkLoad(TYPE_F32,
1593 i->getDef(0),
1594 bld.mkSymbol(
1595 FILE_MEMORY_CONST, prog->driver->io.resInfoCBSlot,
1596 TYPE_U32, prog->driver->io.sampleInfoBase +
1597 4 * sym->reg.data.sv.index),
1598 off);
1599 break;
1600 }
1601 case SV_SAMPLE_MASK:
1602 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
1603 ld->subOp = NV50_IR_SUBOP_PIXLD_COVMASK;
1604 break;
1605 default:
1606 if (prog->getType() == Program::TYPE_TESSELLATION_EVAL && !i->perPatch)
1607 vtx = bld.mkOp1v(OP_PFETCH, TYPE_U32, bld.getSSA(), bld.mkImm(0));
1608 ld = bld.mkFetch(i->getDef(0), i->dType,
1609 FILE_SHADER_INPUT, addr, i->getIndirect(0, 0), vtx);
1610 ld->perPatch = i->perPatch;
1611 break;
1612 }
1613 bld.getBB()->remove(i);
1614 return true;
1615 }
1616
1617 bool
1618 NVC0LoweringPass::handleDIV(Instruction *i)
1619 {
1620 if (!isFloatType(i->dType))
1621 return true;
1622 bld.setPosition(i, false);
1623 Instruction *rcp = bld.mkOp1(OP_RCP, i->dType, bld.getSSA(typeSizeof(i->dType)), i->getSrc(1));
1624 i->op = OP_MUL;
1625 i->setSrc(1, rcp->getDef(0));
1626 return true;
1627 }
1628
1629 bool
1630 NVC0LoweringPass::handleMOD(Instruction *i)
1631 {
1632 if (!isFloatType(i->dType))
1633 return true;
1634 LValue *value = bld.getScratch(typeSizeof(i->dType));
1635 bld.mkOp1(OP_RCP, i->dType, value, i->getSrc(1));
1636 bld.mkOp2(OP_MUL, i->dType, value, i->getSrc(0), value);
1637 bld.mkOp1(OP_TRUNC, i->dType, value, value);
1638 bld.mkOp2(OP_MUL, i->dType, value, i->getSrc(1), value);
1639 i->op = OP_SUB;
1640 i->setSrc(1, value);
1641 return true;
1642 }
1643
1644 bool
1645 NVC0LoweringPass::handleSQRT(Instruction *i)
1646 {
1647 Value *pred = bld.getSSA(1, FILE_PREDICATE);
1648 Value *zero = bld.getSSA();
1649 Instruction *rsq;
1650
1651 bld.mkOp1(OP_MOV, TYPE_U32, zero, bld.mkImm(0));
1652 if (i->dType == TYPE_F64)
1653 zero = bld.mkOp2v(OP_MERGE, TYPE_U64, bld.getSSA(8), zero, zero);
1654 bld.mkCmp(OP_SET, CC_LE, i->dType, pred, i->dType, i->getSrc(0), zero);
1655 bld.mkOp1(OP_MOV, i->dType, i->getDef(0), zero)->setPredicate(CC_P, pred);
1656 rsq = bld.mkOp1(OP_RSQ, i->dType,
1657 bld.getSSA(typeSizeof(i->dType)), i->getSrc(0));
1658 rsq->setPredicate(CC_NOT_P, pred);
1659 i->op = OP_MUL;
1660 i->setSrc(1, rsq->getDef(0));
1661 i->setPredicate(CC_NOT_P, pred);
1662
1663
1664 return true;
1665 }
1666
1667 bool
1668 NVC0LoweringPass::handlePOW(Instruction *i)
1669 {
1670 LValue *val = bld.getScratch();
1671
1672 bld.mkOp1(OP_LG2, TYPE_F32, val, i->getSrc(0));
1673 bld.mkOp2(OP_MUL, TYPE_F32, val, i->getSrc(1), val)->dnz = 1;
1674 bld.mkOp1(OP_PREEX2, TYPE_F32, val, val);
1675
1676 i->op = OP_EX2;
1677 i->setSrc(0, val);
1678 i->setSrc(1, NULL);
1679
1680 return true;
1681 }
1682
1683 bool
1684 NVC0LoweringPass::handleEXPORT(Instruction *i)
1685 {
1686 if (prog->getType() == Program::TYPE_FRAGMENT) {
1687 int id = i->getSrc(0)->reg.data.offset / 4;
1688
1689 if (i->src(0).isIndirect(0)) // TODO, ugly
1690 return false;
1691 i->op = OP_MOV;
1692 i->subOp = NV50_IR_SUBOP_MOV_FINAL;
1693 i->src(0).set(i->src(1));
1694 i->setSrc(1, NULL);
1695 i->setDef(0, new_LValue(func, FILE_GPR));
1696 i->getDef(0)->reg.data.id = id;
1697
1698 prog->maxGPR = MAX2(prog->maxGPR, id);
1699 } else
1700 if (prog->getType() == Program::TYPE_GEOMETRY) {
1701 i->setIndirect(0, 1, gpEmitAddress);
1702 }
1703 return true;
1704 }
1705
1706 bool
1707 NVC0LoweringPass::handleOUT(Instruction *i)
1708 {
1709 Instruction *prev = i->prev;
1710 ImmediateValue stream, prevStream;
1711
1712 // Only merge if the stream ids match. Also, note that the previous
1713 // instruction would have already been lowered, so we take arg1 from it.
1714 if (i->op == OP_RESTART && prev && prev->op == OP_EMIT &&
1715 i->src(0).getImmediate(stream) &&
1716 prev->src(1).getImmediate(prevStream) &&
1717 stream.reg.data.u32 == prevStream.reg.data.u32) {
1718 i->prev->subOp = NV50_IR_SUBOP_EMIT_RESTART;
1719 delete_Instruction(prog, i);
1720 } else {
1721 assert(gpEmitAddress);
1722 i->setDef(0, gpEmitAddress);
1723 i->setSrc(1, i->getSrc(0));
1724 i->setSrc(0, gpEmitAddress);
1725 }
1726 return true;
1727 }
1728
1729 // Generate a binary predicate if an instruction is predicated by
1730 // e.g. an f32 value.
1731 void
1732 NVC0LoweringPass::checkPredicate(Instruction *insn)
1733 {
1734 Value *pred = insn->getPredicate();
1735 Value *pdst;
1736
1737 if (!pred || pred->reg.file == FILE_PREDICATE)
1738 return;
1739 pdst = new_LValue(func, FILE_PREDICATE);
1740
1741 // CAUTION: don't use pdst->getInsn, the definition might not be unique,
1742 // delay turning PSET(FSET(x,y),0) into PSET(x,y) to a later pass
1743
1744 bld.mkCmp(OP_SET, CC_NEU, insn->dType, pdst, insn->dType, bld.mkImm(0), pred);
1745
1746 insn->setPredicate(insn->cc, pdst);
1747 }
1748
1749 //
1750 // - add quadop dance for texturing
1751 // - put FP outputs in GPRs
1752 // - convert instruction sequences
1753 //
1754 bool
1755 NVC0LoweringPass::visit(Instruction *i)
1756 {
1757 bool ret = true;
1758 bld.setPosition(i, false);
1759
1760 if (i->cc != CC_ALWAYS)
1761 checkPredicate(i);
1762
1763 switch (i->op) {
1764 case OP_TEX:
1765 case OP_TXB:
1766 case OP_TXL:
1767 case OP_TXF:
1768 case OP_TXG:
1769 return handleTEX(i->asTex());
1770 case OP_TXD:
1771 return handleTXD(i->asTex());
1772 case OP_TXLQ:
1773 return handleTXLQ(i->asTex());
1774 case OP_TXQ:
1775 return handleTXQ(i->asTex());
1776 case OP_EX2:
1777 bld.mkOp1(OP_PREEX2, TYPE_F32, i->getDef(0), i->getSrc(0));
1778 i->setSrc(0, i->getDef(0));
1779 break;
1780 case OP_POW:
1781 return handlePOW(i);
1782 case OP_DIV:
1783 return handleDIV(i);
1784 case OP_MOD:
1785 return handleMOD(i);
1786 case OP_SQRT:
1787 return handleSQRT(i);
1788 case OP_EXPORT:
1789 ret = handleEXPORT(i);
1790 break;
1791 case OP_EMIT:
1792 case OP_RESTART:
1793 return handleOUT(i);
1794 case OP_RDSV:
1795 return handleRDSV(i);
1796 case OP_WRSV:
1797 return handleWRSV(i);
1798 case OP_LOAD:
1799 if (i->src(0).getFile() == FILE_SHADER_INPUT) {
1800 if (prog->getType() == Program::TYPE_COMPUTE) {
1801 i->getSrc(0)->reg.file = FILE_MEMORY_CONST;
1802 i->getSrc(0)->reg.fileIndex = 0;
1803 } else
1804 if (prog->getType() == Program::TYPE_GEOMETRY &&
1805 i->src(0).isIndirect(0)) {
1806 // XXX: this assumes vec4 units
1807 Value *ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
1808 i->getIndirect(0, 0), bld.mkImm(4));
1809 i->setIndirect(0, 0, ptr);
1810 i->op = OP_VFETCH;
1811 } else {
1812 i->op = OP_VFETCH;
1813 assert(prog->getType() != Program::TYPE_FRAGMENT); // INTERP
1814 }
1815 } else if (i->src(0).getFile() == FILE_MEMORY_CONST) {
1816 if (i->src(0).isIndirect(1)) {
1817 Value *ptr;
1818 if (i->src(0).isIndirect(0))
1819 ptr = bld.mkOp3v(OP_INSBF, TYPE_U32, bld.getSSA(),
1820 i->getIndirect(0, 1), bld.mkImm(0x1010),
1821 i->getIndirect(0, 0));
1822 else
1823 ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
1824 i->getIndirect(0, 1), bld.mkImm(16));
1825 i->setIndirect(0, 1, NULL);
1826 i->setIndirect(0, 0, ptr);
1827 i->subOp = NV50_IR_SUBOP_LDC_IS;
1828 }
1829 } else if (i->src(0).getFile() == FILE_SHADER_OUTPUT) {
1830 assert(prog->getType() == Program::TYPE_TESSELLATION_CONTROL);
1831 i->op = OP_VFETCH;
1832 }
1833 break;
1834 case OP_ATOM:
1835 {
1836 const bool cctl = i->src(0).getFile() == FILE_MEMORY_GLOBAL;
1837 handleATOM(i);
1838 handleCasExch(i, cctl);
1839 }
1840 break;
1841 case OP_SULDB:
1842 case OP_SULDP:
1843 case OP_SUSTB:
1844 case OP_SUSTP:
1845 case OP_SUREDB:
1846 case OP_SUREDP:
1847 if (targ->getChipset() >= NVISA_GK104_CHIPSET)
1848 handleSurfaceOpNVE4(i->asTex());
1849 break;
1850 default:
1851 break;
1852 }
1853
1854 /* Kepler+ has a special opcode to compute a new base address to be used
1855 * for indirect loads.
1856 */
1857 if (targ->getChipset() >= NVISA_GK104_CHIPSET && !i->perPatch &&
1858 (i->op == OP_VFETCH || i->op == OP_EXPORT) && i->src(0).isIndirect(0)) {
1859 Instruction *afetch = bld.mkOp1(OP_AFETCH, TYPE_U32, bld.getSSA(),
1860 cloneShallow(func, i->getSrc(0)));
1861 afetch->setIndirect(0, 0, i->getIndirect(0, 0));
1862 i->src(0).get()->reg.data.offset = 0;
1863 i->setIndirect(0, 0, afetch->getDef(0));
1864 }
1865
1866 return ret;
1867 }
1868
1869 bool
1870 TargetNVC0::runLegalizePass(Program *prog, CGStage stage) const
1871 {
1872 if (stage == CG_STAGE_PRE_SSA) {
1873 NVC0LoweringPass pass(prog);
1874 return pass.run(prog, false, true);
1875 } else
1876 if (stage == CG_STAGE_POST_RA) {
1877 NVC0LegalizePostRA pass(prog);
1878 return pass.run(prog, false, true);
1879 } else
1880 if (stage == CG_STAGE_SSA) {
1881 NVC0LegalizeSSA pass;
1882 return pass.run(prog, false, true);
1883 }
1884 return false;
1885 }
1886
1887 } // namespace nv50_ir