gk110/ir: Use the new rcp/rsq in library
[mesa.git] / src / gallium / drivers / nouveau / codegen / nv50_ir_lowering_nvc0.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 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 /* On nvc0, surface info is obtained via the surface binding points passed
27 * to the SULD/SUST instructions.
28 * On nve4, surface info is stored in c[] and is used by various special
29 * instructions, e.g. for clamping coordinates or generating an address.
30 * They couldn't just have added an equivalent to TIC now, couldn't they ?
31 */
32 #define NVC0_SU_INFO_ADDR 0x00
33 #define NVC0_SU_INFO_FMT 0x04
34 #define NVC0_SU_INFO_DIM_X 0x08
35 #define NVC0_SU_INFO_PITCH 0x0c
36 #define NVC0_SU_INFO_DIM_Y 0x10
37 #define NVC0_SU_INFO_ARRAY 0x14
38 #define NVC0_SU_INFO_DIM_Z 0x18
39 #define NVC0_SU_INFO_UNK1C 0x1c
40 #define NVC0_SU_INFO_WIDTH 0x20
41 #define NVC0_SU_INFO_HEIGHT 0x24
42 #define NVC0_SU_INFO_DEPTH 0x28
43 #define NVC0_SU_INFO_TARGET 0x2c
44 #define NVC0_SU_INFO_BSIZE 0x30
45 #define NVC0_SU_INFO_RAW_X 0x34
46 #define NVC0_SU_INFO_MS_X 0x38
47 #define NVC0_SU_INFO_MS_Y 0x3c
48
49 #define NVC0_SU_INFO__STRIDE 0x40
50
51 #define NVC0_SU_INFO_DIM(i) (0x08 + (i) * 8)
52 #define NVC0_SU_INFO_SIZE(i) (0x20 + (i) * 4)
53 #define NVC0_SU_INFO_MS(i) (0x38 + (i) * 4)
54
55 namespace nv50_ir {
56
57 class NVC0LegalizeSSA : public Pass
58 {
59 private:
60 virtual bool visit(BasicBlock *);
61 virtual bool visit(Function *);
62
63 // we want to insert calls to the builtin library only after optimization
64 void handleDIV(Instruction *); // integer division, modulus
65 void handleRCPRSQLib(Instruction *, Value *[]);
66 void handleRCPRSQ(Instruction *); // double precision float recip/rsqrt
67 void handleFTZ(Instruction *);
68 void handleSET(CmpInstruction *);
69 void handleTEXLOD(TexInstruction *);
70 void handleShift(Instruction *);
71
72 protected:
73 BuildUtil bld;
74 };
75
76 class NVC0LegalizePostRA : public Pass
77 {
78 public:
79 NVC0LegalizePostRA(const Program *);
80
81 private:
82 virtual bool visit(Function *);
83 virtual bool visit(BasicBlock *);
84
85 void replaceCvt(Instruction *);
86 void replaceZero(Instruction *);
87 bool tryReplaceContWithBra(BasicBlock *);
88 void propagateJoin(BasicBlock *);
89
90 struct TexUse
91 {
92 TexUse(Instruction *use, const Instruction *tex, bool after)
93 : insn(use), tex(tex), after(after), level(-1) { }
94 Instruction *insn;
95 const Instruction *tex; // or split / mov
96 bool after;
97 int level;
98 };
99 struct Limits
100 {
101 Limits() { }
102 Limits(int min, int max) : min(min), max(max) { }
103 int min, max;
104 };
105 bool insertTextureBarriers(Function *);
106 inline bool insnDominatedBy(const Instruction *, const Instruction *) const;
107 void findFirstUses(Instruction *texi, std::list<TexUse> &uses);
108 void findFirstUsesBB(int minGPR, int maxGPR, Instruction *start,
109 const Instruction *texi, std::list<TexUse> &uses,
110 unordered_set<const BasicBlock *> &visited);
111 void addTexUse(std::list<TexUse>&, Instruction *, const Instruction *);
112 const Instruction *recurseDef(const Instruction *);
113
114 private:
115 LValue *rZero;
116 LValue *carry;
117 LValue *pOne;
118 const bool needTexBar;
119 };
120
121 class NVC0LoweringPass : public Pass
122 {
123 public:
124 NVC0LoweringPass(Program *);
125
126 protected:
127 bool handleRDSV(Instruction *);
128 bool handleWRSV(Instruction *);
129 bool handleEXPORT(Instruction *);
130 bool handleOUT(Instruction *);
131 bool handleDIV(Instruction *);
132 bool handleMOD(Instruction *);
133 bool handleSQRT(Instruction *);
134 bool handlePOW(Instruction *);
135 bool handleTEX(TexInstruction *);
136 bool handleTXD(TexInstruction *);
137 bool handleTXQ(TexInstruction *);
138 virtual bool handleManualTXD(TexInstruction *);
139 bool handleTXLQ(TexInstruction *);
140 bool handleSUQ(TexInstruction *);
141 bool handleATOM(Instruction *);
142 bool handleCasExch(Instruction *, bool needCctl);
143 void handleSurfaceOpGM107(TexInstruction *);
144 void handleSurfaceOpNVE4(TexInstruction *);
145 void handleSurfaceOpNVC0(TexInstruction *);
146 void handleSharedATOM(Instruction *);
147 void handleSharedATOMNVE4(Instruction *);
148 void handleLDST(Instruction *);
149 bool handleBUFQ(Instruction *);
150 void handlePIXLD(Instruction *);
151
152 void checkPredicate(Instruction *);
153 Value *loadMsAdjInfo32(TexInstruction::Target targ, uint32_t index, int slot, Value *ind, bool bindless);
154
155 virtual bool visit(Instruction *);
156
157 private:
158 virtual bool visit(Function *);
159 virtual bool visit(BasicBlock *);
160
161 void readTessCoord(LValue *dst, int c);
162
163 Value *loadResInfo32(Value *ptr, uint32_t off, uint16_t base);
164 Value *loadResInfo64(Value *ptr, uint32_t off, uint16_t base);
165 Value *loadResLength32(Value *ptr, uint32_t off, uint16_t base);
166 Value *loadSuInfo32(Value *ptr, int slot, uint32_t off, bool bindless);
167 Value *loadBufInfo64(Value *ptr, uint32_t off);
168 Value *loadBufLength32(Value *ptr, uint32_t off);
169 Value *loadUboInfo64(Value *ptr, uint32_t off);
170 Value *loadUboLength32(Value *ptr, uint32_t off);
171 Value *loadMsInfo32(Value *ptr, uint32_t off);
172
173 void adjustCoordinatesMS(TexInstruction *);
174 void processSurfaceCoordsGM107(TexInstruction *);
175 void processSurfaceCoordsNVE4(TexInstruction *);
176 void processSurfaceCoordsNVC0(TexInstruction *);
177 void convertSurfaceFormat(TexInstruction *);
178 void insertOOBSurfaceOpResult(TexInstruction *);
179 Value *calculateSampleOffset(Value *sampleID);
180
181 protected:
182 Value *loadTexHandle(Value *ptr, unsigned int slot);
183
184 BuildUtil bld;
185
186 private:
187 const Target *const targ;
188
189 LValue *gpEmitAddress;
190 };
191
192 } // namespace nv50_ir