R600: Add local memory support via LDS
[oota-llvm.git] / lib / Target / R600 / AMDGPUInstructions.td
1 //===-- AMDGPUInstructions.td - Common instruction defs ---*- tablegen -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains instruction defs that are common to all hw codegen
11 // targets.
12 //
13 //===----------------------------------------------------------------------===//
14
15 class AMDGPUInst <dag outs, dag ins, string asm, list<dag> pattern> : Instruction {
16   field bit isRegisterLoad = 0;
17   field bit isRegisterStore = 0;
18
19   let Namespace = "AMDGPU";
20   let OutOperandList = outs;
21   let InOperandList = ins;
22   let AsmString = asm;
23   let Pattern = pattern;
24   let Itinerary = NullALU;
25
26   let TSFlags{63} = isRegisterLoad;
27   let TSFlags{62} = isRegisterStore;
28 }
29
30 class AMDGPUShaderInst <dag outs, dag ins, string asm, list<dag> pattern>
31     : AMDGPUInst<outs, ins, asm, pattern> {
32
33   field bits<32> Inst = 0xffffffff;
34
35 }
36
37 def InstFlag : OperandWithDefaultOps <i32, (ops (i32 0))>;
38
39 def COND_EQ : PatLeaf <
40   (cond),
41   [{switch(N->get()){{default: return false;
42                      case ISD::SETOEQ: case ISD::SETUEQ:
43                      case ISD::SETEQ: return true;}}}]
44 >;
45
46 def COND_NE : PatLeaf <
47   (cond),
48   [{switch(N->get()){{default: return false;
49                      case ISD::SETONE: case ISD::SETUNE:
50                      case ISD::SETNE: return true;}}}]
51 >;
52 def COND_GT : PatLeaf <
53   (cond),
54   [{switch(N->get()){{default: return false;
55                      case ISD::SETOGT: case ISD::SETUGT:
56                      case ISD::SETGT: return true;}}}]
57 >;
58
59 def COND_GE : PatLeaf <
60   (cond),
61   [{switch(N->get()){{default: return false;
62                      case ISD::SETOGE: case ISD::SETUGE:
63                      case ISD::SETGE: return true;}}}]
64 >;
65
66 def COND_LT : PatLeaf <
67   (cond),
68   [{switch(N->get()){{default: return false;
69                      case ISD::SETOLT: case ISD::SETULT:
70                      case ISD::SETLT: return true;}}}]
71 >;
72
73 def COND_LE : PatLeaf <
74   (cond),
75   [{switch(N->get()){{default: return false;
76                      case ISD::SETOLE: case ISD::SETULE:
77                      case ISD::SETLE: return true;}}}]
78 >;
79
80 def COND_NULL : PatLeaf <
81   (cond),
82   [{return false;}]
83 >;
84
85 //===----------------------------------------------------------------------===//
86 // Load/Store Pattern Fragments
87 //===----------------------------------------------------------------------===//
88
89 def zextloadi8_global : PatFrag<(ops node:$ptr), (zextloadi8 node:$ptr), [{
90     return isGlobalLoad(dyn_cast<LoadSDNode>(N));
91 }]>;
92
93 def zextloadi8_constant : PatFrag<(ops node:$ptr), (zextloadi8 node:$ptr), [{
94     return isGlobalLoad(dyn_cast<LoadSDNode>(N));
95 }]>;
96
97 def local_load : PatFrag<(ops node:$ptr), (load node:$ptr), [{
98     return isLocalLoad(dyn_cast<LoadSDNode>(N));
99 }]>;
100
101 def local_store : PatFrag<(ops node:$val, node:$ptr),
102                              (store node:$val, node:$ptr), [{
103     return isLocalStore(dyn_cast<StoreSDNode>(N));
104 }]>;
105
106 class Constants {
107 int TWO_PI = 0x40c90fdb;
108 int PI = 0x40490fdb;
109 int TWO_PI_INV = 0x3e22f983;
110 int FP_UINT_MAX_PLUS_1 = 0x4f800000;    // 1 << 32 in floating point encoding
111 }
112 def CONST : Constants;
113
114 def FP_ZERO : PatLeaf <
115   (fpimm),
116   [{return N->getValueAPF().isZero();}]
117 >;
118
119 def FP_ONE : PatLeaf <
120   (fpimm),
121   [{return N->isExactlyValue(1.0);}]
122 >;
123
124 let isCodeGenOnly = 1, isPseudo = 1 in {
125
126 let usesCustomInserter = 1  in {
127
128 class CLAMP <RegisterClass rc> : AMDGPUShaderInst <
129   (outs rc:$dst),
130   (ins rc:$src0),
131   "CLAMP $dst, $src0",
132   [(set f32:$dst, (int_AMDIL_clamp f32:$src0, (f32 FP_ZERO), (f32 FP_ONE)))]
133 >;
134
135 class FABS <RegisterClass rc> : AMDGPUShaderInst <
136   (outs rc:$dst),
137   (ins rc:$src0),
138   "FABS $dst, $src0",
139   [(set f32:$dst, (fabs f32:$src0))]
140 >;
141
142 class FNEG <RegisterClass rc> : AMDGPUShaderInst <
143   (outs rc:$dst),
144   (ins rc:$src0),
145   "FNEG $dst, $src0",
146   [(set f32:$dst, (fneg f32:$src0))]
147 >;
148
149 } // usesCustomInserter = 1
150
151 multiclass RegisterLoadStore <RegisterClass dstClass, Operand addrClass,
152                     ComplexPattern addrPat> {
153   def RegisterLoad : AMDGPUShaderInst <
154     (outs dstClass:$dst),
155     (ins addrClass:$addr, i32imm:$chan),
156     "RegisterLoad $dst, $addr",
157     [(set i32:$dst, (AMDGPUregister_load addrPat:$addr, (i32 timm:$chan)))]
158   > {
159     let isRegisterLoad = 1;
160   }
161
162   def RegisterStore : AMDGPUShaderInst <
163     (outs),
164     (ins dstClass:$val, addrClass:$addr, i32imm:$chan),
165     "RegisterStore $val, $addr",
166     [(AMDGPUregister_store i32:$val, addrPat:$addr, (i32 timm:$chan))]
167   > {
168     let isRegisterStore = 1;
169   }
170 }
171
172 } // End isCodeGenOnly = 1, isPseudo = 1
173
174 /* Generic helper patterns for intrinsics */
175 /* -------------------------------------- */
176
177 class POW_Common <AMDGPUInst log_ieee, AMDGPUInst exp_ieee, AMDGPUInst mul>
178   : Pat <
179   (fpow f32:$src0, f32:$src1),
180   (exp_ieee (mul f32:$src1, (log_ieee f32:$src0)))
181 >;
182
183 /* Other helper patterns */
184 /* --------------------- */
185
186 /* Extract element pattern */
187 class Extract_Element <ValueType sub_type, ValueType vec_type, int sub_idx, 
188                        SubRegIndex sub_reg>
189   : Pat<
190   (sub_type (vector_extract vec_type:$src, sub_idx)),
191   (EXTRACT_SUBREG $src, sub_reg)
192 >;
193
194 /* Insert element pattern */
195 class Insert_Element <ValueType elem_type, ValueType vec_type,
196                       int sub_idx, SubRegIndex sub_reg>
197   : Pat <
198   (vector_insert vec_type:$vec, elem_type:$elem, sub_idx),
199   (INSERT_SUBREG $vec, $elem, sub_reg)
200 >;
201
202 // Vector Build pattern
203 class Vector1_Build <ValueType vecType, ValueType elemType,
204                      RegisterClass rc> : Pat <
205   (vecType (build_vector elemType:$src)),
206   (vecType (COPY_TO_REGCLASS $src, rc))
207 >;
208
209 class Vector2_Build <ValueType vecType, ValueType elemType> : Pat <
210   (vecType (build_vector elemType:$sub0, elemType:$sub1)),
211   (INSERT_SUBREG (INSERT_SUBREG
212     (vecType (IMPLICIT_DEF)), $sub0, sub0), $sub1, sub1)
213 >;
214
215 class Vector4_Build <ValueType vecType, ValueType elemType> : Pat <
216   (vecType (build_vector elemType:$x, elemType:$y, elemType:$z, elemType:$w)),
217   (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG
218     (vecType (IMPLICIT_DEF)), $x, sub0), $y, sub1), $z, sub2), $w, sub3)
219 >;
220
221 class Vector8_Build <ValueType vecType, ValueType elemType> : Pat <
222   (vecType (build_vector elemType:$sub0, elemType:$sub1,
223                          elemType:$sub2, elemType:$sub3,
224                          elemType:$sub4, elemType:$sub5,
225                          elemType:$sub6, elemType:$sub7)),
226   (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG
227     (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG
228     (vecType (IMPLICIT_DEF)), $sub0, sub0), $sub1, sub1),
229                               $sub2, sub2), $sub3, sub3),
230                               $sub4, sub4), $sub5, sub5),
231                               $sub6, sub6), $sub7, sub7)
232 >;
233
234 class Vector16_Build <ValueType vecType, ValueType elemType> : Pat <
235   (vecType (build_vector elemType:$sub0, elemType:$sub1,
236                          elemType:$sub2, elemType:$sub3,
237                          elemType:$sub4, elemType:$sub5,
238                          elemType:$sub6, elemType:$sub7,
239                          elemType:$sub8, elemType:$sub9,
240                          elemType:$sub10, elemType:$sub11,
241                          elemType:$sub12, elemType:$sub13,
242                          elemType:$sub14, elemType:$sub15)),
243   (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG
244     (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG
245     (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG
246     (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG (INSERT_SUBREG
247     (vecType (IMPLICIT_DEF)), $sub0, sub0), $sub1, sub1),
248                             $sub2, sub2), $sub3, sub3),
249                             $sub4, sub4), $sub5, sub5),
250                             $sub6, sub6), $sub7, sub7),
251                             $sub8, sub8), $sub9, sub9),
252                             $sub10, sub10), $sub11, sub11),
253                             $sub12, sub12), $sub13, sub13),
254                             $sub14, sub14), $sub15, sub15)
255 >;
256
257 // XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
258 // can handle COPY instructions.
259 // bitconvert pattern
260 class BitConvert <ValueType dt, ValueType st, RegisterClass rc> : Pat <
261   (dt (bitconvert (st rc:$src0))),
262   (dt rc:$src0)
263 >;
264
265 // XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
266 // can handle COPY instructions.
267 class DwordAddrPat<ValueType vt, RegisterClass rc> : Pat <
268   (vt (AMDGPUdwordaddr (vt rc:$addr))),
269   (vt rc:$addr)
270 >;
271
272 // BFI_INT patterns
273
274 multiclass BFIPatterns <Instruction BFI_INT> {
275
276   // Definition from ISA doc:
277   // (y & x) | (z & ~x)
278   def : Pat <
279     (or (and i32:$y, i32:$x), (and i32:$z, (not i32:$x))),
280     (BFI_INT $x, $y, $z)
281   >;
282
283   // SHA-256 Ch function
284   // z ^ (x & (y ^ z))
285   def : Pat <
286     (xor i32:$z, (and i32:$x, (xor i32:$y, i32:$z))),
287     (BFI_INT $x, $y, $z)
288   >;
289
290 }
291
292 // SHA-256 Ma patterns
293
294 // ((x & z) | (y & (x | z))) -> BFI_INT (XOR x, y), z, y
295 class SHA256MaPattern <Instruction BFI_INT, Instruction XOR> : Pat <
296   (or (and i32:$x, i32:$z), (and i32:$y, (or i32:$x, i32:$z))),
297   (BFI_INT (XOR i32:$x, i32:$y), i32:$z, i32:$y)
298 >;
299
300 // Bitfield extract patterns
301
302 def legalshift32 : ImmLeaf <i32, [{return Imm >=0 && Imm < 32;}]>;
303 def bfemask : PatLeaf <(imm), [{return isMask_32(N->getZExtValue());}],
304                             SDNodeXForm<imm, [{ return CurDAG->getTargetConstant(CountTrailingOnes_32(N->getZExtValue()), MVT::i32);}]>>;
305
306 class BFEPattern <Instruction BFE> : Pat <
307   (and (srl i32:$x, legalshift32:$y), bfemask:$z),
308   (BFE $x, $y, $z)
309 >;
310
311 // rotr pattern
312 class ROTRPattern <Instruction BIT_ALIGN> : Pat <
313   (rotr i32:$src0, i32:$src1),
314   (BIT_ALIGN $src0, $src0, $src1)
315 >;
316
317 include "R600Instructions.td"
318
319 include "SIInstrInfo.td"
320