Merge branch 'wip/nir-vtn' 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 // TODO: Move this to before register allocation for operations that
564 // need the $c register !
565 if (typeSizeof(i->dType) == 8) {
566 Instruction *hi;
567 hi = BuildUtil::split64BitOpPostRA(func, i, rZero, carry);
568 if (hi)
569 next = hi;
570 }
571
572 if (i->op != OP_MOV && i->op != OP_PFETCH)
573 replaceZero(i);
574 }
575 }
576 if (!bb->getEntry())
577 return true;
578
579 if (!tryReplaceContWithBra(bb))
580 propagateJoin(bb);
581
582 return true;
583 }
584
585 NVC0LoweringPass::NVC0LoweringPass(Program *prog) : targ(prog->getTarget())
586 {
587 bld.setProgram(prog);
588 gMemBase = NULL;
589 }
590
591 bool
592 NVC0LoweringPass::visit(Function *fn)
593 {
594 if (prog->getType() == Program::TYPE_GEOMETRY) {
595 assert(!strncmp(fn->getName(), "MAIN", 4));
596 // TODO: when we generate actual functions pass this value along somehow
597 bld.setPosition(BasicBlock::get(fn->cfg.getRoot()), false);
598 gpEmitAddress = bld.loadImm(NULL, 0)->asLValue();
599 if (fn->cfgExit) {
600 bld.setPosition(BasicBlock::get(fn->cfgExit)->getExit(), false);
601 bld.mkMovToReg(0, gpEmitAddress);
602 }
603 }
604 return true;
605 }
606
607 bool
608 NVC0LoweringPass::visit(BasicBlock *bb)
609 {
610 return true;
611 }
612
613 inline Value *
614 NVC0LoweringPass::loadTexHandle(Value *ptr, unsigned int slot)
615 {
616 uint8_t b = prog->driver->io.resInfoCBSlot;
617 uint32_t off = prog->driver->io.texBindBase + slot * 4;
618 return bld.
619 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
620 }
621
622 // move array source to first slot, convert to u16, add indirections
623 bool
624 NVC0LoweringPass::handleTEX(TexInstruction *i)
625 {
626 const int dim = i->tex.target.getDim() + i->tex.target.isCube();
627 const int arg = i->tex.target.getArgCount();
628 const int lyr = arg - (i->tex.target.isMS() ? 2 : 1);
629 const int chipset = prog->getTarget()->getChipset();
630
631 // Arguments to the TEX instruction are a little insane. Even though the
632 // encoding is identical between SM20 and SM30, the arguments mean
633 // different things between Fermi and Kepler+. A lot of arguments are
634 // optional based on flags passed to the instruction. This summarizes the
635 // order of things.
636 //
637 // Fermi:
638 // array/indirect
639 // coords
640 // sample
641 // lod bias
642 // depth compare
643 // offsets:
644 // - tg4: 8 bits each, either 2 (1 offset reg) or 8 (2 offset reg)
645 // - other: 4 bits each, single reg
646 //
647 // Kepler+:
648 // indirect handle
649 // array (+ offsets for txd in upper 16 bits)
650 // coords
651 // sample
652 // lod bias
653 // depth compare
654 // offsets (same as fermi, except txd which takes it with array)
655 //
656 // Maxwell (tex):
657 // array
658 // coords
659 // indirect handle
660 // sample
661 // lod bias
662 // depth compare
663 // offsets
664 //
665 // Maxwell (txd):
666 // indirect handle
667 // coords
668 // array + offsets
669 // derivatives
670
671 if (chipset >= NVISA_GK104_CHIPSET) {
672 if (i->tex.rIndirectSrc >= 0 || i->tex.sIndirectSrc >= 0) {
673 // XXX this ignores tsc, and assumes a 1:1 mapping
674 assert(i->tex.rIndirectSrc >= 0);
675 Value *hnd = loadTexHandle(
676 bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
677 i->getIndirectR(), bld.mkImm(2)),
678 i->tex.r);
679 i->tex.r = 0xff;
680 i->tex.s = 0x1f;
681 i->setIndirectR(hnd);
682 i->setIndirectS(NULL);
683 } else if (i->tex.r == i->tex.s) {
684 i->tex.r += prog->driver->io.texBindBase / 4;
685 i->tex.s = 0; // only a single cX[] value possible here
686 } else {
687 Value *hnd = bld.getScratch();
688 Value *rHnd = loadTexHandle(NULL, i->tex.r);
689 Value *sHnd = loadTexHandle(NULL, i->tex.s);
690
691 bld.mkOp3(OP_INSBF, TYPE_U32, hnd, rHnd, bld.mkImm(0x1400), sHnd);
692
693 i->tex.r = 0; // not used for indirect tex
694 i->tex.s = 0;
695 i->setIndirectR(hnd);
696 }
697 if (i->tex.target.isArray()) {
698 LValue *layer = new_LValue(func, FILE_GPR);
699 Value *src = i->getSrc(lyr);
700 const int sat = (i->op == OP_TXF) ? 1 : 0;
701 DataType sTy = (i->op == OP_TXF) ? TYPE_U32 : TYPE_F32;
702 bld.mkCvt(OP_CVT, TYPE_U16, layer, sTy, src)->saturate = sat;
703 if (i->op != OP_TXD || chipset < NVISA_GM107_CHIPSET) {
704 for (int s = dim; s >= 1; --s)
705 i->setSrc(s, i->getSrc(s - 1));
706 i->setSrc(0, layer);
707 } else {
708 i->setSrc(dim, layer);
709 }
710 }
711 // Move the indirect reference to the first place
712 if (i->tex.rIndirectSrc >= 0 && (
713 i->op == OP_TXD || chipset < NVISA_GM107_CHIPSET)) {
714 Value *hnd = i->getIndirectR();
715
716 i->setIndirectR(NULL);
717 i->moveSources(0, 1);
718 i->setSrc(0, hnd);
719 i->tex.rIndirectSrc = 0;
720 i->tex.sIndirectSrc = -1;
721 }
722 } else
723 // (nvc0) generate and move the tsc/tic/array source to the front
724 if (i->tex.target.isArray() || i->tex.rIndirectSrc >= 0 || i->tex.sIndirectSrc >= 0) {
725 LValue *src = new_LValue(func, FILE_GPR); // 0xttxsaaaa
726
727 Value *ticRel = i->getIndirectR();
728 Value *tscRel = i->getIndirectS();
729
730 if (ticRel) {
731 i->setSrc(i->tex.rIndirectSrc, NULL);
732 if (i->tex.r)
733 ticRel = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(),
734 ticRel, bld.mkImm(i->tex.r));
735 }
736 if (tscRel) {
737 i->setSrc(i->tex.sIndirectSrc, NULL);
738 if (i->tex.s)
739 tscRel = bld.mkOp2v(OP_ADD, TYPE_U32, bld.getScratch(),
740 tscRel, bld.mkImm(i->tex.s));
741 }
742
743 Value *arrayIndex = i->tex.target.isArray() ? i->getSrc(lyr) : NULL;
744 for (int s = dim; s >= 1; --s)
745 i->setSrc(s, i->getSrc(s - 1));
746 i->setSrc(0, arrayIndex);
747
748 if (arrayIndex) {
749 int sat = (i->op == OP_TXF) ? 1 : 0;
750 DataType sTy = (i->op == OP_TXF) ? TYPE_U32 : TYPE_F32;
751 bld.mkCvt(OP_CVT, TYPE_U16, src, sTy, arrayIndex)->saturate = sat;
752 } else {
753 bld.loadImm(src, 0);
754 }
755
756 if (ticRel)
757 bld.mkOp3(OP_INSBF, TYPE_U32, src, ticRel, bld.mkImm(0x0917), src);
758 if (tscRel)
759 bld.mkOp3(OP_INSBF, TYPE_U32, src, tscRel, bld.mkImm(0x0710), src);
760
761 i->setSrc(0, src);
762 }
763
764 // For nvc0, the sample id has to be in the second operand, as the offset
765 // does. Right now we don't know how to pass both in, and this case can't
766 // happen with OpenGL. On nve0, the sample id is part of the texture
767 // coordinate argument.
768 assert(chipset >= NVISA_GK104_CHIPSET ||
769 !i->tex.useOffsets || !i->tex.target.isMS());
770
771 // offset is between lod and dc
772 if (i->tex.useOffsets) {
773 int n, c;
774 int s = i->srcCount(0xff, true);
775 if (i->op != OP_TXD || chipset < NVISA_GK104_CHIPSET) {
776 if (i->tex.target.isShadow())
777 s--;
778 if (i->srcExists(s)) // move potential predicate out of the way
779 i->moveSources(s, 1);
780 if (i->tex.useOffsets == 4 && i->srcExists(s + 1))
781 i->moveSources(s + 1, 1);
782 }
783 if (i->op == OP_TXG) {
784 // Either there is 1 offset, which goes into the 2 low bytes of the
785 // first source, or there are 4 offsets, which go into 2 sources (8
786 // values, 1 byte each).
787 Value *offs[2] = {NULL, NULL};
788 for (n = 0; n < i->tex.useOffsets; n++) {
789 for (c = 0; c < 2; ++c) {
790 if ((n % 2) == 0 && c == 0)
791 offs[n / 2] = i->offset[n][c].get();
792 else
793 bld.mkOp3(OP_INSBF, TYPE_U32,
794 offs[n / 2],
795 i->offset[n][c].get(),
796 bld.mkImm(0x800 | ((n * 16 + c * 8) % 32)),
797 offs[n / 2]);
798 }
799 }
800 i->setSrc(s, offs[0]);
801 if (offs[1])
802 i->setSrc(s + 1, offs[1]);
803 } else {
804 unsigned imm = 0;
805 assert(i->tex.useOffsets == 1);
806 for (c = 0; c < 3; ++c) {
807 ImmediateValue val;
808 if (!i->offset[0][c].getImmediate(val))
809 assert(!"non-immediate offset passed to non-TXG");
810 imm |= (val.reg.data.u32 & 0xf) << (c * 4);
811 }
812 if (i->op == OP_TXD && chipset >= NVISA_GK104_CHIPSET) {
813 // The offset goes into the upper 16 bits of the array index. So
814 // create it if it's not already there, and INSBF it if it already
815 // is.
816 s = (i->tex.rIndirectSrc >= 0) ? 1 : 0;
817 if (chipset >= NVISA_GM107_CHIPSET)
818 s += dim;
819 if (i->tex.target.isArray()) {
820 bld.mkOp3(OP_INSBF, TYPE_U32, i->getSrc(s),
821 bld.loadImm(NULL, imm), bld.mkImm(0xc10),
822 i->getSrc(s));
823 } else {
824 i->moveSources(s, 1);
825 i->setSrc(s, bld.loadImm(NULL, imm << 16));
826 }
827 } else {
828 i->setSrc(s, bld.loadImm(NULL, imm));
829 }
830 }
831 }
832
833 if (chipset >= NVISA_GK104_CHIPSET) {
834 //
835 // If TEX requires more than 4 sources, the 2nd register tuple must be
836 // aligned to 4, even if it consists of just a single 4-byte register.
837 //
838 // XXX HACK: We insert 0 sources to avoid the 5 or 6 regs case.
839 //
840 int s = i->srcCount(0xff, true);
841 if (s > 4 && s < 7) {
842 if (i->srcExists(s)) // move potential predicate out of the way
843 i->moveSources(s, 7 - s);
844 while (s < 7)
845 i->setSrc(s++, bld.loadImm(NULL, 0));
846 }
847 }
848
849 return true;
850 }
851
852 bool
853 NVC0LoweringPass::handleManualTXD(TexInstruction *i)
854 {
855 static const uint8_t qOps[4][2] =
856 {
857 { QUADOP(MOV2, ADD, MOV2, ADD), QUADOP(MOV2, MOV2, ADD, ADD) }, // l0
858 { QUADOP(SUBR, MOV2, SUBR, MOV2), QUADOP(MOV2, MOV2, ADD, ADD) }, // l1
859 { QUADOP(MOV2, ADD, MOV2, ADD), QUADOP(SUBR, SUBR, MOV2, MOV2) }, // l2
860 { QUADOP(SUBR, MOV2, SUBR, MOV2), QUADOP(SUBR, SUBR, MOV2, MOV2) }, // l3
861 };
862 Value *def[4][4];
863 Value *crd[3];
864 Instruction *tex;
865 Value *zero = bld.loadImm(bld.getSSA(), 0);
866 int l, c;
867 const int dim = i->tex.target.getDim();
868 const int array = i->tex.target.isArray();
869
870 i->op = OP_TEX; // no need to clone dPdx/dPdy later
871
872 for (c = 0; c < dim; ++c)
873 crd[c] = bld.getScratch();
874
875 bld.mkOp(OP_QUADON, TYPE_NONE, NULL);
876 for (l = 0; l < 4; ++l) {
877 // mov coordinates from lane l to all lanes
878 for (c = 0; c < dim; ++c)
879 bld.mkQuadop(0x00, crd[c], l, i->getSrc(c + array), zero);
880 // add dPdx from lane l to lanes dx
881 for (c = 0; c < dim; ++c)
882 bld.mkQuadop(qOps[l][0], crd[c], l, i->dPdx[c].get(), crd[c]);
883 // add dPdy from lane l to lanes dy
884 for (c = 0; c < dim; ++c)
885 bld.mkQuadop(qOps[l][1], crd[c], l, i->dPdy[c].get(), crd[c]);
886 // texture
887 bld.insert(tex = cloneForward(func, i));
888 for (c = 0; c < dim; ++c)
889 tex->setSrc(c + array, crd[c]);
890 // save results
891 for (c = 0; i->defExists(c); ++c) {
892 Instruction *mov;
893 def[c][l] = bld.getSSA();
894 mov = bld.mkMov(def[c][l], tex->getDef(c));
895 mov->fixed = 1;
896 mov->lanes = 1 << l;
897 }
898 }
899 bld.mkOp(OP_QUADPOP, TYPE_NONE, NULL);
900
901 for (c = 0; i->defExists(c); ++c) {
902 Instruction *u = bld.mkOp(OP_UNION, TYPE_U32, i->getDef(c));
903 for (l = 0; l < 4; ++l)
904 u->setSrc(l, def[c][l]);
905 }
906
907 i->bb->remove(i);
908 return true;
909 }
910
911 bool
912 NVC0LoweringPass::handleTXD(TexInstruction *txd)
913 {
914 int dim = txd->tex.target.getDim();
915 unsigned arg = txd->tex.target.getArgCount();
916 unsigned expected_args = arg;
917 const int chipset = prog->getTarget()->getChipset();
918
919 if (chipset >= NVISA_GK104_CHIPSET) {
920 if (!txd->tex.target.isArray() && txd->tex.useOffsets)
921 expected_args++;
922 if (txd->tex.rIndirectSrc >= 0 || txd->tex.sIndirectSrc >= 0)
923 expected_args++;
924 } else {
925 if (txd->tex.useOffsets)
926 expected_args++;
927 if (!txd->tex.target.isArray() && (
928 txd->tex.rIndirectSrc >= 0 || txd->tex.sIndirectSrc >= 0))
929 expected_args++;
930 }
931
932 if (expected_args > 4 ||
933 dim > 2 ||
934 txd->tex.target.isShadow() ||
935 txd->tex.target.isCube())
936 txd->op = OP_TEX;
937
938 handleTEX(txd);
939 while (txd->srcExists(arg))
940 ++arg;
941
942 txd->tex.derivAll = true;
943 if (txd->op == OP_TEX)
944 return handleManualTXD(txd);
945
946 assert(arg == expected_args);
947 for (int c = 0; c < dim; ++c) {
948 txd->setSrc(arg + c * 2 + 0, txd->dPdx[c]);
949 txd->setSrc(arg + c * 2 + 1, txd->dPdy[c]);
950 txd->dPdx[c].set(NULL);
951 txd->dPdy[c].set(NULL);
952 }
953 return true;
954 }
955
956 bool
957 NVC0LoweringPass::handleTXQ(TexInstruction *txq)
958 {
959 // TODO: indirect resource/sampler index
960 return true;
961 }
962
963 bool
964 NVC0LoweringPass::handleTXLQ(TexInstruction *i)
965 {
966 /* The outputs are inverted compared to what the TGSI instruction
967 * expects. Take that into account in the mask.
968 */
969 assert((i->tex.mask & ~3) == 0);
970 if (i->tex.mask == 1)
971 i->tex.mask = 2;
972 else if (i->tex.mask == 2)
973 i->tex.mask = 1;
974 handleTEX(i);
975 bld.setPosition(i, true);
976
977 /* The returned values are not quite what we want:
978 * (a) convert from s16/u16 to f32
979 * (b) multiply by 1/256
980 */
981 for (int def = 0; def < 2; ++def) {
982 if (!i->defExists(def))
983 continue;
984 enum DataType type = TYPE_S16;
985 if (i->tex.mask == 2 || def > 0)
986 type = TYPE_U16;
987 bld.mkCvt(OP_CVT, TYPE_F32, i->getDef(def), type, i->getDef(def));
988 bld.mkOp2(OP_MUL, TYPE_F32, i->getDef(def),
989 i->getDef(def), bld.loadImm(NULL, 1.0f / 256));
990 }
991 if (i->tex.mask == 3) {
992 LValue *t = new_LValue(func, FILE_GPR);
993 bld.mkMov(t, i->getDef(0));
994 bld.mkMov(i->getDef(0), i->getDef(1));
995 bld.mkMov(i->getDef(1), t);
996 }
997 return true;
998 }
999
1000
1001 bool
1002 NVC0LoweringPass::handleATOM(Instruction *atom)
1003 {
1004 SVSemantic sv;
1005
1006 switch (atom->src(0).getFile()) {
1007 case FILE_MEMORY_LOCAL:
1008 sv = SV_LBASE;
1009 break;
1010 case FILE_MEMORY_SHARED:
1011 sv = SV_SBASE;
1012 break;
1013 default:
1014 assert(atom->src(0).getFile() == FILE_MEMORY_GLOBAL);
1015 return true;
1016 }
1017 Value *base =
1018 bld.mkOp1v(OP_RDSV, TYPE_U32, bld.getScratch(), bld.mkSysVal(sv, 0));
1019 Value *ptr = atom->getIndirect(0, 0);
1020
1021 atom->setSrc(0, cloneShallow(func, atom->getSrc(0)));
1022 atom->getSrc(0)->reg.file = FILE_MEMORY_GLOBAL;
1023 if (ptr)
1024 base = bld.mkOp2v(OP_ADD, TYPE_U32, base, base, ptr);
1025 atom->setIndirect(0, 0, base);
1026
1027 return true;
1028 }
1029
1030 bool
1031 NVC0LoweringPass::handleCasExch(Instruction *cas, bool needCctl)
1032 {
1033 if (cas->subOp != NV50_IR_SUBOP_ATOM_CAS &&
1034 cas->subOp != NV50_IR_SUBOP_ATOM_EXCH)
1035 return false;
1036 bld.setPosition(cas, true);
1037
1038 if (needCctl) {
1039 Instruction *cctl = bld.mkOp1(OP_CCTL, TYPE_NONE, NULL, cas->getSrc(0));
1040 cctl->setIndirect(0, 0, cas->getIndirect(0, 0));
1041 cctl->fixed = 1;
1042 cctl->subOp = NV50_IR_SUBOP_CCTL_IV;
1043 if (cas->isPredicated())
1044 cctl->setPredicate(cas->cc, cas->getPredicate());
1045 }
1046
1047 if (cas->defExists(0) && cas->subOp == NV50_IR_SUBOP_ATOM_CAS) {
1048 // CAS is crazy. It's 2nd source is a double reg, and the 3rd source
1049 // should be set to the high part of the double reg or bad things will
1050 // happen elsewhere in the universe.
1051 // Also, it sometimes returns the new value instead of the old one
1052 // under mysterious circumstances.
1053 Value *dreg = bld.getSSA(8);
1054 bld.setPosition(cas, false);
1055 bld.mkOp2(OP_MERGE, TYPE_U64, dreg, cas->getSrc(1), cas->getSrc(2));
1056 cas->setSrc(1, dreg);
1057 }
1058
1059 return true;
1060 }
1061
1062 inline Value *
1063 NVC0LoweringPass::loadResInfo32(Value *ptr, uint32_t off)
1064 {
1065 uint8_t b = prog->driver->io.resInfoCBSlot;
1066 off += prog->driver->io.suInfoBase;
1067 return bld.
1068 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
1069 }
1070
1071 inline Value *
1072 NVC0LoweringPass::loadMsInfo32(Value *ptr, uint32_t off)
1073 {
1074 uint8_t b = prog->driver->io.msInfoCBSlot;
1075 off += prog->driver->io.msInfoBase;
1076 return bld.
1077 mkLoadv(TYPE_U32, bld.mkSymbol(FILE_MEMORY_CONST, b, TYPE_U32, off), ptr);
1078 }
1079
1080 /* On nvc0, surface info is obtained via the surface binding points passed
1081 * to the SULD/SUST instructions.
1082 * On nve4, surface info is stored in c[] and is used by various special
1083 * instructions, e.g. for clamping coordiantes or generating an address.
1084 * They couldn't just have added an equivalent to TIC now, couldn't they ?
1085 */
1086 #define NVE4_SU_INFO_ADDR 0x00
1087 #define NVE4_SU_INFO_FMT 0x04
1088 #define NVE4_SU_INFO_DIM_X 0x08
1089 #define NVE4_SU_INFO_PITCH 0x0c
1090 #define NVE4_SU_INFO_DIM_Y 0x10
1091 #define NVE4_SU_INFO_ARRAY 0x14
1092 #define NVE4_SU_INFO_DIM_Z 0x18
1093 #define NVE4_SU_INFO_UNK1C 0x1c
1094 #define NVE4_SU_INFO_WIDTH 0x20
1095 #define NVE4_SU_INFO_HEIGHT 0x24
1096 #define NVE4_SU_INFO_DEPTH 0x28
1097 #define NVE4_SU_INFO_TARGET 0x2c
1098 #define NVE4_SU_INFO_CALL 0x30
1099 #define NVE4_SU_INFO_RAW_X 0x34
1100 #define NVE4_SU_INFO_MS_X 0x38
1101 #define NVE4_SU_INFO_MS_Y 0x3c
1102
1103 #define NVE4_SU_INFO__STRIDE 0x40
1104
1105 #define NVE4_SU_INFO_DIM(i) (0x08 + (i) * 8)
1106 #define NVE4_SU_INFO_SIZE(i) (0x20 + (i) * 4)
1107 #define NVE4_SU_INFO_MS(i) (0x38 + (i) * 4)
1108
1109 static inline uint16_t getSuClampSubOp(const TexInstruction *su, int c)
1110 {
1111 switch (su->tex.target.getEnum()) {
1112 case TEX_TARGET_BUFFER: return NV50_IR_SUBOP_SUCLAMP_PL(0, 1);
1113 case TEX_TARGET_RECT: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1114 case TEX_TARGET_1D: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1115 case TEX_TARGET_1D_ARRAY: return (c == 1) ?
1116 NV50_IR_SUBOP_SUCLAMP_PL(0, 2) :
1117 NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1118 case TEX_TARGET_2D: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1119 case TEX_TARGET_2D_MS: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1120 case TEX_TARGET_2D_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1121 case TEX_TARGET_2D_MS_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1122 case TEX_TARGET_3D: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1123 case TEX_TARGET_CUBE: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1124 case TEX_TARGET_CUBE_ARRAY: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1125 default:
1126 assert(0);
1127 return 0;
1128 }
1129 }
1130
1131 void
1132 NVC0LoweringPass::adjustCoordinatesMS(TexInstruction *tex)
1133 {
1134 const uint16_t base = tex->tex.r * NVE4_SU_INFO__STRIDE;
1135 const int arg = tex->tex.target.getArgCount();
1136
1137 if (tex->tex.target == TEX_TARGET_2D_MS)
1138 tex->tex.target = TEX_TARGET_2D;
1139 else
1140 if (tex->tex.target == TEX_TARGET_2D_MS_ARRAY)
1141 tex->tex.target = TEX_TARGET_2D_ARRAY;
1142 else
1143 return;
1144
1145 Value *x = tex->getSrc(0);
1146 Value *y = tex->getSrc(1);
1147 Value *s = tex->getSrc(arg - 1);
1148
1149 Value *tx = bld.getSSA(), *ty = bld.getSSA(), *ts = bld.getSSA();
1150
1151 Value *ms_x = loadResInfo32(NULL, base + NVE4_SU_INFO_MS(0));
1152 Value *ms_y = loadResInfo32(NULL, base + NVE4_SU_INFO_MS(1));
1153
1154 bld.mkOp2(OP_SHL, TYPE_U32, tx, x, ms_x);
1155 bld.mkOp2(OP_SHL, TYPE_U32, ty, y, ms_y);
1156
1157 s = bld.mkOp2v(OP_AND, TYPE_U32, ts, s, bld.loadImm(NULL, 0x7));
1158 s = bld.mkOp2v(OP_SHL, TYPE_U32, ts, ts, bld.mkImm(3));
1159
1160 Value *dx = loadMsInfo32(ts, 0x0);
1161 Value *dy = loadMsInfo32(ts, 0x4);
1162
1163 bld.mkOp2(OP_ADD, TYPE_U32, tx, tx, dx);
1164 bld.mkOp2(OP_ADD, TYPE_U32, ty, ty, dy);
1165
1166 tex->setSrc(0, tx);
1167 tex->setSrc(1, ty);
1168 tex->moveSources(arg, -1);
1169 }
1170
1171 // Sets 64-bit "generic address", predicate and format sources for SULD/SUST.
1172 // They're computed from the coordinates using the surface info in c[] space.
1173 void
1174 NVC0LoweringPass::processSurfaceCoordsNVE4(TexInstruction *su)
1175 {
1176 Instruction *insn;
1177 const bool atom = su->op == OP_SUREDB || su->op == OP_SUREDP;
1178 const bool raw =
1179 su->op == OP_SULDB || su->op == OP_SUSTB || su->op == OP_SUREDB;
1180 const int idx = su->tex.r;
1181 const int dim = su->tex.target.getDim();
1182 const int arg = dim + (su->tex.target.isArray() ? 1 : 0);
1183 const uint16_t base = idx * NVE4_SU_INFO__STRIDE;
1184 int c;
1185 Value *zero = bld.mkImm(0);
1186 Value *p1 = NULL;
1187 Value *v;
1188 Value *src[3];
1189 Value *bf, *eau, *off;
1190 Value *addr, *pred;
1191
1192 off = bld.getScratch(4);
1193 bf = bld.getScratch(4);
1194 addr = bld.getSSA(8);
1195 pred = bld.getScratch(1, FILE_PREDICATE);
1196
1197 bld.setPosition(su, false);
1198
1199 adjustCoordinatesMS(su);
1200
1201 // calculate clamped coordinates
1202 for (c = 0; c < arg; ++c) {
1203 src[c] = bld.getScratch();
1204 if (c == 0 && raw)
1205 v = loadResInfo32(NULL, base + NVE4_SU_INFO_RAW_X);
1206 else
1207 v = loadResInfo32(NULL, base + NVE4_SU_INFO_DIM(c));
1208 bld.mkOp3(OP_SUCLAMP, TYPE_S32, src[c], su->getSrc(c), v, zero)
1209 ->subOp = getSuClampSubOp(su, c);
1210 }
1211 for (; c < 3; ++c)
1212 src[c] = zero;
1213
1214 // set predicate output
1215 if (su->tex.target == TEX_TARGET_BUFFER) {
1216 src[0]->getInsn()->setFlagsDef(1, pred);
1217 } else
1218 if (su->tex.target.isArray()) {
1219 p1 = bld.getSSA(1, FILE_PREDICATE);
1220 src[dim]->getInsn()->setFlagsDef(1, p1);
1221 }
1222
1223 // calculate pixel offset
1224 if (dim == 1) {
1225 if (su->tex.target != TEX_TARGET_BUFFER)
1226 bld.mkOp2(OP_AND, TYPE_U32, off, src[0], bld.loadImm(NULL, 0xffff));
1227 } else
1228 if (dim == 3) {
1229 v = loadResInfo32(NULL, base + NVE4_SU_INFO_UNK1C);
1230 bld.mkOp3(OP_MADSP, TYPE_U32, off, src[2], v, src[1])
1231 ->subOp = NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1232
1233 v = loadResInfo32(NULL, base + NVE4_SU_INFO_PITCH);
1234 bld.mkOp3(OP_MADSP, TYPE_U32, off, off, v, src[0])
1235 ->subOp = NV50_IR_SUBOP_MADSP(0,2,8); // u32 u16l u16l
1236 } else {
1237 assert(dim == 2);
1238 v = loadResInfo32(NULL, base + NVE4_SU_INFO_PITCH);
1239 bld.mkOp3(OP_MADSP, TYPE_U32, off, src[1], v, src[0])
1240 ->subOp = su->tex.target.isArray() ?
1241 NV50_IR_SUBOP_MADSP_SD : NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1242 }
1243
1244 // calculate effective address part 1
1245 if (su->tex.target == TEX_TARGET_BUFFER) {
1246 if (raw) {
1247 bf = src[0];
1248 } else {
1249 v = loadResInfo32(NULL, base + NVE4_SU_INFO_FMT);
1250 bld.mkOp3(OP_VSHL, TYPE_U32, bf, src[0], v, zero)
1251 ->subOp = NV50_IR_SUBOP_V1(7,6,8|2);
1252 }
1253 } else {
1254 Value *y = src[1];
1255 Value *z = src[2];
1256 uint16_t subOp = 0;
1257
1258 switch (dim) {
1259 case 1:
1260 y = zero;
1261 z = zero;
1262 break;
1263 case 2:
1264 z = off;
1265 if (!su->tex.target.isArray()) {
1266 z = loadResInfo32(NULL, base + NVE4_SU_INFO_UNK1C);
1267 subOp = NV50_IR_SUBOP_SUBFM_3D;
1268 }
1269 break;
1270 default:
1271 subOp = NV50_IR_SUBOP_SUBFM_3D;
1272 assert(dim == 3);
1273 break;
1274 }
1275 insn = bld.mkOp3(OP_SUBFM, TYPE_U32, bf, src[0], y, z);
1276 insn->subOp = subOp;
1277 insn->setFlagsDef(1, pred);
1278 }
1279
1280 // part 2
1281 v = loadResInfo32(NULL, base + NVE4_SU_INFO_ADDR);
1282
1283 if (su->tex.target == TEX_TARGET_BUFFER) {
1284 eau = v;
1285 } else {
1286 eau = bld.mkOp3v(OP_SUEAU, TYPE_U32, bld.getScratch(4), off, bf, v);
1287 }
1288 // add array layer offset
1289 if (su->tex.target.isArray()) {
1290 v = loadResInfo32(NULL, base + NVE4_SU_INFO_ARRAY);
1291 if (dim == 1)
1292 bld.mkOp3(OP_MADSP, TYPE_U32, eau, src[1], v, eau)
1293 ->subOp = NV50_IR_SUBOP_MADSP(4,0,0); // u16 u24 u32
1294 else
1295 bld.mkOp3(OP_MADSP, TYPE_U32, eau, v, src[2], eau)
1296 ->subOp = NV50_IR_SUBOP_MADSP(0,0,0); // u32 u24 u32
1297 // combine predicates
1298 assert(p1);
1299 bld.mkOp2(OP_OR, TYPE_U8, pred, pred, p1);
1300 }
1301
1302 if (atom) {
1303 Value *lo = bf;
1304 if (su->tex.target == TEX_TARGET_BUFFER) {
1305 lo = zero;
1306 bld.mkMov(off, bf);
1307 }
1308 // bf == g[] address & 0xff
1309 // eau == g[] address >> 8
1310 bld.mkOp3(OP_PERMT, TYPE_U32, bf, lo, bld.loadImm(NULL, 0x6540), eau);
1311 bld.mkOp3(OP_PERMT, TYPE_U32, eau, zero, bld.loadImm(NULL, 0x0007), eau);
1312 } else
1313 if (su->op == OP_SULDP && su->tex.target == TEX_TARGET_BUFFER) {
1314 // Convert from u32 to u8 address format, which is what the library code
1315 // doing SULDP currently uses.
1316 // XXX: can SUEAU do this ?
1317 // XXX: does it matter that we don't mask high bytes in bf ?
1318 // Grrr.
1319 bld.mkOp2(OP_SHR, TYPE_U32, off, bf, bld.mkImm(8));
1320 bld.mkOp2(OP_ADD, TYPE_U32, eau, eau, off);
1321 }
1322
1323 bld.mkOp2(OP_MERGE, TYPE_U64, addr, bf, eau);
1324
1325 if (atom && su->tex.target == TEX_TARGET_BUFFER)
1326 bld.mkOp2(OP_ADD, TYPE_U64, addr, addr, off);
1327
1328 // let's just set it 0 for raw access and hope it works
1329 v = raw ?
1330 bld.mkImm(0) : loadResInfo32(NULL, base + NVE4_SU_INFO_FMT);
1331
1332 // get rid of old coordinate sources, make space for fmt info and predicate
1333 su->moveSources(arg, 3 - arg);
1334 // set 64 bit address and 32-bit format sources
1335 su->setSrc(0, addr);
1336 su->setSrc(1, v);
1337 su->setSrc(2, pred);
1338 }
1339
1340 void
1341 NVC0LoweringPass::handleSurfaceOpNVE4(TexInstruction *su)
1342 {
1343 processSurfaceCoordsNVE4(su);
1344
1345 // Who do we hate more ? The person who decided that nvc0's SULD doesn't
1346 // have to support conversion or the person who decided that, in OpenCL,
1347 // you don't have to specify the format here like you do in OpenGL ?
1348
1349 if (su->op == OP_SULDP) {
1350 // We don't patch shaders. Ever.
1351 // You get an indirect call to our library blob here.
1352 // But at least it's uniform.
1353 FlowInstruction *call;
1354 LValue *p[3];
1355 LValue *r[5];
1356 uint16_t base = su->tex.r * NVE4_SU_INFO__STRIDE + NVE4_SU_INFO_CALL;
1357
1358 for (int i = 0; i < 4; ++i)
1359 (r[i] = bld.getScratch(4, FILE_GPR))->reg.data.id = i;
1360 for (int i = 0; i < 3; ++i)
1361 (p[i] = bld.getScratch(1, FILE_PREDICATE))->reg.data.id = i;
1362 (r[4] = bld.getScratch(8, FILE_GPR))->reg.data.id = 4;
1363
1364 bld.mkMov(p[1], bld.mkImm((su->cache == CACHE_CA) ? 1 : 0), TYPE_U8);
1365 bld.mkMov(p[2], bld.mkImm((su->cache == CACHE_CG) ? 1 : 0), TYPE_U8);
1366 bld.mkMov(p[0], su->getSrc(2), TYPE_U8);
1367 bld.mkMov(r[4], su->getSrc(0), TYPE_U64);
1368 bld.mkMov(r[2], su->getSrc(1), TYPE_U32);
1369
1370 call = bld.mkFlow(OP_CALL, NULL, su->cc, su->getPredicate());
1371
1372 call->indirect = 1;
1373 call->absolute = 1;
1374 call->setSrc(0, bld.mkSymbol(FILE_MEMORY_CONST,
1375 prog->driver->io.resInfoCBSlot, TYPE_U32,
1376 prog->driver->io.suInfoBase + base));
1377 call->setSrc(1, r[2]);
1378 call->setSrc(2, r[4]);
1379 for (int i = 0; i < 3; ++i)
1380 call->setSrc(3 + i, p[i]);
1381 for (int i = 0; i < 4; ++i) {
1382 call->setDef(i, r[i]);
1383 bld.mkMov(su->getDef(i), r[i]);
1384 }
1385 call->setDef(4, p[1]);
1386 delete_Instruction(bld.getProgram(), su);
1387 }
1388
1389 if (su->op == OP_SUREDB || su->op == OP_SUREDP) {
1390 // FIXME: for out of bounds access, destination value will be undefined !
1391 Value *pred = su->getSrc(2);
1392 CondCode cc = CC_NOT_P;
1393 if (su->getPredicate()) {
1394 pred = bld.getScratch(1, FILE_PREDICATE);
1395 cc = su->cc;
1396 if (cc == CC_NOT_P) {
1397 bld.mkOp2(OP_OR, TYPE_U8, pred, su->getPredicate(), su->getSrc(2));
1398 } else {
1399 bld.mkOp2(OP_AND, TYPE_U8, pred, su->getPredicate(), su->getSrc(2));
1400 pred->getInsn()->src(1).mod = Modifier(NV50_IR_MOD_NOT);
1401 }
1402 }
1403 Instruction *red = bld.mkOp(OP_ATOM, su->dType, su->getDef(0));
1404 red->subOp = su->subOp;
1405 if (!gMemBase)
1406 gMemBase = bld.mkSymbol(FILE_MEMORY_GLOBAL, 0, TYPE_U32, 0);
1407 red->setSrc(0, gMemBase);
1408 red->setSrc(1, su->getSrc(3));
1409 if (su->subOp == NV50_IR_SUBOP_ATOM_CAS)
1410 red->setSrc(2, su->getSrc(4));
1411 red->setIndirect(0, 0, su->getSrc(0));
1412 red->setPredicate(cc, pred);
1413 delete_Instruction(bld.getProgram(), su);
1414 handleCasExch(red, true);
1415 } else {
1416 su->sType = (su->tex.target == TEX_TARGET_BUFFER) ? TYPE_U32 : TYPE_U8;
1417 }
1418 }
1419
1420 bool
1421 NVC0LoweringPass::handleWRSV(Instruction *i)
1422 {
1423 Instruction *st;
1424 Symbol *sym;
1425 uint32_t addr;
1426
1427 // must replace, $sreg are not writeable
1428 addr = targ->getSVAddress(FILE_SHADER_OUTPUT, i->getSrc(0)->asSym());
1429 if (addr >= 0x400)
1430 return false;
1431 sym = bld.mkSymbol(FILE_SHADER_OUTPUT, 0, i->sType, addr);
1432
1433 st = bld.mkStore(OP_EXPORT, i->dType, sym, i->getIndirect(0, 0),
1434 i->getSrc(1));
1435 st->perPatch = i->perPatch;
1436
1437 bld.getBB()->remove(i);
1438 return true;
1439 }
1440
1441 void
1442 NVC0LoweringPass::readTessCoord(LValue *dst, int c)
1443 {
1444 Value *laneid = bld.getSSA();
1445 Value *x, *y;
1446
1447 bld.mkOp1(OP_RDSV, TYPE_U32, laneid, bld.mkSysVal(SV_LANEID, 0));
1448
1449 if (c == 0) {
1450 x = dst;
1451 y = NULL;
1452 } else
1453 if (c == 1) {
1454 x = NULL;
1455 y = dst;
1456 } else {
1457 assert(c == 2);
1458 x = bld.getSSA();
1459 y = bld.getSSA();
1460 }
1461 if (x)
1462 bld.mkFetch(x, TYPE_F32, FILE_SHADER_OUTPUT, 0x2f0, NULL, laneid);
1463 if (y)
1464 bld.mkFetch(y, TYPE_F32, FILE_SHADER_OUTPUT, 0x2f4, NULL, laneid);
1465
1466 if (c == 2) {
1467 bld.mkOp2(OP_ADD, TYPE_F32, dst, x, y);
1468 bld.mkOp2(OP_SUB, TYPE_F32, dst, bld.loadImm(NULL, 1.0f), dst);
1469 }
1470 }
1471
1472 bool
1473 NVC0LoweringPass::handleRDSV(Instruction *i)
1474 {
1475 Symbol *sym = i->getSrc(0)->asSym();
1476 const SVSemantic sv = sym->reg.data.sv.sv;
1477 Value *vtx = NULL;
1478 Instruction *ld;
1479 uint32_t addr = targ->getSVAddress(FILE_SHADER_INPUT, sym);
1480
1481 if (addr >= 0x400) {
1482 // mov $sreg
1483 if (sym->reg.data.sv.index == 3) {
1484 // TGSI backend may use 4th component of TID,NTID,CTAID,NCTAID
1485 i->op = OP_MOV;
1486 i->setSrc(0, bld.mkImm((sv == SV_NTID || sv == SV_NCTAID) ? 1 : 0));
1487 }
1488 return true;
1489 }
1490
1491 switch (sv) {
1492 case SV_POSITION:
1493 assert(prog->getType() == Program::TYPE_FRAGMENT);
1494 if (i->srcExists(1)) {
1495 // Pass offset through to the interpolation logic
1496 ld = bld.mkInterp(NV50_IR_INTERP_LINEAR | NV50_IR_INTERP_OFFSET,
1497 i->getDef(0), addr, NULL);
1498 ld->setSrc(1, i->getSrc(1));
1499 } else {
1500 bld.mkInterp(NV50_IR_INTERP_LINEAR, i->getDef(0), addr, NULL);
1501 }
1502 break;
1503 case SV_FACE:
1504 {
1505 Value *face = i->getDef(0);
1506 bld.mkInterp(NV50_IR_INTERP_FLAT, face, addr, NULL);
1507 if (i->dType == TYPE_F32) {
1508 bld.mkOp2(OP_OR, TYPE_U32, face, face, bld.mkImm(0x00000001));
1509 bld.mkOp1(OP_NEG, TYPE_S32, face, face);
1510 bld.mkCvt(OP_CVT, TYPE_F32, face, TYPE_S32, face);
1511 }
1512 }
1513 break;
1514 case SV_TESS_COORD:
1515 assert(prog->getType() == Program::TYPE_TESSELLATION_EVAL);
1516 readTessCoord(i->getDef(0)->asLValue(), i->getSrc(0)->reg.data.sv.index);
1517 break;
1518 case SV_NTID:
1519 case SV_NCTAID:
1520 case SV_GRIDID:
1521 assert(targ->getChipset() >= NVISA_GK104_CHIPSET); // mov $sreg otherwise
1522 if (sym->reg.data.sv.index == 3) {
1523 i->op = OP_MOV;
1524 i->setSrc(0, bld.mkImm(sv == SV_GRIDID ? 0 : 1));
1525 return true;
1526 }
1527 addr += prog->driver->prop.cp.gridInfoBase;
1528 bld.mkLoad(TYPE_U32, i->getDef(0),
1529 bld.mkSymbol(FILE_MEMORY_CONST, 0, TYPE_U32, addr), NULL);
1530 break;
1531 case SV_SAMPLE_INDEX:
1532 // TODO: Properly pass source as an address in the PIX address space
1533 // (which can be of the form [r0+offset]). But this is currently
1534 // unnecessary.
1535 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
1536 ld->subOp = NV50_IR_SUBOP_PIXLD_SAMPLEID;
1537 break;
1538 case SV_SAMPLE_POS: {
1539 Value *off = new_LValue(func, FILE_GPR);
1540 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
1541 ld->subOp = NV50_IR_SUBOP_PIXLD_SAMPLEID;
1542 bld.mkOp2(OP_SHL, TYPE_U32, off, i->getDef(0), bld.mkImm(3));
1543 bld.mkLoad(TYPE_F32,
1544 i->getDef(0),
1545 bld.mkSymbol(
1546 FILE_MEMORY_CONST, prog->driver->io.resInfoCBSlot,
1547 TYPE_U32, prog->driver->io.sampleInfoBase +
1548 4 * sym->reg.data.sv.index),
1549 off);
1550 break;
1551 }
1552 case SV_SAMPLE_MASK:
1553 ld = bld.mkOp1(OP_PIXLD, TYPE_U32, i->getDef(0), bld.mkImm(0));
1554 ld->subOp = NV50_IR_SUBOP_PIXLD_COVMASK;
1555 break;
1556 default:
1557 if (prog->getType() == Program::TYPE_TESSELLATION_EVAL)
1558 vtx = bld.mkOp1v(OP_PFETCH, TYPE_U32, bld.getSSA(), bld.mkImm(0));
1559 ld = bld.mkFetch(i->getDef(0), i->dType,
1560 FILE_SHADER_INPUT, addr, i->getIndirect(0, 0), vtx);
1561 ld->perPatch = i->perPatch;
1562 break;
1563 }
1564 bld.getBB()->remove(i);
1565 return true;
1566 }
1567
1568 bool
1569 NVC0LoweringPass::handleDIV(Instruction *i)
1570 {
1571 if (!isFloatType(i->dType))
1572 return true;
1573 bld.setPosition(i, false);
1574 Instruction *rcp = bld.mkOp1(OP_RCP, i->dType, bld.getSSA(typeSizeof(i->dType)), i->getSrc(1));
1575 i->op = OP_MUL;
1576 i->setSrc(1, rcp->getDef(0));
1577 return true;
1578 }
1579
1580 bool
1581 NVC0LoweringPass::handleMOD(Instruction *i)
1582 {
1583 if (!isFloatType(i->dType))
1584 return true;
1585 LValue *value = bld.getScratch(typeSizeof(i->dType));
1586 bld.mkOp1(OP_RCP, i->dType, value, i->getSrc(1));
1587 bld.mkOp2(OP_MUL, i->dType, value, i->getSrc(0), value);
1588 bld.mkOp1(OP_TRUNC, i->dType, value, value);
1589 bld.mkOp2(OP_MUL, i->dType, value, i->getSrc(1), value);
1590 i->op = OP_SUB;
1591 i->setSrc(1, value);
1592 return true;
1593 }
1594
1595 bool
1596 NVC0LoweringPass::handleSQRT(Instruction *i)
1597 {
1598 Value *pred = bld.getSSA(1, FILE_PREDICATE);
1599 Value *zero = bld.getSSA();
1600 Instruction *rsq;
1601
1602 bld.mkOp1(OP_MOV, TYPE_U32, zero, bld.mkImm(0));
1603 if (i->dType == TYPE_F64)
1604 zero = bld.mkOp2v(OP_MERGE, TYPE_U64, bld.getSSA(8), zero, zero);
1605 bld.mkCmp(OP_SET, CC_LE, i->dType, pred, i->dType, i->getSrc(0), zero);
1606 bld.mkOp1(OP_MOV, i->dType, i->getDef(0), zero)->setPredicate(CC_P, pred);
1607 rsq = bld.mkOp1(OP_RSQ, i->dType,
1608 bld.getSSA(typeSizeof(i->dType)), i->getSrc(0));
1609 rsq->setPredicate(CC_NOT_P, pred);
1610 i->op = OP_MUL;
1611 i->setSrc(1, rsq->getDef(0));
1612 i->setPredicate(CC_NOT_P, pred);
1613
1614
1615 return true;
1616 }
1617
1618 bool
1619 NVC0LoweringPass::handlePOW(Instruction *i)
1620 {
1621 LValue *val = bld.getScratch();
1622
1623 bld.mkOp1(OP_LG2, TYPE_F32, val, i->getSrc(0));
1624 bld.mkOp2(OP_MUL, TYPE_F32, val, i->getSrc(1), val)->dnz = 1;
1625 bld.mkOp1(OP_PREEX2, TYPE_F32, val, val);
1626
1627 i->op = OP_EX2;
1628 i->setSrc(0, val);
1629 i->setSrc(1, NULL);
1630
1631 return true;
1632 }
1633
1634 bool
1635 NVC0LoweringPass::handleEXPORT(Instruction *i)
1636 {
1637 if (prog->getType() == Program::TYPE_FRAGMENT) {
1638 int id = i->getSrc(0)->reg.data.offset / 4;
1639
1640 if (i->src(0).isIndirect(0)) // TODO, ugly
1641 return false;
1642 i->op = OP_MOV;
1643 i->subOp = NV50_IR_SUBOP_MOV_FINAL;
1644 i->src(0).set(i->src(1));
1645 i->setSrc(1, NULL);
1646 i->setDef(0, new_LValue(func, FILE_GPR));
1647 i->getDef(0)->reg.data.id = id;
1648
1649 prog->maxGPR = MAX2(prog->maxGPR, id);
1650 } else
1651 if (prog->getType() == Program::TYPE_GEOMETRY) {
1652 i->setIndirect(0, 1, gpEmitAddress);
1653 }
1654 return true;
1655 }
1656
1657 bool
1658 NVC0LoweringPass::handleOUT(Instruction *i)
1659 {
1660 Instruction *prev = i->prev;
1661 ImmediateValue stream, prevStream;
1662
1663 // Only merge if the stream ids match. Also, note that the previous
1664 // instruction would have already been lowered, so we take arg1 from it.
1665 if (i->op == OP_RESTART && prev && prev->op == OP_EMIT &&
1666 i->src(0).getImmediate(stream) &&
1667 prev->src(1).getImmediate(prevStream) &&
1668 stream.reg.data.u32 == prevStream.reg.data.u32) {
1669 i->prev->subOp = NV50_IR_SUBOP_EMIT_RESTART;
1670 delete_Instruction(prog, i);
1671 } else {
1672 assert(gpEmitAddress);
1673 i->setDef(0, gpEmitAddress);
1674 i->setSrc(1, i->getSrc(0));
1675 i->setSrc(0, gpEmitAddress);
1676 }
1677 return true;
1678 }
1679
1680 // Generate a binary predicate if an instruction is predicated by
1681 // e.g. an f32 value.
1682 void
1683 NVC0LoweringPass::checkPredicate(Instruction *insn)
1684 {
1685 Value *pred = insn->getPredicate();
1686 Value *pdst;
1687
1688 if (!pred || pred->reg.file == FILE_PREDICATE)
1689 return;
1690 pdst = new_LValue(func, FILE_PREDICATE);
1691
1692 // CAUTION: don't use pdst->getInsn, the definition might not be unique,
1693 // delay turning PSET(FSET(x,y),0) into PSET(x,y) to a later pass
1694
1695 bld.mkCmp(OP_SET, CC_NEU, insn->dType, pdst, insn->dType, bld.mkImm(0), pred);
1696
1697 insn->setPredicate(insn->cc, pdst);
1698 }
1699
1700 //
1701 // - add quadop dance for texturing
1702 // - put FP outputs in GPRs
1703 // - convert instruction sequences
1704 //
1705 bool
1706 NVC0LoweringPass::visit(Instruction *i)
1707 {
1708 bld.setPosition(i, false);
1709
1710 if (i->cc != CC_ALWAYS)
1711 checkPredicate(i);
1712
1713 switch (i->op) {
1714 case OP_TEX:
1715 case OP_TXB:
1716 case OP_TXL:
1717 case OP_TXF:
1718 case OP_TXG:
1719 return handleTEX(i->asTex());
1720 case OP_TXD:
1721 return handleTXD(i->asTex());
1722 case OP_TXLQ:
1723 return handleTXLQ(i->asTex());
1724 case OP_TXQ:
1725 return handleTXQ(i->asTex());
1726 case OP_EX2:
1727 bld.mkOp1(OP_PREEX2, TYPE_F32, i->getDef(0), i->getSrc(0));
1728 i->setSrc(0, i->getDef(0));
1729 break;
1730 case OP_POW:
1731 return handlePOW(i);
1732 case OP_DIV:
1733 return handleDIV(i);
1734 case OP_MOD:
1735 return handleMOD(i);
1736 case OP_SQRT:
1737 return handleSQRT(i);
1738 case OP_EXPORT:
1739 return handleEXPORT(i);
1740 case OP_EMIT:
1741 case OP_RESTART:
1742 return handleOUT(i);
1743 case OP_RDSV:
1744 return handleRDSV(i);
1745 case OP_WRSV:
1746 return handleWRSV(i);
1747 case OP_LOAD:
1748 if (i->src(0).getFile() == FILE_SHADER_INPUT) {
1749 if (prog->getType() == Program::TYPE_COMPUTE) {
1750 i->getSrc(0)->reg.file = FILE_MEMORY_CONST;
1751 i->getSrc(0)->reg.fileIndex = 0;
1752 } else
1753 if (prog->getType() == Program::TYPE_GEOMETRY &&
1754 i->src(0).isIndirect(0)) {
1755 // XXX: this assumes vec4 units
1756 Value *ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
1757 i->getIndirect(0, 0), bld.mkImm(4));
1758 i->setIndirect(0, 0, ptr);
1759 i->op = OP_VFETCH;
1760 } else {
1761 i->op = OP_VFETCH;
1762 assert(prog->getType() != Program::TYPE_FRAGMENT); // INTERP
1763 }
1764 } else if (i->src(0).getFile() == FILE_MEMORY_CONST) {
1765 if (i->src(0).isIndirect(1)) {
1766 Value *ptr;
1767 if (i->src(0).isIndirect(0))
1768 ptr = bld.mkOp3v(OP_INSBF, TYPE_U32, bld.getSSA(),
1769 i->getIndirect(0, 1), bld.mkImm(0x1010),
1770 i->getIndirect(0, 0));
1771 else
1772 ptr = bld.mkOp2v(OP_SHL, TYPE_U32, bld.getSSA(),
1773 i->getIndirect(0, 1), bld.mkImm(16));
1774 i->setIndirect(0, 1, NULL);
1775 i->setIndirect(0, 0, ptr);
1776 i->subOp = NV50_IR_SUBOP_LDC_IS;
1777 }
1778 }
1779 break;
1780 case OP_ATOM:
1781 {
1782 const bool cctl = i->src(0).getFile() == FILE_MEMORY_GLOBAL;
1783 handleATOM(i);
1784 handleCasExch(i, cctl);
1785 }
1786 break;
1787 case OP_SULDB:
1788 case OP_SULDP:
1789 case OP_SUSTB:
1790 case OP_SUSTP:
1791 case OP_SUREDB:
1792 case OP_SUREDP:
1793 if (targ->getChipset() >= NVISA_GK104_CHIPSET)
1794 handleSurfaceOpNVE4(i->asTex());
1795 break;
1796 default:
1797 break;
1798 }
1799 return true;
1800 }
1801
1802 bool
1803 TargetNVC0::runLegalizePass(Program *prog, CGStage stage) const
1804 {
1805 if (stage == CG_STAGE_PRE_SSA) {
1806 NVC0LoweringPass pass(prog);
1807 return pass.run(prog, false, true);
1808 } else
1809 if (stage == CG_STAGE_POST_RA) {
1810 NVC0LegalizePostRA pass(prog);
1811 return pass.run(prog, false, true);
1812 } else
1813 if (stage == CG_STAGE_SSA) {
1814 NVC0LegalizeSSA pass;
1815 return pass.run(prog, false, true);
1816 }
1817 return false;
1818 }
1819
1820 } // namespace nv50_ir