blob: 4bf55fb8f38ab20fc2034c2a2276f80374fa94e4 [file] [log] [blame]
Eric Christopherab695882010-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 Christopher456144e2010-08-19 00:37:05 +000017#include "ARMBaseInstrInfo.h"
Eric Christopherd10cd7b2010-09-10 23:18:12 +000018#include "ARMCallingConv.h"
Eric Christopherab695882010-07-21 22:26:11 +000019#include "ARMRegisterInfo.h"
20#include "ARMTargetMachine.h"
21#include "ARMSubtarget.h"
Eric Christopherc9932f62010-10-01 23:24:42 +000022#include "ARMConstantPoolValue.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Eric Christopherab695882010-07-21 22:26:11 +000024#include "llvm/CallingConv.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/Instructions.h"
28#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000029#include "llvm/Module.h"
Jay Foad562b84b2011-04-11 09:35:34 +000030#include "llvm/Operator.h"
Eric Christopherab695882010-07-21 22:26:11 +000031#include "llvm/CodeGen/Analysis.h"
32#include "llvm/CodeGen/FastISel.h"
33#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
35#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000036#include "llvm/CodeGen/MachineConstantPool.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000038#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000040#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000042#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000043#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000045#include "llvm/Target/TargetData.h"
46#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetLowering.h"
48#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000049#include "llvm/Target/TargetOptions.h"
50using namespace llvm;
51
Eric Christopher038fea52010-08-17 00:46:57 +000052static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000053DisableARMFastISel("disable-arm-fast-isel",
54 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000055 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000056
Eric Christopher836c6242010-12-15 23:47:29 +000057extern cl::opt<bool> EnableARMLongCalls;
58
Eric Christopherab695882010-07-21 22:26:11 +000059namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000060
Eric Christopher0d581222010-11-19 22:30:02 +000061 // All possible address modes, plus some.
62 typedef struct Address {
63 enum {
64 RegBase,
65 FrameIndexBase
66 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000067
Eric Christopher0d581222010-11-19 22:30:02 +000068 union {
69 unsigned Reg;
70 int FI;
71 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000072
Eric Christopher0d581222010-11-19 22:30:02 +000073 int Offset;
Eric Christopher827656d2010-11-20 22:38:27 +000074
Eric Christopher0d581222010-11-19 22:30:02 +000075 // Innocuous defaults for our address.
76 Address()
Jim Grosbach0c720762011-05-16 22:24:07 +000077 : BaseType(RegBase), Offset(0) {
Eric Christopher0d581222010-11-19 22:30:02 +000078 Base.Reg = 0;
79 }
80 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000081
82class ARMFastISel : public FastISel {
83
84 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
85 /// make the right decision when generating code for different targets.
86 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000087 const TargetMachine &TM;
88 const TargetInstrInfo &TII;
89 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000090 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000091
Eric Christopher8cf6c602010-09-29 22:24:45 +000092 // Convenience variables to avoid some queries.
Chad Rosier66dc8ca2011-11-08 21:12:00 +000093 bool isThumb2;
Eric Christopher8cf6c602010-09-29 22:24:45 +000094 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000095
Eric Christopherab695882010-07-21 22:26:11 +000096 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000097 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000098 : FastISel(funcInfo),
99 TM(funcInfo.MF->getTarget()),
100 TII(*TM.getInstrInfo()),
101 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000102 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000103 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000104 isThumb2 = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000105 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000106 }
107
Eric Christophercb592292010-08-20 00:20:31 +0000108 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000109 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC);
111 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill);
114 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 unsigned Op1, bool Op1IsKill);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000118 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
121 unsigned Op1, bool Op1IsKill,
122 unsigned Op2, bool Op2IsKill);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000123 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
124 const TargetRegisterClass *RC,
125 unsigned Op0, bool Op0IsKill,
126 uint64_t Imm);
127 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
128 const TargetRegisterClass *RC,
129 unsigned Op0, bool Op0IsKill,
130 const ConstantFP *FPImm);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000131 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
132 const TargetRegisterClass *RC,
133 unsigned Op0, bool Op0IsKill,
134 unsigned Op1, bool Op1IsKill,
135 uint64_t Imm);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000136 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
137 const TargetRegisterClass *RC,
138 uint64_t Imm);
Eric Christopherd94bc542011-04-29 22:07:50 +0000139 virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
140 const TargetRegisterClass *RC,
141 uint64_t Imm1, uint64_t Imm2);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000142
Eric Christopher0fe7d542010-08-17 01:25:29 +0000143 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
144 unsigned Op0, bool Op0IsKill,
145 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000146
Eric Christophercb592292010-08-20 00:20:31 +0000147 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000148 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000149 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000150 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000151
152 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000153
Eric Christopher83007122010-08-23 21:44:12 +0000154 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000155 private:
Eric Christopher17787722010-10-21 21:47:51 +0000156 bool SelectLoad(const Instruction *I);
157 bool SelectStore(const Instruction *I);
158 bool SelectBranch(const Instruction *I);
159 bool SelectCmp(const Instruction *I);
160 bool SelectFPExt(const Instruction *I);
161 bool SelectFPTrunc(const Instruction *I);
162 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
163 bool SelectSIToFP(const Instruction *I);
164 bool SelectFPToSI(const Instruction *I);
165 bool SelectSDiv(const Instruction *I);
166 bool SelectSRem(const Instruction *I);
Chad Rosier11add262011-11-11 23:31:03 +0000167 bool SelectCall(const Instruction *I, const char *IntrMemName);
168 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher17787722010-10-21 21:47:51 +0000169 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000170 bool SelectRet(const Instruction *I);
Chad Rosier0d7b2312011-11-02 00:18:48 +0000171 bool SelectTrunc(const Instruction *I);
172 bool SelectIntExt(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000173
Eric Christopher83007122010-08-23 21:44:12 +0000174 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000175 private:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000176 bool isTypeLegal(Type *Ty, MVT &VT);
177 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosiere07cd5e2011-11-02 18:08:25 +0000178 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
179 bool isZExt);
Eric Christopher0d581222010-11-19 22:30:02 +0000180 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
181 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
182 bool ARMComputeAddress(const Value *Obj, Address &Addr);
183 void ARMSimplifyAddress(Address &Addr, EVT VT);
Chad Rosier87633022011-11-02 17:20:24 +0000184 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000185 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000186 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000187 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000188 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000189 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000190 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000191
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000192 // Call handling routines.
193 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000194 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
195 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000196 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000197 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000198 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000199 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000200 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
201 SmallVectorImpl<unsigned> &RegArgs,
202 CallingConv::ID CC,
203 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000204 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000205 const Instruction *I, CallingConv::ID CC,
206 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000207 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000208
209 // OptionalDef handling routines.
210 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000211 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000212 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
213 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000214 void AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000215 const MachineInstrBuilder &MIB,
216 unsigned Flags);
Eric Christopher456144e2010-08-19 00:37:05 +0000217};
Eric Christopherab695882010-07-21 22:26:11 +0000218
219} // end anonymous namespace
220
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000221#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000222
Eric Christopher456144e2010-08-19 00:37:05 +0000223// DefinesOptionalPredicate - This is different from DefinesPredicate in that
224// we don't care about implicit defs here, just places we'll need to add a
225// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
226bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Chenge837dea2011-06-28 19:10:37 +0000227 const MCInstrDesc &MCID = MI->getDesc();
228 if (!MCID.hasOptionalDef())
Eric Christopher456144e2010-08-19 00:37:05 +0000229 return false;
230
231 // Look to see if our OptionalDef is defining CPSR or CCR.
232 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
233 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000234 if (!MO.isReg() || !MO.isDef()) continue;
235 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000236 *CPSR = true;
237 }
238 return true;
239}
240
Eric Christopheraf3dce52011-03-12 01:09:29 +0000241bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000242 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher299bbb22011-04-29 00:03:10 +0000243
Eric Christopheraf3dce52011-03-12 01:09:29 +0000244 // If we're a thumb2 or not NEON function we were handled via isPredicable.
Evan Chenge837dea2011-06-28 19:10:37 +0000245 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopheraf3dce52011-03-12 01:09:29 +0000246 AFI->isThumb2Function())
247 return false;
Eric Christopher299bbb22011-04-29 00:03:10 +0000248
Evan Chenge837dea2011-06-28 19:10:37 +0000249 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
250 if (MCID.OpInfo[i].isPredicate())
Eric Christopheraf3dce52011-03-12 01:09:29 +0000251 return true;
Eric Christopher299bbb22011-04-29 00:03:10 +0000252
Eric Christopheraf3dce52011-03-12 01:09:29 +0000253 return false;
254}
255
Eric Christopher456144e2010-08-19 00:37:05 +0000256// If the machine is predicable go ahead and add the predicate operands, if
257// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000258// TODO: If we want to support thumb1 then we'll need to deal with optional
259// CPSR defs that need to be added before the remaining operands. See s_cc_out
260// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000261const MachineInstrBuilder &
262ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
263 MachineInstr *MI = &*MIB;
264
Eric Christopheraf3dce52011-03-12 01:09:29 +0000265 // Do we use a predicate? or...
266 // Are we NEON in ARM mode and have a predicate operand? If so, I know
267 // we're not predicable but add it anyways.
268 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000269 AddDefaultPred(MIB);
Eric Christopher299bbb22011-04-29 00:03:10 +0000270
Eric Christopher456144e2010-08-19 00:37:05 +0000271 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
272 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000273 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000274 if (DefinesOptionalPredicate(MI, &CPSR)) {
275 if (CPSR)
276 AddDefaultT1CC(MIB);
277 else
278 AddDefaultCC(MIB);
279 }
280 return MIB;
281}
282
Eric Christopher0fe7d542010-08-17 01:25:29 +0000283unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
284 const TargetRegisterClass* RC) {
285 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000286 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000287
Eric Christopher456144e2010-08-19 00:37:05 +0000288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000289 return ResultReg;
290}
291
292unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
293 const TargetRegisterClass *RC,
294 unsigned Op0, bool Op0IsKill) {
295 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000296 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000297
298 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000299 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000300 .addReg(Op0, Op0IsKill * RegState::Kill));
301 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000302 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000303 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 TII.get(TargetOpcode::COPY), ResultReg)
306 .addReg(II.ImplicitDefs[0]));
307 }
308 return ResultReg;
309}
310
311unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
312 const TargetRegisterClass *RC,
313 unsigned Op0, bool Op0IsKill,
314 unsigned Op1, bool Op1IsKill) {
315 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000316 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000317
318 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000319 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000320 .addReg(Op0, Op0IsKill * RegState::Kill)
321 .addReg(Op1, Op1IsKill * RegState::Kill));
322 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000323 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000324 .addReg(Op0, Op0IsKill * RegState::Kill)
325 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000327 TII.get(TargetOpcode::COPY), ResultReg)
328 .addReg(II.ImplicitDefs[0]));
329 }
330 return ResultReg;
331}
332
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000333unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
334 const TargetRegisterClass *RC,
335 unsigned Op0, bool Op0IsKill,
336 unsigned Op1, bool Op1IsKill,
337 unsigned Op2, bool Op2IsKill) {
338 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000339 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000340
341 if (II.getNumDefs() >= 1)
342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
343 .addReg(Op0, Op0IsKill * RegState::Kill)
344 .addReg(Op1, Op1IsKill * RegState::Kill)
345 .addReg(Op2, Op2IsKill * RegState::Kill));
346 else {
347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
348 .addReg(Op0, Op0IsKill * RegState::Kill)
349 .addReg(Op1, Op1IsKill * RegState::Kill)
350 .addReg(Op2, Op2IsKill * RegState::Kill));
351 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
352 TII.get(TargetOpcode::COPY), ResultReg)
353 .addReg(II.ImplicitDefs[0]));
354 }
355 return ResultReg;
356}
357
Eric Christopher0fe7d542010-08-17 01:25:29 +0000358unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
359 const TargetRegisterClass *RC,
360 unsigned Op0, bool Op0IsKill,
361 uint64_t Imm) {
362 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000363 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000364
365 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000366 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000367 .addReg(Op0, Op0IsKill * RegState::Kill)
368 .addImm(Imm));
369 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000370 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000371 .addReg(Op0, Op0IsKill * RegState::Kill)
372 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000374 TII.get(TargetOpcode::COPY), ResultReg)
375 .addReg(II.ImplicitDefs[0]));
376 }
377 return ResultReg;
378}
379
380unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
381 const TargetRegisterClass *RC,
382 unsigned Op0, bool Op0IsKill,
383 const ConstantFP *FPImm) {
384 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000385 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000386
387 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000388 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000389 .addReg(Op0, Op0IsKill * RegState::Kill)
390 .addFPImm(FPImm));
391 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000392 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000393 .addReg(Op0, Op0IsKill * RegState::Kill)
394 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000395 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000396 TII.get(TargetOpcode::COPY), ResultReg)
397 .addReg(II.ImplicitDefs[0]));
398 }
399 return ResultReg;
400}
401
402unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
403 const TargetRegisterClass *RC,
404 unsigned Op0, bool Op0IsKill,
405 unsigned Op1, bool Op1IsKill,
406 uint64_t Imm) {
407 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000408 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000409
410 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000411 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000412 .addReg(Op0, Op0IsKill * RegState::Kill)
413 .addReg(Op1, Op1IsKill * RegState::Kill)
414 .addImm(Imm));
415 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000416 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000417 .addReg(Op0, Op0IsKill * RegState::Kill)
418 .addReg(Op1, Op1IsKill * RegState::Kill)
419 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000420 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000421 TII.get(TargetOpcode::COPY), ResultReg)
422 .addReg(II.ImplicitDefs[0]));
423 }
424 return ResultReg;
425}
426
427unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
428 const TargetRegisterClass *RC,
429 uint64_t Imm) {
430 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000431 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000432
Eric Christopher0fe7d542010-08-17 01:25:29 +0000433 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000434 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000435 .addImm(Imm));
436 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000437 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000438 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000439 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000440 TII.get(TargetOpcode::COPY), ResultReg)
441 .addReg(II.ImplicitDefs[0]));
442 }
443 return ResultReg;
444}
445
Eric Christopherd94bc542011-04-29 22:07:50 +0000446unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
447 const TargetRegisterClass *RC,
448 uint64_t Imm1, uint64_t Imm2) {
449 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000450 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher471e4222011-06-08 23:55:35 +0000451
Eric Christopherd94bc542011-04-29 22:07:50 +0000452 if (II.getNumDefs() >= 1)
453 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
454 .addImm(Imm1).addImm(Imm2));
455 else {
456 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
457 .addImm(Imm1).addImm(Imm2));
Eric Christopher471e4222011-06-08 23:55:35 +0000458 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherd94bc542011-04-29 22:07:50 +0000459 TII.get(TargetOpcode::COPY),
460 ResultReg)
461 .addReg(II.ImplicitDefs[0]));
462 }
463 return ResultReg;
464}
465
Eric Christopher0fe7d542010-08-17 01:25:29 +0000466unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
467 unsigned Op0, bool Op0IsKill,
468 uint32_t Idx) {
469 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
470 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
471 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000472 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000473 DL, TII.get(TargetOpcode::COPY), ResultReg)
474 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
475 return ResultReg;
476}
477
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000478// TODO: Don't worry about 64-bit now, but when this is fixed remove the
479// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000480unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000481 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000482
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000483 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
484 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
485 TII.get(ARM::VMOVRS), MoveReg)
486 .addReg(SrcReg));
487 return MoveReg;
488}
489
490unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000491 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000492
Eric Christopheraa3ace12010-09-09 20:49:25 +0000493 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
494 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000495 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000496 .addReg(SrcReg));
497 return MoveReg;
498}
499
Eric Christopher9ed58df2010-09-09 00:19:41 +0000500// For double width floating point we need to materialize two constants
501// (the high and the low) into integer registers then use a move to get
502// the combined constant into an FP reg.
503unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
504 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000505 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000506
Eric Christopher9ed58df2010-09-09 00:19:41 +0000507 // This checks to see if we can use VFP3 instructions to materialize
508 // a constant, otherwise we have to go through the constant pool.
509 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000510 int Imm;
511 unsigned Opc;
512 if (is64bit) {
513 Imm = ARM_AM::getFP64Imm(Val);
514 Opc = ARM::FCONSTD;
515 } else {
516 Imm = ARM_AM::getFP32Imm(Val);
517 Opc = ARM::FCONSTS;
518 }
Eric Christopher9ed58df2010-09-09 00:19:41 +0000519 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
520 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
521 DestReg)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000522 .addImm(Imm));
Eric Christopher9ed58df2010-09-09 00:19:41 +0000523 return DestReg;
524 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000525
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000526 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000527 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000528
Eric Christopher238bb162010-09-09 23:50:00 +0000529 // MachineConstantPool wants an explicit alignment.
530 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
531 if (Align == 0) {
532 // TODO: Figure out if this is correct.
533 Align = TD.getTypeAllocSize(CFP->getType());
534 }
535 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
536 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
537 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000538
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000539 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000540 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
541 DestReg)
542 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000543 .addReg(0));
544 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000545}
546
Eric Christopher744c7c82010-09-28 22:47:54 +0000547unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000548
Chad Rosier44e89572011-11-04 22:29:00 +0000549 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
550 return false;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000551
552 // If we can do this in a single instruction without a constant pool entry
553 // do so now.
554 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiera4e07272011-11-04 23:09:49 +0000555 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000556 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier4e89d972011-11-11 00:36:21 +0000557 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000558 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier44e89572011-11-04 22:29:00 +0000559 TII.get(Opc), ImmReg)
Chad Rosier42536af2011-11-05 20:16:15 +0000560 .addImm(CI->getZExtValue()));
Chad Rosier44e89572011-11-04 22:29:00 +0000561 return ImmReg;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000562 }
563
Chad Rosier4e89d972011-11-11 00:36:21 +0000564 // Use MVN to emit negative constants.
565 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
566 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosier1c47de82011-11-11 06:27:41 +0000567 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier4e89d972011-11-11 00:36:21 +0000568 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier1c47de82011-11-11 06:27:41 +0000569 if (UseImm) {
Chad Rosier4e89d972011-11-11 00:36:21 +0000570 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
571 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
572 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
573 TII.get(Opc), ImmReg)
574 .addImm(Imm));
575 return ImmReg;
576 }
577 }
578
579 // Load from constant pool. For now 32-bit only.
Chad Rosier44e89572011-11-04 22:29:00 +0000580 if (VT != MVT::i32)
581 return false;
582
583 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
584
Eric Christopher56d2b722010-09-02 23:43:26 +0000585 // MachineConstantPool wants an explicit alignment.
586 unsigned Align = TD.getPrefTypeAlignment(C->getType());
587 if (Align == 0) {
588 // TODO: Figure out if this is correct.
589 Align = TD.getTypeAllocSize(C->getType());
590 }
591 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000592
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000593 if (isThumb2)
Eric Christopher56d2b722010-09-02 23:43:26 +0000594 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000595 TII.get(ARM::t2LDRpci), DestReg)
596 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000597 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000598 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000599 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000600 TII.get(ARM::LDRcp), DestReg)
601 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000602 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000603
Eric Christopher56d2b722010-09-02 23:43:26 +0000604 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000605}
606
Eric Christopherc9932f62010-10-01 23:24:42 +0000607unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000608 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000609 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000610
Eric Christopher890dbbe2010-10-02 00:32:44 +0000611 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000612
Eric Christopher890dbbe2010-10-02 00:32:44 +0000613 // TODO: Need more magic for ARM PIC.
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000614 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000615
Eric Christopher890dbbe2010-10-02 00:32:44 +0000616 // MachineConstantPool wants an explicit alignment.
617 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
618 if (Align == 0) {
619 // TODO: Figure out if this is correct.
620 Align = TD.getTypeAllocSize(GV->getType());
621 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000622
Eric Christopher890dbbe2010-10-02 00:32:44 +0000623 // Grab index.
624 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000625 unsigned Id = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +0000626 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
627 ARMCP::CPValue,
628 PCAdj);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000629 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000630
Eric Christopher890dbbe2010-10-02 00:32:44 +0000631 // Load value.
632 MachineInstrBuilder MIB;
633 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000634 if (isThumb2) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000635 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
636 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
637 .addConstantPoolIndex(Idx);
638 if (RelocM == Reloc::PIC_)
639 MIB.addImm(Id);
640 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000641 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000642 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
643 DestReg)
644 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000645 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000646 }
647 AddOptionalDefs(MIB);
Eli Friedmand6412c92011-06-03 01:13:19 +0000648
649 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
650 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000651 if (isThumb2)
Jim Grosbachb04546f2011-09-13 20:30:37 +0000652 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
653 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedmand6412c92011-06-03 01:13:19 +0000654 .addReg(DestReg)
655 .addImm(0);
656 else
657 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
658 NewDestReg)
659 .addReg(DestReg)
660 .addImm(0);
661 DestReg = NewDestReg;
662 AddOptionalDefs(MIB);
663 }
664
Eric Christopher890dbbe2010-10-02 00:32:44 +0000665 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000666}
667
Eric Christopher9ed58df2010-09-09 00:19:41 +0000668unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
669 EVT VT = TLI.getValueType(C->getType(), true);
670
671 // Only handle simple types.
672 if (!VT.isSimple()) return 0;
673
674 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
675 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000676 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
677 return ARMMaterializeGV(GV, VT);
678 else if (isa<ConstantInt>(C))
679 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000680
Eric Christopherc9932f62010-10-01 23:24:42 +0000681 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000682}
683
Eric Christopherf9764fa2010-09-30 20:49:44 +0000684unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
685 // Don't handle dynamic allocas.
686 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000687
Duncan Sands1440e8b2010-11-03 11:35:31 +0000688 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000689 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000690
Eric Christopherf9764fa2010-09-30 20:49:44 +0000691 DenseMap<const AllocaInst*, int>::iterator SI =
692 FuncInfo.StaticAllocaMap.find(AI);
693
694 // This will get lowered later into the correct offsets and registers
695 // via rewriteXFrameIndex.
696 if (SI != FuncInfo.StaticAllocaMap.end()) {
697 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
698 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000699 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopherf9764fa2010-09-30 20:49:44 +0000700 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
701 TII.get(Opc), ResultReg)
702 .addFrameIndex(SI->second)
703 .addImm(0));
704 return ResultReg;
705 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000706
Eric Christopherf9764fa2010-09-30 20:49:44 +0000707 return 0;
708}
709
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000710bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000711 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000712
Eric Christopherb1cc8482010-08-25 07:23:49 +0000713 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000714 if (evt == MVT::Other || !evt.isSimple()) return false;
715 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000716
Eric Christopherdc908042010-08-31 01:28:42 +0000717 // Handle all legal types, i.e. a register that will directly hold this
718 // value.
719 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000720}
721
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000722bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000723 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000724
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000725 // If this is a type than can be sign or zero-extended to a basic operation
726 // go ahead and accept it now.
727 if (VT == MVT::i8 || VT == MVT::i16)
728 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000729
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000730 return false;
731}
732
Eric Christopher88de86b2010-11-19 22:36:41 +0000733// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000734bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000735 // Some boilerplate from the X86 FastISel.
736 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000737 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000738 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000739 // Don't walk into other basic blocks unless the object is an alloca from
740 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000741 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
742 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
743 Opcode = I->getOpcode();
744 U = I;
745 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000746 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000747 Opcode = C->getOpcode();
748 U = C;
749 }
750
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000751 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000752 if (Ty->getAddressSpace() > 255)
753 // Fast instruction selection doesn't support the special
754 // address spaces.
755 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000756
Eric Christopher83007122010-08-23 21:44:12 +0000757 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000758 default:
Eric Christopher83007122010-08-23 21:44:12 +0000759 break;
Eric Christopher55324332010-10-12 00:43:21 +0000760 case Instruction::BitCast: {
761 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000762 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000763 }
764 case Instruction::IntToPtr: {
765 // Look past no-op inttoptrs.
766 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000767 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000768 break;
769 }
770 case Instruction::PtrToInt: {
771 // Look past no-op ptrtoints.
772 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000773 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000774 break;
775 }
Eric Christophereae84392010-10-14 09:29:41 +0000776 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000777 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000778 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000779
Eric Christophereae84392010-10-14 09:29:41 +0000780 // Iterate through the GEP folding the constants into offsets where
781 // we can.
782 gep_type_iterator GTI = gep_type_begin(U);
783 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
784 i != e; ++i, ++GTI) {
785 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000786 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Eric Christophereae84392010-10-14 09:29:41 +0000787 const StructLayout *SL = TD.getStructLayout(STy);
788 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
789 TmpOffset += SL->getElementOffset(Idx);
790 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000791 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000792 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000793 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
794 // Constant-offset addressing.
795 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000796 break;
797 }
798 if (isa<AddOperator>(Op) &&
799 (!isa<Instruction>(Op) ||
800 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
801 == FuncInfo.MBB) &&
802 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
Eric Christopher299bbb22011-04-29 00:03:10 +0000803 // An add (in the same block) with a constant operand. Fold the
Eric Christopher7244d7c2011-03-22 19:39:17 +0000804 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000805 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000806 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000807 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000808 // Iterate on the other operand.
809 Op = cast<AddOperator>(Op)->getOperand(0);
810 continue;
Eric Christopher299bbb22011-04-29 00:03:10 +0000811 }
Eric Christopher7244d7c2011-03-22 19:39:17 +0000812 // Unsupported
813 goto unsupported_gep;
814 }
Eric Christophereae84392010-10-14 09:29:41 +0000815 }
816 }
Eric Christopher2896df82010-10-15 18:02:07 +0000817
818 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000819 Addr.Offset = TmpOffset;
820 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000821
822 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000823 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000824
Eric Christophereae84392010-10-14 09:29:41 +0000825 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000826 break;
827 }
Eric Christopher83007122010-08-23 21:44:12 +0000828 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000829 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000830 DenseMap<const AllocaInst*, int>::iterator SI =
831 FuncInfo.StaticAllocaMap.find(AI);
832 if (SI != FuncInfo.StaticAllocaMap.end()) {
833 Addr.BaseType = Address::FrameIndexBase;
834 Addr.Base.FI = SI->second;
835 return true;
836 }
837 break;
Eric Christopher83007122010-08-23 21:44:12 +0000838 }
839 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000840
Eric Christophera9c57512010-10-13 21:41:51 +0000841 // Materialize the global variable's address into a reg which can
842 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000843 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000844 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
845 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000846
Eric Christopher0d581222010-11-19 22:30:02 +0000847 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000848 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000849 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000850
Eric Christophercb0b04b2010-08-24 00:07:24 +0000851 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000852 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
853 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000854}
855
Eric Christopher0d581222010-11-19 22:30:02 +0000856void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000857
Eric Christopher212ae932010-10-21 19:40:30 +0000858 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000859
Eric Christopher212ae932010-10-21 19:40:30 +0000860 bool needsLowering = false;
861 switch (VT.getSimpleVT().SimpleTy) {
862 default:
863 assert(false && "Unhandled load/store type!");
Chad Rosier73463472011-11-09 21:30:12 +0000864 case MVT::i16:
865 if (isThumb2)
866 // Integer loads/stores handle 12-bit offsets.
867 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
868 else
869 // ARM i16 integer loads/stores handle +/-imm8 offsets.
Chad Rosier16455ce2011-11-10 21:09:49 +0000870 // FIXME: Negative offsets require special handling.
871 if (Addr.Offset > 255 || Addr.Offset < 0)
Chad Rosier73463472011-11-09 21:30:12 +0000872 needsLowering = true;
873 break;
Eric Christopher212ae932010-10-21 19:40:30 +0000874 case MVT::i1:
875 case MVT::i8:
Eric Christopher212ae932010-10-21 19:40:30 +0000876 case MVT::i32:
877 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000878 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000879 break;
880 case MVT::f32:
881 case MVT::f64:
882 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000883 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000884 break;
885 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000886
Eric Christopher827656d2010-11-20 22:38:27 +0000887 // If this is a stack pointer and the offset needs to be simplified then
888 // put the alloca address into a register, set the base type back to
889 // register and continue. This should almost never happen.
890 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000891 TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher827656d2010-11-20 22:38:27 +0000892 ARM::GPRRegisterClass;
893 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000894 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopher827656d2010-11-20 22:38:27 +0000895 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
896 TII.get(Opc), ResultReg)
897 .addFrameIndex(Addr.Base.FI)
898 .addImm(0));
899 Addr.Base.Reg = ResultReg;
900 Addr.BaseType = Address::RegBase;
901 }
902
Eric Christopher212ae932010-10-21 19:40:30 +0000903 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000904 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000905 if (needsLowering) {
Eli Friedman9ebf57a2011-04-29 21:22:56 +0000906 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
907 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopher0d581222010-11-19 22:30:02 +0000908 Addr.Offset = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000909 }
Eric Christopher83007122010-08-23 21:44:12 +0000910}
911
Eric Christopher564857f2010-12-01 01:40:24 +0000912void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000913 const MachineInstrBuilder &MIB,
914 unsigned Flags) {
Eric Christopher564857f2010-12-01 01:40:24 +0000915 // addrmode5 output depends on the selection dag addressing dividing the
916 // offset by 4 that it then later multiplies. Do this here as well.
917 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
918 VT.getSimpleVT().SimpleTy == MVT::f64)
919 Addr.Offset /= 4;
Eric Christopher299bbb22011-04-29 00:03:10 +0000920
Eric Christopher564857f2010-12-01 01:40:24 +0000921 // Frame base works a bit differently. Handle it separately.
922 if (Addr.BaseType == Address::FrameIndexBase) {
923 int FI = Addr.Base.FI;
924 int Offset = Addr.Offset;
925 MachineMemOperand *MMO =
926 FuncInfo.MF->getMachineMemOperand(
927 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000928 Flags,
Eric Christopher564857f2010-12-01 01:40:24 +0000929 MFI.getObjectSize(FI),
930 MFI.getObjectAlignment(FI));
931 // Now add the rest of the operands.
932 MIB.addFrameIndex(FI);
933
934 // ARM halfword load/stores need an additional operand.
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000935 if (!isThumb2 && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
Eric Christopher564857f2010-12-01 01:40:24 +0000936
937 MIB.addImm(Addr.Offset);
938 MIB.addMemOperand(MMO);
939 } else {
940 // Now add the rest of the operands.
941 MIB.addReg(Addr.Base.Reg);
Eric Christopher299bbb22011-04-29 00:03:10 +0000942
Eric Christopher564857f2010-12-01 01:40:24 +0000943 // ARM halfword load/stores need an additional operand.
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000944 if (!isThumb2 && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
Eric Christopher564857f2010-12-01 01:40:24 +0000945
946 MIB.addImm(Addr.Offset);
947 }
948 AddOptionalDefs(MIB);
949}
950
Eric Christopher0d581222010-11-19 22:30:02 +0000951bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000952
Eric Christopherb1cc8482010-08-25 07:23:49 +0000953 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000954 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000955 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000956 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000957 // This is mostly going to be Neon/vector support.
958 default: return false;
Chad Rosier646abbf2011-11-11 02:38:59 +0000959 case MVT::i1:
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000960 case MVT::i8:
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000961 Opc = isThumb2 ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000962 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000963 break;
Chad Rosier73463472011-11-09 21:30:12 +0000964 case MVT::i16:
965 Opc = isThumb2 ? ARM::t2LDRHi12 : ARM::LDRH;
966 RC = ARM::GPRRegisterClass;
967 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000968 case MVT::i32:
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000969 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000970 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000971 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000972 case MVT::f32:
973 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000974 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000975 break;
976 case MVT::f64:
977 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000978 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000979 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000980 }
Eric Christopher564857f2010-12-01 01:40:24 +0000981 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000982 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000983
Eric Christopher564857f2010-12-01 01:40:24 +0000984 // Create the base instruction, then add the operands.
985 ResultReg = createResultReg(RC);
986 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
987 TII.get(Opc), ResultReg);
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000988 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad);
Eric Christopherdc908042010-08-31 01:28:42 +0000989 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000990}
991
Eric Christopher43b62be2010-09-27 06:02:23 +0000992bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000993 // Atomic loads need special handling.
994 if (cast<LoadInst>(I)->isAtomic())
995 return false;
996
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000997 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000998 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000999 if (!isLoadTypeLegal(I->getType(), VT))
1000 return false;
1001
Eric Christopher564857f2010-12-01 01:40:24 +00001002 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001003 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +00001004 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001005
1006 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +00001007 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001008 UpdateValueMap(I, ResultReg);
1009 return true;
1010}
1011
Eric Christopher0d581222010-11-19 22:30:02 +00001012bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001013 unsigned StrOpc;
1014 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +00001015 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +00001016 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +00001017 case MVT::i1: {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001018 unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher4c914122010-11-02 23:59:09 +00001019 ARM::GPRRegisterClass);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001020 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eric Christopher4c914122010-11-02 23:59:09 +00001021 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1022 TII.get(Opc), Res)
1023 .addReg(SrcReg).addImm(1));
1024 SrcReg = Res;
1025 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +00001026 case MVT::i8:
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001027 StrOpc = isThumb2 ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +00001028 break;
1029 case MVT::i16:
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001030 StrOpc = isThumb2 ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +00001031 break;
Eric Christopher47650ec2010-10-16 01:10:35 +00001032 case MVT::i32:
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001033 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +00001034 break;
Eric Christopher56d2b722010-09-02 23:43:26 +00001035 case MVT::f32:
1036 if (!Subtarget->hasVFP2()) return false;
1037 StrOpc = ARM::VSTRS;
1038 break;
1039 case MVT::f64:
1040 if (!Subtarget->hasVFP2()) return false;
1041 StrOpc = ARM::VSTRD;
1042 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001043 }
Eric Christopher564857f2010-12-01 01:40:24 +00001044 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +00001045 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +00001046
Eric Christopher564857f2010-12-01 01:40:24 +00001047 // Create the base instruction, then add the operands.
1048 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1049 TII.get(StrOpc))
1050 .addReg(SrcReg, getKillRegState(true));
Cameron Zwarichc152aa62011-05-28 20:34:49 +00001051 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore);
Eric Christopher318b6ee2010-09-02 00:53:56 +00001052 return true;
1053}
1054
Eric Christopher43b62be2010-09-27 06:02:23 +00001055bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001056 Value *Op0 = I->getOperand(0);
1057 unsigned SrcReg = 0;
1058
Eli Friedman4136d232011-09-02 22:33:24 +00001059 // Atomic stores need special handling.
1060 if (cast<StoreInst>(I)->isAtomic())
1061 return false;
1062
Eric Christopher564857f2010-12-01 01:40:24 +00001063 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001064 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001065 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +00001066 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001067
Eric Christopher1b61ef42010-09-02 01:48:11 +00001068 // Get the value to be stored into a register.
1069 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +00001070 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001071
Eric Christopher564857f2010-12-01 01:40:24 +00001072 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001073 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +00001074 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +00001075 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001076
Eric Christopher0d581222010-11-19 22:30:02 +00001077 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +00001078 return true;
1079}
1080
1081static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1082 switch (Pred) {
1083 // Needs two compares...
1084 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001085 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +00001086 default:
Eric Christopher4053e632010-11-02 01:24:49 +00001087 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +00001088 return ARMCC::AL;
1089 case CmpInst::ICMP_EQ:
1090 case CmpInst::FCMP_OEQ:
1091 return ARMCC::EQ;
1092 case CmpInst::ICMP_SGT:
1093 case CmpInst::FCMP_OGT:
1094 return ARMCC::GT;
1095 case CmpInst::ICMP_SGE:
1096 case CmpInst::FCMP_OGE:
1097 return ARMCC::GE;
1098 case CmpInst::ICMP_UGT:
1099 case CmpInst::FCMP_UGT:
1100 return ARMCC::HI;
1101 case CmpInst::FCMP_OLT:
1102 return ARMCC::MI;
1103 case CmpInst::ICMP_ULE:
1104 case CmpInst::FCMP_OLE:
1105 return ARMCC::LS;
1106 case CmpInst::FCMP_ORD:
1107 return ARMCC::VC;
1108 case CmpInst::FCMP_UNO:
1109 return ARMCC::VS;
1110 case CmpInst::FCMP_UGE:
1111 return ARMCC::PL;
1112 case CmpInst::ICMP_SLT:
1113 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001114 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001115 case CmpInst::ICMP_SLE:
1116 case CmpInst::FCMP_ULE:
1117 return ARMCC::LE;
1118 case CmpInst::FCMP_UNE:
1119 case CmpInst::ICMP_NE:
1120 return ARMCC::NE;
1121 case CmpInst::ICMP_UGE:
1122 return ARMCC::HS;
1123 case CmpInst::ICMP_ULT:
1124 return ARMCC::LO;
1125 }
Eric Christopher543cf052010-09-01 22:16:27 +00001126}
1127
Eric Christopher43b62be2010-09-27 06:02:23 +00001128bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001129 const BranchInst *BI = cast<BranchInst>(I);
1130 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1131 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001132
Eric Christophere5734102010-09-03 00:35:47 +00001133 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001134
Eric Christopher0e6233b2010-10-29 21:08:19 +00001135 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1136 // behavior.
Eric Christopher0e6233b2010-10-29 21:08:19 +00001137 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosier75698f32011-10-26 23:17:28 +00001138 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001139
1140 // Get the compare predicate.
Eric Christopher632ae892011-04-29 21:56:31 +00001141 // Try to take advantage of fallthrough opportunities.
1142 CmpInst::Predicate Predicate = CI->getPredicate();
1143 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1144 std::swap(TBB, FBB);
1145 Predicate = CmpInst::getInversePredicate(Predicate);
1146 }
1147
1148 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher0e6233b2010-10-29 21:08:19 +00001149
1150 // We may not handle every CC for now.
1151 if (ARMPred == ARMCC::AL) return false;
1152
Chad Rosier75698f32011-10-26 23:17:28 +00001153 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001154 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier75698f32011-10-26 23:17:28 +00001155 return false;
Jim Grosbach16cb3762010-11-09 19:22:26 +00001156
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001157 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001158 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1159 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1160 FastEmitBranch(FBB, DL);
1161 FuncInfo.MBB->addSuccessor(TBB);
1162 return true;
1163 }
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001164 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1165 MVT SourceVT;
1166 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedman76927d732011-05-25 23:49:02 +00001167 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001168 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001169 unsigned OpReg = getRegForValue(TI->getOperand(0));
1170 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1171 TII.get(TstOpc))
1172 .addReg(OpReg).addImm(1));
1173
1174 unsigned CCMode = ARMCC::NE;
1175 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1176 std::swap(TBB, FBB);
1177 CCMode = ARMCC::EQ;
1178 }
1179
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001180 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1182 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1183
1184 FastEmitBranch(FBB, DL);
1185 FuncInfo.MBB->addSuccessor(TBB);
1186 return true;
1187 }
Chad Rosier6d64b3a2011-10-27 00:21:16 +00001188 } else if (const ConstantInt *CI =
1189 dyn_cast<ConstantInt>(BI->getCondition())) {
1190 uint64_t Imm = CI->getZExtValue();
1191 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1192 FastEmitBranch(Target, DL);
1193 return true;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001194 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001195
Eric Christopher0e6233b2010-10-29 21:08:19 +00001196 unsigned CmpReg = getRegForValue(BI->getCondition());
1197 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001198
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001199 // We've been divorced from our compare! Our block was split, and
1200 // now our compare lives in a predecessor block. We musn't
1201 // re-compare here, as the children of the compare aren't guaranteed
1202 // live across the block boundary (we *could* check for this).
1203 // Regardless, the compare has been done in the predecessor block,
1204 // and it left a value for us in a virtual register. Ergo, we test
1205 // the one-bit value left in the virtual register.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001206 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001207 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1208 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001209
Eric Christopher7a20a372011-04-28 16:52:09 +00001210 unsigned CCMode = ARMCC::NE;
1211 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1212 std::swap(TBB, FBB);
1213 CCMode = ARMCC::EQ;
1214 }
1215
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001216 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001217 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher7a20a372011-04-28 16:52:09 +00001218 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001219 FastEmitBranch(FBB, DL);
1220 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001221 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001222}
1223
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001224bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1225 bool isZExt) {
Chad Rosierade62002011-10-26 23:25:44 +00001226 Type *Ty = Src1Value->getType();
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001227 EVT SrcVT = TLI.getValueType(Ty, true);
1228 if (!SrcVT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001229
Chad Rosierade62002011-10-26 23:25:44 +00001230 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1231 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherd43393a2010-09-08 23:13:45 +00001232 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001233
Chad Rosier2f2fe412011-11-09 03:22:02 +00001234 // Check to see if the 2nd operand is a constant that we can encode directly
1235 // in the compare.
Chad Rosier1c47de82011-11-11 06:27:41 +00001236 int Imm = 0;
1237 bool UseImm = false;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001238 bool isNegativeImm = false;
1239 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1240 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1241 SrcVT == MVT::i1) {
1242 const APInt &CIVal = ConstInt->getValue();
Chad Rosier1c47de82011-11-11 06:27:41 +00001243 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1244 if (Imm < 0) {
Chad Rosier6cba97c2011-11-10 01:30:39 +00001245 isNegativeImm = true;
Chad Rosier1c47de82011-11-11 06:27:41 +00001246 Imm = -Imm;
Chad Rosier6cba97c2011-11-10 01:30:39 +00001247 }
Chad Rosier1c47de82011-11-11 06:27:41 +00001248 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1249 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001250 }
1251 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1252 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1253 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosier1c47de82011-11-11 06:27:41 +00001254 UseImm = true;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001255 }
1256
Eric Christopherd43393a2010-09-08 23:13:45 +00001257 unsigned CmpOpc;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001258 bool isICmp = true;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001259 bool needsExt = false;
1260 switch (SrcVT.getSimpleVT().SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001261 default: return false;
1262 // TODO: Verify compares.
1263 case MVT::f32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001264 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001265 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherd43393a2010-09-08 23:13:45 +00001266 break;
1267 case MVT::f64:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001268 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001269 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherd43393a2010-09-08 23:13:45 +00001270 break;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001271 case MVT::i1:
1272 case MVT::i8:
1273 case MVT::i16:
1274 needsExt = true;
1275 // Intentional fall-through.
Eric Christopherd43393a2010-09-08 23:13:45 +00001276 case MVT::i32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001277 if (isThumb2) {
Chad Rosier1c47de82011-11-11 06:27:41 +00001278 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001279 CmpOpc = ARM::t2CMPrr;
1280 else
1281 CmpOpc = isNegativeImm ? ARM::t2CMNzri : ARM::t2CMPri;
1282 } else {
Chad Rosier1c47de82011-11-11 06:27:41 +00001283 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001284 CmpOpc = ARM::CMPrr;
1285 else
1286 CmpOpc = isNegativeImm ? ARM::CMNzri : ARM::CMPri;
1287 }
Eric Christopherd43393a2010-09-08 23:13:45 +00001288 break;
1289 }
1290
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001291 unsigned SrcReg1 = getRegForValue(Src1Value);
1292 if (SrcReg1 == 0) return false;
Chad Rosier530f7ce2011-10-26 22:47:55 +00001293
Chad Rosier2f2fe412011-11-09 03:22:02 +00001294 unsigned SrcReg2;
Chad Rosier1c47de82011-11-11 06:27:41 +00001295 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001296 SrcReg2 = getRegForValue(Src2Value);
1297 if (SrcReg2 == 0) return false;
1298 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001299
1300 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1301 if (needsExt) {
1302 unsigned ResultReg;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001303 ResultReg = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001304 if (ResultReg == 0) return false;
1305 SrcReg1 = ResultReg;
Chad Rosier1c47de82011-11-11 06:27:41 +00001306 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001307 ResultReg = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1308 if (ResultReg == 0) return false;
1309 SrcReg2 = ResultReg;
1310 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001311 }
Chad Rosier530f7ce2011-10-26 22:47:55 +00001312
Chad Rosier1c47de82011-11-11 06:27:41 +00001313 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001314 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1315 TII.get(CmpOpc))
1316 .addReg(SrcReg1).addReg(SrcReg2));
1317 } else {
1318 MachineInstrBuilder MIB;
1319 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1320 .addReg(SrcReg1);
1321
1322 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1323 if (isICmp)
Chad Rosier1c47de82011-11-11 06:27:41 +00001324 MIB.addImm(Imm);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001325 AddOptionalDefs(MIB);
1326 }
Chad Rosierade62002011-10-26 23:25:44 +00001327
1328 // For floating point we need to move the result to a comparison register
1329 // that we can then use for branches.
1330 if (Ty->isFloatTy() || Ty->isDoubleTy())
1331 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1332 TII.get(ARM::FMSTAT)));
Chad Rosier530f7ce2011-10-26 22:47:55 +00001333 return true;
1334}
1335
1336bool ARMFastISel::SelectCmp(const Instruction *I) {
1337 const CmpInst *CI = cast<CmpInst>(I);
Chad Rosierade62002011-10-26 23:25:44 +00001338 Type *Ty = CI->getOperand(0)->getType();
Chad Rosier530f7ce2011-10-26 22:47:55 +00001339
Eric Christopher229207a2010-09-29 01:14:47 +00001340 // Get the compare predicate.
1341 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001342
Eric Christopher229207a2010-09-29 01:14:47 +00001343 // We may not handle every CC for now.
1344 if (ARMPred == ARMCC::AL) return false;
1345
Chad Rosier530f7ce2011-10-26 22:47:55 +00001346 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001347 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier530f7ce2011-10-26 22:47:55 +00001348 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001349
Eric Christopher229207a2010-09-29 01:14:47 +00001350 // Now set a register based on the comparison. Explicitly set the predicates
1351 // here.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001352 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1353 TargetRegisterClass *RC = isThumb2 ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001354 : ARM::GPRRegisterClass;
1355 unsigned DestReg = createResultReg(RC);
Chad Rosierade62002011-10-26 23:25:44 +00001356 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001357 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosierade62002011-10-26 23:25:44 +00001358 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
Chad Rosier530f7ce2011-10-26 22:47:55 +00001359 unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR;
Eric Christopher229207a2010-09-29 01:14:47 +00001360 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1361 .addReg(ZeroReg).addImm(1)
1362 .addImm(ARMPred).addReg(CondReg);
1363
Eric Christophera5b1e682010-09-17 22:28:18 +00001364 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001365 return true;
1366}
1367
Eric Christopher43b62be2010-09-27 06:02:23 +00001368bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001369 // Make sure we have VFP and that we're extending float to double.
1370 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001371
Eric Christopher46203602010-09-09 00:26:48 +00001372 Value *V = I->getOperand(0);
1373 if (!I->getType()->isDoubleTy() ||
1374 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001375
Eric Christopher46203602010-09-09 00:26:48 +00001376 unsigned Op = getRegForValue(V);
1377 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001378
Eric Christopher46203602010-09-09 00:26:48 +00001379 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001380 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001381 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001382 .addReg(Op));
1383 UpdateValueMap(I, Result);
1384 return true;
1385}
1386
Eric Christopher43b62be2010-09-27 06:02:23 +00001387bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001388 // Make sure we have VFP and that we're truncating double to float.
1389 if (!Subtarget->hasVFP2()) return false;
1390
1391 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001392 if (!(I->getType()->isFloatTy() &&
1393 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001394
1395 unsigned Op = getRegForValue(V);
1396 if (Op == 0) return false;
1397
1398 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001400 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001401 .addReg(Op));
1402 UpdateValueMap(I, Result);
1403 return true;
1404}
1405
Eric Christopher43b62be2010-09-27 06:02:23 +00001406bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001407 // Make sure we have VFP.
1408 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001409
Duncan Sands1440e8b2010-11-03 11:35:31 +00001410 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001411 Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001412 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001413 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001414
Chad Rosier463fe242011-11-03 02:04:59 +00001415 Value *Src = I->getOperand(0);
1416 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1417 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman783c6642011-05-25 19:09:45 +00001418 return false;
1419
Chad Rosier463fe242011-11-03 02:04:59 +00001420 unsigned SrcReg = getRegForValue(Src);
1421 if (SrcReg == 0) return false;
1422
1423 // Handle sign-extension.
1424 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1425 EVT DestVT = MVT::i32;
1426 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, /*isZExt*/ false);
1427 if (ResultReg == 0) return false;
1428 SrcReg = ResultReg;
1429 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001430
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001431 // The conversion routine works on fp-reg to fp-reg and the operand above
1432 // was an integer, move it to the fp registers if possible.
Chad Rosier463fe242011-11-03 02:04:59 +00001433 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001434 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001435
Eric Christopher9a040492010-09-09 18:54:59 +00001436 unsigned Opc;
1437 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1438 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001439 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001440
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001441 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001442 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1443 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001444 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001445 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001446 return true;
1447}
1448
Eric Christopher43b62be2010-09-27 06:02:23 +00001449bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001450 // Make sure we have VFP.
1451 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001452
Duncan Sands1440e8b2010-11-03 11:35:31 +00001453 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001454 Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001455 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001456 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001457
Eric Christopher9a040492010-09-09 18:54:59 +00001458 unsigned Op = getRegForValue(I->getOperand(0));
1459 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001460
Eric Christopher9a040492010-09-09 18:54:59 +00001461 unsigned Opc;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001462 Type *OpTy = I->getOperand(0)->getType();
Eric Christopher9a040492010-09-09 18:54:59 +00001463 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1464 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001465 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001466
Eric Christopher022b7fb2010-10-05 23:13:24 +00001467 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1468 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001469 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1470 ResultReg)
1471 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001472
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001473 // This result needs to be in an integer register, but the conversion only
1474 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001475 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001476 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001477
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001478 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001479 return true;
1480}
1481
Eric Christopher3bbd3962010-10-11 08:27:59 +00001482bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001483 MVT VT;
1484 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001485 return false;
1486
1487 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001488 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001489 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1490
1491 unsigned CondReg = getRegForValue(I->getOperand(0));
1492 if (CondReg == 0) return false;
1493 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1494 if (Op1Reg == 0) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001495
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001496 // Check to see if we can use an immediate in the conditional move.
1497 int Imm = 0;
1498 bool UseImm = false;
1499 bool isNegativeImm = false;
1500 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1501 assert (VT == MVT::i32 && "Expecting an i32.");
1502 Imm = (int)ConstInt->getValue().getZExtValue();
1503 if (Imm < 0) {
1504 isNegativeImm = true;
1505 Imm = ~Imm;
1506 }
1507 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1508 (ARM_AM::getSOImmVal(Imm) != -1);
1509 }
1510
1511 unsigned Op2Reg;
1512 if (!UseImm) {
1513 Op2Reg = getRegForValue(I->getOperand(2));
1514 if (Op2Reg == 0) return false;
1515 }
1516
1517 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001518 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001519 .addReg(CondReg).addImm(0));
1520
1521 unsigned MovCCOpc;
1522 if (!UseImm) {
1523 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1524 } else {
1525 if (!isNegativeImm) {
1526 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1527 } else {
1528 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1529 }
1530 }
Eric Christopher3bbd3962010-10-11 08:27:59 +00001531 unsigned ResultReg = createResultReg(RC);
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001532 if (!UseImm)
1533 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1534 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1535 else
1536 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1537 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001538 UpdateValueMap(I, ResultReg);
1539 return true;
1540}
1541
Eric Christopher08637852010-09-30 22:34:19 +00001542bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001543 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001544 Type *Ty = I->getType();
Eric Christopher08637852010-09-30 22:34:19 +00001545 if (!isTypeLegal(Ty, VT))
1546 return false;
1547
1548 // If we have integer div support we should have selected this automagically.
1549 // In case we have a real miss go ahead and return false and we'll pick
1550 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001551 if (Subtarget->hasDivide()) return false;
1552
Eric Christopher08637852010-09-30 22:34:19 +00001553 // Otherwise emit a libcall.
1554 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001555 if (VT == MVT::i8)
1556 LC = RTLIB::SDIV_I8;
1557 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001558 LC = RTLIB::SDIV_I16;
1559 else if (VT == MVT::i32)
1560 LC = RTLIB::SDIV_I32;
1561 else if (VT == MVT::i64)
1562 LC = RTLIB::SDIV_I64;
1563 else if (VT == MVT::i128)
1564 LC = RTLIB::SDIV_I128;
1565 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001566
Eric Christopher08637852010-09-30 22:34:19 +00001567 return ARMEmitLibcall(I, LC);
1568}
1569
Eric Christopher6a880d62010-10-11 08:37:26 +00001570bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001571 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001572 Type *Ty = I->getType();
Eric Christopher6a880d62010-10-11 08:37:26 +00001573 if (!isTypeLegal(Ty, VT))
1574 return false;
1575
1576 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1577 if (VT == MVT::i8)
1578 LC = RTLIB::SREM_I8;
1579 else if (VT == MVT::i16)
1580 LC = RTLIB::SREM_I16;
1581 else if (VT == MVT::i32)
1582 LC = RTLIB::SREM_I32;
1583 else if (VT == MVT::i64)
1584 LC = RTLIB::SREM_I64;
1585 else if (VT == MVT::i128)
1586 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001587 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001588
Eric Christopher6a880d62010-10-11 08:37:26 +00001589 return ARMEmitLibcall(I, LC);
1590}
1591
Eric Christopher43b62be2010-09-27 06:02:23 +00001592bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001593 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001594
Eric Christopherbc39b822010-09-09 00:53:57 +00001595 // We can get here in the case when we want to use NEON for our fp
1596 // operations, but can't figure out how to. Just use the vfp instructions
1597 // if we have them.
1598 // FIXME: It'd be nice to use NEON instructions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001599 Type *Ty = I->getType();
Eric Christopherbd6bf082010-09-09 01:02:03 +00001600 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1601 if (isFloat && !Subtarget->hasVFP2())
1602 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001603
Eric Christopherbc39b822010-09-09 00:53:57 +00001604 unsigned Op1 = getRegForValue(I->getOperand(0));
1605 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001606
Eric Christopherbc39b822010-09-09 00:53:57 +00001607 unsigned Op2 = getRegForValue(I->getOperand(1));
1608 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001609
Eric Christopherbc39b822010-09-09 00:53:57 +00001610 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001611 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001612 switch (ISDOpcode) {
1613 default: return false;
1614 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001615 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001616 break;
1617 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001618 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001619 break;
1620 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001621 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001622 break;
1623 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001624 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001625 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1626 TII.get(Opc), ResultReg)
1627 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001628 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001629 return true;
1630}
1631
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001632// Call Handling Code
1633
Eric Christopherfa87d662010-10-18 02:17:53 +00001634bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1635 EVT SrcVT, unsigned &ResultReg) {
1636 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1637 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001638
Eric Christopherfa87d662010-10-18 02:17:53 +00001639 if (RR != 0) {
1640 ResultReg = RR;
1641 return true;
1642 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001643 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001644}
1645
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001646// This is largely taken directly from CCAssignFnForNode - we don't support
1647// varargs in FastISel so that part has been removed.
1648// TODO: We may not support all of this.
1649CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1650 switch (CC) {
1651 default:
1652 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001653 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001654 // Ignore fastcc. Silence compiler warnings.
1655 (void)RetFastCC_ARM_APCS;
1656 (void)FastCC_ARM_APCS;
1657 // Fallthrough
1658 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001659 // Use target triple & subtarget features to do actual dispatch.
1660 if (Subtarget->isAAPCS_ABI()) {
1661 if (Subtarget->hasVFP2() &&
1662 FloatABIType == FloatABI::Hard)
1663 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1664 else
1665 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1666 } else
1667 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1668 case CallingConv::ARM_AAPCS_VFP:
1669 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1670 case CallingConv::ARM_AAPCS:
1671 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1672 case CallingConv::ARM_APCS:
1673 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1674 }
1675}
1676
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001677bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1678 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001679 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001680 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1681 SmallVectorImpl<unsigned> &RegArgs,
1682 CallingConv::ID CC,
1683 unsigned &NumBytes) {
1684 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001685 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001686 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1687
1688 // Get a count of how many bytes are to be pushed on the stack.
1689 NumBytes = CCInfo.getNextStackOffset();
1690
1691 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001692 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001693 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1694 TII.get(AdjStackDown))
1695 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001696
1697 // Process the args.
1698 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1699 CCValAssign &VA = ArgLocs[i];
1700 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001701 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001702
Eric Christopher4a2b3162011-01-27 05:44:56 +00001703 // We don't handle NEON/vector parameters yet.
1704 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001705 return false;
1706
Eric Christopherf9764fa2010-09-30 20:49:44 +00001707 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001708 switch (VA.getLocInfo()) {
1709 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001710 case CCValAssign::SExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001711 EVT DestVT = VA.getLocVT();
1712 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1713 /*isZExt*/false);
1714 assert (ResultReg != 0 && "Failed to emit a sext");
1715 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001716 break;
1717 }
Chad Rosier42536af2011-11-05 20:16:15 +00001718 case CCValAssign::AExt:
1719 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherfa87d662010-10-18 02:17:53 +00001720 case CCValAssign::ZExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001721 EVT DestVT = VA.getLocVT();
1722 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1723 /*isZExt*/true);
1724 assert (ResultReg != 0 && "Failed to emit a sext");
1725 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001726 break;
1727 }
1728 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001729 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001730 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001731 assert(BC != 0 && "Failed to emit a bitcast!");
1732 Arg = BC;
1733 ArgVT = VA.getLocVT();
1734 break;
1735 }
1736 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001737 }
1738
1739 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001740 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001741 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001742 VA.getLocReg())
Chad Rosier42536af2011-11-05 20:16:15 +00001743 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001744 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001745 } else if (VA.needsCustom()) {
1746 // TODO: We need custom lowering for vector (v2f64) args.
1747 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001748
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001749 CCValAssign &NextVA = ArgLocs[++i];
1750
1751 // TODO: Only handle register args for now.
1752 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1753
1754 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1755 TII.get(ARM::VMOVRRD), VA.getLocReg())
1756 .addReg(NextVA.getLocReg(), RegState::Define)
1757 .addReg(Arg));
1758 RegArgs.push_back(VA.getLocReg());
1759 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001760 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001761 assert(VA.isMemLoc());
1762 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001763 Address Addr;
1764 Addr.BaseType = Address::RegBase;
1765 Addr.Base.Reg = ARM::SP;
1766 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001767
Eric Christopher0d581222010-11-19 22:30:02 +00001768 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001769 }
1770 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001771 return true;
1772}
1773
Duncan Sands1440e8b2010-11-03 11:35:31 +00001774bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001775 const Instruction *I, CallingConv::ID CC,
1776 unsigned &NumBytes) {
1777 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001778 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001779 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1780 TII.get(AdjStackUp))
1781 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001782
1783 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001784 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001785 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001786 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001787 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1788
1789 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001790 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001791 // For this move we copy into two registers and then move into the
1792 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001793 EVT DestVT = RVLocs[0].getValVT();
1794 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1795 unsigned ResultReg = createResultReg(DstRC);
1796 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1797 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001798 .addReg(RVLocs[0].getLocReg())
1799 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001800
Eric Christopher3659ac22010-10-20 08:02:24 +00001801 UsedRegs.push_back(RVLocs[0].getLocReg());
1802 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001803
Eric Christopherdccd2c32010-10-11 08:38:55 +00001804 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001805 UpdateValueMap(I, ResultReg);
1806 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001807 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001808 EVT CopyVT = RVLocs[0].getValVT();
Chad Rosier0eff39f2011-11-08 00:03:32 +00001809
1810 // Special handling for extended integers.
1811 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1812 CopyVT = MVT::i32;
1813
Eric Christopher14df8822010-10-01 00:00:11 +00001814 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001815
Eric Christopher14df8822010-10-01 00:00:11 +00001816 unsigned ResultReg = createResultReg(DstRC);
1817 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1818 ResultReg).addReg(RVLocs[0].getLocReg());
1819 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001820
Eric Christopherdccd2c32010-10-11 08:38:55 +00001821 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001822 UpdateValueMap(I, ResultReg);
1823 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001824 }
1825
Eric Christopherdccd2c32010-10-11 08:38:55 +00001826 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001827}
1828
Eric Christopher4f512ef2010-10-22 01:28:00 +00001829bool ARMFastISel::SelectRet(const Instruction *I) {
1830 const ReturnInst *Ret = cast<ReturnInst>(I);
1831 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001832
Eric Christopher4f512ef2010-10-22 01:28:00 +00001833 if (!FuncInfo.CanLowerReturn)
1834 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001835
Eric Christopher4f512ef2010-10-22 01:28:00 +00001836 if (F.isVarArg())
1837 return false;
1838
1839 CallingConv::ID CC = F.getCallingConv();
1840 if (Ret->getNumOperands() > 0) {
1841 SmallVector<ISD::OutputArg, 4> Outs;
1842 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1843 Outs, TLI);
1844
1845 // Analyze operands of the call, assigning locations to each operand.
1846 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbachb04546f2011-09-13 20:30:37 +00001847 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Eric Christopher4f512ef2010-10-22 01:28:00 +00001848 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1849
1850 const Value *RV = Ret->getOperand(0);
1851 unsigned Reg = getRegForValue(RV);
1852 if (Reg == 0)
1853 return false;
1854
1855 // Only handle a single return value for now.
1856 if (ValLocs.size() != 1)
1857 return false;
1858
1859 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001860
Eric Christopher4f512ef2010-10-22 01:28:00 +00001861 // Don't bother handling odd stuff for now.
1862 if (VA.getLocInfo() != CCValAssign::Full)
1863 return false;
1864 // Only handle register returns for now.
1865 if (!VA.isRegLoc())
1866 return false;
Chad Rosierf470cbb2011-11-04 00:50:21 +00001867
1868 unsigned SrcReg = Reg + VA.getValNo();
1869 EVT RVVT = TLI.getValueType(RV->getType());
1870 EVT DestVT = VA.getValVT();
1871 // Special handling for extended integers.
1872 if (RVVT != DestVT) {
1873 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1874 return false;
1875
1876 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1877 return false;
1878
1879 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
1880
1881 bool isZExt = Outs[0].Flags.isZExt();
1882 unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1883 if (ResultReg == 0) return false;
1884 SrcReg = ResultReg;
1885 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001886
Eric Christopher4f512ef2010-10-22 01:28:00 +00001887 // Make the copy.
Eric Christopher4f512ef2010-10-22 01:28:00 +00001888 unsigned DstReg = VA.getLocReg();
1889 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1890 // Avoid a cross-class copy. This is very unlikely.
1891 if (!SrcRC->contains(DstReg))
1892 return false;
1893 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1894 DstReg).addReg(SrcReg);
1895
1896 // Mark the register as live out of the function.
1897 MRI.addLiveOut(VA.getLocReg());
1898 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001899
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001900 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Eric Christopher4f512ef2010-10-22 01:28:00 +00001901 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1902 TII.get(RetOpc)));
1903 return true;
1904}
1905
Eric Christopher872f4a22011-02-22 01:37:10 +00001906unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1907
Eric Christopher872f4a22011-02-22 01:37:10 +00001908 // Darwin needs the r9 versions of the opcodes.
1909 bool isDarwin = Subtarget->isTargetDarwin();
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001910 if (isThumb2) {
Eric Christopher872f4a22011-02-22 01:37:10 +00001911 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1912 } else {
1913 return isDarwin ? ARM::BLr9 : ARM::BL;
1914 }
1915}
1916
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001917// A quick function that will emit a call for a named libcall in F with the
1918// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001919// can emit a call for any libcall we can produce. This is an abridged version
1920// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001921// like computed function pointers or strange arguments at call sites.
1922// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1923// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001924bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1925 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001926
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001927 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001928 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001929 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001930 if (RetTy->isVoidTy())
1931 RetVT = MVT::isVoid;
1932 else if (!isTypeLegal(RetTy, RetVT))
1933 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001934
Eric Christopher836c6242010-12-15 23:47:29 +00001935 // TODO: For now if we have long calls specified we don't handle the call.
1936 if (EnableARMLongCalls) return false;
1937
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001938 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001939 SmallVector<Value*, 8> Args;
1940 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001941 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001942 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1943 Args.reserve(I->getNumOperands());
1944 ArgRegs.reserve(I->getNumOperands());
1945 ArgVTs.reserve(I->getNumOperands());
1946 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001947 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001948 Value *Op = I->getOperand(i);
1949 unsigned Arg = getRegForValue(Op);
1950 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001951
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001952 Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001953 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001954 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001955
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001956 ISD::ArgFlagsTy Flags;
1957 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1958 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001959
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001960 Args.push_back(Op);
1961 ArgRegs.push_back(Arg);
1962 ArgVTs.push_back(ArgVT);
1963 ArgFlags.push_back(Flags);
1964 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001965
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001966 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001967 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001968 unsigned NumBytes;
1969 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1970 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001971
Eric Christopher6344a5f2011-04-29 00:07:20 +00001972 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001973 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001974 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001975 unsigned CallOpc = ARMSelectCallOp(NULL);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001976 if(isThumb2)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001977 // Explicitly adding the predicate here.
1978 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1979 TII.get(CallOpc)))
1980 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00001981 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001982 // Explicitly adding the predicate here.
1983 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1984 TII.get(CallOpc))
1985 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001986
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001987 // Add implicit physical register uses to the call.
1988 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1989 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001990
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001991 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001992 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001993 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001994
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001995 // Set all unused physreg defs as dead.
1996 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001997
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001998 return true;
1999}
2000
Chad Rosier11add262011-11-11 23:31:03 +00002001bool ARMFastISel::SelectCall(const Instruction *I,
2002 const char *IntrMemName = 0) {
Eric Christopherf9764fa2010-09-30 20:49:44 +00002003 const CallInst *CI = cast<CallInst>(I);
2004 const Value *Callee = CI->getCalledValue();
2005
Chad Rosier11add262011-11-11 23:31:03 +00002006 // Can't handle inline asm.
2007 if (isa<InlineAsm>(Callee)) return false;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002008
Eric Christopher52f6c032011-05-02 20:16:33 +00002009 // Only handle global variable Callees.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002010 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christopher52f6c032011-05-02 20:16:33 +00002011 if (!GV)
Eric Christophere6ca6772010-10-01 21:33:12 +00002012 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002013
Eric Christopherf9764fa2010-09-30 20:49:44 +00002014 // Check the calling convention.
2015 ImmutableCallSite CS(CI);
2016 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00002017
Eric Christopherf9764fa2010-09-30 20:49:44 +00002018 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00002019
Eric Christopherf9764fa2010-09-30 20:49:44 +00002020 // Let SDISel handle vararg functions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002021 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2022 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eric Christopherf9764fa2010-09-30 20:49:44 +00002023 if (FTy->isVarArg())
2024 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002025
Eric Christopherf9764fa2010-09-30 20:49:44 +00002026 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002027 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002028 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002029 if (RetTy->isVoidTy())
2030 RetVT = MVT::isVoid;
Chad Rosier0eff39f2011-11-08 00:03:32 +00002031 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2032 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002033 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002034
Eric Christopher836c6242010-12-15 23:47:29 +00002035 // TODO: For now if we have long calls specified we don't handle the call.
2036 if (EnableARMLongCalls) return false;
Eric Christopher299bbb22011-04-29 00:03:10 +00002037
Eric Christopherf9764fa2010-09-30 20:49:44 +00002038 // Set up the argument vectors.
2039 SmallVector<Value*, 8> Args;
2040 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002041 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002042 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2043 Args.reserve(CS.arg_size());
2044 ArgRegs.reserve(CS.arg_size());
2045 ArgVTs.reserve(CS.arg_size());
2046 ArgFlags.reserve(CS.arg_size());
2047 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2048 i != e; ++i) {
Chad Rosier11add262011-11-11 23:31:03 +00002049 // If we're lowering a memory intrinsic instead of a regular call, skip the
2050 // last two arguments, which shouldn't be passed to the underlying function.
2051 if (IntrMemName && e-i <= 2)
2052 break;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002053
Chad Rosier11add262011-11-11 23:31:03 +00002054 unsigned Arg = getRegForValue(*i);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002055 if (Arg == 0)
2056 return false;
2057 ISD::ArgFlagsTy Flags;
2058 unsigned AttrInd = i - CS.arg_begin() + 1;
2059 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2060 Flags.setSExt();
2061 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2062 Flags.setZExt();
2063
Chad Rosier8e4a2e42011-11-04 00:58:10 +00002064 // FIXME: Only handle *easy* calls for now.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002065 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2066 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2067 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2068 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2069 return false;
2070
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002071 Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002072 MVT ArgVT;
Chad Rosier42536af2011-11-05 20:16:15 +00002073 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2074 ArgVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002075 return false;
2076 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2077 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002078
Eric Christopherf9764fa2010-09-30 20:49:44 +00002079 Args.push_back(*i);
2080 ArgRegs.push_back(Arg);
2081 ArgVTs.push_back(ArgVT);
2082 ArgFlags.push_back(Flags);
2083 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002084
Eric Christopherf9764fa2010-09-30 20:49:44 +00002085 // Handle the arguments now that we've gotten them.
2086 SmallVector<unsigned, 4> RegArgs;
2087 unsigned NumBytes;
2088 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2089 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002090
Eric Christopher6344a5f2011-04-29 00:07:20 +00002091 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002092 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002093 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00002094 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00002095 // Explicitly adding the predicate here.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002096 if(isThumb2)
Eric Christopherc19aadb2010-12-21 03:50:43 +00002097 // Explicitly adding the predicate here.
2098 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier11add262011-11-11 23:31:03 +00002099 TII.get(CallOpc)));
Eric Christopher872f4a22011-02-22 01:37:10 +00002100 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00002101 // Explicitly adding the predicate here.
2102 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier11add262011-11-11 23:31:03 +00002103 TII.get(CallOpc)));
2104 if (!IntrMemName)
2105 MIB.addGlobalAddress(GV, 0, 0);
2106 else
2107 MIB.addExternalSymbol(IntrMemName, 0);
2108
Eric Christopherf9764fa2010-09-30 20:49:44 +00002109 // Add implicit physical register uses to the call.
2110 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2111 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002112
Eric Christopherf9764fa2010-09-30 20:49:44 +00002113 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002114 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002115 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002116
Eric Christopherf9764fa2010-09-30 20:49:44 +00002117 // Set all unused physreg defs as dead.
2118 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002119
Eric Christopherf9764fa2010-09-30 20:49:44 +00002120 return true;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002121}
2122
Chad Rosier11add262011-11-11 23:31:03 +00002123bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2124 // FIXME: Handle more intrinsics.
2125 switch (I.getIntrinsicID()) {
2126 default: return false;
2127 case Intrinsic::memcpy:
2128 case Intrinsic::memmove: {
2129 // FIXME: Small memcpy/memmove's are common enough that we want to do them
2130 // without a call if possible.
2131 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2132 // Don't handle volatile.
2133 if (MTI.isVolatile())
2134 return false;
2135
2136 if (!MTI.getLength()->getType()->isIntegerTy(32))
2137 return false;
2138
2139 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2140 return false;
2141
2142 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2143 return SelectCall(&I, IntrMemName);
2144 }
2145 case Intrinsic::memset: {
2146 const MemSetInst &MSI = cast<MemSetInst>(I);
2147 // Don't handle volatile.
2148 if (MSI.isVolatile())
2149 return false;
2150
2151 if (!MSI.getLength()->getType()->isIntegerTy(32))
2152 return false;
2153
2154 if (MSI.getDestAddressSpace() > 255)
2155 return false;
2156
2157 return SelectCall(&I, "memset");
2158 }
2159 }
2160 return false;
2161}
2162
Chad Rosier0d7b2312011-11-02 00:18:48 +00002163bool ARMFastISel::SelectTrunc(const Instruction *I) {
2164 // The high bits for a type smaller than the register size are assumed to be
2165 // undefined.
2166 Value *Op = I->getOperand(0);
2167
2168 EVT SrcVT, DestVT;
2169 SrcVT = TLI.getValueType(Op->getType(), true);
2170 DestVT = TLI.getValueType(I->getType(), true);
2171
2172 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2173 return false;
2174 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2175 return false;
2176
2177 unsigned SrcReg = getRegForValue(Op);
2178 if (!SrcReg) return false;
2179
2180 // Because the high bits are undefined, a truncate doesn't generate
2181 // any code.
2182 UpdateValueMap(I, SrcReg);
2183 return true;
2184}
2185
Chad Rosier87633022011-11-02 17:20:24 +00002186unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2187 bool isZExt) {
Eli Friedman76927d732011-05-25 23:49:02 +00002188 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier87633022011-11-02 17:20:24 +00002189 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002190
2191 unsigned Opc;
Eli Friedman76927d732011-05-25 23:49:02 +00002192 bool isBoolZext = false;
Chad Rosier87633022011-11-02 17:20:24 +00002193 if (!SrcVT.isSimple()) return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002194 switch (SrcVT.getSimpleVT().SimpleTy) {
Chad Rosier87633022011-11-02 17:20:24 +00002195 default: return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002196 case MVT::i16:
Chad Rosier87633022011-11-02 17:20:24 +00002197 if (!Subtarget->hasV6Ops()) return 0;
2198 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002199 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002200 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002201 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002202 break;
2203 case MVT::i8:
Chad Rosier87633022011-11-02 17:20:24 +00002204 if (!Subtarget->hasV6Ops()) return 0;
2205 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002206 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002207 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002208 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002209 break;
2210 case MVT::i1:
Chad Rosier87633022011-11-02 17:20:24 +00002211 if (isZExt) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002212 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eli Friedman76927d732011-05-25 23:49:02 +00002213 isBoolZext = true;
2214 break;
2215 }
Chad Rosier87633022011-11-02 17:20:24 +00002216 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002217 }
2218
Chad Rosier87633022011-11-02 17:20:24 +00002219 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eli Friedman76927d732011-05-25 23:49:02 +00002220 MachineInstrBuilder MIB;
Chad Rosier87633022011-11-02 17:20:24 +00002221 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
Eli Friedman76927d732011-05-25 23:49:02 +00002222 .addReg(SrcReg);
2223 if (isBoolZext)
2224 MIB.addImm(1);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002225 else
2226 MIB.addImm(0);
Eli Friedman76927d732011-05-25 23:49:02 +00002227 AddOptionalDefs(MIB);
Chad Rosier87633022011-11-02 17:20:24 +00002228 return ResultReg;
2229}
2230
2231bool ARMFastISel::SelectIntExt(const Instruction *I) {
2232 // On ARM, in general, integer casts don't involve legal types; this code
2233 // handles promotable integers.
2234 // FIXME: We could save an instruction in many cases by special-casing
2235 // load instructions.
2236 Type *DestTy = I->getType();
2237 Value *Src = I->getOperand(0);
2238 Type *SrcTy = Src->getType();
2239
2240 EVT SrcVT, DestVT;
2241 SrcVT = TLI.getValueType(SrcTy, true);
2242 DestVT = TLI.getValueType(DestTy, true);
2243
2244 bool isZExt = isa<ZExtInst>(I);
2245 unsigned SrcReg = getRegForValue(Src);
2246 if (!SrcReg) return false;
2247
2248 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2249 if (ResultReg == 0) return false;
2250 UpdateValueMap(I, ResultReg);
Eli Friedman76927d732011-05-25 23:49:02 +00002251 return true;
2252}
2253
Eric Christopher56d2b722010-09-02 23:43:26 +00002254// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00002255bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00002256
Eric Christopherab695882010-07-21 22:26:11 +00002257 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00002258 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00002259 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00002260 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00002261 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00002262 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00002263 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00002264 case Instruction::ICmp:
2265 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00002266 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00002267 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00002268 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00002269 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00002270 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002271 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00002272 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002273 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00002274 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00002275 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00002276 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00002277 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00002278 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00002279 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00002280 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002281 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00002282 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00002283 case Instruction::SRem:
2284 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002285 case Instruction::Call:
Chad Rosier11add262011-11-11 23:31:03 +00002286 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2287 return SelectIntrinsicCall(*II);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002288 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00002289 case Instruction::Select:
2290 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00002291 case Instruction::Ret:
2292 return SelectRet(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002293 case Instruction::Trunc:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002294 return SelectTrunc(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002295 case Instruction::ZExt:
2296 case Instruction::SExt:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002297 return SelectIntExt(I);
Eric Christopherab695882010-07-21 22:26:11 +00002298 default: break;
2299 }
2300 return false;
2301}
2302
2303namespace llvm {
2304 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00002305 // Completely untested on non-darwin.
2306 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002307
Eric Christopheraaa8df42010-11-02 01:21:28 +00002308 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00002309 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002310 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00002311 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00002312 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00002313 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00002314 }
2315}