blob: 9f295302db0e4afb4a4fa5ef9815e47e4e5fb5f2 [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);
126 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 uint64_t Imm);
129 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
130 const TargetRegisterClass *RC,
131 unsigned Op0, bool Op0IsKill,
132 unsigned Op1, bool Op1IsKill,
133 uint64_t Imm);
134 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
135 unsigned Op0, bool Op0IsKill,
136 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000137
Eric Christophercb592292010-08-20 00:20:31 +0000138 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000139 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000140 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000141 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000142
143 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000144
Eric Christopher83007122010-08-23 21:44:12 +0000145 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000146 private:
Eric Christopher17787722010-10-21 21:47:51 +0000147 bool SelectLoad(const Instruction *I);
148 bool SelectStore(const Instruction *I);
149 bool SelectBranch(const Instruction *I);
150 bool SelectCmp(const Instruction *I);
151 bool SelectFPExt(const Instruction *I);
152 bool SelectFPTrunc(const Instruction *I);
153 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
154 bool SelectSIToFP(const Instruction *I);
155 bool SelectFPToSI(const Instruction *I);
156 bool SelectSDiv(const Instruction *I);
157 bool SelectSRem(const Instruction *I);
158 bool SelectCall(const Instruction *I);
159 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000160 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000161
Eric Christopher83007122010-08-23 21:44:12 +0000162 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000163 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000164 bool isTypeLegal(const Type *Ty, MVT &VT);
165 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher0d581222010-11-19 22:30:02 +0000166 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
167 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
168 bool ARMComputeAddress(const Value *Obj, Address &Addr);
169 void ARMSimplifyAddress(Address &Addr, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000170 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000171 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000172 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000173 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000174 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000175
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000176 // Call handling routines.
177 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000178 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
179 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000180 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000181 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000182 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000183 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000184 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
185 SmallVectorImpl<unsigned> &RegArgs,
186 CallingConv::ID CC,
187 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000188 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000189 const Instruction *I, CallingConv::ID CC,
190 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000191 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000192
193 // OptionalDef handling routines.
194 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000195 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
196 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000197 void AddLoadStoreOperands(EVT VT, Address &Addr,
198 const MachineInstrBuilder &MIB);
Eric Christopher456144e2010-08-19 00:37:05 +0000199};
Eric Christopherab695882010-07-21 22:26:11 +0000200
201} // end anonymous namespace
202
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000203#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000204
Eric Christopher456144e2010-08-19 00:37:05 +0000205// DefinesOptionalPredicate - This is different from DefinesPredicate in that
206// we don't care about implicit defs here, just places we'll need to add a
207// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
208bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
209 const TargetInstrDesc &TID = MI->getDesc();
210 if (!TID.hasOptionalDef())
211 return false;
212
213 // Look to see if our OptionalDef is defining CPSR or CCR.
214 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
215 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000216 if (!MO.isReg() || !MO.isDef()) continue;
217 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000218 *CPSR = true;
219 }
220 return true;
221}
222
223// If the machine is predicable go ahead and add the predicate operands, if
224// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000225// TODO: If we want to support thumb1 then we'll need to deal with optional
226// CPSR defs that need to be added before the remaining operands. See s_cc_out
227// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000228const MachineInstrBuilder &
229ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
230 MachineInstr *MI = &*MIB;
231
232 // Do we use a predicate?
233 if (TII.isPredicable(MI))
234 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000235
Eric Christopher456144e2010-08-19 00:37:05 +0000236 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
237 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000238 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000239 if (DefinesOptionalPredicate(MI, &CPSR)) {
240 if (CPSR)
241 AddDefaultT1CC(MIB);
242 else
243 AddDefaultCC(MIB);
244 }
245 return MIB;
246}
247
Eric Christopher0fe7d542010-08-17 01:25:29 +0000248unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
249 const TargetRegisterClass* RC) {
250 unsigned ResultReg = createResultReg(RC);
251 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
252
Eric Christopher456144e2010-08-19 00:37:05 +0000253 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000254 return ResultReg;
255}
256
257unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
258 const TargetRegisterClass *RC,
259 unsigned Op0, bool Op0IsKill) {
260 unsigned ResultReg = createResultReg(RC);
261 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
262
263 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000264 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000265 .addReg(Op0, Op0IsKill * RegState::Kill));
266 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000267 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000268 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000269 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000270 TII.get(TargetOpcode::COPY), ResultReg)
271 .addReg(II.ImplicitDefs[0]));
272 }
273 return ResultReg;
274}
275
276unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
277 const TargetRegisterClass *RC,
278 unsigned Op0, bool Op0IsKill,
279 unsigned Op1, bool Op1IsKill) {
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 .addReg(Op1, Op1IsKill * RegState::Kill));
287 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000289 .addReg(Op0, Op0IsKill * RegState::Kill)
290 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000291 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000292 TII.get(TargetOpcode::COPY), ResultReg)
293 .addReg(II.ImplicitDefs[0]));
294 }
295 return ResultReg;
296}
297
298unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
299 const TargetRegisterClass *RC,
300 unsigned Op0, bool Op0IsKill,
301 uint64_t Imm) {
302 unsigned ResultReg = createResultReg(RC);
303 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
304
305 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000307 .addReg(Op0, Op0IsKill * RegState::Kill)
308 .addImm(Imm));
309 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000311 .addReg(Op0, Op0IsKill * RegState::Kill)
312 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000313 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000314 TII.get(TargetOpcode::COPY), ResultReg)
315 .addReg(II.ImplicitDefs[0]));
316 }
317 return ResultReg;
318}
319
320unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
321 const TargetRegisterClass *RC,
322 unsigned Op0, bool Op0IsKill,
323 const ConstantFP *FPImm) {
324 unsigned ResultReg = createResultReg(RC);
325 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
326
327 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000329 .addReg(Op0, Op0IsKill * RegState::Kill)
330 .addFPImm(FPImm));
331 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000332 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000333 .addReg(Op0, Op0IsKill * RegState::Kill)
334 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000335 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000336 TII.get(TargetOpcode::COPY), ResultReg)
337 .addReg(II.ImplicitDefs[0]));
338 }
339 return ResultReg;
340}
341
342unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
343 const TargetRegisterClass *RC,
344 unsigned Op0, bool Op0IsKill,
345 unsigned Op1, bool Op1IsKill,
346 uint64_t Imm) {
347 unsigned ResultReg = createResultReg(RC);
348 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
349
350 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000351 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000352 .addReg(Op0, Op0IsKill * RegState::Kill)
353 .addReg(Op1, Op1IsKill * RegState::Kill)
354 .addImm(Imm));
355 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000357 .addReg(Op0, Op0IsKill * RegState::Kill)
358 .addReg(Op1, Op1IsKill * RegState::Kill)
359 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000360 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000361 TII.get(TargetOpcode::COPY), ResultReg)
362 .addReg(II.ImplicitDefs[0]));
363 }
364 return ResultReg;
365}
366
367unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
368 const TargetRegisterClass *RC,
369 uint64_t Imm) {
370 unsigned ResultReg = createResultReg(RC);
371 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000372
Eric Christopher0fe7d542010-08-17 01:25:29 +0000373 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000375 .addImm(Imm));
376 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000378 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000379 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000380 TII.get(TargetOpcode::COPY), ResultReg)
381 .addReg(II.ImplicitDefs[0]));
382 }
383 return ResultReg;
384}
385
386unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
387 unsigned Op0, bool Op0IsKill,
388 uint32_t Idx) {
389 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
390 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
391 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000392 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000393 DL, TII.get(TargetOpcode::COPY), ResultReg)
394 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
395 return ResultReg;
396}
397
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000398// TODO: Don't worry about 64-bit now, but when this is fixed remove the
399// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000400unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000401 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000402
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000403 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
404 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
405 TII.get(ARM::VMOVRS), MoveReg)
406 .addReg(SrcReg));
407 return MoveReg;
408}
409
410unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000411 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000412
Eric Christopheraa3ace12010-09-09 20:49:25 +0000413 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
414 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000415 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000416 .addReg(SrcReg));
417 return MoveReg;
418}
419
Eric Christopher9ed58df2010-09-09 00:19:41 +0000420// For double width floating point we need to materialize two constants
421// (the high and the low) into integer registers then use a move to get
422// the combined constant into an FP reg.
423unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
424 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000425 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000426
Eric Christopher9ed58df2010-09-09 00:19:41 +0000427 // This checks to see if we can use VFP3 instructions to materialize
428 // a constant, otherwise we have to go through the constant pool.
429 if (TLI.isFPImmLegal(Val, VT)) {
430 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
431 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
432 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
433 DestReg)
434 .addFPImm(CFP));
435 return DestReg;
436 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000437
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000438 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000439 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000440
Eric Christopher238bb162010-09-09 23:50:00 +0000441 // MachineConstantPool wants an explicit alignment.
442 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
443 if (Align == 0) {
444 // TODO: Figure out if this is correct.
445 Align = TD.getTypeAllocSize(CFP->getType());
446 }
447 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
448 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
449 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000450
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000451 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000452 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
453 DestReg)
454 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000455 .addReg(0));
456 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000457}
458
Eric Christopher744c7c82010-09-28 22:47:54 +0000459unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000460
Eric Christopher744c7c82010-09-28 22:47:54 +0000461 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000462 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000463
Eric Christophere5b13cf2010-11-03 20:21:17 +0000464 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
465
466 // If we can do this in a single instruction without a constant pool entry
467 // do so now.
468 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000469 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000470 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
471 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000472 TII.get(Opc), DestReg)
473 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000474 return DestReg;
475 }
476
Eric Christopher56d2b722010-09-02 23:43:26 +0000477 // MachineConstantPool wants an explicit alignment.
478 unsigned Align = TD.getPrefTypeAlignment(C->getType());
479 if (Align == 0) {
480 // TODO: Figure out if this is correct.
481 Align = TD.getTypeAllocSize(C->getType());
482 }
483 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000484
Eric Christopher56d2b722010-09-02 23:43:26 +0000485 if (isThumb)
486 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000487 TII.get(ARM::t2LDRpci), DestReg)
488 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000489 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000490 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000491 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000492 TII.get(ARM::LDRcp), DestReg)
493 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000494 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000495
Eric Christopher56d2b722010-09-02 23:43:26 +0000496 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000497}
498
Eric Christopherc9932f62010-10-01 23:24:42 +0000499unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000500 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000501 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000502
Eric Christopher890dbbe2010-10-02 00:32:44 +0000503 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000504
Eric Christopher890dbbe2010-10-02 00:32:44 +0000505 // TODO: No external globals for now.
506 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000507
Eric Christopher890dbbe2010-10-02 00:32:44 +0000508 // TODO: Need more magic for ARM PIC.
509 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000510
Eric Christopher890dbbe2010-10-02 00:32:44 +0000511 // MachineConstantPool wants an explicit alignment.
512 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
513 if (Align == 0) {
514 // TODO: Figure out if this is correct.
515 Align = TD.getTypeAllocSize(GV->getType());
516 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000517
Eric Christopher890dbbe2010-10-02 00:32:44 +0000518 // Grab index.
519 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000520 unsigned Id = AFI->createPICLabelUId();
Eric Christopher890dbbe2010-10-02 00:32:44 +0000521 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
522 ARMCP::CPValue, PCAdj);
523 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000524
Eric Christopher890dbbe2010-10-02 00:32:44 +0000525 // Load value.
526 MachineInstrBuilder MIB;
527 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
528 if (isThumb) {
529 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
530 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
531 .addConstantPoolIndex(Idx);
532 if (RelocM == Reloc::PIC_)
533 MIB.addImm(Id);
534 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000535 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000536 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
537 DestReg)
538 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000539 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000540 }
541 AddOptionalDefs(MIB);
542 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000543}
544
Eric Christopher9ed58df2010-09-09 00:19:41 +0000545unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
546 EVT VT = TLI.getValueType(C->getType(), true);
547
548 // Only handle simple types.
549 if (!VT.isSimple()) return 0;
550
551 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
552 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000553 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
554 return ARMMaterializeGV(GV, VT);
555 else if (isa<ConstantInt>(C))
556 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000557
Eric Christopherc9932f62010-10-01 23:24:42 +0000558 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000559}
560
Eric Christopherf9764fa2010-09-30 20:49:44 +0000561unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
562 // Don't handle dynamic allocas.
563 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000564
Duncan Sands1440e8b2010-11-03 11:35:31 +0000565 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000566 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000567
Eric Christopherf9764fa2010-09-30 20:49:44 +0000568 DenseMap<const AllocaInst*, int>::iterator SI =
569 FuncInfo.StaticAllocaMap.find(AI);
570
571 // This will get lowered later into the correct offsets and registers
572 // via rewriteXFrameIndex.
573 if (SI != FuncInfo.StaticAllocaMap.end()) {
574 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
575 unsigned ResultReg = createResultReg(RC);
576 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
577 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
578 TII.get(Opc), ResultReg)
579 .addFrameIndex(SI->second)
580 .addImm(0));
581 return ResultReg;
582 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000583
Eric Christopherf9764fa2010-09-30 20:49:44 +0000584 return 0;
585}
586
Duncan Sands1440e8b2010-11-03 11:35:31 +0000587bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
588 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000589
Eric Christopherb1cc8482010-08-25 07:23:49 +0000590 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000591 if (evt == MVT::Other || !evt.isSimple()) return false;
592 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000593
Eric Christopherdc908042010-08-31 01:28:42 +0000594 // Handle all legal types, i.e. a register that will directly hold this
595 // value.
596 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000597}
598
Duncan Sands1440e8b2010-11-03 11:35:31 +0000599bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000600 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000601
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000602 // If this is a type than can be sign or zero-extended to a basic operation
603 // go ahead and accept it now.
604 if (VT == MVT::i8 || VT == MVT::i16)
605 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000606
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000607 return false;
608}
609
Eric Christopher88de86b2010-11-19 22:36:41 +0000610// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000611bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000612 // Some boilerplate from the X86 FastISel.
613 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000614 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000615 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000616 // Don't walk into other basic blocks unless the object is an alloca from
617 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000618 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
619 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
620 Opcode = I->getOpcode();
621 U = I;
622 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000623 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000624 Opcode = C->getOpcode();
625 U = C;
626 }
627
Eric Christophercb0b04b2010-08-24 00:07:24 +0000628 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000629 if (Ty->getAddressSpace() > 255)
630 // Fast instruction selection doesn't support the special
631 // address spaces.
632 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000633
Eric Christopher83007122010-08-23 21:44:12 +0000634 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000635 default:
Eric Christopher83007122010-08-23 21:44:12 +0000636 break;
Eric Christopher55324332010-10-12 00:43:21 +0000637 case Instruction::BitCast: {
638 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000639 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000640 }
641 case Instruction::IntToPtr: {
642 // Look past no-op inttoptrs.
643 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000644 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000645 break;
646 }
647 case Instruction::PtrToInt: {
648 // Look past no-op ptrtoints.
649 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000650 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000651 break;
652 }
Eric Christophereae84392010-10-14 09:29:41 +0000653 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000654 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000655 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000656
Eric Christophereae84392010-10-14 09:29:41 +0000657 // Iterate through the GEP folding the constants into offsets where
658 // we can.
659 gep_type_iterator GTI = gep_type_begin(U);
660 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
661 i != e; ++i, ++GTI) {
662 const Value *Op = *i;
663 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
664 const StructLayout *SL = TD.getStructLayout(STy);
665 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
666 TmpOffset += SL->getElementOffset(Idx);
667 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000668 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
669 SmallVector<const Value *, 4> Worklist;
670 Worklist.push_back(Op);
671 do {
672 Op = Worklist.pop_back_val();
673 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
674 // Constant-offset addressing.
675 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000676 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000677 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
678 // An add with a constant operand. Fold the constant.
679 ConstantInt *CI =
680 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
681 TmpOffset += CI->getSExtValue() * S;
682 // Add the other operand back to the work list.
683 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
684 } else
685 goto unsupported_gep;
686 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000687 }
688 }
Eric Christopher2896df82010-10-15 18:02:07 +0000689
690 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000691 Addr.Offset = TmpOffset;
692 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000693
694 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000695 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000696
Eric Christophereae84392010-10-14 09:29:41 +0000697 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000698 break;
699 }
Eric Christopher83007122010-08-23 21:44:12 +0000700 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000701 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000702 DenseMap<const AllocaInst*, int>::iterator SI =
703 FuncInfo.StaticAllocaMap.find(AI);
704 if (SI != FuncInfo.StaticAllocaMap.end()) {
705 Addr.BaseType = Address::FrameIndexBase;
706 Addr.Base.FI = SI->second;
707 return true;
708 }
709 break;
Eric Christopher83007122010-08-23 21:44:12 +0000710 }
711 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000712
Eric Christophera9c57512010-10-13 21:41:51 +0000713 // Materialize the global variable's address into a reg which can
714 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000715 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000716 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
717 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000718
Eric Christopher0d581222010-11-19 22:30:02 +0000719 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000720 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000721 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000722
Eric Christophercb0b04b2010-08-24 00:07:24 +0000723 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000724 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
725 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000726}
727
Eric Christopher0d581222010-11-19 22:30:02 +0000728void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000729
Eric Christopher212ae932010-10-21 19:40:30 +0000730 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000731
Eric Christopher212ae932010-10-21 19:40:30 +0000732 bool needsLowering = false;
733 switch (VT.getSimpleVT().SimpleTy) {
734 default:
735 assert(false && "Unhandled load/store type!");
736 case MVT::i1:
737 case MVT::i8:
738 case MVT::i16:
739 case MVT::i32:
740 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000741 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000742 break;
743 case MVT::f32:
744 case MVT::f64:
745 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000746 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000747 break;
748 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000749
Eric Christopher827656d2010-11-20 22:38:27 +0000750 // If this is a stack pointer and the offset needs to be simplified then
751 // put the alloca address into a register, set the base type back to
752 // register and continue. This should almost never happen.
753 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
754 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
755 ARM::GPRRegisterClass;
756 unsigned ResultReg = createResultReg(RC);
757 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
758 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
759 TII.get(Opc), ResultReg)
760 .addFrameIndex(Addr.Base.FI)
761 .addImm(0));
762 Addr.Base.Reg = ResultReg;
763 Addr.BaseType = Address::RegBase;
764 }
765
Eric Christopher212ae932010-10-21 19:40:30 +0000766 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000767 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000768 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000769 ARMCC::CondCodes Pred = ARMCC::AL;
770 unsigned PredReg = 0;
771
Eric Christopher2896df82010-10-15 18:02:07 +0000772 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
773 ARM::GPRRegisterClass;
774 unsigned BaseReg = createResultReg(RC);
775
Eric Christophereaa204b2010-09-02 01:39:14 +0000776 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000777 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000778 BaseReg, Addr.Base.Reg, Addr.Offset,
779 Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000780 static_cast<const ARMBaseInstrInfo&>(TII));
781 else {
782 assert(AFI->isThumb2Function());
783 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000784 BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000785 static_cast<const ARMBaseInstrInfo&>(TII));
786 }
Eric Christopher0d581222010-11-19 22:30:02 +0000787 Addr.Offset = 0;
788 Addr.Base.Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000789 }
Eric Christopher83007122010-08-23 21:44:12 +0000790}
791
Eric Christopher564857f2010-12-01 01:40:24 +0000792void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
793 const MachineInstrBuilder &MIB) {
794 // addrmode5 output depends on the selection dag addressing dividing the
795 // offset by 4 that it then later multiplies. Do this here as well.
796 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
797 VT.getSimpleVT().SimpleTy == MVT::f64)
798 Addr.Offset /= 4;
799
800 // Frame base works a bit differently. Handle it separately.
801 if (Addr.BaseType == Address::FrameIndexBase) {
802 int FI = Addr.Base.FI;
803 int Offset = Addr.Offset;
804 MachineMemOperand *MMO =
805 FuncInfo.MF->getMachineMemOperand(
806 MachinePointerInfo::getFixedStack(FI, Offset),
807 MachineMemOperand::MOLoad,
808 MFI.getObjectSize(FI),
809 MFI.getObjectAlignment(FI));
810 // Now add the rest of the operands.
811 MIB.addFrameIndex(FI);
812
813 // ARM halfword load/stores need an additional operand.
814 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
815
816 MIB.addImm(Addr.Offset);
817 MIB.addMemOperand(MMO);
818 } else {
819 // Now add the rest of the operands.
820 MIB.addReg(Addr.Base.Reg);
821
822 // ARM halfword load/stores need an additional operand.
823 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
824
825 MIB.addImm(Addr.Offset);
826 }
827 AddOptionalDefs(MIB);
828}
829
Eric Christopher0d581222010-11-19 22:30:02 +0000830bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000831
Eric Christopherb1cc8482010-08-25 07:23:49 +0000832 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000833 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000834 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000835 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000836 // This is mostly going to be Neon/vector support.
837 default: return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000838 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000839 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000840 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000841 break;
842 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000843 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000844 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000845 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000846 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000847 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000848 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000849 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000850 case MVT::f32:
851 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000852 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000853 break;
854 case MVT::f64:
855 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000856 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000857 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000858 }
Eric Christopher564857f2010-12-01 01:40:24 +0000859 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000860 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000861
Eric Christopher564857f2010-12-01 01:40:24 +0000862 // Create the base instruction, then add the operands.
863 ResultReg = createResultReg(RC);
864 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
865 TII.get(Opc), ResultReg);
866 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopherdc908042010-08-31 01:28:42 +0000867 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000868}
869
Eric Christopher43b62be2010-09-27 06:02:23 +0000870bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000871 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000872 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000873 if (!isLoadTypeLegal(I->getType(), VT))
874 return false;
875
Eric Christopher564857f2010-12-01 01:40:24 +0000876 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000877 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +0000878 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000879
880 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000881 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000882 UpdateValueMap(I, ResultReg);
883 return true;
884}
885
Eric Christopher0d581222010-11-19 22:30:02 +0000886bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000887 unsigned StrOpc;
888 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000889 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000890 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000891 case MVT::i1: {
892 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
893 ARM::GPRRegisterClass);
894 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
895 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
896 TII.get(Opc), Res)
897 .addReg(SrcReg).addImm(1));
898 SrcReg = Res;
899 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000900 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000901 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000902 break;
903 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000904 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000905 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000906 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000907 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000908 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000909 case MVT::f32:
910 if (!Subtarget->hasVFP2()) return false;
911 StrOpc = ARM::VSTRS;
912 break;
913 case MVT::f64:
914 if (!Subtarget->hasVFP2()) return false;
915 StrOpc = ARM::VSTRD;
916 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000917 }
Eric Christopher564857f2010-12-01 01:40:24 +0000918 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000919 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000920
Eric Christopher564857f2010-12-01 01:40:24 +0000921 // Create the base instruction, then add the operands.
922 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
923 TII.get(StrOpc))
924 .addReg(SrcReg, getKillRegState(true));
925 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000926 return true;
927}
928
Eric Christopher43b62be2010-09-27 06:02:23 +0000929bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000930 Value *Op0 = I->getOperand(0);
931 unsigned SrcReg = 0;
932
Eric Christopher564857f2010-12-01 01:40:24 +0000933 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000934 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000935 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000936 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000937
Eric Christopher1b61ef42010-09-02 01:48:11 +0000938 // Get the value to be stored into a register.
939 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +0000940 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000941
Eric Christopher564857f2010-12-01 01:40:24 +0000942 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000943 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000944 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000945 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000946
Eric Christopher0d581222010-11-19 22:30:02 +0000947 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +0000948 return true;
949}
950
951static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
952 switch (Pred) {
953 // Needs two compares...
954 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000955 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000956 default:
Eric Christopher4053e632010-11-02 01:24:49 +0000957 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +0000958 return ARMCC::AL;
959 case CmpInst::ICMP_EQ:
960 case CmpInst::FCMP_OEQ:
961 return ARMCC::EQ;
962 case CmpInst::ICMP_SGT:
963 case CmpInst::FCMP_OGT:
964 return ARMCC::GT;
965 case CmpInst::ICMP_SGE:
966 case CmpInst::FCMP_OGE:
967 return ARMCC::GE;
968 case CmpInst::ICMP_UGT:
969 case CmpInst::FCMP_UGT:
970 return ARMCC::HI;
971 case CmpInst::FCMP_OLT:
972 return ARMCC::MI;
973 case CmpInst::ICMP_ULE:
974 case CmpInst::FCMP_OLE:
975 return ARMCC::LS;
976 case CmpInst::FCMP_ORD:
977 return ARMCC::VC;
978 case CmpInst::FCMP_UNO:
979 return ARMCC::VS;
980 case CmpInst::FCMP_UGE:
981 return ARMCC::PL;
982 case CmpInst::ICMP_SLT:
983 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000984 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000985 case CmpInst::ICMP_SLE:
986 case CmpInst::FCMP_ULE:
987 return ARMCC::LE;
988 case CmpInst::FCMP_UNE:
989 case CmpInst::ICMP_NE:
990 return ARMCC::NE;
991 case CmpInst::ICMP_UGE:
992 return ARMCC::HS;
993 case CmpInst::ICMP_ULT:
994 return ARMCC::LO;
995 }
Eric Christopher543cf052010-09-01 22:16:27 +0000996}
997
Eric Christopher43b62be2010-09-27 06:02:23 +0000998bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000999 const BranchInst *BI = cast<BranchInst>(I);
1000 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1001 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001002
Eric Christophere5734102010-09-03 00:35:47 +00001003 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001004
Eric Christopher0e6233b2010-10-29 21:08:19 +00001005 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1006 // behavior.
1007 // TODO: Factor this out.
1008 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1009 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001010 MVT VT;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001011 const Type *Ty = CI->getOperand(0)->getType();
Eric Christopher76d61472010-10-30 21:25:26 +00001012 if (!isTypeLegal(Ty, VT))
1013 return false;
1014
Eric Christopher0e6233b2010-10-29 21:08:19 +00001015 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1016 if (isFloat && !Subtarget->hasVFP2())
1017 return false;
1018
1019 unsigned CmpOpc;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001020 switch (VT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001021 default: return false;
1022 // TODO: Verify compares.
1023 case MVT::f32:
1024 CmpOpc = ARM::VCMPES;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001025 break;
1026 case MVT::f64:
1027 CmpOpc = ARM::VCMPED;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001028 break;
1029 case MVT::i32:
1030 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001031 break;
1032 }
1033
1034 // Get the compare predicate.
1035 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1036
1037 // We may not handle every CC for now.
1038 if (ARMPred == ARMCC::AL) return false;
1039
1040 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1041 if (Arg1 == 0) return false;
1042
1043 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1044 if (Arg2 == 0) return false;
1045
1046 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1047 TII.get(CmpOpc))
1048 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001049
Eric Christopher0e6233b2010-10-29 21:08:19 +00001050 // For floating point we need to move the result to a comparison register
1051 // that we can then use for branches.
1052 if (isFloat)
1053 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1054 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001055
Eric Christopher0e6233b2010-10-29 21:08:19 +00001056 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1057 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1058 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1059 FastEmitBranch(FBB, DL);
1060 FuncInfo.MBB->addSuccessor(TBB);
1061 return true;
1062 }
1063 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001064
Eric Christopher0e6233b2010-10-29 21:08:19 +00001065 unsigned CmpReg = getRegForValue(BI->getCondition());
1066 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001067
Eric Christopher229207a2010-09-29 01:14:47 +00001068 // Re-set the flags just in case.
1069 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1070 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001071 .addReg(CmpReg).addImm(0));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001072
Eric Christophere5734102010-09-03 00:35:47 +00001073 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001074 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001075 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001076 FastEmitBranch(FBB, DL);
1077 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001078 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001079}
1080
Eric Christopher43b62be2010-09-27 06:02:23 +00001081bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001082 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001083
Duncan Sands1440e8b2010-11-03 11:35:31 +00001084 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001085 const Type *Ty = CI->getOperand(0)->getType();
1086 if (!isTypeLegal(Ty, VT))
1087 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001088
Eric Christopherd43393a2010-09-08 23:13:45 +00001089 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1090 if (isFloat && !Subtarget->hasVFP2())
1091 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001092
Eric Christopherd43393a2010-09-08 23:13:45 +00001093 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001094 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001095 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001096 default: return false;
1097 // TODO: Verify compares.
1098 case MVT::f32:
1099 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001100 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001101 break;
1102 case MVT::f64:
1103 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001104 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001105 break;
1106 case MVT::i32:
1107 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001108 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001109 break;
1110 }
1111
Eric Christopher229207a2010-09-29 01:14:47 +00001112 // Get the compare predicate.
1113 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001114
Eric Christopher229207a2010-09-29 01:14:47 +00001115 // We may not handle every CC for now.
1116 if (ARMPred == ARMCC::AL) return false;
1117
Eric Christopherd43393a2010-09-08 23:13:45 +00001118 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1119 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001120
Eric Christopherd43393a2010-09-08 23:13:45 +00001121 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1122 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001123
Eric Christopherd43393a2010-09-08 23:13:45 +00001124 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1125 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001126
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001127 // For floating point we need to move the result to a comparison register
1128 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001129 if (isFloat)
1130 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1131 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001132
Eric Christopher229207a2010-09-29 01:14:47 +00001133 // Now set a register based on the comparison. Explicitly set the predicates
1134 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001135 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001136 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001137 : ARM::GPRRegisterClass;
1138 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001139 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001140 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001141 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1142 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1143 .addReg(ZeroReg).addImm(1)
1144 .addImm(ARMPred).addReg(CondReg);
1145
Eric Christophera5b1e682010-09-17 22:28:18 +00001146 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001147 return true;
1148}
1149
Eric Christopher43b62be2010-09-27 06:02:23 +00001150bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001151 // Make sure we have VFP and that we're extending float to double.
1152 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001153
Eric Christopher46203602010-09-09 00:26:48 +00001154 Value *V = I->getOperand(0);
1155 if (!I->getType()->isDoubleTy() ||
1156 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001157
Eric Christopher46203602010-09-09 00:26:48 +00001158 unsigned Op = getRegForValue(V);
1159 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001160
Eric Christopher46203602010-09-09 00:26:48 +00001161 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001162 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001163 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001164 .addReg(Op));
1165 UpdateValueMap(I, Result);
1166 return true;
1167}
1168
Eric Christopher43b62be2010-09-27 06:02:23 +00001169bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001170 // Make sure we have VFP and that we're truncating double to float.
1171 if (!Subtarget->hasVFP2()) return false;
1172
1173 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001174 if (!(I->getType()->isFloatTy() &&
1175 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001176
1177 unsigned Op = getRegForValue(V);
1178 if (Op == 0) return false;
1179
1180 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001181 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001182 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001183 .addReg(Op));
1184 UpdateValueMap(I, Result);
1185 return true;
1186}
1187
Eric Christopher43b62be2010-09-27 06:02:23 +00001188bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001189 // Make sure we have VFP.
1190 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001191
Duncan Sands1440e8b2010-11-03 11:35:31 +00001192 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001193 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001194 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001195 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001196
Eric Christopher9a040492010-09-09 18:54:59 +00001197 unsigned Op = getRegForValue(I->getOperand(0));
1198 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001199
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001200 // The conversion routine works on fp-reg to fp-reg and the operand above
1201 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001202 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001203 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001204
Eric Christopher9a040492010-09-09 18:54:59 +00001205 unsigned Opc;
1206 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1207 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1208 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001209
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001210 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001211 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1212 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001213 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001214 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001215 return true;
1216}
1217
Eric Christopher43b62be2010-09-27 06:02:23 +00001218bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001219 // Make sure we have VFP.
1220 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001221
Duncan Sands1440e8b2010-11-03 11:35:31 +00001222 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001223 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001224 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001225 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001226
Eric Christopher9a040492010-09-09 18:54:59 +00001227 unsigned Op = getRegForValue(I->getOperand(0));
1228 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001229
Eric Christopher9a040492010-09-09 18:54:59 +00001230 unsigned Opc;
1231 const Type *OpTy = I->getOperand(0)->getType();
1232 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1233 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1234 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001235
Eric Christopher022b7fb2010-10-05 23:13:24 +00001236 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1237 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001238 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1239 ResultReg)
1240 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001241
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001242 // This result needs to be in an integer register, but the conversion only
1243 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001244 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001245 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001246
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001247 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001248 return true;
1249}
1250
Eric Christopher3bbd3962010-10-11 08:27:59 +00001251bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001252 MVT VT;
1253 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001254 return false;
1255
1256 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001257 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001258 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1259
1260 unsigned CondReg = getRegForValue(I->getOperand(0));
1261 if (CondReg == 0) return false;
1262 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1263 if (Op1Reg == 0) return false;
1264 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1265 if (Op2Reg == 0) return false;
1266
1267 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1268 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1269 .addReg(CondReg).addImm(1));
1270 unsigned ResultReg = createResultReg(RC);
1271 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1272 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1273 .addReg(Op1Reg).addReg(Op2Reg)
1274 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1275 UpdateValueMap(I, ResultReg);
1276 return true;
1277}
1278
Eric Christopher08637852010-09-30 22:34:19 +00001279bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001280 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001281 const Type *Ty = I->getType();
1282 if (!isTypeLegal(Ty, VT))
1283 return false;
1284
1285 // If we have integer div support we should have selected this automagically.
1286 // In case we have a real miss go ahead and return false and we'll pick
1287 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001288 if (Subtarget->hasDivide()) return false;
1289
Eric Christopher08637852010-09-30 22:34:19 +00001290 // Otherwise emit a libcall.
1291 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001292 if (VT == MVT::i8)
1293 LC = RTLIB::SDIV_I8;
1294 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001295 LC = RTLIB::SDIV_I16;
1296 else if (VT == MVT::i32)
1297 LC = RTLIB::SDIV_I32;
1298 else if (VT == MVT::i64)
1299 LC = RTLIB::SDIV_I64;
1300 else if (VT == MVT::i128)
1301 LC = RTLIB::SDIV_I128;
1302 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001303
Eric Christopher08637852010-09-30 22:34:19 +00001304 return ARMEmitLibcall(I, LC);
1305}
1306
Eric Christopher6a880d62010-10-11 08:37:26 +00001307bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001308 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001309 const Type *Ty = I->getType();
1310 if (!isTypeLegal(Ty, VT))
1311 return false;
1312
1313 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1314 if (VT == MVT::i8)
1315 LC = RTLIB::SREM_I8;
1316 else if (VT == MVT::i16)
1317 LC = RTLIB::SREM_I16;
1318 else if (VT == MVT::i32)
1319 LC = RTLIB::SREM_I32;
1320 else if (VT == MVT::i64)
1321 LC = RTLIB::SREM_I64;
1322 else if (VT == MVT::i128)
1323 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001324 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001325
Eric Christopher6a880d62010-10-11 08:37:26 +00001326 return ARMEmitLibcall(I, LC);
1327}
1328
Eric Christopher43b62be2010-09-27 06:02:23 +00001329bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001330 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001331
Eric Christopherbc39b822010-09-09 00:53:57 +00001332 // We can get here in the case when we want to use NEON for our fp
1333 // operations, but can't figure out how to. Just use the vfp instructions
1334 // if we have them.
1335 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001336 const Type *Ty = I->getType();
1337 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1338 if (isFloat && !Subtarget->hasVFP2())
1339 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001340
Eric Christopherbc39b822010-09-09 00:53:57 +00001341 unsigned Op1 = getRegForValue(I->getOperand(0));
1342 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001343
Eric Christopherbc39b822010-09-09 00:53:57 +00001344 unsigned Op2 = getRegForValue(I->getOperand(1));
1345 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001346
Eric Christopherbc39b822010-09-09 00:53:57 +00001347 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001348 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001349 switch (ISDOpcode) {
1350 default: return false;
1351 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001352 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001353 break;
1354 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001355 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001356 break;
1357 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001358 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001359 break;
1360 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001361 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001362 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1363 TII.get(Opc), ResultReg)
1364 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001365 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001366 return true;
1367}
1368
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001369// Call Handling Code
1370
Eric Christopherfa87d662010-10-18 02:17:53 +00001371bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1372 EVT SrcVT, unsigned &ResultReg) {
1373 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1374 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001375
Eric Christopherfa87d662010-10-18 02:17:53 +00001376 if (RR != 0) {
1377 ResultReg = RR;
1378 return true;
1379 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001380 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001381}
1382
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001383// This is largely taken directly from CCAssignFnForNode - we don't support
1384// varargs in FastISel so that part has been removed.
1385// TODO: We may not support all of this.
1386CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1387 switch (CC) {
1388 default:
1389 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001390 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001391 // Ignore fastcc. Silence compiler warnings.
1392 (void)RetFastCC_ARM_APCS;
1393 (void)FastCC_ARM_APCS;
1394 // Fallthrough
1395 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001396 // Use target triple & subtarget features to do actual dispatch.
1397 if (Subtarget->isAAPCS_ABI()) {
1398 if (Subtarget->hasVFP2() &&
1399 FloatABIType == FloatABI::Hard)
1400 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1401 else
1402 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1403 } else
1404 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1405 case CallingConv::ARM_AAPCS_VFP:
1406 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1407 case CallingConv::ARM_AAPCS:
1408 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1409 case CallingConv::ARM_APCS:
1410 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1411 }
1412}
1413
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001414bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1415 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001416 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001417 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1418 SmallVectorImpl<unsigned> &RegArgs,
1419 CallingConv::ID CC,
1420 unsigned &NumBytes) {
1421 SmallVector<CCValAssign, 16> ArgLocs;
1422 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1423 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1424
1425 // Get a count of how many bytes are to be pushed on the stack.
1426 NumBytes = CCInfo.getNextStackOffset();
1427
1428 // Issue CALLSEQ_START
1429 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001430 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1431 TII.get(AdjStackDown))
1432 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001433
1434 // Process the args.
1435 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1436 CCValAssign &VA = ArgLocs[i];
1437 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001438 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001439
Eric Christopher4a2b3162011-01-27 05:44:56 +00001440 // We don't handle NEON/vector parameters yet.
1441 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001442 return false;
1443
Eric Christopherf9764fa2010-09-30 20:49:44 +00001444 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001445 switch (VA.getLocInfo()) {
1446 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001447 case CCValAssign::SExt: {
1448 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1449 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001450 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001451 Emitted = true;
1452 ArgVT = VA.getLocVT();
1453 break;
1454 }
1455 case CCValAssign::ZExt: {
1456 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1457 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001458 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001459 Emitted = true;
1460 ArgVT = VA.getLocVT();
1461 break;
1462 }
1463 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001464 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1465 Arg, ArgVT, Arg);
1466 if (!Emitted)
1467 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1468 Arg, ArgVT, Arg);
1469 if (!Emitted)
1470 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1471 Arg, ArgVT, Arg);
1472
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001473 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001474 ArgVT = VA.getLocVT();
1475 break;
1476 }
1477 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001478 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001479 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001480 assert(BC != 0 && "Failed to emit a bitcast!");
1481 Arg = BC;
1482 ArgVT = VA.getLocVT();
1483 break;
1484 }
1485 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001486 }
1487
1488 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001489 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001490 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001491 VA.getLocReg())
1492 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001493 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001494 } else if (VA.needsCustom()) {
1495 // TODO: We need custom lowering for vector (v2f64) args.
1496 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001497
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001498 CCValAssign &NextVA = ArgLocs[++i];
1499
1500 // TODO: Only handle register args for now.
1501 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1502
1503 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1504 TII.get(ARM::VMOVRRD), VA.getLocReg())
1505 .addReg(NextVA.getLocReg(), RegState::Define)
1506 .addReg(Arg));
1507 RegArgs.push_back(VA.getLocReg());
1508 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001509 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001510 assert(VA.isMemLoc());
1511 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001512 Address Addr;
1513 Addr.BaseType = Address::RegBase;
1514 Addr.Base.Reg = ARM::SP;
1515 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001516
Eric Christopher0d581222010-11-19 22:30:02 +00001517 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001518 }
1519 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001520 return true;
1521}
1522
Duncan Sands1440e8b2010-11-03 11:35:31 +00001523bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001524 const Instruction *I, CallingConv::ID CC,
1525 unsigned &NumBytes) {
1526 // Issue CALLSEQ_END
1527 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001528 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1529 TII.get(AdjStackUp))
1530 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001531
1532 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001533 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001534 SmallVector<CCValAssign, 16> RVLocs;
1535 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1536 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1537
1538 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001539 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001540 // For this move we copy into two registers and then move into the
1541 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001542 EVT DestVT = RVLocs[0].getValVT();
1543 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1544 unsigned ResultReg = createResultReg(DstRC);
1545 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1546 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001547 .addReg(RVLocs[0].getLocReg())
1548 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001549
Eric Christopher3659ac22010-10-20 08:02:24 +00001550 UsedRegs.push_back(RVLocs[0].getLocReg());
1551 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001552
Eric Christopherdccd2c32010-10-11 08:38:55 +00001553 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001554 UpdateValueMap(I, ResultReg);
1555 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001556 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001557 EVT CopyVT = RVLocs[0].getValVT();
1558 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001559
Eric Christopher14df8822010-10-01 00:00:11 +00001560 unsigned ResultReg = createResultReg(DstRC);
1561 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1562 ResultReg).addReg(RVLocs[0].getLocReg());
1563 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001564
Eric Christopherdccd2c32010-10-11 08:38:55 +00001565 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001566 UpdateValueMap(I, ResultReg);
1567 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001568 }
1569
Eric Christopherdccd2c32010-10-11 08:38:55 +00001570 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001571}
1572
Eric Christopher4f512ef2010-10-22 01:28:00 +00001573bool ARMFastISel::SelectRet(const Instruction *I) {
1574 const ReturnInst *Ret = cast<ReturnInst>(I);
1575 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001576
Eric Christopher4f512ef2010-10-22 01:28:00 +00001577 if (!FuncInfo.CanLowerReturn)
1578 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001579
Eric Christopher4f512ef2010-10-22 01:28:00 +00001580 if (F.isVarArg())
1581 return false;
1582
1583 CallingConv::ID CC = F.getCallingConv();
1584 if (Ret->getNumOperands() > 0) {
1585 SmallVector<ISD::OutputArg, 4> Outs;
1586 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1587 Outs, TLI);
1588
1589 // Analyze operands of the call, assigning locations to each operand.
1590 SmallVector<CCValAssign, 16> ValLocs;
1591 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1592 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1593
1594 const Value *RV = Ret->getOperand(0);
1595 unsigned Reg = getRegForValue(RV);
1596 if (Reg == 0)
1597 return false;
1598
1599 // Only handle a single return value for now.
1600 if (ValLocs.size() != 1)
1601 return false;
1602
1603 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001604
Eric Christopher4f512ef2010-10-22 01:28:00 +00001605 // Don't bother handling odd stuff for now.
1606 if (VA.getLocInfo() != CCValAssign::Full)
1607 return false;
1608 // Only handle register returns for now.
1609 if (!VA.isRegLoc())
1610 return false;
1611 // TODO: For now, don't try to handle cases where getLocInfo()
1612 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001613 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001614 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001615
Eric Christopher4f512ef2010-10-22 01:28:00 +00001616 // Make the copy.
1617 unsigned SrcReg = Reg + VA.getValNo();
1618 unsigned DstReg = VA.getLocReg();
1619 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1620 // Avoid a cross-class copy. This is very unlikely.
1621 if (!SrcRC->contains(DstReg))
1622 return false;
1623 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1624 DstReg).addReg(SrcReg);
1625
1626 // Mark the register as live out of the function.
1627 MRI.addLiveOut(VA.getLocReg());
1628 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001629
Eric Christopher4f512ef2010-10-22 01:28:00 +00001630 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1631 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1632 TII.get(RetOpc)));
1633 return true;
1634}
1635
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001636// A quick function that will emit a call for a named libcall in F with the
1637// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001638// can emit a call for any libcall we can produce. This is an abridged version
1639// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001640// like computed function pointers or strange arguments at call sites.
1641// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1642// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001643bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1644 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001645
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001646 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001647 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001648 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001649 if (RetTy->isVoidTy())
1650 RetVT = MVT::isVoid;
1651 else if (!isTypeLegal(RetTy, RetVT))
1652 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001653
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001654 // For now we're using BLX etc on the assumption that we have v5t ops.
1655 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001656
Eric Christopher836c6242010-12-15 23:47:29 +00001657 // TODO: For now if we have long calls specified we don't handle the call.
1658 if (EnableARMLongCalls) return false;
1659
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001660 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001661 SmallVector<Value*, 8> Args;
1662 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001663 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001664 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1665 Args.reserve(I->getNumOperands());
1666 ArgRegs.reserve(I->getNumOperands());
1667 ArgVTs.reserve(I->getNumOperands());
1668 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001669 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001670 Value *Op = I->getOperand(i);
1671 unsigned Arg = getRegForValue(Op);
1672 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001673
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001674 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001675 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001676 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001677
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001678 ISD::ArgFlagsTy Flags;
1679 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1680 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001681
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001682 Args.push_back(Op);
1683 ArgRegs.push_back(Arg);
1684 ArgVTs.push_back(ArgVT);
1685 ArgFlags.push_back(Flags);
1686 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001687
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001688 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001689 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001690 unsigned NumBytes;
1691 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1692 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001693
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001694 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001695 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001696 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001697 unsigned CallOpc;
Eric Christopherc19aadb2010-12-21 03:50:43 +00001698 if(isThumb) {
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001699 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc19aadb2010-12-21 03:50:43 +00001700 // Explicitly adding the predicate here.
1701 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1702 TII.get(CallOpc)))
1703 .addExternalSymbol(TLI.getLibcallName(Call));
1704 } else {
Eric Christopherc1095562010-09-18 02:32:38 +00001705 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherc19aadb2010-12-21 03:50:43 +00001706 // Explicitly adding the predicate here.
1707 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1708 TII.get(CallOpc))
1709 .addExternalSymbol(TLI.getLibcallName(Call)));
1710 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001711
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001712 // Add implicit physical register uses to the call.
1713 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1714 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001715
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001716 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001717 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001718 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001719
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001720 // Set all unused physreg defs as dead.
1721 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001722
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001723 return true;
1724}
1725
Eric Christopherf9764fa2010-09-30 20:49:44 +00001726bool ARMFastISel::SelectCall(const Instruction *I) {
1727 const CallInst *CI = cast<CallInst>(I);
1728 const Value *Callee = CI->getCalledValue();
1729
1730 // Can't handle inline asm or worry about intrinsics yet.
1731 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1732
Eric Christophere6ca6772010-10-01 21:33:12 +00001733 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001734 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001735 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1736 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001737
Eric Christopherf9764fa2010-09-30 20:49:44 +00001738 // Check the calling convention.
1739 ImmutableCallSite CS(CI);
1740 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001741
Eric Christopherf9764fa2010-09-30 20:49:44 +00001742 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001743
Eric Christopherf9764fa2010-09-30 20:49:44 +00001744 // Let SDISel handle vararg functions.
1745 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1746 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1747 if (FTy->isVarArg())
1748 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001749
Eric Christopherf9764fa2010-09-30 20:49:44 +00001750 // Handle *simple* calls for now.
1751 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001752 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001753 if (RetTy->isVoidTy())
1754 RetVT = MVT::isVoid;
1755 else if (!isTypeLegal(RetTy, RetVT))
1756 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001757
Eric Christopherf9764fa2010-09-30 20:49:44 +00001758 // For now we're using BLX etc on the assumption that we have v5t ops.
1759 // TODO: Maybe?
1760 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001761
Eric Christopher836c6242010-12-15 23:47:29 +00001762 // TODO: For now if we have long calls specified we don't handle the call.
1763 if (EnableARMLongCalls) return false;
1764
Eric Christopherf9764fa2010-09-30 20:49:44 +00001765 // Set up the argument vectors.
1766 SmallVector<Value*, 8> Args;
1767 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001768 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001769 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1770 Args.reserve(CS.arg_size());
1771 ArgRegs.reserve(CS.arg_size());
1772 ArgVTs.reserve(CS.arg_size());
1773 ArgFlags.reserve(CS.arg_size());
1774 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1775 i != e; ++i) {
1776 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001777
Eric Christopherf9764fa2010-09-30 20:49:44 +00001778 if (Arg == 0)
1779 return false;
1780 ISD::ArgFlagsTy Flags;
1781 unsigned AttrInd = i - CS.arg_begin() + 1;
1782 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1783 Flags.setSExt();
1784 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1785 Flags.setZExt();
1786
1787 // FIXME: Only handle *easy* calls for now.
1788 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1789 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1790 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1791 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1792 return false;
1793
1794 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001795 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001796 if (!isTypeLegal(ArgTy, ArgVT))
1797 return false;
1798 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1799 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001800
Eric Christopherf9764fa2010-09-30 20:49:44 +00001801 Args.push_back(*i);
1802 ArgRegs.push_back(Arg);
1803 ArgVTs.push_back(ArgVT);
1804 ArgFlags.push_back(Flags);
1805 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001806
Eric Christopherf9764fa2010-09-30 20:49:44 +00001807 // Handle the arguments now that we've gotten them.
1808 SmallVector<unsigned, 4> RegArgs;
1809 unsigned NumBytes;
1810 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1811 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001812
Eric Christopherf9764fa2010-09-30 20:49:44 +00001813 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001814 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001815 MachineInstrBuilder MIB;
1816 unsigned CallOpc;
Eric Christopher7bb59962010-11-29 21:56:23 +00001817 // Explicitly adding the predicate here.
Eric Christopherc19aadb2010-12-21 03:50:43 +00001818 if(isThumb) {
1819 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1820 // Explicitly adding the predicate here.
1821 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1822 TII.get(CallOpc)))
1823 .addGlobalAddress(GV, 0, 0);
1824 } else {
1825 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1826 // Explicitly adding the predicate here.
1827 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1828 TII.get(CallOpc))
1829 .addGlobalAddress(GV, 0, 0));
1830 }
1831
Eric Christopherf9764fa2010-09-30 20:49:44 +00001832 // Add implicit physical register uses to the call.
1833 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1834 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001835
Eric Christopherf9764fa2010-09-30 20:49:44 +00001836 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001837 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001838 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001839
Eric Christopherf9764fa2010-09-30 20:49:44 +00001840 // Set all unused physreg defs as dead.
1841 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001842
Eric Christopherf9764fa2010-09-30 20:49:44 +00001843 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001844
Eric Christopherf9764fa2010-09-30 20:49:44 +00001845}
1846
Eric Christopher56d2b722010-09-02 23:43:26 +00001847// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001848bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001849
Eric Christopherab695882010-07-21 22:26:11 +00001850 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001851 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001852 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001853 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001854 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001855 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001856 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001857 case Instruction::ICmp:
1858 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001859 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001860 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001861 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001862 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001863 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001864 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001865 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001866 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001867 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001868 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001869 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001870 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001871 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001872 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001873 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001874 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001875 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001876 case Instruction::SRem:
1877 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001878 case Instruction::Call:
1879 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001880 case Instruction::Select:
1881 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001882 case Instruction::Ret:
1883 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001884 default: break;
1885 }
1886 return false;
1887}
1888
1889namespace llvm {
1890 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001891 // Completely untested on non-darwin.
1892 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001893
Eric Christopheraaa8df42010-11-02 01:21:28 +00001894 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001895 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001896 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001897 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001898 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001899 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001900 }
1901}