blob: a81917638bb7b1a7f079cea93a5c6daaa87feced [file] [log] [blame]
Eric Christopher84bdfd82010-07-21 22:26:11 +00001//===-- ARMFastISel.cpp - ARM 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 ARM-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// ARMGenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ARM.h"
Eric Christopher0d274a02010-08-19 00:37:05 +000017#include "ARMBaseInstrInfo.h"
Eric Christopher72497e52010-09-10 23:18:12 +000018#include "ARMCallingConv.h"
Eric Christopher83a5ec82010-10-01 23:24:42 +000019#include "ARMConstantPoolValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Evan Chenga20cde32011-07-20 23:34:39 +000022#include "MCTargetDesc/ARMAddressingModes.h"
JF Bastien3c6bb8e2013-06-11 22:13:46 +000023#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/Analysis.h"
25#include "llvm/CodeGen/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineMemOperand.h"
31#include "llvm/CodeGen/MachineModuleInfo.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000033#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/CallingConv.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000037#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/GlobalVariable.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/IntrinsicInst.h"
41#include "llvm/IR/Module.h"
42#include "llvm/IR/Operator.h"
Eric Christopher663f4992010-08-17 00:46:57 +000043#include "llvm/Support/CommandLine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000044#include "llvm/Support/ErrorHandling.h"
Eric Christopher09f757d2010-08-17 01:25:29 +000045#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetLowering.h"
47#include "llvm/Target/TargetMachine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000048#include "llvm/Target/TargetOptions.h"
49using namespace llvm;
50
Eric Christopher347f4c32010-12-15 23:47:29 +000051extern cl::opt<bool> EnableARMLongCalls;
52
Eric Christopher84bdfd82010-07-21 22:26:11 +000053namespace {
Eric Christopher0a3c28b2010-11-20 22:38:27 +000054
Eric Christopherfef5f312010-11-19 22:30:02 +000055 // All possible address modes, plus some.
56 typedef struct Address {
57 enum {
58 RegBase,
59 FrameIndexBase
60 } BaseType;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000061
Eric Christopherfef5f312010-11-19 22:30:02 +000062 union {
63 unsigned Reg;
64 int FI;
65 } Base;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000066
Eric Christopherfef5f312010-11-19 22:30:02 +000067 int Offset;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000068
Eric Christopherfef5f312010-11-19 22:30:02 +000069 // Innocuous defaults for our address.
70 Address()
Jim Grosbach4e983162011-05-16 22:24:07 +000071 : BaseType(RegBase), Offset(0) {
Eric Christopherfef5f312010-11-19 22:30:02 +000072 Base.Reg = 0;
73 }
74 } Address;
Eric Christopher84bdfd82010-07-21 22:26:11 +000075
Craig Topper26696312014-03-18 07:27:13 +000076class ARMFastISel final : public FastISel {
Eric Christopher84bdfd82010-07-21 22:26:11 +000077
78 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
79 /// make the right decision when generating code for different targets.
80 const ARMSubtarget *Subtarget;
Bill Wendling6c1d9592013-12-30 05:17:29 +000081 Module &M;
Eric Christopher09f757d2010-08-17 01:25:29 +000082 const TargetMachine &TM;
83 const TargetInstrInfo &TII;
84 const TargetLowering &TLI;
Eric Christopher83a5ec82010-10-01 23:24:42 +000085 ARMFunctionInfo *AFI;
Eric Christopher84bdfd82010-07-21 22:26:11 +000086
Eric Christopherb024be32010-09-29 22:24:45 +000087 // Convenience variables to avoid some queries.
Chad Rosier0439cfc2011-11-08 21:12:00 +000088 bool isThumb2;
Eric Christopherb024be32010-09-29 22:24:45 +000089 LLVMContext *Context;
Eric Christopher6a0333c2010-09-02 01:39:14 +000090
Eric Christopher84bdfd82010-07-21 22:26:11 +000091 public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000092 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
93 const TargetLibraryInfo *libInfo)
94 : FastISel(funcInfo, libInfo),
Bill Wendling76cce192013-12-29 08:00:04 +000095 M(const_cast<Module&>(*funcInfo.Fn->getParent())),
Eric Christopher09f757d2010-08-17 01:25:29 +000096 TM(funcInfo.MF->getTarget()),
97 TII(*TM.getInstrInfo()),
98 TLI(*TM.getTargetLowering()) {
Eric Christopher84bdfd82010-07-21 22:26:11 +000099 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher8d03b8a2010-08-23 22:32:45 +0000100 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier0439cfc2011-11-08 21:12:00 +0000101 isThumb2 = AFI->isThumbFunction();
Eric Christopherb024be32010-09-29 22:24:45 +0000102 Context = &funcInfo.Fn->getContext();
Eric Christopher84bdfd82010-07-21 22:26:11 +0000103 }
104
Eric Christopherd8e8a292010-08-20 00:20:31 +0000105 // Code from FastISel.cpp.
Craig Topperfd1c9252012-08-18 21:38:45 +0000106 private:
107 unsigned FastEmitInst_(unsigned MachineInstOpcode,
108 const TargetRegisterClass *RC);
109 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC,
111 unsigned Op0, bool Op0IsKill);
112 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
113 const TargetRegisterClass *RC,
114 unsigned Op0, bool Op0IsKill,
115 unsigned Op1, bool Op1IsKill);
116 unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
117 const TargetRegisterClass *RC,
118 unsigned Op0, bool Op0IsKill,
119 unsigned Op1, bool Op1IsKill,
120 unsigned Op2, bool Op2IsKill);
121 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
122 const TargetRegisterClass *RC,
123 unsigned Op0, bool Op0IsKill,
124 uint64_t Imm);
125 unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
126 const TargetRegisterClass *RC,
127 unsigned Op0, bool Op0IsKill,
128 const ConstantFP *FPImm);
129 unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
130 const TargetRegisterClass *RC,
131 unsigned Op0, bool Op0IsKill,
132 unsigned Op1, bool Op1IsKill,
133 uint64_t Imm);
134 unsigned FastEmitInst_i(unsigned MachineInstOpcode,
135 const TargetRegisterClass *RC,
136 uint64_t Imm);
137 unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
138 const TargetRegisterClass *RC,
139 uint64_t Imm1, uint64_t Imm2);
Eric Christopher174d8722011-03-12 01:09:29 +0000140
Craig Topperfd1c9252012-08-18 21:38:45 +0000141 unsigned FastEmitInst_extractsubreg(MVT RetVT,
142 unsigned Op0, bool Op0IsKill,
143 uint32_t Idx);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000144
Eric Christopherd8e8a292010-08-20 00:20:31 +0000145 // Backend specific FastISel code.
Craig Topperfd1c9252012-08-18 21:38:45 +0000146 private:
Craig Topper6bc27bf2014-03-10 02:09:33 +0000147 bool TargetSelectInstruction(const Instruction *I) override;
148 unsigned TargetMaterializeConstant(const Constant *C) override;
149 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
150 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
151 const LoadInst *LI) override;
152 bool FastLowerArguments() override;
Craig Topperfd1c9252012-08-18 21:38:45 +0000153 private:
Eric Christopher84bdfd82010-07-21 22:26:11 +0000154 #include "ARMGenFastISel.inc"
Eric Christopher2ff757d2010-09-09 01:06:51 +0000155
Eric Christopher00202ee2010-08-23 21:44:12 +0000156 // Instruction selection routines.
Eric Christophercc766a22010-09-10 23:10:30 +0000157 private:
Eric Christopher2f8637d2010-10-21 21:47:51 +0000158 bool SelectLoad(const Instruction *I);
159 bool SelectStore(const Instruction *I);
160 bool SelectBranch(const Instruction *I);
Chad Rosierded4c992012-02-07 23:56:08 +0000161 bool SelectIndirectBr(const Instruction *I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000162 bool SelectCmp(const Instruction *I);
163 bool SelectFPExt(const Instruction *I);
164 bool SelectFPTrunc(const Instruction *I);
Chad Rosier685b20c2012-02-06 23:50:07 +0000165 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
166 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
Chad Rosiere023d5d2012-02-03 21:14:11 +0000167 bool SelectIToFP(const Instruction *I, bool isSigned);
168 bool SelectFPToI(const Instruction *I, bool isSigned);
Chad Rosieraaa55a82012-02-03 21:07:27 +0000169 bool SelectDiv(const Instruction *I, bool isSigned);
Chad Rosierb84a4b42012-02-03 21:23:45 +0000170 bool SelectRem(const Instruction *I, bool isSigned);
Chad Rosiera7ebc562011-11-11 23:31:03 +0000171 bool SelectCall(const Instruction *I, const char *IntrMemName);
172 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000173 bool SelectSelect(const Instruction *I);
Eric Christopher93bbe652010-10-22 01:28:00 +0000174 bool SelectRet(const Instruction *I);
Chad Rosieree7e4522011-11-02 00:18:48 +0000175 bool SelectTrunc(const Instruction *I);
176 bool SelectIntExt(const Instruction *I);
Jush Lu4705da92012-08-03 02:37:48 +0000177 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
Eric Christopher84bdfd82010-07-21 22:26:11 +0000178
Eric Christopher00202ee2010-08-23 21:44:12 +0000179 // Utility routines.
Eric Christopher0d274a02010-08-19 00:37:05 +0000180 private:
Jim Grosbach06c2a682013-08-16 23:37:31 +0000181 unsigned constrainOperandRegClass(const MCInstrDesc &II, unsigned OpNum,
182 unsigned Op);
Chris Lattner229907c2011-07-18 04:54:35 +0000183 bool isTypeLegal(Type *Ty, MVT &VT);
184 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosier9cf803c2011-11-02 18:08:25 +0000185 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
186 bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000187 bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosiera26979b2011-12-14 17:26:05 +0000188 unsigned Alignment = 0, bool isZExt = true,
189 bool allocReg = true);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000190 bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +0000191 unsigned Alignment = 0);
Eric Christopherfef5f312010-11-19 22:30:02 +0000192 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosier150d35b2012-12-17 22:35:29 +0000193 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
Chad Rosier057b6d32011-11-14 23:04:09 +0000194 bool ARMIsMemCpySmall(uint64_t Len);
Chad Rosier9f5c68a2012-12-06 01:34:31 +0000195 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
196 unsigned Alignment);
Chad Rosier62a144f2012-12-17 19:59:43 +0000197 unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000198 unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
199 unsigned ARMMaterializeInt(const Constant *C, MVT VT);
200 unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT);
201 unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg);
202 unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000203 unsigned ARMSelectCallOp(bool UseReg);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000204 unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000205
Eric Christopher72497e52010-09-10 23:18:12 +0000206 // Call handling routines.
207 private:
Jush Lue67e07b2012-07-19 09:49:00 +0000208 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
209 bool Return,
210 bool isVarArg);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000211 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christopher79398062010-09-29 23:11:09 +0000212 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +0000213 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +0000214 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
215 SmallVectorImpl<unsigned> &RegArgs,
216 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000217 unsigned &NumBytes,
218 bool isVarArg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000219 unsigned getLibcallReg(const Twine &Name);
Duncan Sandsf5dda012010-11-03 11:35:31 +0000220 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +0000221 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000222 unsigned &NumBytes, bool isVarArg);
Eric Christopher7990df12010-09-28 01:21:42 +0000223 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopher72497e52010-09-10 23:18:12 +0000224
225 // OptionalDef handling routines.
226 private:
Eric Christopher174d8722011-03-12 01:09:29 +0000227 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher0d274a02010-08-19 00:37:05 +0000228 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
229 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Chad Rosier150d35b2012-12-17 22:35:29 +0000230 void AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000231 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000232 unsigned Flags, bool useAM3);
Eric Christopher0d274a02010-08-19 00:37:05 +0000233};
Eric Christopher84bdfd82010-07-21 22:26:11 +0000234
235} // end anonymous namespace
236
Eric Christopher72497e52010-09-10 23:18:12 +0000237#include "ARMGenCallingConv.inc"
Eric Christopher84bdfd82010-07-21 22:26:11 +0000238
Eric Christopher0d274a02010-08-19 00:37:05 +0000239// DefinesOptionalPredicate - This is different from DefinesPredicate in that
240// we don't care about implicit defs here, just places we'll need to add a
241// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
242bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000243 if (!MI->hasOptionalDef())
Eric Christopher0d274a02010-08-19 00:37:05 +0000244 return false;
245
246 // Look to see if our OptionalDef is defining CPSR or CCR.
247 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
248 const MachineOperand &MO = MI->getOperand(i);
Eric Christopher985d9e42010-08-20 00:36:24 +0000249 if (!MO.isReg() || !MO.isDef()) continue;
250 if (MO.getReg() == ARM::CPSR)
Eric Christopher0d274a02010-08-19 00:37:05 +0000251 *CPSR = true;
252 }
253 return true;
254}
255
Eric Christopher174d8722011-03-12 01:09:29 +0000256bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000257 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher501d2e22011-04-29 00:03:10 +0000258
Joey Goulya5153cb2013-09-09 14:21:49 +0000259 // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000260 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopher174d8722011-03-12 01:09:29 +0000261 AFI->isThumb2Function())
Joey Goulya5153cb2013-09-09 14:21:49 +0000262 return MI->isPredicable();
Eric Christopher501d2e22011-04-29 00:03:10 +0000263
Evan Cheng6cc775f2011-06-28 19:10:37 +0000264 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
265 if (MCID.OpInfo[i].isPredicate())
Eric Christopher174d8722011-03-12 01:09:29 +0000266 return true;
Eric Christopher501d2e22011-04-29 00:03:10 +0000267
Eric Christopher174d8722011-03-12 01:09:29 +0000268 return false;
269}
270
Eric Christopher0d274a02010-08-19 00:37:05 +0000271// If the machine is predicable go ahead and add the predicate operands, if
272// it needs default CC operands add those.
Eric Christophere8fccc82010-11-02 01:21:28 +0000273// TODO: If we want to support thumb1 then we'll need to deal with optional
274// CPSR defs that need to be added before the remaining operands. See s_cc_out
275// for descriptions why.
Eric Christopher0d274a02010-08-19 00:37:05 +0000276const MachineInstrBuilder &
277ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
278 MachineInstr *MI = &*MIB;
279
Eric Christopher174d8722011-03-12 01:09:29 +0000280 // Do we use a predicate? or...
281 // Are we NEON in ARM mode and have a predicate operand? If so, I know
282 // we're not predicable but add it anyways.
Joey Goulya5153cb2013-09-09 14:21:49 +0000283 if (isARMNEONPred(MI))
Eric Christopher0d274a02010-08-19 00:37:05 +0000284 AddDefaultPred(MIB);
Eric Christopher501d2e22011-04-29 00:03:10 +0000285
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000286 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
Eric Christopher0d274a02010-08-19 00:37:05 +0000287 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christophera5d60c62010-08-19 15:35:27 +0000288 bool CPSR = false;
Eric Christopher0d274a02010-08-19 00:37:05 +0000289 if (DefinesOptionalPredicate(MI, &CPSR)) {
290 if (CPSR)
291 AddDefaultT1CC(MIB);
292 else
293 AddDefaultCC(MIB);
294 }
295 return MIB;
296}
297
Jim Grosbach06c2a682013-08-16 23:37:31 +0000298unsigned ARMFastISel::constrainOperandRegClass(const MCInstrDesc &II,
299 unsigned Op, unsigned OpNum) {
300 if (TargetRegisterInfo::isVirtualRegister(Op)) {
301 const TargetRegisterClass *RegClass =
302 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
303 if (!MRI.constrainRegClass(Op, RegClass)) {
304 // If it's not legal to COPY between the register classes, something
305 // has gone very wrong before we got here.
306 unsigned NewOp = createResultReg(RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach06c2a682013-08-16 23:37:31 +0000308 TII.get(TargetOpcode::COPY), NewOp).addReg(Op));
309 return NewOp;
310 }
311 }
312 return Op;
313}
314
Eric Christopher09f757d2010-08-17 01:25:29 +0000315unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
316 const TargetRegisterClass* RC) {
317 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000318 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000319
Rafael Espindolaea09c592014-02-18 22:05:46 +0000320 AddOptionalDefs(
321 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg));
Eric Christopher09f757d2010-08-17 01:25:29 +0000322 return ResultReg;
323}
324
325unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
326 const TargetRegisterClass *RC,
327 unsigned Op0, bool Op0IsKill) {
328 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000329 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000330
Jim Grosbach06c2a682013-08-16 23:37:31 +0000331 // Make sure the input operand is sufficiently constrained to be legal
332 // for this instruction.
333 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000334 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000335 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
336 ResultReg).addReg(Op0, Op0IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000337 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000338 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000339 .addReg(Op0, Op0IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000340 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000341 TII.get(TargetOpcode::COPY), ResultReg)
342 .addReg(II.ImplicitDefs[0]));
343 }
344 return ResultReg;
345}
346
347unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
348 const TargetRegisterClass *RC,
349 unsigned Op0, bool Op0IsKill,
350 unsigned Op1, bool Op1IsKill) {
351 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000352 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000353
Jim Grosbach06c2a682013-08-16 23:37:31 +0000354 // Make sure the input operands are sufficiently constrained to be legal
355 // for this instruction.
356 Op0 = constrainOperandRegClass(II, Op0, 1);
357 Op1 = constrainOperandRegClass(II, Op1, 2);
358
Chad Rosier0bc51322012-02-15 17:36:21 +0000359 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000360 AddOptionalDefs(
361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
362 .addReg(Op0, Op0IsKill * RegState::Kill)
363 .addReg(Op1, Op1IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000364 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000365 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000366 .addReg(Op0, Op0IsKill * RegState::Kill)
367 .addReg(Op1, Op1IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000368 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000369 TII.get(TargetOpcode::COPY), ResultReg)
370 .addReg(II.ImplicitDefs[0]));
371 }
372 return ResultReg;
373}
374
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000375unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
376 const TargetRegisterClass *RC,
377 unsigned Op0, bool Op0IsKill,
378 unsigned Op1, bool Op1IsKill,
379 unsigned Op2, bool Op2IsKill) {
380 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000381 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000382
Jim Grosbach06c2a682013-08-16 23:37:31 +0000383 // Make sure the input operands are sufficiently constrained to be legal
384 // for this instruction.
385 Op0 = constrainOperandRegClass(II, Op0, 1);
386 Op1 = constrainOperandRegClass(II, Op1, 2);
387 Op2 = constrainOperandRegClass(II, Op1, 3);
388
Chad Rosier0bc51322012-02-15 17:36:21 +0000389 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000390 AddOptionalDefs(
391 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
392 .addReg(Op0, Op0IsKill * RegState::Kill)
393 .addReg(Op1, Op1IsKill * RegState::Kill)
394 .addReg(Op2, Op2IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000395 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000396 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000397 .addReg(Op0, Op0IsKill * RegState::Kill)
398 .addReg(Op1, Op1IsKill * RegState::Kill)
399 .addReg(Op2, Op2IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000400 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000401 TII.get(TargetOpcode::COPY), ResultReg)
402 .addReg(II.ImplicitDefs[0]));
403 }
404 return ResultReg;
405}
406
Eric Christopher09f757d2010-08-17 01:25:29 +0000407unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
408 const TargetRegisterClass *RC,
409 unsigned Op0, bool Op0IsKill,
410 uint64_t Imm) {
411 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000412 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000413
Jim Grosbach06c2a682013-08-16 23:37:31 +0000414 // Make sure the input operand is sufficiently constrained to be legal
415 // for this instruction.
416 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000417 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000418 AddOptionalDefs(
419 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
420 .addReg(Op0, Op0IsKill * RegState::Kill)
421 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000422 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000423 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000424 .addReg(Op0, Op0IsKill * RegState::Kill)
425 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000426 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000427 TII.get(TargetOpcode::COPY), ResultReg)
428 .addReg(II.ImplicitDefs[0]));
429 }
430 return ResultReg;
431}
432
433unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
434 const TargetRegisterClass *RC,
435 unsigned Op0, bool Op0IsKill,
436 const ConstantFP *FPImm) {
437 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000438 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000439
Jim Grosbach06c2a682013-08-16 23:37:31 +0000440 // Make sure the input operand is sufficiently constrained to be legal
441 // for this instruction.
442 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000443 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000444 AddOptionalDefs(
445 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
446 .addReg(Op0, Op0IsKill * RegState::Kill)
447 .addFPImm(FPImm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000448 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000449 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000450 .addReg(Op0, Op0IsKill * RegState::Kill)
451 .addFPImm(FPImm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000452 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000453 TII.get(TargetOpcode::COPY), ResultReg)
454 .addReg(II.ImplicitDefs[0]));
455 }
456 return ResultReg;
457}
458
459unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
460 const TargetRegisterClass *RC,
461 unsigned Op0, bool Op0IsKill,
462 unsigned Op1, bool Op1IsKill,
463 uint64_t Imm) {
464 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000465 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000466
Jim Grosbach06c2a682013-08-16 23:37:31 +0000467 // Make sure the input operands are sufficiently constrained to be legal
468 // for this instruction.
469 Op0 = constrainOperandRegClass(II, Op0, 1);
470 Op1 = constrainOperandRegClass(II, Op1, 2);
Chad Rosier0bc51322012-02-15 17:36:21 +0000471 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000472 AddOptionalDefs(
473 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
474 .addReg(Op0, Op0IsKill * RegState::Kill)
475 .addReg(Op1, Op1IsKill * RegState::Kill)
476 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000477 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000478 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000479 .addReg(Op0, Op0IsKill * RegState::Kill)
480 .addReg(Op1, Op1IsKill * RegState::Kill)
481 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000482 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000483 TII.get(TargetOpcode::COPY), ResultReg)
484 .addReg(II.ImplicitDefs[0]));
485 }
486 return ResultReg;
487}
488
489unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
490 const TargetRegisterClass *RC,
491 uint64_t Imm) {
492 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000493 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000494
Chad Rosier0bc51322012-02-15 17:36:21 +0000495 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000496 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
497 ResultReg).addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000498 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000499 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000500 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000501 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000502 TII.get(TargetOpcode::COPY), ResultReg)
503 .addReg(II.ImplicitDefs[0]));
504 }
505 return ResultReg;
506}
507
Eric Christopher77087462011-04-29 22:07:50 +0000508unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
509 const TargetRegisterClass *RC,
510 uint64_t Imm1, uint64_t Imm2) {
511 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000512 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0713a9d2011-06-08 23:55:35 +0000513
Chad Rosier0bc51322012-02-15 17:36:21 +0000514 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000515 AddOptionalDefs(
516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
517 .addImm(Imm1)
518 .addImm(Imm2));
Chad Rosier0bc51322012-02-15 17:36:21 +0000519 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000520 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher77087462011-04-29 22:07:50 +0000521 .addImm(Imm1).addImm(Imm2));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000522 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher77087462011-04-29 22:07:50 +0000523 TII.get(TargetOpcode::COPY),
524 ResultReg)
525 .addReg(II.ImplicitDefs[0]));
526 }
527 return ResultReg;
528}
529
Eric Christopher09f757d2010-08-17 01:25:29 +0000530unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
531 unsigned Op0, bool Op0IsKill,
532 uint32_t Idx) {
533 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
534 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
535 "Cannot yet extract from physregs");
Chad Rosier0bc51322012-02-15 17:36:21 +0000536
Eric Christopher0d274a02010-08-19 00:37:05 +0000537 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000538 DbgLoc, TII.get(TargetOpcode::COPY), ResultReg)
Chad Rosier0bc51322012-02-15 17:36:21 +0000539 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
Eric Christopher09f757d2010-08-17 01:25:29 +0000540 return ResultReg;
541}
542
Eric Christopher860fc932010-09-10 00:34:35 +0000543// TODO: Don't worry about 64-bit now, but when this is fixed remove the
544// checks from the various callers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000545unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000546 if (VT == MVT::f64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000547
Eric Christopher4bd70472010-09-09 21:44:45 +0000548 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000549 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000550 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopher4bd70472010-09-09 21:44:45 +0000551 .addReg(SrcReg));
552 return MoveReg;
553}
554
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000555unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000556 if (VT == MVT::i64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000557
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000558 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000559 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000560 TII.get(ARM::VMOVRS), MoveReg)
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000561 .addReg(SrcReg));
562 return MoveReg;
563}
564
Eric Christopher3cf63f12010-09-09 00:19:41 +0000565// For double width floating point we need to materialize two constants
566// (the high and the low) into integer registers then use a move to get
567// the combined constant into an FP reg.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000568unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
Eric Christopher3cf63f12010-09-09 00:19:41 +0000569 const APFloat Val = CFP->getValueAPF();
Duncan Sands14627772010-11-03 12:17:33 +0000570 bool is64bit = VT == MVT::f64;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000571
Eric Christopher3cf63f12010-09-09 00:19:41 +0000572 // This checks to see if we can use VFP3 instructions to materialize
573 // a constant, otherwise we have to go through the constant pool.
574 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000575 int Imm;
576 unsigned Opc;
577 if (is64bit) {
578 Imm = ARM_AM::getFP64Imm(Val);
579 Opc = ARM::FCONSTD;
580 } else {
581 Imm = ARM_AM::getFP32Imm(Val);
582 Opc = ARM::FCONSTS;
583 }
Eric Christopher3cf63f12010-09-09 00:19:41 +0000584 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000585 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
586 TII.get(Opc), DestReg).addImm(Imm));
Eric Christopher3cf63f12010-09-09 00:19:41 +0000587 return DestReg;
588 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000589
Eric Christopher860fc932010-09-10 00:34:35 +0000590 // Require VFP2 for loading fp constants.
Eric Christopher22fd29a2010-09-09 23:50:00 +0000591 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000592
Eric Christopher22fd29a2010-09-09 23:50:00 +0000593 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000594 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000595 if (Align == 0) {
596 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000597 Align = DL.getTypeAllocSize(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000598 }
599 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
600 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
601 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000602
Eric Christopher860fc932010-09-10 00:34:35 +0000603 // The extra reg is for addrmode5.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000604 AddOptionalDefs(
605 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
606 .addConstantPoolIndex(Idx)
607 .addReg(0));
Eric Christopher22fd29a2010-09-09 23:50:00 +0000608 return DestReg;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000609}
610
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000611unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
Eric Christopher7ac602b2010-10-11 08:38:55 +0000612
Chad Rosier67f96882011-11-04 22:29:00 +0000613 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
614 return false;
Eric Christophere4dd7372010-11-03 20:21:17 +0000615
616 // If we can do this in a single instruction without a constant pool entry
617 // do so now.
618 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiere8b8b772011-11-04 23:09:49 +0000619 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier0439cfc2011-11-08 21:12:00 +0000620 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier2e82ad12012-11-27 01:06:49 +0000621 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
622 &ARM::GPRRegClass;
623 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000624 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier67f96882011-11-04 22:29:00 +0000625 TII.get(Opc), ImmReg)
Chad Rosierd0191a52011-11-05 20:16:15 +0000626 .addImm(CI->getZExtValue()));
Chad Rosier67f96882011-11-04 22:29:00 +0000627 return ImmReg;
Eric Christophere4dd7372010-11-03 20:21:17 +0000628 }
629
Chad Rosier2a3503e2011-11-11 00:36:21 +0000630 // Use MVN to emit negative constants.
631 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
632 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosiere19b0a92011-11-11 06:27:41 +0000633 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier2a3503e2011-11-11 00:36:21 +0000634 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosiere19b0a92011-11-11 06:27:41 +0000635 if (UseImm) {
Chad Rosier2a3503e2011-11-11 00:36:21 +0000636 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
637 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000638 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier2a3503e2011-11-11 00:36:21 +0000639 TII.get(Opc), ImmReg)
640 .addImm(Imm));
641 return ImmReg;
642 }
643 }
644
645 // Load from constant pool. For now 32-bit only.
Chad Rosier67f96882011-11-04 22:29:00 +0000646 if (VT != MVT::i32)
647 return false;
648
649 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
650
Eric Christopherc3e118e2010-09-02 23:43:26 +0000651 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000652 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000653 if (Align == 0) {
654 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000655 Align = DL.getTypeAllocSize(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000656 }
657 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000658
Chad Rosier0439cfc2011-11-08 21:12:00 +0000659 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000660 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher953b1af2010-09-28 21:55:34 +0000661 TII.get(ARM::t2LDRpci), DestReg)
662 .addConstantPoolIndex(Idx));
Tim Northovere42fb072014-02-04 10:38:46 +0000663 else {
Eric Christopher22d04922010-11-12 09:48:30 +0000664 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000665 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000666 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher953b1af2010-09-28 21:55:34 +0000667 TII.get(ARM::LDRcp), DestReg)
668 .addConstantPoolIndex(Idx)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000669 .addImm(0));
Tim Northovere42fb072014-02-04 10:38:46 +0000670 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000671
Eric Christopherc3e118e2010-09-02 23:43:26 +0000672 return DestReg;
Eric Christopher92db2012010-09-02 01:48:11 +0000673}
674
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000675unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
Eric Christopher7787f792010-10-02 00:32:44 +0000676 // For now 32-bit only.
Duncan Sands14627772010-11-03 12:17:33 +0000677 if (VT != MVT::i32) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000678
Eric Christopher7787f792010-10-02 00:32:44 +0000679 Reloc::Model RelocM = TM.getRelocationModel();
Jush Lue87e5592012-08-29 02:41:21 +0000680 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
Chad Rosier65710a72012-11-07 00:13:01 +0000681 const TargetRegisterClass *RC = isThumb2 ?
682 (const TargetRegisterClass*)&ARM::rGPRRegClass :
683 (const TargetRegisterClass*)&ARM::GPRRegClass;
684 unsigned DestReg = createResultReg(RC);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000685
Tim Northoverd6a729b2014-01-06 14:28:05 +0000686 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
JF Bastien18db1f22013-06-14 02:49:43 +0000687 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
688 bool IsThreadLocal = GVar && GVar->isThreadLocal();
Tim Northoverd6a729b2014-01-06 14:28:05 +0000689 if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0;
JF Bastien18db1f22013-06-14 02:49:43 +0000690
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000691 // Use movw+movt when possible, it avoids constant pool entries.
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000692 // Non-darwin targets only support static movt relocations in FastISel.
Jakob Stoklund Olesen083dbdc2012-01-07 20:49:15 +0000693 if (Subtarget->useMovt() &&
Tim Northoverd6a729b2014-01-06 14:28:05 +0000694 (Subtarget->isTargetMachO() || RelocM == Reloc::Static)) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000695 unsigned Opc;
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000696 unsigned char TF = 0;
Tim Northoverd6a729b2014-01-06 14:28:05 +0000697 if (Subtarget->isTargetMachO())
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000698 TF = ARMII::MO_NONLAZY;
699
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000700 switch (RelocM) {
701 case Reloc::PIC_:
702 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
703 break;
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000704 default:
705 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
706 break;
707 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000708 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
709 TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF));
Eric Christopher7787f792010-10-02 00:32:44 +0000710 } else {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000711 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000712 unsigned Align = DL.getPrefTypeAlignment(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000713 if (Align == 0) {
714 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000715 Align = DL.getTypeAllocSize(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000716 }
717
Jush Lu47172a02012-09-27 05:21:41 +0000718 if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_)
719 return ARMLowerPICELF(GV, Align, VT);
720
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000721 // Grab index.
722 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
723 (Subtarget->isThumb() ? 4 : 8);
724 unsigned Id = AFI->createPICLabelUId();
725 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
726 ARMCP::CPValue,
727 PCAdj);
728 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
729
730 // Load value.
731 MachineInstrBuilder MIB;
732 if (isThumb2) {
733 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000734 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
735 DestReg).addConstantPoolIndex(Idx);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000736 if (RelocM == Reloc::PIC_)
737 MIB.addImm(Id);
Jush Lue87e5592012-08-29 02:41:21 +0000738 AddOptionalDefs(MIB);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000739 } else {
740 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000741 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000742 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
743 TII.get(ARM::LDRcp), DestReg)
744 .addConstantPoolIndex(Idx)
745 .addImm(0);
Jush Lue87e5592012-08-29 02:41:21 +0000746 AddOptionalDefs(MIB);
747
748 if (RelocM == Reloc::PIC_) {
749 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
750 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
751
752 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000753 DbgLoc, TII.get(Opc), NewDestReg)
Jush Lue87e5592012-08-29 02:41:21 +0000754 .addReg(DestReg)
755 .addImm(Id);
756 AddOptionalDefs(MIB);
757 return NewDestReg;
758 }
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000759 }
Eric Christopher7787f792010-10-02 00:32:44 +0000760 }
Eli Friedman86585792011-06-03 01:13:19 +0000761
Jush Lue87e5592012-08-29 02:41:21 +0000762 if (IsIndirect) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000763 MachineInstrBuilder MIB;
Eli Friedman86585792011-06-03 01:13:19 +0000764 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000765 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000766 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbache7e2aca2011-09-13 20:30:37 +0000767 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedman86585792011-06-03 01:13:19 +0000768 .addReg(DestReg)
769 .addImm(0);
770 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000771 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
772 TII.get(ARM::LDRi12), NewDestReg)
773 .addReg(DestReg)
774 .addImm(0);
Eli Friedman86585792011-06-03 01:13:19 +0000775 DestReg = NewDestReg;
776 AddOptionalDefs(MIB);
777 }
778
Eric Christopher7787f792010-10-02 00:32:44 +0000779 return DestReg;
Eric Christopher83a5ec82010-10-01 23:24:42 +0000780}
781
Eric Christopher3cf63f12010-09-09 00:19:41 +0000782unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
Patrik Hagglundc494d242012-12-17 14:30:06 +0000783 EVT CEVT = TLI.getValueType(C->getType(), true);
784
785 // Only handle simple types.
786 if (!CEVT.isSimple()) return 0;
787 MVT VT = CEVT.getSimpleVT();
Eric Christopher3cf63f12010-09-09 00:19:41 +0000788
789 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
790 return ARMMaterializeFP(CFP, VT);
Eric Christopher83a5ec82010-10-01 23:24:42 +0000791 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
792 return ARMMaterializeGV(GV, VT);
793 else if (isa<ConstantInt>(C))
794 return ARMMaterializeInt(C, VT);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000795
Eric Christopher83a5ec82010-10-01 23:24:42 +0000796 return 0;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000797}
798
Chad Rosier0eff3e52011-11-17 21:46:13 +0000799// TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
800
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000801unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
802 // Don't handle dynamic allocas.
803 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000804
Duncan Sandsf5dda012010-11-03 11:35:31 +0000805 MVT VT;
Chad Rosier466d3d82012-05-11 16:41:38 +0000806 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000807
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000808 DenseMap<const AllocaInst*, int>::iterator SI =
809 FuncInfo.StaticAllocaMap.find(AI);
810
811 // This will get lowered later into the correct offsets and registers
812 // via rewriteXFrameIndex.
813 if (SI != FuncInfo.StaticAllocaMap.end()) {
Tim Northover76fc8a42013-12-11 16:04:57 +0000814 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Craig Topper760b1342012-02-22 05:59:10 +0000815 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000816 unsigned ResultReg = createResultReg(RC);
Tim Northover76fc8a42013-12-11 16:04:57 +0000817 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
818
Rafael Espindolaea09c592014-02-18 22:05:46 +0000819 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000820 TII.get(Opc), ResultReg)
821 .addFrameIndex(SI->second)
822 .addImm(0));
823 return ResultReg;
824 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000825
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000826 return 0;
827}
828
Chris Lattner229907c2011-07-18 04:54:35 +0000829bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000830 EVT evt = TLI.getValueType(Ty, true);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000831
Eric Christopher761e7fb2010-08-25 07:23:49 +0000832 // Only handle simple types.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000833 if (evt == MVT::Other || !evt.isSimple()) return false;
834 VT = evt.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +0000835
Eric Christopher901176a2010-08-31 01:28:42 +0000836 // Handle all legal types, i.e. a register that will directly hold this
837 // value.
838 return TLI.isTypeLegal(VT);
Eric Christopher761e7fb2010-08-25 07:23:49 +0000839}
840
Chris Lattner229907c2011-07-18 04:54:35 +0000841bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000842 if (isTypeLegal(Ty, VT)) return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000843
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000844 // If this is a type than can be sign or zero-extended to a basic operation
845 // go ahead and accept it now.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000846 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000847 return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000848
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000849 return false;
850}
851
Eric Christopher558b61e2010-11-19 22:36:41 +0000852// Computes the address to get to an object.
Eric Christopherfef5f312010-11-19 22:30:02 +0000853bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000854 // Some boilerplate from the X86 FastISel.
855 const User *U = NULL;
Eric Christopher00202ee2010-08-23 21:44:12 +0000856 unsigned Opcode = Instruction::UserOp1;
Eric Christopher9d4e4712010-08-24 00:07:24 +0000857 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christophercee83d62010-11-19 22:37:58 +0000858 // Don't walk into other basic blocks unless the object is an alloca from
859 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher96494372010-11-15 21:11:06 +0000860 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
861 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
862 Opcode = I->getOpcode();
863 U = I;
864 }
Eric Christopher9d4e4712010-08-24 00:07:24 +0000865 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000866 Opcode = C->getOpcode();
867 U = C;
868 }
869
Chris Lattner229907c2011-07-18 04:54:35 +0000870 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher00202ee2010-08-23 21:44:12 +0000871 if (Ty->getAddressSpace() > 255)
872 // Fast instruction selection doesn't support the special
873 // address spaces.
874 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000875
Eric Christopher00202ee2010-08-23 21:44:12 +0000876 switch (Opcode) {
Eric Christopher2ff757d2010-09-09 01:06:51 +0000877 default:
Eric Christopher00202ee2010-08-23 21:44:12 +0000878 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000879 case Instruction::BitCast:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000880 // Look through bitcasts.
Eric Christopherfef5f312010-11-19 22:30:02 +0000881 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher3931cf92013-07-12 22:08:24 +0000882 case Instruction::IntToPtr:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000883 // Look past no-op inttoptrs.
884 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000885 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000886 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000887 case Instruction::PtrToInt:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000888 // Look past no-op ptrtoints.
889 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000890 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000891 break;
Eric Christopher21d0c172010-10-14 09:29:41 +0000892 case Instruction::GetElementPtr: {
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000893 Address SavedAddr = Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +0000894 int TmpOffset = Addr.Offset;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000895
Eric Christopher21d0c172010-10-14 09:29:41 +0000896 // Iterate through the GEP folding the constants into offsets where
897 // we can.
898 gep_type_iterator GTI = gep_type_begin(U);
899 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
900 i != e; ++i, ++GTI) {
901 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000902 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000903 const StructLayout *SL = DL.getStructLayout(STy);
Eric Christopher21d0c172010-10-14 09:29:41 +0000904 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
905 TmpOffset += SL->getElementOffset(Idx);
906 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000907 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eric Christophera5a779e2011-03-22 19:39:17 +0000908 for (;;) {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000909 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
910 // Constant-offset addressing.
911 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000912 break;
913 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000914 if (canFoldAddIntoGEP(U, Op)) {
915 // A compatible add with a constant operand. Fold the constant.
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000916 ConstantInt *CI =
Eric Christophera5a779e2011-03-22 19:39:17 +0000917 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000918 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000919 // Iterate on the other operand.
920 Op = cast<AddOperator>(Op)->getOperand(0);
921 continue;
Eric Christopher501d2e22011-04-29 00:03:10 +0000922 }
Eric Christophera5a779e2011-03-22 19:39:17 +0000923 // Unsupported
924 goto unsupported_gep;
925 }
Eric Christopher21d0c172010-10-14 09:29:41 +0000926 }
927 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000928
929 // Try to grab the base operand now.
Eric Christopherfef5f312010-11-19 22:30:02 +0000930 Addr.Offset = TmpOffset;
931 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000932
933 // We failed, restore everything and try the other options.
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000934 Addr = SavedAddr;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000935
Eric Christopher21d0c172010-10-14 09:29:41 +0000936 unsupported_gep:
Eric Christopher21d0c172010-10-14 09:29:41 +0000937 break;
938 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000939 case Instruction::Alloca: {
Eric Christopher7cd5cda2010-10-12 05:39:06 +0000940 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000941 DenseMap<const AllocaInst*, int>::iterator SI =
942 FuncInfo.StaticAllocaMap.find(AI);
943 if (SI != FuncInfo.StaticAllocaMap.end()) {
944 Addr.BaseType = Address::FrameIndexBase;
945 Addr.Base.FI = SI->second;
946 return true;
947 }
948 break;
Eric Christopher00202ee2010-08-23 21:44:12 +0000949 }
950 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000951
Eric Christopher9d4e4712010-08-24 00:07:24 +0000952 // Try to get this in a register if nothing else has worked.
Eric Christopherfef5f312010-11-19 22:30:02 +0000953 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
954 return Addr.Base.Reg != 0;
Eric Christopher21d0c172010-10-14 09:29:41 +0000955}
956
Chad Rosier150d35b2012-12-17 22:35:29 +0000957void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
Eric Christopher73bc5b02010-10-21 19:40:30 +0000958 bool needsLowering = false;
Chad Rosier150d35b2012-12-17 22:35:29 +0000959 switch (VT.SimpleTy) {
Craig Toppere55c5562012-02-07 02:50:20 +0000960 default: llvm_unreachable("Unhandled load/store type!");
Eric Christopher73bc5b02010-10-21 19:40:30 +0000961 case MVT::i1:
962 case MVT::i8:
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000963 case MVT::i16:
Eric Christopher73bc5b02010-10-21 19:40:30 +0000964 case MVT::i32:
Chad Rosieradfd2002011-11-14 20:22:27 +0000965 if (!useAM3) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000966 // Integer loads/stores handle 12-bit offsets.
967 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosieradfd2002011-11-14 20:22:27 +0000968 // Handle negative offsets.
Chad Rosier45110fd2011-11-14 22:34:48 +0000969 if (needsLowering && isThumb2)
970 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
971 Addr.Offset > -256);
Chad Rosieradfd2002011-11-14 20:22:27 +0000972 } else {
Chad Rosier5196efd2011-11-13 04:25:02 +0000973 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosier2a1df882011-11-14 04:09:28 +0000974 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosieradfd2002011-11-14 20:22:27 +0000975 }
Eric Christopher73bc5b02010-10-21 19:40:30 +0000976 break;
977 case MVT::f32:
978 case MVT::f64:
979 // Floating point operands handle 8-bit offsets.
Eric Christopherfef5f312010-11-19 22:30:02 +0000980 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher73bc5b02010-10-21 19:40:30 +0000981 break;
982 }
Jim Grosbach055de2c2010-10-27 21:39:08 +0000983
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000984 // If this is a stack pointer and the offset needs to be simplified then
985 // put the alloca address into a register, set the base type back to
986 // register and continue. This should almost never happen.
987 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Craig Topperc7242e02012-04-20 07:30:17 +0000988 const TargetRegisterClass *RC = isThumb2 ?
989 (const TargetRegisterClass*)&ARM::tGPRRegClass :
990 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000991 unsigned ResultReg = createResultReg(RC);
Chad Rosier0439cfc2011-11-08 21:12:00 +0000992 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000993 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000994 TII.get(Opc), ResultReg)
995 .addFrameIndex(Addr.Base.FI)
996 .addImm(0));
997 Addr.Base.Reg = ResultReg;
998 Addr.BaseType = Address::RegBase;
999 }
1000
Eric Christopher73bc5b02010-10-21 19:40:30 +00001001 // Since the offset is too large for the load/store instruction
Eric Christopher74487fc2010-09-02 00:53:56 +00001002 // get the reg+offset into a register.
Eric Christopher73bc5b02010-10-21 19:40:30 +00001003 if (needsLowering) {
Eli Friedman86caced2011-04-29 21:22:56 +00001004 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
1005 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopherfef5f312010-11-19 22:30:02 +00001006 Addr.Offset = 0;
Eric Christopher74487fc2010-09-02 00:53:56 +00001007 }
Eric Christopher00202ee2010-08-23 21:44:12 +00001008}
1009
Chad Rosier150d35b2012-12-17 22:35:29 +00001010void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +00001011 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001012 unsigned Flags, bool useAM3) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001013 // addrmode5 output depends on the selection dag addressing dividing the
1014 // offset by 4 that it then later multiplies. Do this here as well.
Chad Rosier150d35b2012-12-17 22:35:29 +00001015 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
Eric Christopher119ff7f2010-12-01 01:40:24 +00001016 Addr.Offset /= 4;
Eric Christopher501d2e22011-04-29 00:03:10 +00001017
Eric Christopher119ff7f2010-12-01 01:40:24 +00001018 // Frame base works a bit differently. Handle it separately.
1019 if (Addr.BaseType == Address::FrameIndexBase) {
1020 int FI = Addr.Base.FI;
1021 int Offset = Addr.Offset;
1022 MachineMemOperand *MMO =
1023 FuncInfo.MF->getMachineMemOperand(
1024 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarich6528a542011-05-28 20:34:49 +00001025 Flags,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001026 MFI.getObjectSize(FI),
1027 MFI.getObjectAlignment(FI));
1028 // Now add the rest of the operands.
1029 MIB.addFrameIndex(FI);
1030
Bob Wilson80381f62011-12-04 00:52:23 +00001031 // ARM halfword load/stores and signed byte loads need an additional
1032 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +00001033 if (useAM3) {
1034 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
1035 MIB.addReg(0);
1036 MIB.addImm(Imm);
1037 } else {
1038 MIB.addImm(Addr.Offset);
1039 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001040 MIB.addMemOperand(MMO);
1041 } else {
1042 // Now add the rest of the operands.
1043 MIB.addReg(Addr.Base.Reg);
Eric Christopher501d2e22011-04-29 00:03:10 +00001044
Bob Wilson80381f62011-12-04 00:52:23 +00001045 // ARM halfword load/stores and signed byte loads need an additional
1046 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +00001047 if (useAM3) {
1048 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
1049 MIB.addReg(0);
1050 MIB.addImm(Imm);
1051 } else {
1052 MIB.addImm(Addr.Offset);
1053 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001054 }
1055 AddOptionalDefs(MIB);
1056}
1057
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001058bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier563de602011-12-13 19:22:14 +00001059 unsigned Alignment, bool isZExt, bool allocReg) {
Eric Christopher901176a2010-08-31 01:28:42 +00001060 unsigned Opc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001061 bool useAM3 = false;
Chad Rosier563de602011-12-13 19:22:14 +00001062 bool needVMOV = false;
Craig Topper760b1342012-02-22 05:59:10 +00001063 const TargetRegisterClass *RC;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001064 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001065 // This is mostly going to be Neon/vector support.
1066 default: return false;
Chad Rosier023ede52011-11-11 02:38:59 +00001067 case MVT::i1:
Eric Christopher3ce9c4a2010-09-01 18:01:32 +00001068 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001069 if (isThumb2) {
1070 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1071 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
1072 else
1073 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001074 } else {
Chad Rosieradfd2002011-11-14 20:22:27 +00001075 if (isZExt) {
1076 Opc = ARM::LDRBi12;
1077 } else {
1078 Opc = ARM::LDRSB;
1079 useAM3 = true;
1080 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001081 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001082 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher3ce9c4a2010-09-01 18:01:32 +00001083 break;
Chad Rosier2f27fab2011-11-09 21:30:12 +00001084 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001085 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001086 return false;
1087
Chad Rosieradfd2002011-11-14 20:22:27 +00001088 if (isThumb2) {
1089 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1090 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1091 else
1092 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1093 } else {
1094 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1095 useAM3 = true;
1096 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001097 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier2f27fab2011-11-09 21:30:12 +00001098 break;
Eric Christopher901176a2010-08-31 01:28:42 +00001099 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001100 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001101 return false;
1102
Chad Rosieradfd2002011-11-14 20:22:27 +00001103 if (isThumb2) {
1104 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1105 Opc = ARM::t2LDRi8;
1106 else
1107 Opc = ARM::t2LDRi12;
1108 } else {
1109 Opc = ARM::LDRi12;
1110 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001111 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher901176a2010-08-31 01:28:42 +00001112 break;
Eric Christopheraef6499b2010-09-18 01:59:37 +00001113 case MVT::f32:
Chad Rosierded61602011-12-14 17:55:03 +00001114 if (!Subtarget->hasVFP2()) return false;
Chad Rosier563de602011-12-13 19:22:14 +00001115 // Unaligned loads need special handling. Floats require word-alignment.
1116 if (Alignment && Alignment < 4) {
1117 needVMOV = true;
1118 VT = MVT::i32;
1119 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
JF Bastien652fa6a2013-06-09 00:20:24 +00001120 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier563de602011-12-13 19:22:14 +00001121 } else {
1122 Opc = ARM::VLDRS;
1123 RC = TLI.getRegClassFor(VT);
1124 }
Eric Christopheraef6499b2010-09-18 01:59:37 +00001125 break;
1126 case MVT::f64:
Chad Rosierded61602011-12-14 17:55:03 +00001127 if (!Subtarget->hasVFP2()) return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001128 // FIXME: Unaligned loads need special handling. Doublewords require
1129 // word-alignment.
1130 if (Alignment && Alignment < 4)
Chad Rosier563de602011-12-13 19:22:14 +00001131 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001132
Eric Christopheraef6499b2010-09-18 01:59:37 +00001133 Opc = ARM::VLDRD;
Eric Christophera2583ea2010-10-07 05:50:44 +00001134 RC = TLI.getRegClassFor(VT);
Eric Christopheraef6499b2010-09-18 01:59:37 +00001135 break;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001136 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001137 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001138 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001139
Eric Christopher119ff7f2010-12-01 01:40:24 +00001140 // Create the base instruction, then add the operands.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001141 if (allocReg)
1142 ResultReg = createResultReg(RC);
1143 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Rafael Espindolaea09c592014-02-18 22:05:46 +00001144 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001145 TII.get(Opc), ResultReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001146 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Chad Rosier563de602011-12-13 19:22:14 +00001147
1148 // If we had an unaligned load of a float we've converted it to an regular
1149 // load. Now we must move from the GRP to the FP register.
1150 if (needVMOV) {
1151 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001152 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier563de602011-12-13 19:22:14 +00001153 TII.get(ARM::VMOVSR), MoveReg)
1154 .addReg(ResultReg));
1155 ResultReg = MoveReg;
1156 }
Eric Christopher901176a2010-08-31 01:28:42 +00001157 return true;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001158}
1159
Eric Christopher29ab6d12010-09-27 06:02:23 +00001160bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001161 // Atomic loads need special handling.
1162 if (cast<LoadInst>(I)->isAtomic())
1163 return false;
1164
Eric Christopher860fc932010-09-10 00:34:35 +00001165 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001166 MVT VT;
Eric Christopher860fc932010-09-10 00:34:35 +00001167 if (!isLoadTypeLegal(I->getType(), VT))
1168 return false;
1169
Eric Christopher119ff7f2010-12-01 01:40:24 +00001170 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001171 Address Addr;
Eric Christopher119ff7f2010-12-01 01:40:24 +00001172 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001173
1174 unsigned ResultReg;
Chad Rosier563de602011-12-13 19:22:14 +00001175 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1176 return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001177 UpdateValueMap(I, ResultReg);
1178 return true;
1179}
1180
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001181bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +00001182 unsigned Alignment) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001183 unsigned StrOpc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001184 bool useAM3 = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001185 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001186 // This is mostly going to be Neon/vector support.
Eric Christopher74487fc2010-09-02 00:53:56 +00001187 default: return false;
Eric Christopher1e43892e2010-11-02 23:59:09 +00001188 case MVT::i1: {
Craig Topperc7242e02012-04-20 07:30:17 +00001189 unsigned Res = createResultReg(isThumb2 ?
1190 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1191 (const TargetRegisterClass*)&ARM::GPRRegClass);
Chad Rosier0439cfc2011-11-08 21:12:00 +00001192 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001193 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001194 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher1e43892e2010-11-02 23:59:09 +00001195 TII.get(Opc), Res)
1196 .addReg(SrcReg).addImm(1));
1197 SrcReg = Res;
1198 } // Fallthrough here.
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001199 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001200 if (isThumb2) {
1201 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1202 StrOpc = ARM::t2STRBi8;
1203 else
1204 StrOpc = ARM::t2STRBi12;
1205 } else {
1206 StrOpc = ARM::STRBi12;
1207 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001208 break;
1209 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001210 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001211 return false;
1212
Chad Rosieradfd2002011-11-14 20:22:27 +00001213 if (isThumb2) {
1214 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1215 StrOpc = ARM::t2STRHi8;
1216 else
1217 StrOpc = ARM::t2STRHi12;
1218 } else {
1219 StrOpc = ARM::STRH;
1220 useAM3 = true;
1221 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001222 break;
Eric Christopherc918d552010-10-16 01:10:35 +00001223 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001224 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001225 return false;
1226
Chad Rosieradfd2002011-11-14 20:22:27 +00001227 if (isThumb2) {
1228 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1229 StrOpc = ARM::t2STRi8;
1230 else
1231 StrOpc = ARM::t2STRi12;
1232 } else {
1233 StrOpc = ARM::STRi12;
1234 }
Eric Christopherc918d552010-10-16 01:10:35 +00001235 break;
Eric Christopherc3e118e2010-09-02 23:43:26 +00001236 case MVT::f32:
1237 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001238 // Unaligned stores need special handling. Floats require word-alignment.
Chad Rosierec3b77e2011-12-03 02:21:57 +00001239 if (Alignment && Alignment < 4) {
1240 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001241 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosierec3b77e2011-12-03 02:21:57 +00001242 TII.get(ARM::VMOVRS), MoveReg)
1243 .addReg(SrcReg));
1244 SrcReg = MoveReg;
1245 VT = MVT::i32;
1246 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Chad Rosierfce28912011-12-14 17:32:02 +00001247 } else {
1248 StrOpc = ARM::VSTRS;
Chad Rosierec3b77e2011-12-03 02:21:57 +00001249 }
Eric Christopherc3e118e2010-09-02 23:43:26 +00001250 break;
1251 case MVT::f64:
1252 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001253 // FIXME: Unaligned stores need special handling. Doublewords require
1254 // word-alignment.
Chad Rosiera26979b2011-12-14 17:26:05 +00001255 if (Alignment && Alignment < 4)
Chad Rosierec3b77e2011-12-03 02:21:57 +00001256 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001257
Eric Christopherc3e118e2010-09-02 23:43:26 +00001258 StrOpc = ARM::VSTRD;
1259 break;
Eric Christopher74487fc2010-09-02 00:53:56 +00001260 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001261 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001262 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001263
Eric Christopher119ff7f2010-12-01 01:40:24 +00001264 // Create the base instruction, then add the operands.
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001265 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001266 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001267 TII.get(StrOpc))
Chad Rosierce619dd2011-11-17 01:16:53 +00001268 .addReg(SrcReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001269 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher74487fc2010-09-02 00:53:56 +00001270 return true;
1271}
1272
Eric Christopher29ab6d12010-09-27 06:02:23 +00001273bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001274 Value *Op0 = I->getOperand(0);
1275 unsigned SrcReg = 0;
1276
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001277 // Atomic stores need special handling.
1278 if (cast<StoreInst>(I)->isAtomic())
1279 return false;
1280
Eric Christopher119ff7f2010-12-01 01:40:24 +00001281 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001282 MVT VT;
Eric Christopher74487fc2010-09-02 00:53:56 +00001283 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001284 return false;
Eric Christopher74487fc2010-09-02 00:53:56 +00001285
Eric Christopher92db2012010-09-02 01:48:11 +00001286 // Get the value to be stored into a register.
1287 SrcReg = getRegForValue(Op0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001288 if (SrcReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001289
Eric Christopher119ff7f2010-12-01 01:40:24 +00001290 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001291 Address Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +00001292 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher74487fc2010-09-02 00:53:56 +00001293 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001294
Chad Rosierec3b77e2011-12-03 02:21:57 +00001295 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1296 return false;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001297 return true;
1298}
1299
1300static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1301 switch (Pred) {
1302 // Needs two compares...
1303 case CmpInst::FCMP_ONE:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001304 case CmpInst::FCMP_UEQ:
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001305 default:
Eric Christopherb2abb502010-11-02 01:24:49 +00001306 // AL is our "false" for now. The other two need more compares.
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001307 return ARMCC::AL;
1308 case CmpInst::ICMP_EQ:
1309 case CmpInst::FCMP_OEQ:
1310 return ARMCC::EQ;
1311 case CmpInst::ICMP_SGT:
1312 case CmpInst::FCMP_OGT:
1313 return ARMCC::GT;
1314 case CmpInst::ICMP_SGE:
1315 case CmpInst::FCMP_OGE:
1316 return ARMCC::GE;
1317 case CmpInst::ICMP_UGT:
1318 case CmpInst::FCMP_UGT:
1319 return ARMCC::HI;
1320 case CmpInst::FCMP_OLT:
1321 return ARMCC::MI;
1322 case CmpInst::ICMP_ULE:
1323 case CmpInst::FCMP_OLE:
1324 return ARMCC::LS;
1325 case CmpInst::FCMP_ORD:
1326 return ARMCC::VC;
1327 case CmpInst::FCMP_UNO:
1328 return ARMCC::VS;
1329 case CmpInst::FCMP_UGE:
1330 return ARMCC::PL;
1331 case CmpInst::ICMP_SLT:
1332 case CmpInst::FCMP_ULT:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001333 return ARMCC::LT;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001334 case CmpInst::ICMP_SLE:
1335 case CmpInst::FCMP_ULE:
1336 return ARMCC::LE;
1337 case CmpInst::FCMP_UNE:
1338 case CmpInst::ICMP_NE:
1339 return ARMCC::NE;
1340 case CmpInst::ICMP_UGE:
1341 return ARMCC::HS;
1342 case CmpInst::ICMP_ULT:
1343 return ARMCC::LO;
1344 }
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001345}
1346
Eric Christopher29ab6d12010-09-27 06:02:23 +00001347bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christopher6aaed722010-09-03 00:35:47 +00001348 const BranchInst *BI = cast<BranchInst>(I);
1349 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1350 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopher2ff757d2010-09-09 01:06:51 +00001351
Eric Christopher6aaed722010-09-03 00:35:47 +00001352 // Simple branch support.
Jim Grosbach68147ee2010-11-09 19:22:26 +00001353
Eric Christopher5c308f82010-10-29 21:08:19 +00001354 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1355 // behavior.
Eric Christopher5c308f82010-10-29 21:08:19 +00001356 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001357 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher5c308f82010-10-29 21:08:19 +00001358
1359 // Get the compare predicate.
Eric Christopher26b8ac42011-04-29 21:56:31 +00001360 // Try to take advantage of fallthrough opportunities.
1361 CmpInst::Predicate Predicate = CI->getPredicate();
1362 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1363 std::swap(TBB, FBB);
1364 Predicate = CmpInst::getInversePredicate(Predicate);
1365 }
1366
1367 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher5c308f82010-10-29 21:08:19 +00001368
1369 // We may not handle every CC for now.
1370 if (ARMPred == ARMCC::AL) return false;
1371
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001372 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001373 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001374 return false;
Jim Grosbach68147ee2010-11-09 19:22:26 +00001375
Chad Rosier0439cfc2011-11-08 21:12:00 +00001376 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001377 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher5c308f82010-10-29 21:08:19 +00001378 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001379 FastEmitBranch(FBB, DbgLoc);
Eric Christopher5c308f82010-10-29 21:08:19 +00001380 FuncInfo.MBB->addSuccessor(TBB);
1381 return true;
1382 }
Eric Christopher8d46b472011-04-29 20:02:39 +00001383 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1384 MVT SourceVT;
1385 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedmanc7035512011-05-25 23:49:02 +00001386 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier0439cfc2011-11-08 21:12:00 +00001387 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopher8d46b472011-04-29 20:02:39 +00001388 unsigned OpReg = getRegForValue(TI->getOperand(0));
Jim Grosbach667b1472013-08-26 20:22:05 +00001389 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001390 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8d46b472011-04-29 20:02:39 +00001391 TII.get(TstOpc))
1392 .addReg(OpReg).addImm(1));
1393
1394 unsigned CCMode = ARMCC::NE;
1395 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1396 std::swap(TBB, FBB);
1397 CCMode = ARMCC::EQ;
1398 }
1399
Chad Rosier0439cfc2011-11-08 21:12:00 +00001400 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001401 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher8d46b472011-04-29 20:02:39 +00001402 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1403
Rafael Espindolaea09c592014-02-18 22:05:46 +00001404 FastEmitBranch(FBB, DbgLoc);
Eric Christopher8d46b472011-04-29 20:02:39 +00001405 FuncInfo.MBB->addSuccessor(TBB);
1406 return true;
1407 }
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001408 } else if (const ConstantInt *CI =
1409 dyn_cast<ConstantInt>(BI->getCondition())) {
1410 uint64_t Imm = CI->getZExtValue();
1411 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001412 FastEmitBranch(Target, DbgLoc);
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001413 return true;
Eric Christopher5c308f82010-10-29 21:08:19 +00001414 }
Jim Grosbach68147ee2010-11-09 19:22:26 +00001415
Eric Christopher5c308f82010-10-29 21:08:19 +00001416 unsigned CmpReg = getRegForValue(BI->getCondition());
1417 if (CmpReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001418
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001419 // We've been divorced from our compare! Our block was split, and
1420 // now our compare lives in a predecessor block. We musn't
1421 // re-compare here, as the children of the compare aren't guaranteed
1422 // live across the block boundary (we *could* check for this).
1423 // Regardless, the compare has been done in the predecessor block,
1424 // and it left a value for us in a virtual register. Ergo, we test
1425 // the one-bit value left in the virtual register.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001426 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Jim Grosbach667b1472013-08-26 20:22:05 +00001427 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001428 AddOptionalDefs(
1429 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
1430 .addReg(CmpReg)
1431 .addImm(1));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001432
Eric Christopher4f012fd2011-04-28 16:52:09 +00001433 unsigned CCMode = ARMCC::NE;
1434 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1435 std::swap(TBB, FBB);
1436 CCMode = ARMCC::EQ;
1437 }
1438
Chad Rosier0439cfc2011-11-08 21:12:00 +00001439 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001440 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher4f012fd2011-04-28 16:52:09 +00001441 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001442 FastEmitBranch(FBB, DbgLoc);
Eric Christopher6aaed722010-09-03 00:35:47 +00001443 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopher7ac602b2010-10-11 08:38:55 +00001444 return true;
Eric Christopher6aaed722010-09-03 00:35:47 +00001445}
1446
Chad Rosierded4c992012-02-07 23:56:08 +00001447bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1448 unsigned AddrReg = getRegForValue(I->getOperand(0));
1449 if (AddrReg == 0) return false;
1450
1451 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001452 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1453 TII.get(Opc)).addReg(AddrReg));
Bill Wendling12cda502012-10-22 23:30:04 +00001454
1455 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1456 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1457 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1458
Jush Luac96b762012-06-14 06:08:19 +00001459 return true;
Chad Rosierded4c992012-02-07 23:56:08 +00001460}
1461
Chad Rosier9cf803c2011-11-02 18:08:25 +00001462bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1463 bool isZExt) {
Chad Rosier78127d32011-10-26 23:25:44 +00001464 Type *Ty = Src1Value->getType();
Patrik Hagglundc494d242012-12-17 14:30:06 +00001465 EVT SrcEVT = TLI.getValueType(Ty, true);
1466 if (!SrcEVT.isSimple()) return false;
1467 MVT SrcVT = SrcEVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001468
Chad Rosier78127d32011-10-26 23:25:44 +00001469 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1470 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherc3e9c402010-09-08 23:13:45 +00001471 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001472
Chad Rosier595d4192011-11-09 03:22:02 +00001473 // Check to see if the 2nd operand is a constant that we can encode directly
1474 // in the compare.
Chad Rosiere19b0a92011-11-11 06:27:41 +00001475 int Imm = 0;
1476 bool UseImm = false;
Chad Rosier595d4192011-11-09 03:22:02 +00001477 bool isNegativeImm = false;
Chad Rosieraf13d762011-11-16 00:32:20 +00001478 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1479 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier595d4192011-11-09 03:22:02 +00001480 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1481 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1482 SrcVT == MVT::i1) {
1483 const APInt &CIVal = ConstInt->getValue();
Chad Rosiere19b0a92011-11-11 06:27:41 +00001484 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
Chad Rosier26d05882012-03-15 22:54:20 +00001485 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1486 // then a cmn, because there is no way to represent 2147483648 as a
1487 // signed 32-bit int.
1488 if (Imm < 0 && Imm != (int)0x80000000) {
1489 isNegativeImm = true;
1490 Imm = -Imm;
Chad Rosier3fbd0942011-11-10 01:30:39 +00001491 }
Chad Rosier26d05882012-03-15 22:54:20 +00001492 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1493 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier595d4192011-11-09 03:22:02 +00001494 }
1495 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1496 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1497 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosiere19b0a92011-11-11 06:27:41 +00001498 UseImm = true;
Chad Rosier595d4192011-11-09 03:22:02 +00001499 }
1500
Eric Christopherc3e9c402010-09-08 23:13:45 +00001501 unsigned CmpOpc;
Chad Rosier595d4192011-11-09 03:22:02 +00001502 bool isICmp = true;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001503 bool needsExt = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001504 switch (SrcVT.SimpleTy) {
Eric Christopherc3e9c402010-09-08 23:13:45 +00001505 default: return false;
1506 // TODO: Verify compares.
1507 case MVT::f32:
Chad Rosier595d4192011-11-09 03:22:02 +00001508 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001509 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001510 break;
1511 case MVT::f64:
Chad Rosier595d4192011-11-09 03:22:02 +00001512 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001513 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001514 break;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001515 case MVT::i1:
1516 case MVT::i8:
1517 case MVT::i16:
1518 needsExt = true;
1519 // Intentional fall-through.
Eric Christopherc3e9c402010-09-08 23:13:45 +00001520 case MVT::i32:
Chad Rosier595d4192011-11-09 03:22:02 +00001521 if (isThumb2) {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001522 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001523 CmpOpc = ARM::t2CMPrr;
1524 else
Bill Wendling4b796472012-06-11 08:07:26 +00001525 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001526 } else {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001527 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001528 CmpOpc = ARM::CMPrr;
1529 else
Bill Wendling4b796472012-06-11 08:07:26 +00001530 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001531 }
Eric Christopherc3e9c402010-09-08 23:13:45 +00001532 break;
1533 }
1534
Chad Rosier9cf803c2011-11-02 18:08:25 +00001535 unsigned SrcReg1 = getRegForValue(Src1Value);
1536 if (SrcReg1 == 0) return false;
Chad Rosier59a20192011-10-26 22:47:55 +00001537
Duncan Sands12330652011-11-28 10:31:27 +00001538 unsigned SrcReg2 = 0;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001539 if (!UseImm) {
Chad Rosier595d4192011-11-09 03:22:02 +00001540 SrcReg2 = getRegForValue(Src2Value);
1541 if (SrcReg2 == 0) return false;
1542 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001543
1544 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1545 if (needsExt) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001546 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1547 if (SrcReg1 == 0) return false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001548 if (!UseImm) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001549 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1550 if (SrcReg2 == 0) return false;
Chad Rosier595d4192011-11-09 03:22:02 +00001551 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001552 }
Chad Rosier59a20192011-10-26 22:47:55 +00001553
Jim Grosbachd7866792013-08-16 23:37:40 +00001554 const MCInstrDesc &II = TII.get(CmpOpc);
1555 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
Chad Rosiere19b0a92011-11-11 06:27:41 +00001556 if (!UseImm) {
Jim Grosbachd7866792013-08-16 23:37:40 +00001557 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001558 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001559 .addReg(SrcReg1).addReg(SrcReg2));
1560 } else {
1561 MachineInstrBuilder MIB;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001562 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001563 .addReg(SrcReg1);
1564
1565 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1566 if (isICmp)
Chad Rosiere19b0a92011-11-11 06:27:41 +00001567 MIB.addImm(Imm);
Chad Rosier595d4192011-11-09 03:22:02 +00001568 AddOptionalDefs(MIB);
1569 }
Chad Rosier78127d32011-10-26 23:25:44 +00001570
1571 // For floating point we need to move the result to a comparison register
1572 // that we can then use for branches.
1573 if (Ty->isFloatTy() || Ty->isDoubleTy())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001574 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier78127d32011-10-26 23:25:44 +00001575 TII.get(ARM::FMSTAT)));
Chad Rosier59a20192011-10-26 22:47:55 +00001576 return true;
1577}
1578
1579bool ARMFastISel::SelectCmp(const Instruction *I) {
1580 const CmpInst *CI = cast<CmpInst>(I);
1581
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001582 // Get the compare predicate.
1583 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopher7ac602b2010-10-11 08:38:55 +00001584
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001585 // We may not handle every CC for now.
1586 if (ARMPred == ARMCC::AL) return false;
1587
Chad Rosier59a20192011-10-26 22:47:55 +00001588 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001589 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier59a20192011-10-26 22:47:55 +00001590 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001591
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001592 // Now set a register based on the comparison. Explicitly set the predicates
1593 // here.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001594 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Craig Topperc7242e02012-04-20 07:30:17 +00001595 const TargetRegisterClass *RC = isThumb2 ?
1596 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1597 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher76a97522010-10-07 05:39:19 +00001598 unsigned DestReg = createResultReg(RC);
Chad Rosier78127d32011-10-26 23:25:44 +00001599 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001600 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosier377f1f22012-03-07 20:59:26 +00001601 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001602 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg)
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001603 .addReg(ZeroReg).addImm(1)
Chad Rosier377f1f22012-03-07 20:59:26 +00001604 .addImm(ARMPred).addReg(ARM::CPSR);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001605
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001606 UpdateValueMap(I, DestReg);
Eric Christopherc3e9c402010-09-08 23:13:45 +00001607 return true;
1608}
1609
Eric Christopher29ab6d12010-09-27 06:02:23 +00001610bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001611 // Make sure we have VFP and that we're extending float to double.
1612 if (!Subtarget->hasVFP2()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001613
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001614 Value *V = I->getOperand(0);
1615 if (!I->getType()->isDoubleTy() ||
1616 !V->getType()->isFloatTy()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001617
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001618 unsigned Op = getRegForValue(V);
1619 if (Op == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001620
Craig Topperc7242e02012-04-20 07:30:17 +00001621 unsigned Result = createResultReg(&ARM::DPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001622 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001623 TII.get(ARM::VCVTDS), Result)
Eric Christopher5903c0b2010-09-09 20:26:31 +00001624 .addReg(Op));
1625 UpdateValueMap(I, Result);
1626 return true;
1627}
1628
Eric Christopher29ab6d12010-09-27 06:02:23 +00001629bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopher5903c0b2010-09-09 20:26:31 +00001630 // Make sure we have VFP and that we're truncating double to float.
1631 if (!Subtarget->hasVFP2()) return false;
1632
1633 Value *V = I->getOperand(0);
Eric Christopher8cfc4592010-10-05 23:13:24 +00001634 if (!(I->getType()->isFloatTy() &&
1635 V->getType()->isDoubleTy())) return false;
Eric Christopher5903c0b2010-09-09 20:26:31 +00001636
1637 unsigned Op = getRegForValue(V);
1638 if (Op == 0) return false;
1639
Craig Topperc7242e02012-04-20 07:30:17 +00001640 unsigned Result = createResultReg(&ARM::SPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001641 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001642 TII.get(ARM::VCVTSD), Result)
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001643 .addReg(Op));
1644 UpdateValueMap(I, Result);
1645 return true;
1646}
1647
Chad Rosiere023d5d2012-02-03 21:14:11 +00001648bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001649 // Make sure we have VFP.
1650 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001651
Duncan Sandsf5dda012010-11-03 11:35:31 +00001652 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001653 Type *Ty = I->getType();
Eric Christopher4bd70472010-09-09 21:44:45 +00001654 if (!isTypeLegal(Ty, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001655 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001656
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001657 Value *Src = I->getOperand(0);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001658 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
1659 if (!SrcEVT.isSimple())
1660 return false;
1661 MVT SrcVT = SrcEVT.getSimpleVT();
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001662 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman5bbb7562011-05-25 19:09:45 +00001663 return false;
1664
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001665 unsigned SrcReg = getRegForValue(Src);
1666 if (SrcReg == 0) return false;
1667
1668 // Handle sign-extension.
1669 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001670 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
Chad Rosiere023d5d2012-02-03 21:14:11 +00001671 /*isZExt*/!isSigned);
Chad Rosiera0d3c752012-02-16 22:45:33 +00001672 if (SrcReg == 0) return false;
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001673 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00001674
Eric Christopher860fc932010-09-10 00:34:35 +00001675 // The conversion routine works on fp-reg to fp-reg and the operand above
1676 // was an integer, move it to the fp registers if possible.
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001677 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001678 if (FP == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001679
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001680 unsigned Opc;
Chad Rosiere023d5d2012-02-03 21:14:11 +00001681 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1682 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001683 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001684
Eric Christopher4bd70472010-09-09 21:44:45 +00001685 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001686 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1687 TII.get(Opc), ResultReg).addReg(FP));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001688 UpdateValueMap(I, ResultReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001689 return true;
1690}
1691
Chad Rosiere023d5d2012-02-03 21:14:11 +00001692bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001693 // Make sure we have VFP.
1694 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001695
Duncan Sandsf5dda012010-11-03 11:35:31 +00001696 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001697 Type *RetTy = I->getType();
Eric Christopher712bd0a2010-09-10 00:35:09 +00001698 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001699 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001700
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001701 unsigned Op = getRegForValue(I->getOperand(0));
1702 if (Op == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001703
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001704 unsigned Opc;
Chris Lattner229907c2011-07-18 04:54:35 +00001705 Type *OpTy = I->getOperand(0)->getType();
Chad Rosiere023d5d2012-02-03 21:14:11 +00001706 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1707 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001708 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001709
Chad Rosier41f0e782012-02-03 20:27:51 +00001710 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
Eric Christopher8cfc4592010-10-05 23:13:24 +00001711 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001712 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1713 TII.get(Opc), ResultReg).addReg(Op));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001714
Eric Christopher4bd70472010-09-09 21:44:45 +00001715 // This result needs to be in an integer register, but the conversion only
1716 // takes place in fp-regs.
Eric Christopher860fc932010-09-10 00:34:35 +00001717 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001718 if (IntReg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001719
Eric Christopher4bd70472010-09-09 21:44:45 +00001720 UpdateValueMap(I, IntReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001721 return true;
1722}
1723
Eric Christopher511aa312010-10-11 08:27:59 +00001724bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001725 MVT VT;
1726 if (!isTypeLegal(I->getType(), VT))
Eric Christopher511aa312010-10-11 08:27:59 +00001727 return false;
1728
1729 // Things need to be register sized for register moves.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001730 if (VT != MVT::i32) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001731
1732 unsigned CondReg = getRegForValue(I->getOperand(0));
1733 if (CondReg == 0) return false;
1734 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1735 if (Op1Reg == 0) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001736
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001737 // Check to see if we can use an immediate in the conditional move.
1738 int Imm = 0;
1739 bool UseImm = false;
1740 bool isNegativeImm = false;
1741 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1742 assert (VT == MVT::i32 && "Expecting an i32.");
1743 Imm = (int)ConstInt->getValue().getZExtValue();
1744 if (Imm < 0) {
1745 isNegativeImm = true;
1746 Imm = ~Imm;
1747 }
1748 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1749 (ARM_AM::getSOImmVal(Imm) != -1);
1750 }
1751
Duncan Sands12330652011-11-28 10:31:27 +00001752 unsigned Op2Reg = 0;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001753 if (!UseImm) {
1754 Op2Reg = getRegForValue(I->getOperand(2));
1755 if (Op2Reg == 0) return false;
1756 }
1757
1758 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Jim Grosbachd7866792013-08-16 23:37:40 +00001759 CondReg = constrainOperandRegClass(TII.get(CmpOpc), CondReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001760 AddOptionalDefs(
1761 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1762 .addReg(CondReg)
1763 .addImm(0));
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001764
1765 unsigned MovCCOpc;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001766 const TargetRegisterClass *RC;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001767 if (!UseImm) {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001768 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001769 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1770 } else {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001771 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1772 if (!isNegativeImm)
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001773 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001774 else
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001775 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001776 }
Eric Christopher511aa312010-10-11 08:27:59 +00001777 unsigned ResultReg = createResultReg(RC);
Jim Grosbachd7866792013-08-16 23:37:40 +00001778 if (!UseImm) {
Jim Grosbach71a78f92013-08-20 19:12:42 +00001779 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
Jim Grosbachd7866792013-08-16 23:37:40 +00001780 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001781 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1782 ResultReg)
1783 .addReg(Op2Reg)
1784 .addReg(Op1Reg)
1785 .addImm(ARMCC::NE)
1786 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001787 } else {
1788 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001789 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1790 ResultReg)
1791 .addReg(Op1Reg)
1792 .addImm(Imm)
1793 .addImm(ARMCC::EQ)
1794 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001795 }
Eric Christopher511aa312010-10-11 08:27:59 +00001796 UpdateValueMap(I, ResultReg);
1797 return true;
1798}
1799
Chad Rosieraaa55a82012-02-03 21:07:27 +00001800bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001801 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001802 Type *Ty = I->getType();
Eric Christopher56094ff2010-09-30 22:34:19 +00001803 if (!isTypeLegal(Ty, VT))
1804 return false;
1805
1806 // If we have integer div support we should have selected this automagically.
1807 // In case we have a real miss go ahead and return false and we'll pick
1808 // it up later.
Eric Christopher7ac602b2010-10-11 08:38:55 +00001809 if (Subtarget->hasDivide()) return false;
1810
Eric Christopher56094ff2010-09-30 22:34:19 +00001811 // Otherwise emit a libcall.
1812 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christophere11017c2010-10-11 08:31:54 +00001813 if (VT == MVT::i8)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001814 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
Eric Christophere11017c2010-10-11 08:31:54 +00001815 else if (VT == MVT::i16)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001816 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
Eric Christopher56094ff2010-09-30 22:34:19 +00001817 else if (VT == MVT::i32)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001818 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
Eric Christopher56094ff2010-09-30 22:34:19 +00001819 else if (VT == MVT::i64)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001820 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
Eric Christopher56094ff2010-09-30 22:34:19 +00001821 else if (VT == MVT::i128)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001822 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
Eric Christopher56094ff2010-09-30 22:34:19 +00001823 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ac602b2010-10-11 08:38:55 +00001824
Eric Christopher56094ff2010-09-30 22:34:19 +00001825 return ARMEmitLibcall(I, LC);
1826}
1827
Chad Rosierb84a4b42012-02-03 21:23:45 +00001828bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001829 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001830 Type *Ty = I->getType();
Eric Christophereae1b382010-10-11 08:37:26 +00001831 if (!isTypeLegal(Ty, VT))
1832 return false;
1833
1834 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1835 if (VT == MVT::i8)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001836 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
Eric Christophereae1b382010-10-11 08:37:26 +00001837 else if (VT == MVT::i16)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001838 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
Eric Christophereae1b382010-10-11 08:37:26 +00001839 else if (VT == MVT::i32)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001840 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
Eric Christophereae1b382010-10-11 08:37:26 +00001841 else if (VT == MVT::i64)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001842 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
Eric Christophereae1b382010-10-11 08:37:26 +00001843 else if (VT == MVT::i128)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001844 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
Eric Christophere1bcb432010-10-11 08:40:05 +00001845 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001846
Eric Christophereae1b382010-10-11 08:37:26 +00001847 return ARMEmitLibcall(I, LC);
1848}
1849
Chad Rosier685b20c2012-02-06 23:50:07 +00001850bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier685b20c2012-02-06 23:50:07 +00001851 EVT DestVT = TLI.getValueType(I->getType(), true);
1852
1853 // We can get here in the case when we have a binary operation on a non-legal
1854 // type and the target independent selector doesn't know how to handle it.
1855 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1856 return false;
Jush Luac96b762012-06-14 06:08:19 +00001857
Chad Rosierbd471252012-02-08 02:29:21 +00001858 unsigned Opc;
1859 switch (ISDOpcode) {
1860 default: return false;
1861 case ISD::ADD:
1862 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1863 break;
1864 case ISD::OR:
1865 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1866 break;
Chad Rosier0ee8c512012-02-08 02:45:44 +00001867 case ISD::SUB:
1868 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1869 break;
Chad Rosierbd471252012-02-08 02:29:21 +00001870 }
1871
Chad Rosier685b20c2012-02-06 23:50:07 +00001872 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1873 if (SrcReg1 == 0) return false;
1874
1875 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1876 // in the instruction, rather then materializing the value in a register.
1877 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1878 if (SrcReg2 == 0) return false;
1879
JF Bastien13969d02013-05-29 15:45:47 +00001880 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001881 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1882 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001883 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier685b20c2012-02-06 23:50:07 +00001884 TII.get(Opc), ResultReg)
1885 .addReg(SrcReg1).addReg(SrcReg2));
1886 UpdateValueMap(I, ResultReg);
1887 return true;
1888}
1889
1890bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001891 EVT FPVT = TLI.getValueType(I->getType(), true);
1892 if (!FPVT.isSimple()) return false;
1893 MVT VT = FPVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001894
Eric Christopher24dc27f2010-09-09 00:53:57 +00001895 // We can get here in the case when we want to use NEON for our fp
1896 // operations, but can't figure out how to. Just use the vfp instructions
1897 // if we have them.
1898 // FIXME: It'd be nice to use NEON instructions.
Chris Lattner229907c2011-07-18 04:54:35 +00001899 Type *Ty = I->getType();
Eric Christopherbd3d1212010-09-09 01:02:03 +00001900 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1901 if (isFloat && !Subtarget->hasVFP2())
1902 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001903
Eric Christopher24dc27f2010-09-09 00:53:57 +00001904 unsigned Opc;
Duncan Sands14627772010-11-03 12:17:33 +00001905 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001906 switch (ISDOpcode) {
1907 default: return false;
1908 case ISD::FADD:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001909 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001910 break;
1911 case ISD::FSUB:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001912 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001913 break;
1914 case ISD::FMUL:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001915 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001916 break;
1917 }
Chad Rosier80979b62011-11-16 18:39:44 +00001918 unsigned Op1 = getRegForValue(I->getOperand(0));
1919 if (Op1 == 0) return false;
1920
1921 unsigned Op2 = getRegForValue(I->getOperand(1));
1922 if (Op2 == 0) return false;
1923
Chad Rosier62a144f2012-12-17 19:59:43 +00001924 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001925 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher24dc27f2010-09-09 00:53:57 +00001926 TII.get(Opc), ResultReg)
1927 .addReg(Op1).addReg(Op2));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001928 UpdateValueMap(I, ResultReg);
Eric Christopher24dc27f2010-09-09 00:53:57 +00001929 return true;
1930}
1931
Eric Christopher72497e52010-09-10 23:18:12 +00001932// Call Handling Code
1933
Jush Lue67e07b2012-07-19 09:49:00 +00001934// This is largely taken directly from CCAssignFnForNode
Eric Christopher72497e52010-09-10 23:18:12 +00001935// TODO: We may not support all of this.
Jush Lue67e07b2012-07-19 09:49:00 +00001936CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1937 bool Return,
1938 bool isVarArg) {
Eric Christopher72497e52010-09-10 23:18:12 +00001939 switch (CC) {
1940 default:
1941 llvm_unreachable("Unsupported calling convention");
Eric Christopher72497e52010-09-10 23:18:12 +00001942 case CallingConv::Fast:
Jush Lu26088cb2012-08-16 05:15:53 +00001943 if (Subtarget->hasVFP2() && !isVarArg) {
1944 if (!Subtarget->isAAPCS_ABI())
1945 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1946 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1947 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1948 }
Evan Cheng21abfc92010-10-22 18:57:05 +00001949 // Fallthrough
1950 case CallingConv::C:
Eric Christopher72497e52010-09-10 23:18:12 +00001951 // Use target triple & subtarget features to do actual dispatch.
1952 if (Subtarget->isAAPCS_ABI()) {
1953 if (Subtarget->hasVFP2() &&
Jush Lue67e07b2012-07-19 09:49:00 +00001954 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
Eric Christopher72497e52010-09-10 23:18:12 +00001955 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1956 else
1957 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1958 } else
1959 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1960 case CallingConv::ARM_AAPCS_VFP:
Jush Lue67e07b2012-07-19 09:49:00 +00001961 if (!isVarArg)
1962 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1963 // Fall through to soft float variant, variadic functions don't
1964 // use hard floating point ABI.
Eric Christopher72497e52010-09-10 23:18:12 +00001965 case CallingConv::ARM_AAPCS:
1966 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1967 case CallingConv::ARM_APCS:
1968 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001969 case CallingConv::GHC:
1970 if (Return)
1971 llvm_unreachable("Can't return in GHC call convention");
1972 else
1973 return CC_ARM_APCS_GHC;
Eric Christopher72497e52010-09-10 23:18:12 +00001974 }
1975}
1976
Eric Christopher79398062010-09-29 23:11:09 +00001977bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1978 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001979 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +00001980 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1981 SmallVectorImpl<unsigned> &RegArgs,
1982 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00001983 unsigned &NumBytes,
1984 bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00001985 SmallVector<CCValAssign, 16> ArgLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00001986 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1987 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1988 CCAssignFnForCall(CC, false, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00001989
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001990 // Check that we can handle all of the arguments. If we can't, then bail out
1991 // now before we add code to the MBB.
1992 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1993 CCValAssign &VA = ArgLocs[i];
1994 MVT ArgVT = ArgVTs[VA.getValNo()];
1995
1996 // We don't handle NEON/vector parameters yet.
1997 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1998 return false;
1999
2000 // Now copy/store arg to correct locations.
2001 if (VA.isRegLoc() && !VA.needsCustom()) {
2002 continue;
2003 } else if (VA.needsCustom()) {
2004 // TODO: We need custom lowering for vector (v2f64) args.
2005 if (VA.getLocVT() != MVT::f64 ||
2006 // TODO: Only handle register args for now.
2007 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
2008 return false;
2009 } else {
Craig Topper56710102013-08-15 02:33:50 +00002010 switch (ArgVT.SimpleTy) {
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002011 default:
2012 return false;
2013 case MVT::i1:
2014 case MVT::i8:
2015 case MVT::i16:
2016 case MVT::i32:
2017 break;
2018 case MVT::f32:
2019 if (!Subtarget->hasVFP2())
2020 return false;
2021 break;
2022 case MVT::f64:
2023 if (!Subtarget->hasVFP2())
2024 return false;
2025 break;
2026 }
2027 }
2028 }
2029
2030 // At the point, we are able to handle the call's arguments in fast isel.
2031
Eric Christopher79398062010-09-29 23:11:09 +00002032 // Get a count of how many bytes are to be pushed on the stack.
2033 NumBytes = CCInfo.getNextStackOffset();
2034
2035 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002036 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002037 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00002038 TII.get(AdjStackDown))
2039 .addImm(NumBytes));
Eric Christopher79398062010-09-29 23:11:09 +00002040
2041 // Process the args.
2042 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2043 CCValAssign &VA = ArgLocs[i];
2044 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sandsf5dda012010-11-03 11:35:31 +00002045 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00002046
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002047 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
2048 "We don't handle NEON/vector parameters yet.");
Eric Christopherc9616f22010-10-23 09:37:17 +00002049
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002050 // Handle arg promotion, etc.
Eric Christopher79398062010-09-29 23:11:09 +00002051 switch (VA.getLocInfo()) {
2052 case CCValAssign::Full: break;
Eric Christopherc103c662010-10-18 02:17:53 +00002053 case CCValAssign::SExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00002054 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00002055 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
2056 assert (Arg != 0 && "Failed to emit a sext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00002057 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00002058 break;
2059 }
Chad Rosierd0191a52011-11-05 20:16:15 +00002060 case CCValAssign::AExt:
2061 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherc103c662010-10-18 02:17:53 +00002062 case CCValAssign::ZExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00002063 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00002064 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
JF Bastien06ce03d2013-06-07 20:10:37 +00002065 assert (Arg != 0 && "Failed to emit a zext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00002066 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00002067 break;
2068 }
2069 case CCValAssign::BCvt: {
Wesley Peck527da1b2010-11-23 03:31:01 +00002070 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sandsf5dda012010-11-03 11:35:31 +00002071 /*TODO: Kill=*/false);
Eric Christopherc103c662010-10-18 02:17:53 +00002072 assert(BC != 0 && "Failed to emit a bitcast!");
2073 Arg = BC;
2074 ArgVT = VA.getLocVT();
2075 break;
2076 }
2077 default: llvm_unreachable("Unknown arg promotion!");
Eric Christopher79398062010-09-29 23:11:09 +00002078 }
2079
2080 // Now copy/store arg to correct locations.
Eric Christopher71ef1af2010-10-11 21:20:02 +00002081 if (VA.isRegLoc() && !VA.needsCustom()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002082 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2083 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Eric Christopher79398062010-09-29 23:11:09 +00002084 RegArgs.push_back(VA.getLocReg());
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002085 } else if (VA.needsCustom()) {
2086 // TODO: We need custom lowering for vector (v2f64) args.
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002087 assert(VA.getLocVT() == MVT::f64 &&
2088 "Custom lowering for v2f64 args not available");
Jim Grosbach055de2c2010-10-27 21:39:08 +00002089
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002090 CCValAssign &NextVA = ArgLocs[++i];
2091
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002092 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
2093 "We only handle register args!");
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002094
Rafael Espindolaea09c592014-02-18 22:05:46 +00002095 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002096 TII.get(ARM::VMOVRRD), VA.getLocReg())
2097 .addReg(NextVA.getLocReg(), RegState::Define)
2098 .addReg(Arg));
2099 RegArgs.push_back(VA.getLocReg());
2100 RegArgs.push_back(NextVA.getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002101 } else {
Eric Christopherb353e4f2010-10-21 20:09:54 +00002102 assert(VA.isMemLoc());
2103 // Need to store on the stack.
Eric Christopherfef5f312010-11-19 22:30:02 +00002104 Address Addr;
2105 Addr.BaseType = Address::RegBase;
2106 Addr.Base.Reg = ARM::SP;
2107 Addr.Offset = VA.getLocMemOffset();
Eric Christopherb353e4f2010-10-21 20:09:54 +00002108
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002109 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2110 assert(EmitRet && "Could not emit a store for argument!");
Eric Christopher79398062010-09-29 23:11:09 +00002111 }
2112 }
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002113
Eric Christopher79398062010-09-29 23:11:09 +00002114 return true;
2115}
2116
Duncan Sandsf5dda012010-11-03 11:35:31 +00002117bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +00002118 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00002119 unsigned &NumBytes, bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00002120 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002121 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002122 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00002123 TII.get(AdjStackUp))
2124 .addImm(NumBytes).addImm(0));
Eric Christopher79398062010-09-29 23:11:09 +00002125
2126 // Now the return value.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002127 if (RetVT != MVT::isVoid) {
Eric Christopher79398062010-09-29 23:11:09 +00002128 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002129 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2130 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00002131
2132 // Copy all of the result registers out of their specified physreg.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002133 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopherc1e209d2010-10-01 00:00:11 +00002134 // For this move we copy into two registers and then move into the
2135 // double fp reg we want.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002136 MVT DestVT = RVLocs[0].getValVT();
Craig Topper760b1342012-02-22 05:59:10 +00002137 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002138 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002139 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopherc1e209d2010-10-01 00:00:11 +00002140 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopheraf719ef2010-10-20 08:02:24 +00002141 .addReg(RVLocs[0].getLocReg())
2142 .addReg(RVLocs[1].getLocReg()));
Eric Christopher7ac602b2010-10-11 08:38:55 +00002143
Eric Christopheraf719ef2010-10-20 08:02:24 +00002144 UsedRegs.push_back(RVLocs[0].getLocReg());
2145 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach055de2c2010-10-27 21:39:08 +00002146
Eric Christopher7ac602b2010-10-11 08:38:55 +00002147 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002148 UpdateValueMap(I, ResultReg);
Chad Rosier90f9afe2012-05-11 18:51:55 +00002149 } else {
2150 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002151 MVT CopyVT = RVLocs[0].getValVT();
Chad Rosier5de1bea2011-11-08 00:03:32 +00002152
2153 // Special handling for extended integers.
2154 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2155 CopyVT = MVT::i32;
2156
Craig Topper760b1342012-02-22 05:59:10 +00002157 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christopher79398062010-09-29 23:11:09 +00002158
Eric Christopherc1e209d2010-10-01 00:00:11 +00002159 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002160 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2161 TII.get(TargetOpcode::COPY),
Eric Christopherc1e209d2010-10-01 00:00:11 +00002162 ResultReg).addReg(RVLocs[0].getLocReg());
2163 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002164
Eric Christopher7ac602b2010-10-11 08:38:55 +00002165 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002166 UpdateValueMap(I, ResultReg);
2167 }
Eric Christopher79398062010-09-29 23:11:09 +00002168 }
2169
Eric Christopher7ac602b2010-10-11 08:38:55 +00002170 return true;
Eric Christopher79398062010-09-29 23:11:09 +00002171}
2172
Eric Christopher93bbe652010-10-22 01:28:00 +00002173bool ARMFastISel::SelectRet(const Instruction *I) {
2174 const ReturnInst *Ret = cast<ReturnInst>(I);
2175 const Function &F = *I->getParent()->getParent();
Jim Grosbach055de2c2010-10-27 21:39:08 +00002176
Eric Christopher93bbe652010-10-22 01:28:00 +00002177 if (!FuncInfo.CanLowerReturn)
2178 return false;
Jim Grosbach055de2c2010-10-27 21:39:08 +00002179
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002180 // Build a list of return value registers.
2181 SmallVector<unsigned, 4> RetRegs;
2182
Eric Christopher93bbe652010-10-22 01:28:00 +00002183 CallingConv::ID CC = F.getCallingConv();
2184 if (Ret->getNumOperands() > 0) {
2185 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002186 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Eric Christopher93bbe652010-10-22 01:28:00 +00002187
2188 // Analyze operands of the call, assigning locations to each operand.
2189 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbache7e2aca2011-09-13 20:30:37 +00002190 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Jush Lue67e07b2012-07-19 09:49:00 +00002191 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2192 F.isVarArg()));
Eric Christopher93bbe652010-10-22 01:28:00 +00002193
2194 const Value *RV = Ret->getOperand(0);
2195 unsigned Reg = getRegForValue(RV);
2196 if (Reg == 0)
2197 return false;
2198
2199 // Only handle a single return value for now.
2200 if (ValLocs.size() != 1)
2201 return false;
2202
2203 CCValAssign &VA = ValLocs[0];
Jim Grosbach055de2c2010-10-27 21:39:08 +00002204
Eric Christopher93bbe652010-10-22 01:28:00 +00002205 // Don't bother handling odd stuff for now.
2206 if (VA.getLocInfo() != CCValAssign::Full)
2207 return false;
2208 // Only handle register returns for now.
2209 if (!VA.isRegLoc())
2210 return false;
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002211
2212 unsigned SrcReg = Reg + VA.getValNo();
Chad Rosier62a144f2012-12-17 19:59:43 +00002213 EVT RVEVT = TLI.getValueType(RV->getType());
2214 if (!RVEVT.isSimple()) return false;
2215 MVT RVVT = RVEVT.getSimpleVT();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002216 MVT DestVT = VA.getValVT();
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002217 // Special handling for extended integers.
2218 if (RVVT != DestVT) {
2219 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2220 return false;
2221
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002222 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2223
Chad Rosierfcd29ae2012-02-17 01:21:28 +00002224 // Perform extension if flagged as either zext or sext. Otherwise, do
2225 // nothing.
2226 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2227 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2228 if (SrcReg == 0) return false;
2229 }
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002230 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002231
Eric Christopher93bbe652010-10-22 01:28:00 +00002232 // Make the copy.
Eric Christopher93bbe652010-10-22 01:28:00 +00002233 unsigned DstReg = VA.getLocReg();
2234 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2235 // Avoid a cross-class copy. This is very unlikely.
2236 if (!SrcRC->contains(DstReg))
2237 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2239 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
Eric Christopher93bbe652010-10-22 01:28:00 +00002240
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002241 // Add register to return instruction.
2242 RetRegs.push_back(VA.getLocReg());
Eric Christopher93bbe652010-10-22 01:28:00 +00002243 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002244
Chad Rosier0439cfc2011-11-08 21:12:00 +00002245 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002246 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002247 TII.get(RetOpc));
2248 AddOptionalDefs(MIB);
2249 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2250 MIB.addReg(RetRegs[i], RegState::Implicit);
Eric Christopher93bbe652010-10-22 01:28:00 +00002251 return true;
2252}
2253
Chad Rosierc6916f82012-06-12 19:25:13 +00002254unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2255 if (UseReg)
2256 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2257 else
2258 return isThumb2 ? ARM::tBL : ARM::BL;
2259}
2260
2261unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
Chandler Carruth1c82d332013-07-27 11:23:08 +00002262 // Manually compute the global's type to avoid building it when unnecessary.
2263 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
2264 EVT LCREVT = TLI.getValueType(GVTy);
2265 if (!LCREVT.isSimple()) return 0;
2266
Bill Wendling76cce192013-12-29 08:00:04 +00002267 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
Chad Rosierc6916f82012-06-12 19:25:13 +00002268 GlobalValue::ExternalLinkage, 0, Name);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002269 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
Chad Rosier62a144f2012-12-17 19:59:43 +00002270 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
Eric Christopher919772f2011-02-22 01:37:10 +00002271}
2272
Eric Christopher8b912662010-09-14 23:03:37 +00002273// A quick function that will emit a call for a named libcall in F with the
2274// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopher7ac602b2010-10-11 08:38:55 +00002275// can emit a call for any libcall we can produce. This is an abridged version
2276// of the full call infrastructure since we won't need to worry about things
Eric Christopher8b912662010-09-14 23:03:37 +00002277// like computed function pointers or strange arguments at call sites.
2278// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2279// with X86.
Eric Christopher7990df12010-09-28 01:21:42 +00002280bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2281 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002282
Eric Christopher8b912662010-09-14 23:03:37 +00002283 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002284 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002285 MVT RetVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002286 if (RetTy->isVoidTy())
2287 RetVT = MVT::isVoid;
2288 else if (!isTypeLegal(RetTy, RetVT))
2289 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002290
Chad Rosier90f9afe2012-05-11 18:51:55 +00002291 // Can't handle non-double multi-reg retvals.
Jush Luac96b762012-06-14 06:08:19 +00002292 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
Chad Rosier90f9afe2012-05-11 18:51:55 +00002293 SmallVector<CCValAssign, 16> RVLocs;
2294 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002295 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002296 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2297 return false;
2298 }
2299
Eric Christopher79398062010-09-29 23:11:09 +00002300 // Set up the argument vectors.
Eric Christopher8b912662010-09-14 23:03:37 +00002301 SmallVector<Value*, 8> Args;
2302 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002303 SmallVector<MVT, 8> ArgVTs;
Eric Christopher8b912662010-09-14 23:03:37 +00002304 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2305 Args.reserve(I->getNumOperands());
2306 ArgRegs.reserve(I->getNumOperands());
2307 ArgVTs.reserve(I->getNumOperands());
2308 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7990df12010-09-28 01:21:42 +00002309 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopher8b912662010-09-14 23:03:37 +00002310 Value *Op = I->getOperand(i);
2311 unsigned Arg = getRegForValue(Op);
2312 if (Arg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002313
Chris Lattner229907c2011-07-18 04:54:35 +00002314 Type *ArgTy = Op->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002315 MVT ArgVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002316 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002317
Eric Christopher8b912662010-09-14 23:03:37 +00002318 ISD::ArgFlagsTy Flags;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002319 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher8b912662010-09-14 23:03:37 +00002320 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002321
Eric Christopher8b912662010-09-14 23:03:37 +00002322 Args.push_back(Op);
2323 ArgRegs.push_back(Arg);
2324 ArgVTs.push_back(ArgVT);
2325 ArgFlags.push_back(Flags);
2326 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002327
Eric Christopher79398062010-09-29 23:11:09 +00002328 // Handle the arguments now that we've gotten them.
Eric Christopher8b912662010-09-14 23:03:37 +00002329 SmallVector<unsigned, 4> RegArgs;
Eric Christopher79398062010-09-29 23:11:09 +00002330 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002331 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2332 RegArgs, CC, NumBytes, false))
Eric Christopher79398062010-09-29 23:11:09 +00002333 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002334
Chad Rosierc6916f82012-06-12 19:25:13 +00002335 unsigned CalleeReg = 0;
2336 if (EnableARMLongCalls) {
2337 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2338 if (CalleeReg == 0) return false;
2339 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002340
Chad Rosierc6916f82012-06-12 19:25:13 +00002341 // Issue the call.
2342 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2343 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002344 DbgLoc, TII.get(CallOpc));
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002345 // BL / BLX don't take a predicate, but tBL / tBLX do.
2346 if (isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002347 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002348 if (EnableARMLongCalls)
2349 MIB.addReg(CalleeReg);
2350 else
2351 MIB.addExternalSymbol(TLI.getLibcallName(Call));
Chad Rosierc6916f82012-06-12 19:25:13 +00002352
Eric Christopher8b912662010-09-14 23:03:37 +00002353 // Add implicit physical register uses to the call.
2354 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002355 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002356
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002357 // Add a register mask with the call-preserved registers.
2358 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2359 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2360
Eric Christopher79398062010-09-29 23:11:09 +00002361 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002362 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002363 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002364
Eric Christopher8b912662010-09-14 23:03:37 +00002365 // Set all unused physreg defs as dead.
2366 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002367
Eric Christopher8b912662010-09-14 23:03:37 +00002368 return true;
2369}
2370
Chad Rosiera7ebc562011-11-11 23:31:03 +00002371bool ARMFastISel::SelectCall(const Instruction *I,
2372 const char *IntrMemName = 0) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002373 const CallInst *CI = cast<CallInst>(I);
2374 const Value *Callee = CI->getCalledValue();
2375
Chad Rosiera7ebc562011-11-11 23:31:03 +00002376 // Can't handle inline asm.
2377 if (isa<InlineAsm>(Callee)) return false;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002378
Chad Rosierdf42cf32012-12-11 00:18:02 +00002379 // Allow SelectionDAG isel to handle tail calls.
2380 if (CI->isTailCall()) return false;
2381
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002382 // Check the calling convention.
2383 ImmutableCallSite CS(CI);
2384 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher167a70022010-10-18 06:49:12 +00002385
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002386 // TODO: Avoid some calling conventions?
Eric Christopher7ac602b2010-10-11 08:38:55 +00002387
Chris Lattner229907c2011-07-18 04:54:35 +00002388 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2389 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Jush Lue67e07b2012-07-19 09:49:00 +00002390 bool isVarArg = FTy->isVarArg();
Eric Christopher7ac602b2010-10-11 08:38:55 +00002391
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002392 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002393 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002394 MVT RetVT;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002395 if (RetTy->isVoidTy())
2396 RetVT = MVT::isVoid;
Chad Rosier5de1bea2011-11-08 00:03:32 +00002397 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2398 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002399 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002400
Chad Rosier90f9afe2012-05-11 18:51:55 +00002401 // Can't handle non-double multi-reg retvals.
2402 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2403 RetVT != MVT::i16 && RetVT != MVT::i32) {
2404 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002405 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2406 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002407 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2408 return false;
2409 }
2410
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002411 // Set up the argument vectors.
2412 SmallVector<Value*, 8> Args;
2413 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002414 SmallVector<MVT, 8> ArgVTs;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002415 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierdccc4792012-02-15 00:23:55 +00002416 unsigned arg_size = CS.arg_size();
2417 Args.reserve(arg_size);
2418 ArgRegs.reserve(arg_size);
2419 ArgVTs.reserve(arg_size);
2420 ArgFlags.reserve(arg_size);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002421 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2422 i != e; ++i) {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002423 // If we're lowering a memory intrinsic instead of a regular call, skip the
2424 // last two arguments, which shouldn't be passed to the underlying function.
2425 if (IntrMemName && e-i <= 2)
2426 break;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002427
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002428 ISD::ArgFlagsTy Flags;
2429 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002430 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002431 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002432 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002433 Flags.setZExt();
2434
Chad Rosier8a98ec42011-11-04 00:58:10 +00002435 // FIXME: Only handle *easy* calls for now.
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002436 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2437 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2438 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2439 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002440 return false;
2441
Chris Lattner229907c2011-07-18 04:54:35 +00002442 Type *ArgTy = (*i)->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002443 MVT ArgVT;
Chad Rosierd0191a52011-11-05 20:16:15 +00002444 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2445 ArgVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002446 return false;
Chad Rosieree93ff72011-11-18 01:17:34 +00002447
2448 unsigned Arg = getRegForValue(*i);
2449 if (Arg == 0)
2450 return false;
2451
Rafael Espindolaea09c592014-02-18 22:05:46 +00002452 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002453 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002454
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002455 Args.push_back(*i);
2456 ArgRegs.push_back(Arg);
2457 ArgVTs.push_back(ArgVT);
2458 ArgFlags.push_back(Flags);
2459 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002460
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002461 // Handle the arguments now that we've gotten them.
2462 SmallVector<unsigned, 4> RegArgs;
2463 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002464 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2465 RegArgs, CC, NumBytes, isVarArg))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002466 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002467
Chad Rosierc6916f82012-06-12 19:25:13 +00002468 bool UseReg = false;
Chad Rosier223faf72012-05-23 18:38:57 +00002469 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Chad Rosierc6916f82012-06-12 19:25:13 +00002470 if (!GV || EnableARMLongCalls) UseReg = true;
Chad Rosier223faf72012-05-23 18:38:57 +00002471
Chad Rosierc6916f82012-06-12 19:25:13 +00002472 unsigned CalleeReg = 0;
2473 if (UseReg) {
2474 if (IntrMemName)
2475 CalleeReg = getLibcallReg(IntrMemName);
2476 else
2477 CalleeReg = getRegForValue(Callee);
2478
Chad Rosier223faf72012-05-23 18:38:57 +00002479 if (CalleeReg == 0) return false;
2480 }
2481
Chad Rosierc6916f82012-06-12 19:25:13 +00002482 // Issue the call.
2483 unsigned CallOpc = ARMSelectCallOp(UseReg);
2484 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002485 DbgLoc, TII.get(CallOpc));
Chad Rosierc6916f82012-06-12 19:25:13 +00002486
Logan Chien2361f512013-08-22 12:08:04 +00002487 unsigned char OpFlags = 0;
2488
2489 // Add MO_PLT for global address or external symbol in the PIC relocation
2490 // model.
2491 if (Subtarget->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_)
2492 OpFlags = ARMII::MO_PLT;
2493
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002494 // ARM calls don't take a predicate, but tBL / tBLX do.
2495 if(isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002496 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002497 if (UseReg)
2498 MIB.addReg(CalleeReg);
2499 else if (!IntrMemName)
Logan Chien2361f512013-08-22 12:08:04 +00002500 MIB.addGlobalAddress(GV, 0, OpFlags);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002501 else
Logan Chien2361f512013-08-22 12:08:04 +00002502 MIB.addExternalSymbol(IntrMemName, OpFlags);
Jush Luac96b762012-06-14 06:08:19 +00002503
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002504 // Add implicit physical register uses to the call.
2505 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002506 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002507
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002508 // Add a register mask with the call-preserved registers.
2509 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2510 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2511
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002512 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002513 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002514 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2515 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002516
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002517 // Set all unused physreg defs as dead.
2518 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002519
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002520 return true;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002521}
2522
Chad Rosier057b6d32011-11-14 23:04:09 +00002523bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002524 return Len <= 16;
2525}
2526
Jim Grosbach0c509fa2012-04-06 23:43:50 +00002527bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002528 uint64_t Len, unsigned Alignment) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002529 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier057b6d32011-11-14 23:04:09 +00002530 if (!ARMIsMemCpySmall(Len))
Chad Rosierab7223e2011-11-14 22:46:17 +00002531 return false;
2532
Chad Rosierab7223e2011-11-14 22:46:17 +00002533 while (Len) {
2534 MVT VT;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002535 if (!Alignment || Alignment >= 4) {
2536 if (Len >= 4)
2537 VT = MVT::i32;
2538 else if (Len >= 2)
2539 VT = MVT::i16;
2540 else {
2541 assert (Len == 1 && "Expected a length of 1!");
2542 VT = MVT::i8;
2543 }
2544 } else {
2545 // Bound based on alignment.
2546 if (Len >= 2 && Alignment == 2)
2547 VT = MVT::i16;
2548 else {
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002549 VT = MVT::i8;
2550 }
Chad Rosierab7223e2011-11-14 22:46:17 +00002551 }
2552
2553 bool RV;
2554 unsigned ResultReg;
2555 RV = ARMEmitLoad(VT, ResultReg, Src);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002556 assert (RV == true && "Should be able to handle this load.");
Chad Rosierab7223e2011-11-14 22:46:17 +00002557 RV = ARMEmitStore(VT, ResultReg, Dest);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002558 assert (RV == true && "Should be able to handle this store.");
Duncan Sandsae22c602012-02-05 14:20:11 +00002559 (void)RV;
Chad Rosierab7223e2011-11-14 22:46:17 +00002560
2561 unsigned Size = VT.getSizeInBits()/8;
2562 Len -= Size;
2563 Dest.Offset += Size;
2564 Src.Offset += Size;
2565 }
2566
2567 return true;
2568}
2569
Chad Rosiera7ebc562011-11-11 23:31:03 +00002570bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2571 // FIXME: Handle more intrinsics.
2572 switch (I.getIntrinsicID()) {
2573 default: return false;
Chad Rosier820d248c2012-05-30 17:23:22 +00002574 case Intrinsic::frameaddress: {
2575 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2576 MFI->setFrameAddressIsTaken(true);
2577
2578 unsigned LdrOpc;
2579 const TargetRegisterClass *RC;
2580 if (isThumb2) {
2581 LdrOpc = ARM::t2LDRi12;
2582 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2583 } else {
2584 LdrOpc = ARM::LDRi12;
2585 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2586 }
2587
2588 const ARMBaseRegisterInfo *RegInfo =
2589 static_cast<const ARMBaseRegisterInfo*>(TM.getRegisterInfo());
2590 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2591 unsigned SrcReg = FramePtr;
2592
2593 // Recursively load frame address
2594 // ldr r0 [fp]
2595 // ldr r0 [r0]
2596 // ldr r0 [r0]
2597 // ...
2598 unsigned DestReg;
2599 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2600 while (Depth--) {
2601 DestReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002602 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier820d248c2012-05-30 17:23:22 +00002603 TII.get(LdrOpc), DestReg)
2604 .addReg(SrcReg).addImm(0));
2605 SrcReg = DestReg;
2606 }
Chad Rosierf3193242012-06-01 21:12:31 +00002607 UpdateValueMap(&I, SrcReg);
Chad Rosier820d248c2012-05-30 17:23:22 +00002608 return true;
2609 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002610 case Intrinsic::memcpy:
2611 case Intrinsic::memmove: {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002612 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2613 // Don't handle volatile.
2614 if (MTI.isVolatile())
2615 return false;
Chad Rosierab7223e2011-11-14 22:46:17 +00002616
2617 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2618 // we would emit dead code because we don't currently handle memmoves.
2619 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2620 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier057b6d32011-11-14 23:04:09 +00002621 // Small memcpy's are common enough that we want to do them without a call
2622 // if possible.
Chad Rosierab7223e2011-11-14 22:46:17 +00002623 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier057b6d32011-11-14 23:04:09 +00002624 if (ARMIsMemCpySmall(Len)) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002625 Address Dest, Src;
2626 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2627 !ARMComputeAddress(MTI.getRawSource(), Src))
2628 return false;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002629 unsigned Alignment = MTI.getAlignment();
2630 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
Chad Rosierab7223e2011-11-14 22:46:17 +00002631 return true;
2632 }
2633 }
Jush Luac96b762012-06-14 06:08:19 +00002634
Chad Rosiera7ebc562011-11-11 23:31:03 +00002635 if (!MTI.getLength()->getType()->isIntegerTy(32))
2636 return false;
Jush Luac96b762012-06-14 06:08:19 +00002637
Chad Rosiera7ebc562011-11-11 23:31:03 +00002638 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2639 return false;
2640
2641 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2642 return SelectCall(&I, IntrMemName);
2643 }
2644 case Intrinsic::memset: {
2645 const MemSetInst &MSI = cast<MemSetInst>(I);
2646 // Don't handle volatile.
2647 if (MSI.isVolatile())
2648 return false;
Jush Luac96b762012-06-14 06:08:19 +00002649
Chad Rosiera7ebc562011-11-11 23:31:03 +00002650 if (!MSI.getLength()->getType()->isIntegerTy(32))
2651 return false;
Jush Luac96b762012-06-14 06:08:19 +00002652
Chad Rosiera7ebc562011-11-11 23:31:03 +00002653 if (MSI.getDestAddressSpace() > 255)
2654 return false;
Jush Luac96b762012-06-14 06:08:19 +00002655
Chad Rosiera7ebc562011-11-11 23:31:03 +00002656 return SelectCall(&I, "memset");
2657 }
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002658 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002659 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(
Eli Bendersky2e2ce492013-01-30 16:30:19 +00002660 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002661 return true;
2662 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002663 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002664}
2665
Chad Rosieree7e4522011-11-02 00:18:48 +00002666bool ARMFastISel::SelectTrunc(const Instruction *I) {
Jush Luac96b762012-06-14 06:08:19 +00002667 // The high bits for a type smaller than the register size are assumed to be
Chad Rosieree7e4522011-11-02 00:18:48 +00002668 // undefined.
2669 Value *Op = I->getOperand(0);
2670
2671 EVT SrcVT, DestVT;
2672 SrcVT = TLI.getValueType(Op->getType(), true);
2673 DestVT = TLI.getValueType(I->getType(), true);
2674
2675 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2676 return false;
2677 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2678 return false;
2679
2680 unsigned SrcReg = getRegForValue(Op);
2681 if (!SrcReg) return false;
2682
2683 // Because the high bits are undefined, a truncate doesn't generate
2684 // any code.
2685 UpdateValueMap(I, SrcReg);
2686 return true;
2687}
2688
Chad Rosier62a144f2012-12-17 19:59:43 +00002689unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
Chad Rosier4489f942011-11-02 17:20:24 +00002690 bool isZExt) {
Eli Friedmanc7035512011-05-25 23:49:02 +00002691 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier4489f942011-11-02 17:20:24 +00002692 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002693 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
Chad Rosier4489f942011-11-02 17:20:24 +00002694 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002695
2696 // Table of which combinations can be emitted as a single instruction,
2697 // and which will require two.
2698 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2699 // ARM Thumb
2700 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2701 // ext: s z s z s z s z
2702 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2703 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2704 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2705 };
2706
2707 // Target registers for:
2708 // - For ARM can never be PC.
2709 // - For 16-bit Thumb are restricted to lower 8 registers.
2710 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2711 static const TargetRegisterClass *RCTbl[2][2] = {
2712 // Instructions: Two Single
2713 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2714 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2715 };
2716
2717 // Table governing the instruction(s) to be emitted.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002718 static const struct InstructionTable {
2719 uint32_t Opc : 16;
2720 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2721 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2722 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2723 } IT[2][2][3][2] = {
JF Bastien06ce03d2013-06-07 20:10:37 +00002724 { // Two instructions (first is left shift, second is in this table).
JF Bastiencd4c64d2013-07-17 05:46:46 +00002725 { // ARM Opc S Shift Imm
2726 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2727 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2728 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2729 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2730 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2731 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002732 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002733 { // Thumb Opc S Shift Imm
2734 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2735 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2736 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2737 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2738 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2739 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002740 }
2741 },
2742 { // Single instruction.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002743 { // ARM Opc S Shift Imm
2744 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2745 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2746 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2747 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2748 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2749 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002750 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002751 { // Thumb Opc S Shift Imm
2752 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2753 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2754 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2755 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2756 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2757 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002758 }
2759 }
2760 };
2761
2762 unsigned SrcBits = SrcVT.getSizeInBits();
2763 unsigned DestBits = DestVT.getSizeInBits();
JF Bastien60a24422013-06-08 00:51:51 +00002764 (void) DestBits;
JF Bastien06ce03d2013-06-07 20:10:37 +00002765 assert((SrcBits < DestBits) && "can only extend to larger types");
2766 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2767 "other sizes unimplemented");
2768 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2769 "other sizes unimplemented");
2770
2771 bool hasV6Ops = Subtarget->hasV6Ops();
JF Bastiencd4c64d2013-07-17 05:46:46 +00002772 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
JF Bastien06ce03d2013-06-07 20:10:37 +00002773 assert((Bitness < 3) && "sanity-check table bounds");
2774
2775 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2776 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
JF Bastiencd4c64d2013-07-17 05:46:46 +00002777 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2778 unsigned Opc = ITP->Opc;
JF Bastien06ce03d2013-06-07 20:10:37 +00002779 assert(ARM::KILL != Opc && "Invalid table entry");
JF Bastiencd4c64d2013-07-17 05:46:46 +00002780 unsigned hasS = ITP->hasS;
2781 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2782 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2783 "only MOVsi has shift operand addressing mode");
2784 unsigned Imm = ITP->Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002785
2786 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2787 bool setsCPSR = &ARM::tGPRRegClass == RC;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002788 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
JF Bastien06ce03d2013-06-07 20:10:37 +00002789 unsigned ResultReg;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002790 // MOVsi encodes shift and immediate in shift operand addressing mode.
2791 // The following condition has the same value when emitting two
2792 // instruction sequences: both are shifts.
2793 bool ImmIsSO = (Shift != ARM_AM::no_shift);
JF Bastien06ce03d2013-06-07 20:10:37 +00002794
2795 // Either one or two instructions are emitted.
2796 // They're always of the form:
2797 // dst = in OP imm
2798 // CPSR is set only by 16-bit Thumb instructions.
2799 // Predicate, if any, is AL.
2800 // S bit, if available, is always 0.
2801 // When two are emitted the first's result will feed as the second's input,
2802 // that value is then dead.
2803 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2804 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2805 ResultReg = createResultReg(RC);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002806 bool isLsl = (0 == Instr) && !isSingleInstr;
2807 unsigned Opcode = isLsl ? LSLOpc : Opc;
2808 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2809 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002810 bool isKill = 1 == Instr;
2811 MachineInstrBuilder MIB = BuildMI(
Rafael Espindolaea09c592014-02-18 22:05:46 +00002812 *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg);
JF Bastien06ce03d2013-06-07 20:10:37 +00002813 if (setsCPSR)
2814 MIB.addReg(ARM::CPSR, RegState::Define);
Jim Grosbach3fa74912013-08-16 23:37:36 +00002815 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002816 AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
JF Bastien06ce03d2013-06-07 20:10:37 +00002817 if (hasS)
2818 AddDefaultCC(MIB);
2819 // Second instruction consumes the first's result.
2820 SrcReg = ResultReg;
Eli Friedmanc7035512011-05-25 23:49:02 +00002821 }
2822
Chad Rosier4489f942011-11-02 17:20:24 +00002823 return ResultReg;
2824}
2825
2826bool ARMFastISel::SelectIntExt(const Instruction *I) {
2827 // On ARM, in general, integer casts don't involve legal types; this code
2828 // handles promotable integers.
Chad Rosier4489f942011-11-02 17:20:24 +00002829 Type *DestTy = I->getType();
2830 Value *Src = I->getOperand(0);
2831 Type *SrcTy = Src->getType();
2832
Chad Rosier4489f942011-11-02 17:20:24 +00002833 bool isZExt = isa<ZExtInst>(I);
2834 unsigned SrcReg = getRegForValue(Src);
2835 if (!SrcReg) return false;
2836
Chad Rosier62a144f2012-12-17 19:59:43 +00002837 EVT SrcEVT, DestEVT;
2838 SrcEVT = TLI.getValueType(SrcTy, true);
2839 DestEVT = TLI.getValueType(DestTy, true);
2840 if (!SrcEVT.isSimple()) return false;
2841 if (!DestEVT.isSimple()) return false;
Patrik Hagglundc494d242012-12-17 14:30:06 +00002842
Chad Rosier62a144f2012-12-17 19:59:43 +00002843 MVT SrcVT = SrcEVT.getSimpleVT();
2844 MVT DestVT = DestEVT.getSimpleVT();
Chad Rosier4489f942011-11-02 17:20:24 +00002845 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2846 if (ResultReg == 0) return false;
2847 UpdateValueMap(I, ResultReg);
Eli Friedmanc7035512011-05-25 23:49:02 +00002848 return true;
2849}
2850
Jush Lu4705da92012-08-03 02:37:48 +00002851bool ARMFastISel::SelectShift(const Instruction *I,
2852 ARM_AM::ShiftOpc ShiftTy) {
2853 // We handle thumb2 mode by target independent selector
2854 // or SelectionDAG ISel.
2855 if (isThumb2)
2856 return false;
2857
2858 // Only handle i32 now.
2859 EVT DestVT = TLI.getValueType(I->getType(), true);
2860 if (DestVT != MVT::i32)
2861 return false;
2862
2863 unsigned Opc = ARM::MOVsr;
2864 unsigned ShiftImm;
2865 Value *Src2Value = I->getOperand(1);
2866 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2867 ShiftImm = CI->getZExtValue();
2868
2869 // Fall back to selection DAG isel if the shift amount
2870 // is zero or greater than the width of the value type.
2871 if (ShiftImm == 0 || ShiftImm >=32)
2872 return false;
2873
2874 Opc = ARM::MOVsi;
2875 }
2876
2877 Value *Src1Value = I->getOperand(0);
2878 unsigned Reg1 = getRegForValue(Src1Value);
2879 if (Reg1 == 0) return false;
2880
Nadav Rotema8e15b02012-09-06 11:13:55 +00002881 unsigned Reg2 = 0;
Jush Lu4705da92012-08-03 02:37:48 +00002882 if (Opc == ARM::MOVsr) {
2883 Reg2 = getRegForValue(Src2Value);
2884 if (Reg2 == 0) return false;
2885 }
2886
JF Bastien13969d02013-05-29 15:45:47 +00002887 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Jush Lu4705da92012-08-03 02:37:48 +00002888 if(ResultReg == 0) return false;
2889
Rafael Espindolaea09c592014-02-18 22:05:46 +00002890 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu4705da92012-08-03 02:37:48 +00002891 TII.get(Opc), ResultReg)
2892 .addReg(Reg1);
2893
2894 if (Opc == ARM::MOVsi)
2895 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2896 else if (Opc == ARM::MOVsr) {
2897 MIB.addReg(Reg2);
2898 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2899 }
2900
2901 AddOptionalDefs(MIB);
2902 UpdateValueMap(I, ResultReg);
2903 return true;
2904}
2905
Eric Christopherc3e118e2010-09-02 23:43:26 +00002906// TODO: SoftFP support.
Eric Christopher84bdfd82010-07-21 22:26:11 +00002907bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher2ff757d2010-09-09 01:06:51 +00002908
Eric Christopher84bdfd82010-07-21 22:26:11 +00002909 switch (I->getOpcode()) {
Eric Christopher00202ee2010-08-23 21:44:12 +00002910 case Instruction::Load:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002911 return SelectLoad(I);
Eric Christopherfde5a3d2010-09-01 22:16:27 +00002912 case Instruction::Store:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002913 return SelectStore(I);
Eric Christopher6aaed722010-09-03 00:35:47 +00002914 case Instruction::Br:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002915 return SelectBranch(I);
Chad Rosierded4c992012-02-07 23:56:08 +00002916 case Instruction::IndirectBr:
2917 return SelectIndirectBr(I);
Eric Christopherc3e9c402010-09-08 23:13:45 +00002918 case Instruction::ICmp:
2919 case Instruction::FCmp:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002920 return SelectCmp(I);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00002921 case Instruction::FPExt:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002922 return SelectFPExt(I);
Eric Christopher5903c0b2010-09-09 20:26:31 +00002923 case Instruction::FPTrunc:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002924 return SelectFPTrunc(I);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002925 case Instruction::SIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002926 return SelectIToFP(I, /*isSigned*/ true);
Chad Rosiera8a8ac52012-02-03 19:42:52 +00002927 case Instruction::UIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002928 return SelectIToFP(I, /*isSigned*/ false);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002929 case Instruction::FPToSI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002930 return SelectFPToI(I, /*isSigned*/ true);
Chad Rosier41f0e782012-02-03 20:27:51 +00002931 case Instruction::FPToUI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002932 return SelectFPToI(I, /*isSigned*/ false);
Chad Rosier685b20c2012-02-06 23:50:07 +00002933 case Instruction::Add:
2934 return SelectBinaryIntOp(I, ISD::ADD);
Chad Rosierbd471252012-02-08 02:29:21 +00002935 case Instruction::Or:
2936 return SelectBinaryIntOp(I, ISD::OR);
Chad Rosier0ee8c512012-02-08 02:45:44 +00002937 case Instruction::Sub:
2938 return SelectBinaryIntOp(I, ISD::SUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002939 case Instruction::FAdd:
Chad Rosier685b20c2012-02-06 23:50:07 +00002940 return SelectBinaryFPOp(I, ISD::FADD);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002941 case Instruction::FSub:
Chad Rosier685b20c2012-02-06 23:50:07 +00002942 return SelectBinaryFPOp(I, ISD::FSUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002943 case Instruction::FMul:
Chad Rosier685b20c2012-02-06 23:50:07 +00002944 return SelectBinaryFPOp(I, ISD::FMUL);
Eric Christopher8b912662010-09-14 23:03:37 +00002945 case Instruction::SDiv:
Chad Rosieraaa55a82012-02-03 21:07:27 +00002946 return SelectDiv(I, /*isSigned*/ true);
2947 case Instruction::UDiv:
2948 return SelectDiv(I, /*isSigned*/ false);
Eric Christophereae1b382010-10-11 08:37:26 +00002949 case Instruction::SRem:
Chad Rosierb84a4b42012-02-03 21:23:45 +00002950 return SelectRem(I, /*isSigned*/ true);
2951 case Instruction::URem:
2952 return SelectRem(I, /*isSigned*/ false);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002953 case Instruction::Call:
Chad Rosiera7ebc562011-11-11 23:31:03 +00002954 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2955 return SelectIntrinsicCall(*II);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002956 return SelectCall(I);
Eric Christopher511aa312010-10-11 08:27:59 +00002957 case Instruction::Select:
2958 return SelectSelect(I);
Eric Christopher93bbe652010-10-22 01:28:00 +00002959 case Instruction::Ret:
2960 return SelectRet(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002961 case Instruction::Trunc:
Chad Rosieree7e4522011-11-02 00:18:48 +00002962 return SelectTrunc(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002963 case Instruction::ZExt:
2964 case Instruction::SExt:
Chad Rosieree7e4522011-11-02 00:18:48 +00002965 return SelectIntExt(I);
Jush Lu4705da92012-08-03 02:37:48 +00002966 case Instruction::Shl:
2967 return SelectShift(I, ARM_AM::lsl);
2968 case Instruction::LShr:
2969 return SelectShift(I, ARM_AM::lsr);
2970 case Instruction::AShr:
2971 return SelectShift(I, ARM_AM::asr);
Eric Christopher84bdfd82010-07-21 22:26:11 +00002972 default: break;
2973 }
2974 return false;
2975}
2976
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002977namespace {
2978// This table describes sign- and zero-extend instructions which can be
2979// folded into a preceding load. All of these extends have an immediate
2980// (sometimes a mask and sometimes a shift) that's applied after
2981// extension.
2982const struct FoldableLoadExtendsStruct {
2983 uint16_t Opc[2]; // ARM, Thumb.
2984 uint8_t ExpectedImm;
2985 uint8_t isZExt : 1;
2986 uint8_t ExpectedVT : 7;
2987} FoldableLoadExtends[] = {
2988 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2989 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2990 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2991 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2992 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2993};
2994}
2995
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002996/// \brief The specified machine instr operand is a vreg, and that
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002997/// vreg is being provided by the specified load instruction. If possible,
2998/// try to fold the load as an operand to the instruction, returning true if
2999/// successful.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00003000bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3001 const LoadInst *LI) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003002 // Verify we have a legal type before going any further.
3003 MVT VT;
3004 if (!isLoadTypeLegal(LI->getType(), VT))
3005 return false;
3006
3007 // Combine load followed by zero- or sign-extend.
3008 // ldrb r1, [r0] ldrb r1, [r0]
3009 // uxtb r2, r1 =>
3010 // mov r3, r2 mov r3, r1
JF Bastien3c6bb8e2013-06-11 22:13:46 +00003011 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
3012 return false;
3013 const uint64_t Imm = MI->getOperand(2).getImm();
3014
3015 bool Found = false;
3016 bool isZExt;
3017 for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
3018 i != e; ++i) {
3019 if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
3020 (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
3021 MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
3022 Found = true;
3023 isZExt = FoldableLoadExtends[i].isZExt;
3024 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003025 }
JF Bastien3c6bb8e2013-06-11 22:13:46 +00003026 if (!Found) return false;
3027
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003028 // See if we can handle this address.
3029 Address Addr;
3030 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
Jush Luac96b762012-06-14 06:08:19 +00003031
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003032 unsigned ResultReg = MI->getOperand(0).getReg();
Chad Rosier563de602011-12-13 19:22:14 +00003033 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003034 return false;
3035 MI->eraseFromParent();
3036 return true;
3037}
3038
Jush Lu47172a02012-09-27 05:21:41 +00003039unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003040 unsigned Align, MVT VT) {
Jush Lu47172a02012-09-27 05:21:41 +00003041 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
3042 ARMConstantPoolConstant *CPV =
3043 ARMConstantPoolConstant::Create(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
3044 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
3045
3046 unsigned Opc;
3047 unsigned DestReg1 = createResultReg(TLI.getRegClassFor(VT));
3048 // Load value.
3049 if (isThumb2) {
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003050 DestReg1 = constrainOperandRegClass(TII.get(ARM::t2LDRpci), DestReg1, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003051 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu47172a02012-09-27 05:21:41 +00003052 TII.get(ARM::t2LDRpci), DestReg1)
3053 .addConstantPoolIndex(Idx));
3054 Opc = UseGOTOFF ? ARM::t2ADDrr : ARM::t2LDRs;
3055 } else {
3056 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003057 DestReg1 = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg1, 0);
Jush Lu47172a02012-09-27 05:21:41 +00003058 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00003059 DbgLoc, TII.get(ARM::LDRcp), DestReg1)
Jush Lu47172a02012-09-27 05:21:41 +00003060 .addConstantPoolIndex(Idx).addImm(0));
3061 Opc = UseGOTOFF ? ARM::ADDrr : ARM::LDRrs;
3062 }
3063
3064 unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
3065 if (GlobalBaseReg == 0) {
3066 GlobalBaseReg = MRI.createVirtualRegister(TLI.getRegClassFor(VT));
3067 AFI->setGlobalBaseReg(GlobalBaseReg);
3068 }
3069
3070 unsigned DestReg2 = createResultReg(TLI.getRegClassFor(VT));
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003071 DestReg2 = constrainOperandRegClass(TII.get(Opc), DestReg2, 0);
3072 DestReg1 = constrainOperandRegClass(TII.get(Opc), DestReg1, 1);
3073 GlobalBaseReg = constrainOperandRegClass(TII.get(Opc), GlobalBaseReg, 2);
Jush Lu47172a02012-09-27 05:21:41 +00003074 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00003075 DbgLoc, TII.get(Opc), DestReg2)
Jush Lu47172a02012-09-27 05:21:41 +00003076 .addReg(DestReg1)
3077 .addReg(GlobalBaseReg);
3078 if (!UseGOTOFF)
3079 MIB.addImm(0);
3080 AddOptionalDefs(MIB);
3081
3082 return DestReg2;
3083}
3084
Evan Cheng615620c2013-02-11 01:27:15 +00003085bool ARMFastISel::FastLowerArguments() {
3086 if (!FuncInfo.CanLowerReturn)
3087 return false;
3088
3089 const Function *F = FuncInfo.Fn;
3090 if (F->isVarArg())
3091 return false;
3092
3093 CallingConv::ID CC = F->getCallingConv();
3094 switch (CC) {
3095 default:
3096 return false;
3097 case CallingConv::Fast:
3098 case CallingConv::C:
3099 case CallingConv::ARM_AAPCS_VFP:
3100 case CallingConv::ARM_AAPCS:
3101 case CallingConv::ARM_APCS:
3102 break;
3103 }
3104
3105 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3106 // which are passed in r0 - r3.
3107 unsigned Idx = 1;
3108 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3109 I != E; ++I, ++Idx) {
3110 if (Idx > 4)
3111 return false;
3112
3113 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
3114 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
3115 F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
3116 return false;
3117
3118 Type *ArgTy = I->getType();
3119 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3120 return false;
3121
3122 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00003123 if (!ArgVT.isSimple()) return false;
Evan Cheng615620c2013-02-11 01:27:15 +00003124 switch (ArgVT.getSimpleVT().SimpleTy) {
3125 case MVT::i8:
3126 case MVT::i16:
3127 case MVT::i32:
3128 break;
3129 default:
3130 return false;
3131 }
3132 }
3133
3134
3135 static const uint16_t GPRArgRegs[] = {
3136 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3137 };
3138
Jim Grosbachd69f3ed2013-08-16 23:37:23 +00003139 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
Evan Cheng615620c2013-02-11 01:27:15 +00003140 Idx = 0;
3141 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3142 I != E; ++I, ++Idx) {
Evan Cheng615620c2013-02-11 01:27:15 +00003143 unsigned SrcReg = GPRArgRegs[Idx];
3144 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3145 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3146 // Without this, EmitLiveInCopies may eliminate the livein if its only
3147 // use is a bitcast (which isn't turned into an instruction).
3148 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003149 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3150 TII.get(TargetOpcode::COPY),
Evan Cheng615620c2013-02-11 01:27:15 +00003151 ResultReg).addReg(DstReg, getKillRegState(true));
3152 UpdateValueMap(I, ResultReg);
3153 }
3154
3155 return true;
3156}
3157
Eric Christopher84bdfd82010-07-21 22:26:11 +00003158namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003159 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3160 const TargetLibraryInfo *libInfo) {
Eric Christopher5501b7e2010-10-11 20:05:22 +00003161 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach68147ee2010-11-09 19:22:26 +00003162
Eric Christopher5501b7e2010-10-11 20:05:22 +00003163 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
JF Bastien18db1f22013-06-14 02:49:43 +00003164 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
3165 bool UseFastISel = false;
Tim Northoverd6a729b2014-01-06 14:28:05 +00003166 UseFastISel |= Subtarget->isTargetMachO() && !Subtarget->isThumb1Only();
JF Bastien18db1f22013-06-14 02:49:43 +00003167 UseFastISel |= Subtarget->isTargetLinux() && !Subtarget->isThumb();
3168 UseFastISel |= Subtarget->isTargetNaCl() && !Subtarget->isThumb();
3169
3170 if (UseFastISel) {
3171 // iOS always has a FP for backtracking, force other targets
3172 // to keep their FP when doing FastISel. The emitted code is
3173 // currently superior, and in cases like test-suite's lencod
3174 // FastISel isn't quite correct when FP is eliminated.
3175 TM.Options.NoFramePointerElim = true;
Bob Wilson3e6fa462012-08-03 04:06:28 +00003176 return new ARMFastISel(funcInfo, libInfo);
JF Bastien18db1f22013-06-14 02:49:43 +00003177 }
Evan Cheng23b05d12010-07-26 18:32:55 +00003178 return 0;
Eric Christopher84bdfd82010-07-21 22:26:11 +00003179 }
3180}