aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/nouveau/codegen/nv50_ir_build_util.h
blob: 2305a275d0de5fad0384a13e94a1bbf3bc3ed521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright 2011 Christoph Bumiller
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef __NV50_IR_BUILD_UTIL__
#define __NV50_IR_BUILD_UTIL__

namespace nv50_ir {

class BuildUtil
{
public:
   BuildUtil();
   BuildUtil(Program *);

   inline void setProgram(Program *);
   inline Program *getProgram() const { return prog; }
   inline Function *getFunction() const { return func; }

   // keeps inserting at head/tail of block
   inline void setPosition(BasicBlock *, bool tail);
   // position advances only if @after is true
   inline void setPosition(Instruction *, bool after);

   inline BasicBlock *getBB() { return bb; }

   inline void insert(Instruction *);
   inline void remove(Instruction *i) { assert(i->bb == bb); bb->remove(i); }

   inline LValue *getScratch(int size = 4, DataFile = FILE_GPR);
   // scratch value for a single assignment:
   inline LValue *getSSA(int size = 4, DataFile = FILE_GPR);

   inline Instruction *mkOp(operation, DataType, Value *);
   Instruction *mkOp1(operation, DataType, Value *, Value *);
   Instruction *mkOp2(operation, DataType, Value *, Value *, Value *);
   Instruction *mkOp3(operation, DataType, Value *, Value *, Value *, Value *);

   LValue *mkOp1v(operation, DataType, Value *, Value *);
   LValue *mkOp2v(operation, DataType, Value *, Value *, Value *);
   LValue *mkOp3v(operation, DataType, Value *, Value *, Value *, Value *);

   Instruction *mkLoad(DataType, Value *dst, Symbol *, Value *ptr);
   Instruction *mkStore(operation, DataType, Symbol *, Value *ptr, Value *val);

   LValue *mkLoadv(DataType, Symbol *, Value *ptr);

   Instruction *mkMov(Value *, Value *, DataType = TYPE_U32);
   Instruction *mkMovToReg(int id, Value *);
   Instruction *mkMovFromReg(Value *, int id);

   Instruction *mkInterp(unsigned mode, Value *, int32_t offset, Value *rel);
   Instruction *mkFetch(Value *, DataType, DataFile, int32_t offset,
                        Value *attrRel, Value *primRel);

   Instruction *mkCvt(operation, DataType, Value *, DataType, Value *);
   CmpInstruction *mkCmp(operation, CondCode, DataType,
			 Value *,
			 Value *, Value *, Value * = NULL);
   TexInstruction *mkTex(operation, TexTarget,
                         uint16_t tic, uint16_t tsc,
                         const std::vector<Value *> &def,
                         const std::vector<Value *> &src);
   Instruction *mkQuadop(uint8_t qop, Value *, uint8_t l, Value *, Value *);

   FlowInstruction *mkFlow(operation, void *target, CondCode, Value *pred);

   Instruction *mkSelect(Value *pred, Value *dst, Value *trSrc, Value *flSrc);

   Instruction *mkSplit(Value *half[2], uint8_t halfSize, Value *);

   void mkClobber(DataFile file, uint32_t regMask, int regUnitLog2);

   ImmediateValue *mkImm(float);
   ImmediateValue *mkImm(uint32_t);
   ImmediateValue *mkImm(uint64_t);

   ImmediateValue *mkImm(int i) { return mkImm((uint32_t)i); }

   Value *loadImm(Value *dst, float);
   Value *loadImm(Value *dst, uint32_t);
   Value *loadImm(Value *dst, uint64_t);

   Value *loadImm(Value *dst, int i) { return loadImm(dst, (uint32_t)i); }

   // returns high part of the operation
   static Instruction *split64BitOpPostRA(Function *, Instruction *,
                                          Value *zero, Value *carry);

   struct Location
   {
      Location(unsigned array, unsigned arrayIdx, unsigned i, unsigned c)
         : array(array), arrayIdx(arrayIdx), i(i), c(c) { }
      Location(const Location &l)
         : array(l.array), arrayIdx(l.arrayIdx), i(l.i), c(l.c) { }

      bool operator==(const Location &l) const
      {
         return
            array == l.array && arrayIdx == l.arrayIdx && i == l.i && c == l.c;
      }

      bool operator<(const Location &l) const
      {
         return array != l.array ? array < l.array :
            arrayIdx != l.arrayIdx ? arrayIdx < l.arrayIdx :
            i != l.i ? i < l.i :
            c != l.c ? c < l.c :
            false;
      }

      unsigned array, arrayIdx, i, c;
   };

   typedef bimap<Location, Value *> ValueMap;

   class DataArray
   {
   public:
      DataArray(BuildUtil *bld) : up(bld) { }

      void setup(unsigned array, unsigned arrayIdx,
                 uint32_t base, int len, int vecDim, int eltSize,
                 DataFile file, int8_t fileIdx);

