1 //===- AMDGPULegalizerInfo.cpp -----------------------------------*- C++ -*-==//
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 /// \file
10 /// This file implements the targeting of the Machinelegalizer class for
11 /// AMDGPU.
12 /// \todo This should be generated by TableGen.
13 //===----------------------------------------------------------------------===//
14 
15 #include "AMDGPU.h"
16 #include "AMDGPULegalizerInfo.h"
17 #include "AMDGPUTargetMachine.h"
18 #include "llvm/CodeGen/TargetOpcodes.h"
19 #include "llvm/CodeGen/ValueTypes.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/Support/Debug.h"
23 
24 using namespace llvm;
25 using namespace LegalizeActions;
26 
27 AMDGPULegalizerInfo::AMDGPULegalizerInfo(const GCNSubtarget &ST,
28                                          const GCNTargetMachine &TM) {
29   using namespace TargetOpcode;
30 
31   auto GetAddrSpacePtr = [&TM](unsigned AS) {
32     return LLT::pointer(AS, TM.getPointerSizeInBits(AS));
33   };
34 
35   const LLT S1 = LLT::scalar(1);
36   const LLT V2S16 = LLT::vector(2, 16);
37 
38   const LLT S32 = LLT::scalar(32);
39   const LLT S64 = LLT::scalar(64);
40   const LLT S512 = LLT::scalar(512);
41 
42   const LLT GlobalPtr = GetAddrSpacePtr(AMDGPUAS::GLOBAL_ADDRESS);
43   const LLT ConstantPtr = GetAddrSpacePtr(AMDGPUAS::CONSTANT_ADDRESS);
44   const LLT LocalPtr = GetAddrSpacePtr(AMDGPUAS::LOCAL_ADDRESS);
45   const LLT FlatPtr = GetAddrSpacePtr(AMDGPUAS::FLAT_ADDRESS);
46   const LLT PrivatePtr = GetAddrSpacePtr(AMDGPUAS::PRIVATE_ADDRESS);
47 
48   const LLT AddrSpaces[] = {
49     GlobalPtr,
50     ConstantPtr,
51     LocalPtr,
52     FlatPtr,
53     PrivatePtr
54   };
55 
56   setAction({G_ADD, S32}, Legal);
57   setAction({G_ASHR, S32}, Legal);
58   setAction({G_SUB, S32}, Legal);
59   setAction({G_MUL, S32}, Legal);
60   setAction({G_AND, S32}, Legal);
61   setAction({G_OR, S32}, Legal);
62   setAction({G_XOR, S32}, Legal);
63 
64   setAction({G_BITCAST, V2S16}, Legal);
65   setAction({G_BITCAST, 1, S32}, Legal);
66 
67   setAction({G_BITCAST, S32}, Legal);
68   setAction({G_BITCAST, 1, V2S16}, Legal);
69 
70   getActionDefinitionsBuilder(G_FCONSTANT)
71     .legalFor({S32, S64});
72 
73   // G_IMPLICIT_DEF is a no-op so we can make it legal for any value type that
74   // can fit in a register.
75   // FIXME: We need to legalize several more operations before we can add
76   // a test case for size > 512.
77   getActionDefinitionsBuilder(G_IMPLICIT_DEF)
78     .legalIf([=](const LegalityQuery &Query) {
79         return Query.Types[0].getSizeInBits() <= 512;
80     })
81     .clampScalar(0, S1, S512);
82 
83   getActionDefinitionsBuilder(G_CONSTANT)
84     .legalFor({S1, S32, S64});
85 
86   // FIXME: i1 operands to intrinsics should always be legal, but other i1
87   // values may not be legal.  We need to figure out how to distinguish
88   // between these two scenarios.
89   setAction({G_CONSTANT, S1}, Legal);
90 
91   getActionDefinitionsBuilder(
92     { G_FADD, G_FMUL })
93     .legalFor({S32, S64});
94 
95   setAction({G_FCMP, S1}, Legal);
96   setAction({G_FCMP, 1, S32}, Legal);
97   setAction({G_FCMP, 1, S64}, Legal);
98 
99   setAction({G_ZEXT, S64}, Legal);
100   setAction({G_ZEXT, 1, S32}, Legal);
101 
102   setAction({G_SEXT, S64}, Legal);
103   setAction({G_SEXT, 1, S32}, Legal);
104 
105   setAction({G_ANYEXT, S64}, Legal);
106   setAction({G_ANYEXT, 1, S32}, Legal);
107 
108   setAction({G_FPTOSI, S32}, Legal);
109   setAction({G_FPTOSI, 1, S32}, Legal);
110 
111   setAction({G_SITOFP, S32}, Legal);
112   setAction({G_SITOFP, 1, S32}, Legal);
113 
114   setAction({G_FPTOUI, S32}, Legal);
115   setAction({G_FPTOUI, 1, S32}, Legal);
116 
117   for (LLT PtrTy : AddrSpaces) {
118     LLT IdxTy = LLT::scalar(PtrTy.getSizeInBits());
119     setAction({G_GEP, PtrTy}, Legal);
120     setAction({G_GEP, 1, IdxTy}, Legal);
121   }
122 
123   setAction({G_ICMP, S1}, Legal);
124   setAction({G_ICMP, 1, S32}, Legal);
125 
126   setAction({G_CTLZ, S32}, Legal);
127   setAction({G_CTLZ_ZERO_UNDEF, S32}, Legal);
128   setAction({G_CTTZ, S32}, Legal);
129   setAction({G_CTTZ_ZERO_UNDEF, S32}, Legal);
130   setAction({G_BSWAP, S32}, Legal);
131   setAction({G_CTPOP, S32}, Legal);
132 
133   getActionDefinitionsBuilder(G_INTTOPTR)
134     .legalIf([](const LegalityQuery &Query) {
135       return true;
136     });
137 
138   getActionDefinitionsBuilder(G_PTRTOINT)
139     .legalIf([](const LegalityQuery &Query) {
140       return true;
141     });
142 
143   getActionDefinitionsBuilder({G_LOAD, G_STORE})
144     .legalIf([=, &ST](const LegalityQuery &Query) {
145         const LLT &Ty0 = Query.Types[0];
146 
147         // TODO: Decompose private loads into 4-byte components.
148         // TODO: Illegal flat loads on SI
149         switch (Ty0.getSizeInBits()) {
150         case 32:
151         case 64:
152         case 128:
153           return true;
154 
155         case 96:
156           // XXX hasLoadX3
157           return (ST.getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS);
158 
159         case 256:
160         case 512:
161           // TODO: constant loads
162         default:
163           return false;
164         }
165       });
166 
167 
168 
169   setAction({G_SELECT, S32}, Legal);
170   setAction({G_SELECT, 1, S1}, Legal);
171 
172   setAction({G_SHL, S32}, Legal);
173 
174 
175   // FIXME: When RegBankSelect inserts copies, it will only create new
176   // registers with scalar types.  This means we can end up with
177   // G_LOAD/G_STORE/G_GEP instruction with scalar types for their pointer
178   // operands.  In assert builds, the instruction selector will assert
179   // if it sees a generic instruction which isn't legal, so we need to
180   // tell it that scalar types are legal for pointer operands
181   setAction({G_GEP, S64}, Legal);
182 
183   for (unsigned Op : {G_EXTRACT_VECTOR_ELT, G_INSERT_VECTOR_ELT}) {
184     getActionDefinitionsBuilder(Op)
185       .legalIf([=](const LegalityQuery &Query) {
186           const LLT &VecTy = Query.Types[1];
187           const LLT &IdxTy = Query.Types[2];
188           return VecTy.getSizeInBits() % 32 == 0 &&
189             VecTy.getSizeInBits() <= 512 &&
190             IdxTy.getSizeInBits() == 32;
191         });
192   }
193 
194   // FIXME: Doesn't handle extract of illegal sizes.
195   getActionDefinitionsBuilder({G_EXTRACT, G_INSERT})
196     .legalIf([=](const LegalityQuery &Query) {
197         const LLT &Ty0 = Query.Types[0];
198         const LLT &Ty1 = Query.Types[1];
199         return (Ty0.getSizeInBits() % 32 == 0) &&
200                (Ty1.getSizeInBits() % 32 == 0);
201       });
202 
203   getActionDefinitionsBuilder(G_BUILD_VECTOR)
204       .legalIf([=](const LegalityQuery &Query) {
205         const LLT &VecTy = Query.Types[0];
206         const LLT &ScalarTy = Query.Types[1];
207         return VecTy.getSizeInBits() % 32 == 0 &&
208                ScalarTy.getSizeInBits() % 32 == 0 &&
209                VecTy.getSizeInBits() <= 512;
210       });
211   // Merge/Unmerge
212   for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
213     unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
214     unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
215 
216     getActionDefinitionsBuilder(Op)
217       .legalIf([=](const LegalityQuery &Query) {
218           const LLT &BigTy = Query.Types[BigTyIdx];
219           const LLT &LitTy = Query.Types[LitTyIdx];
220           return BigTy.getSizeInBits() % 32 == 0 &&
221                  LitTy.getSizeInBits() % 32 == 0 &&
222                  BigTy.getSizeInBits() <= 512;
223         })
224       // Any vectors left are the wrong size. Scalarize them.
225       .fewerElementsIf([](const LegalityQuery &Query) { return true; },
226                        [](const LegalityQuery &Query) {
227                          return std::make_pair(
228                            0, Query.Types[0].getElementType());
229                        })
230       .fewerElementsIf([](const LegalityQuery &Query) { return true; },
231                        [](const LegalityQuery &Query) {
232                          return std::make_pair(
233                            1, Query.Types[1].getElementType());
234                        });
235 
236   }
237 
238   computeTables();
239   verify(*ST.getInstrInfo());
240 }
241