blob: 9a86cac44b514e99fa062049d65c0dc79b9d12e7 [file] [log] [blame]
Tim Northover69fa84a2016-10-14 22:18:18 +00001//===- AArch64LegalizerInfo.cpp ----------------------------------*- C++ -*-==//
Tim Northover33b07d62016-07-22 20:03:43 +00002//
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
Tim Northover69fa84a2016-10-14 22:18:18 +000015#include "AArch64LegalizerInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
Tim Northover91366172017-02-15 23:22:50 +000017#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
Tim Northover33b07d62016-07-22 20:03:43 +000019#include "llvm/CodeGen/ValueTypes.h"
Tim Northover33b07d62016-07-22 20:03:43 +000020#include "llvm/IR/DerivedTypes.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/IR/Type.h"
Tim Northover33b07d62016-07-22 20:03:43 +000022#include "llvm/Target/TargetOpcodes.h"
23
24using namespace llvm;
25
Tim Northover69fa84a2016-10-14 22:18:18 +000026AArch64LegalizerInfo::AArch64LegalizerInfo() {
Ahmed Bougachaad30db32016-08-02 15:10:28 +000027 using namespace TargetOpcode;
Tim Northover5ae83502016-09-15 09:20:34 +000028 const LLT p0 = LLT::pointer(0, 64);
Tim Northoverea904f92016-08-19 22:40:00 +000029 const LLT s1 = LLT::scalar(1);
Tim Northover9656f142016-08-04 20:54:13 +000030 const LLT s8 = LLT::scalar(8);
31 const LLT s16 = LLT::scalar(16);
Ahmed Bougachaad30db32016-08-02 15:10:28 +000032 const LLT s32 = LLT::scalar(32);
33 const LLT s64 = LLT::scalar(64);
34 const LLT v2s32 = LLT::vector(2, 32);
35 const LLT v4s32 = LLT::vector(4, 32);
36 const LLT v2s64 = LLT::vector(2, 64);
37
Tim Northoverff5e7e12017-06-30 20:27:36 +000038 for (auto Ty : {p0, s1, s8, s16, s32, s64})
39 setAction({G_IMPLICIT_DEF, Ty}, Legal);
40
Aditya Nandakumarefd8a842017-08-23 20:45:48 +000041 for (auto Ty : {s16, s32, s64})
42 setAction({G_PHI, Ty}, Legal);
43
Aditya Nandakumar892979e2017-08-25 04:57:27 +000044 for (auto Ty : {s1, s8})
45 setAction({G_PHI, Ty}, WidenScalar);
46
Quentin Colombet24203cf2017-01-27 01:13:30 +000047 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR, G_SHL}) {
Tim Northoverfe880a82016-08-25 17:37:39 +000048 // These operations naturally get the right answer when used on
49 // GPR32, even if the actual type is narrower.
Ahmed Bougachacfb384d2017-01-23 21:10:05 +000050 for (auto Ty : {s32, s64, v2s32, v4s32, v2s64})
Quentin Colombete15e4602017-01-27 01:13:25 +000051 setAction({BinOp, Ty}, Legal);
Ahmed Bougachacfb384d2017-01-23 21:10:05 +000052
53 for (auto Ty : {s1, s8, s16})
Quentin Colombete15e4602017-01-27 01:13:25 +000054 setAction({BinOp, Ty}, WidenScalar);
Tim Northover9656f142016-08-04 20:54:13 +000055 }
56
Quentin Colombete15e4602017-01-27 01:13:25 +000057 setAction({G_GEP, p0}, Legal);
58 setAction({G_GEP, 1, s64}, Legal);
Tim Northover22d82cf2016-09-15 11:02:19 +000059
60 for (auto Ty : {s1, s8, s16, s32})
Quentin Colombete15e4602017-01-27 01:13:25 +000061 setAction({G_GEP, 1, Ty}, WidenScalar);
Tim Northover22d82cf2016-09-15 11:02:19 +000062
Tim Northover398c5f52017-02-14 20:56:29 +000063 setAction({G_PTR_MASK, p0}, Legal);
64
Quentin Colombet24203cf2017-01-27 01:13:30 +000065 for (unsigned BinOp : {G_LSHR, G_ASHR, G_SDIV, G_UDIV}) {
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +000066 for (auto Ty : {s32, s64})
Quentin Colombete15e4602017-01-27 01:13:25 +000067 setAction({BinOp, Ty}, Legal);
Ahmed Bougacha2ac5bf92016-08-16 14:02:47 +000068
Tim Northover7a753d92016-08-26 17:46:06 +000069 for (auto Ty : {s1, s8, s16})
Quentin Colombete15e4602017-01-27 01:13:25 +000070 setAction({BinOp, Ty}, WidenScalar);
Tim Northover7a753d92016-08-26 17:46:06 +000071 }
72
Quentin Colombet24203cf2017-01-27 01:13:30 +000073 for (unsigned BinOp : {G_SREM, G_UREM})
Tim Northovercecee562016-08-26 17:46:13 +000074 for (auto Ty : { s1, s8, s16, s32, s64 })
Quentin Colombete15e4602017-01-27 01:13:25 +000075 setAction({BinOp, Ty}, Lower);
Tim Northovercecee562016-08-26 17:46:13 +000076
Tim Northover0a9b2792017-02-08 21:22:15 +000077 for (unsigned Op : {G_SMULO, G_UMULO})
78 setAction({Op, s64}, Lower);
79
80 for (unsigned Op : {G_UADDE, G_USUBE, G_SADDO, G_SSUBO, G_SMULH, G_UMULH}) {
Tim Northover438c77c2016-08-25 17:37:32 +000081 for (auto Ty : { s32, s64 })
Quentin Colombete15e4602017-01-27 01:13:25 +000082 setAction({Op, Ty}, Legal);
Tim Northover438c77c2016-08-25 17:37:32 +000083
Quentin Colombete15e4602017-01-27 01:13:25 +000084 setAction({Op, 1, s1}, Legal);
Tim Northoverd8a6d7c2016-08-25 17:37:41 +000085 }
86
Daniel Sanders40b66d62017-07-18 14:10:07 +000087 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMA, G_FMUL, G_FDIV})
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +000088 for (auto Ty : {s32, s64})
Quentin Colombete15e4602017-01-27 01:13:25 +000089 setAction({BinOp, Ty}, Legal);
Ahmed Bougacha33e19fe2016-08-18 16:05:11 +000090
Tim Northovere0418412017-02-08 23:23:39 +000091 for (unsigned BinOp : {G_FREM, G_FPOW}) {
92 setAction({BinOp, s32}, Libcall);
93 setAction({BinOp, s64}, Libcall);
94 }
Tim Northoveredb3c8c2016-08-29 19:07:16 +000095
Tim Northover3e6a7af2017-03-03 23:05:47 +000096 for (auto Ty : {s32, s64, p0}) {
Tim Northover0e6afbd2017-02-06 21:56:47 +000097 setAction({G_INSERT, Ty}, Legal);
98 setAction({G_INSERT, 1, Ty}, Legal);
99 }
100 for (auto Ty : {s1, s8, s16}) {
101 setAction({G_INSERT, Ty}, WidenScalar);
Tim Northover3e6a7af2017-03-03 23:05:47 +0000102 setAction({G_INSERT, 1, Ty}, Legal);
Tim Northover0e6afbd2017-02-06 21:56:47 +0000103 // FIXME: Can't widen the sources because that violates the constraints on
104 // G_INSERT (It seems entirely reasonable that inputs shouldn't overlap).
105 }
106
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000107 for (auto Ty : {s1, s8, s16, s32, s64, p0})
108 setAction({G_EXTRACT, Ty}, Legal);
109
110 for (auto Ty : {s32, s64})
111 setAction({G_EXTRACT, 1, Ty}, Legal);
112
Quentin Colombet24203cf2017-01-27 01:13:30 +0000113 for (unsigned MemOp : {G_LOAD, G_STORE}) {
Quentin Colombetd3126d52016-10-11 00:21:08 +0000114 for (auto Ty : {s8, s16, s32, s64, p0, v2s32})
Quentin Colombete15e4602017-01-27 01:13:25 +0000115 setAction({MemOp, Ty}, Legal);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000116
Quentin Colombete15e4602017-01-27 01:13:25 +0000117 setAction({MemOp, s1}, WidenScalar);
Tim Northovera01bece2016-08-23 19:30:42 +0000118
119 // And everything's fine in addrspace 0.
Quentin Colombete15e4602017-01-27 01:13:25 +0000120 setAction({MemOp, 1, p0}, Legal);
Tim Northover3c73e362016-08-23 18:20:09 +0000121 }
122
Tim Northoverb3a0be42016-08-23 21:01:20 +0000123 // Constants
Tim Northoverea904f92016-08-19 22:40:00 +0000124 for (auto Ty : {s32, s64}) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000125 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
126 setAction({TargetOpcode::G_FCONSTANT, Ty}, Legal);
Tim Northoverea904f92016-08-19 22:40:00 +0000127 }
128
Quentin Colombete15e4602017-01-27 01:13:25 +0000129 setAction({G_CONSTANT, p0}, Legal);
Tim Northover7a1ec012016-08-25 17:37:35 +0000130
Tim Northoverea904f92016-08-19 22:40:00 +0000131 for (auto Ty : {s1, s8, s16})
Quentin Colombete15e4602017-01-27 01:13:25 +0000132 setAction({TargetOpcode::G_CONSTANT, Ty}, WidenScalar);
Tim Northoverea904f92016-08-19 22:40:00 +0000133
Quentin Colombete15e4602017-01-27 01:13:25 +0000134 setAction({TargetOpcode::G_FCONSTANT, s16}, WidenScalar);
Tim Northover9656f142016-08-04 20:54:13 +0000135
Quentin Colombete15e4602017-01-27 01:13:25 +0000136 setAction({G_ICMP, 1, s32}, Legal);
137 setAction({G_ICMP, 1, s64}, Legal);
138 setAction({G_ICMP, 1, p0}, Legal);
Tim Northover6cd4b232016-08-23 21:01:26 +0000139
Tim Northover051b8ad2016-08-26 17:46:17 +0000140 for (auto Ty : {s1, s8, s16}) {
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000141 setAction({G_ICMP, Ty}, WidenScalar);
142 setAction({G_FCMP, Ty}, WidenScalar);
Quentin Colombete15e4602017-01-27 01:13:25 +0000143 setAction({G_ICMP, 1, Ty}, WidenScalar);
Tim Northover6cd4b232016-08-23 21:01:26 +0000144 }
145
Aditya Nandakumar02c602e2017-07-31 17:00:16 +0000146 setAction({G_ICMP, s32}, Legal);
147 setAction({G_FCMP, s32}, Legal);
Quentin Colombete15e4602017-01-27 01:13:25 +0000148 setAction({G_FCMP, 1, s32}, Legal);
149 setAction({G_FCMP, 1, s64}, Legal);
Tim Northover30bd36e2016-08-26 17:46:19 +0000150
Tim Northover2c4a8382016-08-25 17:37:25 +0000151 // Extensions
152 for (auto Ty : { s1, s8, s16, s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000153 setAction({G_ZEXT, Ty}, Legal);
154 setAction({G_SEXT, Ty}, Legal);
155 setAction({G_ANYEXT, Ty}, Legal);
Tim Northover2c4a8382016-08-25 17:37:25 +0000156 }
157
158 for (auto Ty : { s1, s8, s16, s32 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000159 setAction({G_ZEXT, 1, Ty}, Legal);
160 setAction({G_SEXT, 1, Ty}, Legal);
161 setAction({G_ANYEXT, 1, Ty}, Legal);
Tim Northover2c4a8382016-08-25 17:37:25 +0000162 }
163
Quentin Colombete15e4602017-01-27 01:13:25 +0000164 setAction({G_FPEXT, s64}, Legal);
165 setAction({G_FPEXT, 1, s32}, Legal);
Tim Northoverbc1701c2016-08-26 17:46:22 +0000166
Tim Northover438c77c2016-08-25 17:37:32 +0000167 // Truncations
168 for (auto Ty : { s16, s32 })
Quentin Colombete15e4602017-01-27 01:13:25 +0000169 setAction({G_FPTRUNC, Ty}, Legal);
Tim Northover438c77c2016-08-25 17:37:32 +0000170
171 for (auto Ty : { s32, s64 })
Quentin Colombete15e4602017-01-27 01:13:25 +0000172 setAction({G_FPTRUNC, 1, Ty}, Legal);
Tim Northover438c77c2016-08-25 17:37:32 +0000173
174 for (auto Ty : { s1, s8, s16, s32 })
Quentin Colombete15e4602017-01-27 01:13:25 +0000175 setAction({G_TRUNC, Ty}, Legal);
Tim Northover438c77c2016-08-25 17:37:32 +0000176
177 for (auto Ty : { s8, s16, s32, s64 })
Quentin Colombete15e4602017-01-27 01:13:25 +0000178 setAction({G_TRUNC, 1, Ty}, Legal);
Tim Northover438c77c2016-08-25 17:37:32 +0000179
Tim Northover5d0eaa42016-08-26 17:45:58 +0000180 // Conversions
Ahmed Bougachad2948232017-01-20 01:37:24 +0000181 for (auto Ty : { s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000182 setAction({G_FPTOSI, 0, Ty}, Legal);
183 setAction({G_FPTOUI, 0, Ty}, Legal);
184 setAction({G_SITOFP, 1, Ty}, Legal);
185 setAction({G_UITOFP, 1, Ty}, Legal);
Tim Northover5d0eaa42016-08-26 17:45:58 +0000186 }
Ahmed Bougachad2948232017-01-20 01:37:24 +0000187 for (auto Ty : { s1, s8, s16 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000188 setAction({G_FPTOSI, 0, Ty}, WidenScalar);
189 setAction({G_FPTOUI, 0, Ty}, WidenScalar);
190 setAction({G_SITOFP, 1, Ty}, WidenScalar);
191 setAction({G_UITOFP, 1, Ty}, WidenScalar);
Ahmed Bougachad2948232017-01-20 01:37:24 +0000192 }
Tim Northover5d0eaa42016-08-26 17:45:58 +0000193
194 for (auto Ty : { s32, s64 }) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000195 setAction({G_FPTOSI, 1, Ty}, Legal);
196 setAction({G_FPTOUI, 1, Ty}, Legal);
197 setAction({G_SITOFP, 0, Ty}, Legal);
198 setAction({G_UITOFP, 0, Ty}, Legal);
Tim Northover5d0eaa42016-08-26 17:45:58 +0000199 }
Tim Northover438c77c2016-08-25 17:37:32 +0000200
Tim Northoverb3a0be42016-08-23 21:01:20 +0000201 // Control-flow
Tim Northover6aacd272016-10-12 22:48:36 +0000202 for (auto Ty : {s1, s8, s16, s32})
Quentin Colombete15e4602017-01-27 01:13:25 +0000203 setAction({G_BRCOND, Ty}, Legal);
Kristof Beyls65a12c02017-01-30 09:13:18 +0000204 setAction({G_BRINDIRECT, p0}, Legal);
Ahmed Bougachaad30db32016-08-02 15:10:28 +0000205
Tim Northover1d18a992016-08-26 17:46:03 +0000206 // Select
Tim Northover868332d2017-02-06 23:41:27 +0000207 for (auto Ty : {s1, s8, s16})
208 setAction({G_SELECT, Ty}, WidenScalar);
209
210 for (auto Ty : {s32, s64, p0})
Quentin Colombete15e4602017-01-27 01:13:25 +0000211 setAction({G_SELECT, Ty}, Legal);
Tim Northover1d18a992016-08-26 17:46:03 +0000212
Quentin Colombete15e4602017-01-27 01:13:25 +0000213 setAction({G_SELECT, 1, s1}, Legal);
Tim Northover1d18a992016-08-26 17:46:03 +0000214
Tim Northoverb3a0be42016-08-23 21:01:20 +0000215 // Pointer-handling
Quentin Colombete15e4602017-01-27 01:13:25 +0000216 setAction({G_FRAME_INDEX, p0}, Legal);
217 setAction({G_GLOBAL_VALUE, p0}, Legal);
Ahmed Bougacha0306b5e2016-08-16 14:02:42 +0000218
Tim Northover037af52c2016-10-31 18:31:09 +0000219 for (auto Ty : {s1, s8, s16, s32, s64})
Quentin Colombete15e4602017-01-27 01:13:25 +0000220 setAction({G_PTRTOINT, 0, Ty}, Legal);
Tim Northover037af52c2016-10-31 18:31:09 +0000221
Quentin Colombete15e4602017-01-27 01:13:25 +0000222 setAction({G_PTRTOINT, 1, p0}, Legal);
Tim Northovera01bece2016-08-23 19:30:42 +0000223
Quentin Colombete15e4602017-01-27 01:13:25 +0000224 setAction({G_INTTOPTR, 0, p0}, Legal);
225 setAction({G_INTTOPTR, 1, s64}, Legal);
Tim Northover456a3c02016-08-23 19:30:38 +0000226
Quentin Colombet404e4352016-10-12 03:57:43 +0000227 // Casts for 32 and 64-bit width type are just copies.
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000228 for (auto Ty : {s1, s8, s16, s32, s64}) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000229 setAction({G_BITCAST, 0, Ty}, Legal);
230 setAction({G_BITCAST, 1, Ty}, Legal);
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000231 }
232
Quentin Colombetdb643d92016-10-13 00:12:01 +0000233 // For the sake of copying bits around, the type does not really
234 // matter as long as it fits a register.
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000235 for (int EltSize = 8; EltSize <= 64; EltSize *= 2) {
Quentin Colombete15e4602017-01-27 01:13:25 +0000236 setAction({G_BITCAST, 0, LLT::vector(128/EltSize, EltSize)}, Legal);
237 setAction({G_BITCAST, 1, LLT::vector(128/EltSize, EltSize)}, Legal);
Quentin Colombetdb643d92016-10-13 00:12:01 +0000238 if (EltSize >= 64)
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000239 continue;
240
Quentin Colombete15e4602017-01-27 01:13:25 +0000241 setAction({G_BITCAST, 0, LLT::vector(64/EltSize, EltSize)}, Legal);
242 setAction({G_BITCAST, 1, LLT::vector(64/EltSize, EltSize)}, Legal);
Quentin Colombetdb643d92016-10-13 00:12:01 +0000243 if (EltSize >= 32)
244 continue;
245
Quentin Colombete15e4602017-01-27 01:13:25 +0000246 setAction({G_BITCAST, 0, LLT::vector(32/EltSize, EltSize)}, Legal);
247 setAction({G_BITCAST, 1, LLT::vector(32/EltSize, EltSize)}, Legal);
Tim Northoverc1d8c2b2016-10-11 22:29:23 +0000248 }
249
Tim Northovere9600d82017-02-08 17:57:27 +0000250 setAction({G_VASTART, p0}, Legal);
251
Tim Northover91366172017-02-15 23:22:50 +0000252 // va_list must be a pointer, but most sized types are pretty easy to handle
253 // as the destination.
254 setAction({G_VAARG, 1, p0}, Legal);
255
256 for (auto Ty : {s8, s16, s32, s64, p0})
257 setAction({G_VAARG, Ty}, Custom);
258
Tim Northover33b07d62016-07-22 20:03:43 +0000259 computeTables();
260}
Tim Northover91366172017-02-15 23:22:50 +0000261
262bool AArch64LegalizerInfo::legalizeCustom(MachineInstr &MI,
263 MachineRegisterInfo &MRI,
264 MachineIRBuilder &MIRBuilder) const {
265 switch (MI.getOpcode()) {
266 default:
267 // No idea what to do.
268 return false;
269 case TargetOpcode::G_VAARG:
270 return legalizeVaArg(MI, MRI, MIRBuilder);
271 }
272
273 llvm_unreachable("expected switch to return");
274}
275
276bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI,
277 MachineRegisterInfo &MRI,
278 MachineIRBuilder &MIRBuilder) const {
279 MIRBuilder.setInstr(MI);
280 MachineFunction &MF = MIRBuilder.getMF();
281 unsigned Align = MI.getOperand(2).getImm();
282 unsigned Dst = MI.getOperand(0).getReg();
283 unsigned ListPtr = MI.getOperand(1).getReg();
284
285 LLT PtrTy = MRI.getType(ListPtr);
286 LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
287
288 const unsigned PtrSize = PtrTy.getSizeInBits() / 8;
289 unsigned List = MRI.createGenericVirtualRegister(PtrTy);
290 MIRBuilder.buildLoad(
291 List, ListPtr,
292 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
293 PtrSize, /* Align = */ PtrSize));
294
295 unsigned DstPtr;
296 if (Align > PtrSize) {
297 // Realign the list to the actual required alignment.
Aditya Nandakumar17451212017-07-06 19:40:07 +0000298 auto AlignMinus1 = MIRBuilder.buildConstant(IntPtrTy, Align - 1);
Tim Northover91366172017-02-15 23:22:50 +0000299
300 unsigned ListTmp = MRI.createGenericVirtualRegister(PtrTy);
Aditya Nandakumar17451212017-07-06 19:40:07 +0000301 MIRBuilder.buildGEP(ListTmp, List, AlignMinus1->getOperand(0).getReg());
Tim Northover91366172017-02-15 23:22:50 +0000302
303 DstPtr = MRI.createGenericVirtualRegister(PtrTy);
304 MIRBuilder.buildPtrMask(DstPtr, ListTmp, Log2_64(Align));
305 } else
306 DstPtr = List;
307
308 uint64_t ValSize = MRI.getType(Dst).getSizeInBits() / 8;
309 MIRBuilder.buildLoad(
310 Dst, DstPtr,
311 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
312 ValSize, std::max(Align, PtrSize)));
313
314 unsigned SizeReg = MRI.createGenericVirtualRegister(IntPtrTy);
315 MIRBuilder.buildConstant(SizeReg, alignTo(ValSize, PtrSize));
316
317 unsigned NewList = MRI.createGenericVirtualRegister(PtrTy);
318 MIRBuilder.buildGEP(NewList, DstPtr, SizeReg);
319
320 MIRBuilder.buildStore(
321 NewList, ListPtr,
322 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOStore,
323 PtrSize, /* Align = */ PtrSize));
324
325 MI.eraseFromParent();
326 return true;
327}