]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/AArch64/AArch64LegalizerInfo.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / AArch64 / AArch64LegalizerInfo.cpp
1 //===- AArch64LegalizerInfo.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 /// AArch64.
12 /// \todo This should be generated by TableGen.
13 //===----------------------------------------------------------------------===//
14
15 #include "AArch64LegalizerInfo.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/ValueTypes.h"
19 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/Target/TargetOpcodes.h"
23
24 using namespace llvm;
25
26 #ifndef LLVM_BUILD_GLOBAL_ISEL
27 #error "You shouldn't build this"
28 #endif
29
30 AArch64LegalizerInfo::AArch64LegalizerInfo() {
31   using namespace TargetOpcode;
32   const LLT p0 = LLT::pointer(0, 64);
33   const LLT s1 = LLT::scalar(1);
34   const LLT s8 = LLT::scalar(8);
35   const LLT s16 = LLT::scalar(16);
36   const LLT s32 = LLT::scalar(32);
37   const LLT s64 = LLT::scalar(64);
38   const LLT v2s32 = LLT::vector(2, 32);
39   const LLT v4s32 = LLT::vector(4, 32);
40   const LLT v2s64 = LLT::vector(2, 64);
41
42   for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR, G_SHL}) {
43     // These operations naturally get the right answer when used on
44     // GPR32, even if the actual type is narrower.
45     for (auto Ty : {s32, s64, v2s32, v4s32, v2s64})
46       setAction({BinOp, Ty}, Legal);
47
48     for (auto Ty : {s1, s8, s16})
49       setAction({BinOp, Ty}, WidenScalar);
50   }
51
52   setAction({G_GEP, p0}, Legal);
53   setAction({G_GEP, 1, s64}, Legal);
54
55   for (auto Ty : {s1, s8, s16, s32})
56     setAction({G_GEP, 1, Ty}, WidenScalar);
57
58   setAction({G_PTR_MASK, p0}, Legal);
59
60   for (unsigned BinOp : {G_LSHR, G_ASHR, G_SDIV, G_UDIV}) {
61     for (auto Ty : {s32, s64})
62       setAction({BinOp, Ty}, Legal);
63
64     for (auto Ty : {s1, s8, s16})
65       setAction({BinOp, Ty}, WidenScalar);
66   }
67
68   for (unsigned BinOp : {G_SREM, G_UREM})
69     for (auto Ty : { s1, s8, s16, s32, s64 })
70       setAction({BinOp, Ty}, Lower);
71
72   for (unsigned Op : {G_SMULO, G_UMULO})
73       setAction({Op, s64}, Lower);
74
75   for (unsigned Op : {G_UADDE, G_USUBE, G_SADDO, G_SSUBO, G_SMULH, G_UMULH}) {
76     for (auto Ty : { s32, s64 })
77       setAction({Op, Ty}, Legal);
78
79     setAction({Op, 1, s1}, Legal);
80   }
81
82   for (unsigned BinOp : {G_FADD, G_FSUB, G_FMUL, G_FDIV})
83     for (auto Ty : {s32, s64})
84       setAction({BinOp, Ty}, Legal);
85
86   for (unsigned BinOp : {G_FREM, G_FPOW}) {
87     setAction({BinOp, s32}, Libcall);
88     setAction({BinOp, s64}, Libcall);
89   }
90
91   for (auto Ty : {s32, s64, p0}) {
92     setAction({G_INSERT, Ty}, Legal);
93     setAction({G_INSERT, 1, Ty}, Legal);
94   }
95   for (auto Ty : {s1, s8, s16}) {
96     setAction({G_INSERT, Ty}, WidenScalar);
97     setAction({G_INSERT, 1, Ty}, Legal);
98     // FIXME: Can't widen the sources because that violates the constraints on
99     // G_INSERT (It seems entirely reasonable that inputs shouldn't overlap).
100   }
101
102   for (unsigned MemOp : {G_LOAD, G_STORE}) {
103     for (auto Ty : {s8, s16, s32, s64, p0, v2s32})
104       setAction({MemOp, Ty}, Legal);
105
106     setAction({MemOp, s1}, WidenScalar);
107
108     // And everything's fine in addrspace 0.
109     setAction({MemOp, 1, p0}, Legal);
110   }
111
112   // Constants
113   for (auto Ty : {s32, s64}) {
114     setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
115     setAction({TargetOpcode::G_FCONSTANT, Ty}, Legal);
116   }
117
118   setAction({G_CONSTANT, p0}, Legal);
119
120   for (auto Ty : {s1, s8, s16})
121     setAction({TargetOpcode::G_CONSTANT, Ty}, WidenScalar);
122
123   setAction({TargetOpcode::G_FCONSTANT, s16}, WidenScalar);
124
125   setAction({G_ICMP, s1}, Legal);
126   setAction({G_ICMP, 1, s32}, Legal);
127   setAction({G_ICMP, 1, s64}, Legal);
128   setAction({G_ICMP, 1, p0}, Legal);
129
130   for (auto Ty : {s1, s8, s16}) {
131     setAction({G_ICMP, 1, Ty}, WidenScalar);
132   }
133
134   setAction({G_FCMP, s1}, Legal);
135   setAction({G_FCMP, 1, s32}, Legal);
136   setAction({G_FCMP, 1, s64}, Legal);
137
138   // Extensions
139   for (auto Ty : { s1, s8, s16, s32, s64 }) {
140     setAction({G_ZEXT, Ty}, Legal);
141     setAction({G_SEXT, Ty}, Legal);
142     setAction({G_ANYEXT, Ty}, Legal);
143   }
144
145   for (auto Ty : { s1, s8, s16, s32 }) {
146     setAction({G_ZEXT, 1, Ty}, Legal);
147     setAction({G_SEXT, 1, Ty}, Legal);
148     setAction({G_ANYEXT, 1, Ty}, Legal);
149   }
150
151   setAction({G_FPEXT, s64}, Legal);
152   setAction({G_FPEXT, 1, s32}, Legal);
153
154   // Truncations
155   for (auto Ty : { s16, s32 })
156     setAction({G_FPTRUNC, Ty}, Legal);
157
158   for (auto Ty : { s32, s64 })
159     setAction({G_FPTRUNC, 1, Ty}, Legal);
160
161   for (auto Ty : { s1, s8, s16, s32 })
162     setAction({G_TRUNC, Ty}, Legal);
163
164   for (auto Ty : { s8, s16, s32, s64 })
165     setAction({G_TRUNC, 1, Ty}, Legal);
166
167   // Conversions
168   for (auto Ty : { s32, s64 }) {
169     setAction({G_FPTOSI, 0, Ty}, Legal);
170     setAction({G_FPTOUI, 0, Ty}, Legal);
171     setAction({G_SITOFP, 1, Ty}, Legal);
172     setAction({G_UITOFP, 1, Ty}, Legal);
173   }
174   for (auto Ty : { s1, s8, s16 }) {
175     setAction({G_FPTOSI, 0, Ty}, WidenScalar);
176     setAction({G_FPTOUI, 0, Ty}, WidenScalar);
177     setAction({G_SITOFP, 1, Ty}, WidenScalar);
178     setAction({G_UITOFP, 1, Ty}, WidenScalar);
179   }
180
181   for (auto Ty : { s32, s64 }) {
182     setAction({G_FPTOSI, 1, Ty}, Legal);
183     setAction({G_FPTOUI, 1, Ty}, Legal);
184     setAction({G_SITOFP, 0, Ty}, Legal);
185     setAction({G_UITOFP, 0, Ty}, Legal);
186   }
187
188   // Control-flow
189   for (auto Ty : {s1, s8, s16, s32})
190     setAction({G_BRCOND, Ty}, Legal);
191   setAction({G_BRINDIRECT, p0}, Legal);
192
193   // Select
194   for (auto Ty : {s1, s8, s16})
195     setAction({G_SELECT, Ty}, WidenScalar);
196
197   for (auto Ty : {s32, s64, p0})
198     setAction({G_SELECT, Ty}, Legal);
199
200   setAction({G_SELECT, 1, s1}, Legal);
201
202   // Pointer-handling
203   setAction({G_FRAME_INDEX, p0}, Legal);
204   setAction({G_GLOBAL_VALUE, p0}, Legal);
205
206   for (auto Ty : {s1, s8, s16, s32, s64})
207     setAction({G_PTRTOINT, 0, Ty}, Legal);
208
209   setAction({G_PTRTOINT, 1, p0}, Legal);
210
211   setAction({G_INTTOPTR, 0, p0}, Legal);
212   setAction({G_INTTOPTR, 1, s64}, Legal);
213
214   // Casts for 32 and 64-bit width type are just copies.
215   for (auto Ty : {s1, s8, s16, s32, s64}) {
216     setAction({G_BITCAST, 0, Ty}, Legal);
217     setAction({G_BITCAST, 1, Ty}, Legal);
218   }
219
220   // For the sake of copying bits around, the type does not really
221   // matter as long as it fits a register.
222   for (int EltSize = 8; EltSize <= 64; EltSize *= 2) {
223     setAction({G_BITCAST, 0, LLT::vector(128/EltSize, EltSize)}, Legal);
224     setAction({G_BITCAST, 1, LLT::vector(128/EltSize, EltSize)}, Legal);
225     if (EltSize >= 64)
226       continue;
227
228     setAction({G_BITCAST, 0, LLT::vector(64/EltSize, EltSize)}, Legal);
229     setAction({G_BITCAST, 1, LLT::vector(64/EltSize, EltSize)}, Legal);
230     if (EltSize >= 32)
231       continue;
232
233     setAction({G_BITCAST, 0, LLT::vector(32/EltSize, EltSize)}, Legal);
234     setAction({G_BITCAST, 1, LLT::vector(32/EltSize, EltSize)}, Legal);
235   }
236
237   setAction({G_VASTART, p0}, Legal);
238
239   // va_list must be a pointer, but most sized types are pretty easy to handle
240   // as the destination.
241   setAction({G_VAARG, 1, p0}, Legal);
242
243   for (auto Ty : {s8, s16, s32, s64, p0})
244     setAction({G_VAARG, Ty}, Custom);
245
246   computeTables();
247 }
248
249 bool AArch64LegalizerInfo::legalizeCustom(MachineInstr &MI,
250                                           MachineRegisterInfo &MRI,
251                                           MachineIRBuilder &MIRBuilder) const {
252   switch (MI.getOpcode()) {
253   default:
254     // No idea what to do.
255     return false;
256   case TargetOpcode::G_VAARG:
257     return legalizeVaArg(MI, MRI, MIRBuilder);
258   }
259
260   llvm_unreachable("expected switch to return");
261 }
262
263 bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI,
264                                          MachineRegisterInfo &MRI,
265                                          MachineIRBuilder &MIRBuilder) const {
266   MIRBuilder.setInstr(MI);
267   MachineFunction &MF = MIRBuilder.getMF();
268   unsigned Align = MI.getOperand(2).getImm();
269   unsigned Dst = MI.getOperand(0).getReg();
270   unsigned ListPtr = MI.getOperand(1).getReg();
271
272   LLT PtrTy = MRI.getType(ListPtr);
273   LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
274
275   const unsigned PtrSize = PtrTy.getSizeInBits() / 8;
276   unsigned List = MRI.createGenericVirtualRegister(PtrTy);
277   MIRBuilder.buildLoad(
278       List, ListPtr,
279       *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
280                                PtrSize, /* Align = */ PtrSize));
281
282   unsigned DstPtr;
283   if (Align > PtrSize) {
284     // Realign the list to the actual required alignment.
285     unsigned AlignMinus1 = MRI.createGenericVirtualRegister(IntPtrTy);
286     MIRBuilder.buildConstant(AlignMinus1, Align - 1);
287
288     unsigned ListTmp = MRI.createGenericVirtualRegister(PtrTy);
289     MIRBuilder.buildGEP(ListTmp, List, AlignMinus1);
290
291     DstPtr = MRI.createGenericVirtualRegister(PtrTy);
292     MIRBuilder.buildPtrMask(DstPtr, ListTmp, Log2_64(Align));
293   } else
294     DstPtr = List;
295
296   uint64_t ValSize = MRI.getType(Dst).getSizeInBits() / 8;
297   MIRBuilder.buildLoad(
298       Dst, DstPtr,
299       *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
300                                ValSize, std::max(Align, PtrSize)));
301
302   unsigned SizeReg = MRI.createGenericVirtualRegister(IntPtrTy);
303   MIRBuilder.buildConstant(SizeReg, alignTo(ValSize, PtrSize));
304
305   unsigned NewList = MRI.createGenericVirtualRegister(PtrTy);
306   MIRBuilder.buildGEP(NewList, DstPtr, SizeReg);
307
308   MIRBuilder.buildStore(
309       NewList, ListPtr,
310       *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOStore,
311                                PtrSize, /* Align = */ PtrSize));
312
313   MI.eraseFromParent();
314   return true;
315 }