blob: 5fc86017a26db4fa0217cfc626cc48d5b0c2c6d8 [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 Christopherab695882010-07-21 22:26:11 +000055namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000056
Eric Christopher0d581222010-11-19 22:30:02 +000057 // All possible address modes, plus some.
58 typedef struct Address {
59 enum {
60 RegBase,
61 FrameIndexBase
62 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000063
Eric Christopher0d581222010-11-19 22:30:02 +000064 union {
65 unsigned Reg;
66 int FI;
67 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000068
Eric Christopher0d581222010-11-19 22:30:02 +000069 int Offset;
70 unsigned Scale;
71 unsigned PlusReg;
Eric Christopher827656d2010-11-20 22:38:27 +000072
Eric Christopher0d581222010-11-19 22:30:02 +000073 // Innocuous defaults for our address.
74 Address()
75 : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) {
76 Base.Reg = 0;
77 }
78 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000079
80class ARMFastISel : public FastISel {
81
82 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
83 /// make the right decision when generating code for different targets.
84 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000085 const TargetMachine &TM;
86 const TargetInstrInfo &TII;
87 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000088 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000089
Eric Christopher8cf6c602010-09-29 22:24:45 +000090 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000091 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000092 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000093
Eric Christopherab695882010-07-21 22:26:11 +000094 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000095 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000096 : FastISel(funcInfo),
97 TM(funcInfo.MF->getTarget()),
98 TII(*TM.getInstrInfo()),
99 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000100 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000101 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +0000102 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000103 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000104 }
105
Eric Christophercb592292010-08-20 00:20:31 +0000106 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000107 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
108 const TargetRegisterClass *RC);
109 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC,
111 unsigned Op0, bool Op0IsKill);
112 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
113 const TargetRegisterClass *RC,
114 unsigned Op0, bool Op0IsKill,
115 unsigned Op1, bool Op1IsKill);
116 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
117 const TargetRegisterClass *RC,
118 unsigned Op0, bool Op0IsKill,
119 uint64_t Imm);
120 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
121 const TargetRegisterClass *RC,
122 unsigned Op0, bool Op0IsKill,
123 const ConstantFP *FPImm);
124 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
125 const TargetRegisterClass *RC,
126 uint64_t Imm);
127 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
128 const TargetRegisterClass *RC,
129 unsigned Op0, bool Op0IsKill,
130 unsigned Op1, bool Op1IsKill,
131 uint64_t Imm);
132 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
133 unsigned Op0, bool Op0IsKill,
134 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000135
Eric Christophercb592292010-08-20 00:20:31 +0000136 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000137 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000138 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000139 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000140
141 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000142
Eric Christopher83007122010-08-23 21:44:12 +0000143 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000144 private:
Eric Christopher17787722010-10-21 21:47:51 +0000145 bool SelectLoad(const Instruction *I);
146 bool SelectStore(const Instruction *I);
147 bool SelectBranch(const Instruction *I);
148 bool SelectCmp(const Instruction *I);
149 bool SelectFPExt(const Instruction *I);
150 bool SelectFPTrunc(const Instruction *I);
151 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
152 bool SelectSIToFP(const Instruction *I);
153 bool SelectFPToSI(const Instruction *I);
154 bool SelectSDiv(const Instruction *I);
155 bool SelectSRem(const Instruction *I);
156 bool SelectCall(const Instruction *I);
157 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000158 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000159
Eric Christopher83007122010-08-23 21:44:12 +0000160 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000161 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000162 bool isTypeLegal(const Type *Ty, MVT &VT);
163 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher0d581222010-11-19 22:30:02 +0000164 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
165 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
166 bool ARMComputeAddress(const Value *Obj, Address &Addr);
167 void ARMSimplifyAddress(Address &Addr, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000168 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000169 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000170 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000171 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000172 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000173
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000174 // Call handling routines.
175 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000176 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
177 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000178 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000179 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000180 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000181 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000182 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
183 SmallVectorImpl<unsigned> &RegArgs,
184 CallingConv::ID CC,
185 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000186 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000187 const Instruction *I, CallingConv::ID CC,
188 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000189 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000190
191 // OptionalDef handling routines.
192 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000193 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
194 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000195 void AddLoadStoreOperands(EVT VT, Address &Addr,
196 const MachineInstrBuilder &MIB);
Eric Christopher456144e2010-08-19 00:37:05 +0000197};
Eric Christopherab695882010-07-21 22:26:11 +0000198
199} // end anonymous namespace
200
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000201#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000202
Eric Christopher456144e2010-08-19 00:37:05 +0000203// DefinesOptionalPredicate - This is different from DefinesPredicate in that
204// we don't care about implicit defs here, just places we'll need to add a
205// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
206bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
207 const TargetInstrDesc &TID = MI->getDesc();
208 if (!TID.hasOptionalDef())
209 return false;
210
211 // Look to see if our OptionalDef is defining CPSR or CCR.
212 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
213 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000214 if (!MO.isReg() || !MO.isDef()) continue;
215 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000216 *CPSR = true;
217 }
218 return true;
219}
220
221// If the machine is predicable go ahead and add the predicate operands, if
222// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000223// TODO: If we want to support thumb1 then we'll need to deal with optional
224// CPSR defs that need to be added before the remaining operands. See s_cc_out
225// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000226const MachineInstrBuilder &
227ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
228 MachineInstr *MI = &*MIB;
229
230 // Do we use a predicate?
231 if (TII.isPredicable(MI))
232 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000233
Eric Christopher456144e2010-08-19 00:37:05 +0000234 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
235 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000236 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000237 if (DefinesOptionalPredicate(MI, &CPSR)) {
238 if (CPSR)
239 AddDefaultT1CC(MIB);
240 else
241 AddDefaultCC(MIB);
242 }
243 return MIB;
244}
245
Eric Christopher0fe7d542010-08-17 01:25:29 +0000246unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
247 const TargetRegisterClass* RC) {
248 unsigned ResultReg = createResultReg(RC);
249 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
250
Eric Christopher456144e2010-08-19 00:37:05 +0000251 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000252 return ResultReg;
253}
254
255unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
256 const TargetRegisterClass *RC,
257 unsigned Op0, bool Op0IsKill) {
258 unsigned ResultReg = createResultReg(RC);
259 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
260
261 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000262 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000263 .addReg(Op0, Op0IsKill * RegState::Kill));
264 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000265 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000266 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000267 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000268 TII.get(TargetOpcode::COPY), ResultReg)
269 .addReg(II.ImplicitDefs[0]));
270 }
271 return ResultReg;
272}
273
274unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
275 const TargetRegisterClass *RC,
276 unsigned Op0, bool Op0IsKill,
277 unsigned Op1, bool Op1IsKill) {
278 unsigned ResultReg = createResultReg(RC);
279 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
280
281 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000282 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000283 .addReg(Op0, Op0IsKill * RegState::Kill)
284 .addReg(Op1, Op1IsKill * RegState::Kill));
285 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000287 .addReg(Op0, Op0IsKill * RegState::Kill)
288 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000289 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000290 TII.get(TargetOpcode::COPY), ResultReg)
291 .addReg(II.ImplicitDefs[0]));
292 }
293 return ResultReg;
294}
295
296unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
297 const TargetRegisterClass *RC,
298 unsigned Op0, bool Op0IsKill,
299 uint64_t Imm) {
300 unsigned ResultReg = createResultReg(RC);
301 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
302
303 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 .addReg(Op0, Op0IsKill * RegState::Kill)
306 .addImm(Imm));
307 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 .addReg(Op0, Op0IsKill * RegState::Kill)
310 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000312 TII.get(TargetOpcode::COPY), ResultReg)
313 .addReg(II.ImplicitDefs[0]));
314 }
315 return ResultReg;
316}
317
318unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
319 const TargetRegisterClass *RC,
320 unsigned Op0, bool Op0IsKill,
321 const ConstantFP *FPImm) {
322 unsigned ResultReg = createResultReg(RC);
323 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
324
325 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000327 .addReg(Op0, Op0IsKill * RegState::Kill)
328 .addFPImm(FPImm));
329 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000331 .addReg(Op0, Op0IsKill * RegState::Kill)
332 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000334 TII.get(TargetOpcode::COPY), ResultReg)
335 .addReg(II.ImplicitDefs[0]));
336 }
337 return ResultReg;
338}
339
340unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
341 const TargetRegisterClass *RC,
342 unsigned Op0, bool Op0IsKill,
343 unsigned Op1, bool Op1IsKill,
344 uint64_t Imm) {
345 unsigned ResultReg = createResultReg(RC);
346 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
347
348 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000350 .addReg(Op0, Op0IsKill * RegState::Kill)
351 .addReg(Op1, Op1IsKill * RegState::Kill)
352 .addImm(Imm));
353 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000354 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000355 .addReg(Op0, Op0IsKill * RegState::Kill)
356 .addReg(Op1, Op1IsKill * RegState::Kill)
357 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000358 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000359 TII.get(TargetOpcode::COPY), ResultReg)
360 .addReg(II.ImplicitDefs[0]));
361 }
362 return ResultReg;
363}
364
365unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
366 const TargetRegisterClass *RC,
367 uint64_t Imm) {
368 unsigned ResultReg = createResultReg(RC);
369 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000370
Eric Christopher0fe7d542010-08-17 01:25:29 +0000371 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000372 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000373 .addImm(Imm));
374 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000375 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000376 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000378 TII.get(TargetOpcode::COPY), ResultReg)
379 .addReg(II.ImplicitDefs[0]));
380 }
381 return ResultReg;
382}
383
384unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
385 unsigned Op0, bool Op0IsKill,
386 uint32_t Idx) {
387 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
388 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
389 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000390 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000391 DL, TII.get(TargetOpcode::COPY), ResultReg)
392 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
393 return ResultReg;
394}
395
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000396// TODO: Don't worry about 64-bit now, but when this is fixed remove the
397// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000398unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000399 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000400
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000401 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
402 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
403 TII.get(ARM::VMOVRS), MoveReg)
404 .addReg(SrcReg));
405 return MoveReg;
406}
407
408unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000409 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000410
Eric Christopheraa3ace12010-09-09 20:49:25 +0000411 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
412 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000413 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000414 .addReg(SrcReg));
415 return MoveReg;
416}
417
Eric Christopher9ed58df2010-09-09 00:19:41 +0000418// For double width floating point we need to materialize two constants
419// (the high and the low) into integer registers then use a move to get
420// the combined constant into an FP reg.
421unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
422 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000423 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000424
Eric Christopher9ed58df2010-09-09 00:19:41 +0000425 // This checks to see if we can use VFP3 instructions to materialize
426 // a constant, otherwise we have to go through the constant pool.
427 if (TLI.isFPImmLegal(Val, VT)) {
428 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
429 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
430 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
431 DestReg)
432 .addFPImm(CFP));
433 return DestReg;
434 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000435
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000436 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000437 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000438
Eric Christopher238bb162010-09-09 23:50:00 +0000439 // MachineConstantPool wants an explicit alignment.
440 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
441 if (Align == 0) {
442 // TODO: Figure out if this is correct.
443 Align = TD.getTypeAllocSize(CFP->getType());
444 }
445 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
446 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
447 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000448
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000449 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000450 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
451 DestReg)
452 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000453 .addReg(0));
454 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000455}
456
Eric Christopher744c7c82010-09-28 22:47:54 +0000457unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000458
Eric Christopher744c7c82010-09-28 22:47:54 +0000459 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000460 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000461
Eric Christophere5b13cf2010-11-03 20:21:17 +0000462 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
463
464 // If we can do this in a single instruction without a constant pool entry
465 // do so now.
466 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000467 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000468 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
469 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000470 TII.get(Opc), DestReg)
471 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000472 return DestReg;
473 }
474
Eric Christopher56d2b722010-09-02 23:43:26 +0000475 // MachineConstantPool wants an explicit alignment.
476 unsigned Align = TD.getPrefTypeAlignment(C->getType());
477 if (Align == 0) {
478 // TODO: Figure out if this is correct.
479 Align = TD.getTypeAllocSize(C->getType());
480 }
481 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000482
Eric Christopher56d2b722010-09-02 23:43:26 +0000483 if (isThumb)
484 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000485 TII.get(ARM::t2LDRpci), DestReg)
486 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000487 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000488 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000489 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000490 TII.get(ARM::LDRcp), DestReg)
491 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000492 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000493
Eric Christopher56d2b722010-09-02 23:43:26 +0000494 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000495}
496
Eric Christopherc9932f62010-10-01 23:24:42 +0000497unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000498 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000499 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000500
Eric Christopher890dbbe2010-10-02 00:32:44 +0000501 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000502
Eric Christopher890dbbe2010-10-02 00:32:44 +0000503 // TODO: No external globals for now.
504 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000505
Eric Christopher890dbbe2010-10-02 00:32:44 +0000506 // TODO: Need more magic for ARM PIC.
507 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000508
Eric Christopher890dbbe2010-10-02 00:32:44 +0000509 // MachineConstantPool wants an explicit alignment.
510 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
511 if (Align == 0) {
512 // TODO: Figure out if this is correct.
513 Align = TD.getTypeAllocSize(GV->getType());
514 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000515
Eric Christopher890dbbe2010-10-02 00:32:44 +0000516 // Grab index.
517 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
518 unsigned Id = AFI->createConstPoolEntryUId();
519 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
520 ARMCP::CPValue, PCAdj);
521 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000522
Eric Christopher890dbbe2010-10-02 00:32:44 +0000523 // Load value.
524 MachineInstrBuilder MIB;
525 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
526 if (isThumb) {
527 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
528 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
529 .addConstantPoolIndex(Idx);
530 if (RelocM == Reloc::PIC_)
531 MIB.addImm(Id);
532 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000533 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000534 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
535 DestReg)
536 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000537 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000538 }
539 AddOptionalDefs(MIB);
540 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000541}
542
Eric Christopher9ed58df2010-09-09 00:19:41 +0000543unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
544 EVT VT = TLI.getValueType(C->getType(), true);
545
546 // Only handle simple types.
547 if (!VT.isSimple()) return 0;
548
549 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
550 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000551 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
552 return ARMMaterializeGV(GV, VT);
553 else if (isa<ConstantInt>(C))
554 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000555
Eric Christopherc9932f62010-10-01 23:24:42 +0000556 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000557}
558
Eric Christopherf9764fa2010-09-30 20:49:44 +0000559unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
560 // Don't handle dynamic allocas.
561 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000562
Duncan Sands1440e8b2010-11-03 11:35:31 +0000563 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000564 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000565
Eric Christopherf9764fa2010-09-30 20:49:44 +0000566 DenseMap<const AllocaInst*, int>::iterator SI =
567 FuncInfo.StaticAllocaMap.find(AI);
568
569 // This will get lowered later into the correct offsets and registers
570 // via rewriteXFrameIndex.
571 if (SI != FuncInfo.StaticAllocaMap.end()) {
572 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
573 unsigned ResultReg = createResultReg(RC);
574 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
575 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
576 TII.get(Opc), ResultReg)
577 .addFrameIndex(SI->second)
578 .addImm(0));
579 return ResultReg;
580 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000581
Eric Christopherf9764fa2010-09-30 20:49:44 +0000582 return 0;
583}
584
Duncan Sands1440e8b2010-11-03 11:35:31 +0000585bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
586 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000587
Eric Christopherb1cc8482010-08-25 07:23:49 +0000588 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000589 if (evt == MVT::Other || !evt.isSimple()) return false;
590 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000591
Eric Christopherdc908042010-08-31 01:28:42 +0000592 // Handle all legal types, i.e. a register that will directly hold this
593 // value.
594 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000595}
596
Duncan Sands1440e8b2010-11-03 11:35:31 +0000597bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000598 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000599
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000600 // If this is a type than can be sign or zero-extended to a basic operation
601 // go ahead and accept it now.
602 if (VT == MVT::i8 || VT == MVT::i16)
603 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000604
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000605 return false;
606}
607
Eric Christopher88de86b2010-11-19 22:36:41 +0000608// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000609bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000610 // Some boilerplate from the X86 FastISel.
611 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000612 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000613 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000614 // Don't walk into other basic blocks unless the object is an alloca from
615 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000616 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
617 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
618 Opcode = I->getOpcode();
619 U = I;
620 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000621 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000622 Opcode = C->getOpcode();
623 U = C;
624 }
625
Eric Christophercb0b04b2010-08-24 00:07:24 +0000626 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000627 if (Ty->getAddressSpace() > 255)
628 // Fast instruction selection doesn't support the special
629 // address spaces.
630 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000631
Eric Christopher83007122010-08-23 21:44:12 +0000632 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000633 default:
Eric Christopher83007122010-08-23 21:44:12 +0000634 break;
Eric Christopher55324332010-10-12 00:43:21 +0000635 case Instruction::BitCast: {
636 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000637 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000638 }
639 case Instruction::IntToPtr: {
640 // Look past no-op inttoptrs.
641 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000642 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000643 break;
644 }
645 case Instruction::PtrToInt: {
646 // Look past no-op ptrtoints.
647 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000648 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000649 break;
650 }
Eric Christophereae84392010-10-14 09:29:41 +0000651 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000652 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000653 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000654
Eric Christophereae84392010-10-14 09:29:41 +0000655 // Iterate through the GEP folding the constants into offsets where
656 // we can.
657 gep_type_iterator GTI = gep_type_begin(U);
658 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
659 i != e; ++i, ++GTI) {
660 const Value *Op = *i;
661 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
662 const StructLayout *SL = TD.getStructLayout(STy);
663 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
664 TmpOffset += SL->getElementOffset(Idx);
665 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000666 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
667 SmallVector<const Value *, 4> Worklist;
668 Worklist.push_back(Op);
669 do {
670 Op = Worklist.pop_back_val();
671 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
672 // Constant-offset addressing.
673 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000674 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000675 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
676 // An add with a constant operand. Fold the constant.
677 ConstantInt *CI =
678 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
679 TmpOffset += CI->getSExtValue() * S;
680 // Add the other operand back to the work list.
681 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
682 } else
683 goto unsupported_gep;
684 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000685 }
686 }
Eric Christopher2896df82010-10-15 18:02:07 +0000687
688 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000689 Addr.Offset = TmpOffset;
690 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000691
692 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000693 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000694
Eric Christophereae84392010-10-14 09:29:41 +0000695 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000696 break;
697 }
Eric Christopher83007122010-08-23 21:44:12 +0000698 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000699 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000700 DenseMap<const AllocaInst*, int>::iterator SI =
701 FuncInfo.StaticAllocaMap.find(AI);
702 if (SI != FuncInfo.StaticAllocaMap.end()) {
703 Addr.BaseType = Address::FrameIndexBase;
704 Addr.Base.FI = SI->second;
705 return true;
706 }
707 break;
Eric Christopher83007122010-08-23 21:44:12 +0000708 }
709 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000710
Eric Christophera9c57512010-10-13 21:41:51 +0000711 // Materialize the global variable's address into a reg which can
712 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000713 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000714 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
715 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000716
Eric Christopher0d581222010-11-19 22:30:02 +0000717 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000718 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000719 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000720
Eric Christophercb0b04b2010-08-24 00:07:24 +0000721 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000722 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
723 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000724}
725
Eric Christopher0d581222010-11-19 22:30:02 +0000726void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000727
Eric Christopher212ae932010-10-21 19:40:30 +0000728 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000729
Eric Christopher212ae932010-10-21 19:40:30 +0000730 bool needsLowering = false;
731 switch (VT.getSimpleVT().SimpleTy) {
732 default:
733 assert(false && "Unhandled load/store type!");
734 case MVT::i1:
735 case MVT::i8:
736 case MVT::i16:
737 case MVT::i32:
738 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000739 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000740 break;
741 case MVT::f32:
742 case MVT::f64:
743 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000744 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000745 break;
746 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000747
Eric Christopher827656d2010-11-20 22:38:27 +0000748 // If this is a stack pointer and the offset needs to be simplified then
749 // put the alloca address into a register, set the base type back to
750 // register and continue. This should almost never happen.
751 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
752 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
753 ARM::GPRRegisterClass;
754 unsigned ResultReg = createResultReg(RC);
755 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
756 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
757 TII.get(Opc), ResultReg)
758 .addFrameIndex(Addr.Base.FI)
759 .addImm(0));
760 Addr.Base.Reg = ResultReg;
761 Addr.BaseType = Address::RegBase;
762 }
763
Eric Christopher212ae932010-10-21 19:40:30 +0000764 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000765 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000766 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000767 ARMCC::CondCodes Pred = ARMCC::AL;
768 unsigned PredReg = 0;
769
Eric Christopher2896df82010-10-15 18:02:07 +0000770 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
771 ARM::GPRRegisterClass;
772 unsigned BaseReg = createResultReg(RC);
773
Eric Christophereaa204b2010-09-02 01:39:14 +0000774 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000775 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000776 BaseReg, Addr.Base.Reg, Addr.Offset,
777 Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000778 static_cast<const ARMBaseInstrInfo&>(TII));
779 else {
780 assert(AFI->isThumb2Function());
781 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000782 BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000783 static_cast<const ARMBaseInstrInfo&>(TII));
784 }
Eric Christopher0d581222010-11-19 22:30:02 +0000785 Addr.Offset = 0;
786 Addr.Base.Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000787 }
Eric Christopher83007122010-08-23 21:44:12 +0000788}
789
Eric Christopher564857f2010-12-01 01:40:24 +0000790void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
791 const MachineInstrBuilder &MIB) {
792 // addrmode5 output depends on the selection dag addressing dividing the
793 // offset by 4 that it then later multiplies. Do this here as well.
794 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
795 VT.getSimpleVT().SimpleTy == MVT::f64)
796 Addr.Offset /= 4;
797
798 // Frame base works a bit differently. Handle it separately.
799 if (Addr.BaseType == Address::FrameIndexBase) {
800 int FI = Addr.Base.FI;
801 int Offset = Addr.Offset;
802 MachineMemOperand *MMO =
803 FuncInfo.MF->getMachineMemOperand(
804 MachinePointerInfo::getFixedStack(FI, Offset),
805 MachineMemOperand::MOLoad,
806 MFI.getObjectSize(FI),
807 MFI.getObjectAlignment(FI));
808 // Now add the rest of the operands.
809 MIB.addFrameIndex(FI);
810
811 // ARM halfword load/stores need an additional operand.
812 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
813
814 MIB.addImm(Addr.Offset);
815 MIB.addMemOperand(MMO);
816 } else {
817 // Now add the rest of the operands.
818 MIB.addReg(Addr.Base.Reg);
819
820 // ARM halfword load/stores need an additional operand.
821 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
822
823 MIB.addImm(Addr.Offset);
824 }
825 AddOptionalDefs(MIB);
826}
827
Eric Christopher0d581222010-11-19 22:30:02 +0000828bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000829
Eric Christopherb1cc8482010-08-25 07:23:49 +0000830 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000831 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000832 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000833 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000834 // This is mostly going to be Neon/vector support.
835 default: return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000836 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000837 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000838 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000839 break;
840 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000841 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000842 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000843 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000844 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000845 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000846 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000847 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000848 case MVT::f32:
849 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000850 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000851 break;
852 case MVT::f64:
853 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000854 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000855 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000856 }
Eric Christopher564857f2010-12-01 01:40:24 +0000857 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000858 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000859
Eric Christopher564857f2010-12-01 01:40:24 +0000860 // Create the base instruction, then add the operands.
861 ResultReg = createResultReg(RC);
862 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
863 TII.get(Opc), ResultReg);
864 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopherdc908042010-08-31 01:28:42 +0000865 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000866}
867
Eric Christopher43b62be2010-09-27 06:02:23 +0000868bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000869 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000870 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000871 if (!isLoadTypeLegal(I->getType(), VT))
872 return false;
873
Eric Christopher564857f2010-12-01 01:40:24 +0000874 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000875 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +0000876 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000877
878 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000879 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000880 UpdateValueMap(I, ResultReg);
881 return true;
882}
883
Eric Christopher0d581222010-11-19 22:30:02 +0000884bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000885 unsigned StrOpc;
886 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000887 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000888 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000889 case MVT::i1: {
890 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
891 ARM::GPRRegisterClass);
892 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
893 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
894 TII.get(Opc), Res)
895 .addReg(SrcReg).addImm(1));
896 SrcReg = Res;
897 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000898 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000899 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000900 break;
901 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000902 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000903 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000904 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000905 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000906 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000907 case MVT::f32:
908 if (!Subtarget->hasVFP2()) return false;
909 StrOpc = ARM::VSTRS;
910 break;
911 case MVT::f64:
912 if (!Subtarget->hasVFP2()) return false;
913 StrOpc = ARM::VSTRD;
914 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000915 }
Eric Christopher564857f2010-12-01 01:40:24 +0000916 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000917 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000918
Eric Christopher564857f2010-12-01 01:40:24 +0000919 // Create the base instruction, then add the operands.
920 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
921 TII.get(StrOpc))
922 .addReg(SrcReg, getKillRegState(true));
923 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000924 return true;
925}
926
Eric Christopher43b62be2010-09-27 06:02:23 +0000927bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000928 Value *Op0 = I->getOperand(0);
929 unsigned SrcReg = 0;
930
Eric Christopher564857f2010-12-01 01:40:24 +0000931 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000932 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000933 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000934 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000935
Eric Christopher1b61ef42010-09-02 01:48:11 +0000936 // Get the value to be stored into a register.
937 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +0000938 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000939
Eric Christopher564857f2010-12-01 01:40:24 +0000940 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000941 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000942 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000943 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000944
Eric Christopher0d581222010-11-19 22:30:02 +0000945 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +0000946 return true;
947}
948
949static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
950 switch (Pred) {
951 // Needs two compares...
952 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000953 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000954 default:
Eric Christopher4053e632010-11-02 01:24:49 +0000955 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +0000956 return ARMCC::AL;
957 case CmpInst::ICMP_EQ:
958 case CmpInst::FCMP_OEQ:
959 return ARMCC::EQ;
960 case CmpInst::ICMP_SGT:
961 case CmpInst::FCMP_OGT:
962 return ARMCC::GT;
963 case CmpInst::ICMP_SGE:
964 case CmpInst::FCMP_OGE:
965 return ARMCC::GE;
966 case CmpInst::ICMP_UGT:
967 case CmpInst::FCMP_UGT:
968 return ARMCC::HI;
969 case CmpInst::FCMP_OLT:
970 return ARMCC::MI;
971 case CmpInst::ICMP_ULE:
972 case CmpInst::FCMP_OLE:
973 return ARMCC::LS;
974 case CmpInst::FCMP_ORD:
975 return ARMCC::VC;
976 case CmpInst::FCMP_UNO:
977 return ARMCC::VS;
978 case CmpInst::FCMP_UGE:
979 return ARMCC::PL;
980 case CmpInst::ICMP_SLT:
981 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000982 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000983 case CmpInst::ICMP_SLE:
984 case CmpInst::FCMP_ULE:
985 return ARMCC::LE;
986 case CmpInst::FCMP_UNE:
987 case CmpInst::ICMP_NE:
988 return ARMCC::NE;
989 case CmpInst::ICMP_UGE:
990 return ARMCC::HS;
991 case CmpInst::ICMP_ULT:
992 return ARMCC::LO;
993 }
Eric Christopher543cf052010-09-01 22:16:27 +0000994}
995
Eric Christopher43b62be2010-09-27 06:02:23 +0000996bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000997 const BranchInst *BI = cast<BranchInst>(I);
998 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
999 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001000
Eric Christophere5734102010-09-03 00:35:47 +00001001 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001002
Eric Christopher0e6233b2010-10-29 21:08:19 +00001003 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1004 // behavior.
1005 // TODO: Factor this out.
1006 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1007 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001008 MVT VT;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001009 const Type *Ty = CI->getOperand(0)->getType();
Eric Christopher76d61472010-10-30 21:25:26 +00001010 if (!isTypeLegal(Ty, VT))
1011 return false;
1012
Eric Christopher0e6233b2010-10-29 21:08:19 +00001013 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1014 if (isFloat && !Subtarget->hasVFP2())
1015 return false;
1016
1017 unsigned CmpOpc;
1018 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001019 switch (VT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001020 default: return false;
1021 // TODO: Verify compares.
1022 case MVT::f32:
1023 CmpOpc = ARM::VCMPES;
1024 CondReg = ARM::FPSCR;
1025 break;
1026 case MVT::f64:
1027 CmpOpc = ARM::VCMPED;
1028 CondReg = ARM::FPSCR;
1029 break;
1030 case MVT::i32:
1031 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
1032 CondReg = ARM::CPSR;
1033 break;
1034 }
1035
1036 // Get the compare predicate.
1037 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1038
1039 // We may not handle every CC for now.
1040 if (ARMPred == ARMCC::AL) return false;
1041
1042 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1043 if (Arg1 == 0) return false;
1044
1045 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1046 if (Arg2 == 0) return false;
1047
1048 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1049 TII.get(CmpOpc))
1050 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001051
Eric Christopher0e6233b2010-10-29 21:08:19 +00001052 // For floating point we need to move the result to a comparison register
1053 // that we can then use for branches.
1054 if (isFloat)
1055 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1056 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001057
Eric Christopher0e6233b2010-10-29 21:08:19 +00001058 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1059 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1060 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1061 FastEmitBranch(FBB, DL);
1062 FuncInfo.MBB->addSuccessor(TBB);
1063 return true;
1064 }
1065 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001066
Eric Christopher0e6233b2010-10-29 21:08:19 +00001067 unsigned CmpReg = getRegForValue(BI->getCondition());
1068 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001069
Eric Christopher229207a2010-09-29 01:14:47 +00001070 // Re-set the flags just in case.
1071 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1072 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001073 .addReg(CmpReg).addImm(0));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001074
Eric Christophere5734102010-09-03 00:35:47 +00001075 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001076 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001077 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001078 FastEmitBranch(FBB, DL);
1079 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001080 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001081}
1082
Eric Christopher43b62be2010-09-27 06:02:23 +00001083bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001084 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001085
Duncan Sands1440e8b2010-11-03 11:35:31 +00001086 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001087 const Type *Ty = CI->getOperand(0)->getType();
1088 if (!isTypeLegal(Ty, VT))
1089 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001090
Eric Christopherd43393a2010-09-08 23:13:45 +00001091 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1092 if (isFloat && !Subtarget->hasVFP2())
1093 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001094
Eric Christopherd43393a2010-09-08 23:13:45 +00001095 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001096 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001097 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001098 default: return false;
1099 // TODO: Verify compares.
1100 case MVT::f32:
1101 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001102 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001103 break;
1104 case MVT::f64:
1105 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001106 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001107 break;
1108 case MVT::i32:
1109 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001110 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001111 break;
1112 }
1113
Eric Christopher229207a2010-09-29 01:14:47 +00001114 // Get the compare predicate.
1115 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001116
Eric Christopher229207a2010-09-29 01:14:47 +00001117 // We may not handle every CC for now.
1118 if (ARMPred == ARMCC::AL) return false;
1119
Eric Christopherd43393a2010-09-08 23:13:45 +00001120 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1121 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001122
Eric Christopherd43393a2010-09-08 23:13:45 +00001123 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1124 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001125
Eric Christopherd43393a2010-09-08 23:13:45 +00001126 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1127 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001128
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001129 // For floating point we need to move the result to a comparison register
1130 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001131 if (isFloat)
1132 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1133 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001134
Eric Christopher229207a2010-09-29 01:14:47 +00001135 // Now set a register based on the comparison. Explicitly set the predicates
1136 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001137 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001138 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001139 : ARM::GPRRegisterClass;
1140 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001141 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001142 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001143 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1145 .addReg(ZeroReg).addImm(1)
1146 .addImm(ARMPred).addReg(CondReg);
1147
Eric Christophera5b1e682010-09-17 22:28:18 +00001148 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001149 return true;
1150}
1151
Eric Christopher43b62be2010-09-27 06:02:23 +00001152bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001153 // Make sure we have VFP and that we're extending float to double.
1154 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001155
Eric Christopher46203602010-09-09 00:26:48 +00001156 Value *V = I->getOperand(0);
1157 if (!I->getType()->isDoubleTy() ||
1158 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001159
Eric Christopher46203602010-09-09 00:26:48 +00001160 unsigned Op = getRegForValue(V);
1161 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001162
Eric Christopher46203602010-09-09 00:26:48 +00001163 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001164 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001165 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001166 .addReg(Op));
1167 UpdateValueMap(I, Result);
1168 return true;
1169}
1170
Eric Christopher43b62be2010-09-27 06:02:23 +00001171bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001172 // Make sure we have VFP and that we're truncating double to float.
1173 if (!Subtarget->hasVFP2()) return false;
1174
1175 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001176 if (!(I->getType()->isFloatTy() &&
1177 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001178
1179 unsigned Op = getRegForValue(V);
1180 if (Op == 0) return false;
1181
1182 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001183 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001184 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001185 .addReg(Op));
1186 UpdateValueMap(I, Result);
1187 return true;
1188}
1189
Eric Christopher43b62be2010-09-27 06:02:23 +00001190bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001191 // Make sure we have VFP.
1192 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001193
Duncan Sands1440e8b2010-11-03 11:35:31 +00001194 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001195 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001196 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001197 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001198
Eric Christopher9a040492010-09-09 18:54:59 +00001199 unsigned Op = getRegForValue(I->getOperand(0));
1200 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001201
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001202 // The conversion routine works on fp-reg to fp-reg and the operand above
1203 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001204 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001205 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001206
Eric Christopher9a040492010-09-09 18:54:59 +00001207 unsigned Opc;
1208 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1209 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1210 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001211
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001212 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001213 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1214 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001215 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001216 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001217 return true;
1218}
1219
Eric Christopher43b62be2010-09-27 06:02:23 +00001220bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001221 // Make sure we have VFP.
1222 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001223
Duncan Sands1440e8b2010-11-03 11:35:31 +00001224 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001225 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001226 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001227 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001228
Eric Christopher9a040492010-09-09 18:54:59 +00001229 unsigned Op = getRegForValue(I->getOperand(0));
1230 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001231
Eric Christopher9a040492010-09-09 18:54:59 +00001232 unsigned Opc;
1233 const Type *OpTy = I->getOperand(0)->getType();
1234 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1235 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1236 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001237
Eric Christopher022b7fb2010-10-05 23:13:24 +00001238 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1239 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001240 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1241 ResultReg)
1242 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001243
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001244 // This result needs to be in an integer register, but the conversion only
1245 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001246 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001247 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001248
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001249 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001250 return true;
1251}
1252
Eric Christopher3bbd3962010-10-11 08:27:59 +00001253bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001254 MVT VT;
1255 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001256 return false;
1257
1258 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001259 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001260 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1261
1262 unsigned CondReg = getRegForValue(I->getOperand(0));
1263 if (CondReg == 0) return false;
1264 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1265 if (Op1Reg == 0) return false;
1266 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1267 if (Op2Reg == 0) return false;
1268
1269 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1270 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1271 .addReg(CondReg).addImm(1));
1272 unsigned ResultReg = createResultReg(RC);
1273 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1275 .addReg(Op1Reg).addReg(Op2Reg)
1276 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1277 UpdateValueMap(I, ResultReg);
1278 return true;
1279}
1280
Eric Christopher08637852010-09-30 22:34:19 +00001281bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001282 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001283 const Type *Ty = I->getType();
1284 if (!isTypeLegal(Ty, VT))
1285 return false;
1286
1287 // If we have integer div support we should have selected this automagically.
1288 // In case we have a real miss go ahead and return false and we'll pick
1289 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001290 if (Subtarget->hasDivide()) return false;
1291
Eric Christopher08637852010-09-30 22:34:19 +00001292 // Otherwise emit a libcall.
1293 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001294 if (VT == MVT::i8)
1295 LC = RTLIB::SDIV_I8;
1296 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001297 LC = RTLIB::SDIV_I16;
1298 else if (VT == MVT::i32)
1299 LC = RTLIB::SDIV_I32;
1300 else if (VT == MVT::i64)
1301 LC = RTLIB::SDIV_I64;
1302 else if (VT == MVT::i128)
1303 LC = RTLIB::SDIV_I128;
1304 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001305
Eric Christopher08637852010-09-30 22:34:19 +00001306 return ARMEmitLibcall(I, LC);
1307}
1308
Eric Christopher6a880d62010-10-11 08:37:26 +00001309bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001310 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001311 const Type *Ty = I->getType();
1312 if (!isTypeLegal(Ty, VT))
1313 return false;
1314
1315 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1316 if (VT == MVT::i8)
1317 LC = RTLIB::SREM_I8;
1318 else if (VT == MVT::i16)
1319 LC = RTLIB::SREM_I16;
1320 else if (VT == MVT::i32)
1321 LC = RTLIB::SREM_I32;
1322 else if (VT == MVT::i64)
1323 LC = RTLIB::SREM_I64;
1324 else if (VT == MVT::i128)
1325 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001326 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001327
Eric Christopher6a880d62010-10-11 08:37:26 +00001328 return ARMEmitLibcall(I, LC);
1329}
1330
Eric Christopher43b62be2010-09-27 06:02:23 +00001331bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001332 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001333
Eric Christopherbc39b822010-09-09 00:53:57 +00001334 // We can get here in the case when we want to use NEON for our fp
1335 // operations, but can't figure out how to. Just use the vfp instructions
1336 // if we have them.
1337 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001338 const Type *Ty = I->getType();
1339 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1340 if (isFloat && !Subtarget->hasVFP2())
1341 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001342
Eric Christopherbc39b822010-09-09 00:53:57 +00001343 unsigned Op1 = getRegForValue(I->getOperand(0));
1344 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001345
Eric Christopherbc39b822010-09-09 00:53:57 +00001346 unsigned Op2 = getRegForValue(I->getOperand(1));
1347 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001348
Eric Christopherbc39b822010-09-09 00:53:57 +00001349 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001350 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001351 switch (ISDOpcode) {
1352 default: return false;
1353 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001354 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001355 break;
1356 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001357 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001358 break;
1359 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001360 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001361 break;
1362 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001363 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001364 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1365 TII.get(Opc), ResultReg)
1366 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001367 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001368 return true;
1369}
1370
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001371// Call Handling Code
1372
Eric Christopherfa87d662010-10-18 02:17:53 +00001373bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1374 EVT SrcVT, unsigned &ResultReg) {
1375 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1376 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001377
Eric Christopherfa87d662010-10-18 02:17:53 +00001378 if (RR != 0) {
1379 ResultReg = RR;
1380 return true;
1381 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001382 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001383}
1384
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001385// This is largely taken directly from CCAssignFnForNode - we don't support
1386// varargs in FastISel so that part has been removed.
1387// TODO: We may not support all of this.
1388CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1389 switch (CC) {
1390 default:
1391 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001392 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001393 // Ignore fastcc. Silence compiler warnings.
1394 (void)RetFastCC_ARM_APCS;
1395 (void)FastCC_ARM_APCS;
1396 // Fallthrough
1397 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001398 // Use target triple & subtarget features to do actual dispatch.
1399 if (Subtarget->isAAPCS_ABI()) {
1400 if (Subtarget->hasVFP2() &&
1401 FloatABIType == FloatABI::Hard)
1402 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1403 else
1404 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1405 } else
1406 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1407 case CallingConv::ARM_AAPCS_VFP:
1408 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1409 case CallingConv::ARM_AAPCS:
1410 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1411 case CallingConv::ARM_APCS:
1412 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1413 }
1414}
1415
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001416bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1417 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001418 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001419 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1420 SmallVectorImpl<unsigned> &RegArgs,
1421 CallingConv::ID CC,
1422 unsigned &NumBytes) {
1423 SmallVector<CCValAssign, 16> ArgLocs;
1424 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1425 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1426
1427 // Get a count of how many bytes are to be pushed on the stack.
1428 NumBytes = CCInfo.getNextStackOffset();
1429
1430 // Issue CALLSEQ_START
1431 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001432 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1433 TII.get(AdjStackDown))
1434 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001435
1436 // Process the args.
1437 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1438 CCValAssign &VA = ArgLocs[i];
1439 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001440 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001441
Eric Christophera4633f52010-10-23 09:37:17 +00001442 // We don't handle NEON parameters yet.
1443 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() > 64)
1444 return false;
1445
Eric Christopherf9764fa2010-09-30 20:49:44 +00001446 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001447 switch (VA.getLocInfo()) {
1448 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001449 case CCValAssign::SExt: {
1450 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1451 Arg, ArgVT, Arg);
1452 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
1453 Emitted = true;
1454 ArgVT = VA.getLocVT();
1455 break;
1456 }
1457 case CCValAssign::ZExt: {
1458 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1459 Arg, ArgVT, Arg);
1460 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
1461 Emitted = true;
1462 ArgVT = VA.getLocVT();
1463 break;
1464 }
1465 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001466 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1467 Arg, ArgVT, Arg);
1468 if (!Emitted)
1469 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1470 Arg, ArgVT, Arg);
1471 if (!Emitted)
1472 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1473 Arg, ArgVT, Arg);
1474
1475 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
1476 ArgVT = VA.getLocVT();
1477 break;
1478 }
1479 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001480 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001481 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001482 assert(BC != 0 && "Failed to emit a bitcast!");
1483 Arg = BC;
1484 ArgVT = VA.getLocVT();
1485 break;
1486 }
1487 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001488 }
1489
1490 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001491 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001492 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001493 VA.getLocReg())
1494 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001495 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001496 } else if (VA.needsCustom()) {
1497 // TODO: We need custom lowering for vector (v2f64) args.
1498 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001499
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001500 CCValAssign &NextVA = ArgLocs[++i];
1501
1502 // TODO: Only handle register args for now.
1503 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1504
1505 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1506 TII.get(ARM::VMOVRRD), VA.getLocReg())
1507 .addReg(NextVA.getLocReg(), RegState::Define)
1508 .addReg(Arg));
1509 RegArgs.push_back(VA.getLocReg());
1510 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001511 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001512 assert(VA.isMemLoc());
1513 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001514 Address Addr;
1515 Addr.BaseType = Address::RegBase;
1516 Addr.Base.Reg = ARM::SP;
1517 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001518
Eric Christopher0d581222010-11-19 22:30:02 +00001519 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001520 }
1521 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001522 return true;
1523}
1524
Duncan Sands1440e8b2010-11-03 11:35:31 +00001525bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001526 const Instruction *I, CallingConv::ID CC,
1527 unsigned &NumBytes) {
1528 // Issue CALLSEQ_END
1529 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001530 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1531 TII.get(AdjStackUp))
1532 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001533
1534 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001535 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001536 SmallVector<CCValAssign, 16> RVLocs;
1537 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1538 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1539
1540 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001541 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001542 // For this move we copy into two registers and then move into the
1543 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001544 EVT DestVT = RVLocs[0].getValVT();
1545 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1546 unsigned ResultReg = createResultReg(DstRC);
1547 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1548 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001549 .addReg(RVLocs[0].getLocReg())
1550 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001551
Eric Christopher3659ac22010-10-20 08:02:24 +00001552 UsedRegs.push_back(RVLocs[0].getLocReg());
1553 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001554
Eric Christopherdccd2c32010-10-11 08:38:55 +00001555 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001556 UpdateValueMap(I, ResultReg);
1557 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001558 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001559 EVT CopyVT = RVLocs[0].getValVT();
1560 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001561
Eric Christopher14df8822010-10-01 00:00:11 +00001562 unsigned ResultReg = createResultReg(DstRC);
1563 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1564 ResultReg).addReg(RVLocs[0].getLocReg());
1565 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001566
Eric Christopherdccd2c32010-10-11 08:38:55 +00001567 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001568 UpdateValueMap(I, ResultReg);
1569 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001570 }
1571
Eric Christopherdccd2c32010-10-11 08:38:55 +00001572 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001573}
1574
Eric Christopher4f512ef2010-10-22 01:28:00 +00001575bool ARMFastISel::SelectRet(const Instruction *I) {
1576 const ReturnInst *Ret = cast<ReturnInst>(I);
1577 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001578
Eric Christopher4f512ef2010-10-22 01:28:00 +00001579 if (!FuncInfo.CanLowerReturn)
1580 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001581
Eric Christopher4f512ef2010-10-22 01:28:00 +00001582 if (F.isVarArg())
1583 return false;
1584
1585 CallingConv::ID CC = F.getCallingConv();
1586 if (Ret->getNumOperands() > 0) {
1587 SmallVector<ISD::OutputArg, 4> Outs;
1588 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1589 Outs, TLI);
1590
1591 // Analyze operands of the call, assigning locations to each operand.
1592 SmallVector<CCValAssign, 16> ValLocs;
1593 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1594 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1595
1596 const Value *RV = Ret->getOperand(0);
1597 unsigned Reg = getRegForValue(RV);
1598 if (Reg == 0)
1599 return false;
1600
1601 // Only handle a single return value for now.
1602 if (ValLocs.size() != 1)
1603 return false;
1604
1605 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001606
Eric Christopher4f512ef2010-10-22 01:28:00 +00001607 // Don't bother handling odd stuff for now.
1608 if (VA.getLocInfo() != CCValAssign::Full)
1609 return false;
1610 // Only handle register returns for now.
1611 if (!VA.isRegLoc())
1612 return false;
1613 // TODO: For now, don't try to handle cases where getLocInfo()
1614 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001615 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001616 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001617
Eric Christopher4f512ef2010-10-22 01:28:00 +00001618 // Make the copy.
1619 unsigned SrcReg = Reg + VA.getValNo();
1620 unsigned DstReg = VA.getLocReg();
1621 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1622 // Avoid a cross-class copy. This is very unlikely.
1623 if (!SrcRC->contains(DstReg))
1624 return false;
1625 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1626 DstReg).addReg(SrcReg);
1627
1628 // Mark the register as live out of the function.
1629 MRI.addLiveOut(VA.getLocReg());
1630 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001631
Eric Christopher4f512ef2010-10-22 01:28:00 +00001632 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1633 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1634 TII.get(RetOpc)));
1635 return true;
1636}
1637
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001638// A quick function that will emit a call for a named libcall in F with the
1639// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001640// can emit a call for any libcall we can produce. This is an abridged version
1641// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001642// like computed function pointers or strange arguments at call sites.
1643// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1644// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001645bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1646 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001647
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001648 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001649 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001650 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001651 if (RetTy->isVoidTy())
1652 RetVT = MVT::isVoid;
1653 else if (!isTypeLegal(RetTy, RetVT))
1654 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001655
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001656 // For now we're using BLX etc on the assumption that we have v5t ops.
1657 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001658
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001659 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001660 SmallVector<Value*, 8> Args;
1661 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001662 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001663 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1664 Args.reserve(I->getNumOperands());
1665 ArgRegs.reserve(I->getNumOperands());
1666 ArgVTs.reserve(I->getNumOperands());
1667 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001668 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001669 Value *Op = I->getOperand(i);
1670 unsigned Arg = getRegForValue(Op);
1671 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001672
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001673 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001674 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001675 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001676
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001677 ISD::ArgFlagsTy Flags;
1678 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1679 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001680
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001681 Args.push_back(Op);
1682 ArgRegs.push_back(Arg);
1683 ArgVTs.push_back(ArgVT);
1684 ArgFlags.push_back(Flags);
1685 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001686
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001687 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001688 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001689 unsigned NumBytes;
1690 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1691 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001692
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001693 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001694 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001695 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001696 unsigned CallOpc;
1697 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001698 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001699 else
1700 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopher7bb59962010-11-29 21:56:23 +00001701 // Explicitly adding the predicate here.
1702 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1703 TII.get(CallOpc)))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001704 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001705
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001706 // Add implicit physical register uses to the call.
1707 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1708 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001709
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001710 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001711 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001712 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001713
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001714 // Set all unused physreg defs as dead.
1715 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001716
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001717 return true;
1718}
1719
Eric Christopherf9764fa2010-09-30 20:49:44 +00001720bool ARMFastISel::SelectCall(const Instruction *I) {
1721 const CallInst *CI = cast<CallInst>(I);
1722 const Value *Callee = CI->getCalledValue();
1723
1724 // Can't handle inline asm or worry about intrinsics yet.
1725 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1726
Eric Christophere6ca6772010-10-01 21:33:12 +00001727 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001728 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001729 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1730 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001731
Eric Christopherf9764fa2010-09-30 20:49:44 +00001732 // Check the calling convention.
1733 ImmutableCallSite CS(CI);
1734 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001735
Eric Christopherf9764fa2010-09-30 20:49:44 +00001736 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001737
Eric Christopherf9764fa2010-09-30 20:49:44 +00001738 // Let SDISel handle vararg functions.
1739 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1740 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1741 if (FTy->isVarArg())
1742 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001743
Eric Christopherf9764fa2010-09-30 20:49:44 +00001744 // Handle *simple* calls for now.
1745 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001746 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001747 if (RetTy->isVoidTy())
1748 RetVT = MVT::isVoid;
1749 else if (!isTypeLegal(RetTy, RetVT))
1750 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001751
Eric Christopherf9764fa2010-09-30 20:49:44 +00001752 // For now we're using BLX etc on the assumption that we have v5t ops.
1753 // TODO: Maybe?
1754 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001755
Eric Christopherf9764fa2010-09-30 20:49:44 +00001756 // Set up the argument vectors.
1757 SmallVector<Value*, 8> Args;
1758 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001759 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001760 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1761 Args.reserve(CS.arg_size());
1762 ArgRegs.reserve(CS.arg_size());
1763 ArgVTs.reserve(CS.arg_size());
1764 ArgFlags.reserve(CS.arg_size());
1765 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1766 i != e; ++i) {
1767 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001768
Eric Christopherf9764fa2010-09-30 20:49:44 +00001769 if (Arg == 0)
1770 return false;
1771 ISD::ArgFlagsTy Flags;
1772 unsigned AttrInd = i - CS.arg_begin() + 1;
1773 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1774 Flags.setSExt();
1775 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1776 Flags.setZExt();
1777
1778 // FIXME: Only handle *easy* calls for now.
1779 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1780 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1781 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1782 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1783 return false;
1784
1785 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001786 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001787 if (!isTypeLegal(ArgTy, ArgVT))
1788 return false;
1789 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1790 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001791
Eric Christopherf9764fa2010-09-30 20:49:44 +00001792 Args.push_back(*i);
1793 ArgRegs.push_back(Arg);
1794 ArgVTs.push_back(ArgVT);
1795 ArgFlags.push_back(Flags);
1796 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001797
Eric Christopherf9764fa2010-09-30 20:49:44 +00001798 // Handle the arguments now that we've gotten them.
1799 SmallVector<unsigned, 4> RegArgs;
1800 unsigned NumBytes;
1801 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1802 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001803
Eric Christopherf9764fa2010-09-30 20:49:44 +00001804 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001805 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001806 MachineInstrBuilder MIB;
1807 unsigned CallOpc;
1808 if(isThumb)
1809 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1810 else
1811 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopher7bb59962010-11-29 21:56:23 +00001812 // Explicitly adding the predicate here.
1813 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1814 TII.get(CallOpc)))
1815 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001816
Eric Christopherf9764fa2010-09-30 20:49:44 +00001817 // Add implicit physical register uses to the call.
1818 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1819 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001820
Eric Christopherf9764fa2010-09-30 20:49:44 +00001821 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001822 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001823 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001824
Eric Christopherf9764fa2010-09-30 20:49:44 +00001825 // Set all unused physreg defs as dead.
1826 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001827
Eric Christopherf9764fa2010-09-30 20:49:44 +00001828 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001829
Eric Christopherf9764fa2010-09-30 20:49:44 +00001830}
1831
Eric Christopher56d2b722010-09-02 23:43:26 +00001832// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001833bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001834
Eric Christopherab695882010-07-21 22:26:11 +00001835 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001836 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001837 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001838 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001839 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001840 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001841 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001842 case Instruction::ICmp:
1843 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001844 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001845 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001846 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001847 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001848 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001849 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001850 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001851 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001852 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001853 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001854 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001855 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001856 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001857 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001858 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001859 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001860 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001861 case Instruction::SRem:
1862 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001863 case Instruction::Call:
1864 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001865 case Instruction::Select:
1866 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001867 case Instruction::Ret:
1868 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001869 default: break;
1870 }
1871 return false;
1872}
1873
1874namespace llvm {
1875 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001876 // Completely untested on non-darwin.
1877 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001878
Eric Christopheraaa8df42010-11-02 01:21:28 +00001879 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001880 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001881 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001882 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001883 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001884 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001885 }
1886}