blob: cd56805ad6857436f95c237a01e17f342a09bf96 [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"
36#include "llvm/CodeGen/MachineRegisterInfo.h"
37#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000038#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/Support/ErrorHandling.h"
40#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000041#include "llvm/Target/TargetData.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetLowering.h"
44#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000045#include "llvm/Target/TargetOptions.h"
46using namespace llvm;
47
Eric Christopher038fea52010-08-17 00:46:57 +000048static cl::opt<bool>
Eric Christopher8ff9a9d2010-10-11 20:26:21 +000049EnableARMFastISel("arm-fast-isel",
50 cl::desc("Turn on experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000051 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000052
Eric Christopherab695882010-07-21 22:26:11 +000053namespace {
54
55class ARMFastISel : public FastISel {
56
57 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
58 /// make the right decision when generating code for different targets.
59 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000060 const TargetMachine &TM;
61 const TargetInstrInfo &TII;
62 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000063 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000064
Eric Christopher8cf6c602010-09-29 22:24:45 +000065 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000066 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000067 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000068
Eric Christopherab695882010-07-21 22:26:11 +000069 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000070 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000071 : FastISel(funcInfo),
72 TM(funcInfo.MF->getTarget()),
73 TII(*TM.getInstrInfo()),
74 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000075 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000076 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000077 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000078 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000079 }
80
Eric Christophercb592292010-08-20 00:20:31 +000081 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000082 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
83 const TargetRegisterClass *RC);
84 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
85 const TargetRegisterClass *RC,
86 unsigned Op0, bool Op0IsKill);
87 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
88 const TargetRegisterClass *RC,
89 unsigned Op0, bool Op0IsKill,
90 unsigned Op1, bool Op1IsKill);
91 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
92 const TargetRegisterClass *RC,
93 unsigned Op0, bool Op0IsKill,
94 uint64_t Imm);
95 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
96 const TargetRegisterClass *RC,
97 unsigned Op0, bool Op0IsKill,
98 const ConstantFP *FPImm);
99 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
100 const TargetRegisterClass *RC,
101 uint64_t Imm);
102 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
103 const TargetRegisterClass *RC,
104 unsigned Op0, bool Op0IsKill,
105 unsigned Op1, bool Op1IsKill,
106 uint64_t Imm);
107 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
108 unsigned Op0, bool Op0IsKill,
109 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000110
Eric Christophercb592292010-08-20 00:20:31 +0000111 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000112 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000113 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000114 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000115
116 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000117
Eric Christopher83007122010-08-23 21:44:12 +0000118 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000119 private:
Eric Christopher43b62be2010-09-27 06:02:23 +0000120 virtual bool SelectLoad(const Instruction *I);
121 virtual bool SelectStore(const Instruction *I);
122 virtual bool SelectBranch(const Instruction *I);
123 virtual bool SelectCmp(const Instruction *I);
124 virtual bool SelectFPExt(const Instruction *I);
125 virtual bool SelectFPTrunc(const Instruction *I);
126 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
127 virtual bool SelectSIToFP(const Instruction *I);
128 virtual bool SelectFPToSI(const Instruction *I);
129 virtual bool SelectSDiv(const Instruction *I);
Eric Christopher6a880d62010-10-11 08:37:26 +0000130 virtual bool SelectSRem(const Instruction *I);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000131 virtual bool SelectCall(const Instruction *I);
Eric Christopher3bbd3962010-10-11 08:27:59 +0000132 virtual bool SelectSelect(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000133
Eric Christopher83007122010-08-23 21:44:12 +0000134 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000135 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000136 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000137 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000138 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000139 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000140 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher2896df82010-10-15 18:02:07 +0000141 void ARMSimplifyRegOffset(unsigned &Reg, int &Offset, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000142 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000143 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000144 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000145 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000146 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000147
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000148 // Call handling routines.
149 private:
150 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000151 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000152 SmallVectorImpl<unsigned> &ArgRegs,
153 SmallVectorImpl<EVT> &ArgVTs,
154 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
155 SmallVectorImpl<unsigned> &RegArgs,
156 CallingConv::ID CC,
157 unsigned &NumBytes);
158 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
159 const Instruction *I, CallingConv::ID CC,
160 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000161 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000162
163 // OptionalDef handling routines.
164 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000165 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
166 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
167};
Eric Christopherab695882010-07-21 22:26:11 +0000168
169} // end anonymous namespace
170
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000171#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000172
Eric Christopher456144e2010-08-19 00:37:05 +0000173// DefinesOptionalPredicate - This is different from DefinesPredicate in that
174// we don't care about implicit defs here, just places we'll need to add a
175// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
176bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
177 const TargetInstrDesc &TID = MI->getDesc();
178 if (!TID.hasOptionalDef())
179 return false;
180
181 // Look to see if our OptionalDef is defining CPSR or CCR.
182 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
183 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000184 if (!MO.isReg() || !MO.isDef()) continue;
185 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000186 *CPSR = true;
187 }
188 return true;
189}
190
191// If the machine is predicable go ahead and add the predicate operands, if
192// it needs default CC operands add those.
193const MachineInstrBuilder &
194ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
195 MachineInstr *MI = &*MIB;
196
197 // Do we use a predicate?
198 if (TII.isPredicable(MI))
199 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000200
Eric Christopher456144e2010-08-19 00:37:05 +0000201 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
202 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000203 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000204 if (DefinesOptionalPredicate(MI, &CPSR)) {
205 if (CPSR)
206 AddDefaultT1CC(MIB);
207 else
208 AddDefaultCC(MIB);
209 }
210 return MIB;
211}
212
Eric Christopher0fe7d542010-08-17 01:25:29 +0000213unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
214 const TargetRegisterClass* RC) {
215 unsigned ResultReg = createResultReg(RC);
216 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
217
Eric Christopher456144e2010-08-19 00:37:05 +0000218 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000219 return ResultReg;
220}
221
222unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
223 const TargetRegisterClass *RC,
224 unsigned Op0, bool Op0IsKill) {
225 unsigned ResultReg = createResultReg(RC);
226 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
227
228 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000229 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000230 .addReg(Op0, Op0IsKill * RegState::Kill));
231 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000232 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000233 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000234 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000235 TII.get(TargetOpcode::COPY), ResultReg)
236 .addReg(II.ImplicitDefs[0]));
237 }
238 return ResultReg;
239}
240
241unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
242 const TargetRegisterClass *RC,
243 unsigned Op0, bool Op0IsKill,
244 unsigned Op1, bool Op1IsKill) {
245 unsigned ResultReg = createResultReg(RC);
246 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
247
248 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000249 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000250 .addReg(Op0, Op0IsKill * RegState::Kill)
251 .addReg(Op1, Op1IsKill * RegState::Kill));
252 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000253 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000254 .addReg(Op0, Op0IsKill * RegState::Kill)
255 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000256 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000257 TII.get(TargetOpcode::COPY), ResultReg)
258 .addReg(II.ImplicitDefs[0]));
259 }
260 return ResultReg;
261}
262
263unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
264 const TargetRegisterClass *RC,
265 unsigned Op0, bool Op0IsKill,
266 uint64_t Imm) {
267 unsigned ResultReg = createResultReg(RC);
268 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
269
270 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000271 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000272 .addReg(Op0, Op0IsKill * RegState::Kill)
273 .addImm(Imm));
274 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000275 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000276 .addReg(Op0, Op0IsKill * RegState::Kill)
277 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000278 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000279 TII.get(TargetOpcode::COPY), ResultReg)
280 .addReg(II.ImplicitDefs[0]));
281 }
282 return ResultReg;
283}
284
285unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
286 const TargetRegisterClass *RC,
287 unsigned Op0, bool Op0IsKill,
288 const ConstantFP *FPImm) {
289 unsigned ResultReg = createResultReg(RC);
290 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
291
292 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000293 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000294 .addReg(Op0, Op0IsKill * RegState::Kill)
295 .addFPImm(FPImm));
296 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000298 .addReg(Op0, Op0IsKill * RegState::Kill)
299 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000300 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000301 TII.get(TargetOpcode::COPY), ResultReg)
302 .addReg(II.ImplicitDefs[0]));
303 }
304 return ResultReg;
305}
306
307unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
308 const TargetRegisterClass *RC,
309 unsigned Op0, bool Op0IsKill,
310 unsigned Op1, bool Op1IsKill,
311 uint64_t Imm) {
312 unsigned ResultReg = createResultReg(RC);
313 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
314
315 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000316 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000317 .addReg(Op0, Op0IsKill * RegState::Kill)
318 .addReg(Op1, Op1IsKill * RegState::Kill)
319 .addImm(Imm));
320 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000321 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000322 .addReg(Op0, Op0IsKill * RegState::Kill)
323 .addReg(Op1, Op1IsKill * RegState::Kill)
324 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000325 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000326 TII.get(TargetOpcode::COPY), ResultReg)
327 .addReg(II.ImplicitDefs[0]));
328 }
329 return ResultReg;
330}
331
332unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
333 const TargetRegisterClass *RC,
334 uint64_t Imm) {
335 unsigned ResultReg = createResultReg(RC);
336 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000337
Eric Christopher0fe7d542010-08-17 01:25:29 +0000338 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000339 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000340 .addImm(Imm));
341 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000343 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000344 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000345 TII.get(TargetOpcode::COPY), ResultReg)
346 .addReg(II.ImplicitDefs[0]));
347 }
348 return ResultReg;
349}
350
351unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
352 unsigned Op0, bool Op0IsKill,
353 uint32_t Idx) {
354 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
355 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
356 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000357 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000358 DL, TII.get(TargetOpcode::COPY), ResultReg)
359 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
360 return ResultReg;
361}
362
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000363// TODO: Don't worry about 64-bit now, but when this is fixed remove the
364// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000365unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000366 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000367
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000368 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
369 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
370 TII.get(ARM::VMOVRS), MoveReg)
371 .addReg(SrcReg));
372 return MoveReg;
373}
374
375unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000376 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000377
Eric Christopheraa3ace12010-09-09 20:49:25 +0000378 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
379 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000380 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000381 .addReg(SrcReg));
382 return MoveReg;
383}
384
Eric Christopher9ed58df2010-09-09 00:19:41 +0000385// For double width floating point we need to materialize two constants
386// (the high and the low) into integer registers then use a move to get
387// the combined constant into an FP reg.
388unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
389 const APFloat Val = CFP->getValueAPF();
390 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000391
Eric Christopher9ed58df2010-09-09 00:19:41 +0000392 // This checks to see if we can use VFP3 instructions to materialize
393 // a constant, otherwise we have to go through the constant pool.
394 if (TLI.isFPImmLegal(Val, VT)) {
395 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
396 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
398 DestReg)
399 .addFPImm(CFP));
400 return DestReg;
401 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000402
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000403 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000404 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000405
Eric Christopher238bb162010-09-09 23:50:00 +0000406 // MachineConstantPool wants an explicit alignment.
407 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
408 if (Align == 0) {
409 // TODO: Figure out if this is correct.
410 Align = TD.getTypeAllocSize(CFP->getType());
411 }
412 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
413 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
414 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000415
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000416 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000417 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
418 DestReg)
419 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000420 .addReg(0));
421 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000422}
423
Eric Christopher744c7c82010-09-28 22:47:54 +0000424unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000425
Eric Christopher744c7c82010-09-28 22:47:54 +0000426 // For now 32-bit only.
427 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000428
Eric Christopher56d2b722010-09-02 23:43:26 +0000429 // MachineConstantPool wants an explicit alignment.
430 unsigned Align = TD.getPrefTypeAlignment(C->getType());
431 if (Align == 0) {
432 // TODO: Figure out if this is correct.
433 Align = TD.getTypeAllocSize(C->getType());
434 }
435 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000436 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000437
Eric Christopher56d2b722010-09-02 23:43:26 +0000438 if (isThumb)
439 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000440 TII.get(ARM::t2LDRpci), DestReg)
441 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000442 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000443 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000445 TII.get(ARM::LDRcp), DestReg)
446 .addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000447 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000448
Eric Christopher56d2b722010-09-02 23:43:26 +0000449 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000450}
451
Eric Christopherc9932f62010-10-01 23:24:42 +0000452unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000453 // For now 32-bit only.
454 if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000455
Eric Christopher890dbbe2010-10-02 00:32:44 +0000456 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000457
Eric Christopher890dbbe2010-10-02 00:32:44 +0000458 // TODO: No external globals for now.
459 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000460
Eric Christopher890dbbe2010-10-02 00:32:44 +0000461 // TODO: Need more magic for ARM PIC.
462 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000463
Eric Christopher890dbbe2010-10-02 00:32:44 +0000464 // MachineConstantPool wants an explicit alignment.
465 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
466 if (Align == 0) {
467 // TODO: Figure out if this is correct.
468 Align = TD.getTypeAllocSize(GV->getType());
469 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000470
Eric Christopher890dbbe2010-10-02 00:32:44 +0000471 // Grab index.
472 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
473 unsigned Id = AFI->createConstPoolEntryUId();
474 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
475 ARMCP::CPValue, PCAdj);
476 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000477
Eric Christopher890dbbe2010-10-02 00:32:44 +0000478 // Load value.
479 MachineInstrBuilder MIB;
480 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
481 if (isThumb) {
482 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
483 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
484 .addConstantPoolIndex(Idx);
485 if (RelocM == Reloc::PIC_)
486 MIB.addImm(Id);
487 } else {
488 // The extra reg and immediate are for addrmode2.
489 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
490 DestReg)
491 .addConstantPoolIndex(Idx)
492 .addReg(0).addImm(0);
493 }
494 AddOptionalDefs(MIB);
495 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000496}
497
Eric Christopher9ed58df2010-09-09 00:19:41 +0000498unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
499 EVT VT = TLI.getValueType(C->getType(), true);
500
501 // Only handle simple types.
502 if (!VT.isSimple()) return 0;
503
504 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
505 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000506 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
507 return ARMMaterializeGV(GV, VT);
508 else if (isa<ConstantInt>(C))
509 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000510
Eric Christopherc9932f62010-10-01 23:24:42 +0000511 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000512}
513
Eric Christopherf9764fa2010-09-30 20:49:44 +0000514unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
515 // Don't handle dynamic allocas.
516 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000517
Eric Christopherf9764fa2010-09-30 20:49:44 +0000518 EVT VT;
519 if (!isTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000520
Eric Christopherf9764fa2010-09-30 20:49:44 +0000521 DenseMap<const AllocaInst*, int>::iterator SI =
522 FuncInfo.StaticAllocaMap.find(AI);
523
524 // This will get lowered later into the correct offsets and registers
525 // via rewriteXFrameIndex.
526 if (SI != FuncInfo.StaticAllocaMap.end()) {
527 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
528 unsigned ResultReg = createResultReg(RC);
529 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
530 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
531 TII.get(Opc), ResultReg)
532 .addFrameIndex(SI->second)
533 .addImm(0));
534 return ResultReg;
535 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000536
Eric Christopherf9764fa2010-09-30 20:49:44 +0000537 return 0;
538}
539
Eric Christopherb1cc8482010-08-25 07:23:49 +0000540bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
541 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000542
Eric Christopherb1cc8482010-08-25 07:23:49 +0000543 // Only handle simple types.
544 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000545
Eric Christopherdc908042010-08-31 01:28:42 +0000546 // Handle all legal types, i.e. a register that will directly hold this
547 // value.
548 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000549}
550
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000551bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
552 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000553
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000554 // If this is a type than can be sign or zero-extended to a basic operation
555 // go ahead and accept it now.
556 if (VT == MVT::i8 || VT == MVT::i16)
557 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000558
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000559 return false;
560}
561
Eric Christophercb0b04b2010-08-24 00:07:24 +0000562// Computes the Reg+Offset to get to an object.
563bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000564 int &Offset) {
565 // Some boilerplate from the X86 FastISel.
566 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000567 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000568 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000569 // Don't walk into other basic blocks; it's possible we haven't
570 // visited them yet, so the instructions may not yet be assigned
571 // virtual registers.
572 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
573 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000574 Opcode = I->getOpcode();
575 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000576 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000577 Opcode = C->getOpcode();
578 U = C;
579 }
580
Eric Christophercb0b04b2010-08-24 00:07:24 +0000581 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000582 if (Ty->getAddressSpace() > 255)
583 // Fast instruction selection doesn't support the special
584 // address spaces.
585 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000586
Eric Christopher83007122010-08-23 21:44:12 +0000587 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000588 default:
Eric Christopher83007122010-08-23 21:44:12 +0000589 break;
Eric Christopher55324332010-10-12 00:43:21 +0000590 case Instruction::BitCast: {
591 // Look through bitcasts.
592 return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
593 }
594 case Instruction::IntToPtr: {
595 // Look past no-op inttoptrs.
596 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
597 return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
598 break;
599 }
600 case Instruction::PtrToInt: {
601 // Look past no-op ptrtoints.
602 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
603 return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
604 break;
605 }
Eric Christophereae84392010-10-14 09:29:41 +0000606 case Instruction::GetElementPtr: {
607 int SavedOffset = Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000608 unsigned SavedReg = Reg;
Eric Christophereae84392010-10-14 09:29:41 +0000609 int TmpOffset = Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000610
Eric Christophereae84392010-10-14 09:29:41 +0000611 // Iterate through the GEP folding the constants into offsets where
612 // we can.
613 gep_type_iterator GTI = gep_type_begin(U);
614 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
615 i != e; ++i, ++GTI) {
616 const Value *Op = *i;
617 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
618 const StructLayout *SL = TD.getStructLayout(STy);
619 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
620 TmpOffset += SL->getElementOffset(Idx);
621 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000622 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
623 SmallVector<const Value *, 4> Worklist;
624 Worklist.push_back(Op);
625 do {
626 Op = Worklist.pop_back_val();
627 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
628 // Constant-offset addressing.
629 TmpOffset += CI->getSExtValue() * S;
630 } else if (0 && isa<AddOperator>(Op) &&
631 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
632 // An add with a constant operand. Fold the constant.
633 ConstantInt *CI =
634 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
635 TmpOffset += CI->getSExtValue() * S;
636 // Add the other operand back to the work list.
637 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
638 } else
639 goto unsupported_gep;
640 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000641 }
642 }
Eric Christopher2896df82010-10-15 18:02:07 +0000643
644 // Try to grab the base operand now.
Eric Christophereae84392010-10-14 09:29:41 +0000645 Offset = TmpOffset;
646 if (ARMComputeRegOffset(U->getOperand(0), Reg, Offset)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000647
648 // We failed, restore everything and try the other options.
Eric Christophereae84392010-10-14 09:29:41 +0000649 Offset = SavedOffset;
Eric Christopher2896df82010-10-15 18:02:07 +0000650 Reg = SavedReg;
651
Eric Christophereae84392010-10-14 09:29:41 +0000652 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000653 break;
654 }
Eric Christopher83007122010-08-23 21:44:12 +0000655 case Instruction::Alloca: {
Eric Christophereae84392010-10-14 09:29:41 +0000656 // TODO: Fix this to do intermediate loads, etc.
657 if (Offset != 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000658
Eric Christopher15418772010-10-12 05:39:06 +0000659 const AllocaInst *AI = cast<AllocaInst>(Obj);
660 DenseMap<const AllocaInst*, int>::iterator SI =
661 FuncInfo.StaticAllocaMap.find(AI);
662 if (SI != FuncInfo.StaticAllocaMap.end()) {
663 Reg = ARM::SP;
664 Offset = SI->second;
665 return true;
666 }
Eric Christopher050d16c2010-10-11 21:37:35 +0000667 // Don't handle dynamic allocas.
Eric Christopher5f9e8b92010-10-11 22:01:22 +0000668 assert(!FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Obj)) &&
669 "Alloca should have been handled earlier!");
Eric Christopherf06f3092010-08-24 00:50:47 +0000670 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000671 }
672 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000673
Eric Christophera9c57512010-10-13 21:41:51 +0000674 // Materialize the global variable's address into a reg which can
675 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000676 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000677 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
678 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000679
Eric Christopherede42b02010-10-13 09:11:46 +0000680 Reg = Tmp;
681 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000682 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000683
Eric Christophercb0b04b2010-08-24 00:07:24 +0000684 // Try to get this in a register if nothing else has worked.
Eric Christophereae84392010-10-14 09:29:41 +0000685 if (Reg == 0) Reg = getRegForValue(Obj);
Eric Christophereae84392010-10-14 09:29:41 +0000686 return Reg != 0;
687}
688
Eric Christopher2896df82010-10-15 18:02:07 +0000689void ARMFastISel::ARMSimplifyRegOffset(unsigned &Reg, int &Offset, EVT VT) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000690
691 // Since the offset may be too large for the load instruction
692 // get the reg+offset into a register.
Eric Christophereae84392010-10-14 09:29:41 +0000693 if (Reg != ARM::SP && Offset != 0) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000694 ARMCC::CondCodes Pred = ARMCC::AL;
695 unsigned PredReg = 0;
696
Eric Christopher2896df82010-10-15 18:02:07 +0000697 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
698 ARM::GPRRegisterClass;
699 unsigned BaseReg = createResultReg(RC);
700
Eric Christophereaa204b2010-09-02 01:39:14 +0000701 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000702 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher2896df82010-10-15 18:02:07 +0000703 BaseReg, Reg, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000704 static_cast<const ARMBaseInstrInfo&>(TII));
705 else {
706 assert(AFI->isThumb2Function());
707 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher2896df82010-10-15 18:02:07 +0000708 BaseReg, Reg, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000709 static_cast<const ARMBaseInstrInfo&>(TII));
710 }
Eric Christophereae84392010-10-14 09:29:41 +0000711 Offset = 0;
Eric Christopher2896df82010-10-15 18:02:07 +0000712 Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000713 }
Eric Christopher83007122010-08-23 21:44:12 +0000714}
715
Eric Christopherb1cc8482010-08-25 07:23:49 +0000716bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
717 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000718
Eric Christopherb1cc8482010-08-25 07:23:49 +0000719 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000720 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000721 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000722 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000723 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000724 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000725 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000726 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000727 case MVT::i16:
Eric Christopher7a56f332010-10-08 01:13:17 +0000728 Opc = isThumb ? ARM::t2LDRHi8 : ARM::LDRH;
729 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000730 VT = MVT::i32;
731 break;
732 case MVT::i8:
Eric Christopher7a56f332010-10-08 01:13:17 +0000733 Opc = isThumb ? ARM::t2LDRBi8 : ARM::LDRB;
734 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000735 VT = MVT::i32;
736 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000737 case MVT::i32:
Eric Christopher7a56f332010-10-08 01:13:17 +0000738 Opc = isThumb ? ARM::t2LDRi8 : ARM::LDR;
739 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000740 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000741 case MVT::f32:
742 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000743 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000744 isFloat = true;
745 break;
746 case MVT::f64:
747 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000748 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000749 isFloat = true;
750 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000751 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000752
Eric Christopheree56ea62010-10-07 05:50:44 +0000753 ResultReg = createResultReg(RC);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000754
Eric Christopher7a56f332010-10-08 01:13:17 +0000755 // For now with the additions above the offset should be zero - thus we
756 // can always fit into an i8.
Eric Christopher15418772010-10-12 05:39:06 +0000757 assert((Reg == ARM::SP || Offset == 0) &&
758 "Offset not zero and not a stack load!");
Eric Christopherdccd2c32010-10-11 08:38:55 +0000759
Eric Christopher15418772010-10-12 05:39:06 +0000760 if (Reg == ARM::SP)
761 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
762 ResultReg, Offset, RC,
763 TM.getRegisterInfo());
Eric Christopher7a56f332010-10-08 01:13:17 +0000764 // The thumb and floating point instructions both take 2 operands, ARM takes
765 // another register.
Eric Christopher15418772010-10-12 05:39:06 +0000766 else if (isFloat || isThumb)
Eric Christopher6dab1372010-09-18 01:59:37 +0000767 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
768 TII.get(Opc), ResultReg)
769 .addReg(Reg).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000770 else
771 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
772 TII.get(Opc), ResultReg)
773 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000774 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000775}
776
Eric Christopher43b62be2010-09-27 06:02:23 +0000777bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000778 // Verify we have a legal type before going any further.
779 EVT VT;
780 if (!isLoadTypeLegal(I->getType(), VT))
781 return false;
782
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000783 // Our register and offset with innocuous defaults.
784 unsigned Reg = 0;
785 int Offset = 0;
786
787 // See if we can handle this as Reg + Offset
788 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
789 return false;
790
Eric Christopher2896df82010-10-15 18:02:07 +0000791 ARMSimplifyRegOffset(Reg, Offset, VT);
Eric Christophereae84392010-10-14 09:29:41 +0000792
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000793 unsigned ResultReg;
Eric Christopher2896df82010-10-15 18:02:07 +0000794 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000795
796 UpdateValueMap(I, ResultReg);
797 return true;
798}
799
Eric Christopher318b6ee2010-09-02 00:53:56 +0000800bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
801 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000802 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000803 bool isFloat = false;
Eric Christopher15418772010-10-12 05:39:06 +0000804 // VT is set here only for use in the alloca stores below - those are promoted
805 // to reg size always.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000806 switch (VT.getSimpleVT().SimpleTy) {
807 default: return false;
808 case MVT::i1:
Eric Christopher2896df82010-10-15 18:02:07 +0000809 case MVT::i8:
Eric Christopher15418772010-10-12 05:39:06 +0000810 VT = MVT::i32;
811 StrOpc = isThumb ? ARM::t2STRBi8 : ARM::STRB;
812 break;
813 case MVT::i16:
814 VT = MVT::i32;
815 StrOpc = isThumb ? ARM::t2STRHi8 : ARM::STRH;
816 break;
Eric Christophere93417b2010-10-08 23:52:16 +0000817 case MVT::i32: StrOpc = isThumb ? ARM::t2STRi8 : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000818 case MVT::f32:
819 if (!Subtarget->hasVFP2()) return false;
820 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000821 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000822 break;
823 case MVT::f64:
824 if (!Subtarget->hasVFP2()) return false;
825 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000826 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000827 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000828 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000829
Eric Christopher558cf002010-10-12 21:23:43 +0000830 if (DstReg == ARM::SP)
Eric Christopher15418772010-10-12 05:39:06 +0000831 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
832 SrcReg, true /*isKill*/, Offset,
833 TLI.getRegClassFor(VT), TM.getRegisterInfo());
Eric Christopherb74558a2010-09-18 01:23:38 +0000834 // The thumb addressing mode has operands swapped from the arm addressing
835 // mode, the floating point one only has two operands.
Eric Christophere93417b2010-10-08 23:52:16 +0000836 if (isFloat || isThumb)
Eric Christopherb74558a2010-09-18 01:23:38 +0000837 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000838 TII.get(StrOpc))
839 .addReg(SrcReg).addReg(DstReg).addImm(Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000840 else
841 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000842 TII.get(StrOpc))
843 .addReg(SrcReg).addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000844
Eric Christopher318b6ee2010-09-02 00:53:56 +0000845 return true;
846}
847
Eric Christopher43b62be2010-09-27 06:02:23 +0000848bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000849 Value *Op0 = I->getOperand(0);
850 unsigned SrcReg = 0;
851
Eric Christopher543cf052010-09-01 22:16:27 +0000852 // Yay type legalization
853 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000854 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000855 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000856
Eric Christopher1b61ef42010-09-02 01:48:11 +0000857 // Get the value to be stored into a register.
858 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000859 if (SrcReg == 0)
860 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000861
Eric Christopher318b6ee2010-09-02 00:53:56 +0000862 // Our register and offset with innocuous defaults.
863 unsigned Reg = 0;
864 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000865
Eric Christopher318b6ee2010-09-02 00:53:56 +0000866 // See if we can handle this as Reg + Offset
867 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
868 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000869
Eric Christopher2896df82010-10-15 18:02:07 +0000870 ARMSimplifyRegOffset(Reg, Offset, VT);
Eric Christophereae84392010-10-14 09:29:41 +0000871
Eric Christopher2896df82010-10-15 18:02:07 +0000872 if (!ARMEmitStore(VT, SrcReg, Reg, Offset)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000873
Eric Christophera5b1e682010-09-17 22:28:18 +0000874 return true;
875}
876
877static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
878 switch (Pred) {
879 // Needs two compares...
880 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000881 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000882 default:
883 assert(false && "Unhandled CmpInst::Predicate!");
884 return ARMCC::AL;
885 case CmpInst::ICMP_EQ:
886 case CmpInst::FCMP_OEQ:
887 return ARMCC::EQ;
888 case CmpInst::ICMP_SGT:
889 case CmpInst::FCMP_OGT:
890 return ARMCC::GT;
891 case CmpInst::ICMP_SGE:
892 case CmpInst::FCMP_OGE:
893 return ARMCC::GE;
894 case CmpInst::ICMP_UGT:
895 case CmpInst::FCMP_UGT:
896 return ARMCC::HI;
897 case CmpInst::FCMP_OLT:
898 return ARMCC::MI;
899 case CmpInst::ICMP_ULE:
900 case CmpInst::FCMP_OLE:
901 return ARMCC::LS;
902 case CmpInst::FCMP_ORD:
903 return ARMCC::VC;
904 case CmpInst::FCMP_UNO:
905 return ARMCC::VS;
906 case CmpInst::FCMP_UGE:
907 return ARMCC::PL;
908 case CmpInst::ICMP_SLT:
909 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000910 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000911 case CmpInst::ICMP_SLE:
912 case CmpInst::FCMP_ULE:
913 return ARMCC::LE;
914 case CmpInst::FCMP_UNE:
915 case CmpInst::ICMP_NE:
916 return ARMCC::NE;
917 case CmpInst::ICMP_UGE:
918 return ARMCC::HS;
919 case CmpInst::ICMP_ULT:
920 return ARMCC::LO;
921 }
Eric Christopher543cf052010-09-01 22:16:27 +0000922}
923
Eric Christopher43b62be2010-09-27 06:02:23 +0000924bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000925 const BranchInst *BI = cast<BranchInst>(I);
926 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
927 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000928
Eric Christophere5734102010-09-03 00:35:47 +0000929 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000930 // TODO: Try to avoid the re-computation in some places.
931 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000932 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000933
Eric Christopher229207a2010-09-29 01:14:47 +0000934 // Re-set the flags just in case.
935 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
936 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
937 .addReg(CondReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000938
Eric Christophere5734102010-09-03 00:35:47 +0000939 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000940 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000941 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000942 FastEmitBranch(FBB, DL);
943 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000944 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000945}
946
Eric Christopher43b62be2010-09-27 06:02:23 +0000947bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000948 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000949
Eric Christopherd43393a2010-09-08 23:13:45 +0000950 EVT VT;
951 const Type *Ty = CI->getOperand(0)->getType();
952 if (!isTypeLegal(Ty, VT))
953 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000954
Eric Christopherd43393a2010-09-08 23:13:45 +0000955 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
956 if (isFloat && !Subtarget->hasVFP2())
957 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000958
Eric Christopherd43393a2010-09-08 23:13:45 +0000959 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000960 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +0000961 switch (VT.getSimpleVT().SimpleTy) {
962 default: return false;
963 // TODO: Verify compares.
964 case MVT::f32:
965 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +0000966 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000967 break;
968 case MVT::f64:
969 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +0000970 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000971 break;
972 case MVT::i32:
973 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +0000974 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000975 break;
976 }
977
Eric Christopher229207a2010-09-29 01:14:47 +0000978 // Get the compare predicate.
979 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +0000980
Eric Christopher229207a2010-09-29 01:14:47 +0000981 // We may not handle every CC for now.
982 if (ARMPred == ARMCC::AL) return false;
983
Eric Christopherd43393a2010-09-08 23:13:45 +0000984 unsigned Arg1 = getRegForValue(CI->getOperand(0));
985 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000986
Eric Christopherd43393a2010-09-08 23:13:45 +0000987 unsigned Arg2 = getRegForValue(CI->getOperand(1));
988 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000989
Eric Christopherd43393a2010-09-08 23:13:45 +0000990 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
991 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000992
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000993 // For floating point we need to move the result to a comparison register
994 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000995 if (isFloat)
996 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
997 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000998
Eric Christopher229207a2010-09-29 01:14:47 +0000999 // Now set a register based on the comparison. Explicitly set the predicates
1000 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001001 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001002 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001003 : ARM::GPRRegisterClass;
1004 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001005 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001006 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001007 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1008 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1009 .addReg(ZeroReg).addImm(1)
1010 .addImm(ARMPred).addReg(CondReg);
1011
Eric Christophera5b1e682010-09-17 22:28:18 +00001012 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001013 return true;
1014}
1015
Eric Christopher43b62be2010-09-27 06:02:23 +00001016bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001017 // Make sure we have VFP and that we're extending float to double.
1018 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001019
Eric Christopher46203602010-09-09 00:26:48 +00001020 Value *V = I->getOperand(0);
1021 if (!I->getType()->isDoubleTy() ||
1022 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001023
Eric Christopher46203602010-09-09 00:26:48 +00001024 unsigned Op = getRegForValue(V);
1025 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001026
Eric Christopher46203602010-09-09 00:26:48 +00001027 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001028 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001029 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001030 .addReg(Op));
1031 UpdateValueMap(I, Result);
1032 return true;
1033}
1034
Eric Christopher43b62be2010-09-27 06:02:23 +00001035bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001036 // Make sure we have VFP and that we're truncating double to float.
1037 if (!Subtarget->hasVFP2()) return false;
1038
1039 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001040 if (!(I->getType()->isFloatTy() &&
1041 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001042
1043 unsigned Op = getRegForValue(V);
1044 if (Op == 0) return false;
1045
1046 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001047 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001048 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001049 .addReg(Op));
1050 UpdateValueMap(I, Result);
1051 return true;
1052}
1053
Eric Christopher43b62be2010-09-27 06:02:23 +00001054bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001055 // Make sure we have VFP.
1056 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001057
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001058 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001059 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001060 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001061 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001062
Eric Christopher9a040492010-09-09 18:54:59 +00001063 unsigned Op = getRegForValue(I->getOperand(0));
1064 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001065
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001066 // The conversion routine works on fp-reg to fp-reg and the operand above
1067 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001068 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001069 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001070
Eric Christopher9a040492010-09-09 18:54:59 +00001071 unsigned Opc;
1072 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1073 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1074 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001075
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001076 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001077 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1078 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001079 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001080 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001081 return true;
1082}
1083
Eric Christopher43b62be2010-09-27 06:02:23 +00001084bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001085 // Make sure we have VFP.
1086 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001087
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001088 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001089 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001090 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001091 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001092
Eric Christopher9a040492010-09-09 18:54:59 +00001093 unsigned Op = getRegForValue(I->getOperand(0));
1094 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001095
Eric Christopher9a040492010-09-09 18:54:59 +00001096 unsigned Opc;
1097 const Type *OpTy = I->getOperand(0)->getType();
1098 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1099 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1100 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001101
Eric Christopher022b7fb2010-10-05 23:13:24 +00001102 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1103 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001104 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1105 ResultReg)
1106 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001107
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001108 // This result needs to be in an integer register, but the conversion only
1109 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001110 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001111 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001112
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001113 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001114 return true;
1115}
1116
Eric Christopher3bbd3962010-10-11 08:27:59 +00001117bool ARMFastISel::SelectSelect(const Instruction *I) {
1118 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1119 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1120 return false;
1121
1122 // Things need to be register sized for register moves.
1123 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1124 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1125
1126 unsigned CondReg = getRegForValue(I->getOperand(0));
1127 if (CondReg == 0) return false;
1128 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1129 if (Op1Reg == 0) return false;
1130 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1131 if (Op2Reg == 0) return false;
1132
1133 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1134 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1135 .addReg(CondReg).addImm(1));
1136 unsigned ResultReg = createResultReg(RC);
1137 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1139 .addReg(Op1Reg).addReg(Op2Reg)
1140 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1141 UpdateValueMap(I, ResultReg);
1142 return true;
1143}
1144
Eric Christopher08637852010-09-30 22:34:19 +00001145bool ARMFastISel::SelectSDiv(const Instruction *I) {
1146 EVT VT;
1147 const Type *Ty = I->getType();
1148 if (!isTypeLegal(Ty, VT))
1149 return false;
1150
1151 // If we have integer div support we should have selected this automagically.
1152 // In case we have a real miss go ahead and return false and we'll pick
1153 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001154 if (Subtarget->hasDivide()) return false;
1155
Eric Christopher08637852010-09-30 22:34:19 +00001156 // Otherwise emit a libcall.
1157 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001158 if (VT == MVT::i8)
1159 LC = RTLIB::SDIV_I8;
1160 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001161 LC = RTLIB::SDIV_I16;
1162 else if (VT == MVT::i32)
1163 LC = RTLIB::SDIV_I32;
1164 else if (VT == MVT::i64)
1165 LC = RTLIB::SDIV_I64;
1166 else if (VT == MVT::i128)
1167 LC = RTLIB::SDIV_I128;
1168 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001169
Eric Christopher08637852010-09-30 22:34:19 +00001170 return ARMEmitLibcall(I, LC);
1171}
1172
Eric Christopher6a880d62010-10-11 08:37:26 +00001173bool ARMFastISel::SelectSRem(const Instruction *I) {
1174 EVT VT;
1175 const Type *Ty = I->getType();
1176 if (!isTypeLegal(Ty, VT))
1177 return false;
1178
1179 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1180 if (VT == MVT::i8)
1181 LC = RTLIB::SREM_I8;
1182 else if (VT == MVT::i16)
1183 LC = RTLIB::SREM_I16;
1184 else if (VT == MVT::i32)
1185 LC = RTLIB::SREM_I32;
1186 else if (VT == MVT::i64)
1187 LC = RTLIB::SREM_I64;
1188 else if (VT == MVT::i128)
1189 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001190 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001191
Eric Christopher6a880d62010-10-11 08:37:26 +00001192 return ARMEmitLibcall(I, LC);
1193}
1194
Eric Christopher43b62be2010-09-27 06:02:23 +00001195bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001196 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001197
Eric Christopherbc39b822010-09-09 00:53:57 +00001198 // We can get here in the case when we want to use NEON for our fp
1199 // operations, but can't figure out how to. Just use the vfp instructions
1200 // if we have them.
1201 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001202 const Type *Ty = I->getType();
1203 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1204 if (isFloat && !Subtarget->hasVFP2())
1205 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001206
Eric Christopherbc39b822010-09-09 00:53:57 +00001207 unsigned Op1 = getRegForValue(I->getOperand(0));
1208 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001209
Eric Christopherbc39b822010-09-09 00:53:57 +00001210 unsigned Op2 = getRegForValue(I->getOperand(1));
1211 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001212
Eric Christopherbc39b822010-09-09 00:53:57 +00001213 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +00001214 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1215 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001216 switch (ISDOpcode) {
1217 default: return false;
1218 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001219 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001220 break;
1221 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001222 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001223 break;
1224 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001225 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001226 break;
1227 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001228 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001229 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1230 TII.get(Opc), ResultReg)
1231 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001232 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001233 return true;
1234}
1235
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001236// Call Handling Code
1237
1238// This is largely taken directly from CCAssignFnForNode - we don't support
1239// varargs in FastISel so that part has been removed.
1240// TODO: We may not support all of this.
1241CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1242 switch (CC) {
1243 default:
1244 llvm_unreachable("Unsupported calling convention");
1245 case CallingConv::C:
1246 case CallingConv::Fast:
1247 // Use target triple & subtarget features to do actual dispatch.
1248 if (Subtarget->isAAPCS_ABI()) {
1249 if (Subtarget->hasVFP2() &&
1250 FloatABIType == FloatABI::Hard)
1251 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1252 else
1253 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1254 } else
1255 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1256 case CallingConv::ARM_AAPCS_VFP:
1257 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1258 case CallingConv::ARM_AAPCS:
1259 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1260 case CallingConv::ARM_APCS:
1261 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1262 }
1263}
1264
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001265bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1266 SmallVectorImpl<unsigned> &ArgRegs,
1267 SmallVectorImpl<EVT> &ArgVTs,
1268 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1269 SmallVectorImpl<unsigned> &RegArgs,
1270 CallingConv::ID CC,
1271 unsigned &NumBytes) {
1272 SmallVector<CCValAssign, 16> ArgLocs;
1273 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1274 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1275
1276 // Get a count of how many bytes are to be pushed on the stack.
1277 NumBytes = CCInfo.getNextStackOffset();
1278
1279 // Issue CALLSEQ_START
1280 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001281 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1282 TII.get(AdjStackDown))
1283 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001284
1285 // Process the args.
1286 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1287 CCValAssign &VA = ArgLocs[i];
1288 unsigned Arg = ArgRegs[VA.getValNo()];
1289 EVT ArgVT = ArgVTs[VA.getValNo()];
1290
Eric Christopherf9764fa2010-09-30 20:49:44 +00001291 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001292 switch (VA.getLocInfo()) {
1293 case CCValAssign::Full: break;
1294 default:
Eric Christopher11077342010-10-07 05:14:08 +00001295 // TODO: Handle arg promotion.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001296 return false;
1297 }
1298
1299 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001300 // TODO: We need custom lowering for f64 args.
1301 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001303 VA.getLocReg())
1304 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001305 RegArgs.push_back(VA.getLocReg());
1306 } else {
1307 // Need to store
1308 return false;
1309 }
1310 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001311
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001312 return true;
1313}
1314
1315bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1316 const Instruction *I, CallingConv::ID CC,
1317 unsigned &NumBytes) {
1318 // Issue CALLSEQ_END
1319 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1321 TII.get(AdjStackUp))
1322 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001323
1324 // Now the return value.
1325 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1326 SmallVector<CCValAssign, 16> RVLocs;
1327 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1328 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1329
1330 // Copy all of the result registers out of their specified physreg.
Eric Christopher14df8822010-10-01 00:00:11 +00001331 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1332 // For this move we copy into two registers and then move into the
1333 // double fp reg we want.
1334 // TODO: Are the copies necessary?
1335 TargetRegisterClass *CopyRC = TLI.getRegClassFor(MVT::i32);
1336 unsigned Copy1 = createResultReg(CopyRC);
1337 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1338 Copy1).addReg(RVLocs[0].getLocReg());
1339 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001340
Eric Christopher14df8822010-10-01 00:00:11 +00001341 unsigned Copy2 = createResultReg(CopyRC);
1342 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1343 Copy2).addReg(RVLocs[1].getLocReg());
1344 UsedRegs.push_back(RVLocs[1].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001345
Eric Christopher14df8822010-10-01 00:00:11 +00001346 EVT DestVT = RVLocs[0].getValVT();
1347 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1348 unsigned ResultReg = createResultReg(DstRC);
1349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1350 TII.get(ARM::VMOVDRR), ResultReg)
1351 .addReg(Copy1).addReg(Copy2));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001352
1353 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001354 UpdateValueMap(I, ResultReg);
1355 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001356 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001357 EVT CopyVT = RVLocs[0].getValVT();
1358 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001359
Eric Christopher14df8822010-10-01 00:00:11 +00001360 unsigned ResultReg = createResultReg(DstRC);
1361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1362 ResultReg).addReg(RVLocs[0].getLocReg());
1363 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001364
Eric Christopherdccd2c32010-10-11 08:38:55 +00001365 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001366 UpdateValueMap(I, ResultReg);
1367 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001368 }
1369
Eric Christopherdccd2c32010-10-11 08:38:55 +00001370 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001371}
1372
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001373// A quick function that will emit a call for a named libcall in F with the
1374// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001375// can emit a call for any libcall we can produce. This is an abridged version
1376// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001377// like computed function pointers or strange arguments at call sites.
1378// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1379// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001380bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1381 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001382
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001383 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001384 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001385 EVT RetVT;
1386 if (RetTy->isVoidTy())
1387 RetVT = MVT::isVoid;
1388 else if (!isTypeLegal(RetTy, RetVT))
1389 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001390
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001391 // For now we're using BLX etc on the assumption that we have v5t ops.
1392 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001393
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001394 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001395 SmallVector<Value*, 8> Args;
1396 SmallVector<unsigned, 8> ArgRegs;
1397 SmallVector<EVT, 8> ArgVTs;
1398 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1399 Args.reserve(I->getNumOperands());
1400 ArgRegs.reserve(I->getNumOperands());
1401 ArgVTs.reserve(I->getNumOperands());
1402 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001403 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001404 Value *Op = I->getOperand(i);
1405 unsigned Arg = getRegForValue(Op);
1406 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001407
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001408 const Type *ArgTy = Op->getType();
1409 EVT ArgVT;
1410 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001411
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001412 ISD::ArgFlagsTy Flags;
1413 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1414 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001415
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001416 Args.push_back(Op);
1417 ArgRegs.push_back(Arg);
1418 ArgVTs.push_back(ArgVT);
1419 ArgFlags.push_back(Flags);
1420 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001421
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001422 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001423 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001424 unsigned NumBytes;
1425 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1426 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001427
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001428 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001429 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001430 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001431 unsigned CallOpc;
1432 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001433 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001434 else
1435 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001436 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001437 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001438
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001439 // Add implicit physical register uses to the call.
1440 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1441 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001442
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001443 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001444 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001445 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001446
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001447 // Set all unused physreg defs as dead.
1448 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001449
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001450 return true;
1451}
1452
Eric Christopherf9764fa2010-09-30 20:49:44 +00001453bool ARMFastISel::SelectCall(const Instruction *I) {
1454 const CallInst *CI = cast<CallInst>(I);
1455 const Value *Callee = CI->getCalledValue();
1456
1457 // Can't handle inline asm or worry about intrinsics yet.
1458 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1459
Eric Christophere6ca6772010-10-01 21:33:12 +00001460 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001461 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001462 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1463 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001464
Eric Christopherf9764fa2010-09-30 20:49:44 +00001465 // Check the calling convention.
1466 ImmutableCallSite CS(CI);
1467 CallingConv::ID CC = CS.getCallingConv();
1468 // TODO: Avoid some calling conventions?
1469 if (CC != CallingConv::C) {
Eric Christophere540a6f2010-10-05 23:50:58 +00001470 // errs() << "Can't handle calling convention: " << CC << "\n";
Eric Christopherf9764fa2010-09-30 20:49:44 +00001471 return false;
1472 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001473
Eric Christopherf9764fa2010-09-30 20:49:44 +00001474 // Let SDISel handle vararg functions.
1475 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1476 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1477 if (FTy->isVarArg())
1478 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001479
Eric Christopherf9764fa2010-09-30 20:49:44 +00001480 // Handle *simple* calls for now.
1481 const Type *RetTy = I->getType();
1482 EVT RetVT;
1483 if (RetTy->isVoidTy())
1484 RetVT = MVT::isVoid;
1485 else if (!isTypeLegal(RetTy, RetVT))
1486 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001487
Eric Christopherf9764fa2010-09-30 20:49:44 +00001488 // For now we're using BLX etc on the assumption that we have v5t ops.
1489 // TODO: Maybe?
1490 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001491
Eric Christopherf9764fa2010-09-30 20:49:44 +00001492 // Set up the argument vectors.
1493 SmallVector<Value*, 8> Args;
1494 SmallVector<unsigned, 8> ArgRegs;
1495 SmallVector<EVT, 8> ArgVTs;
1496 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1497 Args.reserve(CS.arg_size());
1498 ArgRegs.reserve(CS.arg_size());
1499 ArgVTs.reserve(CS.arg_size());
1500 ArgFlags.reserve(CS.arg_size());
1501 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1502 i != e; ++i) {
1503 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001504
Eric Christopherf9764fa2010-09-30 20:49:44 +00001505 if (Arg == 0)
1506 return false;
1507 ISD::ArgFlagsTy Flags;
1508 unsigned AttrInd = i - CS.arg_begin() + 1;
1509 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1510 Flags.setSExt();
1511 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1512 Flags.setZExt();
1513
1514 // FIXME: Only handle *easy* calls for now.
1515 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1516 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1517 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1518 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1519 return false;
1520
1521 const Type *ArgTy = (*i)->getType();
1522 EVT ArgVT;
1523 if (!isTypeLegal(ArgTy, ArgVT))
1524 return false;
1525 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1526 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001527
Eric Christopherf9764fa2010-09-30 20:49:44 +00001528 Args.push_back(*i);
1529 ArgRegs.push_back(Arg);
1530 ArgVTs.push_back(ArgVT);
1531 ArgFlags.push_back(Flags);
1532 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001533
Eric Christopherf9764fa2010-09-30 20:49:44 +00001534 // Handle the arguments now that we've gotten them.
1535 SmallVector<unsigned, 4> RegArgs;
1536 unsigned NumBytes;
1537 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1538 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001539
Eric Christopherf9764fa2010-09-30 20:49:44 +00001540 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001541 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001542 MachineInstrBuilder MIB;
1543 unsigned CallOpc;
1544 if(isThumb)
1545 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1546 else
1547 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1548 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1549 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001550
Eric Christopherf9764fa2010-09-30 20:49:44 +00001551 // Add implicit physical register uses to the call.
1552 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1553 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001554
Eric Christopherf9764fa2010-09-30 20:49:44 +00001555 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001556 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001557 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001558
Eric Christopherf9764fa2010-09-30 20:49:44 +00001559 // Set all unused physreg defs as dead.
1560 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001561
Eric Christopherf9764fa2010-09-30 20:49:44 +00001562 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001563
Eric Christopherf9764fa2010-09-30 20:49:44 +00001564}
1565
Eric Christopher56d2b722010-09-02 23:43:26 +00001566// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001567bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001568 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001569 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001570
Eric Christopherab695882010-07-21 22:26:11 +00001571 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001572 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001573 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001574 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001575 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001576 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001577 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001578 case Instruction::ICmp:
1579 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001580 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001581 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001582 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001583 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001584 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001585 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001586 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001587 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001588 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001589 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001590 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001591 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001592 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001593 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001594 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001595 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001596 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001597 case Instruction::SRem:
1598 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001599 case Instruction::Call:
1600 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001601 case Instruction::Select:
1602 return SelectSelect(I);
Eric Christopherab695882010-07-21 22:26:11 +00001603 default: break;
1604 }
1605 return false;
1606}
1607
1608namespace llvm {
1609 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001610 // Completely untested on non-darwin.
1611 const TargetMachine &TM = funcInfo.MF->getTarget();
1612 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher8ff9a9d2010-10-11 20:26:21 +00001613 if (Subtarget->isTargetDarwin() && EnableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001614 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001615 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001616 }
1617}