blob: 46f707ca952c237b2cd3b2a0bb8e972b690302b0 [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 Christopher9ed58df2010-09-09 00:19:41 +0000141 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000142 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000143 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000144 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000145 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000146
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000147 // Call handling routines.
148 private:
149 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000150 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000151 SmallVectorImpl<unsigned> &ArgRegs,
152 SmallVectorImpl<EVT> &ArgVTs,
153 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
154 SmallVectorImpl<unsigned> &RegArgs,
155 CallingConv::ID CC,
156 unsigned &NumBytes);
157 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
158 const Instruction *I, CallingConv::ID CC,
159 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000160 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000161
162 // OptionalDef handling routines.
163 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000164 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
165 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
166};
Eric Christopherab695882010-07-21 22:26:11 +0000167
168} // end anonymous namespace
169
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000170#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000171
Eric Christopher456144e2010-08-19 00:37:05 +0000172// DefinesOptionalPredicate - This is different from DefinesPredicate in that
173// we don't care about implicit defs here, just places we'll need to add a
174// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
175bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
176 const TargetInstrDesc &TID = MI->getDesc();
177 if (!TID.hasOptionalDef())
178 return false;
179
180 // Look to see if our OptionalDef is defining CPSR or CCR.
181 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
182 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000183 if (!MO.isReg() || !MO.isDef()) continue;
184 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000185 *CPSR = true;
186 }
187 return true;
188}
189
190// If the machine is predicable go ahead and add the predicate operands, if
191// it needs default CC operands add those.
192const MachineInstrBuilder &
193ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
194 MachineInstr *MI = &*MIB;
195
196 // Do we use a predicate?
197 if (TII.isPredicable(MI))
198 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000199
Eric Christopher456144e2010-08-19 00:37:05 +0000200 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
201 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000202 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000203 if (DefinesOptionalPredicate(MI, &CPSR)) {
204 if (CPSR)
205 AddDefaultT1CC(MIB);
206 else
207 AddDefaultCC(MIB);
208 }
209 return MIB;
210}
211
Eric Christopher0fe7d542010-08-17 01:25:29 +0000212unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
213 const TargetRegisterClass* RC) {
214 unsigned ResultReg = createResultReg(RC);
215 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
216
Eric Christopher456144e2010-08-19 00:37:05 +0000217 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000218 return ResultReg;
219}
220
221unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
222 const TargetRegisterClass *RC,
223 unsigned Op0, bool Op0IsKill) {
224 unsigned ResultReg = createResultReg(RC);
225 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
226
227 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000228 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000229 .addReg(Op0, Op0IsKill * RegState::Kill));
230 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000231 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000232 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000233 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000234 TII.get(TargetOpcode::COPY), ResultReg)
235 .addReg(II.ImplicitDefs[0]));
236 }
237 return ResultReg;
238}
239
240unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
241 const TargetRegisterClass *RC,
242 unsigned Op0, bool Op0IsKill,
243 unsigned Op1, bool Op1IsKill) {
244 unsigned ResultReg = createResultReg(RC);
245 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
246
247 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000248 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000249 .addReg(Op0, Op0IsKill * RegState::Kill)
250 .addReg(Op1, Op1IsKill * RegState::Kill));
251 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000252 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000253 .addReg(Op0, Op0IsKill * RegState::Kill)
254 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000255 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000256 TII.get(TargetOpcode::COPY), ResultReg)
257 .addReg(II.ImplicitDefs[0]));
258 }
259 return ResultReg;
260}
261
262unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
263 const TargetRegisterClass *RC,
264 unsigned Op0, bool Op0IsKill,
265 uint64_t Imm) {
266 unsigned ResultReg = createResultReg(RC);
267 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
268
269 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000270 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000271 .addReg(Op0, Op0IsKill * RegState::Kill)
272 .addImm(Imm));
273 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000274 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000275 .addReg(Op0, Op0IsKill * RegState::Kill)
276 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000277 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000278 TII.get(TargetOpcode::COPY), ResultReg)
279 .addReg(II.ImplicitDefs[0]));
280 }
281 return ResultReg;
282}
283
284unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
285 const TargetRegisterClass *RC,
286 unsigned Op0, bool Op0IsKill,
287 const ConstantFP *FPImm) {
288 unsigned ResultReg = createResultReg(RC);
289 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
290
291 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293 .addReg(Op0, Op0IsKill * RegState::Kill)
294 .addFPImm(FPImm));
295 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000296 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000297 .addReg(Op0, Op0IsKill * RegState::Kill)
298 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000299 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000300 TII.get(TargetOpcode::COPY), ResultReg)
301 .addReg(II.ImplicitDefs[0]));
302 }
303 return ResultReg;
304}
305
306unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
307 const TargetRegisterClass *RC,
308 unsigned Op0, bool Op0IsKill,
309 unsigned Op1, bool Op1IsKill,
310 uint64_t Imm) {
311 unsigned ResultReg = createResultReg(RC);
312 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
313
314 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000315 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000316 .addReg(Op0, Op0IsKill * RegState::Kill)
317 .addReg(Op1, Op1IsKill * RegState::Kill)
318 .addImm(Imm));
319 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000321 .addReg(Op0, Op0IsKill * RegState::Kill)
322 .addReg(Op1, Op1IsKill * RegState::Kill)
323 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000325 TII.get(TargetOpcode::COPY), ResultReg)
326 .addReg(II.ImplicitDefs[0]));
327 }
328 return ResultReg;
329}
330
331unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
332 const TargetRegisterClass *RC,
333 uint64_t Imm) {
334 unsigned ResultReg = createResultReg(RC);
335 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000336
Eric Christopher0fe7d542010-08-17 01:25:29 +0000337 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000338 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000339 .addImm(Imm));
340 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000341 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000342 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000343 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000344 TII.get(TargetOpcode::COPY), ResultReg)
345 .addReg(II.ImplicitDefs[0]));
346 }
347 return ResultReg;
348}
349
350unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
351 unsigned Op0, bool Op0IsKill,
352 uint32_t Idx) {
353 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
354 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
355 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000357 DL, TII.get(TargetOpcode::COPY), ResultReg)
358 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
359 return ResultReg;
360}
361
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000362// TODO: Don't worry about 64-bit now, but when this is fixed remove the
363// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000364unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000365 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000366
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000367 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
368 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
369 TII.get(ARM::VMOVRS), MoveReg)
370 .addReg(SrcReg));
371 return MoveReg;
372}
373
374unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000375 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000376
Eric Christopheraa3ace12010-09-09 20:49:25 +0000377 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
378 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000379 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000380 .addReg(SrcReg));
381 return MoveReg;
382}
383
Eric Christopher9ed58df2010-09-09 00:19:41 +0000384// For double width floating point we need to materialize two constants
385// (the high and the low) into integer registers then use a move to get
386// the combined constant into an FP reg.
387unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
388 const APFloat Val = CFP->getValueAPF();
389 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000390
Eric Christopher9ed58df2010-09-09 00:19:41 +0000391 // This checks to see if we can use VFP3 instructions to materialize
392 // a constant, otherwise we have to go through the constant pool.
393 if (TLI.isFPImmLegal(Val, VT)) {
394 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
395 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
396 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
397 DestReg)
398 .addFPImm(CFP));
399 return DestReg;
400 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000401
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000402 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000403 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000404
Eric Christopher238bb162010-09-09 23:50:00 +0000405 // MachineConstantPool wants an explicit alignment.
406 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
407 if (Align == 0) {
408 // TODO: Figure out if this is correct.
409 Align = TD.getTypeAllocSize(CFP->getType());
410 }
411 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
412 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
413 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000414
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000415 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000416 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
417 DestReg)
418 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000419 .addReg(0));
420 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000421}
422
Eric Christopher744c7c82010-09-28 22:47:54 +0000423unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000424
Eric Christopher744c7c82010-09-28 22:47:54 +0000425 // For now 32-bit only.
426 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000427
Eric Christopher56d2b722010-09-02 23:43:26 +0000428 // MachineConstantPool wants an explicit alignment.
429 unsigned Align = TD.getPrefTypeAlignment(C->getType());
430 if (Align == 0) {
431 // TODO: Figure out if this is correct.
432 Align = TD.getTypeAllocSize(C->getType());
433 }
434 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000435 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000436
Eric Christopher56d2b722010-09-02 23:43:26 +0000437 if (isThumb)
438 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000439 TII.get(ARM::t2LDRpci), DestReg)
440 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000441 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000442 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000443 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000444 TII.get(ARM::LDRcp), DestReg)
445 .addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000446 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000447
Eric Christopher56d2b722010-09-02 23:43:26 +0000448 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000449}
450
Eric Christopherc9932f62010-10-01 23:24:42 +0000451unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000452 // For now 32-bit only.
453 if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000454
Eric Christopher890dbbe2010-10-02 00:32:44 +0000455 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000456
Eric Christopher890dbbe2010-10-02 00:32:44 +0000457 // TODO: No external globals for now.
458 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000459
Eric Christopher890dbbe2010-10-02 00:32:44 +0000460 // TODO: Need more magic for ARM PIC.
461 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000462
Eric Christopher890dbbe2010-10-02 00:32:44 +0000463 // MachineConstantPool wants an explicit alignment.
464 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
465 if (Align == 0) {
466 // TODO: Figure out if this is correct.
467 Align = TD.getTypeAllocSize(GV->getType());
468 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000469
Eric Christopher890dbbe2010-10-02 00:32:44 +0000470 // Grab index.
471 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
472 unsigned Id = AFI->createConstPoolEntryUId();
473 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
474 ARMCP::CPValue, PCAdj);
475 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000476
Eric Christopher890dbbe2010-10-02 00:32:44 +0000477 // Load value.
478 MachineInstrBuilder MIB;
479 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
480 if (isThumb) {
481 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
482 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
483 .addConstantPoolIndex(Idx);
484 if (RelocM == Reloc::PIC_)
485 MIB.addImm(Id);
486 } else {
487 // The extra reg and immediate are for addrmode2.
488 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
489 DestReg)
490 .addConstantPoolIndex(Idx)
491 .addReg(0).addImm(0);
492 }
493 AddOptionalDefs(MIB);
494 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000495}
496
Eric Christopher9ed58df2010-09-09 00:19:41 +0000497unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
498 EVT VT = TLI.getValueType(C->getType(), true);
499
500 // Only handle simple types.
501 if (!VT.isSimple()) return 0;
502
503 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
504 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000505 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
506 return ARMMaterializeGV(GV, VT);
507 else if (isa<ConstantInt>(C))
508 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000509
Eric Christopherc9932f62010-10-01 23:24:42 +0000510 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000511}
512
Eric Christopherf9764fa2010-09-30 20:49:44 +0000513unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
514 // Don't handle dynamic allocas.
515 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000516
Eric Christopherf9764fa2010-09-30 20:49:44 +0000517 EVT VT;
518 if (!isTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000519
Eric Christopherf9764fa2010-09-30 20:49:44 +0000520 DenseMap<const AllocaInst*, int>::iterator SI =
521 FuncInfo.StaticAllocaMap.find(AI);
522
523 // This will get lowered later into the correct offsets and registers
524 // via rewriteXFrameIndex.
525 if (SI != FuncInfo.StaticAllocaMap.end()) {
526 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
527 unsigned ResultReg = createResultReg(RC);
528 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
529 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
530 TII.get(Opc), ResultReg)
531 .addFrameIndex(SI->second)
532 .addImm(0));
533 return ResultReg;
534 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000535
Eric Christopherf9764fa2010-09-30 20:49:44 +0000536 return 0;
537}
538
Eric Christopherb1cc8482010-08-25 07:23:49 +0000539bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
540 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000541
Eric Christopherb1cc8482010-08-25 07:23:49 +0000542 // Only handle simple types.
543 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000544
Eric Christopherdc908042010-08-31 01:28:42 +0000545 // Handle all legal types, i.e. a register that will directly hold this
546 // value.
547 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000548}
549
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000550bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
551 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000552
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000553 // If this is a type than can be sign or zero-extended to a basic operation
554 // go ahead and accept it now.
555 if (VT == MVT::i8 || VT == MVT::i16)
556 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000557
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000558 return false;
559}
560
Eric Christophercb0b04b2010-08-24 00:07:24 +0000561// Computes the Reg+Offset to get to an object.
562bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000563 int &Offset) {
564 // Some boilerplate from the X86 FastISel.
565 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000566 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000567 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000568 // Don't walk into other basic blocks; it's possible we haven't
569 // visited them yet, so the instructions may not yet be assigned
570 // virtual registers.
571 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
572 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000573 Opcode = I->getOpcode();
574 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000575 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000576 Opcode = C->getOpcode();
577 U = C;
578 }
579
Eric Christophercb0b04b2010-08-24 00:07:24 +0000580 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000581 if (Ty->getAddressSpace() > 255)
582 // Fast instruction selection doesn't support the special
583 // address spaces.
584 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000585
Eric Christopher83007122010-08-23 21:44:12 +0000586 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000587 default:
Eric Christopher83007122010-08-23 21:44:12 +0000588 break;
Eric Christopher55324332010-10-12 00:43:21 +0000589 case Instruction::BitCast: {
590 // Look through bitcasts.
591 return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
592 }
593 case Instruction::IntToPtr: {
594 // Look past no-op inttoptrs.
595 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
596 return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
597 break;
598 }
599 case Instruction::PtrToInt: {
600 // Look past no-op ptrtoints.
601 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
602 return ARMComputeRegOffset(U->getOperand(0), Reg, Offset);
603 break;
604 }
Eric Christopher83007122010-08-23 21:44:12 +0000605 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000606 const AllocaInst *AI = cast<AllocaInst>(Obj);
607 DenseMap<const AllocaInst*, int>::iterator SI =
608 FuncInfo.StaticAllocaMap.find(AI);
609 if (SI != FuncInfo.StaticAllocaMap.end()) {
610 Reg = ARM::SP;
611 Offset = SI->second;
612 return true;
613 }
Eric Christopher050d16c2010-10-11 21:37:35 +0000614 // Don't handle dynamic allocas.
Eric Christopher5f9e8b92010-10-11 22:01:22 +0000615 assert(!FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Obj)) &&
616 "Alloca should have been handled earlier!");
Eric Christopherf06f3092010-08-24 00:50:47 +0000617 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000618 }
619 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000620
Eric Christophera9c57512010-10-13 21:41:51 +0000621 // Materialize the global variable's address into a reg which can
622 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000623 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000624 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
625 if (Tmp == 0) return false;
626
627 Reg = Tmp;
628 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000629 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000630
Eric Christophercb0b04b2010-08-24 00:07:24 +0000631 // Try to get this in a register if nothing else has worked.
632 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000633 if (Reg == 0) return false;
634
635 // Since the offset may be too large for the load instruction
636 // get the reg+offset into a register.
637 // TODO: Verify the additions work, otherwise we'll need to add the
638 // offset instead of 0 to the instructions and do all sorts of operand
639 // munging.
640 // TODO: Optimize this somewhat.
641 if (Offset != 0) {
642 ARMCC::CondCodes Pred = ARMCC::AL;
643 unsigned PredReg = 0;
644
Eric Christophereaa204b2010-09-02 01:39:14 +0000645 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000646 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
647 Reg, Reg, Offset, Pred, PredReg,
648 static_cast<const ARMBaseInstrInfo&>(TII));
649 else {
650 assert(AFI->isThumb2Function());
651 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
652 Reg, Reg, Offset, Pred, PredReg,
653 static_cast<const ARMBaseInstrInfo&>(TII));
654 }
655 }
Eric Christopher318b6ee2010-09-02 00:53:56 +0000656 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000657}
658
Eric Christopherb1cc8482010-08-25 07:23:49 +0000659bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
660 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000661
Eric Christopherb1cc8482010-08-25 07:23:49 +0000662 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000663 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000664 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000665 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000666 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000667 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000668 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000669 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000670 case MVT::i16:
Eric Christopher7a56f332010-10-08 01:13:17 +0000671 Opc = isThumb ? ARM::t2LDRHi8 : ARM::LDRH;
672 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000673 VT = MVT::i32;
674 break;
675 case MVT::i8:
Eric Christopher7a56f332010-10-08 01:13:17 +0000676 Opc = isThumb ? ARM::t2LDRBi8 : ARM::LDRB;
677 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000678 VT = MVT::i32;
679 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000680 case MVT::i32:
Eric Christopher7a56f332010-10-08 01:13:17 +0000681 Opc = isThumb ? ARM::t2LDRi8 : ARM::LDR;
682 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000683 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000684 case MVT::f32:
685 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000686 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000687 isFloat = true;
688 break;
689 case MVT::f64:
690 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000691 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000692 isFloat = true;
693 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000694 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000695
Eric Christopheree56ea62010-10-07 05:50:44 +0000696 ResultReg = createResultReg(RC);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000697
Eric Christopher7a56f332010-10-08 01:13:17 +0000698 // For now with the additions above the offset should be zero - thus we
699 // can always fit into an i8.
Eric Christopher15418772010-10-12 05:39:06 +0000700 assert((Reg == ARM::SP || Offset == 0) &&
701 "Offset not zero and not a stack load!");
Eric Christopherdccd2c32010-10-11 08:38:55 +0000702
Eric Christopher15418772010-10-12 05:39:06 +0000703 if (Reg == ARM::SP)
704 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
705 ResultReg, Offset, RC,
706 TM.getRegisterInfo());
Eric Christopher7a56f332010-10-08 01:13:17 +0000707 // The thumb and floating point instructions both take 2 operands, ARM takes
708 // another register.
Eric Christopher15418772010-10-12 05:39:06 +0000709 else if (isFloat || isThumb)
Eric Christopher6dab1372010-09-18 01:59:37 +0000710 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
711 TII.get(Opc), ResultReg)
712 .addReg(Reg).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000713 else
714 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
715 TII.get(Opc), ResultReg)
716 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000717 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000718}
719
Eric Christopher43b62be2010-09-27 06:02:23 +0000720bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000721 // Verify we have a legal type before going any further.
722 EVT VT;
723 if (!isLoadTypeLegal(I->getType(), VT))
724 return false;
725
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000726 // Our register and offset with innocuous defaults.
727 unsigned Reg = 0;
728 int Offset = 0;
729
730 // See if we can handle this as Reg + Offset
731 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
732 return false;
733
734 unsigned ResultReg;
735 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
736
737 UpdateValueMap(I, ResultReg);
738 return true;
739}
740
Eric Christopher318b6ee2010-09-02 00:53:56 +0000741bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
742 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000743 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000744 bool isFloat = false;
Eric Christopher15418772010-10-12 05:39:06 +0000745 // VT is set here only for use in the alloca stores below - those are promoted
746 // to reg size always.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000747 switch (VT.getSimpleVT().SimpleTy) {
748 default: return false;
749 case MVT::i1:
Eric Christopher15418772010-10-12 05:39:06 +0000750 case MVT::i8:
751 VT = MVT::i32;
752 StrOpc = isThumb ? ARM::t2STRBi8 : ARM::STRB;
753 break;
754 case MVT::i16:
755 VT = MVT::i32;
756 StrOpc = isThumb ? ARM::t2STRHi8 : ARM::STRH;
757 break;
Eric Christophere93417b2010-10-08 23:52:16 +0000758 case MVT::i32: StrOpc = isThumb ? ARM::t2STRi8 : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000759 case MVT::f32:
760 if (!Subtarget->hasVFP2()) return false;
761 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000762 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000763 break;
764 case MVT::f64:
765 if (!Subtarget->hasVFP2()) return false;
766 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000767 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000768 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000769 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000770
Eric Christopher558cf002010-10-12 21:23:43 +0000771 if (DstReg == ARM::SP)
Eric Christopher15418772010-10-12 05:39:06 +0000772 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
773 SrcReg, true /*isKill*/, Offset,
774 TLI.getRegClassFor(VT), TM.getRegisterInfo());
Eric Christopherb74558a2010-09-18 01:23:38 +0000775 // The thumb addressing mode has operands swapped from the arm addressing
776 // mode, the floating point one only has two operands.
Eric Christophere93417b2010-10-08 23:52:16 +0000777 if (isFloat || isThumb)
Eric Christopherb74558a2010-09-18 01:23:38 +0000778 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000779 TII.get(StrOpc))
780 .addReg(SrcReg).addReg(DstReg).addImm(Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000781 else
782 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000783 TII.get(StrOpc))
784 .addReg(SrcReg).addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000785
Eric Christopher318b6ee2010-09-02 00:53:56 +0000786 return true;
787}
788
Eric Christopher43b62be2010-09-27 06:02:23 +0000789bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000790 Value *Op0 = I->getOperand(0);
791 unsigned SrcReg = 0;
792
Eric Christopher543cf052010-09-01 22:16:27 +0000793 // Yay type legalization
794 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000795 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000796 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000797
Eric Christopher1b61ef42010-09-02 01:48:11 +0000798 // Get the value to be stored into a register.
799 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000800 if (SrcReg == 0)
801 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000802
Eric Christopher318b6ee2010-09-02 00:53:56 +0000803 // Our register and offset with innocuous defaults.
804 unsigned Reg = 0;
805 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000806
Eric Christopher318b6ee2010-09-02 00:53:56 +0000807 // See if we can handle this as Reg + Offset
808 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
809 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000810
Eric Christopher318b6ee2010-09-02 00:53:56 +0000811 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000812
Eric Christophera5b1e682010-09-17 22:28:18 +0000813 return true;
814}
815
816static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
817 switch (Pred) {
818 // Needs two compares...
819 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000820 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000821 default:
822 assert(false && "Unhandled CmpInst::Predicate!");
823 return ARMCC::AL;
824 case CmpInst::ICMP_EQ:
825 case CmpInst::FCMP_OEQ:
826 return ARMCC::EQ;
827 case CmpInst::ICMP_SGT:
828 case CmpInst::FCMP_OGT:
829 return ARMCC::GT;
830 case CmpInst::ICMP_SGE:
831 case CmpInst::FCMP_OGE:
832 return ARMCC::GE;
833 case CmpInst::ICMP_UGT:
834 case CmpInst::FCMP_UGT:
835 return ARMCC::HI;
836 case CmpInst::FCMP_OLT:
837 return ARMCC::MI;
838 case CmpInst::ICMP_ULE:
839 case CmpInst::FCMP_OLE:
840 return ARMCC::LS;
841 case CmpInst::FCMP_ORD:
842 return ARMCC::VC;
843 case CmpInst::FCMP_UNO:
844 return ARMCC::VS;
845 case CmpInst::FCMP_UGE:
846 return ARMCC::PL;
847 case CmpInst::ICMP_SLT:
848 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000849 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000850 case CmpInst::ICMP_SLE:
851 case CmpInst::FCMP_ULE:
852 return ARMCC::LE;
853 case CmpInst::FCMP_UNE:
854 case CmpInst::ICMP_NE:
855 return ARMCC::NE;
856 case CmpInst::ICMP_UGE:
857 return ARMCC::HS;
858 case CmpInst::ICMP_ULT:
859 return ARMCC::LO;
860 }
Eric Christopher543cf052010-09-01 22:16:27 +0000861}
862
Eric Christopher43b62be2010-09-27 06:02:23 +0000863bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000864 const BranchInst *BI = cast<BranchInst>(I);
865 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
866 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000867
Eric Christophere5734102010-09-03 00:35:47 +0000868 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000869 // TODO: Try to avoid the re-computation in some places.
870 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000871 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000872
Eric Christopher229207a2010-09-29 01:14:47 +0000873 // Re-set the flags just in case.
874 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
875 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
876 .addReg(CondReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000877
Eric Christophere5734102010-09-03 00:35:47 +0000878 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000879 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000880 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000881 FastEmitBranch(FBB, DL);
882 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000883 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000884}
885
Eric Christopher43b62be2010-09-27 06:02:23 +0000886bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000887 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000888
Eric Christopherd43393a2010-09-08 23:13:45 +0000889 EVT VT;
890 const Type *Ty = CI->getOperand(0)->getType();
891 if (!isTypeLegal(Ty, VT))
892 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000893
Eric Christopherd43393a2010-09-08 23:13:45 +0000894 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
895 if (isFloat && !Subtarget->hasVFP2())
896 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000897
Eric Christopherd43393a2010-09-08 23:13:45 +0000898 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000899 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +0000900 switch (VT.getSimpleVT().SimpleTy) {
901 default: return false;
902 // TODO: Verify compares.
903 case MVT::f32:
904 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +0000905 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000906 break;
907 case MVT::f64:
908 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +0000909 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000910 break;
911 case MVT::i32:
912 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +0000913 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000914 break;
915 }
916
Eric Christopher229207a2010-09-29 01:14:47 +0000917 // Get the compare predicate.
918 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +0000919
Eric Christopher229207a2010-09-29 01:14:47 +0000920 // We may not handle every CC for now.
921 if (ARMPred == ARMCC::AL) return false;
922
Eric Christopherd43393a2010-09-08 23:13:45 +0000923 unsigned Arg1 = getRegForValue(CI->getOperand(0));
924 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000925
Eric Christopherd43393a2010-09-08 23:13:45 +0000926 unsigned Arg2 = getRegForValue(CI->getOperand(1));
927 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000928
Eric Christopherd43393a2010-09-08 23:13:45 +0000929 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
930 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000931
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000932 // For floating point we need to move the result to a comparison register
933 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000934 if (isFloat)
935 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
936 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000937
Eric Christopher229207a2010-09-29 01:14:47 +0000938 // Now set a register based on the comparison. Explicitly set the predicates
939 // here.
Eric Christopher338c2532010-10-07 05:31:49 +0000940 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000941 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +0000942 : ARM::GPRRegisterClass;
943 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000944 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +0000945 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +0000946 unsigned ZeroReg = TargetMaterializeConstant(Zero);
947 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
948 .addReg(ZeroReg).addImm(1)
949 .addImm(ARMPred).addReg(CondReg);
950
Eric Christophera5b1e682010-09-17 22:28:18 +0000951 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +0000952 return true;
953}
954
Eric Christopher43b62be2010-09-27 06:02:23 +0000955bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +0000956 // Make sure we have VFP and that we're extending float to double.
957 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000958
Eric Christopher46203602010-09-09 00:26:48 +0000959 Value *V = I->getOperand(0);
960 if (!I->getType()->isDoubleTy() ||
961 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000962
Eric Christopher46203602010-09-09 00:26:48 +0000963 unsigned Op = getRegForValue(V);
964 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000965
Eric Christopher46203602010-09-09 00:26:48 +0000966 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000967 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000968 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000969 .addReg(Op));
970 UpdateValueMap(I, Result);
971 return true;
972}
973
Eric Christopher43b62be2010-09-27 06:02:23 +0000974bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +0000975 // Make sure we have VFP and that we're truncating double to float.
976 if (!Subtarget->hasVFP2()) return false;
977
978 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +0000979 if (!(I->getType()->isFloatTy() &&
980 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +0000981
982 unsigned Op = getRegForValue(V);
983 if (Op == 0) return false;
984
985 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +0000986 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000987 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000988 .addReg(Op));
989 UpdateValueMap(I, Result);
990 return true;
991}
992
Eric Christopher43b62be2010-09-27 06:02:23 +0000993bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +0000994 // Make sure we have VFP.
995 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000996
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000997 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000998 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000999 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001000 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001001
Eric Christopher9a040492010-09-09 18:54:59 +00001002 unsigned Op = getRegForValue(I->getOperand(0));
1003 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001004
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001005 // The conversion routine works on fp-reg to fp-reg and the operand above
1006 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001007 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001008 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001009
Eric Christopher9a040492010-09-09 18:54:59 +00001010 unsigned Opc;
1011 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1012 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1013 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001014
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001015 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001016 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1017 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001018 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001019 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001020 return true;
1021}
1022
Eric Christopher43b62be2010-09-27 06:02:23 +00001023bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001024 // Make sure we have VFP.
1025 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001026
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001027 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001028 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001029 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001030 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001031
Eric Christopher9a040492010-09-09 18:54:59 +00001032 unsigned Op = getRegForValue(I->getOperand(0));
1033 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001034
Eric Christopher9a040492010-09-09 18:54:59 +00001035 unsigned Opc;
1036 const Type *OpTy = I->getOperand(0)->getType();
1037 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1038 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1039 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001040
Eric Christopher022b7fb2010-10-05 23:13:24 +00001041 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1042 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001043 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1044 ResultReg)
1045 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001046
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001047 // This result needs to be in an integer register, but the conversion only
1048 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001049 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001050 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001051
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001052 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001053 return true;
1054}
1055
Eric Christopher3bbd3962010-10-11 08:27:59 +00001056bool ARMFastISel::SelectSelect(const Instruction *I) {
1057 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1058 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1059 return false;
1060
1061 // Things need to be register sized for register moves.
1062 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1063 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1064
1065 unsigned CondReg = getRegForValue(I->getOperand(0));
1066 if (CondReg == 0) return false;
1067 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1068 if (Op1Reg == 0) return false;
1069 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1070 if (Op2Reg == 0) return false;
1071
1072 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1073 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1074 .addReg(CondReg).addImm(1));
1075 unsigned ResultReg = createResultReg(RC);
1076 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1077 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1078 .addReg(Op1Reg).addReg(Op2Reg)
1079 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1080 UpdateValueMap(I, ResultReg);
1081 return true;
1082}
1083
Eric Christopher08637852010-09-30 22:34:19 +00001084bool ARMFastISel::SelectSDiv(const Instruction *I) {
1085 EVT VT;
1086 const Type *Ty = I->getType();
1087 if (!isTypeLegal(Ty, VT))
1088 return false;
1089
1090 // If we have integer div support we should have selected this automagically.
1091 // In case we have a real miss go ahead and return false and we'll pick
1092 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001093 if (Subtarget->hasDivide()) return false;
1094
Eric Christopher08637852010-09-30 22:34:19 +00001095 // Otherwise emit a libcall.
1096 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001097 if (VT == MVT::i8)
1098 LC = RTLIB::SDIV_I8;
1099 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001100 LC = RTLIB::SDIV_I16;
1101 else if (VT == MVT::i32)
1102 LC = RTLIB::SDIV_I32;
1103 else if (VT == MVT::i64)
1104 LC = RTLIB::SDIV_I64;
1105 else if (VT == MVT::i128)
1106 LC = RTLIB::SDIV_I128;
1107 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001108
Eric Christopher08637852010-09-30 22:34:19 +00001109 return ARMEmitLibcall(I, LC);
1110}
1111
Eric Christopher6a880d62010-10-11 08:37:26 +00001112bool ARMFastISel::SelectSRem(const Instruction *I) {
1113 EVT VT;
1114 const Type *Ty = I->getType();
1115 if (!isTypeLegal(Ty, VT))
1116 return false;
1117
1118 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1119 if (VT == MVT::i8)
1120 LC = RTLIB::SREM_I8;
1121 else if (VT == MVT::i16)
1122 LC = RTLIB::SREM_I16;
1123 else if (VT == MVT::i32)
1124 LC = RTLIB::SREM_I32;
1125 else if (VT == MVT::i64)
1126 LC = RTLIB::SREM_I64;
1127 else if (VT == MVT::i128)
1128 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001129 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher6a880d62010-10-11 08:37:26 +00001130
1131 return ARMEmitLibcall(I, LC);
1132}
1133
Eric Christopher43b62be2010-09-27 06:02:23 +00001134bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001135 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001136
Eric Christopherbc39b822010-09-09 00:53:57 +00001137 // We can get here in the case when we want to use NEON for our fp
1138 // operations, but can't figure out how to. Just use the vfp instructions
1139 // if we have them.
1140 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001141 const Type *Ty = I->getType();
1142 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1143 if (isFloat && !Subtarget->hasVFP2())
1144 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001145
Eric Christopherbc39b822010-09-09 00:53:57 +00001146 unsigned Op1 = getRegForValue(I->getOperand(0));
1147 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001148
Eric Christopherbc39b822010-09-09 00:53:57 +00001149 unsigned Op2 = getRegForValue(I->getOperand(1));
1150 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001151
Eric Christopherbc39b822010-09-09 00:53:57 +00001152 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +00001153 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1154 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001155 switch (ISDOpcode) {
1156 default: return false;
1157 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001158 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001159 break;
1160 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001161 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001162 break;
1163 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001164 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001165 break;
1166 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001167 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001168 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1169 TII.get(Opc), ResultReg)
1170 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001171 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001172 return true;
1173}
1174
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001175// Call Handling Code
1176
1177// This is largely taken directly from CCAssignFnForNode - we don't support
1178// varargs in FastISel so that part has been removed.
1179// TODO: We may not support all of this.
1180CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1181 switch (CC) {
1182 default:
1183 llvm_unreachable("Unsupported calling convention");
1184 case CallingConv::C:
1185 case CallingConv::Fast:
1186 // Use target triple & subtarget features to do actual dispatch.
1187 if (Subtarget->isAAPCS_ABI()) {
1188 if (Subtarget->hasVFP2() &&
1189 FloatABIType == FloatABI::Hard)
1190 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1191 else
1192 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1193 } else
1194 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1195 case CallingConv::ARM_AAPCS_VFP:
1196 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1197 case CallingConv::ARM_AAPCS:
1198 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1199 case CallingConv::ARM_APCS:
1200 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1201 }
1202}
1203
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001204bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1205 SmallVectorImpl<unsigned> &ArgRegs,
1206 SmallVectorImpl<EVT> &ArgVTs,
1207 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1208 SmallVectorImpl<unsigned> &RegArgs,
1209 CallingConv::ID CC,
1210 unsigned &NumBytes) {
1211 SmallVector<CCValAssign, 16> ArgLocs;
1212 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1213 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1214
1215 // Get a count of how many bytes are to be pushed on the stack.
1216 NumBytes = CCInfo.getNextStackOffset();
1217
1218 // Issue CALLSEQ_START
1219 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001220 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1221 TII.get(AdjStackDown))
1222 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001223
1224 // Process the args.
1225 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1226 CCValAssign &VA = ArgLocs[i];
1227 unsigned Arg = ArgRegs[VA.getValNo()];
1228 EVT ArgVT = ArgVTs[VA.getValNo()];
1229
Eric Christopherf9764fa2010-09-30 20:49:44 +00001230 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001231 switch (VA.getLocInfo()) {
1232 case CCValAssign::Full: break;
1233 default:
Eric Christopher11077342010-10-07 05:14:08 +00001234 // TODO: Handle arg promotion.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001235 return false;
1236 }
1237
1238 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001239 // TODO: We need custom lowering for f64 args.
1240 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001242 VA.getLocReg())
1243 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001244 RegArgs.push_back(VA.getLocReg());
1245 } else {
1246 // Need to store
1247 return false;
1248 }
1249 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001250
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001251 return true;
1252}
1253
1254bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1255 const Instruction *I, CallingConv::ID CC,
1256 unsigned &NumBytes) {
1257 // Issue CALLSEQ_END
1258 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001259 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1260 TII.get(AdjStackUp))
1261 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001262
1263 // Now the return value.
1264 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1265 SmallVector<CCValAssign, 16> RVLocs;
1266 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1267 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1268
1269 // Copy all of the result registers out of their specified physreg.
Eric Christopher14df8822010-10-01 00:00:11 +00001270 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1271 // For this move we copy into two registers and then move into the
1272 // double fp reg we want.
1273 // TODO: Are the copies necessary?
1274 TargetRegisterClass *CopyRC = TLI.getRegClassFor(MVT::i32);
1275 unsigned Copy1 = createResultReg(CopyRC);
1276 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1277 Copy1).addReg(RVLocs[0].getLocReg());
1278 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001279
Eric Christopher14df8822010-10-01 00:00:11 +00001280 unsigned Copy2 = createResultReg(CopyRC);
1281 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1282 Copy2).addReg(RVLocs[1].getLocReg());
1283 UsedRegs.push_back(RVLocs[1].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001284
Eric Christopher14df8822010-10-01 00:00:11 +00001285 EVT DestVT = RVLocs[0].getValVT();
1286 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1287 unsigned ResultReg = createResultReg(DstRC);
1288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1289 TII.get(ARM::VMOVDRR), ResultReg)
1290 .addReg(Copy1).addReg(Copy2));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001291
1292 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001293 UpdateValueMap(I, ResultReg);
1294 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001295 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001296 EVT CopyVT = RVLocs[0].getValVT();
1297 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001298
Eric Christopher14df8822010-10-01 00:00:11 +00001299 unsigned ResultReg = createResultReg(DstRC);
1300 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1301 ResultReg).addReg(RVLocs[0].getLocReg());
1302 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001303
Eric Christopherdccd2c32010-10-11 08:38:55 +00001304 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001305 UpdateValueMap(I, ResultReg);
1306 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001307 }
1308
Eric Christopherdccd2c32010-10-11 08:38:55 +00001309 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001310}
1311
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001312// A quick function that will emit a call for a named libcall in F with the
1313// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001314// can emit a call for any libcall we can produce. This is an abridged version
1315// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001316// like computed function pointers or strange arguments at call sites.
1317// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1318// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001319bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1320 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001321
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001322 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001323 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001324 EVT RetVT;
1325 if (RetTy->isVoidTy())
1326 RetVT = MVT::isVoid;
1327 else if (!isTypeLegal(RetTy, RetVT))
1328 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001329
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001330 // For now we're using BLX etc on the assumption that we have v5t ops.
1331 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001332
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001333 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001334 SmallVector<Value*, 8> Args;
1335 SmallVector<unsigned, 8> ArgRegs;
1336 SmallVector<EVT, 8> ArgVTs;
1337 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1338 Args.reserve(I->getNumOperands());
1339 ArgRegs.reserve(I->getNumOperands());
1340 ArgVTs.reserve(I->getNumOperands());
1341 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001342 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001343 Value *Op = I->getOperand(i);
1344 unsigned Arg = getRegForValue(Op);
1345 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001346
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001347 const Type *ArgTy = Op->getType();
1348 EVT ArgVT;
1349 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001350
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001351 ISD::ArgFlagsTy Flags;
1352 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1353 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001354
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001355 Args.push_back(Op);
1356 ArgRegs.push_back(Arg);
1357 ArgVTs.push_back(ArgVT);
1358 ArgFlags.push_back(Flags);
1359 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001360
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001361 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001362 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001363 unsigned NumBytes;
1364 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1365 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001366
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001367 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001368 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001369 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001370 unsigned CallOpc;
1371 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001372 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001373 else
1374 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001375 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001376 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001377
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001378 // Add implicit physical register uses to the call.
1379 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1380 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001381
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001382 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001383 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001384 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001385
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001386 // Set all unused physreg defs as dead.
1387 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001388
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001389 return true;
1390}
1391
Eric Christopherf9764fa2010-09-30 20:49:44 +00001392bool ARMFastISel::SelectCall(const Instruction *I) {
1393 const CallInst *CI = cast<CallInst>(I);
1394 const Value *Callee = CI->getCalledValue();
1395
1396 // Can't handle inline asm or worry about intrinsics yet.
1397 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1398
Eric Christophere6ca6772010-10-01 21:33:12 +00001399 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001400 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001401 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1402 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001403
Eric Christopherf9764fa2010-09-30 20:49:44 +00001404 // Check the calling convention.
1405 ImmutableCallSite CS(CI);
1406 CallingConv::ID CC = CS.getCallingConv();
1407 // TODO: Avoid some calling conventions?
1408 if (CC != CallingConv::C) {
Eric Christophere540a6f2010-10-05 23:50:58 +00001409 // errs() << "Can't handle calling convention: " << CC << "\n";
Eric Christopherf9764fa2010-09-30 20:49:44 +00001410 return false;
1411 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001412
Eric Christopherf9764fa2010-09-30 20:49:44 +00001413 // Let SDISel handle vararg functions.
1414 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1415 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1416 if (FTy->isVarArg())
1417 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001418
Eric Christopherf9764fa2010-09-30 20:49:44 +00001419 // Handle *simple* calls for now.
1420 const Type *RetTy = I->getType();
1421 EVT RetVT;
1422 if (RetTy->isVoidTy())
1423 RetVT = MVT::isVoid;
1424 else if (!isTypeLegal(RetTy, RetVT))
1425 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001426
Eric Christopherf9764fa2010-09-30 20:49:44 +00001427 // For now we're using BLX etc on the assumption that we have v5t ops.
1428 // TODO: Maybe?
1429 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001430
Eric Christopherf9764fa2010-09-30 20:49:44 +00001431 // Set up the argument vectors.
1432 SmallVector<Value*, 8> Args;
1433 SmallVector<unsigned, 8> ArgRegs;
1434 SmallVector<EVT, 8> ArgVTs;
1435 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1436 Args.reserve(CS.arg_size());
1437 ArgRegs.reserve(CS.arg_size());
1438 ArgVTs.reserve(CS.arg_size());
1439 ArgFlags.reserve(CS.arg_size());
1440 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1441 i != e; ++i) {
1442 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001443
Eric Christopherf9764fa2010-09-30 20:49:44 +00001444 if (Arg == 0)
1445 return false;
1446 ISD::ArgFlagsTy Flags;
1447 unsigned AttrInd = i - CS.arg_begin() + 1;
1448 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1449 Flags.setSExt();
1450 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1451 Flags.setZExt();
1452
1453 // FIXME: Only handle *easy* calls for now.
1454 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1455 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1456 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1457 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1458 return false;
1459
1460 const Type *ArgTy = (*i)->getType();
1461 EVT ArgVT;
1462 if (!isTypeLegal(ArgTy, ArgVT))
1463 return false;
1464 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1465 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001466
Eric Christopherf9764fa2010-09-30 20:49:44 +00001467 Args.push_back(*i);
1468 ArgRegs.push_back(Arg);
1469 ArgVTs.push_back(ArgVT);
1470 ArgFlags.push_back(Flags);
1471 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001472
Eric Christopherf9764fa2010-09-30 20:49:44 +00001473 // Handle the arguments now that we've gotten them.
1474 SmallVector<unsigned, 4> RegArgs;
1475 unsigned NumBytes;
1476 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1477 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001478
Eric Christopherf9764fa2010-09-30 20:49:44 +00001479 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001480 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001481 MachineInstrBuilder MIB;
1482 unsigned CallOpc;
1483 if(isThumb)
1484 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1485 else
1486 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1487 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1488 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001489
Eric Christopherf9764fa2010-09-30 20:49:44 +00001490 // Add implicit physical register uses to the call.
1491 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1492 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001493
Eric Christopherf9764fa2010-09-30 20:49:44 +00001494 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001495 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001496 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001497
Eric Christopherf9764fa2010-09-30 20:49:44 +00001498 // Set all unused physreg defs as dead.
1499 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001500
Eric Christopherf9764fa2010-09-30 20:49:44 +00001501 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001502
Eric Christopherf9764fa2010-09-30 20:49:44 +00001503}
1504
Eric Christopher56d2b722010-09-02 23:43:26 +00001505// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001506bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001507 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001508 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001509
Eric Christopherab695882010-07-21 22:26:11 +00001510 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001511 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001512 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001513 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001514 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001515 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001516 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001517 case Instruction::ICmp:
1518 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001519 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001520 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001521 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001522 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001523 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001524 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001525 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001526 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001527 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001528 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001529 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001530 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001531 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001532 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001533 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001534 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001535 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001536 case Instruction::SRem:
1537 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001538 case Instruction::Call:
1539 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001540 case Instruction::Select:
1541 return SelectSelect(I);
Eric Christopherab695882010-07-21 22:26:11 +00001542 default: break;
1543 }
1544 return false;
1545}
1546
1547namespace llvm {
1548 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001549 // Completely untested on non-darwin.
1550 const TargetMachine &TM = funcInfo.MF->getTarget();
1551 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher8ff9a9d2010-10-11 20:26:21 +00001552 if (Subtarget->isTargetDarwin() && EnableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001553 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001554 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001555 }
1556}