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 std::tr1::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 std::tr1::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) {
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 if (txq->tex.rIndirectSrc < 0)
966 return true;
967
968 Value *ticRel = txq->getIndirectR();
969 const int chipset = prog->getTarget()->getChipset();
970
971 txq->setIndirectS(NULL);
972 txq->tex.sIndirectSrc = -1;
973
974 assert(ticRel);
975
976 if (chipset < NVISA_GK104_CHIPSET) {
977 LValue *src = new_LValue(func, FILE_GPR); // 0xttxsaaaa
978
979 txq->setSrc(txq->tex.rIndirectSrc, NULL);
980 if (txq->tex.r)
981 ticRel = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(),
982 ticRel, bld.mkImm(txq->tex.r));
983
984 bld.mkOp2(OP_SHL, TYPE_U32, src, ticRel, bld.mkImm(0x17));
985
986 txq->moveSources(0, 1);
987 txq->setSrc(0, src);
988 } else {
989 Value *hnd = loadTexHandle(
990 bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
991 txq->getIndirectR(), bld.mkImm(2)),
992 txq->tex.r);
993 txq->tex.r = 0xff;
994 txq->tex.s = 0x1f;
995
996 txq->setIndirectR(NULL);
997 txq->moveSources(0, 1);
998 txq->setSrc(0, hnd);
999 txq->tex.rIndirectSrc = 0;
1000 }
1001
1002 return true;
1003 }
1004
1005 bool
1006 NVC0LoweringPass::handleTXLQ(TexInstruction *i)
1007 {
1008 /* The outputs are inverted compared to what the TGSI instruction
1009 * expects. Take that into account in the mask.
1010 */
1011 assert((i->tex.mask & ~3) == 0);
1012 if (i->tex.mask == 1)
1013 i->tex.mask = 2;
1014 else if (i->tex.mask == 2)
1015 i->tex.mask = 1;
1016 handleTEX(i);
1017 bld.setPosition(i, true);
1018
1019 /* The returned values are not quite what we want:
1020 * (a) convert from s16/u16 to f32
1021 * (b) multiply by 1/256
1022 */
1023 for (int def = 0; def < 2; ++def) {
1024 if (!i->defExists(def))
1025 continue;
1026 enum DataType type = TYPE_S16;
1027 if (i->tex.mask == 2 || def > 0)
1028 type = TYPE_U16;
1029 bld.mkCvt(OP_CVT, TYPE_F32, i->getDef(def), type, i->getDef(def));
1030 bld.mkOp2(OP_MUL, TYPE_F32, i->getDef(def),
1031 i->getDef(def), bld.loadImm(NULL, 1.0f / 256));
1032 }
1033 if (i->tex.mask == 3) {
1034 LValue *t = new_LValue(func, FILE_GPR);
1035 bld.mkMov(t, i->getDef(0));
1036 bld.mkMov(i->getDef(0), i->getDef(1));
1037 bld.mkMov(i->getDef(1), t);
1038 }
1039 return true;
1040 }
1041
1042
1043 bool
1044 NVC0LoweringPass::handleATOM(Instruction *atom)
1045 {
1046 SVSemantic sv;
1047
1048 switch (atom->src(0).getFile()) {
1049 case FILE_MEMORY_LOCAL:
1050 sv = SV_LBASE;
1051 break;
1052 case FILE_MEMORY_SHARED:
1053 sv = SV_SBASE;
1054 break;
1055 default:
1056 assert(atom->src(0).getFile() == FILE_MEMORY_GLOBAL);
1057 return true;
1058 }
1059 Value *base =
1060 bld.mkOp1v(OP_RDSV, TYPE_U32, bld.getScratch(), bld.mkSysVal(sv, 0));
1061 Value *ptr = atom->getIndirect(0, 0);
1062
1063 atom->setSrc(0, cloneShallow(func, atom->getSrc(0)));
1064 atom->getSrc(0)->reg.file = FILE_MEMORY_GLOBAL;
1065 if (ptr)
1066 base = bld.mkOp2v(OP_ADD, TYPE_U32, base, base, ptr);
1067 atom->setIndirect(0, 0, base);
1068
1069 return true;
1070 }
1071
1072 bool
1073 NVC0LoweringPass::handleCasExch(Instruction *cas, bool needCctl)
1074 {
1075 if (cas->subOp != NV50_IR_SUBOP_ATOM_CAS &&
1076 cas->subOp != NV50_IR_SUBOP_ATOM_EXCH)
1077 return false;
1078 bld.setPosition(cas, true);
1079
1080 if (needCctl) {
1081 Instruction *cctl = bld.mkOp1(OP_CCTL, TYPE_NONE, NULL, cas->getSrc(0));
1082 cctl->setIndirect(0, 0, cas->getIndirect(0, 0));
1083 cctl->fixed = 1;
1084 cctl->subOp = NV50_IR_SUBOP_CCTL_IV;
1085 if (cas->isPredicated())
1086 cctl->setPredicate(cas->cc, cas->getPredicate());
1087 }
1088
1089 if (cas->defExists(0) && cas->subOp == NV50_IR_SUBOP_ATOM_CAS) {
1090 // CAS is crazy. It's 2nd source is a double reg, and the 3rd source
1091 // should be set to the high part of the double reg or bad things will
1092 // happen elsewhere in the universe.
1093 // Also, it sometimes returns the new value instead of the old one
1094 // under mysterious circumstances.
1095 Value *dreg = bld.getSSA(8);
1096 bld.setPosition(cas, false);
1097 bld.mkOp2(OP_MERGE, TYPE_U64, dreg, cas->getSrc(1), cas->getSrc(2));
1098 cas->setSrc(1, dreg);
1099 }
1100
1101 return true;
1102 }
1103
1104 inline Value *
1105 NVC0LoweringPass::loadResInfo32(Value *ptr, uint32_t off)
1106 {
1107 uint8_t b = prog->driver->io.resInfoCBSlot;
1108 off += prog->driver->io.suInfoBase;
1109 return bld.
1110 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
1111 }
1112
1113 inline Value *
1114 NVC0LoweringPass::loadMsInfo32(Value *ptr, uint32_t off)
1115 {
1116 uint8_t b = prog->driver->io.msInfoCBSlot;
1117 off += prog->driver->io.msInfoBase;
1118 return bld.
1119 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
1120 }
1121
1122 /* On nvc0, surface info is obtained via the surface binding points passed
1123 * to the SULD/SUST instructions.
1124 * On nve4, surface info is stored in c[] and is used by various special
1125 * instructions, e.g. for clamping coordiantes or generating an address.
1126 * They couldn't just have added an equivalent to TIC now, couldn't they ?
1127 */
1128 #define NVE4_SU_INFO_ADDR 0x00
1129 #define NVE4_SU_INFO_FMT 0x04
1130 #define NVE4_SU_INFO_DIM_X 0x08
1131 #define NVE4_SU_INFO_PITCH 0x0c
1132 #define NVE4_SU_INFO_DIM_Y 0x10
1133 #define NVE4_SU_INFO_ARRAY 0x14
1134 #define NVE4_SU_INFO_DIM_Z 0x18
1135 #define NVE4_SU_INFO_UNK1C 0x1c
1136 #define NVE4_SU_INFO_WIDTH 0x20
1137 #define NVE4_SU_INFO_HEIGHT 0x24
1138 #define NVE4_SU_INFO_DEPTH 0x28
1139 #define NVE4_SU_INFO_TARGET 0x2c
1140 #define NVE4_SU_INFO_CALL 0x30
1141 #define NVE4_SU_INFO_RAW_X 0x34
1142 #define NVE4_SU_INFO_MS_X 0x38
1143 #define NVE4_SU_INFO_MS_Y 0x3c
1144
1145 #define NVE4_SU_INFO__STRIDE 0x40
1146
1147 #define NVE4_SU_INFO_DIM(i) (0x08 + (i) * 8)
1148 #define NVE4_SU_INFO_SIZE(i) (0x20 + (i) * 4)
1149 #define NVE4_SU_INFO_MS(i) (0x38 + (i) * 4)
1150
1151 static inline uint16_t getSuClampSubOp(const TexInstruction *su, int c)
1152 {
1153 switch (su->tex.target.getEnum()) {
1154 case TEX_TARGET_BUFFER: return NV50_IR_SUBOP_SUCLAMP_PL(0, 1);
1155 case TEX_TARGET_RECT: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1156 case TEX_TARGET_1D: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1157 case TEX_TARGET_1D_ARRAY: return (c == 1) ?
1158 NV50_IR_SUBOP_SUCLAMP_PL(0, 2) :
1159 NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1160 case TEX_TARGET_2D: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1161 case TEX_TARGET_2D_MS: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1162 case TEX_TARGET_2D_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1163 case TEX_TARGET_2D_MS_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1164 case TEX_TARGET_3D: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1165 case TEX_TARGET_CUBE: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1166 case TEX_TARGET_CUBE_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1167 default:
1168 assert(0);
1169 return 0;
1170 }
1171 }
1172
1173 void
1174 NVC0LoweringPass::adjustCoordinatesMS(TexInstruction *tex)
1175 {
1176 const uint16_t base = tex->tex.r * NVE4_SU_INFO__STRIDE;
1177 const int arg = tex->tex.target.getArgCount();
1178
1179 if (tex->tex.target == TEX_TARGET_2D_MS)
1180 tex->tex.target = TEX_TARGET_2D;
1181 else
1182 if (tex->tex.target == TEX_TARGET_2D_MS_ARRAY)
1183 tex->tex.target = TEX_TARGET_2D_ARRAY;
1184 else
1185 return;
1186
1187 Value *x = tex->getSrc(0);
1188 Value *y = tex->getSrc(1);
1189 Value *s = tex->getSrc(arg - 1);
1190
1191 Value *tx = bld.getSSA(), *ty = bld.getSSA(), *ts = bld.getSSA();
1192
1193 Value *ms_x = loadResInfo32(NULL, base + NVE4_SU_INFO_MS(0));
1194 Value *ms_y = loadResInfo32(NULL, base + NVE4_SU_INFO_MS(1));
1195
1196 bld.mkOp2(OP_SHL, TYPE_U32, tx, x, ms_x);
1197 bld.mkOp2(OP_SHL, TYPE_U32, ty, y, ms_y);
1198
1199 s = bld.mkOp2v(OP_AND, TYPE_U32, ts, s, bld.loadImm(NULL, 0x7));
1200 s = bld.mkOp2v(OP_SHL, TYPE_U32, ts, ts, bld.mkImm(3));
1201
1202 Value *dx = loadMsInfo32(ts, 0x0);
1203 Value *dy = loadMsInfo32(ts, 0x4);
1204
1205 bld.mkOp2(OP_ADD, TYPE_U32, tx, tx, dx);
1206 bld.mkOp2(OP_ADD, TYPE_U32, ty, ty, dy);
1207
1208 tex->setSrc(0, tx);
1209 tex->setSrc(1, ty);
1210 tex->moveSources(arg, -1);
1211 }
1212
1213 // Sets 64-bit "generic address", predicate and format sources for SULD/SUST.
1214 // They're computed from the coordinates using the surface info in c[] space.
1215 void
1216 NVC0LoweringPass::processSurfaceCoordsNVE4(TexInstruction *su)
1217 {
1218 Instruction *insn;
1219 const bool atom = su->op == OP_SUREDB || su->op == OP_SUREDP;
1220 const bool raw =
1221 su->op == OP_SULDB || su->op == OP_SUSTB || su->op == OP_SUREDB;
1222 const int idx = su->tex.r;
1223 const int dim = su->tex.target.getDim();
1224 const int arg = dim + (su->tex.target.isArray() ? 1 : 0);
1225 const uint16_t base = idx * NVE4_SU_INFO__STRIDE;
1226 int c;
1227 Value *zero = bld.mkImm(0);
1228 Value *p1 = NULL;
1229 Value *v;
1230 Value *src[3];
1231 Value *bf, *eau, *off;
1232 Value *addr, *pred;
1233
1234 off = bld.getScratch(4);
1235 bf = bld.getScratch(4);
1236 addr = bld.getSSA(8);
1237 pred = bld.getScratch(1, FILE_PREDICATE);
1238
1239 bld.setPosition(su, false);
1240
1241 adjustCoordinatesMS(su);
1242
1243 // calculate clamped coordinates
1244 for (c = 0; c < arg; ++c) {
1245 src[c] = bld.getScratch();
1246 if (c == 0 && raw)
1247 v = loadResInfo32(NULL, base + NVE4_SU_INFO_RAW_X);
1248 else
1249 v = loadResInfo32(NULL, base + NVE4_SU_INFO_DIM(c));
1250 bld.mkOp3(OP_SUCLAMP, TYPE_S32, src[c], su->getSrc(c), v, zero)
1251 ->subOp = getSuClampSubOp(su, c);
1252 }
1253 for (; c < 3; ++c)
1254 src[c] = zero;
1255
1256 // set predicate output
1257 if (su->tex.target == TEX_TARGET_BUFFER) {
1258 src[0]->getInsn()->setFlagsDef(1, pred);
1259 } else
1260 if (su->tex.target.isArray()) {
1261 p1 = bld.getSSA(1, FILE_PREDICATE);
1262 src[dim]->getInsn()->setFlagsDef(1, p1);
1263 }
1264
1265 // calculate pixel offset
1266 if (dim == 1) {
1267 if (su->tex.target != TEX_TARGET_BUFFER)
1268 bld.mkOp2(OP_AND, TYPE_U32, off, src[0], bld.loadImm(NULL, 0xffff));
1269 } else
1270 if (dim == 3) {
1271 v = loadResInfo32(NULL, base + NVE4_SU_INFO_UNK1C);
1272 bld.mkOp3(OP_MADSP, TYPE_U32, off, src[2], v, src[1])
1273 ->subOp = NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1274
1275 v = loadResInfo32(NULL, base + NVE4_SU_INFO_PITCH);
1276 bld.mkOp3(OP_MADSP, TYPE_U32, off, off, v, src[0])
1277 ->subOp = NV50_IR_SUBOP_MADSP(0,2,8); // u32 u16l u16l
1278 } else {
1279 assert(dim == 2);
1280 v = loadResInfo32(NULL, base + NVE4_SU_INFO_PITCH);
1281 bld.mkOp3(OP_MADSP, TYPE_U32, off, src[1], v, src[0])
1282 ->subOp = su->tex.target.isArray() ?
1283 NV50_IR_SUBOP_MADSP_SD : NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1284 }
1285
1286 // calculate effective address part 1
1287 if (su->tex.target == TEX_TARGET_BUFFER) {
1288 if (raw) {
1289 bf = src[0];
1290 } else {
1291 v = loadResInfo32(NULL, base + NVE4_SU_INFO_FMT);
1292 bld.mkOp3(OP_VSHL, TYPE_U32, bf, src[0], v, zero)
1293 ->subOp = NV50_IR_SUBOP_V1(7,6,8|2);
1294 }
1295 } else {
1296 Value *y = src[1];
1297 Value *z = src[2];
1298 uint16_t subOp = 0;
1299
1300 switch (dim) {
1301 case 1:
1302 y = zero;
1303 z = zero;
1304 break;
1305 case 2:
1306 z = off;
1307 if (!su->tex.target.isArray()) {
1308 z = loadResInfo32(NULL, base + NVE4_SU_INFO_UNK1C);
1309 subOp = NV50_IR_SUBOP_SUBFM_3D;
1310 }
1311 break;
1312 default:
1313 subOp = NV50_IR_SUBOP_SUBFM_3D;
1314 assert(dim == 3);
1315 break;
1316 }
1317 insn = bld.mkOp3(OP_SUBFM, TYPE_U32, bf, src[0], y, z);
1318 insn->subOp = subOp;
1319 insn->setFlagsDef(1, pred);
1320 }
1321
1322 // part 2
1323 v = loadResInfo32(NULL, base + NVE4_SU_INFO_ADDR);
1324
1325 if (su->tex.target == TEX_TARGET_BUFFER) {
1326 eau = v;
1327 } else {
1328 eau = bld.mkOp3v(OP_SUEAU, TYPE_U32, bld.getScratch(4), off, bf, v);
1329 }
1330 // add array layer offset
1331 if (su->tex.target.isArray()) {
1332 v = loadResInfo32(NULL, base + NVE4_SU_INFO_ARRAY);
1333 if (dim == 1)
1334 bld.mkOp3(OP_MADSP, TYPE_U32, eau, src[1], v, eau)
1335 ->subOp = NV50_IR_SUBOP_MADSP(4,0,0); // u16 u24 u32
1336 else
1337 bld.mkOp3(OP_MADSP, TYPE_U32, eau, v, src[2], eau)
1338 ->subOp = NV50_IR_SUBOP_MADSP(0,0,0); // u32 u24 u32
1339 // combine predicates
1340 assert(p1);
1341 bld.mkOp2(OP_OR, TYPE_U8, pred, pred, p1);
1342 }
1343
1344 if (atom) {
1345 Value *lo = bf;
1346 if (su->tex.target == TEX_TARGET_BUFFER) {
1347 lo = zero;
1348 bld.mkMov(off, bf);
1349 }
1350 // bf == g[] address & 0xff
1351 // eau == g[] address >> 8
1352 bld.mkOp3(OP_PERMT, TYPE_U32, bf, lo, bld.loadImm(NULL, 0x6540), eau);
1353 bld.mkOp3(OP_PERMT, TYPE_U32, eau, zero, bld.loadImm(NULL, 0x0007), eau);
1354 } else
1355 if (su->op == OP_SULDP && su->tex.target == TEX_TARGET_BUFFER) {
1356 // Convert from u32 to u8 address format, which is what the library code
1357 // doing SULDP currently uses.
1358 // XXX: can SUEAU do this ?
1359 // XXX: does it matter that we don't mask high bytes in bf ?
1360 // Grrr.
1361 bld.mkOp2(OP_SHR, TYPE_U32, off, bf, bld.mkImm(8));
1362 bld.mkOp2(OP_ADD, TYPE_U32, eau, eau, off);
1363 }
1364
1365 bld.mkOp2(OP_MERGE, TYPE_U64, addr, bf, eau);
1366
1367 if (atom && su->tex.target == TEX_TARGET_BUFFER)
1368 bld.mkOp2(OP_ADD, TYPE_U64, addr, addr, off);
1369
1370 // let's just set it 0 for raw access and hope it works
1371 v = raw ?
1372 bld.mkImm(0) : loadResInfo32(NULL, base + NVE4_SU_INFO_FMT);
1373
1374 // get rid of old coordinate sources, make space for fmt info and predicate
1375 su->moveSources(arg, 3 - arg);
1376 // set 64 bit address and 32-bit format sources
1377 su->setSrc(0, addr);
1378 su->setSrc(1, v);
1379 su->setSrc(2, pred);
1380 }
1381
1382 void
1383 NVC0LoweringPass::handleSurfaceOpNVE4(TexInstruction *su)
1384 {
1385 processSurfaceCoordsNVE4(su);
1386
1387 // Who do we hate more ? The person who decided that nvc0's SULD doesn't
1388 // have to support conversion or the person who decided that, in OpenCL,
1389 // you don't have to specify the format here like you do in OpenGL ?
1390
1391 if (su->op == OP_SULDP) {
1392 // We don't patch shaders. Ever.
1393 // You get an indirect call to our library blob here.
1394 // But at least it's uniform.
1395 FlowInstruction *call;
1396 LValue *p[3];
1397 LValue *r[5];
1398 uint16_t base = su->tex.r * NVE4_SU_INFO__STRIDE + NVE4_SU_INFO_CALL;
1399
1400 for (int i = 0; i < 4; ++i)
1401 (r[i] = bld.getScratch(4, FILE_GPR))->reg.data.id = i;
1402 for (int i = 0; i < 3; ++i)
1403 (p[i] = bld.getScratch(1, FILE_PREDICATE))->reg.data.id = i;
1404 (r[4] = bld.getScratch(8, FILE_GPR))->reg.data.id = 4;
1405
1406 bld.mkMov(p[1], bld.mkImm((su->cache == CACHE_CA) ? 1 : 0), TYPE_U8);
1407 bld.mkMov(p[2], bld.mkImm((su->cache == CACHE_CG) ? 1 : 0), TYPE_U8);
1408 bld.mkMov(p[0], su->getSrc(2), TYPE_U8);
1409 bld.mkMov(r[4], su->getSrc(0), TYPE_U64);
1410 bld.mkMov(r[2], su->getSrc(1), TYPE_U32);
1411
1412 call = bld.mkFlow(OP_CALL, NULL, su->cc, su->getPredicate());
1413
1414 call->indirect = 1;
1415 call->absolute = 1;
1416 call->setSrc(0, bld.mkSymbol(FILE_MEMORY_CONST,
1417 prog->driver->io.resInfoCBSlot, TYPE_U32,
1418 prog->driver->io.suInfoBase + base));
1419 call->setSrc(1, r[2]);
1420 call->setSrc(2, r[4]);
1421 for (int i = 0; i < 3; ++i)
1422 call->setSrc(3 + i, p[i]);
1423 for (int i = 0; i < 4; ++i) {
1424 call->setDef(i, r[i]);
1425 bld.mkMov(su->getDef(i), r[i]);
1426 }
1427 call->setDef(4, p[1]);
1428 delete_Instruction(bld.getProgram(), su);
1429 }
1430
1431 if (su->op == OP_SUREDB || su->op == OP_SUREDP) {
1432 // FIXME: for out of bounds access, destination value will be undefined !
1433 Value *pred = su->getSrc(2);
1434 CondCode cc = CC_NOT_P;
1435 if (su->getPredicate()) {
1436 pred = bld.getScratch(1, FILE_PREDICATE);
1437 cc = su->cc;
1438 if (cc == CC_NOT_P) {
1439 bld.mkOp2(OP_OR, TYPE_U8, pred, su->getPredicate(), su->getSrc(2));
1440 } else {
1441 bld.mkOp2(OP_AND, TYPE_U8, pred, su->getPredicate(), su->getSrc(2));
1442 pred->getInsn()->src(1).mod = Modifier(NV50_IR_MOD_NOT);
1443 }
1444 }
1445 Instruction *red = bld.mkOp(OP_ATOM, su->dType, su->getDef(0));
1446 red->subOp = su->subOp;
1447 if (!gMemBase)
1448 gMemBase = bld.mkSymbol(FILE_MEMORY_GLOBAL, 0, TYPE_U32, 0);
1449 red->setSrc(0, gMemBase);
1450 red->setSrc(1, su->getSrc(3));
1451 if (su->subOp == NV50_IR_SUBOP_ATOM_CAS)
1452 red->setSrc(2, su->getSrc(4));
1453 red->setIndirect(0, 0, su->getSrc(0));
1454 red->setPredicate(cc, pred);
1455 delete_Instruction(bld.getProgram(), su);
1456 handleCasExch(red, true);
1457 } else {
1458 su->sType = (su->tex.target == TEX_TARGET_BUFFER) ? TYPE_U32 : TYPE_U8;
1459 }
1460 }
1461
1462 bool
1463 NVC0LoweringPass::handleWRSV(Instruction *i)
1464 {
1465 Instruction *st;
1466 Symbol *sym;
1467 uint32_t addr;
1468
1469 // must replace, $sreg are not writeable
1470 addr = targ->getSVAddress(FILE_SHADER_OUTPUT, i->getSrc(0)->asSym());
1471 if (addr >= 0x400)
1472 return false;
1473 sym = bld.mkSymbol(FILE_SHADER_OUTPUT, 0, i->sType, addr);
1474
1475 st = bld.mkStore(OP_EXPORT, i->dType, sym, i->getIndirect(0, 0),
1476 i->getSrc(1));
1477 st->perPatch = i->perPatch;
1478
1479 bld.getBB()->remove(i);
1480 return true;
1481 }
1482
1483 void
1484 NVC0LoweringPass::readTessCoord(LValue *dst, int c)
1485 {
1486 Value *laneid = bld.getSSA();
1487 Value *x, *y;
1488
1489 bld.mkOp1(OP_RDSV, TYPE_U32, laneid, bld.mkSysVal(SV_LANEID, 0));
1490
1491 if (c == 0) {
1492 x = dst;
1493 y = NULL;
1494 } else
1495 if (c == 1) {
1496 x = NULL;
1497 y = dst;
1498 } else {
1499 assert(c == 2);
1500 x = bld.getSSA();
1501 y = bld.getSSA();
1502 }
1503 if (x)
1504 bld.mkFetch(x, TYPE_F32, FILE_SHADER_OUTPUT, 0x2f0, NULL, laneid);
1505 if (y)
1506 bld.mkFetch(y, TYPE_F32, FILE_SHADER_OUTPUT, 0x2f4, NULL, laneid);
1507
1508 if (c == 2) {
1509 bld.mkOp2(OP_ADD, TYPE_F32, dst, x, y);
1510 bld.mkOp2(OP_SUB, TYPE_F32, dst, bld.loadImm(NULL, 1.0f), dst);
1511 }
1512 }
1513
1514 bool
1515 NVC0LoweringPass::handleRDSV(Instruction *i)
1516 {
1517 Symbol *sym = i->getSrc(0)->asSym();
1518 const SVSemantic sv = sym->reg.data.sv.sv;
1519 Value *vtx = NULL;
1520 Instruction *ld;
1521 uint32_t addr = targ->getSVAddress(FILE_SHADER_INPUT, sym);
1522
1523 if (addr >= 0x400) {
1524 // mov $sreg
1525 if (sym->reg.data.sv.index == 3) {
1526 // TGSI backend may use 4th component of TID,NTID,CTAID,NCTAID
1527 i->op = OP_MOV;
1528 i->setSrc(0, bld.mkImm((sv == SV_NTID || sv == SV_NCTAID) ? 1 : 0));
1529 }
1530 if (sv == SV_VERTEX_COUNT) {
1531 bld.setPosition(i, true);
1532 bld.mkOp2(OP_EXTBF, TYPE_U32, i->getDef(0), i->getDef(0), bld.mkImm(0x808));
1533 }
1534 return true;
1535 }
1536
1537 switch (sv) {
1538 case SV_POSITION:
1539 assert(prog->getType() == Program::TYPE_FRAGMENT);
1540 if (i->srcExists(1)) {
1541 // Pass offset through to the interpolation logic
1542 ld = bld.mkInterp(NV50_IR_INTERP_LINEAR | NV50_IR_INTERP_OFFSET,
1543 i->getDef(0), addr, NULL);
1544 ld->setSrc(1, i->getSrc(1));
1545 } else {
1546 bld.mkInterp(NV50_IR_INTERP_LINEAR, i->getDef(0), addr, NULL);
1547 }
1548 break;
1549 case SV_FACE:
1550 {
1551 Value *face = i->getDef(0);
1552 bld.mkInterp(NV50_IR_INTERP_FLAT, face, addr, NULL);
1553 if (i->dType == TYPE_F32) {
1554 bld.mkOp2(OP_OR, TYPE_U32, face, face, bld.mkImm(0x00000001));
1555 bld.mkOp1(OP_NEG, TYPE_S32, face, face);
1556 bld.mkCvt(OP_CVT, TYPE_F32, face, TYPE_S32, face);
1557 }
1558 }
1559 break;
1560 case SV_TESS_COORD:
1561 assert(prog->getType() == Program::TYPE_TESSELLATION_EVAL);
1562 readTessCoord(i->getDef(0)->asLValue(), i->getSrc(0)->reg.data.sv.index);
1563 break;
1564 case SV_NTID:
1565 case SV_NCTAID:
1566 case SV_GRIDID:
1567 assert(targ->getChipset() >= NVISA_GK104_CHIPSET); // mov $sreg otherwise
1568 if (sym->reg.data.sv.index == 3) {
1569 i->op = OP_MOV;
1570 i->setSrc(0, bld.mkImm(sv == SV_GRIDID ? 0 : 1));
1571 return true;
1572 }
1573 addr += prog->driver->prop.cp.gridInfoBase;
1574 bld.mkLoad(TYPE_U32, i->getDef(0),
1575 bld.mkSymbol(FILE_MEMORY_CONST, 0, TYPE_U32, addr), NULL);
1576 break;
1577 case SV_SAMPLE_INDEX:
1578 // TODO: Properly pass source as an address in the PIX address space
1579 // (which can be of the form [r0+offset]). But this is currently
1580 // unnecessary.
1581 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
1582 ld->subOp = NV50_IR_SUBOP_PIXLD_SAMPLEID;
1583 break;
1584 case SV_SAMPLE_POS: {
1585 Value *off = new_LValue(func, FILE_GPR);
1586 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
1587 ld->subOp = NV50_IR_SUBOP_PIXLD_SAMPLEID;
1588 bld.mkOp2(OP_SHL, TYPE_U32, off, i->getDef(0), bld.mkImm(3));
1589 bld.mkLoad(TYPE_F32,
1590 i->getDef(0),
1591 bld.mkSymbol(
1592 FILE_MEMORY_CONST, prog->driver->io.resInfoCBSlot,
1593 TYPE_U32, prog->driver->io.sampleInfoBase +
1594 4 * sym->reg.data.sv.index),
1595 off);
1596 break;
1597 }
1598 case SV_SAMPLE_MASK:
1599 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
1600 ld->subOp = NV50_IR_SUBOP_PIXLD_COVMASK;
1601 break;
1602 default:
1603 if (prog->getType() == Program::TYPE_TESSELLATION_EVAL && !i->perPatch)
1604 vtx = bld.mkOp1v(OP_PFETCH, TYPE_U32, bld.getSSA(), bld.mkImm(0));
1605 ld = bld.mkFetch(i->getDef(0), i->dType,
1606 FILE_SHADER_INPUT, addr, i->getIndirect(0, 0), vtx);
1607 ld->perPatch = i->perPatch;
1608 break;
1609 }
1610 bld.getBB()->remove(i);
1611 return true;
1612 }
1613
1614 bool
1615 NVC0LoweringPass::handleDIV(Instruction *i)
1616 {
1617 if (!isFloatType(i->dType))
1618 return true;
1619 bld.setPosition(i, false);
1620 Instruction *rcp = bld.mkOp1(OP_RCP, i->dType, bld.getSSA(typeSizeof(i->dType)), i->getSrc(1));
1621 i->op = OP_MUL;
1622 i->setSrc(1, rcp->getDef(0));
1623 return true;
1624 }
1625
1626 bool
1627 NVC0LoweringPass::handleMOD(Instruction *i)
1628 {
1629 if (!isFloatType(i->dType))
1630 return true;
1631 LValue *value = bld.getScratch(typeSizeof(i->dType));
1632 bld.mkOp1(OP_RCP, i->dType, value, i->getSrc(1));
1633 bld.mkOp2(OP_MUL, i->dType, value, i->getSrc(0), value);
1634 bld.mkOp1(OP_TRUNC, i->dType, value, value);
1635 bld.mkOp2(OP_MUL, i->dType, value, i->getSrc(1), value);
1636 i->op = OP_SUB;
1637 i->setSrc(1, value);
1638 return true;
1639 }
1640
1641 bool
1642 NVC0LoweringPass::handleSQRT(Instruction *i)
1643 {
1644 Value *pred = bld.getSSA(1, FILE_PREDICATE);
1645 Value *zero = bld.getSSA();
1646 Instruction *rsq;
1647
1648 bld.mkOp1(OP_MOV, TYPE_U32, zero, bld.mkImm(0));
1649 if (i->dType == TYPE_F64)
1650 zero = bld.mkOp2v(OP_MERGE, TYPE_U64, bld.getSSA(8), zero, zero);
1651 bld.mkCmp(OP_SET, CC_LE, i->dType, pred, i->dType, i->getSrc(0), zero);
1652 bld.mkOp1(OP_MOV, i->dType, i->getDef(0), zero)->setPredicate(CC_P, pred);
1653 rsq = bld.mkOp1(OP_RSQ, i->dType,
1654 bld.getSSA(typeSizeof(i->dType)), i->getSrc(0));
1655 rsq->setPredicate(CC_NOT_P, pred);
1656 i->op = OP_MUL;
1657 i->setSrc(1, rsq->getDef(0));
1658 i->setPredicate(CC_NOT_P, pred);
1659
1660
1661 return true;
1662 }
1663
1664 bool
1665 NVC0LoweringPass::handlePOW(Instruction *i)
1666 {
1667 LValue *val = bld.getScratch();
1668
1669 bld.mkOp1(OP_LG2, TYPE_F32, val, i->getSrc(0));
1670 bld.mkOp2(OP_MUL, TYPE_F32, val, i->getSrc(1), val)->dnz = 1;
1671 bld.mkOp1(OP_PREEX2, TYPE_F32, val, val);
1672
1673 i->op = OP_EX2;
1674 i->setSrc(0, val);
1675 i->setSrc(1, NULL);
1676
1677 return true;
1678 }
1679
1680 bool
1681 NVC0LoweringPass::handleEXPORT(Instruction *i)
1682 {
1683 if (prog->getType() == Program::TYPE_FRAGMENT) {
1684 int id = i->getSrc(0)->reg.data.offset / 4;
1685
1686 if (i->src(0).isIndirect(0)) // TODO, ugly
1687 return false;
1688 i->op = OP_MOV;
1689 i->subOp = NV50_IR_SUBOP_MOV_FINAL;
1690 i->src(0).set(i->src(1));
1691 i->setSrc(1, NULL);
1692 i->setDef(0, new_LValue(func, FILE_GPR));
1693 i->getDef(0)->reg.data.id = id;
1694
1695 prog->maxGPR = MAX2(prog->maxGPR, id);
1696 } else
1697 if (prog->getType() == Program::TYPE_GEOMETRY) {
1698 i->setIndirect(0, 1, gpEmitAddress);
1699 }
1700 return true;
1701 }
1702
1703 bool
1704 NVC0LoweringPass::handleOUT(Instruction *i)
1705 {
1706 Instruction *prev = i->prev;
1707 ImmediateValue stream, prevStream;
1708
1709 // Only merge if the stream ids match. Also, note that the previous
1710 // instruction would have already been lowered, so we take arg1 from it.
1711 if (i->op == OP_RESTART && prev && prev->op == OP_EMIT &&
1712 i->src(0).getImmediate(stream) &&
1713 prev->src(1).getImmediate(prevStream) &&
1714 stream.reg.data.u32 == prevStream.reg.data.u32) {
1715 i->prev->subOp = NV50_IR_SUBOP_EMIT_RESTART;
1716 delete_Instruction(prog, i);
1717 } else {
1718 assert(gpEmitAddress);
1719 i->setDef(0, gpEmitAddress);
1720 i->setSrc(1, i->getSrc(0));
1721 i->setSrc(0, gpEmitAddress);
1722 }
1723 return true;
1724 }
1725
1726 // Generate a binary predicate if an instruction is predicated by
1727 // e.g. an f32 value.
1728 void
1729 NVC0LoweringPass::checkPredicate(Instruction *insn)
1730 {
1731 Value *pred = insn->getPredicate();
1732 Value *pdst;
1733
1734 if (!pred || pred->reg.file == FILE_PREDICATE)
1735 return;
1736 pdst = new_LValue(func, FILE_PREDICATE);
1737
1738 // CAUTION: don't use pdst->getInsn, the definition might not be unique,
1739 // delay turning PSET(FSET(x,y),0) into PSET(x,y) to a later pass
1740
1741 bld.mkCmp(OP_SET, CC_NEU, insn->dType, pdst, insn->dType, bld.mkImm(0), pred);
1742
1743 insn->setPredicate(insn->cc, pdst);
1744 }
1745
1746 //
1747 // - add quadop dance for texturing
1748 // - put FP outputs in GPRs
1749 // - convert instruction sequences
1750 //
1751 bool
1752 NVC0LoweringPass::visit(Instruction *i)
1753 {
1754 bool ret = true;
1755 bld.setPosition(i, false);
1756
1757 if (i->cc != CC_ALWAYS)
1758 checkPredicate(i);
1759
1760 switch (i->op) {
1761 case OP_TEX:
1762 case OP_TXB:
1763 case OP_TXL:
1764 case OP_TXF:
1765 case OP_TXG:
1766 return handleTEX(i->asTex());
1767 case OP_TXD:
1768 return handleTXD(i->asTex());
1769 case OP_TXLQ:
1770 return handleTXLQ(i->asTex());
1771 case OP_TXQ:
1772 return handleTXQ(i->asTex());
1773 case OP_EX2:
1774 bld.mkOp1(OP_PREEX2, TYPE_F32, i->getDef(0), i->getSrc(0));
1775 i->setSrc(0, i->getDef(0));
1776 break;
1777 case OP_POW:
1778 return handlePOW(i);
1779 case OP_DIV:
1780 return handleDIV(i);
1781 case OP_MOD:
1782 return handleMOD(i);
1783 case OP_SQRT:
1784 return handleSQRT(i);
1785 case OP_EXPORT:
1786 ret = handleEXPORT(i);
1787 break;
1788 case OP_EMIT:
1789 case OP_RESTART:
1790 return handleOUT(i);
1791 case OP_RDSV:
1792 return handleRDSV(i);
1793 case OP_WRSV:
1794 return handleWRSV(i);
1795 case OP_LOAD:
1796 if (i->src(0).getFile() == FILE_SHADER_INPUT) {
1797 if (prog->getType() == Program::TYPE_COMPUTE) {
1798 i->getSrc(0)->reg.file = FILE_MEMORY_CONST;
1799 i->getSrc(0)->reg.fileIndex = 0;
1800 } else
1801 if (prog->getType() == Program::TYPE_GEOMETRY &&
1802 i->src(0).isIndirect(0)) {
1803 // XXX: this assumes vec4 units
1804 Value *ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
1805 i->getIndirect(0, 0), bld.mkImm(4));
1806 i->setIndirect(0, 0, ptr);
1807 i->op = OP_VFETCH;
1808 } else {
1809 i->op = OP_VFETCH;
1810 assert(prog->getType() != Program::TYPE_FRAGMENT); // INTERP
1811 }
1812 } else if (i->src(0).getFile() == FILE_MEMORY_CONST) {
1813 if (i->src(0).isIndirect(1)) {
1814 Value *ptr;
1815 if (i->src(0).isIndirect(0))
1816 ptr = bld.mkOp3v(OP_INSBF, TYPE_U32, bld.getSSA(),
1817 i->getIndirect(0, 1), bld.mkImm(0x1010),
1818 i->getIndirect(0, 0));
1819 else
1820 ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
1821 i->getIndirect(0, 1), bld.mkImm(16));
1822 i->setIndirect(0, 1, NULL);
1823 i->setIndirect(0, 0, ptr);
1824 i->subOp = NV50_IR_SUBOP_LDC_IS;
1825 }
1826 } else if (i->src(0).getFile() == FILE_SHADER_OUTPUT) {
1827 assert(prog->getType() == Program::TYPE_TESSELLATION_CONTROL);
1828 i->op = OP_VFETCH;
1829 }
1830 break;
1831 case OP_ATOM:
1832 {
1833 const bool cctl = i->src(0).getFile() == FILE_MEMORY_GLOBAL;
1834 handleATOM(i);
1835 handleCasExch(i, cctl);
1836 }
1837 break;
1838 case OP_SULDB:
1839 case OP_SULDP:
1840 case OP_SUSTB:
1841 case OP_SUSTP:
1842 case OP_SUREDB:
1843 case OP_SUREDP:
1844 if (targ->getChipset() >= NVISA_GK104_CHIPSET)
1845 handleSurfaceOpNVE4(i->asTex());
1846 break;
1847 default:
1848 break;
1849 }
1850
1851 /* Kepler+ has a special opcode to compute a new base address to be used
1852 * for indirect loads.
1853 */
1854 if (targ->getChipset() >= NVISA_GK104_CHIPSET && !i->perPatch &&
1855 (i->op == OP_VFETCH || i->op == OP_EXPORT) && i->src(0).isIndirect(0)) {
1856 Instruction *afetch = bld.mkOp1(OP_AFETCH, TYPE_U32, bld.getSSA(),
1857 cloneShallow(func, i->getSrc(0)));
1858 afetch->setIndirect(0, 0, i->getIndirect(0, 0));
1859 i->src(0).get()->reg.data.offset = 0;
1860 i->setIndirect(0, 0, afetch->getDef(0));
1861 }
1862
1863 return ret;
1864 }
1865
1866 bool
1867 TargetNVC0::runLegalizePass(Program *prog, CGStage stage) const
1868 {
1869 if (stage == CG_STAGE_PRE_SSA) {
1870 NVC0LoweringPass pass(prog);
1871 return pass.run(prog, false, true);
1872 } else
1873 if (stage == CG_STAGE_POST_RA) {
1874 NVC0LegalizePostRA pass(prog);
1875 return pass.run(prog, false, true);
1876 } else
1877 if (stage == CG_STAGE_SSA) {
1878 NVC0LegalizeSSA pass;
1879 return pass.run(prog, false, true);
1880 }
1881 return false;
1882 }
1883
1884 } // namespace nv50_ir