      inline bool exists(ValueMap&, unsigned int i, unsigned int c);

      Value *load(ValueMap&, int i, int c, Value *ptr);
      void store(ValueMap&, int i, int c, Value *ptr, Value *value);
      Value *acquire(ValueMap&, int i, int c);

   private:
      inline Value *lookup(ValueMap&, unsigned i, unsigned c);
      inline Value *insert(ValueMap&, unsigned i, unsigned c, Value *v);

      Symbol *mkSymbol(int i, int c);

   private:
      BuildUtil *up;
      unsigned array, arrayIdx;

      uint32_t baseAddr;
      uint32_t arrayLen;
      Symbol *baseSym;

      uint8_t vecDim;
      uint8_t eltSize; // in bytes

      DataFile file;
      bool regOnly;
   };

   Symbol *mkSymbol(DataFile file, int8_t fileIndex,
                    DataType ty, uint32_t baseAddress);

   Symbol *mkSysVal(SVSemantic svName, uint32_t svIndex);

private:
   void init(Program *);
   void addImmediate(ImmediateValue *);
   inline unsigned int u32Hash(uint32_t);

protected:
   Program *prog;
   Function *func;
   Instruction *pos;
   BasicBlock *bb;
   bool tail;

#define NV50_IR_BUILD_IMM_HT_SIZE 256

   ImmediateValue *imms[NV50_IR_BUILD_IMM_HT_SIZE];
   unsigned int immCount;
};

unsigned int BuildUtil::u32Hash(uint32_t u)
{
   return (u % 273) % NV50_IR_BUILD_IMM_HT_SIZE;
}

void BuildUtil::setProgram(Program *program)
{
   prog = program;
}

void
BuildUtil::setPosition(BasicBlock *block, bool atTail)
{
   bb = block;
   prog = bb->getProgram();
   func = bb->getFunction();
   pos = NULL;
   tail = atTail;
}

void
BuildUtil::setPosition(Instruction *i, bool after)
{
   bb = i->bb;
   prog = bb->getProgram();
   func = bb->getFunction();
   pos = i;
   tail = after;
   assert(bb);
}

LValue *
BuildUtil::getScratch(int size, DataFile f)
{
   LValue *lval = new_LValue(func, f);
   lval->reg.size = size;
   return lval;
}

LValue *
BuildUtil::getSSA(int size, DataFile f)
{
   LValue *lval = new_LValue(func, f);
   lval->ssa = 1;
   lval->reg.size = size;
   return lval;
}

void BuildUtil::insert(Instruction *i)
{
   if (!pos) {
      tail ? bb->insertTail(i) : bb->insertHead(i);
   } else {
      if (tail) {
         bb->insertAfter(pos, i);
         pos = i;
      } else {
         bb->insertBefore(pos, i);
      }
   }
}

Instruction *
BuildUtil::mkOp(operation op, DataType ty, Value *dst)
{
   Instruction *insn = new_Instruction(func, op, ty);
   insn->setDef(0, dst);
   insert(insn);
   if (op == OP_DISCARD || op == OP_EXIT ||
       op == OP_JOIN ||
       op == OP_QUADON || op == OP_QUADPOP ||
       op == OP_EMIT || op == OP_RESTART)
      insn->fixed = 1;
   return insn;
}

inline LValue *
BuildUtil::mkOp1v(operation op, DataType ty, Value *dst, Value *src)
{
   mkOp1(op, ty, dst, src);
   return dst->asLValue();
}

inline LValue *
BuildUtil::mkOp2v(operation op, DataType ty, Value *dst,
                  Value *src0, Value *src1)
{
   mkOp2(op, ty, dst, src0, src1);
   return dst->asLValue();
}

inline LValue *
BuildUtil::mkOp3v(operation op, DataType ty, Value *dst,
                  Value *src0, Value *src1, Value *src2)
{
   mkOp3(op, ty, dst, src0, src1, src2);
   return dst->asLValue();
}

inline LValue *
BuildUtil::mkLoadv(DataType ty, Symbol *mem, Value *ptr)
{
   LValue *dst = getScratch();
   mkLoad(ty, dst, mem, ptr);
   return dst;
}

bool
BuildUtil::DataArray::exists(ValueMap &m, unsigned int i, unsigned int c)
{
   assert(i < arrayLen && c < vecDim);
   return !regOnly || m.r.count(Location(array, arrayIdx, i, c));
}

Value *
BuildUtil::DataArray::lookup(ValueMap &m, unsigned i, unsigned c)
{
   ValueMap::r_iterator it = m.r.find(Location(array, arrayIdx, i, c));
   return it != m.r.end() ? it->second : NULL;
}

Value *
BuildUtil::DataArray::insert(ValueMap &m, unsigned i, unsigned c, Value *v)
{
   m.insert(Location(array, arrayIdx, i, c), v);
   return v;
}

} // namespace nv50_ir

#endif // __NV50_IR_BUILD_UTIL_H__