blob: 1bc5e424eac4c82b94d764c3c61716f0e0783eba [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===-- AArch6464FastISel.cpp - AArch64 FastISel implementation -----------===//
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 defines the AArch64-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// AArch64GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AArch64.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000017#include "AArch64Subtarget.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000018#include "AArch64TargetMachine.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000019#include "MCTargetDesc/AArch64AddressingModes.h"
Juergen Ributzka50a40052014-08-01 18:39:24 +000020#include "llvm/Analysis/BranchProbabilityInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000021#include "llvm/CodeGen/CallingConvLower.h"
22#include "llvm/CodeGen/FastISel.h"
23#include "llvm/CodeGen/FunctionLoweringInfo.h"
24#include "llvm/CodeGen/MachineConstantPool.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GetElementPtrTypeIterator.h"
33#include "llvm/IR/GlobalAlias.h"
34#include "llvm/IR/GlobalVariable.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/Operator.h"
38#include "llvm/Support/CommandLine.h"
39using namespace llvm;
40
41namespace {
42
43class AArch64FastISel : public FastISel {
Tim Northover3b0846e2014-05-24 12:50:23 +000044 class Address {
45 public:
46 typedef enum {
47 RegBase,
48 FrameIndexBase
49 } BaseKind;
50
51 private:
52 BaseKind Kind;
Juergen Ributzkab46ea082014-08-19 19:44:17 +000053 AArch64_AM::ShiftExtendType ExtType;
Tim Northover3b0846e2014-05-24 12:50:23 +000054 union {
55 unsigned Reg;
56 int FI;
57 } Base;
Juergen Ributzkab46ea082014-08-19 19:44:17 +000058 unsigned OffsetReg;
59 unsigned Shift;
Tim Northover3b0846e2014-05-24 12:50:23 +000060 int64_t Offset;
Juergen Ributzka052e6c22014-07-31 04:10:40 +000061 const GlobalValue *GV;
Tim Northover3b0846e2014-05-24 12:50:23 +000062
63 public:
Juergen Ributzkab46ea082014-08-19 19:44:17 +000064 Address() : Kind(RegBase), ExtType(AArch64_AM::InvalidShiftExtend),
65 OffsetReg(0), Shift(0), Offset(0), GV(nullptr) { Base.Reg = 0; }
Tim Northover3b0846e2014-05-24 12:50:23 +000066 void setKind(BaseKind K) { Kind = K; }
67 BaseKind getKind() const { return Kind; }
Juergen Ributzkab46ea082014-08-19 19:44:17 +000068 void setExtendType(AArch64_AM::ShiftExtendType E) { ExtType = E; }
69 AArch64_AM::ShiftExtendType getExtendType() const { return ExtType; }
Tim Northover3b0846e2014-05-24 12:50:23 +000070 bool isRegBase() const { return Kind == RegBase; }
71 bool isFIBase() const { return Kind == FrameIndexBase; }
72 void setReg(unsigned Reg) {
73 assert(isRegBase() && "Invalid base register access!");
74 Base.Reg = Reg;
75 }
76 unsigned getReg() const {
77 assert(isRegBase() && "Invalid base register access!");
78 return Base.Reg;
79 }
Juergen Ributzkab46ea082014-08-19 19:44:17 +000080 void setOffsetReg(unsigned Reg) {
81 assert(isRegBase() && "Invalid offset register access!");
82 OffsetReg = Reg;
83 }
84 unsigned getOffsetReg() const {
85 assert(isRegBase() && "Invalid offset register access!");
86 return OffsetReg;
87 }
Tim Northover3b0846e2014-05-24 12:50:23 +000088 void setFI(unsigned FI) {
89 assert(isFIBase() && "Invalid base frame index access!");
90 Base.FI = FI;
91 }
92 unsigned getFI() const {
93 assert(isFIBase() && "Invalid base frame index access!");
94 return Base.FI;
95 }
96 void setOffset(int64_t O) { Offset = O; }
97 int64_t getOffset() { return Offset; }
Juergen Ributzkab46ea082014-08-19 19:44:17 +000098 void setShift(unsigned S) { Shift = S; }
99 unsigned getShift() { return Shift; }
Tim Northover3b0846e2014-05-24 12:50:23 +0000100
Juergen Ributzka052e6c22014-07-31 04:10:40 +0000101 void setGlobalValue(const GlobalValue *G) { GV = G; }
102 const GlobalValue *getGlobalValue() { return GV; }
Tim Northover3b0846e2014-05-24 12:50:23 +0000103 };
104
105 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
106 /// make the right decision when generating code for different targets.
107 const AArch64Subtarget *Subtarget;
108 LLVMContext *Context;
109
Juergen Ributzkaa126d1e2014-08-05 05:43:48 +0000110 bool FastLowerArguments() override;
Juergen Ributzka2581fa52014-07-22 23:14:58 +0000111 bool FastLowerCall(CallLoweringInfo &CLI) override;
112 bool FastLowerIntrinsicCall(const IntrinsicInst *II) override;
113
Tim Northover3b0846e2014-05-24 12:50:23 +0000114private:
115 // Selection routines.
116 bool SelectLoad(const Instruction *I);
117 bool SelectStore(const Instruction *I);
118 bool SelectBranch(const Instruction *I);
119 bool SelectIndirectBr(const Instruction *I);
120 bool SelectCmp(const Instruction *I);
121 bool SelectSelect(const Instruction *I);
122 bool SelectFPExt(const Instruction *I);
123 bool SelectFPTrunc(const Instruction *I);
124 bool SelectFPToInt(const Instruction *I, bool Signed);
125 bool SelectIntToFP(const Instruction *I, bool Signed);
126 bool SelectRem(const Instruction *I, unsigned ISDOpcode);
Tim Northover3b0846e2014-05-24 12:50:23 +0000127 bool SelectRet(const Instruction *I);
128 bool SelectTrunc(const Instruction *I);
129 bool SelectIntExt(const Instruction *I);
130 bool SelectMul(const Instruction *I);
Juergen Ributzkaa75cb112014-07-30 22:04:22 +0000131 bool SelectShift(const Instruction *I, bool IsLeftShift, bool IsArithmetic);
Juergen Ributzkac537bd22014-07-31 06:25:37 +0000132 bool SelectBitCast(const Instruction *I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000133
134 // Utility helper routines.
135 bool isTypeLegal(Type *Ty, MVT &VT);
136 bool isLoadStoreTypeLegal(Type *Ty, MVT &VT);
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000137 bool ComputeAddress(const Value *Obj, Address &Addr, Type *Ty = nullptr);
Juergen Ributzka052e6c22014-07-31 04:10:40 +0000138 bool ComputeCallAddress(const Value *V, Address &Addr);
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000139 bool SimplifyAddress(Address &Addr, MVT VT);
Tim Northover3b0846e2014-05-24 12:50:23 +0000140 void AddLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000141 unsigned Flags, unsigned ScaleFactor,
142 MachineMemOperand *MMO);
Tim Northover3b0846e2014-05-24 12:50:23 +0000143 bool IsMemCpySmall(uint64_t Len, unsigned Alignment);
144 bool TryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
145 unsigned Alignment);
Juergen Ributzkaad2109a2014-07-30 22:04:34 +0000146 bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
147 const Value *Cond);
148
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000149 // Emit helper routines.
150 unsigned emitAddsSubs(bool UseAdds, MVT RetVT, const Value *LHS,
151 const Value *RHS, bool IsZExt = false,
152 bool WantResult = true);
153 unsigned emitAddsSubs_rr(bool UseAdds, MVT RetVT, unsigned LHSReg,
154 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
155 bool WantResult = true);
156 unsigned emitAddsSubs_ri(bool UseAdds, MVT RetVT, unsigned LHSReg,
157 bool LHSIsKill, uint64_t Imm,
158 bool WantResult = true);
159 unsigned emitAddsSubs_rs(bool UseAdds, MVT RetVT, unsigned LHSReg,
160 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
161 AArch64_AM::ShiftExtendType ShiftType,
162 uint64_t ShiftImm, bool WantResult = true);
163 unsigned emitAddsSubs_rx(bool UseAdds, MVT RetVT, unsigned LHSReg,
164 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
165 AArch64_AM::ShiftExtendType ExtType,
166 uint64_t ShiftImm, bool WantResult = true);
167
Tim Northover3b0846e2014-05-24 12:50:23 +0000168 // Emit functions.
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000169 bool emitCmp(const Value *LHS, const Value *RHS, bool IsZExt);
170 bool emitICmp(MVT RetVT, const Value *LHS, const Value *RHS, bool IsZExt);
171 bool emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
172 bool emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS);
Tim Northover3b0846e2014-05-24 12:50:23 +0000173 bool EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000174 MachineMemOperand *MMO = nullptr);
Tim Northover3b0846e2014-05-24 12:50:23 +0000175 bool EmitStore(MVT VT, unsigned SrcReg, Address Addr,
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000176 MachineMemOperand *MMO = nullptr);
Tim Northover3b0846e2014-05-24 12:50:23 +0000177 unsigned EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
178 unsigned Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000179 unsigned emitAdds(MVT RetVT, const Value *LHS, const Value *RHS,
180 bool IsZExt = false, bool WantResult = true);
181 unsigned emitSubs(MVT RetVT, const Value *LHS, const Value *RHS,
182 bool IsZExt = false, bool WantResult = true);
183 unsigned emitSubs_rr(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
184 unsigned RHSReg, bool RHSIsKill, bool WantResult = true);
185 unsigned emitSubs_rs(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
186 unsigned RHSReg, bool RHSIsKill,
187 AArch64_AM::ShiftExtendType ShiftType, uint64_t ShiftImm,
188 bool WantResult = true);
Juergen Ributzkaad3b0902014-07-30 22:04:25 +0000189 unsigned Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
190 unsigned Op1, bool Op1IsKill);
191 unsigned Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
192 unsigned Op1, bool Op1IsKill);
193 unsigned Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
194 unsigned Op1, bool Op1IsKill);
Juergen Ributzkaa75cb112014-07-30 22:04:22 +0000195 unsigned Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
196 unsigned Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
197 unsigned Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
Tim Northover3b0846e2014-05-24 12:50:23 +0000198
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000199 unsigned AArch64MaterializeInt(const ConstantInt *CI, MVT VT);
Tim Northover3b0846e2014-05-24 12:50:23 +0000200 unsigned AArch64MaterializeFP(const ConstantFP *CFP, MVT VT);
201 unsigned AArch64MaterializeGV(const GlobalValue *GV);
202
203 // Call handling routines.
204private:
205 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
Juergen Ributzka2581fa52014-07-22 23:14:58 +0000206 bool ProcessCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
Tim Northover3b0846e2014-05-24 12:50:23 +0000207 unsigned &NumBytes);
Juergen Ributzka1b014502014-07-23 20:03:13 +0000208 bool FinishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
Tim Northover3b0846e2014-05-24 12:50:23 +0000209
210public:
211 // Backend specific FastISel code.
212 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
213 unsigned TargetMaterializeConstant(const Constant *C) override;
214
215 explicit AArch64FastISel(FunctionLoweringInfo &funcInfo,
216 const TargetLibraryInfo *libInfo)
217 : FastISel(funcInfo, libInfo) {
218 Subtarget = &TM.getSubtarget<AArch64Subtarget>();
219 Context = &funcInfo.Fn->getContext();
220 }
221
222 bool TargetSelectInstruction(const Instruction *I) override;
223
224#include "AArch64GenFastISel.inc"
225};
226
227} // end anonymous namespace
228
229#include "AArch64GenCallingConv.inc"
230
231CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
232 if (CC == CallingConv::WebKit_JS)
233 return CC_AArch64_WebKit_JS;
234 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
235}
236
237unsigned AArch64FastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
238 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 &&
239 "Alloca should always return a pointer.");
240
241 // Don't handle dynamic allocas.
242 if (!FuncInfo.StaticAllocaMap.count(AI))
243 return 0;
244
245 DenseMap<const AllocaInst *, int>::iterator SI =
246 FuncInfo.StaticAllocaMap.find(AI);
247
248 if (SI != FuncInfo.StaticAllocaMap.end()) {
249 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
250 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
251 ResultReg)
252 .addFrameIndex(SI->second)
253 .addImm(0)
254 .addImm(0);
255 return ResultReg;
256 }
257
258 return 0;
259}
260
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000261unsigned AArch64FastISel::AArch64MaterializeInt(const ConstantInt *CI, MVT VT) {
262 if (VT > MVT::i64)
263 return 0;
Juergen Ributzka7e23f772014-08-19 19:44:02 +0000264
265 if (!CI->isZero())
266 return FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
267
268 // Create a copy from the zero register to materialize a "0" value.
269 const TargetRegisterClass *RC = (VT == MVT::i64) ? &AArch64::GPR64RegClass
270 : &AArch64::GPR32RegClass;
271 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
272 unsigned ResultReg = createResultReg(RC);
273 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
274 TII.get(TargetOpcode::COPY), ResultReg)
275 .addReg(ZeroReg, getKillRegState(true));
276 return ResultReg;
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000277}
278
Tim Northover3b0846e2014-05-24 12:50:23 +0000279unsigned AArch64FastISel::AArch64MaterializeFP(const ConstantFP *CFP, MVT VT) {
280 if (VT != MVT::f32 && VT != MVT::f64)
281 return 0;
282
283 const APFloat Val = CFP->getValueAPF();
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000284 bool Is64Bit = (VT == MVT::f64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000285
286 // This checks to see if we can use FMOV instructions to materialize
287 // a constant, otherwise we have to materialize via the constant pool.
288 if (TLI.isFPImmLegal(Val, VT)) {
Juergen Ributzka6597d312014-08-15 18:55:55 +0000289 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
290 // Positive zero (+0.0) has to be materialized with a fmov from the zero
291 // register, because the immediate version of fmov cannot encode zero.
292 if (Val.isPosZero()) {
293 unsigned ZReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
Juergen Ributzka0781b862014-08-20 01:10:36 +0000294 unsigned Opc = Is64Bit ? AArch64::FMOVXDr : AArch64::FMOVWSr;
Juergen Ributzka6597d312014-08-15 18:55:55 +0000295 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
296 .addReg(ZReg, getKillRegState(true));
297 return ResultReg;
298 }
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000299 int Imm = Is64Bit ? AArch64_AM::getFP64Imm(Val)
300 : AArch64_AM::getFP32Imm(Val);
301 unsigned Opc = Is64Bit ? AArch64::FMOVDi : AArch64::FMOVSi;
Tim Northover3b0846e2014-05-24 12:50:23 +0000302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000303 .addImm(Imm);
Tim Northover3b0846e2014-05-24 12:50:23 +0000304 return ResultReg;
305 }
306
307 // Materialize via constant pool. MachineConstantPool wants an explicit
308 // alignment.
309 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
310 if (Align == 0)
311 Align = DL.getTypeAllocSize(CFP->getType());
312
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000313 unsigned CPI = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
Tim Northover3b0846e2014-05-24 12:50:23 +0000314 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
315 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000316 ADRPReg)
317 .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000318
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000319 unsigned Opc = Is64Bit ? AArch64::LDRDui : AArch64::LDRSui;
Tim Northover3b0846e2014-05-24 12:50:23 +0000320 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
321 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000322 .addReg(ADRPReg)
323 .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
Tim Northover3b0846e2014-05-24 12:50:23 +0000324 return ResultReg;
325}
326
327unsigned AArch64FastISel::AArch64MaterializeGV(const GlobalValue *GV) {
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000328 // We can't handle thread-local variables quickly yet.
329 if (GV->isThreadLocal())
330 return 0;
Tim Northover3b0846e2014-05-24 12:50:23 +0000331
Tim Northover391f93a2014-05-24 19:45:41 +0000332 // MachO still uses GOT for large code-model accesses, but ELF requires
333 // movz/movk sequences, which FastISel doesn't handle yet.
334 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
335 return 0;
336
Tim Northover3b0846e2014-05-24 12:50:23 +0000337 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
338
339 EVT DestEVT = TLI.getValueType(GV->getType(), true);
340 if (!DestEVT.isSimple())
341 return 0;
342
343 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
344 unsigned ResultReg;
345
346 if (OpFlags & AArch64II::MO_GOT) {
347 // ADRP + LDRX
348 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
349 ADRPReg)
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000350 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000351
352 ResultReg = createResultReg(&AArch64::GPR64RegClass);
353 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
354 ResultReg)
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000355 .addReg(ADRPReg)
356 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
357 AArch64II::MO_NC);
Tim Northover3b0846e2014-05-24 12:50:23 +0000358 } else {
359 // ADRP + ADDX
360 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000361 ADRPReg)
362 .addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
Tim Northover3b0846e2014-05-24 12:50:23 +0000363
364 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
365 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
366 ResultReg)
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000367 .addReg(ADRPReg)
368 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
369 .addImm(0);
Tim Northover3b0846e2014-05-24 12:50:23 +0000370 }
371 return ResultReg;
372}
373
374unsigned AArch64FastISel::TargetMaterializeConstant(const Constant *C) {
375 EVT CEVT = TLI.getValueType(C->getType(), true);
376
377 // Only handle simple types.
378 if (!CEVT.isSimple())
379 return 0;
380 MVT VT = CEVT.getSimpleVT();
381
Juergen Ributzka6bca9862014-08-15 18:55:52 +0000382 if (const auto *CI = dyn_cast<ConstantInt>(C))
383 return AArch64MaterializeInt(CI, VT);
384 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Tim Northover3b0846e2014-05-24 12:50:23 +0000385 return AArch64MaterializeFP(CFP, VT);
386 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
387 return AArch64MaterializeGV(GV);
388
389 return 0;
390}
391
392// Computes the address to get to an object.
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000393bool AArch64FastISel::ComputeAddress(const Value *Obj, Address &Addr, Type *Ty)
394{
Tim Northover3b0846e2014-05-24 12:50:23 +0000395 const User *U = nullptr;
396 unsigned Opcode = Instruction::UserOp1;
397 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
398 // Don't walk into other basic blocks unless the object is an alloca from
399 // another block, otherwise it may not have a virtual register assigned.
400 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
401 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
402 Opcode = I->getOpcode();
403 U = I;
404 }
405 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
406 Opcode = C->getOpcode();
407 U = C;
408 }
409
410 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
411 if (Ty->getAddressSpace() > 255)
412 // Fast instruction selection doesn't support the special
413 // address spaces.
414 return false;
415
416 switch (Opcode) {
417 default:
418 break;
419 case Instruction::BitCast: {
420 // Look through bitcasts.
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000421 return ComputeAddress(U->getOperand(0), Addr, Ty);
Tim Northover3b0846e2014-05-24 12:50:23 +0000422 }
423 case Instruction::IntToPtr: {
424 // Look past no-op inttoptrs.
425 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000426 return ComputeAddress(U->getOperand(0), Addr, Ty);
Tim Northover3b0846e2014-05-24 12:50:23 +0000427 break;
428 }
429 case Instruction::PtrToInt: {
430 // Look past no-op ptrtoints.
431 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000432 return ComputeAddress(U->getOperand(0), Addr, Ty);
Tim Northover3b0846e2014-05-24 12:50:23 +0000433 break;
434 }
435 case Instruction::GetElementPtr: {
436 Address SavedAddr = Addr;
437 uint64_t TmpOffset = Addr.getOffset();
438
439 // Iterate through the GEP folding the constants into offsets where
440 // we can.
441 gep_type_iterator GTI = gep_type_begin(U);
442 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
443 ++i, ++GTI) {
444 const Value *Op = *i;
445 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
446 const StructLayout *SL = DL.getStructLayout(STy);
447 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
448 TmpOffset += SL->getElementOffset(Idx);
449 } else {
450 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
451 for (;;) {
452 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
453 // Constant-offset addressing.
454 TmpOffset += CI->getSExtValue() * S;
455 break;
456 }
457 if (canFoldAddIntoGEP(U, Op)) {
458 // A compatible add with a constant operand. Fold the constant.
459 ConstantInt *CI =
460 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
461 TmpOffset += CI->getSExtValue() * S;
462 // Iterate on the other operand.
463 Op = cast<AddOperator>(Op)->getOperand(0);
464 continue;
465 }
466 // Unsupported
467 goto unsupported_gep;
468 }
469 }
470 }
471
472 // Try to grab the base operand now.
473 Addr.setOffset(TmpOffset);
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000474 if (ComputeAddress(U->getOperand(0), Addr, Ty))
Tim Northover3b0846e2014-05-24 12:50:23 +0000475 return true;
476
477 // We failed, restore everything and try the other options.
478 Addr = SavedAddr;
479
480 unsupported_gep:
481 break;
482 }
483 case Instruction::Alloca: {
484 const AllocaInst *AI = cast<AllocaInst>(Obj);
485 DenseMap<const AllocaInst *, int>::iterator SI =
486 FuncInfo.StaticAllocaMap.find(AI);
487 if (SI != FuncInfo.StaticAllocaMap.end()) {
488 Addr.setKind(Address::FrameIndexBase);
489 Addr.setFI(SI->second);
490 return true;
491 }
492 break;
493 }
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000494 case Instruction::Add: {
Juergen Ributzka5dcb33b2014-08-01 19:40:16 +0000495 // Adds of constants are common and easy enough.
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000496 const Value *LHS = U->getOperand(0);
497 const Value *RHS = U->getOperand(1);
498
499 if (isa<ConstantInt>(LHS))
500 std::swap(LHS, RHS);
501
502 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
Juergen Ributzka5dcb33b2014-08-01 19:40:16 +0000503 Addr.setOffset(Addr.getOffset() + (uint64_t)CI->getSExtValue());
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000504 return ComputeAddress(LHS, Addr, Ty);
505 }
506
507 Address Backup = Addr;
508 if (ComputeAddress(LHS, Addr, Ty) && ComputeAddress(RHS, Addr, Ty))
509 return true;
510 Addr = Backup;
511
512 break;
513 }
514 case Instruction::Shl:
515 if (Addr.getOffsetReg())
516 break;
517
518 if (const auto *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
519 unsigned Val = CI->getZExtValue();
520 if (Val < 1 || Val > 3)
521 break;
522
523 uint64_t NumBytes = 0;
524 if (Ty && Ty->isSized()) {
525 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
526 NumBytes = NumBits / 8;
527 if (!isPowerOf2_64(NumBits))
528 NumBytes = 0;
529 }
530
Aaron Ballmanbf6ee222014-08-20 12:14:35 +0000531 if (NumBytes != (1ULL << Val))
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000532 break;
533
534 Addr.setShift(Val);
535 Addr.setExtendType(AArch64_AM::LSL);
536
537 if (const auto *I = dyn_cast<Instruction>(U->getOperand(0)))
538 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
539 U = I;
540
541 if (const auto *ZE = dyn_cast<ZExtInst>(U))
542 if (ZE->getOperand(0)->getType()->isIntegerTy(32))
543 Addr.setExtendType(AArch64_AM::UXTW);
544
545 if (const auto *SE = dyn_cast<SExtInst>(U))
546 if (SE->getOperand(0)->getType()->isIntegerTy(32))
547 Addr.setExtendType(AArch64_AM::SXTW);
548
549 unsigned Reg = getRegForValue(U->getOperand(0));
550 if (!Reg)
551 return false;
552 Addr.setOffsetReg(Reg);
553 return true;
Juergen Ributzka5dcb33b2014-08-01 19:40:16 +0000554 }
555 break;
Tim Northover3b0846e2014-05-24 12:50:23 +0000556 }
557
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000558 if (Addr.getReg()) {
559 if (!Addr.getOffsetReg()) {
560 unsigned Reg = getRegForValue(Obj);
561 if (!Reg)
562 return false;
563 Addr.setOffsetReg(Reg);
564 return true;
565 }
566 return false;
567 }
568
569 unsigned Reg = getRegForValue(Obj);
570 if (!Reg)
571 return false;
572 Addr.setReg(Reg);
573 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000574}
575
Juergen Ributzka052e6c22014-07-31 04:10:40 +0000576bool AArch64FastISel::ComputeCallAddress(const Value *V, Address &Addr) {
577 const User *U = nullptr;
578 unsigned Opcode = Instruction::UserOp1;
579 bool InMBB = true;
580
581 if (const auto *I = dyn_cast<Instruction>(V)) {
582 Opcode = I->getOpcode();
583 U = I;
584 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
585 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
586 Opcode = C->getOpcode();
587 U = C;
588 }
589
590 switch (Opcode) {
591 default: break;
592 case Instruction::BitCast:
593 // Look past bitcasts if its operand is in the same BB.
594 if (InMBB)
595 return ComputeCallAddress(U->getOperand(0), Addr);
596 break;
597 case Instruction::IntToPtr:
598 // Look past no-op inttoptrs if its operand is in the same BB.
599 if (InMBB &&
600 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
601 return ComputeCallAddress(U->getOperand(0), Addr);
602 break;
603 case Instruction::PtrToInt:
604 // Look past no-op ptrtoints if its operand is in the same BB.
605 if (InMBB &&
606 TLI.getValueType(U->getType()) == TLI.getPointerTy())
607 return ComputeCallAddress(U->getOperand(0), Addr);
608 break;
609 }
610
611 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
612 Addr.setGlobalValue(GV);
613 return true;
614 }
615
616 // If all else fails, try to materialize the value in a register.
617 if (!Addr.getGlobalValue()) {
618 Addr.setReg(getRegForValue(V));
619 return Addr.getReg() != 0;
620 }
621
622 return false;
623}
624
625
Tim Northover3b0846e2014-05-24 12:50:23 +0000626bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
627 EVT evt = TLI.getValueType(Ty, true);
628
629 // Only handle simple types.
630 if (evt == MVT::Other || !evt.isSimple())
631 return false;
632 VT = evt.getSimpleVT();
633
634 // This is a legal type, but it's not something we handle in fast-isel.
635 if (VT == MVT::f128)
636 return false;
637
638 // Handle all other legal types, i.e. a register that will directly hold this
639 // value.
640 return TLI.isTypeLegal(VT);
641}
642
643bool AArch64FastISel::isLoadStoreTypeLegal(Type *Ty, MVT &VT) {
644 if (isTypeLegal(Ty, VT))
645 return true;
646
647 // If this is a type than can be sign or zero-extended to a basic operation
648 // go ahead and accept it now. For stores, this reflects truncation.
649 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
650 return true;
651
652 return false;
653}
654
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000655bool AArch64FastISel::SimplifyAddress(Address &Addr, MVT VT) {
656 unsigned ScaleFactor;
Tim Northover3b0846e2014-05-24 12:50:23 +0000657 switch (VT.SimpleTy) {
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000658 default: return false;
659 case MVT::i1: // fall-through
660 case MVT::i8: ScaleFactor = 1; break;
661 case MVT::i16: ScaleFactor = 2; break;
662 case MVT::i32: // fall-through
663 case MVT::f32: ScaleFactor = 4; break;
664 case MVT::i64: // fall-through
665 case MVT::f64: ScaleFactor = 8; break;
Tim Northover3b0846e2014-05-24 12:50:23 +0000666 }
667
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000668 bool ImmediateOffsetNeedsLowering = false;
669 bool RegisterOffsetNeedsLowering = false;
670 int64_t Offset = Addr.getOffset();
671 if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
672 ImmediateOffsetNeedsLowering = true;
673 else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
674 !isUInt<12>(Offset / ScaleFactor))
675 ImmediateOffsetNeedsLowering = true;
676
677 // Cannot encode an offset register and an immediate offset in the same
678 // instruction. Fold the immediate offset into the load/store instruction and
679 // emit an additonal add to take care of the offset register.
680 if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.isRegBase() &&
681 Addr.getOffsetReg())
682 RegisterOffsetNeedsLowering = true;
683
684 // If this is a stack pointer and the offset needs to be simplified then put
Tim Northoverc141ad42014-06-10 09:52:44 +0000685 // the alloca address into a register, set the base type back to register and
686 // continue. This should almost never happen.
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000687 if (ImmediateOffsetNeedsLowering && Addr.isFIBase()) {
Tim Northoverc141ad42014-06-10 09:52:44 +0000688 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
689 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
690 ResultReg)
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000691 .addFrameIndex(Addr.getFI())
692 .addImm(0)
693 .addImm(0);
Tim Northoverc141ad42014-06-10 09:52:44 +0000694 Addr.setKind(Address::RegBase);
695 Addr.setReg(ResultReg);
Tim Northover3b0846e2014-05-24 12:50:23 +0000696 }
697
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000698 if (RegisterOffsetNeedsLowering) {
699 unsigned ResultReg = 0;
700 if (Addr.getReg()) {
701 ResultReg = createResultReg(&AArch64::GPR64RegClass);
702 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
703 TII.get(AArch64::ADDXrs), ResultReg)
704 .addReg(Addr.getReg())
705 .addReg(Addr.getOffsetReg())
706 .addImm(Addr.getShift());
707 } else
708 ResultReg = Emit_LSL_ri(MVT::i64, Addr.getOffsetReg(),
709 /*Op0IsKill=*/false, Addr.getShift());
710 if (!ResultReg)
711 return false;
712
713 Addr.setReg(ResultReg);
714 Addr.setOffsetReg(0);
715 Addr.setShift(0);
716 }
717
Tim Northover3b0846e2014-05-24 12:50:23 +0000718 // Since the offset is too large for the load/store instruction get the
719 // reg+offset into a register.
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000720 if (ImmediateOffsetNeedsLowering) {
721 unsigned ResultReg = 0;
722 if (Addr.getReg())
723 ResultReg = FastEmit_ri_(MVT::i64, ISD::ADD, Addr.getReg(),
724 /*IsKill=*/false, Offset, MVT::i64);
725 else
726 ResultReg = FastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
727
728 if (!ResultReg)
Tim Northover3b0846e2014-05-24 12:50:23 +0000729 return false;
730 Addr.setReg(ResultReg);
731 Addr.setOffset(0);
732 }
733 return true;
734}
735
736void AArch64FastISel::AddLoadStoreOperands(Address &Addr,
737 const MachineInstrBuilder &MIB,
Juergen Ributzka241fd482014-08-08 17:24:10 +0000738 unsigned Flags,
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000739 unsigned ScaleFactor,
740 MachineMemOperand *MMO) {
741 int64_t Offset = Addr.getOffset() / ScaleFactor;
Tim Northover3b0846e2014-05-24 12:50:23 +0000742 // Frame base works a bit differently. Handle it separately.
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000743 if (Addr.isFIBase()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000744 int FI = Addr.getFI();
745 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
746 // and alignment should be based on the VT.
Juergen Ributzka241fd482014-08-08 17:24:10 +0000747 MMO = FuncInfo.MF->getMachineMemOperand(
748 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
749 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
Tim Northover3b0846e2014-05-24 12:50:23 +0000750 // Now add the rest of the operands.
Juergen Ributzka241fd482014-08-08 17:24:10 +0000751 MIB.addFrameIndex(FI).addImm(Offset);
Tim Northover3b0846e2014-05-24 12:50:23 +0000752 } else {
Juergen Ributzkab46ea082014-08-19 19:44:17 +0000753 assert(Addr.isRegBase() && "Unexpected address kind.");
754 if (Addr.getOffsetReg()) {
755 assert(Addr.getOffset() == 0 && "Unexpected offset");
756 bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
757 Addr.getExtendType() == AArch64_AM::SXTX;
758 MIB.addReg(Addr.getReg());
759 MIB.addReg(Addr.getOffsetReg());
760 MIB.addImm(IsSigned);
761 MIB.addImm(Addr.getShift() != 0);
762 } else {
763 MIB.addReg(Addr.getReg());
764 MIB.addImm(Offset);
765 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000766 }
Juergen Ributzka241fd482014-08-08 17:24:10 +0000767
768 if (MMO)
769 MIB.addMemOperand(MMO);
Tim Northover3b0846e2014-05-24 12:50:23 +0000770}
771
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000772unsigned AArch64FastISel::emitAddsSubs(bool UseAdds, MVT RetVT,
773 const Value *LHS, const Value *RHS,
774 bool IsZExt, bool WantResult) {
775 AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000776 bool NeedExtend = false;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000777 switch (RetVT.SimpleTy) {
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000778 default:
779 return 0;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000780 case MVT::i1:
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000781 NeedExtend = true;
782 break;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000783 case MVT::i8:
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000784 NeedExtend = true;
785 ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000786 break;
787 case MVT::i16:
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000788 NeedExtend = true;
789 ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000790 break;
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000791 case MVT::i32: // fall-through
792 case MVT::i64:
793 break;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000794 }
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000795 MVT SrcVT = RetVT;
796 RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000797
798 // Canonicalize immediates to the RHS first.
799 if (UseAdds && isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
800 std::swap(LHS, RHS);
801
802 // Canonicalize shift immediate to the RHS.
803 if (UseAdds)
804 if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
805 if (isa<ConstantInt>(SI->getOperand(1)))
806 if (SI->getOpcode() == Instruction::Shl ||
807 SI->getOpcode() == Instruction::LShr ||
808 SI->getOpcode() == Instruction::AShr )
809 std::swap(LHS, RHS);
810
811 unsigned LHSReg = getRegForValue(LHS);
812 if (!LHSReg)
813 return 0;
814 bool LHSIsKill = hasTrivialKill(LHS);
815
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000816 if (NeedExtend)
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000817 LHSReg = EmitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
818
819 unsigned ResultReg = 0;
820 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
821 uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
822 if (C->isNegative())
823 ResultReg =
824 emitAddsSubs_ri(!UseAdds, RetVT, LHSReg, LHSIsKill, -Imm, WantResult);
825 else
826 ResultReg =
827 emitAddsSubs_ri(UseAdds, RetVT, LHSReg, LHSIsKill, Imm, WantResult);
828 }
829 if (ResultReg)
830 return ResultReg;
831
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000832 // Only extend the RHS within the instruction if there is a valid extend type.
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000833 if (ExtendType != AArch64_AM::InvalidShiftExtend) {
834 if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
835 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
836 if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
837 unsigned RHSReg = getRegForValue(SI->getOperand(0));
838 if (!RHSReg)
839 return 0;
840 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
841 return emitAddsSubs_rx(UseAdds, RetVT, LHSReg, LHSIsKill, RHSReg,
842 RHSIsKill, ExtendType, C->getZExtValue(),
843 WantResult);
844 }
845 unsigned RHSReg = getRegForValue(RHS);
846 if (!RHSReg)
847 return 0;
848 bool RHSIsKill = hasTrivialKill(RHS);
849 return emitAddsSubs_rx(UseAdds, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
850 ExtendType, 0, WantResult);
851 }
852
853 // Check if the shift can be folded into the instruction.
854 if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
855 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
856 AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
857 switch (SI->getOpcode()) {
858 default: break;
859 case Instruction::Shl: ShiftType = AArch64_AM::LSL; break;
860 case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
861 case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
862 }
863 uint64_t ShiftVal = C->getZExtValue();
864 if (ShiftType != AArch64_AM::InvalidShiftExtend) {
865 unsigned RHSReg = getRegForValue(SI->getOperand(0));
866 if (!RHSReg)
867 return 0;
868 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
869 return emitAddsSubs_rs(UseAdds, RetVT, LHSReg, LHSIsKill, RHSReg,
870 RHSIsKill, ShiftType, ShiftVal, WantResult);
871 }
872 }
873 }
874
875 unsigned RHSReg = getRegForValue(RHS);
876 if (!RHSReg)
877 return 0;
878 bool RHSIsKill = hasTrivialKill(RHS);
Juergen Ributzkae1bb0552014-08-20 16:34:15 +0000879
880 if (NeedExtend)
881 RHSReg = EmitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
882
Juergen Ributzkac0886dd2014-08-19 22:29:55 +0000883 return emitAddsSubs_rr(UseAdds, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
884 WantResult);
885}
886
887unsigned AArch64FastISel::emitAddsSubs_rr(bool UseAdds, MVT RetVT,
888 unsigned LHSReg, bool LHSIsKill,
889 unsigned RHSReg, bool RHSIsKill,
890 bool WantResult) {
891 assert(LHSReg && RHSReg && "Invalid register number.");
892
893 if (RetVT != MVT::i32 && RetVT != MVT::i64)
894 return 0;
895
896 static const unsigned OpcTable[2][2] = {
897 { AArch64::ADDSWrr, AArch64::ADDSXrr },
898 { AArch64::SUBSWrr, AArch64::SUBSXrr }
899 };
900 unsigned Opc = OpcTable[!UseAdds][(RetVT == MVT::i64)];
901 unsigned ResultReg;
902 if (WantResult)
903 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
904 else
905 ResultReg = (RetVT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
906
907 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
908 .addReg(LHSReg, getKillRegState(LHSIsKill))
909 .addReg(RHSReg, getKillRegState(RHSIsKill));
910
911 return ResultReg;
912}
913
914unsigned AArch64FastISel::emitAddsSubs_ri(bool UseAdds, MVT RetVT,
915 unsigned LHSReg, bool LHSIsKill,
916 uint64_t Imm, bool WantResult) {
917 assert(LHSReg && "Invalid register number.");
918
919 if (RetVT != MVT::i32 && RetVT != MVT::i64)
920 return 0;
921
922 unsigned ShiftImm;
923 if (isUInt<12>(Imm))
924 ShiftImm = 0;
925 else if ((Imm & 0xfff000) == Imm) {
926 ShiftImm = 12;
927 Imm >>= 12;
928 } else
929 return 0;
930
931 static const unsigned OpcTable[2][2] = {
932 { AArch64::ADDSWri, AArch64::ADDSXri },
933 { AArch64::SUBSWri, AArch64::SUBSXri }
934 };
935 unsigned Opc = OpcTable[!UseAdds][(RetVT == MVT::i64)];
936 unsigned ResultReg;
937 if (WantResult)
938 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
939 else
940 ResultReg = (RetVT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
941
942 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
943 .addReg(LHSReg, getKillRegState(LHSIsKill))
944 .addImm(Imm)
945 .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
946
947 return ResultReg;
948}
949
950unsigned AArch64FastISel::emitAddsSubs_rs(bool UseAdds, MVT RetVT,
951 unsigned LHSReg, bool LHSIsKill,
952 unsigned RHSReg, bool RHSIsKill,
953 AArch64_AM::ShiftExtendType ShiftType,
954 uint64_t ShiftImm, bool WantResult) {
955 assert(LHSReg && RHSReg && "Invalid register number.");
956
957 if (RetVT != MVT::i32 && RetVT != MVT::i64)
958 return 0;
959
960 static const unsigned OpcTable[2][2] = {
961 { AArch64::ADDSWrs, AArch64::ADDSXrs },
962 { AArch64::SUBSWrs, AArch64::SUBSXrs }
963 };
964 unsigned Opc = OpcTable[!UseAdds][(RetVT == MVT::i64)];
965 unsigned ResultReg;
966 if (WantResult)
967 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
968 else
969 ResultReg = (RetVT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
970
971 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
972 .addReg(LHSReg, getKillRegState(LHSIsKill))
973 .addReg(RHSReg, getKillRegState(RHSIsKill))
974 .addImm(getShifterImm(ShiftType, ShiftImm));
975
976 return ResultReg;
977}
978
979unsigned AArch64FastISel::emitAddsSubs_rx(bool UseAdds, MVT RetVT,
980 unsigned LHSReg, bool LHSIsKill,
981 unsigned RHSReg, bool RHSIsKill,
982 AArch64_AM::ShiftExtendType ExtType,
983 uint64_t ShiftImm, bool WantResult) {
984 assert(LHSReg && RHSReg && "Invalid register number.");
985
986 if (RetVT != MVT::i32 && RetVT != MVT::i64)
987 return 0;
988
989 static const unsigned OpcTable[2][2] = {
990 { AArch64::ADDSWrx, AArch64::ADDSXrx },
991 { AArch64::SUBSWrx, AArch64::SUBSXrx }
992 };
993 unsigned Opc = OpcTable[!UseAdds][(RetVT == MVT::i64)];
994 unsigned ResultReg;
995 if (WantResult)
996 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
997 else
998 ResultReg = (RetVT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
999
1000 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1001 .addReg(LHSReg, getKillRegState(LHSIsKill))
1002 .addReg(RHSReg, getKillRegState(RHSIsKill))
1003 .addImm(getArithExtendImm(ExtType, ShiftImm));
1004
1005 return ResultReg;
1006}
1007
1008bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
1009 Type *Ty = LHS->getType();
1010 EVT EVT = TLI.getValueType(Ty, true);
1011 if (!EVT.isSimple())
1012 return false;
1013 MVT VT = EVT.getSimpleVT();
1014
1015 switch (VT.SimpleTy) {
1016 default:
1017 return false;
1018 case MVT::i1:
1019 case MVT::i8:
1020 case MVT::i16:
1021 case MVT::i32:
1022 case MVT::i64:
1023 return emitICmp(VT, LHS, RHS, IsZExt);
1024 case MVT::f32:
1025 case MVT::f64:
1026 return emitFCmp(VT, LHS, RHS);
1027 }
1028}
1029
1030bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
1031 bool IsZExt) {
1032 return emitSubs(RetVT, LHS, RHS, IsZExt, /*WantResult=*/false) != 0;
1033}
1034
1035bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1036 uint64_t Imm) {
1037 return emitAddsSubs_ri(false, RetVT, LHSReg, LHSIsKill, Imm,
1038 /*WantResult=*/false) != 0;
1039}
1040
1041bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
1042 if (RetVT != MVT::f32 && RetVT != MVT::f64)
1043 return false;
1044
1045 // Check to see if the 2nd operand is a constant that we can encode directly
1046 // in the compare.
1047 bool UseImm = false;
1048 if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
1049 if (CFP->isZero() && !CFP->isNegative())
1050 UseImm = true;
1051
1052 unsigned LHSReg = getRegForValue(LHS);
1053 if (!LHSReg)
1054 return false;
1055 bool LHSIsKill = hasTrivialKill(LHS);
1056
1057 if (UseImm) {
1058 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
1059 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1060 .addReg(LHSReg, getKillRegState(LHSIsKill));
1061 return true;
1062 }
1063
1064 unsigned RHSReg = getRegForValue(RHS);
1065 if (!RHSReg)
1066 return false;
1067 bool RHSIsKill = hasTrivialKill(RHS);
1068
1069 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
1070 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1071 .addReg(LHSReg, getKillRegState(LHSIsKill))
1072 .addReg(RHSReg, getKillRegState(RHSIsKill));
1073 return true;
1074}
1075
1076unsigned AArch64FastISel::emitAdds(MVT RetVT, const Value *LHS,
1077 const Value *RHS, bool IsZExt,
1078 bool WantResult) {
1079 return emitAddsSubs(true, RetVT, LHS, RHS, IsZExt, WantResult);
1080}
1081
1082unsigned AArch64FastISel::emitSubs(MVT RetVT, const Value *LHS,
1083 const Value *RHS, bool IsZExt,
1084 bool WantResult) {
1085 return emitAddsSubs(false, RetVT, LHS, RHS, IsZExt, WantResult);
1086}
1087
1088unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
1089 bool LHSIsKill, unsigned RHSReg,
1090 bool RHSIsKill, bool WantResult) {
1091 return emitAddsSubs_rr(false, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1092 WantResult);
1093}
1094
1095unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
1096 bool LHSIsKill, unsigned RHSReg,
1097 bool RHSIsKill,
1098 AArch64_AM::ShiftExtendType ShiftType,
1099 uint64_t ShiftImm, bool WantResult) {
1100 return emitAddsSubs_rs(false, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1101 ShiftType, ShiftImm, WantResult);
1102}
1103
Tim Northover3b0846e2014-05-24 12:50:23 +00001104bool AArch64FastISel::EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001105 MachineMemOperand *MMO) {
1106 // Simplify this down to something we can handle.
1107 if (!SimplifyAddress(Addr, VT))
1108 return false;
1109
1110 unsigned ScaleFactor;
1111 switch (VT.SimpleTy) {
1112 default: llvm_unreachable("Unexpected value type.");
1113 case MVT::i1: // fall-through
1114 case MVT::i8: ScaleFactor = 1; break;
1115 case MVT::i16: ScaleFactor = 2; break;
1116 case MVT::i32: // fall-through
1117 case MVT::f32: ScaleFactor = 4; break;
1118 case MVT::i64: // fall-through
1119 case MVT::f64: ScaleFactor = 8; break;
1120 }
1121
Tim Northover3b0846e2014-05-24 12:50:23 +00001122 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1123 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001124 bool UseScaled = true;
1125 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1126 UseScaled = false;
1127 ScaleFactor = 1;
1128 }
1129
1130 static const unsigned OpcTable[4][6] = {
1131 { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi, AArch64::LDURXi,
1132 AArch64::LDURSi, AArch64::LDURDi },
1133 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui, AArch64::LDRXui,
1134 AArch64::LDRSui, AArch64::LDRDui },
1135 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX, AArch64::LDRXroX,
1136 AArch64::LDRSroX, AArch64::LDRDroX },
1137 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW, AArch64::LDRXroW,
1138 AArch64::LDRSroW, AArch64::LDRDroW }
1139 };
Tim Northover3b0846e2014-05-24 12:50:23 +00001140
1141 unsigned Opc;
1142 const TargetRegisterClass *RC;
1143 bool VTIsi1 = false;
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001144 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1145 Addr.getOffsetReg();
1146 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1147 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1148 Addr.getExtendType() == AArch64_AM::SXTW)
1149 Idx++;
1150
Tim Northover3b0846e2014-05-24 12:50:23 +00001151 switch (VT.SimpleTy) {
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001152 default: llvm_unreachable("Unexpected value type.");
1153 case MVT::i1: VTIsi1 = true; // Intentional fall-through.
1154 case MVT::i8: Opc = OpcTable[Idx][0]; RC = &AArch64::GPR32RegClass; break;
1155 case MVT::i16: Opc = OpcTable[Idx][1]; RC = &AArch64::GPR32RegClass; break;
1156 case MVT::i32: Opc = OpcTable[Idx][2]; RC = &AArch64::GPR32RegClass; break;
1157 case MVT::i64: Opc = OpcTable[Idx][3]; RC = &AArch64::GPR64RegClass; break;
1158 case MVT::f32: Opc = OpcTable[Idx][4]; RC = &AArch64::FPR32RegClass; break;
1159 case MVT::f64: Opc = OpcTable[Idx][5]; RC = &AArch64::FPR64RegClass; break;
Tim Northover3b0846e2014-05-24 12:50:23 +00001160 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001161
1162 // Create the base instruction, then add the operands.
1163 ResultReg = createResultReg(RC);
1164 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1165 TII.get(Opc), ResultReg);
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001166 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
Tim Northover3b0846e2014-05-24 12:50:23 +00001167
1168 // Loading an i1 requires special handling.
1169 if (VTIsi1) {
1170 MRI.constrainRegClass(ResultReg, &AArch64::GPR32RegClass);
1171 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1173 ANDReg)
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001174 .addReg(ResultReg)
1175 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
Tim Northover3b0846e2014-05-24 12:50:23 +00001176 ResultReg = ANDReg;
1177 }
1178 return true;
1179}
1180
1181bool AArch64FastISel::SelectLoad(const Instruction *I) {
1182 MVT VT;
1183 // Verify we have a legal type before going any further. Currently, we handle
1184 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1185 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1186 if (!isLoadStoreTypeLegal(I->getType(), VT) || cast<LoadInst>(I)->isAtomic())
1187 return false;
1188
1189 // See if we can handle this address.
1190 Address Addr;
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001191 if (!ComputeAddress(I->getOperand(0), Addr, I->getType()))
Tim Northover3b0846e2014-05-24 12:50:23 +00001192 return false;
1193
1194 unsigned ResultReg;
Juergen Ributzka241fd482014-08-08 17:24:10 +00001195 if (!EmitLoad(VT, ResultReg, Addr, createMachineMemOperandFor(I)))
Tim Northover3b0846e2014-05-24 12:50:23 +00001196 return false;
1197
1198 UpdateValueMap(I, ResultReg);
1199 return true;
1200}
1201
1202bool AArch64FastISel::EmitStore(MVT VT, unsigned SrcReg, Address Addr,
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001203 MachineMemOperand *MMO) {
1204 // Simplify this down to something we can handle.
1205 if (!SimplifyAddress(Addr, VT))
1206 return false;
1207
1208 unsigned ScaleFactor;
1209 switch (VT.SimpleTy) {
1210 default: llvm_unreachable("Unexpected value type.");
1211 case MVT::i1: // fall-through
1212 case MVT::i8: ScaleFactor = 1; break;
1213 case MVT::i16: ScaleFactor = 2; break;
1214 case MVT::i32: // fall-through
1215 case MVT::f32: ScaleFactor = 4; break;
1216 case MVT::i64: // fall-through
1217 case MVT::f64: ScaleFactor = 8; break;
1218 }
1219
Tim Northover3b0846e2014-05-24 12:50:23 +00001220 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1221 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001222 bool UseScaled = true;
1223 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1224 UseScaled = false;
Juergen Ributzka34ed4222014-08-14 17:10:54 +00001225 ScaleFactor = 1;
Juergen Ributzka34ed4222014-08-14 17:10:54 +00001226 }
1227
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001228
1229 static const unsigned OpcTable[4][6] = {
1230 { AArch64::STURBBi, AArch64::STURHHi, AArch64::STURWi, AArch64::STURXi,
1231 AArch64::STURSi, AArch64::STURDi },
1232 { AArch64::STRBBui, AArch64::STRHHui, AArch64::STRWui, AArch64::STRXui,
1233 AArch64::STRSui, AArch64::STRDui },
1234 { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
1235 AArch64::STRSroX, AArch64::STRDroX },
1236 { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
1237 AArch64::STRSroW, AArch64::STRDroW }
1238
1239 };
1240
1241 unsigned Opc;
1242 bool VTIsi1 = false;
1243 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1244 Addr.getOffsetReg();
1245 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1246 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1247 Addr.getExtendType() == AArch64_AM::SXTW)
1248 Idx++;
1249
1250 switch (VT.SimpleTy) {
1251 default: llvm_unreachable("Unexpected value type.");
1252 case MVT::i1: VTIsi1 = true;
1253 case MVT::i8: Opc = OpcTable[Idx][0]; break;
1254 case MVT::i16: Opc = OpcTable[Idx][1]; break;
1255 case MVT::i32: Opc = OpcTable[Idx][2]; break;
1256 case MVT::i64: Opc = OpcTable[Idx][3]; break;
1257 case MVT::f32: Opc = OpcTable[Idx][4]; break;
1258 case MVT::f64: Opc = OpcTable[Idx][5]; break;
1259 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001260
1261 // Storing an i1 requires special handling.
1262 if (VTIsi1) {
1263 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
1264 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1265 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1266 ANDReg)
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001267 .addReg(SrcReg)
1268 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
Tim Northover3b0846e2014-05-24 12:50:23 +00001269 SrcReg = ANDReg;
1270 }
1271 // Create the base instruction, then add the operands.
1272 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001273 TII.get(Opc))
1274 .addReg(SrcReg);
1275 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
Juergen Ributzka241fd482014-08-08 17:24:10 +00001276
Tim Northover3b0846e2014-05-24 12:50:23 +00001277 return true;
1278}
1279
1280bool AArch64FastISel::SelectStore(const Instruction *I) {
1281 MVT VT;
1282 Value *Op0 = I->getOperand(0);
1283 // Verify we have a legal type before going any further. Currently, we handle
1284 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1285 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1286 if (!isLoadStoreTypeLegal(Op0->getType(), VT) ||
1287 cast<StoreInst>(I)->isAtomic())
1288 return false;
1289
1290 // Get the value to be stored into a register.
1291 unsigned SrcReg = getRegForValue(Op0);
1292 if (SrcReg == 0)
1293 return false;
1294
1295 // See if we can handle this address.
1296 Address Addr;
Juergen Ributzkab46ea082014-08-19 19:44:17 +00001297 if (!ComputeAddress(I->getOperand(1), Addr, I->getOperand(0)->getType()))
Tim Northover3b0846e2014-05-24 12:50:23 +00001298 return false;
1299
Juergen Ributzka241fd482014-08-08 17:24:10 +00001300 if (!EmitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
Tim Northover3b0846e2014-05-24 12:50:23 +00001301 return false;
1302 return true;
1303}
1304
1305static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
1306 switch (Pred) {
1307 case CmpInst::FCMP_ONE:
1308 case CmpInst::FCMP_UEQ:
1309 default:
1310 // AL is our "false" for now. The other two need more compares.
1311 return AArch64CC::AL;
1312 case CmpInst::ICMP_EQ:
1313 case CmpInst::FCMP_OEQ:
1314 return AArch64CC::EQ;
1315 case CmpInst::ICMP_SGT:
1316 case CmpInst::FCMP_OGT:
1317 return AArch64CC::GT;
1318 case CmpInst::ICMP_SGE:
1319 case CmpInst::FCMP_OGE:
1320 return AArch64CC::GE;
1321 case CmpInst::ICMP_UGT:
1322 case CmpInst::FCMP_UGT:
1323 return AArch64CC::HI;
1324 case CmpInst::FCMP_OLT:
1325 return AArch64CC::MI;
1326 case CmpInst::ICMP_ULE:
1327 case CmpInst::FCMP_OLE:
1328 return AArch64CC::LS;
1329 case CmpInst::FCMP_ORD:
1330 return AArch64CC::VC;
1331 case CmpInst::FCMP_UNO:
1332 return AArch64CC::VS;
1333 case CmpInst::FCMP_UGE:
1334 return AArch64CC::PL;
1335 case CmpInst::ICMP_SLT:
1336 case CmpInst::FCMP_ULT:
1337 return AArch64CC::LT;
1338 case CmpInst::ICMP_SLE:
1339 case CmpInst::FCMP_ULE:
1340 return AArch64CC::LE;
1341 case CmpInst::FCMP_UNE:
1342 case CmpInst::ICMP_NE:
1343 return AArch64CC::NE;
1344 case CmpInst::ICMP_UGE:
1345 return AArch64CC::HS;
1346 case CmpInst::ICMP_ULT:
1347 return AArch64CC::LO;
1348 }
1349}
1350
1351bool AArch64FastISel::SelectBranch(const Instruction *I) {
1352 const BranchInst *BI = cast<BranchInst>(I);
1353 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1354 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1355
Juergen Ributzkaad2109a2014-07-30 22:04:34 +00001356 AArch64CC::CondCode CC = AArch64CC::NE;
Tim Northover3b0846e2014-05-24 12:50:23 +00001357 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1358 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1359 // We may not handle every CC for now.
Juergen Ributzkaad2109a2014-07-30 22:04:34 +00001360 CC = getCompareCC(CI->getPredicate());
Tim Northover3b0846e2014-05-24 12:50:23 +00001361 if (CC == AArch64CC::AL)
1362 return false;
1363
1364 // Emit the cmp.
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00001365 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Tim Northover3b0846e2014-05-24 12:50:23 +00001366 return false;
1367
1368 // Emit the branch.
1369 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1370 .addImm(CC)
1371 .addMBB(TBB);
Juergen Ributzka50a40052014-08-01 18:39:24 +00001372
1373 // Obtain the branch weight and add the TrueBB to the successor list.
1374 uint32_t BranchWeight = 0;
1375 if (FuncInfo.BPI)
1376 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1377 TBB->getBasicBlock());
1378 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
Tim Northover3b0846e2014-05-24 12:50:23 +00001379
1380 FastEmitBranch(FBB, DbgLoc);
1381 return true;
1382 }
1383 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1384 MVT SrcVT;
1385 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1386 (isLoadStoreTypeLegal(TI->getOperand(0)->getType(), SrcVT))) {
1387 unsigned CondReg = getRegForValue(TI->getOperand(0));
1388 if (CondReg == 0)
1389 return false;
1390
1391 // Issue an extract_subreg to get the lower 32-bits.
1392 if (SrcVT == MVT::i64)
1393 CondReg = FastEmitInst_extractsubreg(MVT::i32, CondReg, /*Kill=*/true,
1394 AArch64::sub_32);
1395
1396 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
1397 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1398 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1399 TII.get(AArch64::ANDWri), ANDReg)
1400 .addReg(CondReg)
1401 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00001402 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
Tim Northover3b0846e2014-05-24 12:50:23 +00001403
Tim Northover3b0846e2014-05-24 12:50:23 +00001404 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1405 std::swap(TBB, FBB);
1406 CC = AArch64CC::EQ;
1407 }
1408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1409 .addImm(CC)
1410 .addMBB(TBB);
Juergen Ributzka50a40052014-08-01 18:39:24 +00001411
1412 // Obtain the branch weight and add the TrueBB to the successor list.
1413 uint32_t BranchWeight = 0;
1414 if (FuncInfo.BPI)
1415 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1416 TBB->getBasicBlock());
1417 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1418
Tim Northover3b0846e2014-05-24 12:50:23 +00001419 FastEmitBranch(FBB, DbgLoc);
1420 return true;
1421 }
1422 } else if (const ConstantInt *CI =
1423 dyn_cast<ConstantInt>(BI->getCondition())) {
1424 uint64_t Imm = CI->getZExtValue();
1425 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1426 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
1427 .addMBB(Target);
Juergen Ributzka50a40052014-08-01 18:39:24 +00001428
1429 // Obtain the branch weight and add the target to the successor list.
1430 uint32_t BranchWeight = 0;
1431 if (FuncInfo.BPI)
1432 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1433 Target->getBasicBlock());
1434 FuncInfo.MBB->addSuccessor(Target, BranchWeight);
Tim Northover3b0846e2014-05-24 12:50:23 +00001435 return true;
Juergen Ributzkaad2109a2014-07-30 22:04:34 +00001436 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
1437 // Fake request the condition, otherwise the intrinsic might be completely
1438 // optimized away.
1439 unsigned CondReg = getRegForValue(BI->getCondition());
1440 if (!CondReg)
1441 return false;
1442
1443 // Emit the branch.
1444 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1445 .addImm(CC)
1446 .addMBB(TBB);
Juergen Ributzka50a40052014-08-01 18:39:24 +00001447
1448 // Obtain the branch weight and add the TrueBB to the successor list.
1449 uint32_t BranchWeight = 0;
1450 if (FuncInfo.BPI)
1451 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1452 TBB->getBasicBlock());
1453 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
Juergen Ributzkaad2109a2014-07-30 22:04:34 +00001454
1455 FastEmitBranch(FBB, DbgLoc);
1456 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001457 }
1458
1459 unsigned CondReg = getRegForValue(BI->getCondition());
1460 if (CondReg == 0)
1461 return false;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00001462 bool CondRegIsKill = hasTrivialKill(BI->getCondition());
Tim Northover3b0846e2014-05-24 12:50:23 +00001463
1464 // We've been divorced from our compare! Our block was split, and
1465 // now our compare lives in a predecessor block. We musn't
1466 // re-compare here, as the children of the compare aren't guaranteed
1467 // live across the block boundary (we *could* check for this).
1468 // Regardless, the compare has been done in the predecessor block,
1469 // and it left a value for us in a virtual register. Ergo, we test
1470 // the one-bit value left in the virtual register.
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00001471 emitICmp_ri(MVT::i32, CondReg, CondRegIsKill, 0);
Tim Northover3b0846e2014-05-24 12:50:23 +00001472
Tim Northover3b0846e2014-05-24 12:50:23 +00001473 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1474 std::swap(TBB, FBB);
1475 CC = AArch64CC::EQ;
1476 }
1477
1478 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1479 .addImm(CC)
1480 .addMBB(TBB);
Juergen Ributzka50a40052014-08-01 18:39:24 +00001481
1482 // Obtain the branch weight and add the TrueBB to the successor list.
1483 uint32_t BranchWeight = 0;
1484 if (FuncInfo.BPI)
1485 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1486 TBB->getBasicBlock());
1487 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1488
Tim Northover3b0846e2014-05-24 12:50:23 +00001489 FastEmitBranch(FBB, DbgLoc);
1490 return true;
1491}
1492
1493bool AArch64FastISel::SelectIndirectBr(const Instruction *I) {
1494 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
1495 unsigned AddrReg = getRegForValue(BI->getOperand(0));
1496 if (AddrReg == 0)
1497 return false;
1498
1499 // Emit the indirect branch.
1500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BR))
1501 .addReg(AddrReg);
1502
1503 // Make sure the CFG is up-to-date.
1504 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
1505 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
1506
1507 return true;
1508}
1509
Tim Northover3b0846e2014-05-24 12:50:23 +00001510bool AArch64FastISel::SelectCmp(const Instruction *I) {
1511 const CmpInst *CI = cast<CmpInst>(I);
1512
1513 // We may not handle every CC for now.
1514 AArch64CC::CondCode CC = getCompareCC(CI->getPredicate());
1515 if (CC == AArch64CC::AL)
1516 return false;
1517
1518 // Emit the cmp.
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00001519 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Tim Northover3b0846e2014-05-24 12:50:23 +00001520 return false;
1521
1522 // Now set a register based on the comparison.
1523 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
1524 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
1525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1526 ResultReg)
1527 .addReg(AArch64::WZR)
1528 .addReg(AArch64::WZR)
1529 .addImm(invertedCC);
1530
1531 UpdateValueMap(I, ResultReg);
1532 return true;
1533}
1534
1535bool AArch64FastISel::SelectSelect(const Instruction *I) {
1536 const SelectInst *SI = cast<SelectInst>(I);
1537
1538 EVT DestEVT = TLI.getValueType(SI->getType(), true);
1539 if (!DestEVT.isSimple())
1540 return false;
1541
1542 MVT DestVT = DestEVT.getSimpleVT();
1543 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
1544 DestVT != MVT::f64)
1545 return false;
1546
Juergen Ributzka3771fbb2014-07-30 22:04:37 +00001547 unsigned SelectOpc;
1548 switch (DestVT.SimpleTy) {
1549 default: return false;
1550 case MVT::i32: SelectOpc = AArch64::CSELWr; break;
1551 case MVT::i64: SelectOpc = AArch64::CSELXr; break;
1552 case MVT::f32: SelectOpc = AArch64::FCSELSrrr; break;
1553 case MVT::f64: SelectOpc = AArch64::FCSELDrrr; break;
1554 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001555
Juergen Ributzka3771fbb2014-07-30 22:04:37 +00001556 const Value *Cond = SI->getCondition();
1557 bool NeedTest = true;
1558 AArch64CC::CondCode CC = AArch64CC::NE;
1559 if (foldXALUIntrinsic(CC, I, Cond))
1560 NeedTest = false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001561
Juergen Ributzka3771fbb2014-07-30 22:04:37 +00001562 unsigned CondReg = getRegForValue(Cond);
1563 if (!CondReg)
1564 return false;
1565 bool CondIsKill = hasTrivialKill(Cond);
1566
1567 if (NeedTest) {
1568 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
1569 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1570 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1571 ANDReg)
1572 .addReg(CondReg, getKillRegState(CondIsKill))
Tim Northover3b0846e2014-05-24 12:50:23 +00001573 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00001574 emitICmp_ri(MVT::i32, ANDReg, true, 0);
Tim Northover3b0846e2014-05-24 12:50:23 +00001575 }
1576
Juergen Ributzka3771fbb2014-07-30 22:04:37 +00001577 unsigned TrueReg = getRegForValue(SI->getTrueValue());
1578 bool TrueIsKill = hasTrivialKill(SI->getTrueValue());
1579
1580 unsigned FalseReg = getRegForValue(SI->getFalseValue());
1581 bool FalseIsKill = hasTrivialKill(SI->getFalseValue());
1582
1583 if (!TrueReg || !FalseReg)
1584 return false;
1585
Tim Northover3b0846e2014-05-24 12:50:23 +00001586 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1587 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SelectOpc),
1588 ResultReg)
Juergen Ributzka3771fbb2014-07-30 22:04:37 +00001589 .addReg(TrueReg, getKillRegState(TrueIsKill))
1590 .addReg(FalseReg, getKillRegState(FalseIsKill))
1591 .addImm(CC);
Tim Northover3b0846e2014-05-24 12:50:23 +00001592
1593 UpdateValueMap(I, ResultReg);
1594 return true;
1595}
1596
1597bool AArch64FastISel::SelectFPExt(const Instruction *I) {
1598 Value *V = I->getOperand(0);
1599 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
1600 return false;
1601
1602 unsigned Op = getRegForValue(V);
1603 if (Op == 0)
1604 return false;
1605
1606 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
1607 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
1608 ResultReg).addReg(Op);
1609 UpdateValueMap(I, ResultReg);
1610 return true;
1611}
1612
1613bool AArch64FastISel::SelectFPTrunc(const Instruction *I) {
1614 Value *V = I->getOperand(0);
1615 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
1616 return false;
1617
1618 unsigned Op = getRegForValue(V);
1619 if (Op == 0)
1620 return false;
1621
1622 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
1623 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
1624 ResultReg).addReg(Op);
1625 UpdateValueMap(I, ResultReg);
1626 return true;
1627}
1628
1629// FPToUI and FPToSI
1630bool AArch64FastISel::SelectFPToInt(const Instruction *I, bool Signed) {
1631 MVT DestVT;
1632 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1633 return false;
1634
1635 unsigned SrcReg = getRegForValue(I->getOperand(0));
1636 if (SrcReg == 0)
1637 return false;
1638
1639 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1640 if (SrcVT == MVT::f128)
1641 return false;
1642
1643 unsigned Opc;
1644 if (SrcVT == MVT::f64) {
1645 if (Signed)
1646 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
1647 else
1648 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
1649 } else {
1650 if (Signed)
1651 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
1652 else
1653 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
1654 }
1655 unsigned ResultReg = createResultReg(
1656 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
1657 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1658 .addReg(SrcReg);
1659 UpdateValueMap(I, ResultReg);
1660 return true;
1661}
1662
1663bool AArch64FastISel::SelectIntToFP(const Instruction *I, bool Signed) {
1664 MVT DestVT;
1665 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1666 return false;
1667 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
1668 "Unexpected value type.");
1669
1670 unsigned SrcReg = getRegForValue(I->getOperand(0));
1671 if (SrcReg == 0)
1672 return false;
1673
1674 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1675
1676 // Handle sign-extension.
1677 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
1678 SrcReg =
1679 EmitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
1680 if (SrcReg == 0)
1681 return false;
1682 }
1683
1684 MRI.constrainRegClass(SrcReg, SrcVT == MVT::i64 ? &AArch64::GPR64RegClass
1685 : &AArch64::GPR32RegClass);
1686
1687 unsigned Opc;
1688 if (SrcVT == MVT::i64) {
1689 if (Signed)
1690 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
1691 else
1692 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
1693 } else {
1694 if (Signed)
1695 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
1696 else
1697 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
1698 }
1699
1700 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1701 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1702 .addReg(SrcReg);
1703 UpdateValueMap(I, ResultReg);
1704 return true;
1705}
1706
Juergen Ributzkaa126d1e2014-08-05 05:43:48 +00001707bool AArch64FastISel::FastLowerArguments() {
1708 if (!FuncInfo.CanLowerReturn)
1709 return false;
1710
1711 const Function *F = FuncInfo.Fn;
1712 if (F->isVarArg())
1713 return false;
1714
1715 CallingConv::ID CC = F->getCallingConv();
1716 if (CC != CallingConv::C)
1717 return false;
1718
1719 // Only handle simple cases like i1/i8/i16/i32/i64/f32/f64 of up to 8 GPR and
1720 // FPR each.
1721 unsigned GPRCnt = 0;
1722 unsigned FPRCnt = 0;
1723 unsigned Idx = 0;
1724 for (auto const &Arg : F->args()) {
1725 // The first argument is at index 1.
1726 ++Idx;
1727 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1728 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1729 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1730 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1731 return false;
1732
1733 Type *ArgTy = Arg.getType();
1734 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1735 return false;
1736
1737 EVT ArgVT = TLI.getValueType(ArgTy);
1738 if (!ArgVT.isSimple()) return false;
1739 switch (ArgVT.getSimpleVT().SimpleTy) {
1740 default: return false;
1741 case MVT::i1:
1742 case MVT::i8:
1743 case MVT::i16:
1744 case MVT::i32:
1745 case MVT::i64:
1746 ++GPRCnt;
1747 break;
1748 case MVT::f16:
1749 case MVT::f32:
1750 case MVT::f64:
1751 ++FPRCnt;
1752 break;
1753 }
1754
1755 if (GPRCnt > 8 || FPRCnt > 8)
1756 return false;
1757 }
1758
1759 static const MCPhysReg Registers[5][8] = {
1760 { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
1761 AArch64::W5, AArch64::W6, AArch64::W7 },
1762 { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
1763 AArch64::X5, AArch64::X6, AArch64::X7 },
1764 { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
1765 AArch64::H5, AArch64::H6, AArch64::H7 },
1766 { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
1767 AArch64::S5, AArch64::S6, AArch64::S7 },
1768 { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
1769 AArch64::D5, AArch64::D6, AArch64::D7 }
1770 };
1771
1772 unsigned GPRIdx = 0;
1773 unsigned FPRIdx = 0;
1774 for (auto const &Arg : F->args()) {
1775 MVT VT = TLI.getSimpleValueType(Arg.getType());
1776 unsigned SrcReg;
1777 switch (VT.SimpleTy) {
1778 default: llvm_unreachable("Unexpected value type.");
1779 case MVT::i1:
1780 case MVT::i8:
1781 case MVT::i16: VT = MVT::i32; // fall-through
1782 case MVT::i32: SrcReg = Registers[0][GPRIdx++]; break;
1783 case MVT::i64: SrcReg = Registers[1][GPRIdx++]; break;
1784 case MVT::f16: SrcReg = Registers[2][FPRIdx++]; break;
1785 case MVT::f32: SrcReg = Registers[3][FPRIdx++]; break;
1786 case MVT::f64: SrcReg = Registers[4][FPRIdx++]; break;
1787 }
1788
1789 // Skip unused arguments.
1790 if (Arg.use_empty()) {
1791 UpdateValueMap(&Arg, 0);
1792 continue;
1793 }
1794
1795 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1796 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1797 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1798 // Without this, EmitLiveInCopies may eliminate the livein if its only
1799 // use is a bitcast (which isn't turned into an instruction).
1800 unsigned ResultReg = createResultReg(RC);
1801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1802 TII.get(TargetOpcode::COPY), ResultReg)
1803 .addReg(DstReg, getKillRegState(true));
1804 UpdateValueMap(&Arg, ResultReg);
1805 }
1806 return true;
1807}
1808
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001809bool AArch64FastISel::ProcessCallArgs(CallLoweringInfo &CLI,
1810 SmallVectorImpl<MVT> &OutVTs,
1811 unsigned &NumBytes) {
1812 CallingConv::ID CC = CLI.CallConv;
Tim Northover3b0846e2014-05-24 12:50:23 +00001813 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001814 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001815 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
Tim Northover3b0846e2014-05-24 12:50:23 +00001816
1817 // Get a count of how many bytes are to be pushed on the stack.
1818 NumBytes = CCInfo.getNextStackOffset();
1819
1820 // Issue CALLSEQ_START
1821 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1822 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001823 .addImm(NumBytes);
Tim Northover3b0846e2014-05-24 12:50:23 +00001824
1825 // Process the args.
1826 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1827 CCValAssign &VA = ArgLocs[i];
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001828 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1829 MVT ArgVT = OutVTs[VA.getValNo()];
1830
1831 unsigned ArgReg = getRegForValue(ArgVal);
1832 if (!ArgReg)
1833 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001834
1835 // Handle arg promotion: SExt, ZExt, AExt.
1836 switch (VA.getLocInfo()) {
1837 case CCValAssign::Full:
1838 break;
1839 case CCValAssign::SExt: {
1840 MVT DestVT = VA.getLocVT();
1841 MVT SrcVT = ArgVT;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001842 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1843 if (!ArgReg)
Tim Northover3b0846e2014-05-24 12:50:23 +00001844 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001845 break;
1846 }
1847 case CCValAssign::AExt:
1848 // Intentional fall-through.
1849 case CCValAssign::ZExt: {
1850 MVT DestVT = VA.getLocVT();
1851 MVT SrcVT = ArgVT;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001852 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1853 if (!ArgReg)
Tim Northover3b0846e2014-05-24 12:50:23 +00001854 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001855 break;
1856 }
1857 default:
1858 llvm_unreachable("Unknown arg promotion!");
1859 }
1860
1861 // Now copy/store arg to correct locations.
1862 if (VA.isRegLoc() && !VA.needsCustom()) {
1863 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001864 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1865 CLI.OutRegs.push_back(VA.getLocReg());
Tim Northover3b0846e2014-05-24 12:50:23 +00001866 } else if (VA.needsCustom()) {
1867 // FIXME: Handle custom args.
1868 return false;
1869 } else {
1870 assert(VA.isMemLoc() && "Assuming store on stack.");
1871
Juergen Ributzka39032672014-07-31 00:11:11 +00001872 // Don't emit stores for undef values.
1873 if (isa<UndefValue>(ArgVal))
1874 continue;
1875
Tim Northover3b0846e2014-05-24 12:50:23 +00001876 // Need to store on the stack.
Tim Northover6890add2014-06-03 13:54:53 +00001877 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
Tim Northover3b0846e2014-05-24 12:50:23 +00001878
1879 unsigned BEAlign = 0;
1880 if (ArgSize < 8 && !Subtarget->isLittleEndian())
1881 BEAlign = 8 - ArgSize;
1882
1883 Address Addr;
1884 Addr.setKind(Address::RegBase);
1885 Addr.setReg(AArch64::SP);
1886 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1887
Juergen Ributzka241fd482014-08-08 17:24:10 +00001888 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1889 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1890 MachinePointerInfo::getStack(Addr.getOffset()),
1891 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1892
1893 if (!EmitStore(ArgVT, ArgReg, Addr, MMO))
Tim Northover3b0846e2014-05-24 12:50:23 +00001894 return false;
1895 }
1896 }
1897 return true;
1898}
1899
Juergen Ributzka1b014502014-07-23 20:03:13 +00001900bool AArch64FastISel::FinishCall(CallLoweringInfo &CLI, MVT RetVT,
1901 unsigned NumBytes) {
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001902 CallingConv::ID CC = CLI.CallConv;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001903
Tim Northover3b0846e2014-05-24 12:50:23 +00001904 // Issue CALLSEQ_END
1905 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1906 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001907 .addImm(NumBytes).addImm(0);
Tim Northover3b0846e2014-05-24 12:50:23 +00001908
1909 // Now the return value.
1910 if (RetVT != MVT::isVoid) {
1911 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001912 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Tim Northover3b0846e2014-05-24 12:50:23 +00001913 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
1914
1915 // Only handle a single return value.
1916 if (RVLocs.size() != 1)
1917 return false;
1918
1919 // Copy all of the result registers out of their specified physreg.
1920 MVT CopyVT = RVLocs[0].getValVT();
1921 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1922 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001923 TII.get(TargetOpcode::COPY), ResultReg)
1924 .addReg(RVLocs[0].getLocReg());
1925 CLI.InRegs.push_back(RVLocs[0].getLocReg());
Tim Northover3b0846e2014-05-24 12:50:23 +00001926
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001927 CLI.ResultReg = ResultReg;
1928 CLI.NumResultRegs = 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001929 }
1930
1931 return true;
1932}
1933
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001934bool AArch64FastISel::FastLowerCall(CallLoweringInfo &CLI) {
1935 CallingConv::ID CC = CLI.CallConv;
Akira Hatanakab74db092014-08-13 23:23:58 +00001936 bool IsTailCall = CLI.IsTailCall;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001937 bool IsVarArg = CLI.IsVarArg;
1938 const Value *Callee = CLI.Callee;
1939 const char *SymName = CLI.SymName;
Tim Northover3b0846e2014-05-24 12:50:23 +00001940
Akira Hatanakab74db092014-08-13 23:23:58 +00001941 // Allow SelectionDAG isel to handle tail calls.
1942 if (IsTailCall)
1943 return false;
1944
Juergen Ributzka052e6c22014-07-31 04:10:40 +00001945 CodeModel::Model CM = TM.getCodeModel();
1946 // Only support the small and large code model.
1947 if (CM != CodeModel::Small && CM != CodeModel::Large)
1948 return false;
1949
1950 // FIXME: Add large code model support for ELF.
1951 if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +00001952 return false;
1953
Tim Northover3b0846e2014-05-24 12:50:23 +00001954 // Let SDISel handle vararg functions.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001955 if (IsVarArg)
Tim Northover3b0846e2014-05-24 12:50:23 +00001956 return false;
1957
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001958 // FIXME: Only handle *simple* calls for now.
Tim Northover3b0846e2014-05-24 12:50:23 +00001959 MVT RetVT;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001960 if (CLI.RetTy->isVoidTy())
Tim Northover3b0846e2014-05-24 12:50:23 +00001961 RetVT = MVT::isVoid;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001962 else if (!isTypeLegal(CLI.RetTy, RetVT))
Tim Northover3b0846e2014-05-24 12:50:23 +00001963 return false;
1964
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001965 for (auto Flag : CLI.OutFlags)
1966 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1967 return false;
1968
Tim Northover3b0846e2014-05-24 12:50:23 +00001969 // Set up the argument vectors.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001970 SmallVector<MVT, 16> OutVTs;
1971 OutVTs.reserve(CLI.OutVals.size());
Tim Northover3b0846e2014-05-24 12:50:23 +00001972
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001973 for (auto *Val : CLI.OutVals) {
1974 MVT VT;
1975 if (!isTypeLegal(Val->getType(), VT) &&
1976 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
Tim Northover3b0846e2014-05-24 12:50:23 +00001977 return false;
1978
1979 // We don't handle vector parameters yet.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001980 if (VT.isVector() || VT.getSizeInBits() > 64)
Tim Northover3b0846e2014-05-24 12:50:23 +00001981 return false;
1982
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001983 OutVTs.push_back(VT);
Tim Northover3b0846e2014-05-24 12:50:23 +00001984 }
1985
Juergen Ributzka052e6c22014-07-31 04:10:40 +00001986 Address Addr;
1987 if (!ComputeCallAddress(Callee, Addr))
1988 return false;
1989
Tim Northover3b0846e2014-05-24 12:50:23 +00001990 // Handle the arguments now that we've gotten them.
Tim Northover3b0846e2014-05-24 12:50:23 +00001991 unsigned NumBytes;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001992 if (!ProcessCallArgs(CLI, OutVTs, NumBytes))
Tim Northover3b0846e2014-05-24 12:50:23 +00001993 return false;
1994
1995 // Issue the call.
1996 MachineInstrBuilder MIB;
Juergen Ributzka052e6c22014-07-31 04:10:40 +00001997 if (CM == CodeModel::Small) {
1998 unsigned CallOpc = Addr.getReg() ? AArch64::BLR : AArch64::BL;
1999 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
2000 if (SymName)
2001 MIB.addExternalSymbol(SymName, 0);
2002 else if (Addr.getGlobalValue())
2003 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
2004 else if (Addr.getReg())
2005 MIB.addReg(Addr.getReg());
2006 else
2007 return false;
2008 } else {
2009 unsigned CallReg = 0;
2010 if (SymName) {
2011 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
2012 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
2013 ADRPReg)
2014 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGE);
2015
2016 CallReg = createResultReg(&AArch64::GPR64RegClass);
2017 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
2018 CallReg)
2019 .addReg(ADRPReg)
2020 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
2021 AArch64II::MO_NC);
2022 } else if (Addr.getGlobalValue()) {
2023 CallReg = AArch64MaterializeGV(Addr.getGlobalValue());
2024 } else if (Addr.getReg())
2025 CallReg = Addr.getReg();
2026
2027 if (!CallReg)
2028 return false;
2029
2030 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2031 TII.get(AArch64::BLR)).addReg(CallReg);
2032 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002033
2034 // Add implicit physical register uses to the call.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002035 for (auto Reg : CLI.OutRegs)
2036 MIB.addReg(Reg, RegState::Implicit);
Tim Northover3b0846e2014-05-24 12:50:23 +00002037
2038 // Add a register mask with the call-preserved registers.
2039 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002040 MIB.addRegMask(TRI.getCallPreservedMask(CC));
Tim Northover3b0846e2014-05-24 12:50:23 +00002041
Juergen Ributzka052e6c22014-07-31 04:10:40 +00002042 CLI.Call = MIB;
2043
Tim Northover3b0846e2014-05-24 12:50:23 +00002044 // Finish off the call including any return values.
Juergen Ributzka1b014502014-07-23 20:03:13 +00002045 return FinishCall(CLI, RetVT, NumBytes);
Tim Northover3b0846e2014-05-24 12:50:23 +00002046}
2047
2048bool AArch64FastISel::IsMemCpySmall(uint64_t Len, unsigned Alignment) {
2049 if (Alignment)
2050 return Len / Alignment <= 4;
2051 else
2052 return Len < 32;
2053}
2054
2055bool AArch64FastISel::TryEmitSmallMemCpy(Address Dest, Address Src,
2056 uint64_t Len, unsigned Alignment) {
2057 // Make sure we don't bloat code by inlining very large memcpy's.
2058 if (!IsMemCpySmall(Len, Alignment))
2059 return false;
2060
2061 int64_t UnscaledOffset = 0;
2062 Address OrigDest = Dest;
2063 Address OrigSrc = Src;
2064
2065 while (Len) {
2066 MVT VT;
2067 if (!Alignment || Alignment >= 8) {
2068 if (Len >= 8)
2069 VT = MVT::i64;
2070 else if (Len >= 4)
2071 VT = MVT::i32;
2072 else if (Len >= 2)
2073 VT = MVT::i16;
2074 else {
2075 VT = MVT::i8;
2076 }
2077 } else {
2078 // Bound based on alignment.
2079 if (Len >= 4 && Alignment == 4)
2080 VT = MVT::i32;
2081 else if (Len >= 2 && Alignment == 2)
2082 VT = MVT::i16;
2083 else {
2084 VT = MVT::i8;
2085 }
2086 }
2087
2088 bool RV;
2089 unsigned ResultReg;
2090 RV = EmitLoad(VT, ResultReg, Src);
Tim Northoverc19445d2014-06-10 09:52:40 +00002091 if (!RV)
2092 return false;
2093
Tim Northover3b0846e2014-05-24 12:50:23 +00002094 RV = EmitStore(VT, ResultReg, Dest);
Tim Northoverc19445d2014-06-10 09:52:40 +00002095 if (!RV)
2096 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002097
2098 int64_t Size = VT.getSizeInBits() / 8;
2099 Len -= Size;
2100 UnscaledOffset += Size;
2101
2102 // We need to recompute the unscaled offset for each iteration.
2103 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
2104 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
2105 }
2106
2107 return true;
2108}
2109
Juergen Ributzkaad2109a2014-07-30 22:04:34 +00002110/// \brief Check if it is possible to fold the condition from the XALU intrinsic
2111/// into the user. The condition code will only be updated on success.
2112bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
2113 const Instruction *I,
2114 const Value *Cond) {
2115 if (!isa<ExtractValueInst>(Cond))
2116 return false;
2117
2118 const auto *EV = cast<ExtractValueInst>(Cond);
2119 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
2120 return false;
2121
2122 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
2123 MVT RetVT;
2124 const Function *Callee = II->getCalledFunction();
2125 Type *RetTy =
2126 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
2127 if (!isTypeLegal(RetTy, RetVT))
2128 return false;
2129
2130 if (RetVT != MVT::i32 && RetVT != MVT::i64)
2131 return false;
2132
2133 AArch64CC::CondCode TmpCC;
2134 switch (II->getIntrinsicID()) {
2135 default: return false;
2136 case Intrinsic::sadd_with_overflow:
2137 case Intrinsic::ssub_with_overflow: TmpCC = AArch64CC::VS; break;
2138 case Intrinsic::uadd_with_overflow: TmpCC = AArch64CC::HS; break;
2139 case Intrinsic::usub_with_overflow: TmpCC = AArch64CC::LO; break;
2140 case Intrinsic::smul_with_overflow:
2141 case Intrinsic::umul_with_overflow: TmpCC = AArch64CC::NE; break;
2142 }
2143
2144 // Check if both instructions are in the same basic block.
2145 if (II->getParent() != I->getParent())
2146 return false;
2147
2148 // Make sure nothing is in the way
2149 BasicBlock::const_iterator Start = I;
2150 BasicBlock::const_iterator End = II;
2151 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
2152 // We only expect extractvalue instructions between the intrinsic and the
2153 // instruction to be selected.
2154 if (!isa<ExtractValueInst>(Itr))
2155 return false;
2156
2157 // Check that the extractvalue operand comes from the intrinsic.
2158 const auto *EVI = cast<ExtractValueInst>(Itr);
2159 if (EVI->getAggregateOperand() != II)
2160 return false;
2161 }
2162
2163 CC = TmpCC;
2164 return true;
2165}
2166
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002167bool AArch64FastISel::FastLowerIntrinsicCall(const IntrinsicInst *II) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002168 // FIXME: Handle more intrinsics.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002169 switch (II->getIntrinsicID()) {
Juergen Ributzka5d6c43e2014-07-25 17:47:14 +00002170 default: return false;
2171 case Intrinsic::frameaddress: {
2172 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2173 MFI->setFrameAddressIsTaken(true);
2174
2175 const AArch64RegisterInfo *RegInfo =
Eric Christopherd9134482014-08-04 21:25:23 +00002176 static_cast<const AArch64RegisterInfo *>(
2177 TM.getSubtargetImpl()->getRegisterInfo());
Juergen Ributzka5d6c43e2014-07-25 17:47:14 +00002178 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2179 unsigned SrcReg = FramePtr;
2180
2181 // Recursively load frame address
2182 // ldr x0, [fp]
2183 // ldr x0, [x0]
2184 // ldr x0, [x0]
2185 // ...
2186 unsigned DestReg;
2187 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
2188 while (Depth--) {
2189 DestReg = createResultReg(&AArch64::GPR64RegClass);
2190 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2191 TII.get(AArch64::LDRXui), DestReg)
2192 .addReg(SrcReg).addImm(0);
2193 SrcReg = DestReg;
2194 }
2195
2196 UpdateValueMap(II, SrcReg);
2197 return true;
2198 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002199 case Intrinsic::memcpy:
2200 case Intrinsic::memmove: {
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002201 const auto *MTI = cast<MemTransferInst>(II);
Tim Northover3b0846e2014-05-24 12:50:23 +00002202 // Don't handle volatile.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002203 if (MTI->isVolatile())
Tim Northover3b0846e2014-05-24 12:50:23 +00002204 return false;
2205
2206 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2207 // we would emit dead code because we don't currently handle memmoves.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002208 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
2209 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002210 // Small memcpy's are common enough that we want to do them without a call
2211 // if possible.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002212 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
2213 unsigned Alignment = MTI->getAlignment();
Tim Northover3b0846e2014-05-24 12:50:23 +00002214 if (IsMemCpySmall(Len, Alignment)) {
2215 Address Dest, Src;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002216 if (!ComputeAddress(MTI->getRawDest(), Dest) ||
2217 !ComputeAddress(MTI->getRawSource(), Src))
Tim Northover3b0846e2014-05-24 12:50:23 +00002218 return false;
2219 if (TryEmitSmallMemCpy(Dest, Src, Len, Alignment))
2220 return true;
2221 }
2222 }
2223
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002224 if (!MTI->getLength()->getType()->isIntegerTy(64))
Tim Northover3b0846e2014-05-24 12:50:23 +00002225 return false;
2226
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002227 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
Tim Northover3b0846e2014-05-24 12:50:23 +00002228 // Fast instruction selection doesn't support the special
2229 // address spaces.
2230 return false;
2231
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002232 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
2233 return LowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
Tim Northover3b0846e2014-05-24 12:50:23 +00002234 }
2235 case Intrinsic::memset: {
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002236 const MemSetInst *MSI = cast<MemSetInst>(II);
Tim Northover3b0846e2014-05-24 12:50:23 +00002237 // Don't handle volatile.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002238 if (MSI->isVolatile())
Tim Northover3b0846e2014-05-24 12:50:23 +00002239 return false;
2240
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002241 if (!MSI->getLength()->getType()->isIntegerTy(64))
Tim Northover3b0846e2014-05-24 12:50:23 +00002242 return false;
2243
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002244 if (MSI->getDestAddressSpace() > 255)
Tim Northover3b0846e2014-05-24 12:50:23 +00002245 // Fast instruction selection doesn't support the special
2246 // address spaces.
2247 return false;
2248
Juergen Ributzka2581fa52014-07-22 23:14:58 +00002249 return LowerCallTo(II, "memset", II->getNumArgOperands() - 2);
Tim Northover3b0846e2014-05-24 12:50:23 +00002250 }
2251 case Intrinsic::trap: {
2252 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
2253 .addImm(1);
2254 return true;
2255 }
Juergen Ributzka130e77e2014-07-31 06:25:33 +00002256 case Intrinsic::sqrt: {
2257 Type *RetTy = II->getCalledFunction()->getReturnType();
2258
2259 MVT VT;
2260 if (!isTypeLegal(RetTy, VT))
2261 return false;
2262
2263 unsigned Op0Reg = getRegForValue(II->getOperand(0));
2264 if (!Op0Reg)
2265 return false;
2266 bool Op0IsKill = hasTrivialKill(II->getOperand(0));
2267
2268 unsigned ResultReg = FastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
2269 if (!ResultReg)
2270 return false;
2271
2272 UpdateValueMap(II, ResultReg);
2273 return true;
2274 }
Juergen Ributzkad43da752014-07-30 22:04:31 +00002275 case Intrinsic::sadd_with_overflow:
2276 case Intrinsic::uadd_with_overflow:
2277 case Intrinsic::ssub_with_overflow:
2278 case Intrinsic::usub_with_overflow:
2279 case Intrinsic::smul_with_overflow:
2280 case Intrinsic::umul_with_overflow: {
2281 // This implements the basic lowering of the xalu with overflow intrinsics.
2282 const Function *Callee = II->getCalledFunction();
2283 auto *Ty = cast<StructType>(Callee->getReturnType());
2284 Type *RetTy = Ty->getTypeAtIndex(0U);
2285 Type *CondTy = Ty->getTypeAtIndex(1);
2286
2287 MVT VT;
2288 if (!isTypeLegal(RetTy, VT))
2289 return false;
2290
2291 if (VT != MVT::i32 && VT != MVT::i64)
2292 return false;
2293
2294 const Value *LHS = II->getArgOperand(0);
2295 const Value *RHS = II->getArgOperand(1);
2296 // Canonicalize immediate to the RHS.
2297 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2298 isCommutativeIntrinsic(II))
2299 std::swap(LHS, RHS);
2300
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002301 unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
Juergen Ributzkad43da752014-07-30 22:04:31 +00002302 AArch64CC::CondCode CC = AArch64CC::Invalid;
Juergen Ributzkad43da752014-07-30 22:04:31 +00002303 switch (II->getIntrinsicID()) {
2304 default: llvm_unreachable("Unexpected intrinsic!");
2305 case Intrinsic::sadd_with_overflow:
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002306 ResultReg1 = emitAdds(VT, LHS, RHS); CC = AArch64CC::VS; break;
Juergen Ributzkad43da752014-07-30 22:04:31 +00002307 case Intrinsic::uadd_with_overflow:
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002308 ResultReg1 = emitAdds(VT, LHS, RHS); CC = AArch64CC::HS; break;
Juergen Ributzkad43da752014-07-30 22:04:31 +00002309 case Intrinsic::ssub_with_overflow:
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002310 ResultReg1 = emitSubs(VT, LHS, RHS); CC = AArch64CC::VS; break;
Juergen Ributzkad43da752014-07-30 22:04:31 +00002311 case Intrinsic::usub_with_overflow:
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002312 ResultReg1 = emitSubs(VT, LHS, RHS); CC = AArch64CC::LO; break;
Juergen Ributzkad43da752014-07-30 22:04:31 +00002313 case Intrinsic::smul_with_overflow: {
2314 CC = AArch64CC::NE;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002315 unsigned LHSReg = getRegForValue(LHS);
2316 if (!LHSReg)
2317 return false;
2318 bool LHSIsKill = hasTrivialKill(LHS);
2319
2320 unsigned RHSReg = getRegForValue(RHS);
Juergen Ributzka82ecc7f2014-08-01 01:25:55 +00002321 if (!RHSReg)
2322 return false;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002323 bool RHSIsKill = hasTrivialKill(RHS);
Juergen Ributzka82ecc7f2014-08-01 01:25:55 +00002324
Juergen Ributzkad43da752014-07-30 22:04:31 +00002325 if (VT == MVT::i32) {
2326 MulReg = Emit_SMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2327 unsigned ShiftReg = Emit_LSR_ri(MVT::i64, MulReg, false, 32);
2328 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
2329 AArch64::sub_32);
2330 ShiftReg = FastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
2331 AArch64::sub_32);
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002332 emitSubs_rs(VT, ShiftReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
2333 AArch64_AM::ASR, 31, /*WantResult=*/false);
Juergen Ributzkad43da752014-07-30 22:04:31 +00002334 } else {
2335 assert(VT == MVT::i64 && "Unexpected value type.");
2336 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2337 unsigned SMULHReg = FastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
2338 RHSReg, RHSIsKill);
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002339 emitSubs_rs(VT, SMULHReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
2340 AArch64_AM::ASR, 63, /*WantResult=*/false);
Juergen Ributzkad43da752014-07-30 22:04:31 +00002341 }
2342 break;
2343 }
2344 case Intrinsic::umul_with_overflow: {
2345 CC = AArch64CC::NE;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002346 unsigned LHSReg = getRegForValue(LHS);
2347 if (!LHSReg)
2348 return false;
2349 bool LHSIsKill = hasTrivialKill(LHS);
2350
2351 unsigned RHSReg = getRegForValue(RHS);
Juergen Ributzka82ecc7f2014-08-01 01:25:55 +00002352 if (!RHSReg)
2353 return false;
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002354 bool RHSIsKill = hasTrivialKill(RHS);
Juergen Ributzka82ecc7f2014-08-01 01:25:55 +00002355
Juergen Ributzkad43da752014-07-30 22:04:31 +00002356 if (VT == MVT::i32) {
2357 MulReg = Emit_UMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002358 emitSubs_rs(MVT::i64, AArch64::XZR, /*IsKill=*/true, MulReg,
2359 /*IsKill=*/false, AArch64_AM::LSR, 32,
2360 /*WantResult=*/false);
Juergen Ributzkad43da752014-07-30 22:04:31 +00002361 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
2362 AArch64::sub_32);
2363 } else {
2364 assert(VT == MVT::i64 && "Unexpected value type.");
2365 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2366 unsigned UMULHReg = FastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
2367 RHSReg, RHSIsKill);
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002368 emitSubs_rr(VT, AArch64::XZR, /*IsKill=*/true, UMULHReg,
2369 /*IsKill=*/false, /*WantResult=*/false);
Juergen Ributzkad43da752014-07-30 22:04:31 +00002370 }
2371 break;
2372 }
2373 }
2374
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002375 if (MulReg) {
2376 ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
Juergen Ributzkad43da752014-07-30 22:04:31 +00002377 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002378 TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
2379 }
Juergen Ributzkad43da752014-07-30 22:04:31 +00002380
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002381 ResultReg2 = FuncInfo.CreateRegs(CondTy);
2382 assert((ResultReg1 + 1) == ResultReg2 &&
2383 "Nonconsecutive result registers.");
Juergen Ributzkad43da752014-07-30 22:04:31 +00002384 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2385 ResultReg2)
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002386 .addReg(AArch64::WZR, getKillRegState(true))
2387 .addReg(AArch64::WZR, getKillRegState(true))
2388 .addImm(getInvertedCondCode(CC));
Juergen Ributzkad43da752014-07-30 22:04:31 +00002389
Juergen Ributzkac0886dd2014-08-19 22:29:55 +00002390 UpdateValueMap(II, ResultReg1, 2);
Juergen Ributzkad43da752014-07-30 22:04:31 +00002391 return true;
2392 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002393 }
2394 return false;
2395}
2396
2397bool AArch64FastISel::SelectRet(const Instruction *I) {
2398 const ReturnInst *Ret = cast<ReturnInst>(I);
2399 const Function &F = *I->getParent()->getParent();
2400
2401 if (!FuncInfo.CanLowerReturn)
2402 return false;
2403
2404 if (F.isVarArg())
2405 return false;
2406
2407 // Build a list of return value registers.
2408 SmallVector<unsigned, 4> RetRegs;
2409
2410 if (Ret->getNumOperands() > 0) {
2411 CallingConv::ID CC = F.getCallingConv();
2412 SmallVector<ISD::OutputArg, 4> Outs;
2413 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
2414
2415 // Analyze operands of the call, assigning locations to each operand.
2416 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002417 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
Tim Northover3b0846e2014-05-24 12:50:23 +00002418 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
2419 : RetCC_AArch64_AAPCS;
2420 CCInfo.AnalyzeReturn(Outs, RetCC);
2421
2422 // Only handle a single return value for now.
2423 if (ValLocs.size() != 1)
2424 return false;
2425
2426 CCValAssign &VA = ValLocs[0];
2427 const Value *RV = Ret->getOperand(0);
2428
2429 // Don't bother handling odd stuff for now.
2430 if (VA.getLocInfo() != CCValAssign::Full)
2431 return false;
2432 // Only handle register returns for now.
2433 if (!VA.isRegLoc())
2434 return false;
2435 unsigned Reg = getRegForValue(RV);
2436 if (Reg == 0)
2437 return false;
2438
2439 unsigned SrcReg = Reg + VA.getValNo();
2440 unsigned DestReg = VA.getLocReg();
2441 // Avoid a cross-class copy. This is very unlikely.
2442 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
2443 return false;
2444
2445 EVT RVEVT = TLI.getValueType(RV->getType());
2446 if (!RVEVT.isSimple())
2447 return false;
2448
2449 // Vectors (of > 1 lane) in big endian need tricky handling.
2450 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1)
2451 return false;
2452
2453 MVT RVVT = RVEVT.getSimpleVT();
2454 if (RVVT == MVT::f128)
2455 return false;
2456 MVT DestVT = VA.getValVT();
2457 // Special handling for extended integers.
2458 if (RVVT != DestVT) {
2459 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2460 return false;
2461
2462 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
2463 return false;
2464
2465 bool isZExt = Outs[0].Flags.isZExt();
2466 SrcReg = EmitIntExt(RVVT, SrcReg, DestVT, isZExt);
2467 if (SrcReg == 0)
2468 return false;
2469 }
2470
2471 // Make the copy.
2472 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2473 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
2474
2475 // Add register to return instruction.
2476 RetRegs.push_back(VA.getLocReg());
2477 }
2478
2479 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2480 TII.get(AArch64::RET_ReallyLR));
2481 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2482 MIB.addReg(RetRegs[i], RegState::Implicit);
2483 return true;
2484}
2485
2486bool AArch64FastISel::SelectTrunc(const Instruction *I) {
2487 Type *DestTy = I->getType();
2488 Value *Op = I->getOperand(0);
2489 Type *SrcTy = Op->getType();
2490
2491 EVT SrcEVT = TLI.getValueType(SrcTy, true);
2492 EVT DestEVT = TLI.getValueType(DestTy, true);
2493 if (!SrcEVT.isSimple())
2494 return false;
2495 if (!DestEVT.isSimple())
2496 return false;
2497
2498 MVT SrcVT = SrcEVT.getSimpleVT();
2499 MVT DestVT = DestEVT.getSimpleVT();
2500
2501 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
2502 SrcVT != MVT::i8)
2503 return false;
2504 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
2505 DestVT != MVT::i1)
2506 return false;
2507
2508 unsigned SrcReg = getRegForValue(Op);
2509 if (!SrcReg)
2510 return false;
2511
2512 // If we're truncating from i64 to a smaller non-legal type then generate an
2513 // AND. Otherwise, we know the high bits are undefined and a truncate doesn't
2514 // generate any code.
2515 if (SrcVT == MVT::i64) {
2516 uint64_t Mask = 0;
2517 switch (DestVT.SimpleTy) {
2518 default:
2519 // Trunc i64 to i32 is handled by the target-independent fast-isel.
2520 return false;
2521 case MVT::i1:
2522 Mask = 0x1;
2523 break;
2524 case MVT::i8:
2525 Mask = 0xff;
2526 break;
2527 case MVT::i16:
2528 Mask = 0xffff;
2529 break;
2530 }
2531 // Issue an extract_subreg to get the lower 32-bits.
2532 unsigned Reg32 = FastEmitInst_extractsubreg(MVT::i32, SrcReg, /*Kill=*/true,
2533 AArch64::sub_32);
2534 MRI.constrainRegClass(Reg32, &AArch64::GPR32RegClass);
2535 // Create the AND instruction which performs the actual truncation.
2536 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
2537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
2538 ANDReg)
2539 .addReg(Reg32)
2540 .addImm(AArch64_AM::encodeLogicalImmediate(Mask, 32));
2541 SrcReg = ANDReg;
2542 }
2543
2544 UpdateValueMap(I, SrcReg);
2545 return true;
2546}
2547
2548unsigned AArch64FastISel::Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt) {
2549 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
2550 DestVT == MVT::i64) &&
2551 "Unexpected value type.");
2552 // Handle i8 and i16 as i32.
2553 if (DestVT == MVT::i8 || DestVT == MVT::i16)
2554 DestVT = MVT::i32;
2555
2556 if (isZExt) {
2557 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
2558 unsigned ResultReg = createResultReg(&AArch64::GPR32spRegClass);
2559 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
2560 ResultReg)
2561 .addReg(SrcReg)
2562 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
2563
2564 if (DestVT == MVT::i64) {
2565 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
2566 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
2567 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2568 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2569 TII.get(AArch64::SUBREG_TO_REG), Reg64)
2570 .addImm(0)
2571 .addReg(ResultReg)
2572 .addImm(AArch64::sub_32);
2573 ResultReg = Reg64;
2574 }
2575 return ResultReg;
2576 } else {
2577 if (DestVT == MVT::i64) {
2578 // FIXME: We're SExt i1 to i64.
2579 return 0;
2580 }
2581 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
2582 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SBFMWri),
2583 ResultReg)
2584 .addReg(SrcReg)
2585 .addImm(0)
2586 .addImm(0);
2587 return ResultReg;
2588 }
2589}
2590
Juergen Ributzkaad3b0902014-07-30 22:04:25 +00002591unsigned AArch64FastISel::Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2592 unsigned Op1, bool Op1IsKill) {
2593 unsigned Opc, ZReg;
2594 switch (RetVT.SimpleTy) {
2595 default: return 0;
2596 case MVT::i8:
2597 case MVT::i16:
2598 case MVT::i32:
2599 RetVT = MVT::i32;
2600 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
2601 case MVT::i64:
2602 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
2603 }
2604
2605 // Create the base instruction, then add the operands.
2606 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
2607 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2608 .addReg(Op0, getKillRegState(Op0IsKill))
2609 .addReg(Op1, getKillRegState(Op1IsKill))
2610 .addReg(ZReg, getKillRegState(true));
2611
2612 return ResultReg;
2613}
2614
2615unsigned AArch64FastISel::Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2616 unsigned Op1, bool Op1IsKill) {
2617 if (RetVT != MVT::i64)
2618 return 0;
2619
2620 // Create the base instruction, then add the operands.
2621 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
2622 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SMADDLrrr),
2623 ResultReg)
2624 .addReg(Op0, getKillRegState(Op0IsKill))
2625 .addReg(Op1, getKillRegState(Op1IsKill))
2626 .addReg(AArch64::XZR, getKillRegState(true));
2627
2628 return ResultReg;
2629}
2630
2631unsigned AArch64FastISel::Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2632 unsigned Op1, bool Op1IsKill) {
2633 if (RetVT != MVT::i64)
2634 return 0;
2635
2636 // Create the base instruction, then add the operands.
2637 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
2638 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::UMADDLrrr),
2639 ResultReg)
2640 .addReg(Op0, getKillRegState(Op0IsKill))
2641 .addReg(Op1, getKillRegState(Op1IsKill))
2642 .addReg(AArch64::XZR, getKillRegState(true));
2643
2644 return ResultReg;
2645}
2646
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002647unsigned AArch64FastISel::Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2648 uint64_t Shift) {
2649 unsigned Opc, ImmR, ImmS;
2650 switch (RetVT.SimpleTy) {
2651 default: return 0;
2652 case MVT::i8:
Juergen Ributzka53533e82014-08-04 21:49:51 +00002653 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 7 - Shift; break;
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002654 case MVT::i16:
Juergen Ributzka53533e82014-08-04 21:49:51 +00002655 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 15 - Shift; break;
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002656 case MVT::i32:
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002657 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 31 - Shift; break;
2658 case MVT::i64:
2659 Opc = AArch64::UBFMXri; ImmR = -Shift % 64; ImmS = 63 - Shift; break;
2660 }
2661
Juergen Ributzka53533e82014-08-04 21:49:51 +00002662 RetVT.SimpleTy = std::max(MVT::i32, RetVT.SimpleTy);
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002663 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, ImmR,
2664 ImmS);
2665}
2666
2667unsigned AArch64FastISel::Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2668 uint64_t Shift) {
2669 unsigned Opc, ImmS;
2670 switch (RetVT.SimpleTy) {
2671 default: return 0;
Juergen Ributzka53533e82014-08-04 21:49:51 +00002672 case MVT::i8: Opc = AArch64::UBFMWri; ImmS = 7; break;
2673 case MVT::i16: Opc = AArch64::UBFMWri; ImmS = 15; break;
2674 case MVT::i32: Opc = AArch64::UBFMWri; ImmS = 31; break;
2675 case MVT::i64: Opc = AArch64::UBFMXri; ImmS = 63; break;
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002676 }
2677
Juergen Ributzka53533e82014-08-04 21:49:51 +00002678 RetVT.SimpleTy = std::max(MVT::i32, RetVT.SimpleTy);
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002679 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift,
2680 ImmS);
2681}
2682
2683unsigned AArch64FastISel::Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2684 uint64_t Shift) {
2685 unsigned Opc, ImmS;
2686 switch (RetVT.SimpleTy) {
2687 default: return 0;
Juergen Ributzka53533e82014-08-04 21:49:51 +00002688 case MVT::i8: Opc = AArch64::SBFMWri; ImmS = 7; break;
2689 case MVT::i16: Opc = AArch64::SBFMWri; ImmS = 15; break;
2690 case MVT::i32: Opc = AArch64::SBFMWri; ImmS = 31; break;
2691 case MVT::i64: Opc = AArch64::SBFMXri; ImmS = 63; break;
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002692 }
2693
Juergen Ributzka53533e82014-08-04 21:49:51 +00002694 RetVT.SimpleTy = std::max(MVT::i32, RetVT.SimpleTy);
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002695 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift,
2696 ImmS);
2697}
2698
Tim Northover3b0846e2014-05-24 12:50:23 +00002699unsigned AArch64FastISel::EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
2700 bool isZExt) {
2701 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
Louis Gerbarg4c5b4052014-07-07 21:37:51 +00002702
Louis Gerbarg1ce0c37bf2014-07-09 17:54:32 +00002703 // FastISel does not have plumbing to deal with extensions where the SrcVT or
2704 // DestVT are odd things, so test to make sure that they are both types we can
2705 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
2706 // bail out to SelectionDAG.
2707 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
2708 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
2709 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
2710 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
Louis Gerbarg4c5b4052014-07-07 21:37:51 +00002711 return 0;
2712
Tim Northover3b0846e2014-05-24 12:50:23 +00002713 unsigned Opc;
2714 unsigned Imm = 0;
2715
2716 switch (SrcVT.SimpleTy) {
2717 default:
2718 return 0;
2719 case MVT::i1:
2720 return Emiti1Ext(SrcReg, DestVT, isZExt);
2721 case MVT::i8:
2722 if (DestVT == MVT::i64)
2723 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2724 else
2725 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
2726 Imm = 7;
2727 break;
2728 case MVT::i16:
2729 if (DestVT == MVT::i64)
2730 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2731 else
2732 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
2733 Imm = 15;
2734 break;
2735 case MVT::i32:
2736 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
2737 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2738 Imm = 31;
2739 break;
2740 }
2741
2742 // Handle i8 and i16 as i32.
2743 if (DestVT == MVT::i8 || DestVT == MVT::i16)
2744 DestVT = MVT::i32;
2745 else if (DestVT == MVT::i64) {
2746 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2747 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2748 TII.get(AArch64::SUBREG_TO_REG), Src64)
2749 .addImm(0)
2750 .addReg(SrcReg)
2751 .addImm(AArch64::sub_32);
2752 SrcReg = Src64;
2753 }
2754
2755 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
2756 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2757 .addReg(SrcReg)
2758 .addImm(0)
2759 .addImm(Imm);
2760
2761 return ResultReg;
2762}
2763
2764bool AArch64FastISel::SelectIntExt(const Instruction *I) {
2765 // On ARM, in general, integer casts don't involve legal types; this code
2766 // handles promotable integers. The high bits for a type smaller than
2767 // the register size are assumed to be undefined.
2768 Type *DestTy = I->getType();
2769 Value *Src = I->getOperand(0);
2770 Type *SrcTy = Src->getType();
2771
2772 bool isZExt = isa<ZExtInst>(I);
2773 unsigned SrcReg = getRegForValue(Src);
2774 if (!SrcReg)
2775 return false;
2776
2777 EVT SrcEVT = TLI.getValueType(SrcTy, true);
2778 EVT DestEVT = TLI.getValueType(DestTy, true);
2779 if (!SrcEVT.isSimple())
2780 return false;
2781 if (!DestEVT.isSimple())
2782 return false;
2783
2784 MVT SrcVT = SrcEVT.getSimpleVT();
2785 MVT DestVT = DestEVT.getSimpleVT();
Juergen Ributzka51f53262014-08-05 05:43:44 +00002786 unsigned ResultReg = 0;
2787
2788 // Check if it is an argument and if it is already zero/sign-extended.
2789 if (const auto *Arg = dyn_cast<Argument>(Src)) {
2790 if ((isZExt && Arg->hasZExtAttr()) || (!isZExt && Arg->hasSExtAttr())) {
Juergen Ributzka95033272014-08-05 07:31:30 +00002791 if (DestVT == MVT::i64) {
2792 ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
Juergen Ributzka51f53262014-08-05 05:43:44 +00002793 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2794 TII.get(AArch64::SUBREG_TO_REG), ResultReg)
2795 .addImm(0)
2796 .addReg(SrcReg)
2797 .addImm(AArch64::sub_32);
Juergen Ributzka95033272014-08-05 07:31:30 +00002798 } else
2799 ResultReg = SrcReg;
Juergen Ributzka51f53262014-08-05 05:43:44 +00002800 }
2801 }
2802
2803 if (!ResultReg)
2804 ResultReg = EmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2805
2806 if (!ResultReg)
Tim Northover3b0846e2014-05-24 12:50:23 +00002807 return false;
Juergen Ributzka51f53262014-08-05 05:43:44 +00002808
Tim Northover3b0846e2014-05-24 12:50:23 +00002809 UpdateValueMap(I, ResultReg);
2810 return true;
2811}
2812
2813bool AArch64FastISel::SelectRem(const Instruction *I, unsigned ISDOpcode) {
2814 EVT DestEVT = TLI.getValueType(I->getType(), true);
2815 if (!DestEVT.isSimple())
2816 return false;
2817
2818 MVT DestVT = DestEVT.getSimpleVT();
2819 if (DestVT != MVT::i64 && DestVT != MVT::i32)
2820 return false;
2821
2822 unsigned DivOpc;
2823 bool is64bit = (DestVT == MVT::i64);
2824 switch (ISDOpcode) {
2825 default:
2826 return false;
2827 case ISD::SREM:
2828 DivOpc = is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
2829 break;
2830 case ISD::UREM:
2831 DivOpc = is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
2832 break;
2833 }
2834 unsigned MSubOpc = is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
2835 unsigned Src0Reg = getRegForValue(I->getOperand(0));
2836 if (!Src0Reg)
2837 return false;
2838
2839 unsigned Src1Reg = getRegForValue(I->getOperand(1));
2840 if (!Src1Reg)
2841 return false;
2842
2843 unsigned QuotReg = createResultReg(TLI.getRegClassFor(DestVT));
2844 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(DivOpc), QuotReg)
2845 .addReg(Src0Reg)
2846 .addReg(Src1Reg);
2847 // The remainder is computed as numerator - (quotient * denominator) using the
2848 // MSUB instruction.
2849 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
2850 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MSubOpc), ResultReg)
2851 .addReg(QuotReg)
2852 .addReg(Src1Reg)
2853 .addReg(Src0Reg);
2854 UpdateValueMap(I, ResultReg);
2855 return true;
2856}
2857
2858bool AArch64FastISel::SelectMul(const Instruction *I) {
2859 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2860 if (!SrcEVT.isSimple())
2861 return false;
2862 MVT SrcVT = SrcEVT.getSimpleVT();
2863
2864 // Must be simple value type. Don't handle vectors.
2865 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
2866 SrcVT != MVT::i8)
2867 return false;
2868
Tim Northover3b0846e2014-05-24 12:50:23 +00002869 unsigned Src0Reg = getRegForValue(I->getOperand(0));
2870 if (!Src0Reg)
2871 return false;
Juergen Ributzkaad3b0902014-07-30 22:04:25 +00002872 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
Tim Northover3b0846e2014-05-24 12:50:23 +00002873
2874 unsigned Src1Reg = getRegForValue(I->getOperand(1));
2875 if (!Src1Reg)
2876 return false;
Juergen Ributzkaad3b0902014-07-30 22:04:25 +00002877 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
Tim Northover3b0846e2014-05-24 12:50:23 +00002878
Juergen Ributzkaad3b0902014-07-30 22:04:25 +00002879 unsigned ResultReg =
2880 Emit_MUL_rr(SrcVT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
2881
2882 if (!ResultReg)
2883 return false;
2884
Tim Northover3b0846e2014-05-24 12:50:23 +00002885 UpdateValueMap(I, ResultReg);
2886 return true;
2887}
2888
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002889bool AArch64FastISel::SelectShift(const Instruction *I, bool IsLeftShift,
2890 bool IsArithmetic) {
2891 EVT RetEVT = TLI.getValueType(I->getType(), true);
2892 if (!RetEVT.isSimple())
2893 return false;
2894 MVT RetVT = RetEVT.getSimpleVT();
2895
2896 if (!isa<ConstantInt>(I->getOperand(1)))
2897 return false;
2898
2899 unsigned Op0Reg = getRegForValue(I->getOperand(0));
2900 if (!Op0Reg)
2901 return false;
2902 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
2903
2904 uint64_t ShiftVal = cast<ConstantInt>(I->getOperand(1))->getZExtValue();
2905
2906 unsigned ResultReg;
2907 if (IsLeftShift)
2908 ResultReg = Emit_LSL_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2909 else {
2910 if (IsArithmetic)
2911 ResultReg = Emit_ASR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2912 else
2913 ResultReg = Emit_LSR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2914 }
2915
2916 if (!ResultReg)
2917 return false;
2918
2919 UpdateValueMap(I, ResultReg);
2920 return true;
2921}
2922
Juergen Ributzkac537bd22014-07-31 06:25:37 +00002923bool AArch64FastISel::SelectBitCast(const Instruction *I) {
2924 MVT RetVT, SrcVT;
2925
2926 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
2927 return false;
2928 if (!isTypeLegal(I->getType(), RetVT))
2929 return false;
2930
2931 unsigned Opc;
2932 if (RetVT == MVT::f32 && SrcVT == MVT::i32)
2933 Opc = AArch64::FMOVWSr;
2934 else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
2935 Opc = AArch64::FMOVXDr;
2936 else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
2937 Opc = AArch64::FMOVSWr;
2938 else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
2939 Opc = AArch64::FMOVDXr;
2940 else
2941 return false;
2942
2943 unsigned Op0Reg = getRegForValue(I->getOperand(0));
2944 if (!Op0Reg)
2945 return false;
2946 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
2947 unsigned ResultReg = FastEmitInst_r(Opc, TLI.getRegClassFor(RetVT),
2948 Op0Reg, Op0IsKill);
2949
2950 if (!ResultReg)
2951 return false;
2952
2953 UpdateValueMap(I, ResultReg);
2954 return true;
2955}
2956
Tim Northover3b0846e2014-05-24 12:50:23 +00002957bool AArch64FastISel::TargetSelectInstruction(const Instruction *I) {
2958 switch (I->getOpcode()) {
2959 default:
2960 break;
2961 case Instruction::Load:
2962 return SelectLoad(I);
2963 case Instruction::Store:
2964 return SelectStore(I);
2965 case Instruction::Br:
2966 return SelectBranch(I);
2967 case Instruction::IndirectBr:
2968 return SelectIndirectBr(I);
2969 case Instruction::FCmp:
2970 case Instruction::ICmp:
2971 return SelectCmp(I);
2972 case Instruction::Select:
2973 return SelectSelect(I);
2974 case Instruction::FPExt:
2975 return SelectFPExt(I);
2976 case Instruction::FPTrunc:
2977 return SelectFPTrunc(I);
2978 case Instruction::FPToSI:
2979 return SelectFPToInt(I, /*Signed=*/true);
2980 case Instruction::FPToUI:
2981 return SelectFPToInt(I, /*Signed=*/false);
2982 case Instruction::SIToFP:
2983 return SelectIntToFP(I, /*Signed=*/true);
2984 case Instruction::UIToFP:
2985 return SelectIntToFP(I, /*Signed=*/false);
2986 case Instruction::SRem:
2987 return SelectRem(I, ISD::SREM);
2988 case Instruction::URem:
2989 return SelectRem(I, ISD::UREM);
Tim Northover3b0846e2014-05-24 12:50:23 +00002990 case Instruction::Ret:
2991 return SelectRet(I);
2992 case Instruction::Trunc:
2993 return SelectTrunc(I);
2994 case Instruction::ZExt:
2995 case Instruction::SExt:
2996 return SelectIntExt(I);
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00002997
2998 // FIXME: All of these should really be handled by the target-independent
2999 // selector -> improve FastISel tblgen.
Tim Northover3b0846e2014-05-24 12:50:23 +00003000 case Instruction::Mul:
Tim Northover3b0846e2014-05-24 12:50:23 +00003001 return SelectMul(I);
Juergen Ributzkaa75cb112014-07-30 22:04:22 +00003002 case Instruction::Shl:
3003 return SelectShift(I, /*IsLeftShift=*/true, /*IsArithmetic=*/false);
3004 case Instruction::LShr:
3005 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/false);
3006 case Instruction::AShr:
3007 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/true);
Juergen Ributzkac537bd22014-07-31 06:25:37 +00003008 case Instruction::BitCast:
3009 return SelectBitCast(I);
Tim Northover3b0846e2014-05-24 12:50:23 +00003010 }
3011 return false;
3012 // Silence warnings.
3013 (void)&CC_AArch64_DarwinPCS_VarArg;
3014}
3015
3016namespace llvm {
3017llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &funcInfo,
3018 const TargetLibraryInfo *libInfo) {
3019 return new AArch64FastISel(funcInfo, libInfo);
3020}
3021}