blob: fd758859557ebd10e7de228d06e2ab52ebebf811 [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"
22#include "llvm/CallingConv.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/Instructions.h"
26#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000027#include "llvm/Module.h"
Eric Christopherab695882010-07-21 22:26:11 +000028#include "llvm/CodeGen/Analysis.h"
29#include "llvm/CodeGen/FastISel.h"
30#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000033#include "llvm/CodeGen/MachineConstantPool.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineRegisterInfo.h"
36#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000037#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000038#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000040#include "llvm/Target/TargetData.h"
41#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetLowering.h"
43#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000044#include "llvm/Target/TargetOptions.h"
45using namespace llvm;
46
Eric Christopher038fea52010-08-17 00:46:57 +000047static cl::opt<bool>
48EnableARMFastISel("arm-fast-isel",
49 cl::desc("Turn on experimental ARM fast-isel support"),
50 cl::init(false), cl::Hidden);
51
Eric Christopherab695882010-07-21 22:26:11 +000052namespace {
53
54class ARMFastISel : public FastISel {
55
56 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
57 /// make the right decision when generating code for different targets.
58 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000059 const TargetMachine &TM;
60 const TargetInstrInfo &TII;
61 const TargetLowering &TLI;
Eric Christopher7fe55b72010-08-23 22:32:45 +000062 const ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000063
Eric Christopher8cf6c602010-09-29 22:24:45 +000064 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000065 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000066 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000067
Eric Christopherab695882010-07-21 22:26:11 +000068 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000069 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000070 : FastISel(funcInfo),
71 TM(funcInfo.MF->getTarget()),
72 TII(*TM.getInstrInfo()),
73 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000074 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000075 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000076 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000077 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000078 }
79
Eric Christophercb592292010-08-20 00:20:31 +000080 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000081 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
82 const TargetRegisterClass *RC);
83 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
84 const TargetRegisterClass *RC,
85 unsigned Op0, bool Op0IsKill);
86 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill,
89 unsigned Op1, bool Op1IsKill);
90 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
91 const TargetRegisterClass *RC,
92 unsigned Op0, bool Op0IsKill,
93 uint64_t Imm);
94 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
95 const TargetRegisterClass *RC,
96 unsigned Op0, bool Op0IsKill,
97 const ConstantFP *FPImm);
98 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
99 const TargetRegisterClass *RC,
100 uint64_t Imm);
101 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
102 const TargetRegisterClass *RC,
103 unsigned Op0, bool Op0IsKill,
104 unsigned Op1, bool Op1IsKill,
105 uint64_t Imm);
106 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
107 unsigned Op0, bool Op0IsKill,
108 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000109
Eric Christophercb592292010-08-20 00:20:31 +0000110 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000111 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000112 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherab695882010-07-21 22:26:11 +0000113
114 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000115
Eric Christopher83007122010-08-23 21:44:12 +0000116 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000117 private:
Eric Christopher43b62be2010-09-27 06:02:23 +0000118 virtual bool SelectLoad(const Instruction *I);
119 virtual bool SelectStore(const Instruction *I);
120 virtual bool SelectBranch(const Instruction *I);
121 virtual bool SelectCmp(const Instruction *I);
122 virtual bool SelectFPExt(const Instruction *I);
123 virtual bool SelectFPTrunc(const Instruction *I);
124 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
125 virtual bool SelectSIToFP(const Instruction *I);
126 virtual bool SelectFPToSI(const Instruction *I);
127 virtual bool SelectSDiv(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000128
Eric Christopher83007122010-08-23 21:44:12 +0000129 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000130 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000131 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000132 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000133 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000134 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000135 bool ARMLoadAlloca(const Instruction *I, EVT VT);
136 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000137 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000138 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000139 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000140 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000141 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000142
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000143 // Call handling routines.
144 private:
145 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000146 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
147 SmallVectorImpl<unsigned> &ArgRegs,
148 SmallVectorImpl<EVT> &ArgVTs,
149 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
150 SmallVectorImpl<unsigned> &RegArgs,
151 CallingConv::ID CC,
152 unsigned &NumBytes);
153 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
154 const Instruction *I, CallingConv::ID CC,
155 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000156 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000157
158 // OptionalDef handling routines.
159 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000160 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
161 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
162};
Eric Christopherab695882010-07-21 22:26:11 +0000163
164} // end anonymous namespace
165
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000166#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000167
Eric Christopher456144e2010-08-19 00:37:05 +0000168// DefinesOptionalPredicate - This is different from DefinesPredicate in that
169// we don't care about implicit defs here, just places we'll need to add a
170// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
171bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
172 const TargetInstrDesc &TID = MI->getDesc();
173 if (!TID.hasOptionalDef())
174 return false;
175
176 // Look to see if our OptionalDef is defining CPSR or CCR.
177 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
178 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000179 if (!MO.isReg() || !MO.isDef()) continue;
180 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000181 *CPSR = true;
182 }
183 return true;
184}
185
186// If the machine is predicable go ahead and add the predicate operands, if
187// it needs default CC operands add those.
188const MachineInstrBuilder &
189ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
190 MachineInstr *MI = &*MIB;
191
192 // Do we use a predicate?
193 if (TII.isPredicable(MI))
194 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000195
Eric Christopher456144e2010-08-19 00:37:05 +0000196 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
197 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000198 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000199 if (DefinesOptionalPredicate(MI, &CPSR)) {
200 if (CPSR)
201 AddDefaultT1CC(MIB);
202 else
203 AddDefaultCC(MIB);
204 }
205 return MIB;
206}
207
Eric Christopher0fe7d542010-08-17 01:25:29 +0000208unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
209 const TargetRegisterClass* RC) {
210 unsigned ResultReg = createResultReg(RC);
211 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
212
Eric Christopher456144e2010-08-19 00:37:05 +0000213 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000214 return ResultReg;
215}
216
217unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
218 const TargetRegisterClass *RC,
219 unsigned Op0, bool Op0IsKill) {
220 unsigned ResultReg = createResultReg(RC);
221 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
222
223 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000224 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000225 .addReg(Op0, Op0IsKill * RegState::Kill));
226 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000227 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000228 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000229 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000230 TII.get(TargetOpcode::COPY), ResultReg)
231 .addReg(II.ImplicitDefs[0]));
232 }
233 return ResultReg;
234}
235
236unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
237 const TargetRegisterClass *RC,
238 unsigned Op0, bool Op0IsKill,
239 unsigned Op1, bool Op1IsKill) {
240 unsigned ResultReg = createResultReg(RC);
241 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
242
243 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000244 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000245 .addReg(Op0, Op0IsKill * RegState::Kill)
246 .addReg(Op1, Op1IsKill * RegState::Kill));
247 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000248 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000249 .addReg(Op0, Op0IsKill * RegState::Kill)
250 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000251 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000252 TII.get(TargetOpcode::COPY), ResultReg)
253 .addReg(II.ImplicitDefs[0]));
254 }
255 return ResultReg;
256}
257
258unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
259 const TargetRegisterClass *RC,
260 unsigned Op0, bool Op0IsKill,
261 uint64_t Imm) {
262 unsigned ResultReg = createResultReg(RC);
263 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
264
265 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000266 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000267 .addReg(Op0, Op0IsKill * RegState::Kill)
268 .addImm(Imm));
269 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000270 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000271 .addReg(Op0, Op0IsKill * RegState::Kill)
272 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000274 TII.get(TargetOpcode::COPY), ResultReg)
275 .addReg(II.ImplicitDefs[0]));
276 }
277 return ResultReg;
278}
279
280unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
281 const TargetRegisterClass *RC,
282 unsigned Op0, bool Op0IsKill,
283 const ConstantFP *FPImm) {
284 unsigned ResultReg = createResultReg(RC);
285 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
286
287 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000289 .addReg(Op0, Op0IsKill * RegState::Kill)
290 .addFPImm(FPImm));
291 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293 .addReg(Op0, Op0IsKill * RegState::Kill)
294 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000295 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000296 TII.get(TargetOpcode::COPY), ResultReg)
297 .addReg(II.ImplicitDefs[0]));
298 }
299 return ResultReg;
300}
301
302unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
303 const TargetRegisterClass *RC,
304 unsigned Op0, bool Op0IsKill,
305 unsigned Op1, bool Op1IsKill,
306 uint64_t Imm) {
307 unsigned ResultReg = createResultReg(RC);
308 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
309
310 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000312 .addReg(Op0, Op0IsKill * RegState::Kill)
313 .addReg(Op1, Op1IsKill * RegState::Kill)
314 .addImm(Imm));
315 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000316 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000317 .addReg(Op0, Op0IsKill * RegState::Kill)
318 .addReg(Op1, Op1IsKill * RegState::Kill)
319 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000321 TII.get(TargetOpcode::COPY), ResultReg)
322 .addReg(II.ImplicitDefs[0]));
323 }
324 return ResultReg;
325}
326
327unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
328 const TargetRegisterClass *RC,
329 uint64_t Imm) {
330 unsigned ResultReg = createResultReg(RC);
331 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000332
Eric Christopher0fe7d542010-08-17 01:25:29 +0000333 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000334 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000335 .addImm(Imm));
336 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000338 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000339 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000340 TII.get(TargetOpcode::COPY), ResultReg)
341 .addReg(II.ImplicitDefs[0]));
342 }
343 return ResultReg;
344}
345
346unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
347 unsigned Op0, bool Op0IsKill,
348 uint32_t Idx) {
349 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
350 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
351 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000353 DL, TII.get(TargetOpcode::COPY), ResultReg)
354 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
355 return ResultReg;
356}
357
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000358// TODO: Don't worry about 64-bit now, but when this is fixed remove the
359// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000360unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000361 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
362
363 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
364 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
365 TII.get(ARM::VMOVRS), MoveReg)
366 .addReg(SrcReg));
367 return MoveReg;
368}
369
370unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000371 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
372
Eric Christopheraa3ace12010-09-09 20:49:25 +0000373 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000375 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000376 .addReg(SrcReg));
377 return MoveReg;
378}
379
Eric Christopher9ed58df2010-09-09 00:19:41 +0000380// For double width floating point we need to materialize two constants
381// (the high and the low) into integer registers then use a move to get
382// the combined constant into an FP reg.
383unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
384 const APFloat Val = CFP->getValueAPF();
385 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000386
Eric Christopher9ed58df2010-09-09 00:19:41 +0000387 // This checks to see if we can use VFP3 instructions to materialize
388 // a constant, otherwise we have to go through the constant pool.
389 if (TLI.isFPImmLegal(Val, VT)) {
390 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
391 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
392 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
393 DestReg)
394 .addFPImm(CFP));
395 return DestReg;
396 }
Eric Christopher238bb162010-09-09 23:50:00 +0000397
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000398 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000399 if (!Subtarget->hasVFP2()) return false;
400
401 // MachineConstantPool wants an explicit alignment.
402 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
403 if (Align == 0) {
404 // TODO: Figure out if this is correct.
405 Align = TD.getTypeAllocSize(CFP->getType());
406 }
407 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
408 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
409 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
410
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000411 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000412 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
413 DestReg)
414 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000415 .addReg(0));
416 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000417}
418
Eric Christopher744c7c82010-09-28 22:47:54 +0000419unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
420
421 // For now 32-bit only.
422 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
423
Eric Christopher56d2b722010-09-02 23:43:26 +0000424 // MachineConstantPool wants an explicit alignment.
425 unsigned Align = TD.getPrefTypeAlignment(C->getType());
426 if (Align == 0) {
427 // TODO: Figure out if this is correct.
428 Align = TD.getTypeAllocSize(C->getType());
429 }
430 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000431 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000432
Eric Christopher56d2b722010-09-02 23:43:26 +0000433 if (isThumb)
434 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000435 TII.get(ARM::t2LDRpci), DestReg)
436 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000437 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000438 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000439 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000440 TII.get(ARM::LDRcp), DestReg)
441 .addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000442 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000443
Eric Christopher56d2b722010-09-02 23:43:26 +0000444 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000445}
446
Eric Christopher9ed58df2010-09-09 00:19:41 +0000447unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
448 EVT VT = TLI.getValueType(C->getType(), true);
449
450 // Only handle simple types.
451 if (!VT.isSimple()) return 0;
452
453 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
454 return ARMMaterializeFP(CFP, VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000455 return ARMMaterializeInt(C, VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000456}
457
Eric Christopherb1cc8482010-08-25 07:23:49 +0000458bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
459 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000460
Eric Christopherb1cc8482010-08-25 07:23:49 +0000461 // Only handle simple types.
462 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000463
Eric Christopherdc908042010-08-31 01:28:42 +0000464 // Handle all legal types, i.e. a register that will directly hold this
465 // value.
466 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000467}
468
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000469bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
470 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000471
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000472 // If this is a type than can be sign or zero-extended to a basic operation
473 // go ahead and accept it now.
474 if (VT == MVT::i8 || VT == MVT::i16)
475 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000476
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000477 return false;
478}
479
Eric Christophercb0b04b2010-08-24 00:07:24 +0000480// Computes the Reg+Offset to get to an object.
481bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000482 int &Offset) {
483 // Some boilerplate from the X86 FastISel.
484 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000485 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000486 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000487 // Don't walk into other basic blocks; it's possible we haven't
488 // visited them yet, so the instructions may not yet be assigned
489 // virtual registers.
490 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
491 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000492 Opcode = I->getOpcode();
493 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000494 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000495 Opcode = C->getOpcode();
496 U = C;
497 }
498
Eric Christophercb0b04b2010-08-24 00:07:24 +0000499 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000500 if (Ty->getAddressSpace() > 255)
501 // Fast instruction selection doesn't support the special
502 // address spaces.
503 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000504
Eric Christopher83007122010-08-23 21:44:12 +0000505 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000506 default:
Eric Christopher83007122010-08-23 21:44:12 +0000507 break;
508 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000509 assert(false && "Alloca should have been handled earlier!");
510 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000511 }
512 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000513
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000514 // FIXME: Handle global variables.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000515 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000516 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000517 return false;
518 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000519
Eric Christophercb0b04b2010-08-24 00:07:24 +0000520 // Try to get this in a register if nothing else has worked.
521 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000522 if (Reg == 0) return false;
523
524 // Since the offset may be too large for the load instruction
525 // get the reg+offset into a register.
526 // TODO: Verify the additions work, otherwise we'll need to add the
527 // offset instead of 0 to the instructions and do all sorts of operand
528 // munging.
529 // TODO: Optimize this somewhat.
530 if (Offset != 0) {
531 ARMCC::CondCodes Pred = ARMCC::AL;
532 unsigned PredReg = 0;
533
Eric Christophereaa204b2010-09-02 01:39:14 +0000534 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000535 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
536 Reg, Reg, Offset, Pred, PredReg,
537 static_cast<const ARMBaseInstrInfo&>(TII));
538 else {
539 assert(AFI->isThumb2Function());
540 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
541 Reg, Reg, Offset, Pred, PredReg,
542 static_cast<const ARMBaseInstrInfo&>(TII));
543 }
544 }
Eric Christopher318b6ee2010-09-02 00:53:56 +0000545 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000546}
547
Eric Christopher30b66332010-09-08 21:49:50 +0000548bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000549 Value *Op0 = I->getOperand(0);
550
551 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000552 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
553 DenseMap<const AllocaInst*, int>::iterator SI =
554 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000555
Eric Christophere24d66f2010-08-24 22:07:27 +0000556 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000557 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000558 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000559 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000560 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000561 TM.getRegisterInfo());
562 UpdateValueMap(I, ResultReg);
563 return true;
564 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000565 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000566 return false;
567}
568
Eric Christopherb1cc8482010-08-25 07:23:49 +0000569bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
570 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000571
Eric Christopherb1cc8482010-08-25 07:23:49 +0000572 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000573 unsigned Opc;
Eric Christopher6dab1372010-09-18 01:59:37 +0000574 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000575 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000576 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000577 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000578 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000579 case MVT::i16:
580 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
581 VT = MVT::i32;
582 break;
583 case MVT::i8:
584 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
585 VT = MVT::i32;
586 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000587 case MVT::i32:
588 Opc = isThumb ? ARM::tLDR : ARM::LDR;
589 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000590 case MVT::f32:
591 Opc = ARM::VLDRS;
592 isFloat = true;
593 break;
594 case MVT::f64:
595 Opc = ARM::VLDRD;
596 isFloat = true;
597 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000598 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000599
Eric Christopherdc908042010-08-31 01:28:42 +0000600 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000601
Eric Christopherdc908042010-08-31 01:28:42 +0000602 // TODO: Fix the Addressing modes so that these can share some code.
603 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
Eric Christopher6dab1372010-09-18 01:59:37 +0000604 // The thumb addressing mode has operands swapped from the arm addressing
605 // mode, the floating point one only has two operands.
606 if (isFloat)
607 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
608 TII.get(Opc), ResultReg)
609 .addReg(Reg).addImm(Offset));
610 else if (isThumb)
Eric Christopherdc908042010-08-31 01:28:42 +0000611 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
612 TII.get(Opc), ResultReg)
613 .addReg(Reg).addImm(Offset).addReg(0));
614 else
615 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
616 TII.get(Opc), ResultReg)
617 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000618 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000619}
620
Eric Christopher43b62be2010-09-27 06:02:23 +0000621bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000622 // Verify we have a legal type before going any further.
623 EVT VT;
624 if (!isLoadTypeLegal(I->getType(), VT))
625 return false;
626
627 // If we're an alloca we know we have a frame index and can emit the load
628 // directly in short order.
629 if (ARMLoadAlloca(I, VT))
630 return true;
631
632 // Our register and offset with innocuous defaults.
633 unsigned Reg = 0;
634 int Offset = 0;
635
636 // See if we can handle this as Reg + Offset
637 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
638 return false;
639
640 unsigned ResultReg;
641 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
642
643 UpdateValueMap(I, ResultReg);
644 return true;
645}
646
Eric Christopher30b66332010-09-08 21:49:50 +0000647bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000648 Value *Op1 = I->getOperand(1);
649
650 // Verify it's an alloca.
651 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
652 DenseMap<const AllocaInst*, int>::iterator SI =
653 FuncInfo.StaticAllocaMap.find(AI);
654
655 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000656 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000657 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000658 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000659 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000660 TM.getRegisterInfo());
661 return true;
662 }
663 }
664 return false;
665}
666
Eric Christopher318b6ee2010-09-02 00:53:56 +0000667bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
668 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000669 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000670 bool isFloat = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000671 switch (VT.getSimpleVT().SimpleTy) {
672 default: return false;
673 case MVT::i1:
674 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
675 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
676 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000677 case MVT::f32:
678 if (!Subtarget->hasVFP2()) return false;
679 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000680 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000681 break;
682 case MVT::f64:
683 if (!Subtarget->hasVFP2()) return false;
684 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000685 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000686 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000687 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000688
Eric Christopherb74558a2010-09-18 01:23:38 +0000689 // The thumb addressing mode has operands swapped from the arm addressing
690 // mode, the floating point one only has two operands.
Eric Christopher6dab1372010-09-18 01:59:37 +0000691 if (isFloat)
Eric Christopherb74558a2010-09-18 01:23:38 +0000692 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
693 TII.get(StrOpc), SrcReg)
694 .addReg(DstReg).addImm(Offset));
Eric Christopher6dab1372010-09-18 01:59:37 +0000695 else if (isThumb)
696 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
697 TII.get(StrOpc), SrcReg)
698 .addReg(DstReg).addImm(Offset).addReg(0));
699
Eric Christopher318b6ee2010-09-02 00:53:56 +0000700 else
701 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
702 TII.get(StrOpc), SrcReg)
703 .addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000704
Eric Christopher318b6ee2010-09-02 00:53:56 +0000705 return true;
706}
707
Eric Christopher43b62be2010-09-27 06:02:23 +0000708bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000709 Value *Op0 = I->getOperand(0);
710 unsigned SrcReg = 0;
711
Eric Christopher543cf052010-09-01 22:16:27 +0000712 // Yay type legalization
713 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000714 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000715 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000716
Eric Christopher1b61ef42010-09-02 01:48:11 +0000717 // Get the value to be stored into a register.
718 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000719 if (SrcReg == 0)
720 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000721
Eric Christopher318b6ee2010-09-02 00:53:56 +0000722 // If we're an alloca we know we have a frame index and can emit the store
723 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000724 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000725 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000726
Eric Christopher318b6ee2010-09-02 00:53:56 +0000727 // Our register and offset with innocuous defaults.
728 unsigned Reg = 0;
729 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000730
Eric Christopher318b6ee2010-09-02 00:53:56 +0000731 // See if we can handle this as Reg + Offset
732 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
733 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000734
Eric Christopher318b6ee2010-09-02 00:53:56 +0000735 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000736
Eric Christophera5b1e682010-09-17 22:28:18 +0000737 return true;
738}
739
740static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
741 switch (Pred) {
742 // Needs two compares...
743 case CmpInst::FCMP_ONE:
744 case CmpInst::FCMP_UEQ:
745 default:
746 assert(false && "Unhandled CmpInst::Predicate!");
747 return ARMCC::AL;
748 case CmpInst::ICMP_EQ:
749 case CmpInst::FCMP_OEQ:
750 return ARMCC::EQ;
751 case CmpInst::ICMP_SGT:
752 case CmpInst::FCMP_OGT:
753 return ARMCC::GT;
754 case CmpInst::ICMP_SGE:
755 case CmpInst::FCMP_OGE:
756 return ARMCC::GE;
757 case CmpInst::ICMP_UGT:
758 case CmpInst::FCMP_UGT:
759 return ARMCC::HI;
760 case CmpInst::FCMP_OLT:
761 return ARMCC::MI;
762 case CmpInst::ICMP_ULE:
763 case CmpInst::FCMP_OLE:
764 return ARMCC::LS;
765 case CmpInst::FCMP_ORD:
766 return ARMCC::VC;
767 case CmpInst::FCMP_UNO:
768 return ARMCC::VS;
769 case CmpInst::FCMP_UGE:
770 return ARMCC::PL;
771 case CmpInst::ICMP_SLT:
772 case CmpInst::FCMP_ULT:
773 return ARMCC::LT;
774 case CmpInst::ICMP_SLE:
775 case CmpInst::FCMP_ULE:
776 return ARMCC::LE;
777 case CmpInst::FCMP_UNE:
778 case CmpInst::ICMP_NE:
779 return ARMCC::NE;
780 case CmpInst::ICMP_UGE:
781 return ARMCC::HS;
782 case CmpInst::ICMP_ULT:
783 return ARMCC::LO;
784 }
Eric Christopher543cf052010-09-01 22:16:27 +0000785}
786
Eric Christopher43b62be2010-09-27 06:02:23 +0000787bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000788 const BranchInst *BI = cast<BranchInst>(I);
789 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
790 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000791
Eric Christophere5734102010-09-03 00:35:47 +0000792 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000793 // TODO: Try to avoid the re-computation in some places.
794 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000795 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000796
Eric Christopher229207a2010-09-29 01:14:47 +0000797 // Re-set the flags just in case.
798 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
799 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
800 .addReg(CondReg).addImm(1));
Eric Christophera5b1e682010-09-17 22:28:18 +0000801
Eric Christophere5734102010-09-03 00:35:47 +0000802 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000803 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000804 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000805 FastEmitBranch(FBB, DL);
806 FuncInfo.MBB->addSuccessor(TBB);
Eric Christophera5b1e682010-09-17 22:28:18 +0000807 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000808}
809
Eric Christopher43b62be2010-09-27 06:02:23 +0000810bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000811 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000812
Eric Christopherd43393a2010-09-08 23:13:45 +0000813 EVT VT;
814 const Type *Ty = CI->getOperand(0)->getType();
815 if (!isTypeLegal(Ty, VT))
816 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000817
Eric Christopherd43393a2010-09-08 23:13:45 +0000818 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
819 if (isFloat && !Subtarget->hasVFP2())
820 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000821
Eric Christopherd43393a2010-09-08 23:13:45 +0000822 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000823 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +0000824 switch (VT.getSimpleVT().SimpleTy) {
825 default: return false;
826 // TODO: Verify compares.
827 case MVT::f32:
828 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +0000829 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000830 break;
831 case MVT::f64:
832 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +0000833 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000834 break;
835 case MVT::i32:
836 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +0000837 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000838 break;
839 }
840
Eric Christopher229207a2010-09-29 01:14:47 +0000841 // Get the compare predicate.
842 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
843
844 // We may not handle every CC for now.
845 if (ARMPred == ARMCC::AL) return false;
846
Eric Christopherd43393a2010-09-08 23:13:45 +0000847 unsigned Arg1 = getRegForValue(CI->getOperand(0));
848 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000849
Eric Christopherd43393a2010-09-08 23:13:45 +0000850 unsigned Arg2 = getRegForValue(CI->getOperand(1));
851 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000852
Eric Christopherd43393a2010-09-08 23:13:45 +0000853 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
854 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000855
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000856 // For floating point we need to move the result to a comparison register
857 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000858 if (isFloat)
859 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
860 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000861
Eric Christopher229207a2010-09-29 01:14:47 +0000862 // Now set a register based on the comparison. Explicitly set the predicates
863 // here.
864 unsigned MovCCOpc = isThumb ? ARM::tMOVCCi : ARM::MOVCCi;
865 unsigned DestReg = createResultReg(ARM::GPRRegisterClass);
866 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +0000867 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +0000868 unsigned ZeroReg = TargetMaterializeConstant(Zero);
869 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
870 .addReg(ZeroReg).addImm(1)
871 .addImm(ARMPred).addReg(CondReg);
872
Eric Christophera5b1e682010-09-17 22:28:18 +0000873 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +0000874 return true;
875}
876
Eric Christopher43b62be2010-09-27 06:02:23 +0000877bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +0000878 // Make sure we have VFP and that we're extending float to double.
879 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000880
Eric Christopher46203602010-09-09 00:26:48 +0000881 Value *V = I->getOperand(0);
882 if (!I->getType()->isDoubleTy() ||
883 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000884
Eric Christopher46203602010-09-09 00:26:48 +0000885 unsigned Op = getRegForValue(V);
886 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000887
Eric Christopher46203602010-09-09 00:26:48 +0000888 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000889 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000890 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000891 .addReg(Op));
892 UpdateValueMap(I, Result);
893 return true;
894}
895
Eric Christopher43b62be2010-09-27 06:02:23 +0000896bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +0000897 // Make sure we have VFP and that we're truncating double to float.
898 if (!Subtarget->hasVFP2()) return false;
899
900 Value *V = I->getOperand(0);
901 if (!I->getType()->isFloatTy() ||
902 !V->getType()->isDoubleTy()) return false;
903
904 unsigned Op = getRegForValue(V);
905 if (Op == 0) return false;
906
907 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +0000908 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000909 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000910 .addReg(Op));
911 UpdateValueMap(I, Result);
912 return true;
913}
914
Eric Christopher43b62be2010-09-27 06:02:23 +0000915bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +0000916 // Make sure we have VFP.
917 if (!Subtarget->hasVFP2()) return false;
918
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000919 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000920 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000921 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000922 return false;
923
924 unsigned Op = getRegForValue(I->getOperand(0));
925 if (Op == 0) return false;
926
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000927 // The conversion routine works on fp-reg to fp-reg and the operand above
928 // was an integer, move it to the fp registers if possible.
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000929 unsigned FP = ARMMoveToFPReg(DstVT, Op);
930 if (FP == 0) return false;
931
Eric Christopher9a040492010-09-09 18:54:59 +0000932 unsigned Opc;
933 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
934 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
935 else return 0;
936
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000937 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000938 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
939 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000940 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +0000941 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000942 return true;
943}
944
Eric Christopher43b62be2010-09-27 06:02:23 +0000945bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +0000946 // Make sure we have VFP.
947 if (!Subtarget->hasVFP2()) return false;
948
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000949 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000950 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +0000951 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000952 return false;
953
954 unsigned Op = getRegForValue(I->getOperand(0));
955 if (Op == 0) return false;
956
957 unsigned Opc;
958 const Type *OpTy = I->getOperand(0)->getType();
959 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
960 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
961 else return 0;
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000962 EVT OpVT = TLI.getValueType(OpTy, true);
Eric Christopher9a040492010-09-09 18:54:59 +0000963
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000964 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000965 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
966 ResultReg)
967 .addReg(Op));
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000968
969 // This result needs to be in an integer register, but the conversion only
970 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000971 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000972 if (IntReg == 0) return false;
973
974 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000975 return true;
976}
977
Eric Christopher43b62be2010-09-27 06:02:23 +0000978bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000979 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000980
Eric Christopherbc39b822010-09-09 00:53:57 +0000981 // We can get here in the case when we want to use NEON for our fp
982 // operations, but can't figure out how to. Just use the vfp instructions
983 // if we have them.
984 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000985 const Type *Ty = I->getType();
986 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
987 if (isFloat && !Subtarget->hasVFP2())
988 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000989
Eric Christopherbc39b822010-09-09 00:53:57 +0000990 unsigned Op1 = getRegForValue(I->getOperand(0));
991 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000992
Eric Christopherbc39b822010-09-09 00:53:57 +0000993 unsigned Op2 = getRegForValue(I->getOperand(1));
994 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000995
Eric Christopherbc39b822010-09-09 00:53:57 +0000996 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000997 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
998 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000999 switch (ISDOpcode) {
1000 default: return false;
1001 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001002 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001003 break;
1004 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001005 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001006 break;
1007 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001008 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001009 break;
1010 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001011 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001012 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1013 TII.get(Opc), ResultReg)
1014 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001015 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001016 return true;
1017}
1018
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001019// Call Handling Code
1020
1021// This is largely taken directly from CCAssignFnForNode - we don't support
1022// varargs in FastISel so that part has been removed.
1023// TODO: We may not support all of this.
1024CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1025 switch (CC) {
1026 default:
1027 llvm_unreachable("Unsupported calling convention");
1028 case CallingConv::C:
1029 case CallingConv::Fast:
1030 // Use target triple & subtarget features to do actual dispatch.
1031 if (Subtarget->isAAPCS_ABI()) {
1032 if (Subtarget->hasVFP2() &&
1033 FloatABIType == FloatABI::Hard)
1034 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1035 else
1036 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1037 } else
1038 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1039 case CallingConv::ARM_AAPCS_VFP:
1040 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1041 case CallingConv::ARM_AAPCS:
1042 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1043 case CallingConv::ARM_APCS:
1044 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1045 }
1046}
1047
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001048bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1049 SmallVectorImpl<unsigned> &ArgRegs,
1050 SmallVectorImpl<EVT> &ArgVTs,
1051 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1052 SmallVectorImpl<unsigned> &RegArgs,
1053 CallingConv::ID CC,
1054 unsigned &NumBytes) {
1055 SmallVector<CCValAssign, 16> ArgLocs;
1056 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1057 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1058
1059 // Get a count of how many bytes are to be pushed on the stack.
1060 NumBytes = CCInfo.getNextStackOffset();
1061
1062 // Issue CALLSEQ_START
1063 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1064 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1065 .addImm(NumBytes);
1066
1067 // Process the args.
1068 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1069 CCValAssign &VA = ArgLocs[i];
1070 unsigned Arg = ArgRegs[VA.getValNo()];
1071 EVT ArgVT = ArgVTs[VA.getValNo()];
1072
1073 // Should we ever have to promote?
1074 switch (VA.getLocInfo()) {
1075 case CCValAssign::Full: break;
1076 default:
1077 assert(false && "Handle arg promotion for libcalls?");
1078 return false;
1079 }
1080
1081 // Now copy/store arg to correct locations.
1082 if (VA.isRegLoc()) {
1083 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1084 VA.getLocReg())
1085 .addReg(Arg);
1086 RegArgs.push_back(VA.getLocReg());
1087 } else {
1088 // Need to store
1089 return false;
1090 }
1091 }
1092
1093 return true;
1094}
1095
1096bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1097 const Instruction *I, CallingConv::ID CC,
1098 unsigned &NumBytes) {
1099 // Issue CALLSEQ_END
1100 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1101 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1102 .addImm(NumBytes).addImm(0);
1103
1104 // Now the return value.
1105 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1106 SmallVector<CCValAssign, 16> RVLocs;
1107 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1108 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1109
1110 // Copy all of the result registers out of their specified physreg.
1111 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1112 EVT CopyVT = RVLocs[0].getValVT();
1113 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1114
1115 unsigned ResultReg = createResultReg(DstRC);
1116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1117 ResultReg).addReg(RVLocs[0].getLocReg());
1118 UsedRegs.push_back(RVLocs[0].getLocReg());
1119
1120 // Finally update the result.
1121 UpdateValueMap(I, ResultReg);
1122 }
1123
1124 return true;
1125}
1126
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001127// A quick function that will emit a call for a named libcall in F with the
1128// vector of passed arguments for the Instruction in I. We can assume that we
1129// can emit a call for any libcall we can produce. This is an abridged version
1130// of the full call infrastructure since we won't need to worry about things
1131// like computed function pointers or strange arguments at call sites.
1132// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1133// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001134bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1135 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1136
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001137 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001138 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001139 EVT RetVT;
1140 if (RetTy->isVoidTy())
1141 RetVT = MVT::isVoid;
1142 else if (!isTypeLegal(RetTy, RetVT))
1143 return false;
1144
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001145 // For now we're using BLX etc on the assumption that we have v5t ops.
1146 if (!Subtarget->hasV5TOps()) return false;
1147
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001148 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001149 SmallVector<Value*, 8> Args;
1150 SmallVector<unsigned, 8> ArgRegs;
1151 SmallVector<EVT, 8> ArgVTs;
1152 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1153 Args.reserve(I->getNumOperands());
1154 ArgRegs.reserve(I->getNumOperands());
1155 ArgVTs.reserve(I->getNumOperands());
1156 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001157 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001158 Value *Op = I->getOperand(i);
1159 unsigned Arg = getRegForValue(Op);
1160 if (Arg == 0) return false;
1161
1162 const Type *ArgTy = Op->getType();
1163 EVT ArgVT;
1164 if (!isTypeLegal(ArgTy, ArgVT)) return false;
1165
1166 ISD::ArgFlagsTy Flags;
1167 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1168 Flags.setOrigAlign(OriginalAlignment);
1169
1170 Args.push_back(Op);
1171 ArgRegs.push_back(Arg);
1172 ArgVTs.push_back(ArgVT);
1173 ArgFlags.push_back(Flags);
1174 }
1175
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001176 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001177 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001178 unsigned NumBytes;
1179 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1180 return false;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001181
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001182 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1183 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001184 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001185 unsigned CallOpc;
1186 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001187 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001188 else
1189 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001190 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001191 .addExternalSymbol(TLI.getLibcallName(Call));
1192
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001193 // Add implicit physical register uses to the call.
1194 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1195 MIB.addReg(RegArgs[i]);
1196
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001197 // Finish off the call including any return values.
1198 SmallVector<unsigned, 4> UsedRegs;
1199 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001200
1201 // Set all unused physreg defs as dead.
1202 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001203
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001204 return true;
1205}
1206
Eric Christopher43b62be2010-09-27 06:02:23 +00001207bool ARMFastISel::SelectSDiv(const Instruction *I) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001208 EVT VT;
1209 const Type *Ty = I->getType();
1210 if (!isTypeLegal(Ty, VT))
1211 return false;
Eric Christopher1127c722010-09-27 06:08:12 +00001212
1213 // If we have integer div support we should have selected this automagically.
1214 // In case we have a real miss go ahead and return false and we'll pick
1215 // it up later.
1216 if (Subtarget->hasDivide()) return false;
1217
1218 // Otherwise emit a libcall.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001219 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1220 if (VT == MVT::i16)
1221 LC = RTLIB::SDIV_I16;
1222 else if (VT == MVT::i32)
1223 LC = RTLIB::SDIV_I32;
1224 else if (VT == MVT::i64)
1225 LC = RTLIB::SDIV_I64;
1226 else if (VT == MVT::i128)
1227 LC = RTLIB::SDIV_I128;
1228 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001229
1230 return ARMEmitLibcall(I, LC);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001231}
1232
Eric Christopher56d2b722010-09-02 23:43:26 +00001233// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001234bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001235 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001236 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001237
Eric Christopherab695882010-07-21 22:26:11 +00001238 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001239 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001240 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001241 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001242 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001243 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001244 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001245 case Instruction::ICmp:
1246 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001247 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001248 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001249 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001250 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001251 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001252 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001253 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001254 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001255 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001256 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001257 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001258 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001259 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001260 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001261 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001262 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001263 return SelectSDiv(I);
Eric Christopherab695882010-07-21 22:26:11 +00001264 default: break;
1265 }
1266 return false;
1267}
1268
1269namespace llvm {
1270 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +00001271 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001272 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001273 }
1274}