blob: f0413a38ec4778ca7eb4e3bedec13f3687f2f9c2 [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 {
56
57class ARMFastISel : public FastISel {
58
59 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
60 /// make the right decision when generating code for different targets.
61 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000062 const TargetMachine &TM;
63 const TargetInstrInfo &TII;
64 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000065 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000066
Eric Christopher8cf6c602010-09-29 22:24:45 +000067 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000068 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000069 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000070
Eric Christopherab695882010-07-21 22:26:11 +000071 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000072 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000073 : FastISel(funcInfo),
74 TM(funcInfo.MF->getTarget()),
75 TII(*TM.getInstrInfo()),
76 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000077 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000078 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000079 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000080 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000081 }
82
Eric Christophercb592292010-08-20 00:20:31 +000083 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000084 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
85 const TargetRegisterClass *RC);
86 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill);
89 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
90 const TargetRegisterClass *RC,
91 unsigned Op0, bool Op0IsKill,
92 unsigned Op1, bool Op1IsKill);
93 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
94 const TargetRegisterClass *RC,
95 unsigned Op0, bool Op0IsKill,
96 uint64_t Imm);
97 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill,
100 const ConstantFP *FPImm);
101 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
102 const TargetRegisterClass *RC,
103 uint64_t Imm);
104 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
105 const TargetRegisterClass *RC,
106 unsigned Op0, bool Op0IsKill,
107 unsigned Op1, bool Op1IsKill,
108 uint64_t Imm);
109 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
110 unsigned Op0, bool Op0IsKill,
111 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000112
Eric Christophercb592292010-08-20 00:20:31 +0000113 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000114 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000115 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000116 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000117
118 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000119
Eric Christopher83007122010-08-23 21:44:12 +0000120 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000121 private:
Eric Christopher17787722010-10-21 21:47:51 +0000122 bool SelectLoad(const Instruction *I);
123 bool SelectStore(const Instruction *I);
124 bool SelectBranch(const Instruction *I);
125 bool SelectCmp(const Instruction *I);
126 bool SelectFPExt(const Instruction *I);
127 bool SelectFPTrunc(const Instruction *I);
128 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
129 bool SelectSIToFP(const Instruction *I);
130 bool SelectFPToSI(const Instruction *I);
131 bool SelectSDiv(const Instruction *I);
132 bool SelectSRem(const Instruction *I);
133 bool SelectCall(const Instruction *I);
134 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000135 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000136
Eric Christopher83007122010-08-23 21:44:12 +0000137 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000138 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000139 bool isTypeLegal(const Type *Ty, MVT &VT);
140 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher404be0c2010-10-17 11:08:44 +0000141 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Base, int Offset);
142 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Base, int Offset);
143 bool ARMComputeRegOffset(const Value *Obj, unsigned &Base, int &Offset);
144 void ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000145 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000146 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000147 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000148 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000149 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000150
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000151 // Call handling routines.
152 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000153 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
154 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000155 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000156 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000157 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000158 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000159 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
160 SmallVectorImpl<unsigned> &RegArgs,
161 CallingConv::ID CC,
162 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000163 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000164 const Instruction *I, CallingConv::ID CC,
165 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000166 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000167
168 // OptionalDef handling routines.
169 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000170 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
171 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
172};
Eric Christopherab695882010-07-21 22:26:11 +0000173
174} // end anonymous namespace
175
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000176#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000177
Eric Christopher456144e2010-08-19 00:37:05 +0000178// DefinesOptionalPredicate - This is different from DefinesPredicate in that
179// we don't care about implicit defs here, just places we'll need to add a
180// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
181bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
182 const TargetInstrDesc &TID = MI->getDesc();
183 if (!TID.hasOptionalDef())
184 return false;
185
186 // Look to see if our OptionalDef is defining CPSR or CCR.
187 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
188 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000189 if (!MO.isReg() || !MO.isDef()) continue;
190 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000191 *CPSR = true;
192 }
193 return true;
194}
195
196// If the machine is predicable go ahead and add the predicate operands, if
197// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000198// TODO: If we want to support thumb1 then we'll need to deal with optional
199// CPSR defs that need to be added before the remaining operands. See s_cc_out
200// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000201const MachineInstrBuilder &
202ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
203 MachineInstr *MI = &*MIB;
204
205 // Do we use a predicate?
206 if (TII.isPredicable(MI))
207 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000208
Eric Christopher456144e2010-08-19 00:37:05 +0000209 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
210 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000211 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000212 if (DefinesOptionalPredicate(MI, &CPSR)) {
213 if (CPSR)
214 AddDefaultT1CC(MIB);
215 else
216 AddDefaultCC(MIB);
217 }
218 return MIB;
219}
220
Eric Christopher0fe7d542010-08-17 01:25:29 +0000221unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
222 const TargetRegisterClass* RC) {
223 unsigned ResultReg = createResultReg(RC);
224 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
225
Eric Christopher456144e2010-08-19 00:37:05 +0000226 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000227 return ResultReg;
228}
229
230unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
231 const TargetRegisterClass *RC,
232 unsigned Op0, bool Op0IsKill) {
233 unsigned ResultReg = createResultReg(RC);
234 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
235
236 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000237 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000238 .addReg(Op0, Op0IsKill * RegState::Kill));
239 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000240 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000241 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000242 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000243 TII.get(TargetOpcode::COPY), ResultReg)
244 .addReg(II.ImplicitDefs[0]));
245 }
246 return ResultReg;
247}
248
249unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
250 const TargetRegisterClass *RC,
251 unsigned Op0, bool Op0IsKill,
252 unsigned Op1, bool Op1IsKill) {
253 unsigned ResultReg = createResultReg(RC);
254 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
255
256 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000257 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000258 .addReg(Op0, Op0IsKill * RegState::Kill)
259 .addReg(Op1, Op1IsKill * RegState::Kill));
260 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000261 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000262 .addReg(Op0, Op0IsKill * RegState::Kill)
263 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000264 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000265 TII.get(TargetOpcode::COPY), ResultReg)
266 .addReg(II.ImplicitDefs[0]));
267 }
268 return ResultReg;
269}
270
271unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
272 const TargetRegisterClass *RC,
273 unsigned Op0, bool Op0IsKill,
274 uint64_t Imm) {
275 unsigned ResultReg = createResultReg(RC);
276 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
277
278 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000279 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000280 .addReg(Op0, Op0IsKill * RegState::Kill)
281 .addImm(Imm));
282 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000284 .addReg(Op0, Op0IsKill * RegState::Kill)
285 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000287 TII.get(TargetOpcode::COPY), ResultReg)
288 .addReg(II.ImplicitDefs[0]));
289 }
290 return ResultReg;
291}
292
293unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
294 const TargetRegisterClass *RC,
295 unsigned Op0, bool Op0IsKill,
296 const ConstantFP *FPImm) {
297 unsigned ResultReg = createResultReg(RC);
298 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
299
300 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000302 .addReg(Op0, Op0IsKill * RegState::Kill)
303 .addFPImm(FPImm));
304 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000306 .addReg(Op0, Op0IsKill * RegState::Kill)
307 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 TII.get(TargetOpcode::COPY), ResultReg)
310 .addReg(II.ImplicitDefs[0]));
311 }
312 return ResultReg;
313}
314
315unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
316 const TargetRegisterClass *RC,
317 unsigned Op0, bool Op0IsKill,
318 unsigned Op1, bool Op1IsKill,
319 uint64_t Imm) {
320 unsigned ResultReg = createResultReg(RC);
321 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
322
323 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000325 .addReg(Op0, Op0IsKill * RegState::Kill)
326 .addReg(Op1, Op1IsKill * RegState::Kill)
327 .addImm(Imm));
328 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000330 .addReg(Op0, Op0IsKill * RegState::Kill)
331 .addReg(Op1, Op1IsKill * RegState::Kill)
332 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000334 TII.get(TargetOpcode::COPY), ResultReg)
335 .addReg(II.ImplicitDefs[0]));
336 }
337 return ResultReg;
338}
339
340unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
341 const TargetRegisterClass *RC,
342 uint64_t Imm) {
343 unsigned ResultReg = createResultReg(RC);
344 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000345
Eric Christopher0fe7d542010-08-17 01:25:29 +0000346 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000348 .addImm(Imm));
349 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000350 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000351 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000353 TII.get(TargetOpcode::COPY), ResultReg)
354 .addReg(II.ImplicitDefs[0]));
355 }
356 return ResultReg;
357}
358
359unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
360 unsigned Op0, bool Op0IsKill,
361 uint32_t Idx) {
362 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
363 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
364 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000365 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000366 DL, TII.get(TargetOpcode::COPY), ResultReg)
367 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
368 return ResultReg;
369}
370
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000371// TODO: Don't worry about 64-bit now, but when this is fixed remove the
372// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000373unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000374 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000375
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000376 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
378 TII.get(ARM::VMOVRS), MoveReg)
379 .addReg(SrcReg));
380 return MoveReg;
381}
382
383unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000384 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000385
Eric Christopheraa3ace12010-09-09 20:49:25 +0000386 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
387 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000388 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000389 .addReg(SrcReg));
390 return MoveReg;
391}
392
Eric Christopher9ed58df2010-09-09 00:19:41 +0000393// For double width floating point we need to materialize two constants
394// (the high and the low) into integer registers then use a move to get
395// the combined constant into an FP reg.
396unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
397 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000398 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000399
Eric Christopher9ed58df2010-09-09 00:19:41 +0000400 // This checks to see if we can use VFP3 instructions to materialize
401 // a constant, otherwise we have to go through the constant pool.
402 if (TLI.isFPImmLegal(Val, VT)) {
403 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
404 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
405 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
406 DestReg)
407 .addFPImm(CFP));
408 return DestReg;
409 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000410
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000411 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000412 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000413
Eric Christopher238bb162010-09-09 23:50:00 +0000414 // MachineConstantPool wants an explicit alignment.
415 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
416 if (Align == 0) {
417 // TODO: Figure out if this is correct.
418 Align = TD.getTypeAllocSize(CFP->getType());
419 }
420 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
421 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
422 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000423
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000424 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000425 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
426 DestReg)
427 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000428 .addReg(0));
429 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000430}
431
Eric Christopher744c7c82010-09-28 22:47:54 +0000432unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000433
Eric Christopher744c7c82010-09-28 22:47:54 +0000434 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000435 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000436
Eric Christophere5b13cf2010-11-03 20:21:17 +0000437 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
438
439 // If we can do this in a single instruction without a constant pool entry
440 // do so now.
441 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000442 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000443 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
445 TII.get(Opc), DestReg)
446 .addImm(CI->getSExtValue()));
447 return DestReg;
448 }
449
Eric Christopher56d2b722010-09-02 23:43:26 +0000450 // MachineConstantPool wants an explicit alignment.
451 unsigned Align = TD.getPrefTypeAlignment(C->getType());
452 if (Align == 0) {
453 // TODO: Figure out if this is correct.
454 Align = TD.getTypeAllocSize(C->getType());
455 }
456 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000457
Eric Christopher56d2b722010-09-02 23:43:26 +0000458 if (isThumb)
459 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000460 TII.get(ARM::t2LDRpci), DestReg)
461 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000462 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000463 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000464 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000465 TII.get(ARM::LDRcp), DestReg)
466 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000467 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000468
Eric Christopher56d2b722010-09-02 23:43:26 +0000469 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000470}
471
Eric Christopherc9932f62010-10-01 23:24:42 +0000472unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000473 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000474 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000475
Eric Christopher890dbbe2010-10-02 00:32:44 +0000476 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000477
Eric Christopher890dbbe2010-10-02 00:32:44 +0000478 // TODO: No external globals for now.
479 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000480
Eric Christopher890dbbe2010-10-02 00:32:44 +0000481 // TODO: Need more magic for ARM PIC.
482 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000483
Eric Christopher890dbbe2010-10-02 00:32:44 +0000484 // MachineConstantPool wants an explicit alignment.
485 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
486 if (Align == 0) {
487 // TODO: Figure out if this is correct.
488 Align = TD.getTypeAllocSize(GV->getType());
489 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000490
Eric Christopher890dbbe2010-10-02 00:32:44 +0000491 // Grab index.
492 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
493 unsigned Id = AFI->createConstPoolEntryUId();
494 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
495 ARMCP::CPValue, PCAdj);
496 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000497
Eric Christopher890dbbe2010-10-02 00:32:44 +0000498 // Load value.
499 MachineInstrBuilder MIB;
500 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
501 if (isThumb) {
502 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
503 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
504 .addConstantPoolIndex(Idx);
505 if (RelocM == Reloc::PIC_)
506 MIB.addImm(Id);
507 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000508 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000509 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
510 DestReg)
511 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000512 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000513 }
514 AddOptionalDefs(MIB);
515 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000516}
517
Eric Christopher9ed58df2010-09-09 00:19:41 +0000518unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
519 EVT VT = TLI.getValueType(C->getType(), true);
520
521 // Only handle simple types.
522 if (!VT.isSimple()) return 0;
523
524 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
525 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000526 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
527 return ARMMaterializeGV(GV, VT);
528 else if (isa<ConstantInt>(C))
529 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000530
Eric Christopherc9932f62010-10-01 23:24:42 +0000531 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000532}
533
Eric Christopherf9764fa2010-09-30 20:49:44 +0000534unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
535 // Don't handle dynamic allocas.
536 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000537
Duncan Sands1440e8b2010-11-03 11:35:31 +0000538 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000539 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000540
Eric Christopherf9764fa2010-09-30 20:49:44 +0000541 DenseMap<const AllocaInst*, int>::iterator SI =
542 FuncInfo.StaticAllocaMap.find(AI);
543
544 // This will get lowered later into the correct offsets and registers
545 // via rewriteXFrameIndex.
546 if (SI != FuncInfo.StaticAllocaMap.end()) {
547 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
548 unsigned ResultReg = createResultReg(RC);
549 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
550 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
551 TII.get(Opc), ResultReg)
552 .addFrameIndex(SI->second)
553 .addImm(0));
554 return ResultReg;
555 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000556
Eric Christopherf9764fa2010-09-30 20:49:44 +0000557 return 0;
558}
559
Duncan Sands1440e8b2010-11-03 11:35:31 +0000560bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
561 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000562
Eric Christopherb1cc8482010-08-25 07:23:49 +0000563 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000564 if (evt == MVT::Other || !evt.isSimple()) return false;
565 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000566
Eric Christopherdc908042010-08-31 01:28:42 +0000567 // Handle all legal types, i.e. a register that will directly hold this
568 // value.
569 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000570}
571
Duncan Sands1440e8b2010-11-03 11:35:31 +0000572bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000573 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000574
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000575 // If this is a type than can be sign or zero-extended to a basic operation
576 // go ahead and accept it now.
577 if (VT == MVT::i8 || VT == MVT::i16)
578 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000579
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000580 return false;
581}
582
Eric Christophercb0b04b2010-08-24 00:07:24 +0000583// Computes the Reg+Offset to get to an object.
Eric Christopher404be0c2010-10-17 11:08:44 +0000584bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Base,
Eric Christopher83007122010-08-23 21:44:12 +0000585 int &Offset) {
586 // Some boilerplate from the X86 FastISel.
587 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000588 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000589 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000590 // Don't walk into other basic blocks; it's possible we haven't
591 // visited them yet, so the instructions may not yet be assigned
592 // virtual registers.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000593 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
594 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
595 Opcode = I->getOpcode();
596 U = I;
597 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000598 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000599 Opcode = C->getOpcode();
600 U = C;
601 }
602
Eric Christophercb0b04b2010-08-24 00:07:24 +0000603 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000604 if (Ty->getAddressSpace() > 255)
605 // Fast instruction selection doesn't support the special
606 // address spaces.
607 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000608
Eric Christopher83007122010-08-23 21:44:12 +0000609 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000610 default:
Eric Christopher83007122010-08-23 21:44:12 +0000611 break;
Eric Christopher55324332010-10-12 00:43:21 +0000612 case Instruction::BitCast: {
613 // Look through bitcasts.
Eric Christophera3224252010-10-15 21:32:12 +0000614 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000615 }
616 case Instruction::IntToPtr: {
617 // Look past no-op inttoptrs.
618 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000619 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000620 break;
621 }
622 case Instruction::PtrToInt: {
623 // Look past no-op ptrtoints.
624 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000625 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000626 break;
627 }
Eric Christophereae84392010-10-14 09:29:41 +0000628 case Instruction::GetElementPtr: {
629 int SavedOffset = Offset;
Eric Christopher404be0c2010-10-17 11:08:44 +0000630 unsigned SavedBase = Base;
Eric Christophereae84392010-10-14 09:29:41 +0000631 int TmpOffset = Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000632
Eric Christophereae84392010-10-14 09:29:41 +0000633 // Iterate through the GEP folding the constants into offsets where
634 // we can.
635 gep_type_iterator GTI = gep_type_begin(U);
636 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
637 i != e; ++i, ++GTI) {
638 const Value *Op = *i;
639 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
640 const StructLayout *SL = TD.getStructLayout(STy);
641 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
642 TmpOffset += SL->getElementOffset(Idx);
643 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000644 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
645 SmallVector<const Value *, 4> Worklist;
646 Worklist.push_back(Op);
647 do {
648 Op = Worklist.pop_back_val();
649 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
650 // Constant-offset addressing.
651 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000652 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000653 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
654 // An add with a constant operand. Fold the constant.
655 ConstantInt *CI =
656 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
657 TmpOffset += CI->getSExtValue() * S;
658 // Add the other operand back to the work list.
659 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
660 } else
661 goto unsupported_gep;
662 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000663 }
664 }
Eric Christopher2896df82010-10-15 18:02:07 +0000665
666 // Try to grab the base operand now.
Eric Christophereae84392010-10-14 09:29:41 +0000667 Offset = TmpOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000668 if (ARMComputeRegOffset(U->getOperand(0), Base, Offset)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000669
670 // We failed, restore everything and try the other options.
Eric Christophereae84392010-10-14 09:29:41 +0000671 Offset = SavedOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000672 Base = SavedBase;
Eric Christopher2896df82010-10-15 18:02:07 +0000673
Eric Christophereae84392010-10-14 09:29:41 +0000674 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000675 break;
676 }
Eric Christopher83007122010-08-23 21:44:12 +0000677 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000678 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopherd56d61a2010-10-17 01:51:42 +0000679 unsigned Reg = TargetMaterializeAlloca(AI);
680
681 if (Reg == 0) return false;
682
Eric Christopher404be0c2010-10-17 11:08:44 +0000683 Base = Reg;
Eric Christopherd56d61a2010-10-17 01:51:42 +0000684 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000685 }
686 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000687
Eric Christophera9c57512010-10-13 21:41:51 +0000688 // Materialize the global variable's address into a reg which can
689 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000690 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000691 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
692 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000693
Eric Christopher404be0c2010-10-17 11:08:44 +0000694 Base = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000695 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000696 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000697
Eric Christophercb0b04b2010-08-24 00:07:24 +0000698 // Try to get this in a register if nothing else has worked.
Eric Christopher61d69da2010-11-02 01:22:45 +0000699 if (Base == 0) Base = getRegForValue(Obj);
Eric Christopher404be0c2010-10-17 11:08:44 +0000700 return Base != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000701}
702
Eric Christopher404be0c2010-10-17 11:08:44 +0000703void ARMFastISel::ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000704
Eric Christopher212ae932010-10-21 19:40:30 +0000705 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000706
Eric Christopher212ae932010-10-21 19:40:30 +0000707 bool needsLowering = false;
708 switch (VT.getSimpleVT().SimpleTy) {
709 default:
710 assert(false && "Unhandled load/store type!");
711 case MVT::i1:
712 case MVT::i8:
713 case MVT::i16:
714 case MVT::i32:
715 // Integer loads/stores handle 12-bit offsets.
716 needsLowering = ((Offset & 0xfff) != Offset);
717 break;
718 case MVT::f32:
719 case MVT::f64:
720 // Floating point operands handle 8-bit offsets.
721 needsLowering = ((Offset & 0xff) != Offset);
722 break;
723 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000724
Eric Christopher212ae932010-10-21 19:40:30 +0000725 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000726 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000727 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000728 ARMCC::CondCodes Pred = ARMCC::AL;
729 unsigned PredReg = 0;
730
Eric Christopher2896df82010-10-15 18:02:07 +0000731 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
732 ARM::GPRRegisterClass;
733 unsigned BaseReg = createResultReg(RC);
734
Eric Christophereaa204b2010-09-02 01:39:14 +0000735 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000736 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher404be0c2010-10-17 11:08:44 +0000737 BaseReg, Base, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000738 static_cast<const ARMBaseInstrInfo&>(TII));
739 else {
740 assert(AFI->isThumb2Function());
741 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher404be0c2010-10-17 11:08:44 +0000742 BaseReg, Base, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000743 static_cast<const ARMBaseInstrInfo&>(TII));
744 }
Eric Christophereae84392010-10-14 09:29:41 +0000745 Offset = 0;
Eric Christopher404be0c2010-10-17 11:08:44 +0000746 Base = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000747 }
Eric Christopher83007122010-08-23 21:44:12 +0000748}
749
Eric Christopherb1cc8482010-08-25 07:23:49 +0000750bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
Eric Christopher404be0c2010-10-17 11:08:44 +0000751 unsigned Base, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000752
Eric Christopherb1cc8482010-08-25 07:23:49 +0000753 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000754 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000755 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000756 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000757 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000758 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000759 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000760 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000761 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000762 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000763 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000764 break;
765 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000766 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000767 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000768 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000769 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000770 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000771 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000772 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000773 case MVT::f32:
774 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000775 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000776 isFloat = true;
777 break;
778 case MVT::f64:
779 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000780 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000781 isFloat = true;
782 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000783 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000784
Eric Christopheree56ea62010-10-07 05:50:44 +0000785 ResultReg = createResultReg(RC);
Jim Grosbach6b156392010-10-27 21:39:08 +0000786
Eric Christopher212ae932010-10-21 19:40:30 +0000787 ARMSimplifyRegOffset(Base, Offset, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000788
Eric Christopher212ae932010-10-21 19:40:30 +0000789 // addrmode5 output depends on the selection dag addressing dividing the
790 // offset by 4 that it then later multiplies. Do this here as well.
791 if (isFloat)
792 Offset /= 4;
Jim Grosbach6b156392010-10-27 21:39:08 +0000793
Eric Christopherd0c82a62010-11-12 09:48:30 +0000794 // LDRH needs an additional operand.
795 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16)
796 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
797 TII.get(Opc), ResultReg)
798 .addReg(Base).addReg(0).addImm(Offset));
799 else
800 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
801 TII.get(Opc), ResultReg)
802 .addReg(Base).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000803 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000804}
805
Eric Christopher43b62be2010-09-27 06:02:23 +0000806bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000807 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000808 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000809 if (!isLoadTypeLegal(I->getType(), VT))
810 return false;
811
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000812 // Our register and offset with innocuous defaults.
Eric Christopher404be0c2010-10-17 11:08:44 +0000813 unsigned Base = 0;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000814 int Offset = 0;
815
816 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000817 if (!ARMComputeRegOffset(I->getOperand(0), Base, Offset))
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000818 return false;
819
820 unsigned ResultReg;
Eric Christophera3224252010-10-15 21:32:12 +0000821 if (!ARMEmitLoad(VT, ResultReg, Base, Offset)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000822
823 UpdateValueMap(I, ResultReg);
824 return true;
825}
826
Eric Christopher318b6ee2010-09-02 00:53:56 +0000827bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
Eric Christopher404be0c2010-10-17 11:08:44 +0000828 unsigned Base, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000829 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000830 bool isFloat = false;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000831 bool needReg0Op = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000832 switch (VT.getSimpleVT().SimpleTy) {
833 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000834 case MVT::i1: {
835 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
836 ARM::GPRRegisterClass);
837 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
838 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
839 TII.get(Opc), Res)
840 .addReg(SrcReg).addImm(1));
841 SrcReg = Res;
842 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000843 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000844 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000845 break;
846 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000847 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000848 needReg0Op = true;
Eric Christopher15418772010-10-12 05:39:06 +0000849 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000850 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000851 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000852 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000853 case MVT::f32:
854 if (!Subtarget->hasVFP2()) return false;
855 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000856 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000857 break;
858 case MVT::f64:
859 if (!Subtarget->hasVFP2()) return false;
860 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000861 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000862 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000863 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000864
Eric Christopher212ae932010-10-21 19:40:30 +0000865 ARMSimplifyRegOffset(Base, Offset, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000866
Eric Christopher212ae932010-10-21 19:40:30 +0000867 // addrmode5 output depends on the selection dag addressing dividing the
868 // offset by 4 that it then later multiplies. Do this here as well.
869 if (isFloat)
870 Offset /= 4;
Jim Grosbach6b156392010-10-27 21:39:08 +0000871
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000872 // FIXME: The 'needReg0Op' bit goes away once STRH is converted to
873 // not use the mega-addrmode stuff.
874 if (!needReg0Op)
Eric Christopherb74558a2010-09-18 01:23:38 +0000875 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000876 TII.get(StrOpc))
Eric Christopher404be0c2010-10-17 11:08:44 +0000877 .addReg(SrcReg).addReg(Base).addImm(Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000878 else
879 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000880 TII.get(StrOpc))
Eric Christopher404be0c2010-10-17 11:08:44 +0000881 .addReg(SrcReg).addReg(Base).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000882
Eric Christopher318b6ee2010-09-02 00:53:56 +0000883 return true;
884}
885
Eric Christopher43b62be2010-09-27 06:02:23 +0000886bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000887 Value *Op0 = I->getOperand(0);
888 unsigned SrcReg = 0;
889
Eric Christopher543cf052010-09-01 22:16:27 +0000890 // Yay type legalization
Duncan Sands1440e8b2010-11-03 11:35:31 +0000891 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000892 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000893 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000894
Eric Christopher1b61ef42010-09-02 01:48:11 +0000895 // Get the value to be stored into a register.
896 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000897 if (SrcReg == 0)
898 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000899
Eric Christopher318b6ee2010-09-02 00:53:56 +0000900 // Our register and offset with innocuous defaults.
Eric Christopher404be0c2010-10-17 11:08:44 +0000901 unsigned Base = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000902 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000903
Eric Christopher318b6ee2010-09-02 00:53:56 +0000904 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000905 if (!ARMComputeRegOffset(I->getOperand(1), Base, Offset))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000906 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000907
Eric Christophera3224252010-10-15 21:32:12 +0000908 if (!ARMEmitStore(VT, SrcReg, Base, Offset)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000909
Eric Christophera5b1e682010-09-17 22:28:18 +0000910 return true;
911}
912
913static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
914 switch (Pred) {
915 // Needs two compares...
916 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000917 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000918 default:
Eric Christopher4053e632010-11-02 01:24:49 +0000919 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +0000920 return ARMCC::AL;
921 case CmpInst::ICMP_EQ:
922 case CmpInst::FCMP_OEQ:
923 return ARMCC::EQ;
924 case CmpInst::ICMP_SGT:
925 case CmpInst::FCMP_OGT:
926 return ARMCC::GT;
927 case CmpInst::ICMP_SGE:
928 case CmpInst::FCMP_OGE:
929 return ARMCC::GE;
930 case CmpInst::ICMP_UGT:
931 case CmpInst::FCMP_UGT:
932 return ARMCC::HI;
933 case CmpInst::FCMP_OLT:
934 return ARMCC::MI;
935 case CmpInst::ICMP_ULE:
936 case CmpInst::FCMP_OLE:
937 return ARMCC::LS;
938 case CmpInst::FCMP_ORD:
939 return ARMCC::VC;
940 case CmpInst::FCMP_UNO:
941 return ARMCC::VS;
942 case CmpInst::FCMP_UGE:
943 return ARMCC::PL;
944 case CmpInst::ICMP_SLT:
945 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000946 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000947 case CmpInst::ICMP_SLE:
948 case CmpInst::FCMP_ULE:
949 return ARMCC::LE;
950 case CmpInst::FCMP_UNE:
951 case CmpInst::ICMP_NE:
952 return ARMCC::NE;
953 case CmpInst::ICMP_UGE:
954 return ARMCC::HS;
955 case CmpInst::ICMP_ULT:
956 return ARMCC::LO;
957 }
Eric Christopher543cf052010-09-01 22:16:27 +0000958}
959
Eric Christopher43b62be2010-09-27 06:02:23 +0000960bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000961 const BranchInst *BI = cast<BranchInst>(I);
962 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
963 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000964
Eric Christophere5734102010-09-03 00:35:47 +0000965 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +0000966
Eric Christopher0e6233b2010-10-29 21:08:19 +0000967 // If we can, avoid recomputing the compare - redoing it could lead to wonky
968 // behavior.
969 // TODO: Factor this out.
970 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
971 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000972 MVT VT;
Eric Christopher0e6233b2010-10-29 21:08:19 +0000973 const Type *Ty = CI->getOperand(0)->getType();
Eric Christopher76d61472010-10-30 21:25:26 +0000974 if (!isTypeLegal(Ty, VT))
975 return false;
976
Eric Christopher0e6233b2010-10-29 21:08:19 +0000977 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
978 if (isFloat && !Subtarget->hasVFP2())
979 return false;
980
981 unsigned CmpOpc;
982 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000983 switch (VT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +0000984 default: return false;
985 // TODO: Verify compares.
986 case MVT::f32:
987 CmpOpc = ARM::VCMPES;
988 CondReg = ARM::FPSCR;
989 break;
990 case MVT::f64:
991 CmpOpc = ARM::VCMPED;
992 CondReg = ARM::FPSCR;
993 break;
994 case MVT::i32:
995 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
996 CondReg = ARM::CPSR;
997 break;
998 }
999
1000 // Get the compare predicate.
1001 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1002
1003 // We may not handle every CC for now.
1004 if (ARMPred == ARMCC::AL) return false;
1005
1006 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1007 if (Arg1 == 0) return false;
1008
1009 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1010 if (Arg2 == 0) return false;
1011
1012 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1013 TII.get(CmpOpc))
1014 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001015
Eric Christopher0e6233b2010-10-29 21:08:19 +00001016 // For floating point we need to move the result to a comparison register
1017 // that we can then use for branches.
1018 if (isFloat)
1019 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1020 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001021
Eric Christopher0e6233b2010-10-29 21:08:19 +00001022 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1023 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1024 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1025 FastEmitBranch(FBB, DL);
1026 FuncInfo.MBB->addSuccessor(TBB);
1027 return true;
1028 }
1029 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001030
Eric Christopher0e6233b2010-10-29 21:08:19 +00001031 unsigned CmpReg = getRegForValue(BI->getCondition());
1032 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001033
Eric Christopher229207a2010-09-29 01:14:47 +00001034 // Re-set the flags just in case.
1035 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1036 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001037 .addReg(CmpReg).addImm(0));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001038
Eric Christophere5734102010-09-03 00:35:47 +00001039 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001040 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001041 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001042 FastEmitBranch(FBB, DL);
1043 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001044 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001045}
1046
Eric Christopher43b62be2010-09-27 06:02:23 +00001047bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001048 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001049
Duncan Sands1440e8b2010-11-03 11:35:31 +00001050 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001051 const Type *Ty = CI->getOperand(0)->getType();
1052 if (!isTypeLegal(Ty, VT))
1053 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001054
Eric Christopherd43393a2010-09-08 23:13:45 +00001055 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1056 if (isFloat && !Subtarget->hasVFP2())
1057 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001058
Eric Christopherd43393a2010-09-08 23:13:45 +00001059 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001060 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001061 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001062 default: return false;
1063 // TODO: Verify compares.
1064 case MVT::f32:
1065 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001066 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001067 break;
1068 case MVT::f64:
1069 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001070 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001071 break;
1072 case MVT::i32:
1073 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001074 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001075 break;
1076 }
1077
Eric Christopher229207a2010-09-29 01:14:47 +00001078 // Get the compare predicate.
1079 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001080
Eric Christopher229207a2010-09-29 01:14:47 +00001081 // We may not handle every CC for now.
1082 if (ARMPred == ARMCC::AL) return false;
1083
Eric Christopherd43393a2010-09-08 23:13:45 +00001084 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1085 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001086
Eric Christopherd43393a2010-09-08 23:13:45 +00001087 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1088 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001089
Eric Christopherd43393a2010-09-08 23:13:45 +00001090 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1091 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001092
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001093 // For floating point we need to move the result to a comparison register
1094 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001095 if (isFloat)
1096 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1097 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001098
Eric Christopher229207a2010-09-29 01:14:47 +00001099 // Now set a register based on the comparison. Explicitly set the predicates
1100 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001101 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001102 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001103 : ARM::GPRRegisterClass;
1104 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001105 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001106 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001107 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1108 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1109 .addReg(ZeroReg).addImm(1)
1110 .addImm(ARMPred).addReg(CondReg);
1111
Eric Christophera5b1e682010-09-17 22:28:18 +00001112 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001113 return true;
1114}
1115
Eric Christopher43b62be2010-09-27 06:02:23 +00001116bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001117 // Make sure we have VFP and that we're extending float to double.
1118 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001119
Eric Christopher46203602010-09-09 00:26:48 +00001120 Value *V = I->getOperand(0);
1121 if (!I->getType()->isDoubleTy() ||
1122 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001123
Eric Christopher46203602010-09-09 00:26:48 +00001124 unsigned Op = getRegForValue(V);
1125 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001126
Eric Christopher46203602010-09-09 00:26:48 +00001127 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001128 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001129 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001130 .addReg(Op));
1131 UpdateValueMap(I, Result);
1132 return true;
1133}
1134
Eric Christopher43b62be2010-09-27 06:02:23 +00001135bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001136 // Make sure we have VFP and that we're truncating double to float.
1137 if (!Subtarget->hasVFP2()) return false;
1138
1139 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001140 if (!(I->getType()->isFloatTy() &&
1141 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001142
1143 unsigned Op = getRegForValue(V);
1144 if (Op == 0) return false;
1145
1146 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001147 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001148 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001149 .addReg(Op));
1150 UpdateValueMap(I, Result);
1151 return true;
1152}
1153
Eric Christopher43b62be2010-09-27 06:02:23 +00001154bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001155 // Make sure we have VFP.
1156 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001157
Duncan Sands1440e8b2010-11-03 11:35:31 +00001158 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001159 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001160 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001161 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001162
Eric Christopher9a040492010-09-09 18:54:59 +00001163 unsigned Op = getRegForValue(I->getOperand(0));
1164 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001165
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001166 // The conversion routine works on fp-reg to fp-reg and the operand above
1167 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001168 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001169 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001170
Eric Christopher9a040492010-09-09 18:54:59 +00001171 unsigned Opc;
1172 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1173 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1174 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001175
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001176 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001177 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1178 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001179 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001180 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001181 return true;
1182}
1183
Eric Christopher43b62be2010-09-27 06:02:23 +00001184bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001185 // Make sure we have VFP.
1186 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001187
Duncan Sands1440e8b2010-11-03 11:35:31 +00001188 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001189 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001190 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001191 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001192
Eric Christopher9a040492010-09-09 18:54:59 +00001193 unsigned Op = getRegForValue(I->getOperand(0));
1194 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001195
Eric Christopher9a040492010-09-09 18:54:59 +00001196 unsigned Opc;
1197 const Type *OpTy = I->getOperand(0)->getType();
1198 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1199 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1200 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001201
Eric Christopher022b7fb2010-10-05 23:13:24 +00001202 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1203 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001204 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1205 ResultReg)
1206 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001207
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001208 // This result needs to be in an integer register, but the conversion only
1209 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001210 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001211 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001212
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001213 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001214 return true;
1215}
1216
Eric Christopher3bbd3962010-10-11 08:27:59 +00001217bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001218 MVT VT;
1219 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001220 return false;
1221
1222 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001223 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001224 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1225
1226 unsigned CondReg = getRegForValue(I->getOperand(0));
1227 if (CondReg == 0) return false;
1228 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1229 if (Op1Reg == 0) return false;
1230 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1231 if (Op2Reg == 0) return false;
1232
1233 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1234 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1235 .addReg(CondReg).addImm(1));
1236 unsigned ResultReg = createResultReg(RC);
1237 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1239 .addReg(Op1Reg).addReg(Op2Reg)
1240 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1241 UpdateValueMap(I, ResultReg);
1242 return true;
1243}
1244
Eric Christopher08637852010-09-30 22:34:19 +00001245bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001246 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001247 const Type *Ty = I->getType();
1248 if (!isTypeLegal(Ty, VT))
1249 return false;
1250
1251 // If we have integer div support we should have selected this automagically.
1252 // In case we have a real miss go ahead and return false and we'll pick
1253 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001254 if (Subtarget->hasDivide()) return false;
1255
Eric Christopher08637852010-09-30 22:34:19 +00001256 // Otherwise emit a libcall.
1257 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001258 if (VT == MVT::i8)
1259 LC = RTLIB::SDIV_I8;
1260 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001261 LC = RTLIB::SDIV_I16;
1262 else if (VT == MVT::i32)
1263 LC = RTLIB::SDIV_I32;
1264 else if (VT == MVT::i64)
1265 LC = RTLIB::SDIV_I64;
1266 else if (VT == MVT::i128)
1267 LC = RTLIB::SDIV_I128;
1268 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001269
Eric Christopher08637852010-09-30 22:34:19 +00001270 return ARMEmitLibcall(I, LC);
1271}
1272
Eric Christopher6a880d62010-10-11 08:37:26 +00001273bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001274 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001275 const Type *Ty = I->getType();
1276 if (!isTypeLegal(Ty, VT))
1277 return false;
1278
1279 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1280 if (VT == MVT::i8)
1281 LC = RTLIB::SREM_I8;
1282 else if (VT == MVT::i16)
1283 LC = RTLIB::SREM_I16;
1284 else if (VT == MVT::i32)
1285 LC = RTLIB::SREM_I32;
1286 else if (VT == MVT::i64)
1287 LC = RTLIB::SREM_I64;
1288 else if (VT == MVT::i128)
1289 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001290 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001291
Eric Christopher6a880d62010-10-11 08:37:26 +00001292 return ARMEmitLibcall(I, LC);
1293}
1294
Eric Christopher43b62be2010-09-27 06:02:23 +00001295bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001296 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001297
Eric Christopherbc39b822010-09-09 00:53:57 +00001298 // We can get here in the case when we want to use NEON for our fp
1299 // operations, but can't figure out how to. Just use the vfp instructions
1300 // if we have them.
1301 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001302 const Type *Ty = I->getType();
1303 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1304 if (isFloat && !Subtarget->hasVFP2())
1305 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001306
Eric Christopherbc39b822010-09-09 00:53:57 +00001307 unsigned Op1 = getRegForValue(I->getOperand(0));
1308 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001309
Eric Christopherbc39b822010-09-09 00:53:57 +00001310 unsigned Op2 = getRegForValue(I->getOperand(1));
1311 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001312
Eric Christopherbc39b822010-09-09 00:53:57 +00001313 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001314 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001315 switch (ISDOpcode) {
1316 default: return false;
1317 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001318 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001319 break;
1320 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001321 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001322 break;
1323 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001324 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001325 break;
1326 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001327 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1329 TII.get(Opc), ResultReg)
1330 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001331 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001332 return true;
1333}
1334
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001335// Call Handling Code
1336
Eric Christopherfa87d662010-10-18 02:17:53 +00001337bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1338 EVT SrcVT, unsigned &ResultReg) {
1339 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1340 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001341
Eric Christopherfa87d662010-10-18 02:17:53 +00001342 if (RR != 0) {
1343 ResultReg = RR;
1344 return true;
1345 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001346 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001347}
1348
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001349// This is largely taken directly from CCAssignFnForNode - we don't support
1350// varargs in FastISel so that part has been removed.
1351// TODO: We may not support all of this.
1352CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1353 switch (CC) {
1354 default:
1355 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001356 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001357 // Ignore fastcc. Silence compiler warnings.
1358 (void)RetFastCC_ARM_APCS;
1359 (void)FastCC_ARM_APCS;
1360 // Fallthrough
1361 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001362 // Use target triple & subtarget features to do actual dispatch.
1363 if (Subtarget->isAAPCS_ABI()) {
1364 if (Subtarget->hasVFP2() &&
1365 FloatABIType == FloatABI::Hard)
1366 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1367 else
1368 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1369 } else
1370 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1371 case CallingConv::ARM_AAPCS_VFP:
1372 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1373 case CallingConv::ARM_AAPCS:
1374 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1375 case CallingConv::ARM_APCS:
1376 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1377 }
1378}
1379
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001380bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1381 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001382 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001383 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1384 SmallVectorImpl<unsigned> &RegArgs,
1385 CallingConv::ID CC,
1386 unsigned &NumBytes) {
1387 SmallVector<CCValAssign, 16> ArgLocs;
1388 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1389 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1390
1391 // Get a count of how many bytes are to be pushed on the stack.
1392 NumBytes = CCInfo.getNextStackOffset();
1393
1394 // Issue CALLSEQ_START
1395 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001396 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1397 TII.get(AdjStackDown))
1398 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001399
1400 // Process the args.
1401 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1402 CCValAssign &VA = ArgLocs[i];
1403 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001404 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001405
Eric Christophera4633f52010-10-23 09:37:17 +00001406 // We don't handle NEON parameters yet.
1407 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() > 64)
1408 return false;
1409
Eric Christopherf9764fa2010-09-30 20:49:44 +00001410 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001411 switch (VA.getLocInfo()) {
1412 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001413 case CCValAssign::SExt: {
1414 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1415 Arg, ArgVT, Arg);
1416 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
1417 Emitted = true;
1418 ArgVT = VA.getLocVT();
1419 break;
1420 }
1421 case CCValAssign::ZExt: {
1422 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1423 Arg, ArgVT, Arg);
1424 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
1425 Emitted = true;
1426 ArgVT = VA.getLocVT();
1427 break;
1428 }
1429 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001430 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1431 Arg, ArgVT, Arg);
1432 if (!Emitted)
1433 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1434 Arg, ArgVT, Arg);
1435 if (!Emitted)
1436 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1437 Arg, ArgVT, Arg);
1438
1439 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
1440 ArgVT = VA.getLocVT();
1441 break;
1442 }
1443 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001444 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BIT_CONVERT, Arg,
1445 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001446 assert(BC != 0 && "Failed to emit a bitcast!");
1447 Arg = BC;
1448 ArgVT = VA.getLocVT();
1449 break;
1450 }
1451 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001452 }
1453
1454 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001455 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001456 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001457 VA.getLocReg())
1458 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001459 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001460 } else if (VA.needsCustom()) {
1461 // TODO: We need custom lowering for vector (v2f64) args.
1462 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001463
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001464 CCValAssign &NextVA = ArgLocs[++i];
1465
1466 // TODO: Only handle register args for now.
1467 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1468
1469 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1470 TII.get(ARM::VMOVRRD), VA.getLocReg())
1471 .addReg(NextVA.getLocReg(), RegState::Define)
1472 .addReg(Arg));
1473 RegArgs.push_back(VA.getLocReg());
1474 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001475 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001476 assert(VA.isMemLoc());
1477 // Need to store on the stack.
1478 unsigned Base = ARM::SP;
1479 int Offset = VA.getLocMemOffset();
1480
1481 if (!ARMEmitStore(ArgVT, Arg, Base, Offset)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001482 }
1483 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001484 return true;
1485}
1486
Duncan Sands1440e8b2010-11-03 11:35:31 +00001487bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001488 const Instruction *I, CallingConv::ID CC,
1489 unsigned &NumBytes) {
1490 // Issue CALLSEQ_END
1491 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001492 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1493 TII.get(AdjStackUp))
1494 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001495
1496 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001497 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001498 SmallVector<CCValAssign, 16> RVLocs;
1499 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1500 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1501
1502 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001503 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001504 // For this move we copy into two registers and then move into the
1505 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001506 EVT DestVT = RVLocs[0].getValVT();
1507 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1508 unsigned ResultReg = createResultReg(DstRC);
1509 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1510 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001511 .addReg(RVLocs[0].getLocReg())
1512 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001513
Eric Christopher3659ac22010-10-20 08:02:24 +00001514 UsedRegs.push_back(RVLocs[0].getLocReg());
1515 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001516
Eric Christopherdccd2c32010-10-11 08:38:55 +00001517 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001518 UpdateValueMap(I, ResultReg);
1519 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001520 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001521 EVT CopyVT = RVLocs[0].getValVT();
1522 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001523
Eric Christopher14df8822010-10-01 00:00:11 +00001524 unsigned ResultReg = createResultReg(DstRC);
1525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1526 ResultReg).addReg(RVLocs[0].getLocReg());
1527 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001528
Eric Christopherdccd2c32010-10-11 08:38:55 +00001529 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001530 UpdateValueMap(I, ResultReg);
1531 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001532 }
1533
Eric Christopherdccd2c32010-10-11 08:38:55 +00001534 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001535}
1536
Eric Christopher4f512ef2010-10-22 01:28:00 +00001537bool ARMFastISel::SelectRet(const Instruction *I) {
1538 const ReturnInst *Ret = cast<ReturnInst>(I);
1539 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001540
Eric Christopher4f512ef2010-10-22 01:28:00 +00001541 if (!FuncInfo.CanLowerReturn)
1542 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001543
Eric Christopher4f512ef2010-10-22 01:28:00 +00001544 if (F.isVarArg())
1545 return false;
1546
1547 CallingConv::ID CC = F.getCallingConv();
1548 if (Ret->getNumOperands() > 0) {
1549 SmallVector<ISD::OutputArg, 4> Outs;
1550 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1551 Outs, TLI);
1552
1553 // Analyze operands of the call, assigning locations to each operand.
1554 SmallVector<CCValAssign, 16> ValLocs;
1555 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1556 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1557
1558 const Value *RV = Ret->getOperand(0);
1559 unsigned Reg = getRegForValue(RV);
1560 if (Reg == 0)
1561 return false;
1562
1563 // Only handle a single return value for now.
1564 if (ValLocs.size() != 1)
1565 return false;
1566
1567 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001568
Eric Christopher4f512ef2010-10-22 01:28:00 +00001569 // Don't bother handling odd stuff for now.
1570 if (VA.getLocInfo() != CCValAssign::Full)
1571 return false;
1572 // Only handle register returns for now.
1573 if (!VA.isRegLoc())
1574 return false;
1575 // TODO: For now, don't try to handle cases where getLocInfo()
1576 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001577 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001578 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001579
Eric Christopher4f512ef2010-10-22 01:28:00 +00001580 // Make the copy.
1581 unsigned SrcReg = Reg + VA.getValNo();
1582 unsigned DstReg = VA.getLocReg();
1583 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1584 // Avoid a cross-class copy. This is very unlikely.
1585 if (!SrcRC->contains(DstReg))
1586 return false;
1587 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1588 DstReg).addReg(SrcReg);
1589
1590 // Mark the register as live out of the function.
1591 MRI.addLiveOut(VA.getLocReg());
1592 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001593
Eric Christopher4f512ef2010-10-22 01:28:00 +00001594 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1595 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1596 TII.get(RetOpc)));
1597 return true;
1598}
1599
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001600// A quick function that will emit a call for a named libcall in F with the
1601// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001602// can emit a call for any libcall we can produce. This is an abridged version
1603// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001604// like computed function pointers or strange arguments at call sites.
1605// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1606// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001607bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1608 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001609
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001610 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001611 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001612 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001613 if (RetTy->isVoidTy())
1614 RetVT = MVT::isVoid;
1615 else if (!isTypeLegal(RetTy, RetVT))
1616 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001617
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001618 // For now we're using BLX etc on the assumption that we have v5t ops.
1619 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001620
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001621 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001622 SmallVector<Value*, 8> Args;
1623 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001624 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001625 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1626 Args.reserve(I->getNumOperands());
1627 ArgRegs.reserve(I->getNumOperands());
1628 ArgVTs.reserve(I->getNumOperands());
1629 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001630 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001631 Value *Op = I->getOperand(i);
1632 unsigned Arg = getRegForValue(Op);
1633 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001634
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001635 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001636 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001637 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001638
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001639 ISD::ArgFlagsTy Flags;
1640 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1641 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001642
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001643 Args.push_back(Op);
1644 ArgRegs.push_back(Arg);
1645 ArgVTs.push_back(ArgVT);
1646 ArgFlags.push_back(Flags);
1647 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001648
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001649 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001650 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001651 unsigned NumBytes;
1652 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1653 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001654
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001655 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001656 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001657 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001658 unsigned CallOpc;
1659 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001660 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001661 else
1662 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001663 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001664 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001665
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001666 // Add implicit physical register uses to the call.
1667 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1668 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001669
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001670 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001671 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001672 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001673
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001674 // Set all unused physreg defs as dead.
1675 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001676
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001677 return true;
1678}
1679
Eric Christopherf9764fa2010-09-30 20:49:44 +00001680bool ARMFastISel::SelectCall(const Instruction *I) {
1681 const CallInst *CI = cast<CallInst>(I);
1682 const Value *Callee = CI->getCalledValue();
1683
1684 // Can't handle inline asm or worry about intrinsics yet.
1685 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1686
Eric Christophere6ca6772010-10-01 21:33:12 +00001687 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001688 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001689 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1690 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001691
Eric Christopherf9764fa2010-09-30 20:49:44 +00001692 // Check the calling convention.
1693 ImmutableCallSite CS(CI);
1694 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001695
Eric Christopherf9764fa2010-09-30 20:49:44 +00001696 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001697
Eric Christopherf9764fa2010-09-30 20:49:44 +00001698 // Let SDISel handle vararg functions.
1699 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1700 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1701 if (FTy->isVarArg())
1702 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001703
Eric Christopherf9764fa2010-09-30 20:49:44 +00001704 // Handle *simple* calls for now.
1705 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001706 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001707 if (RetTy->isVoidTy())
1708 RetVT = MVT::isVoid;
1709 else if (!isTypeLegal(RetTy, RetVT))
1710 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001711
Eric Christopherf9764fa2010-09-30 20:49:44 +00001712 // For now we're using BLX etc on the assumption that we have v5t ops.
1713 // TODO: Maybe?
1714 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001715
Eric Christopherf9764fa2010-09-30 20:49:44 +00001716 // Set up the argument vectors.
1717 SmallVector<Value*, 8> Args;
1718 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001719 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001720 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1721 Args.reserve(CS.arg_size());
1722 ArgRegs.reserve(CS.arg_size());
1723 ArgVTs.reserve(CS.arg_size());
1724 ArgFlags.reserve(CS.arg_size());
1725 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1726 i != e; ++i) {
1727 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001728
Eric Christopherf9764fa2010-09-30 20:49:44 +00001729 if (Arg == 0)
1730 return false;
1731 ISD::ArgFlagsTy Flags;
1732 unsigned AttrInd = i - CS.arg_begin() + 1;
1733 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1734 Flags.setSExt();
1735 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1736 Flags.setZExt();
1737
1738 // FIXME: Only handle *easy* calls for now.
1739 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1740 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1741 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1742 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1743 return false;
1744
1745 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001746 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001747 if (!isTypeLegal(ArgTy, ArgVT))
1748 return false;
1749 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1750 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001751
Eric Christopherf9764fa2010-09-30 20:49:44 +00001752 Args.push_back(*i);
1753 ArgRegs.push_back(Arg);
1754 ArgVTs.push_back(ArgVT);
1755 ArgFlags.push_back(Flags);
1756 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001757
Eric Christopherf9764fa2010-09-30 20:49:44 +00001758 // Handle the arguments now that we've gotten them.
1759 SmallVector<unsigned, 4> RegArgs;
1760 unsigned NumBytes;
1761 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1762 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001763
Eric Christopherf9764fa2010-09-30 20:49:44 +00001764 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001765 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001766 MachineInstrBuilder MIB;
1767 unsigned CallOpc;
1768 if(isThumb)
1769 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1770 else
1771 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1772 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1773 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001774
Eric Christopherf9764fa2010-09-30 20:49:44 +00001775 // Add implicit physical register uses to the call.
1776 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1777 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001778
Eric Christopherf9764fa2010-09-30 20:49:44 +00001779 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001780 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001781 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001782
Eric Christopherf9764fa2010-09-30 20:49:44 +00001783 // Set all unused physreg defs as dead.
1784 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001785
Eric Christopherf9764fa2010-09-30 20:49:44 +00001786 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001787
Eric Christopherf9764fa2010-09-30 20:49:44 +00001788}
1789
Eric Christopher56d2b722010-09-02 23:43:26 +00001790// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001791bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001792
Eric Christopherab695882010-07-21 22:26:11 +00001793 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001794 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001795 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001796 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001797 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001798 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001799 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001800 case Instruction::ICmp:
1801 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001802 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001803 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001804 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001805 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001806 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001807 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001808 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001809 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001810 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001811 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001812 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001813 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001814 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001815 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001816 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001817 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001818 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001819 case Instruction::SRem:
1820 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001821 case Instruction::Call:
1822 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001823 case Instruction::Select:
1824 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001825 case Instruction::Ret:
1826 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001827 default: break;
1828 }
1829 return false;
1830}
1831
1832namespace llvm {
1833 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001834 // Completely untested on non-darwin.
1835 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001836
Eric Christopheraaa8df42010-11-02 01:21:28 +00001837 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001838 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001839 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001840 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001841 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001842 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001843 }
1844}