blob: 2af42c90af159b87fcfa2de092df50c882288c9e [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"
Eric Christopherab695882010-07-21 22:26:11 +000023#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/Instructions.h"
27#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000028#include "llvm/Module.h"
Eric Christopherab695882010-07-21 22:26:11 +000029#include "llvm/CodeGen/Analysis.h"
30#include "llvm/CodeGen/FastISel.h"
31#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
33#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000034#include "llvm/CodeGen/MachineConstantPool.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000036#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000040#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000043#include "llvm/Target/TargetData.h"
44#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetLowering.h"
46#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000047#include "llvm/Target/TargetOptions.h"
48using namespace llvm;
49
Eric Christopher038fea52010-08-17 00:46:57 +000050static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000051DisableARMFastISel("disable-arm-fast-isel",
52 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000053 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000054
Eric Christopher836c6242010-12-15 23:47:29 +000055extern cl::opt<bool> EnableARMLongCalls;
56
Eric Christopherab695882010-07-21 22:26:11 +000057namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000058
Eric Christopher0d581222010-11-19 22:30:02 +000059 // All possible address modes, plus some.
60 typedef struct Address {
61 enum {
62 RegBase,
63 FrameIndexBase
64 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000065
Eric Christopher0d581222010-11-19 22:30:02 +000066 union {
67 unsigned Reg;
68 int FI;
69 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000070
Eric Christopher0d581222010-11-19 22:30:02 +000071 int Offset;
72 unsigned Scale;
73 unsigned PlusReg;
Eric Christopher827656d2010-11-20 22:38:27 +000074
Eric Christopher0d581222010-11-19 22:30:02 +000075 // Innocuous defaults for our address.
76 Address()
77 : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) {
78 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.
Eric Christophereaa204b2010-09-02 01:39:14 +000093 bool isThumb;
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>();
Eric Christophereaa204b2010-09-02 01:39:14 +0000104 isThumb = 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);
118 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
121 uint64_t Imm);
122 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
125 const ConstantFP *FPImm);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000126 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 unsigned Op0, bool Op0IsKill,
129 unsigned Op1, bool Op1IsKill,
130 uint64_t Imm);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000131 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
132 const TargetRegisterClass *RC,
133 uint64_t Imm);
134
Eric Christopher0fe7d542010-08-17 01:25:29 +0000135 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
136 unsigned Op0, bool Op0IsKill,
137 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000138
Eric Christophercb592292010-08-20 00:20:31 +0000139 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000140 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000141 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000142 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000143
144 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000145
Eric Christopher83007122010-08-23 21:44:12 +0000146 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000147 private:
Eric Christopher17787722010-10-21 21:47:51 +0000148 bool SelectLoad(const Instruction *I);
149 bool SelectStore(const Instruction *I);
150 bool SelectBranch(const Instruction *I);
151 bool SelectCmp(const Instruction *I);
152 bool SelectFPExt(const Instruction *I);
153 bool SelectFPTrunc(const Instruction *I);
154 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
155 bool SelectSIToFP(const Instruction *I);
156 bool SelectFPToSI(const Instruction *I);
157 bool SelectSDiv(const Instruction *I);
158 bool SelectSRem(const Instruction *I);
159 bool SelectCall(const Instruction *I);
160 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000161 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000162
Eric Christopher83007122010-08-23 21:44:12 +0000163 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000164 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000165 bool isTypeLegal(const Type *Ty, MVT &VT);
166 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher0d581222010-11-19 22:30:02 +0000167 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
168 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
169 bool ARMComputeAddress(const Value *Obj, Address &Addr);
170 void ARMSimplifyAddress(Address &Addr, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000171 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000172 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000173 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000174 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000175 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000176 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000177
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000178 // Call handling routines.
179 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000180 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
181 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000182 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000183 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000184 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000185 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000186 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
187 SmallVectorImpl<unsigned> &RegArgs,
188 CallingConv::ID CC,
189 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000190 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000191 const Instruction *I, CallingConv::ID CC,
192 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000193 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000194
195 // OptionalDef handling routines.
196 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000197 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000198 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
199 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000200 void AddLoadStoreOperands(EVT VT, Address &Addr,
201 const MachineInstrBuilder &MIB);
Eric Christopher456144e2010-08-19 00:37:05 +0000202};
Eric Christopherab695882010-07-21 22:26:11 +0000203
204} // end anonymous namespace
205
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000206#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000207
Eric Christopher456144e2010-08-19 00:37:05 +0000208// DefinesOptionalPredicate - This is different from DefinesPredicate in that
209// we don't care about implicit defs here, just places we'll need to add a
210// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
211bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
212 const TargetInstrDesc &TID = MI->getDesc();
213 if (!TID.hasOptionalDef())
214 return false;
215
216 // Look to see if our OptionalDef is defining CPSR or CCR.
217 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
218 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000219 if (!MO.isReg() || !MO.isDef()) continue;
220 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000221 *CPSR = true;
222 }
223 return true;
224}
225
Eric Christopheraf3dce52011-03-12 01:09:29 +0000226bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
227 const TargetInstrDesc &TID = MI->getDesc();
228
229 // If we're a thumb2 or not NEON function we were handled via isPredicable.
230 if ((TID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
231 AFI->isThumb2Function())
232 return false;
233
234 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i)
235 if (TID.OpInfo[i].isPredicate())
236 return true;
237
238 return false;
239}
240
Eric Christopher456144e2010-08-19 00:37:05 +0000241// If the machine is predicable go ahead and add the predicate operands, if
242// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000243// TODO: If we want to support thumb1 then we'll need to deal with optional
244// CPSR defs that need to be added before the remaining operands. See s_cc_out
245// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000246const MachineInstrBuilder &
247ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
248 MachineInstr *MI = &*MIB;
249
Eric Christopheraf3dce52011-03-12 01:09:29 +0000250 // Do we use a predicate? or...
251 // Are we NEON in ARM mode and have a predicate operand? If so, I know
252 // we're not predicable but add it anyways.
253 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000254 AddDefaultPred(MIB);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000255
Eric Christopher456144e2010-08-19 00:37:05 +0000256 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
257 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000258 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000259 if (DefinesOptionalPredicate(MI, &CPSR)) {
260 if (CPSR)
261 AddDefaultT1CC(MIB);
262 else
263 AddDefaultCC(MIB);
264 }
265 return MIB;
266}
267
Eric Christopher0fe7d542010-08-17 01:25:29 +0000268unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
269 const TargetRegisterClass* RC) {
270 unsigned ResultReg = createResultReg(RC);
271 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
272
Eric Christopher456144e2010-08-19 00:37:05 +0000273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000274 return ResultReg;
275}
276
277unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
278 const TargetRegisterClass *RC,
279 unsigned Op0, bool Op0IsKill) {
280 unsigned ResultReg = createResultReg(RC);
281 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
282
283 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000284 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000285 .addReg(Op0, Op0IsKill * RegState::Kill));
286 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000287 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000288 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000289 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000290 TII.get(TargetOpcode::COPY), ResultReg)
291 .addReg(II.ImplicitDefs[0]));
292 }
293 return ResultReg;
294}
295
296unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
297 const TargetRegisterClass *RC,
298 unsigned Op0, bool Op0IsKill,
299 unsigned Op1, bool Op1IsKill) {
300 unsigned ResultReg = createResultReg(RC);
301 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
302
303 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 .addReg(Op0, Op0IsKill * RegState::Kill)
306 .addReg(Op1, Op1IsKill * RegState::Kill));
307 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 .addReg(Op0, Op0IsKill * RegState::Kill)
310 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000312 TII.get(TargetOpcode::COPY), ResultReg)
313 .addReg(II.ImplicitDefs[0]));
314 }
315 return ResultReg;
316}
317
318unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
319 const TargetRegisterClass *RC,
320 unsigned Op0, bool Op0IsKill,
321 uint64_t Imm) {
322 unsigned ResultReg = createResultReg(RC);
323 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
324
325 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000327 .addReg(Op0, Op0IsKill * RegState::Kill)
328 .addImm(Imm));
329 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000331 .addReg(Op0, Op0IsKill * RegState::Kill)
332 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000334 TII.get(TargetOpcode::COPY), ResultReg)
335 .addReg(II.ImplicitDefs[0]));
336 }
337 return ResultReg;
338}
339
340unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
341 const TargetRegisterClass *RC,
342 unsigned Op0, bool Op0IsKill,
343 const ConstantFP *FPImm) {
344 unsigned ResultReg = createResultReg(RC);
345 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
346
347 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000348 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000349 .addReg(Op0, Op0IsKill * RegState::Kill)
350 .addFPImm(FPImm));
351 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000353 .addReg(Op0, Op0IsKill * RegState::Kill)
354 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000355 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000356 TII.get(TargetOpcode::COPY), ResultReg)
357 .addReg(II.ImplicitDefs[0]));
358 }
359 return ResultReg;
360}
361
362unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
363 const TargetRegisterClass *RC,
364 unsigned Op0, bool Op0IsKill,
365 unsigned Op1, bool Op1IsKill,
366 uint64_t Imm) {
367 unsigned ResultReg = createResultReg(RC);
368 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
369
370 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000371 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000372 .addReg(Op0, Op0IsKill * RegState::Kill)
373 .addReg(Op1, Op1IsKill * RegState::Kill)
374 .addImm(Imm));
375 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000377 .addReg(Op0, Op0IsKill * RegState::Kill)
378 .addReg(Op1, Op1IsKill * RegState::Kill)
379 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000380 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000381 TII.get(TargetOpcode::COPY), ResultReg)
382 .addReg(II.ImplicitDefs[0]));
383 }
384 return ResultReg;
385}
386
387unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
388 const TargetRegisterClass *RC,
389 uint64_t Imm) {
390 unsigned ResultReg = createResultReg(RC);
391 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000392
Eric Christopher0fe7d542010-08-17 01:25:29 +0000393 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000394 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000395 .addImm(Imm));
396 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000398 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000400 TII.get(TargetOpcode::COPY), ResultReg)
401 .addReg(II.ImplicitDefs[0]));
402 }
403 return ResultReg;
404}
405
406unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
407 unsigned Op0, bool Op0IsKill,
408 uint32_t Idx) {
409 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
410 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
411 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000412 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000413 DL, TII.get(TargetOpcode::COPY), ResultReg)
414 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
415 return ResultReg;
416}
417
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000418// TODO: Don't worry about 64-bit now, but when this is fixed remove the
419// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000420unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000421 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000422
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000423 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
424 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
425 TII.get(ARM::VMOVRS), MoveReg)
426 .addReg(SrcReg));
427 return MoveReg;
428}
429
430unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000431 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000432
Eric Christopheraa3ace12010-09-09 20:49:25 +0000433 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
434 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000435 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000436 .addReg(SrcReg));
437 return MoveReg;
438}
439
Eric Christopher9ed58df2010-09-09 00:19:41 +0000440// For double width floating point we need to materialize two constants
441// (the high and the low) into integer registers then use a move to get
442// the combined constant into an FP reg.
443unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
444 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000445 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000446
Eric Christopher9ed58df2010-09-09 00:19:41 +0000447 // This checks to see if we can use VFP3 instructions to materialize
448 // a constant, otherwise we have to go through the constant pool.
449 if (TLI.isFPImmLegal(Val, VT)) {
450 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
451 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
452 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
453 DestReg)
454 .addFPImm(CFP));
455 return DestReg;
456 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000457
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000458 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000459 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000460
Eric Christopher238bb162010-09-09 23:50:00 +0000461 // MachineConstantPool wants an explicit alignment.
462 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
463 if (Align == 0) {
464 // TODO: Figure out if this is correct.
465 Align = TD.getTypeAllocSize(CFP->getType());
466 }
467 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
468 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
469 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000470
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000471 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000472 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
473 DestReg)
474 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000475 .addReg(0));
476 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000477}
478
Eric Christopher744c7c82010-09-28 22:47:54 +0000479unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000480
Eric Christopher744c7c82010-09-28 22:47:54 +0000481 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000482 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000483
Eric Christophere5b13cf2010-11-03 20:21:17 +0000484 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
485
486 // If we can do this in a single instruction without a constant pool entry
487 // do so now.
488 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000489 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000490 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
491 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000492 TII.get(Opc), DestReg)
493 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000494 return DestReg;
495 }
496
Eric Christopher56d2b722010-09-02 23:43:26 +0000497 // MachineConstantPool wants an explicit alignment.
498 unsigned Align = TD.getPrefTypeAlignment(C->getType());
499 if (Align == 0) {
500 // TODO: Figure out if this is correct.
501 Align = TD.getTypeAllocSize(C->getType());
502 }
503 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000504
Eric Christopher56d2b722010-09-02 23:43:26 +0000505 if (isThumb)
506 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000507 TII.get(ARM::t2LDRpci), DestReg)
508 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000509 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000510 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000511 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000512 TII.get(ARM::LDRcp), DestReg)
513 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000514 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000515
Eric Christopher56d2b722010-09-02 23:43:26 +0000516 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000517}
518
Eric Christopherc9932f62010-10-01 23:24:42 +0000519unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000520 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000521 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000522
Eric Christopher890dbbe2010-10-02 00:32:44 +0000523 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000524
Eric Christopher890dbbe2010-10-02 00:32:44 +0000525 // TODO: No external globals for now.
526 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000527
Eric Christopher890dbbe2010-10-02 00:32:44 +0000528 // TODO: Need more magic for ARM PIC.
529 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000530
Eric Christopher890dbbe2010-10-02 00:32:44 +0000531 // MachineConstantPool wants an explicit alignment.
532 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
533 if (Align == 0) {
534 // TODO: Figure out if this is correct.
535 Align = TD.getTypeAllocSize(GV->getType());
536 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000537
Eric Christopher890dbbe2010-10-02 00:32:44 +0000538 // Grab index.
539 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000540 unsigned Id = AFI->createPICLabelUId();
Eric Christopher890dbbe2010-10-02 00:32:44 +0000541 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
542 ARMCP::CPValue, PCAdj);
543 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000544
Eric Christopher890dbbe2010-10-02 00:32:44 +0000545 // Load value.
546 MachineInstrBuilder MIB;
547 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
548 if (isThumb) {
549 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
550 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
551 .addConstantPoolIndex(Idx);
552 if (RelocM == Reloc::PIC_)
553 MIB.addImm(Id);
554 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000555 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000556 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
557 DestReg)
558 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000559 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000560 }
561 AddOptionalDefs(MIB);
562 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000563}
564
Eric Christopher9ed58df2010-09-09 00:19:41 +0000565unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
566 EVT VT = TLI.getValueType(C->getType(), true);
567
568 // Only handle simple types.
569 if (!VT.isSimple()) return 0;
570
571 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
572 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000573 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
574 return ARMMaterializeGV(GV, VT);
575 else if (isa<ConstantInt>(C))
576 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000577
Eric Christopherc9932f62010-10-01 23:24:42 +0000578 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000579}
580
Eric Christopherf9764fa2010-09-30 20:49:44 +0000581unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
582 // Don't handle dynamic allocas.
583 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000584
Duncan Sands1440e8b2010-11-03 11:35:31 +0000585 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000586 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000587
Eric Christopherf9764fa2010-09-30 20:49:44 +0000588 DenseMap<const AllocaInst*, int>::iterator SI =
589 FuncInfo.StaticAllocaMap.find(AI);
590
591 // This will get lowered later into the correct offsets and registers
592 // via rewriteXFrameIndex.
593 if (SI != FuncInfo.StaticAllocaMap.end()) {
594 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
595 unsigned ResultReg = createResultReg(RC);
596 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
597 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
598 TII.get(Opc), ResultReg)
599 .addFrameIndex(SI->second)
600 .addImm(0));
601 return ResultReg;
602 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000603
Eric Christopherf9764fa2010-09-30 20:49:44 +0000604 return 0;
605}
606
Duncan Sands1440e8b2010-11-03 11:35:31 +0000607bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
608 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000609
Eric Christopherb1cc8482010-08-25 07:23:49 +0000610 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000611 if (evt == MVT::Other || !evt.isSimple()) return false;
612 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000613
Eric Christopherdc908042010-08-31 01:28:42 +0000614 // Handle all legal types, i.e. a register that will directly hold this
615 // value.
616 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000617}
618
Duncan Sands1440e8b2010-11-03 11:35:31 +0000619bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000620 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000621
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000622 // If this is a type than can be sign or zero-extended to a basic operation
623 // go ahead and accept it now.
624 if (VT == MVT::i8 || VT == MVT::i16)
625 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000626
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000627 return false;
628}
629
Eric Christopher88de86b2010-11-19 22:36:41 +0000630// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000631bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000632 // Some boilerplate from the X86 FastISel.
633 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000634 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000635 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000636 // Don't walk into other basic blocks unless the object is an alloca from
637 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000638 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
639 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
640 Opcode = I->getOpcode();
641 U = I;
642 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000643 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000644 Opcode = C->getOpcode();
645 U = C;
646 }
647
Eric Christophercb0b04b2010-08-24 00:07:24 +0000648 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000649 if (Ty->getAddressSpace() > 255)
650 // Fast instruction selection doesn't support the special
651 // address spaces.
652 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000653
Eric Christopher83007122010-08-23 21:44:12 +0000654 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000655 default:
Eric Christopher83007122010-08-23 21:44:12 +0000656 break;
Eric Christopher55324332010-10-12 00:43:21 +0000657 case Instruction::BitCast: {
658 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000659 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000660 }
661 case Instruction::IntToPtr: {
662 // Look past no-op inttoptrs.
663 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000664 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000665 break;
666 }
667 case Instruction::PtrToInt: {
668 // Look past no-op ptrtoints.
669 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000670 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000671 break;
672 }
Eric Christophereae84392010-10-14 09:29:41 +0000673 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000674 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000675 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000676
Eric Christophereae84392010-10-14 09:29:41 +0000677 // Iterate through the GEP folding the constants into offsets where
678 // we can.
679 gep_type_iterator GTI = gep_type_begin(U);
680 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
681 i != e; ++i, ++GTI) {
682 const Value *Op = *i;
683 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
684 const StructLayout *SL = TD.getStructLayout(STy);
685 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
686 TmpOffset += SL->getElementOffset(Idx);
687 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000688 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000689 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000690 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
691 // Constant-offset addressing.
692 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000693 break;
694 }
695 if (isa<AddOperator>(Op) &&
696 (!isa<Instruction>(Op) ||
697 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
698 == FuncInfo.MBB) &&
699 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
700 // An add (in the same block) with a constant operand. Fold the
701 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000702 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000703 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000704 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000705 // Iterate on the other operand.
706 Op = cast<AddOperator>(Op)->getOperand(0);
707 continue;
708 }
709 // Unsupported
710 goto unsupported_gep;
711 }
Eric Christophereae84392010-10-14 09:29:41 +0000712 }
713 }
Eric Christopher2896df82010-10-15 18:02:07 +0000714
715 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000716 Addr.Offset = TmpOffset;
717 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000718
719 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000720 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000721
Eric Christophereae84392010-10-14 09:29:41 +0000722 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000723 break;
724 }
Eric Christopher83007122010-08-23 21:44:12 +0000725 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000726 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000727 DenseMap<const AllocaInst*, int>::iterator SI =
728 FuncInfo.StaticAllocaMap.find(AI);
729 if (SI != FuncInfo.StaticAllocaMap.end()) {
730 Addr.BaseType = Address::FrameIndexBase;
731 Addr.Base.FI = SI->second;
732 return true;
733 }
734 break;
Eric Christopher83007122010-08-23 21:44:12 +0000735 }
736 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000737
Eric Christophera9c57512010-10-13 21:41:51 +0000738 // Materialize the global variable's address into a reg which can
739 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000740 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000741 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
742 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000743
Eric Christopher0d581222010-11-19 22:30:02 +0000744 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000745 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000746 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000747
Eric Christophercb0b04b2010-08-24 00:07:24 +0000748 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000749 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
750 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000751}
752
Eric Christopher0d581222010-11-19 22:30:02 +0000753void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000754
Eric Christopher212ae932010-10-21 19:40:30 +0000755 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000756
Eric Christopher212ae932010-10-21 19:40:30 +0000757 bool needsLowering = false;
758 switch (VT.getSimpleVT().SimpleTy) {
759 default:
760 assert(false && "Unhandled load/store type!");
761 case MVT::i1:
762 case MVT::i8:
763 case MVT::i16:
764 case MVT::i32:
765 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000766 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000767 break;
768 case MVT::f32:
769 case MVT::f64:
770 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000771 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000772 break;
773 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000774
Eric Christopher827656d2010-11-20 22:38:27 +0000775 // If this is a stack pointer and the offset needs to be simplified then
776 // put the alloca address into a register, set the base type back to
777 // register and continue. This should almost never happen.
778 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
779 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
780 ARM::GPRRegisterClass;
781 unsigned ResultReg = createResultReg(RC);
782 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
783 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
784 TII.get(Opc), ResultReg)
785 .addFrameIndex(Addr.Base.FI)
786 .addImm(0));
787 Addr.Base.Reg = ResultReg;
788 Addr.BaseType = Address::RegBase;
789 }
790
Eric Christopher212ae932010-10-21 19:40:30 +0000791 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000792 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000793 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000794 ARMCC::CondCodes Pred = ARMCC::AL;
795 unsigned PredReg = 0;
796
Eric Christopher2896df82010-10-15 18:02:07 +0000797 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
798 ARM::GPRRegisterClass;
799 unsigned BaseReg = createResultReg(RC);
800
Eric Christophereaa204b2010-09-02 01:39:14 +0000801 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000802 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000803 BaseReg, Addr.Base.Reg, Addr.Offset,
804 Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000805 static_cast<const ARMBaseInstrInfo&>(TII));
806 else {
807 assert(AFI->isThumb2Function());
808 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000809 BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000810 static_cast<const ARMBaseInstrInfo&>(TII));
811 }
Eric Christopher0d581222010-11-19 22:30:02 +0000812 Addr.Offset = 0;
813 Addr.Base.Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000814 }
Eric Christopher83007122010-08-23 21:44:12 +0000815}
816
Eric Christopher564857f2010-12-01 01:40:24 +0000817void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
818 const MachineInstrBuilder &MIB) {
819 // addrmode5 output depends on the selection dag addressing dividing the
820 // offset by 4 that it then later multiplies. Do this here as well.
821 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
822 VT.getSimpleVT().SimpleTy == MVT::f64)
823 Addr.Offset /= 4;
824
825 // Frame base works a bit differently. Handle it separately.
826 if (Addr.BaseType == Address::FrameIndexBase) {
827 int FI = Addr.Base.FI;
828 int Offset = Addr.Offset;
829 MachineMemOperand *MMO =
830 FuncInfo.MF->getMachineMemOperand(
831 MachinePointerInfo::getFixedStack(FI, Offset),
832 MachineMemOperand::MOLoad,
833 MFI.getObjectSize(FI),
834 MFI.getObjectAlignment(FI));
835 // Now add the rest of the operands.
836 MIB.addFrameIndex(FI);
837
838 // ARM halfword load/stores need an additional operand.
839 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
840
841 MIB.addImm(Addr.Offset);
842 MIB.addMemOperand(MMO);
843 } else {
844 // Now add the rest of the operands.
845 MIB.addReg(Addr.Base.Reg);
846
847 // ARM halfword load/stores need an additional operand.
848 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
849
850 MIB.addImm(Addr.Offset);
851 }
852 AddOptionalDefs(MIB);
853}
854
Eric Christopher0d581222010-11-19 22:30:02 +0000855bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000856
Eric Christopherb1cc8482010-08-25 07:23:49 +0000857 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000858 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000859 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000860 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000861 // This is mostly going to be Neon/vector support.
862 default: return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000863 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000864 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000865 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000866 break;
867 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000868 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000869 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000870 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000871 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000872 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000873 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000874 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000875 case MVT::f32:
876 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000877 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000878 break;
879 case MVT::f64:
880 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000881 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000882 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000883 }
Eric Christopher564857f2010-12-01 01:40:24 +0000884 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000885 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000886
Eric Christopher564857f2010-12-01 01:40:24 +0000887 // Create the base instruction, then add the operands.
888 ResultReg = createResultReg(RC);
889 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
890 TII.get(Opc), ResultReg);
891 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopherdc908042010-08-31 01:28:42 +0000892 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000893}
894
Eric Christopher43b62be2010-09-27 06:02:23 +0000895bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000896 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000897 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000898 if (!isLoadTypeLegal(I->getType(), VT))
899 return false;
900
Eric Christopher564857f2010-12-01 01:40:24 +0000901 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000902 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +0000903 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000904
905 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000906 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000907 UpdateValueMap(I, ResultReg);
908 return true;
909}
910
Eric Christopher0d581222010-11-19 22:30:02 +0000911bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000912 unsigned StrOpc;
913 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000914 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000915 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000916 case MVT::i1: {
917 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
918 ARM::GPRRegisterClass);
919 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
920 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
921 TII.get(Opc), Res)
922 .addReg(SrcReg).addImm(1));
923 SrcReg = Res;
924 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000925 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000926 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000927 break;
928 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000929 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000930 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000931 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000932 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000933 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000934 case MVT::f32:
935 if (!Subtarget->hasVFP2()) return false;
936 StrOpc = ARM::VSTRS;
937 break;
938 case MVT::f64:
939 if (!Subtarget->hasVFP2()) return false;
940 StrOpc = ARM::VSTRD;
941 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000942 }
Eric Christopher564857f2010-12-01 01:40:24 +0000943 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000944 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000945
Eric Christopher564857f2010-12-01 01:40:24 +0000946 // Create the base instruction, then add the operands.
947 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
948 TII.get(StrOpc))
949 .addReg(SrcReg, getKillRegState(true));
950 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000951 return true;
952}
953
Eric Christopher43b62be2010-09-27 06:02:23 +0000954bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000955 Value *Op0 = I->getOperand(0);
956 unsigned SrcReg = 0;
957
Eric Christopher564857f2010-12-01 01:40:24 +0000958 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000959 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000960 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000961 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000962
Eric Christopher1b61ef42010-09-02 01:48:11 +0000963 // Get the value to be stored into a register.
964 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +0000965 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000966
Eric Christopher564857f2010-12-01 01:40:24 +0000967 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000968 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000969 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000970 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000971
Eric Christopher0d581222010-11-19 22:30:02 +0000972 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +0000973 return true;
974}
975
976static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
977 switch (Pred) {
978 // Needs two compares...
979 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000980 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000981 default:
Eric Christopher4053e632010-11-02 01:24:49 +0000982 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +0000983 return ARMCC::AL;
984 case CmpInst::ICMP_EQ:
985 case CmpInst::FCMP_OEQ:
986 return ARMCC::EQ;
987 case CmpInst::ICMP_SGT:
988 case CmpInst::FCMP_OGT:
989 return ARMCC::GT;
990 case CmpInst::ICMP_SGE:
991 case CmpInst::FCMP_OGE:
992 return ARMCC::GE;
993 case CmpInst::ICMP_UGT:
994 case CmpInst::FCMP_UGT:
995 return ARMCC::HI;
996 case CmpInst::FCMP_OLT:
997 return ARMCC::MI;
998 case CmpInst::ICMP_ULE:
999 case CmpInst::FCMP_OLE:
1000 return ARMCC::LS;
1001 case CmpInst::FCMP_ORD:
1002 return ARMCC::VC;
1003 case CmpInst::FCMP_UNO:
1004 return ARMCC::VS;
1005 case CmpInst::FCMP_UGE:
1006 return ARMCC::PL;
1007 case CmpInst::ICMP_SLT:
1008 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001009 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001010 case CmpInst::ICMP_SLE:
1011 case CmpInst::FCMP_ULE:
1012 return ARMCC::LE;
1013 case CmpInst::FCMP_UNE:
1014 case CmpInst::ICMP_NE:
1015 return ARMCC::NE;
1016 case CmpInst::ICMP_UGE:
1017 return ARMCC::HS;
1018 case CmpInst::ICMP_ULT:
1019 return ARMCC::LO;
1020 }
Eric Christopher543cf052010-09-01 22:16:27 +00001021}
1022
Eric Christopher43b62be2010-09-27 06:02:23 +00001023bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001024 const BranchInst *BI = cast<BranchInst>(I);
1025 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1026 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001027
Eric Christophere5734102010-09-03 00:35:47 +00001028 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001029
Eric Christopher0e6233b2010-10-29 21:08:19 +00001030 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1031 // behavior.
1032 // TODO: Factor this out.
1033 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1034 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001035 MVT VT;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001036 const Type *Ty = CI->getOperand(0)->getType();
Eric Christopher76d61472010-10-30 21:25:26 +00001037 if (!isTypeLegal(Ty, VT))
1038 return false;
1039
Eric Christopher0e6233b2010-10-29 21:08:19 +00001040 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1041 if (isFloat && !Subtarget->hasVFP2())
1042 return false;
1043
1044 unsigned CmpOpc;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001045 switch (VT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001046 default: return false;
1047 // TODO: Verify compares.
1048 case MVT::f32:
1049 CmpOpc = ARM::VCMPES;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001050 break;
1051 case MVT::f64:
1052 CmpOpc = ARM::VCMPED;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001053 break;
1054 case MVT::i32:
1055 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001056 break;
1057 }
1058
1059 // Get the compare predicate.
1060 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1061
1062 // We may not handle every CC for now.
1063 if (ARMPred == ARMCC::AL) return false;
1064
1065 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1066 if (Arg1 == 0) return false;
1067
1068 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1069 if (Arg2 == 0) return false;
1070
1071 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1072 TII.get(CmpOpc))
1073 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001074
Eric Christopher0e6233b2010-10-29 21:08:19 +00001075 // For floating point we need to move the result to a comparison register
1076 // that we can then use for branches.
1077 if (isFloat)
1078 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1079 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001080
Eric Christopher0e6233b2010-10-29 21:08:19 +00001081 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1082 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1083 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1084 FastEmitBranch(FBB, DL);
1085 FuncInfo.MBB->addSuccessor(TBB);
1086 return true;
1087 }
1088 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001089
Eric Christopher0e6233b2010-10-29 21:08:19 +00001090 unsigned CmpReg = getRegForValue(BI->getCondition());
1091 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001092
Eric Christopher229207a2010-09-29 01:14:47 +00001093 // Re-set the flags just in case.
1094 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1095 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001096 .addReg(CmpReg).addImm(0));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001097
Eric Christophere5734102010-09-03 00:35:47 +00001098 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001099 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001100 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001101 FastEmitBranch(FBB, DL);
1102 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001103 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001104}
1105
Eric Christopher43b62be2010-09-27 06:02:23 +00001106bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001107 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001108
Duncan Sands1440e8b2010-11-03 11:35:31 +00001109 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001110 const Type *Ty = CI->getOperand(0)->getType();
1111 if (!isTypeLegal(Ty, VT))
1112 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001113
Eric Christopherd43393a2010-09-08 23:13:45 +00001114 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1115 if (isFloat && !Subtarget->hasVFP2())
1116 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001117
Eric Christopherd43393a2010-09-08 23:13:45 +00001118 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001119 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001120 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001121 default: return false;
1122 // TODO: Verify compares.
1123 case MVT::f32:
1124 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001125 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001126 break;
1127 case MVT::f64:
1128 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001129 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001130 break;
1131 case MVT::i32:
1132 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001133 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001134 break;
1135 }
1136
Eric Christopher229207a2010-09-29 01:14:47 +00001137 // Get the compare predicate.
1138 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001139
Eric Christopher229207a2010-09-29 01:14:47 +00001140 // We may not handle every CC for now.
1141 if (ARMPred == ARMCC::AL) return false;
1142
Eric Christopherd43393a2010-09-08 23:13:45 +00001143 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1144 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001145
Eric Christopherd43393a2010-09-08 23:13:45 +00001146 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1147 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001148
Eric Christopherd43393a2010-09-08 23:13:45 +00001149 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1150 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001151
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001152 // For floating point we need to move the result to a comparison register
1153 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001154 if (isFloat)
1155 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1156 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001157
Eric Christopher229207a2010-09-29 01:14:47 +00001158 // Now set a register based on the comparison. Explicitly set the predicates
1159 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001160 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001161 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001162 : ARM::GPRRegisterClass;
1163 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001164 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001165 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001166 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1167 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1168 .addReg(ZeroReg).addImm(1)
1169 .addImm(ARMPred).addReg(CondReg);
1170
Eric Christophera5b1e682010-09-17 22:28:18 +00001171 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001172 return true;
1173}
1174
Eric Christopher43b62be2010-09-27 06:02:23 +00001175bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001176 // Make sure we have VFP and that we're extending float to double.
1177 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001178
Eric Christopher46203602010-09-09 00:26:48 +00001179 Value *V = I->getOperand(0);
1180 if (!I->getType()->isDoubleTy() ||
1181 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001182
Eric Christopher46203602010-09-09 00:26:48 +00001183 unsigned Op = getRegForValue(V);
1184 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001185
Eric Christopher46203602010-09-09 00:26:48 +00001186 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001187 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001188 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001189 .addReg(Op));
1190 UpdateValueMap(I, Result);
1191 return true;
1192}
1193
Eric Christopher43b62be2010-09-27 06:02:23 +00001194bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001195 // Make sure we have VFP and that we're truncating double to float.
1196 if (!Subtarget->hasVFP2()) return false;
1197
1198 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001199 if (!(I->getType()->isFloatTy() &&
1200 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001201
1202 unsigned Op = getRegForValue(V);
1203 if (Op == 0) return false;
1204
1205 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001206 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001207 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001208 .addReg(Op));
1209 UpdateValueMap(I, Result);
1210 return true;
1211}
1212
Eric Christopher43b62be2010-09-27 06:02:23 +00001213bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001214 // Make sure we have VFP.
1215 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001216
Duncan Sands1440e8b2010-11-03 11:35:31 +00001217 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001218 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001219 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001220 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001221
Eric Christopher9a040492010-09-09 18:54:59 +00001222 unsigned Op = getRegForValue(I->getOperand(0));
1223 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001224
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001225 // The conversion routine works on fp-reg to fp-reg and the operand above
1226 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001227 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001228 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001229
Eric Christopher9a040492010-09-09 18:54:59 +00001230 unsigned Opc;
1231 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1232 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1233 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001234
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001235 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1237 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001238 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001239 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001240 return true;
1241}
1242
Eric Christopher43b62be2010-09-27 06:02:23 +00001243bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001244 // Make sure we have VFP.
1245 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001246
Duncan Sands1440e8b2010-11-03 11:35:31 +00001247 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001248 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001249 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001250 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001251
Eric Christopher9a040492010-09-09 18:54:59 +00001252 unsigned Op = getRegForValue(I->getOperand(0));
1253 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001254
Eric Christopher9a040492010-09-09 18:54:59 +00001255 unsigned Opc;
1256 const Type *OpTy = I->getOperand(0)->getType();
1257 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1258 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1259 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001260
Eric Christopher022b7fb2010-10-05 23:13:24 +00001261 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1262 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001263 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1264 ResultReg)
1265 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001266
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001267 // This result needs to be in an integer register, but the conversion only
1268 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001269 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001270 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001271
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001272 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001273 return true;
1274}
1275
Eric Christopher3bbd3962010-10-11 08:27:59 +00001276bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001277 MVT VT;
1278 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001279 return false;
1280
1281 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001282 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001283 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1284
1285 unsigned CondReg = getRegForValue(I->getOperand(0));
1286 if (CondReg == 0) return false;
1287 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1288 if (Op1Reg == 0) return false;
1289 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1290 if (Op2Reg == 0) return false;
1291
1292 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1293 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1294 .addReg(CondReg).addImm(1));
1295 unsigned ResultReg = createResultReg(RC);
1296 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1297 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1298 .addReg(Op1Reg).addReg(Op2Reg)
1299 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1300 UpdateValueMap(I, ResultReg);
1301 return true;
1302}
1303
Eric Christopher08637852010-09-30 22:34:19 +00001304bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001305 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001306 const Type *Ty = I->getType();
1307 if (!isTypeLegal(Ty, VT))
1308 return false;
1309
1310 // If we have integer div support we should have selected this automagically.
1311 // In case we have a real miss go ahead and return false and we'll pick
1312 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001313 if (Subtarget->hasDivide()) return false;
1314
Eric Christopher08637852010-09-30 22:34:19 +00001315 // Otherwise emit a libcall.
1316 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001317 if (VT == MVT::i8)
1318 LC = RTLIB::SDIV_I8;
1319 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001320 LC = RTLIB::SDIV_I16;
1321 else if (VT == MVT::i32)
1322 LC = RTLIB::SDIV_I32;
1323 else if (VT == MVT::i64)
1324 LC = RTLIB::SDIV_I64;
1325 else if (VT == MVT::i128)
1326 LC = RTLIB::SDIV_I128;
1327 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001328
Eric Christopher08637852010-09-30 22:34:19 +00001329 return ARMEmitLibcall(I, LC);
1330}
1331
Eric Christopher6a880d62010-10-11 08:37:26 +00001332bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001333 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001334 const Type *Ty = I->getType();
1335 if (!isTypeLegal(Ty, VT))
1336 return false;
1337
1338 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1339 if (VT == MVT::i8)
1340 LC = RTLIB::SREM_I8;
1341 else if (VT == MVT::i16)
1342 LC = RTLIB::SREM_I16;
1343 else if (VT == MVT::i32)
1344 LC = RTLIB::SREM_I32;
1345 else if (VT == MVT::i64)
1346 LC = RTLIB::SREM_I64;
1347 else if (VT == MVT::i128)
1348 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001349 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001350
Eric Christopher6a880d62010-10-11 08:37:26 +00001351 return ARMEmitLibcall(I, LC);
1352}
1353
Eric Christopher43b62be2010-09-27 06:02:23 +00001354bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001355 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001356
Eric Christopherbc39b822010-09-09 00:53:57 +00001357 // We can get here in the case when we want to use NEON for our fp
1358 // operations, but can't figure out how to. Just use the vfp instructions
1359 // if we have them.
1360 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001361 const Type *Ty = I->getType();
1362 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1363 if (isFloat && !Subtarget->hasVFP2())
1364 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001365
Eric Christopherbc39b822010-09-09 00:53:57 +00001366 unsigned Op1 = getRegForValue(I->getOperand(0));
1367 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001368
Eric Christopherbc39b822010-09-09 00:53:57 +00001369 unsigned Op2 = getRegForValue(I->getOperand(1));
1370 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001371
Eric Christopherbc39b822010-09-09 00:53:57 +00001372 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001373 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001374 switch (ISDOpcode) {
1375 default: return false;
1376 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001377 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001378 break;
1379 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001380 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001381 break;
1382 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001383 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001384 break;
1385 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001386 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001387 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1388 TII.get(Opc), ResultReg)
1389 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001390 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001391 return true;
1392}
1393
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001394// Call Handling Code
1395
Eric Christopherfa87d662010-10-18 02:17:53 +00001396bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1397 EVT SrcVT, unsigned &ResultReg) {
1398 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1399 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001400
Eric Christopherfa87d662010-10-18 02:17:53 +00001401 if (RR != 0) {
1402 ResultReg = RR;
1403 return true;
1404 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001405 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001406}
1407
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001408// This is largely taken directly from CCAssignFnForNode - we don't support
1409// varargs in FastISel so that part has been removed.
1410// TODO: We may not support all of this.
1411CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1412 switch (CC) {
1413 default:
1414 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001415 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001416 // Ignore fastcc. Silence compiler warnings.
1417 (void)RetFastCC_ARM_APCS;
1418 (void)FastCC_ARM_APCS;
1419 // Fallthrough
1420 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001421 // Use target triple & subtarget features to do actual dispatch.
1422 if (Subtarget->isAAPCS_ABI()) {
1423 if (Subtarget->hasVFP2() &&
1424 FloatABIType == FloatABI::Hard)
1425 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1426 else
1427 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1428 } else
1429 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1430 case CallingConv::ARM_AAPCS_VFP:
1431 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1432 case CallingConv::ARM_AAPCS:
1433 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1434 case CallingConv::ARM_APCS:
1435 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1436 }
1437}
1438
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001439bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1440 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001441 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001442 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1443 SmallVectorImpl<unsigned> &RegArgs,
1444 CallingConv::ID CC,
1445 unsigned &NumBytes) {
1446 SmallVector<CCValAssign, 16> ArgLocs;
1447 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1448 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1449
1450 // Get a count of how many bytes are to be pushed on the stack.
1451 NumBytes = CCInfo.getNextStackOffset();
1452
1453 // Issue CALLSEQ_START
1454 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001455 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1456 TII.get(AdjStackDown))
1457 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001458
1459 // Process the args.
1460 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1461 CCValAssign &VA = ArgLocs[i];
1462 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001463 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001464
Eric Christopher4a2b3162011-01-27 05:44:56 +00001465 // We don't handle NEON/vector parameters yet.
1466 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001467 return false;
1468
Eric Christopherf9764fa2010-09-30 20:49:44 +00001469 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001470 switch (VA.getLocInfo()) {
1471 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001472 case CCValAssign::SExt: {
1473 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1474 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001475 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001476 Emitted = true;
1477 ArgVT = VA.getLocVT();
1478 break;
1479 }
1480 case CCValAssign::ZExt: {
1481 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1482 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001483 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001484 Emitted = true;
1485 ArgVT = VA.getLocVT();
1486 break;
1487 }
1488 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001489 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1490 Arg, ArgVT, Arg);
1491 if (!Emitted)
1492 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1493 Arg, ArgVT, Arg);
1494 if (!Emitted)
1495 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1496 Arg, ArgVT, Arg);
1497
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001498 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001499 ArgVT = VA.getLocVT();
1500 break;
1501 }
1502 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001503 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001504 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001505 assert(BC != 0 && "Failed to emit a bitcast!");
1506 Arg = BC;
1507 ArgVT = VA.getLocVT();
1508 break;
1509 }
1510 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001511 }
1512
1513 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001514 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001515 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001516 VA.getLocReg())
1517 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001518 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001519 } else if (VA.needsCustom()) {
1520 // TODO: We need custom lowering for vector (v2f64) args.
1521 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001522
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001523 CCValAssign &NextVA = ArgLocs[++i];
1524
1525 // TODO: Only handle register args for now.
1526 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1527
1528 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1529 TII.get(ARM::VMOVRRD), VA.getLocReg())
1530 .addReg(NextVA.getLocReg(), RegState::Define)
1531 .addReg(Arg));
1532 RegArgs.push_back(VA.getLocReg());
1533 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001534 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001535 assert(VA.isMemLoc());
1536 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001537 Address Addr;
1538 Addr.BaseType = Address::RegBase;
1539 Addr.Base.Reg = ARM::SP;
1540 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001541
Eric Christopher0d581222010-11-19 22:30:02 +00001542 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001543 }
1544 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001545 return true;
1546}
1547
Duncan Sands1440e8b2010-11-03 11:35:31 +00001548bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001549 const Instruction *I, CallingConv::ID CC,
1550 unsigned &NumBytes) {
1551 // Issue CALLSEQ_END
1552 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001553 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1554 TII.get(AdjStackUp))
1555 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001556
1557 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001558 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001559 SmallVector<CCValAssign, 16> RVLocs;
1560 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1561 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1562
1563 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001564 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001565 // For this move we copy into two registers and then move into the
1566 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001567 EVT DestVT = RVLocs[0].getValVT();
1568 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1569 unsigned ResultReg = createResultReg(DstRC);
1570 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1571 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001572 .addReg(RVLocs[0].getLocReg())
1573 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001574
Eric Christopher3659ac22010-10-20 08:02:24 +00001575 UsedRegs.push_back(RVLocs[0].getLocReg());
1576 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001577
Eric Christopherdccd2c32010-10-11 08:38:55 +00001578 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001579 UpdateValueMap(I, ResultReg);
1580 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001581 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001582 EVT CopyVT = RVLocs[0].getValVT();
1583 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001584
Eric Christopher14df8822010-10-01 00:00:11 +00001585 unsigned ResultReg = createResultReg(DstRC);
1586 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1587 ResultReg).addReg(RVLocs[0].getLocReg());
1588 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001589
Eric Christopherdccd2c32010-10-11 08:38:55 +00001590 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001591 UpdateValueMap(I, ResultReg);
1592 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001593 }
1594
Eric Christopherdccd2c32010-10-11 08:38:55 +00001595 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001596}
1597
Eric Christopher4f512ef2010-10-22 01:28:00 +00001598bool ARMFastISel::SelectRet(const Instruction *I) {
1599 const ReturnInst *Ret = cast<ReturnInst>(I);
1600 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001601
Eric Christopher4f512ef2010-10-22 01:28:00 +00001602 if (!FuncInfo.CanLowerReturn)
1603 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001604
Eric Christopher4f512ef2010-10-22 01:28:00 +00001605 if (F.isVarArg())
1606 return false;
1607
1608 CallingConv::ID CC = F.getCallingConv();
1609 if (Ret->getNumOperands() > 0) {
1610 SmallVector<ISD::OutputArg, 4> Outs;
1611 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1612 Outs, TLI);
1613
1614 // Analyze operands of the call, assigning locations to each operand.
1615 SmallVector<CCValAssign, 16> ValLocs;
1616 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1617 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1618
1619 const Value *RV = Ret->getOperand(0);
1620 unsigned Reg = getRegForValue(RV);
1621 if (Reg == 0)
1622 return false;
1623
1624 // Only handle a single return value for now.
1625 if (ValLocs.size() != 1)
1626 return false;
1627
1628 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001629
Eric Christopher4f512ef2010-10-22 01:28:00 +00001630 // Don't bother handling odd stuff for now.
1631 if (VA.getLocInfo() != CCValAssign::Full)
1632 return false;
1633 // Only handle register returns for now.
1634 if (!VA.isRegLoc())
1635 return false;
1636 // TODO: For now, don't try to handle cases where getLocInfo()
1637 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001638 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001639 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001640
Eric Christopher4f512ef2010-10-22 01:28:00 +00001641 // Make the copy.
1642 unsigned SrcReg = Reg + VA.getValNo();
1643 unsigned DstReg = VA.getLocReg();
1644 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1645 // Avoid a cross-class copy. This is very unlikely.
1646 if (!SrcRC->contains(DstReg))
1647 return false;
1648 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1649 DstReg).addReg(SrcReg);
1650
1651 // Mark the register as live out of the function.
1652 MRI.addLiveOut(VA.getLocReg());
1653 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001654
Eric Christopher4f512ef2010-10-22 01:28:00 +00001655 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1656 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1657 TII.get(RetOpc)));
1658 return true;
1659}
1660
Eric Christopher872f4a22011-02-22 01:37:10 +00001661unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1662
1663 // Depend our opcode for thumb on whether or not we're targeting an
1664 // externally callable function. For libcalls we'll just pass a NULL GV
1665 // in here.
1666 bool isExternal = false;
1667 if (!GV || GV->hasExternalLinkage()) isExternal = true;
1668
1669 // Darwin needs the r9 versions of the opcodes.
1670 bool isDarwin = Subtarget->isTargetDarwin();
1671 if (isThumb && isExternal) {
1672 return isDarwin ? ARM::tBLXi_r9 : ARM::tBLXi;
1673 } else if (isThumb) {
1674 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1675 } else {
1676 return isDarwin ? ARM::BLr9 : ARM::BL;
1677 }
1678}
1679
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001680// A quick function that will emit a call for a named libcall in F with the
1681// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001682// can emit a call for any libcall we can produce. This is an abridged version
1683// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001684// like computed function pointers or strange arguments at call sites.
1685// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1686// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001687bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1688 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001689
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001690 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001691 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001692 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001693 if (RetTy->isVoidTy())
1694 RetVT = MVT::isVoid;
1695 else if (!isTypeLegal(RetTy, RetVT))
1696 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001697
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001698 // For now we're using BLX etc on the assumption that we have v5t ops.
1699 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001700
Eric Christopher836c6242010-12-15 23:47:29 +00001701 // TODO: For now if we have long calls specified we don't handle the call.
1702 if (EnableARMLongCalls) return false;
1703
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001704 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001705 SmallVector<Value*, 8> Args;
1706 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001707 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001708 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1709 Args.reserve(I->getNumOperands());
1710 ArgRegs.reserve(I->getNumOperands());
1711 ArgVTs.reserve(I->getNumOperands());
1712 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001713 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001714 Value *Op = I->getOperand(i);
1715 unsigned Arg = getRegForValue(Op);
1716 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001717
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001718 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001719 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001720 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001721
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001722 ISD::ArgFlagsTy Flags;
1723 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1724 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001725
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001726 Args.push_back(Op);
1727 ArgRegs.push_back(Arg);
1728 ArgVTs.push_back(ArgVT);
1729 ArgFlags.push_back(Flags);
1730 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001731
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001732 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001733 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001734 unsigned NumBytes;
1735 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1736 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001737
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001738 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001739 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001740 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001741 unsigned CallOpc = ARMSelectCallOp(NULL);
1742 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001743 // Explicitly adding the predicate here.
1744 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1745 TII.get(CallOpc)))
1746 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00001747 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001748 // Explicitly adding the predicate here.
1749 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1750 TII.get(CallOpc))
1751 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001752
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001753 // Add implicit physical register uses to the call.
1754 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1755 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001756
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001757 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001758 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001759 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001760
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001761 // Set all unused physreg defs as dead.
1762 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001763
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001764 return true;
1765}
1766
Eric Christopherf9764fa2010-09-30 20:49:44 +00001767bool ARMFastISel::SelectCall(const Instruction *I) {
1768 const CallInst *CI = cast<CallInst>(I);
1769 const Value *Callee = CI->getCalledValue();
1770
1771 // Can't handle inline asm or worry about intrinsics yet.
1772 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1773
Eric Christophere6ca6772010-10-01 21:33:12 +00001774 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001775 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001776 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1777 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001778
Eric Christopherf9764fa2010-09-30 20:49:44 +00001779 // Check the calling convention.
1780 ImmutableCallSite CS(CI);
1781 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001782
Eric Christopherf9764fa2010-09-30 20:49:44 +00001783 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001784
Eric Christopherf9764fa2010-09-30 20:49:44 +00001785 // Let SDISel handle vararg functions.
1786 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1787 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1788 if (FTy->isVarArg())
1789 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001790
Eric Christopherf9764fa2010-09-30 20:49:44 +00001791 // Handle *simple* calls for now.
1792 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001793 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001794 if (RetTy->isVoidTy())
1795 RetVT = MVT::isVoid;
1796 else if (!isTypeLegal(RetTy, RetVT))
1797 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001798
Eric Christopherf9764fa2010-09-30 20:49:44 +00001799 // For now we're using BLX etc on the assumption that we have v5t ops.
1800 // TODO: Maybe?
1801 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001802
Eric Christopher836c6242010-12-15 23:47:29 +00001803 // TODO: For now if we have long calls specified we don't handle the call.
1804 if (EnableARMLongCalls) return false;
1805
Eric Christopherf9764fa2010-09-30 20:49:44 +00001806 // Set up the argument vectors.
1807 SmallVector<Value*, 8> Args;
1808 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001809 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001810 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1811 Args.reserve(CS.arg_size());
1812 ArgRegs.reserve(CS.arg_size());
1813 ArgVTs.reserve(CS.arg_size());
1814 ArgFlags.reserve(CS.arg_size());
1815 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1816 i != e; ++i) {
1817 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001818
Eric Christopherf9764fa2010-09-30 20:49:44 +00001819 if (Arg == 0)
1820 return false;
1821 ISD::ArgFlagsTy Flags;
1822 unsigned AttrInd = i - CS.arg_begin() + 1;
1823 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1824 Flags.setSExt();
1825 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1826 Flags.setZExt();
1827
1828 // FIXME: Only handle *easy* calls for now.
1829 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1830 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1831 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1832 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1833 return false;
1834
1835 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001836 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001837 if (!isTypeLegal(ArgTy, ArgVT))
1838 return false;
1839 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1840 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001841
Eric Christopherf9764fa2010-09-30 20:49:44 +00001842 Args.push_back(*i);
1843 ArgRegs.push_back(Arg);
1844 ArgVTs.push_back(ArgVT);
1845 ArgFlags.push_back(Flags);
1846 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001847
Eric Christopherf9764fa2010-09-30 20:49:44 +00001848 // Handle the arguments now that we've gotten them.
1849 SmallVector<unsigned, 4> RegArgs;
1850 unsigned NumBytes;
1851 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1852 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001853
Eric Christopherf9764fa2010-09-30 20:49:44 +00001854 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001855 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001856 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001857 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00001858 // Explicitly adding the predicate here.
Eric Christopher872f4a22011-02-22 01:37:10 +00001859 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001860 // Explicitly adding the predicate here.
1861 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1862 TII.get(CallOpc)))
1863 .addGlobalAddress(GV, 0, 0);
Eric Christopher872f4a22011-02-22 01:37:10 +00001864 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001865 // Explicitly adding the predicate here.
1866 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1867 TII.get(CallOpc))
1868 .addGlobalAddress(GV, 0, 0));
Eric Christopherc19aadb2010-12-21 03:50:43 +00001869
Eric Christopherf9764fa2010-09-30 20:49:44 +00001870 // Add implicit physical register uses to the call.
1871 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1872 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001873
Eric Christopherf9764fa2010-09-30 20:49:44 +00001874 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001875 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001876 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001877
Eric Christopherf9764fa2010-09-30 20:49:44 +00001878 // Set all unused physreg defs as dead.
1879 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001880
Eric Christopherf9764fa2010-09-30 20:49:44 +00001881 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001882
Eric Christopherf9764fa2010-09-30 20:49:44 +00001883}
1884
Eric Christopher56d2b722010-09-02 23:43:26 +00001885// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001886bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001887
Eric Christopherab695882010-07-21 22:26:11 +00001888 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001889 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001890 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001891 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001892 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001893 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001894 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001895 case Instruction::ICmp:
1896 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001897 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001898 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001899 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001900 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001901 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001902 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001903 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001904 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001905 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001906 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001907 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001908 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001909 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001910 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001911 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001912 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001913 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001914 case Instruction::SRem:
1915 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001916 case Instruction::Call:
1917 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001918 case Instruction::Select:
1919 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001920 case Instruction::Ret:
1921 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001922 default: break;
1923 }
1924 return false;
1925}
1926
1927namespace llvm {
1928 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001929 // Completely untested on non-darwin.
1930 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001931
Eric Christopheraaa8df42010-11-02 01:21:28 +00001932 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001933 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001934 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001935 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001936 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001937 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001938 }
1939}