nv50/ir: Rename "mkLoad" to "mkLoadv" for consistency.
[mesa.git] / src / gallium / drivers / nv50 / codegen / nv50_ir_build_util.h
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #ifndef __NV50_IR_BUILD_UTIL__
24 #define __NV50_IR_BUILD_UTIL__
25
26 namespace nv50_ir {
27
28 class BuildUtil
29 {
30 public:
31 BuildUtil();
32 BuildUtil(Program *);
33
34 inline void setProgram(Program *);
35 inline Program *getProgram() const { return prog; }
36 inline Function *getFunction() const { return func; }
37
38 // keeps inserting at head/tail of block
39 inline void setPosition(BasicBlock *, bool tail);
40 // position advances only if @after is true
41 inline void setPosition(Instruction *, bool after);
42
43 inline BasicBlock *getBB() { return bb; }
44
45 inline void insert(Instruction *);
46 inline void remove(Instruction *i) { assert(i->bb == bb); bb->remove(i); }
47
48 inline LValue *getScratch(int size = 4, DataFile = FILE_GPR);
49 // scratch value for a single assignment:
50 inline LValue *getSSA(int size = 4, DataFile = FILE_GPR);
51
52 inline Instruction *mkOp(operation, DataType, Value *);
53 Instruction *mkOp1(operation, DataType, Value *, Value *);
54 Instruction *mkOp2(operation, DataType, Value *, Value *, Value *);
55 Instruction *mkOp3(operation, DataType, Value *, Value *, Value *, Value *);
56
57 LValue *mkOp1v(operation, DataType, Value *, Value *);
58 LValue *mkOp2v(operation, DataType, Value *, Value *, Value *);
59 LValue *mkOp3v(operation, DataType, Value *, Value *, Value *, Value *);
60
61 Instruction *mkLoad(DataType, Value *dst, Symbol *, Value *ptr);
62 Instruction *mkStore(operation, DataType, Symbol *, Value *ptr, Value *val);
63
64 LValue *mkLoadv(DataType, Symbol *, Value *ptr);
65
66 Instruction *mkMov(Value *, Value *, DataType = TYPE_U32);
67 Instruction *mkMovToReg(int id, Value *);
68 Instruction *mkMovFromReg(Value *, int id);
69
70 Instruction *mkInterp(unsigned mode, Value *, int32_t offset, Value *rel);
71 Instruction *mkFetch(Value *, DataType, DataFile, int32_t offset,
72 Value *attrRel, Value *primRel);
73
74 Instruction *mkCvt(operation, DataType, Value *, DataType, Value *);
75 CmpInstruction *mkCmp(operation, CondCode, DataType,
76 Value *,
77 Value *, Value *, Value * = NULL);
78 Instruction *mkTex(operation, TexTarget, uint8_t tic, uint8_t tsc,
79 Value **def, Value **src);
80 Instruction *mkQuadop(uint8_t qop, Value *, uint8_t l, Value *, Value *);
81
82 FlowInstruction *mkFlow(operation, void *target, CondCode, Value *pred);
83
84 Instruction *mkSelect(Value *pred, Value *dst, Value *trSrc, Value *flSrc);
85
86 Instruction *mkSplit(Value *half[2], uint8_t halfSize, Value *);
87
88 void mkClobber(DataFile file, uint32_t regMask, int regUnitLog2);
89
90 ImmediateValue *mkImm(float);
91 ImmediateValue *mkImm(uint32_t);
92 ImmediateValue *mkImm(uint64_t);
93
94 ImmediateValue *mkImm(int i) { return mkImm((uint32_t)i); }
95
96 Value *loadImm(Value *dst, float);
97 Value *loadImm(Value *dst, uint32_t);
98 Value *loadImm(Value *dst, uint64_t);
99
100 Value *loadImm(Value *dst, int i) { return loadImm(dst, (uint32_t)i); }
101
102 struct Location
103 {
104 Location(unsigned array, unsigned arrayIdx, unsigned i, unsigned c)
105 : array(array), arrayIdx(arrayIdx), i(i), c(c) { }
106 Location(const Location &l)
107 : array(l.array), arrayIdx(l.arrayIdx), i(l.i), c(l.c) { }
108
109 bool operator==(const Location &l) const
110 {
111 return
112 array == l.array && arrayIdx == l.arrayIdx && i == l.i && c == l.c;
113 }
114
115 bool operator<(const Location &l) const
116 {
117 return array != l.array ? array < l.array :
118 arrayIdx != l.arrayIdx ? arrayIdx < l.arrayIdx :
119 i != l.i ? i < l.i :
120 c != l.c ? c < l.c :
121 false;
122 }
123
124 unsigned array, arrayIdx, i, c;
125 };
126
127 typedef bimap<Location, Value *> ValueMap;
128
129 class DataArray
130 {
131 public:
132 DataArray(BuildUtil *bld) : up(bld) { }
133
134 void setup(unsigned array, unsigned arrayIdx,
135 uint32_t base, int len, int vecDim, int eltSize,
136 DataFile file, int8_t fileIdx);
137
138 inline bool exists(ValueMap&, unsigned int i, unsigned int c);
139
140 Value *load(ValueMap&, int i, int c, Value *ptr);
141 void store(ValueMap&, int i, int c, Value *ptr, Value *value);
142 Value *acquire(ValueMap&, int i, int c);
143
144 private:
145 inline Value *lookup(ValueMap&, unsigned i, unsigned c);
146 inline Value *insert(ValueMap&, unsigned i, unsigned c, Value *v);
147
148 Symbol *mkSymbol(int i, int c);
149
150 private:
151 BuildUtil *up;
152 unsigned array, arrayIdx;
153
154 uint32_t baseAddr;
155 uint32_t arrayLen;
156 Symbol *baseSym;
157
158 uint8_t vecDim;
159 uint8_t eltSize; // in bytes
160
161 DataFile file;
162 bool regOnly;
163 };
164
165 Symbol *mkSymbol(DataFile file, int8_t fileIndex,
166 DataType ty, uint32_t baseAddress);
167
168 Symbol *mkSysVal(SVSemantic svName, uint32_t svIndex);
169
170 private:
171 void init(Program *);
172 void addImmediate(ImmediateValue *);
173 inline unsigned int u32Hash(uint32_t);
174
175 protected:
176 Program *prog;
177 Function *func;
178 Instruction *pos;
179 BasicBlock *bb;
180 bool tail;
181
182 #define NV50_IR_BUILD_IMM_HT_SIZE 256
183
184 ImmediateValue *imms[NV50_IR_BUILD_IMM_HT_SIZE];
185 unsigned int immCount;
186 };
187
188 unsigned int BuildUtil::u32Hash(uint32_t u)
189 {
190 return (u % 273) % NV50_IR_BUILD_IMM_HT_SIZE;
191 }
192
193 void BuildUtil::setProgram(Program *program)
194 {
195 prog = program;
196 }
197
198 void
199 BuildUtil::setPosition(BasicBlock *block, bool atTail)
200 {
201 bb = block;
202 prog = bb->getProgram();
203 func = bb->getFunction();
204 pos = NULL;
205 tail = atTail;
206 }
207
208 void
209 BuildUtil::setPosition(Instruction *i, bool after)
210 {
211 bb = i->bb;
212 prog = bb->getProgram();
213 func = bb->getFunction();
214 pos = i;
215 tail = after;
216 assert(bb);
217 }
218
219 LValue *
220 BuildUtil::getScratch(int size, DataFile f)
221 {
222 LValue *lval = new_LValue(func, f);
223 lval->reg.size = size;
224 return lval;
225 }
226
227 LValue *
228 BuildUtil::getSSA(int size, DataFile f)
229 {
230 LValue *lval = new_LValue(func, f);
231 lval->ssa = 1;
232 lval->reg.size = size;
233 return lval;
234 }
235
236 void BuildUtil::insert(Instruction *i)
237 {
238 if (!pos) {
239 tail ? bb->insertTail(i) : bb->insertHead(i);
240 } else {
241 if (tail) {
242 bb->insertAfter(pos, i);
243 pos = i;
244 } else {
245 bb->insertBefore(pos, i);
246 }
247 }
248 }
249
250 Instruction *
251 BuildUtil::mkOp(operation op, DataType ty, Value *dst)
252 {
253 Instruction *insn = new_Instruction(func, op, ty);
254 insn->setDef(0, dst);
255 insert(insn);
256 if (op == OP_DISCARD || op == OP_EXIT ||
257 op == OP_JOIN ||
258 op == OP_QUADON || op == OP_QUADPOP ||
259 op == OP_EMIT || op == OP_RESTART)
260 insn->fixed = 1;
261 return insn;
262 }
263
264 inline LValue *
265 BuildUtil::mkOp1v(operation op, DataType ty, Value *dst, Value *src)
266 {
267 mkOp1(op, ty, dst, src);
268 return dst->asLValue();
269 }
270
271 inline LValue *
272 BuildUtil::mkOp2v(operation op, DataType ty, Value *dst,
273 Value *src0, Value *src1)
274 {
275 mkOp2(op, ty, dst, src0, src1);
276 return dst->asLValue();
277 }
278
279 inline LValue *
280 BuildUtil::mkOp3v(operation op, DataType ty, Value *dst,
281 Value *src0, Value *src1, Value *src2)
282 {
283 mkOp3(op, ty, dst, src0, src1, src2);
284 return dst->asLValue();
285 }
286
287 inline LValue *
288 BuildUtil::mkLoadv(DataType ty, Symbol *mem, Value *ptr)
289 {
290 LValue *dst = getScratch();
291 mkLoad(ty, dst, mem, ptr);
292 return dst;
293 }
294
295 bool
296 BuildUtil::DataArray::exists(ValueMap &m, unsigned int i, unsigned int c)
297 {
298 assert(i < arrayLen && c < vecDim);
299 return !regOnly || m.r.count(Location(array, arrayIdx, i, c));
300 }
301
302 Value *
303 BuildUtil::DataArray::lookup(ValueMap &m, unsigned i, unsigned c)
304 {
305 ValueMap::r_iterator it = m.r.find(Location(array, arrayIdx, i, c));
306 return it != m.r.end() ? it->second : NULL;
307 }
308
309 Value *
310 BuildUtil::DataArray::insert(ValueMap &m, unsigned i, unsigned c, Value *v)
311 {
312 m.insert(Location(array, arrayIdx, i, c), v);
313 return v;
314 }
315
316 } // namespace nv50_ir
317
318 #endif // __NV50_IR_BUILD_UTIL_H__