blob: 9e8e1df0f156508a24a144c588b9f66dfead30a2 [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>
49EnableARMFastISel("arm-fast-isel",
50 cl::desc("Turn on experimental ARM fast-isel support"),
51 cl::init(false), cl::Hidden);
52
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 Christopherf9764fa2010-09-30 20:49:44 +0000130 virtual bool SelectCall(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000131
Eric Christopher83007122010-08-23 21:44:12 +0000132 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000133 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000134 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000135 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000136 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000137 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000138 bool ARMLoadAlloca(const Instruction *I, EVT VT);
139 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
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 Christophera9a7a1a2010-09-29 23:11:09 +0000150 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
151 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;
366
367 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;
376
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 Christopher238bb162010-09-09 23:50:00 +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;
404
405 // 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;
414
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) {
424
425 // For now 32-bit only.
426 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
427
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 Christopherdb12b2b2010-09-10 00:34:35 +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;
454
455 Reloc::Model RelocM = TM.getRelocationModel();
456
457 // TODO: No external globals for now.
458 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
459
460 // TODO: Need more magic for ARM PIC.
461 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
462
463 // 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 }
469
470 // 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);
476
477 // 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);
509
510 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;
516
517 EVT VT;
518 if (!isTypeLegal(AI->getType(), VT)) return false;
519
520 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 }
535
536 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;
589 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000590 assert(false && "Alloca should have been handled earlier!");
591 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000592 }
593 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000594
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000595 // FIXME: Handle global variables.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000596 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000597 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000598 return false;
599 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000600
Eric Christophercb0b04b2010-08-24 00:07:24 +0000601 // Try to get this in a register if nothing else has worked.
602 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000603 if (Reg == 0) return false;
604
605 // Since the offset may be too large for the load instruction
606 // get the reg+offset into a register.
607 // TODO: Verify the additions work, otherwise we'll need to add the
608 // offset instead of 0 to the instructions and do all sorts of operand
609 // munging.
610 // TODO: Optimize this somewhat.
611 if (Offset != 0) {
612 ARMCC::CondCodes Pred = ARMCC::AL;
613 unsigned PredReg = 0;
614
Eric Christophereaa204b2010-09-02 01:39:14 +0000615 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000616 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
617 Reg, Reg, Offset, Pred, PredReg,
618 static_cast<const ARMBaseInstrInfo&>(TII));
619 else {
620 assert(AFI->isThumb2Function());
621 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
622 Reg, Reg, Offset, Pred, PredReg,
623 static_cast<const ARMBaseInstrInfo&>(TII));
624 }
625 }
Eric Christopher318b6ee2010-09-02 00:53:56 +0000626 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000627}
628
Eric Christopher30b66332010-09-08 21:49:50 +0000629bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000630 Value *Op0 = I->getOperand(0);
631
632 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000633 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
634 DenseMap<const AllocaInst*, int>::iterator SI =
635 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000636
Eric Christophere24d66f2010-08-24 22:07:27 +0000637 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000638 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000639 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000640 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000641 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000642 TM.getRegisterInfo());
643 UpdateValueMap(I, ResultReg);
644 return true;
645 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000646 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000647 return false;
648}
649
Eric Christopherb1cc8482010-08-25 07:23:49 +0000650bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
651 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000652
Eric Christopherb1cc8482010-08-25 07:23:49 +0000653 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000654 unsigned Opc;
Eric Christopher6dab1372010-09-18 01:59:37 +0000655 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000656 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000657 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000658 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000659 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000660 case MVT::i16:
661 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
662 VT = MVT::i32;
663 break;
664 case MVT::i8:
665 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
666 VT = MVT::i32;
667 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000668 case MVT::i32:
669 Opc = isThumb ? ARM::tLDR : ARM::LDR;
670 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000671 case MVT::f32:
672 Opc = ARM::VLDRS;
673 isFloat = true;
674 break;
675 case MVT::f64:
676 Opc = ARM::VLDRD;
677 isFloat = true;
678 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000679 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000680
Eric Christopherdc908042010-08-31 01:28:42 +0000681 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000682
Eric Christopherdc908042010-08-31 01:28:42 +0000683 // TODO: Fix the Addressing modes so that these can share some code.
684 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
Eric Christopher6dab1372010-09-18 01:59:37 +0000685 // The thumb addressing mode has operands swapped from the arm addressing
686 // mode, the floating point one only has two operands.
687 if (isFloat)
688 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
689 TII.get(Opc), ResultReg)
690 .addReg(Reg).addImm(Offset));
691 else if (isThumb)
Eric Christopherdc908042010-08-31 01:28:42 +0000692 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
693 TII.get(Opc), ResultReg)
694 .addReg(Reg).addImm(Offset).addReg(0));
695 else
696 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
697 TII.get(Opc), ResultReg)
698 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000699 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000700}
701
Eric Christopher43b62be2010-09-27 06:02:23 +0000702bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000703 // Verify we have a legal type before going any further.
704 EVT VT;
705 if (!isLoadTypeLegal(I->getType(), VT))
706 return false;
707
708 // If we're an alloca we know we have a frame index and can emit the load
709 // directly in short order.
710 if (ARMLoadAlloca(I, VT))
711 return true;
712
713 // Our register and offset with innocuous defaults.
714 unsigned Reg = 0;
715 int Offset = 0;
716
717 // See if we can handle this as Reg + Offset
718 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
719 return false;
720
721 unsigned ResultReg;
722 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
723
724 UpdateValueMap(I, ResultReg);
725 return true;
726}
727
Eric Christopher30b66332010-09-08 21:49:50 +0000728bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000729 Value *Op1 = I->getOperand(1);
730
731 // Verify it's an alloca.
732 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
733 DenseMap<const AllocaInst*, int>::iterator SI =
734 FuncInfo.StaticAllocaMap.find(AI);
735
736 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000737 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000738 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000739 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000740 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000741 TM.getRegisterInfo());
742 return true;
743 }
744 }
745 return false;
746}
747
Eric Christopher318b6ee2010-09-02 00:53:56 +0000748bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
749 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000750 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000751 bool isFloat = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000752 switch (VT.getSimpleVT().SimpleTy) {
753 default: return false;
754 case MVT::i1:
755 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
756 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
757 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000758 case MVT::f32:
759 if (!Subtarget->hasVFP2()) return false;
760 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000761 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000762 break;
763 case MVT::f64:
764 if (!Subtarget->hasVFP2()) return false;
765 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000766 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000767 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000768 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000769
Eric Christopherb74558a2010-09-18 01:23:38 +0000770 // The thumb addressing mode has operands swapped from the arm addressing
771 // mode, the floating point one only has two operands.
Eric Christopher6dab1372010-09-18 01:59:37 +0000772 if (isFloat)
Eric Christopherb74558a2010-09-18 01:23:38 +0000773 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000774 TII.get(StrOpc))
775 .addReg(SrcReg).addReg(DstReg).addImm(Offset));
Eric Christopher6dab1372010-09-18 01:59:37 +0000776 else if (isThumb)
777 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000778 TII.get(StrOpc))
779 .addReg(SrcReg).addReg(DstReg).addImm(Offset).addReg(0));
Eric Christopher6dab1372010-09-18 01:59:37 +0000780
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 // If we're an alloca we know we have a frame index and can emit the store
804 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000805 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000806 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000807
Eric Christopher318b6ee2010-09-02 00:53:56 +0000808 // Our register and offset with innocuous defaults.
809 unsigned Reg = 0;
810 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000811
Eric Christopher318b6ee2010-09-02 00:53:56 +0000812 // See if we can handle this as Reg + Offset
813 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
814 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000815
Eric Christopher318b6ee2010-09-02 00:53:56 +0000816 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000817
Eric Christophera5b1e682010-09-17 22:28:18 +0000818 return true;
819}
820
821static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
822 switch (Pred) {
823 // Needs two compares...
824 case CmpInst::FCMP_ONE:
825 case CmpInst::FCMP_UEQ:
826 default:
827 assert(false && "Unhandled CmpInst::Predicate!");
828 return ARMCC::AL;
829 case CmpInst::ICMP_EQ:
830 case CmpInst::FCMP_OEQ:
831 return ARMCC::EQ;
832 case CmpInst::ICMP_SGT:
833 case CmpInst::FCMP_OGT:
834 return ARMCC::GT;
835 case CmpInst::ICMP_SGE:
836 case CmpInst::FCMP_OGE:
837 return ARMCC::GE;
838 case CmpInst::ICMP_UGT:
839 case CmpInst::FCMP_UGT:
840 return ARMCC::HI;
841 case CmpInst::FCMP_OLT:
842 return ARMCC::MI;
843 case CmpInst::ICMP_ULE:
844 case CmpInst::FCMP_OLE:
845 return ARMCC::LS;
846 case CmpInst::FCMP_ORD:
847 return ARMCC::VC;
848 case CmpInst::FCMP_UNO:
849 return ARMCC::VS;
850 case CmpInst::FCMP_UGE:
851 return ARMCC::PL;
852 case CmpInst::ICMP_SLT:
853 case CmpInst::FCMP_ULT:
854 return ARMCC::LT;
855 case CmpInst::ICMP_SLE:
856 case CmpInst::FCMP_ULE:
857 return ARMCC::LE;
858 case CmpInst::FCMP_UNE:
859 case CmpInst::ICMP_NE:
860 return ARMCC::NE;
861 case CmpInst::ICMP_UGE:
862 return ARMCC::HS;
863 case CmpInst::ICMP_ULT:
864 return ARMCC::LO;
865 }
Eric Christopher543cf052010-09-01 22:16:27 +0000866}
867
Eric Christopher43b62be2010-09-27 06:02:23 +0000868bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000869 const BranchInst *BI = cast<BranchInst>(I);
870 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
871 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000872
Eric Christophere5734102010-09-03 00:35:47 +0000873 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000874 // TODO: Try to avoid the re-computation in some places.
875 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000876 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000877
Eric Christopher229207a2010-09-29 01:14:47 +0000878 // Re-set the flags just in case.
879 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
880 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
881 .addReg(CondReg).addImm(1));
Eric Christophera5b1e682010-09-17 22:28:18 +0000882
Eric Christophere5734102010-09-03 00:35:47 +0000883 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000884 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000885 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000886 FastEmitBranch(FBB, DL);
887 FuncInfo.MBB->addSuccessor(TBB);
Eric Christophera5b1e682010-09-17 22:28:18 +0000888 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000889}
890
Eric Christopher43b62be2010-09-27 06:02:23 +0000891bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000892 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000893
Eric Christopherd43393a2010-09-08 23:13:45 +0000894 EVT VT;
895 const Type *Ty = CI->getOperand(0)->getType();
896 if (!isTypeLegal(Ty, VT))
897 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000898
Eric Christopherd43393a2010-09-08 23:13:45 +0000899 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
900 if (isFloat && !Subtarget->hasVFP2())
901 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000902
Eric Christopherd43393a2010-09-08 23:13:45 +0000903 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000904 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +0000905 switch (VT.getSimpleVT().SimpleTy) {
906 default: return false;
907 // TODO: Verify compares.
908 case MVT::f32:
909 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +0000910 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000911 break;
912 case MVT::f64:
913 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +0000914 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000915 break;
916 case MVT::i32:
917 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +0000918 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000919 break;
920 }
921
Eric Christopher229207a2010-09-29 01:14:47 +0000922 // Get the compare predicate.
923 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
924
925 // We may not handle every CC for now.
926 if (ARMPred == ARMCC::AL) return false;
927
Eric Christopherd43393a2010-09-08 23:13:45 +0000928 unsigned Arg1 = getRegForValue(CI->getOperand(0));
929 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000930
Eric Christopherd43393a2010-09-08 23:13:45 +0000931 unsigned Arg2 = getRegForValue(CI->getOperand(1));
932 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000933
Eric Christopherd43393a2010-09-08 23:13:45 +0000934 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
935 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000936
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000937 // For floating point we need to move the result to a comparison register
938 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000939 if (isFloat)
940 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
941 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000942
Eric Christopher229207a2010-09-29 01:14:47 +0000943 // Now set a register based on the comparison. Explicitly set the predicates
944 // here.
Eric Christopher338c2532010-10-07 05:31:49 +0000945 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopher5d18d922010-10-07 05:39:19 +0000946 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
947 : ARM::GPRRegisterClass;
948 unsigned DestReg = createResultReg(RC);
Eric Christopher229207a2010-09-29 01:14:47 +0000949 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +0000950 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +0000951 unsigned ZeroReg = TargetMaterializeConstant(Zero);
952 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
953 .addReg(ZeroReg).addImm(1)
954 .addImm(ARMPred).addReg(CondReg);
955
Eric Christophera5b1e682010-09-17 22:28:18 +0000956 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +0000957 return true;
958}
959
Eric Christopher43b62be2010-09-27 06:02:23 +0000960bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +0000961 // Make sure we have VFP and that we're extending float to double.
962 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000963
Eric Christopher46203602010-09-09 00:26:48 +0000964 Value *V = I->getOperand(0);
965 if (!I->getType()->isDoubleTy() ||
966 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000967
Eric Christopher46203602010-09-09 00:26:48 +0000968 unsigned Op = getRegForValue(V);
969 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000970
Eric Christopher46203602010-09-09 00:26:48 +0000971 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000972 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000973 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000974 .addReg(Op));
975 UpdateValueMap(I, Result);
976 return true;
977}
978
Eric Christopher43b62be2010-09-27 06:02:23 +0000979bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +0000980 // Make sure we have VFP and that we're truncating double to float.
981 if (!Subtarget->hasVFP2()) return false;
982
983 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +0000984 if (!(I->getType()->isFloatTy() &&
985 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +0000986
987 unsigned Op = getRegForValue(V);
988 if (Op == 0) return false;
989
990 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +0000991 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000992 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000993 .addReg(Op));
994 UpdateValueMap(I, Result);
995 return true;
996}
997
Eric Christopher43b62be2010-09-27 06:02:23 +0000998bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +0000999 // Make sure we have VFP.
1000 if (!Subtarget->hasVFP2()) return false;
1001
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001002 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001003 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001004 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001005 return false;
1006
1007 unsigned Op = getRegForValue(I->getOperand(0));
1008 if (Op == 0) return false;
1009
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001010 // The conversion routine works on fp-reg to fp-reg and the operand above
1011 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001012 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001013 if (FP == 0) return false;
1014
Eric Christopher9a040492010-09-09 18:54:59 +00001015 unsigned Opc;
1016 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1017 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1018 else return 0;
1019
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001020 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001021 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1022 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001023 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001024 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001025 return true;
1026}
1027
Eric Christopher43b62be2010-09-27 06:02:23 +00001028bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001029 // Make sure we have VFP.
1030 if (!Subtarget->hasVFP2()) return false;
1031
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001032 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001033 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001034 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001035 return false;
1036
1037 unsigned Op = getRegForValue(I->getOperand(0));
1038 if (Op == 0) return false;
1039
1040 unsigned Opc;
1041 const Type *OpTy = I->getOperand(0)->getType();
1042 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1043 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1044 else return 0;
1045
Eric Christopher022b7fb2010-10-05 23:13:24 +00001046 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1047 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001048 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1049 ResultReg)
1050 .addReg(Op));
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001051
1052 // This result needs to be in an integer register, but the conversion only
1053 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001054 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001055 if (IntReg == 0) return false;
1056
1057 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001058 return true;
1059}
1060
Eric Christopher08637852010-09-30 22:34:19 +00001061bool ARMFastISel::SelectSDiv(const Instruction *I) {
1062 EVT VT;
1063 const Type *Ty = I->getType();
1064 if (!isTypeLegal(Ty, VT))
1065 return false;
1066
1067 // If we have integer div support we should have selected this automagically.
1068 // In case we have a real miss go ahead and return false and we'll pick
1069 // it up later.
1070 if (Subtarget->hasDivide()) return false;
1071
1072 // Otherwise emit a libcall.
1073 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1074 if (VT == MVT::i16)
1075 LC = RTLIB::SDIV_I16;
1076 else if (VT == MVT::i32)
1077 LC = RTLIB::SDIV_I32;
1078 else if (VT == MVT::i64)
1079 LC = RTLIB::SDIV_I64;
1080 else if (VT == MVT::i128)
1081 LC = RTLIB::SDIV_I128;
1082 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1083
1084 return ARMEmitLibcall(I, LC);
1085}
1086
Eric Christopher43b62be2010-09-27 06:02:23 +00001087bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001088 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001089
Eric Christopherbc39b822010-09-09 00:53:57 +00001090 // We can get here in the case when we want to use NEON for our fp
1091 // operations, but can't figure out how to. Just use the vfp instructions
1092 // if we have them.
1093 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001094 const Type *Ty = I->getType();
1095 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1096 if (isFloat && !Subtarget->hasVFP2())
1097 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001098
Eric Christopherbc39b822010-09-09 00:53:57 +00001099 unsigned Op1 = getRegForValue(I->getOperand(0));
1100 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001101
Eric Christopherbc39b822010-09-09 00:53:57 +00001102 unsigned Op2 = getRegForValue(I->getOperand(1));
1103 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001104
Eric Christopherbc39b822010-09-09 00:53:57 +00001105 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +00001106 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1107 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001108 switch (ISDOpcode) {
1109 default: return false;
1110 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001111 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001112 break;
1113 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001114 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001115 break;
1116 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001117 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001118 break;
1119 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001120 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001121 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1122 TII.get(Opc), ResultReg)
1123 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001124 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001125 return true;
1126}
1127
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001128// Call Handling Code
1129
1130// This is largely taken directly from CCAssignFnForNode - we don't support
1131// varargs in FastISel so that part has been removed.
1132// TODO: We may not support all of this.
1133CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1134 switch (CC) {
1135 default:
1136 llvm_unreachable("Unsupported calling convention");
1137 case CallingConv::C:
1138 case CallingConv::Fast:
1139 // Use target triple & subtarget features to do actual dispatch.
1140 if (Subtarget->isAAPCS_ABI()) {
1141 if (Subtarget->hasVFP2() &&
1142 FloatABIType == FloatABI::Hard)
1143 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1144 else
1145 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1146 } else
1147 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1148 case CallingConv::ARM_AAPCS_VFP:
1149 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1150 case CallingConv::ARM_AAPCS:
1151 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1152 case CallingConv::ARM_APCS:
1153 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1154 }
1155}
1156
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001157bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1158 SmallVectorImpl<unsigned> &ArgRegs,
1159 SmallVectorImpl<EVT> &ArgVTs,
1160 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1161 SmallVectorImpl<unsigned> &RegArgs,
1162 CallingConv::ID CC,
1163 unsigned &NumBytes) {
1164 SmallVector<CCValAssign, 16> ArgLocs;
1165 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1166 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1167
1168 // Get a count of how many bytes are to be pushed on the stack.
1169 NumBytes = CCInfo.getNextStackOffset();
1170
1171 // Issue CALLSEQ_START
1172 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1173 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1174 .addImm(NumBytes);
1175
1176 // Process the args.
1177 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1178 CCValAssign &VA = ArgLocs[i];
1179 unsigned Arg = ArgRegs[VA.getValNo()];
1180 EVT ArgVT = ArgVTs[VA.getValNo()];
1181
Eric Christopherf9764fa2010-09-30 20:49:44 +00001182 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001183 switch (VA.getLocInfo()) {
1184 case CCValAssign::Full: break;
1185 default:
Eric Christopher11077342010-10-07 05:14:08 +00001186 // TODO: Handle arg promotion.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001187 return false;
1188 }
1189
1190 // Now copy/store arg to correct locations.
1191 if (VA.isRegLoc()) {
1192 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001193 VA.getLocReg())
1194 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001195 RegArgs.push_back(VA.getLocReg());
1196 } else {
1197 // Need to store
1198 return false;
1199 }
1200 }
1201
1202 return true;
1203}
1204
1205bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1206 const Instruction *I, CallingConv::ID CC,
1207 unsigned &NumBytes) {
1208 // Issue CALLSEQ_END
1209 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1210 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1211 .addImm(NumBytes).addImm(0);
1212
1213 // Now the return value.
1214 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1215 SmallVector<CCValAssign, 16> RVLocs;
1216 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1217 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1218
1219 // Copy all of the result registers out of their specified physreg.
Eric Christopher14df8822010-10-01 00:00:11 +00001220 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1221 // For this move we copy into two registers and then move into the
1222 // double fp reg we want.
1223 // TODO: Are the copies necessary?
1224 TargetRegisterClass *CopyRC = TLI.getRegClassFor(MVT::i32);
1225 unsigned Copy1 = createResultReg(CopyRC);
1226 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1227 Copy1).addReg(RVLocs[0].getLocReg());
1228 UsedRegs.push_back(RVLocs[0].getLocReg());
1229
1230 unsigned Copy2 = createResultReg(CopyRC);
1231 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1232 Copy2).addReg(RVLocs[1].getLocReg());
1233 UsedRegs.push_back(RVLocs[1].getLocReg());
1234
1235 EVT DestVT = RVLocs[0].getValVT();
1236 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1237 unsigned ResultReg = createResultReg(DstRC);
1238 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1239 TII.get(ARM::VMOVDRR), ResultReg)
1240 .addReg(Copy1).addReg(Copy2));
1241
1242 // Finally update the result.
1243 UpdateValueMap(I, ResultReg);
1244 } else {
1245 assert(RVLocs.size() == 1 && "Can't handle non-double multi-reg retvals!");
1246 EVT CopyVT = RVLocs[0].getValVT();
1247 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001248
Eric Christopher14df8822010-10-01 00:00:11 +00001249 unsigned ResultReg = createResultReg(DstRC);
1250 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1251 ResultReg).addReg(RVLocs[0].getLocReg());
1252 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001253
Eric Christopher14df8822010-10-01 00:00:11 +00001254 // Finally update the result.
1255 UpdateValueMap(I, ResultReg);
1256 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001257 }
1258
1259 return true;
1260}
1261
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001262// A quick function that will emit a call for a named libcall in F with the
1263// vector of passed arguments for the Instruction in I. We can assume that we
1264// can emit a call for any libcall we can produce. This is an abridged version
1265// of the full call infrastructure since we won't need to worry about things
1266// like computed function pointers or strange arguments at call sites.
1267// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1268// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001269bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1270 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1271
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001272 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001273 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001274 EVT RetVT;
1275 if (RetTy->isVoidTy())
1276 RetVT = MVT::isVoid;
1277 else if (!isTypeLegal(RetTy, RetVT))
1278 return false;
1279
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001280 // For now we're using BLX etc on the assumption that we have v5t ops.
1281 if (!Subtarget->hasV5TOps()) return false;
1282
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001283 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001284 SmallVector<Value*, 8> Args;
1285 SmallVector<unsigned, 8> ArgRegs;
1286 SmallVector<EVT, 8> ArgVTs;
1287 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1288 Args.reserve(I->getNumOperands());
1289 ArgRegs.reserve(I->getNumOperands());
1290 ArgVTs.reserve(I->getNumOperands());
1291 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001292 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001293 Value *Op = I->getOperand(i);
1294 unsigned Arg = getRegForValue(Op);
1295 if (Arg == 0) return false;
1296
1297 const Type *ArgTy = Op->getType();
1298 EVT ArgVT;
1299 if (!isTypeLegal(ArgTy, ArgVT)) return false;
1300
1301 ISD::ArgFlagsTy Flags;
1302 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1303 Flags.setOrigAlign(OriginalAlignment);
1304
1305 Args.push_back(Op);
1306 ArgRegs.push_back(Arg);
1307 ArgVTs.push_back(ArgVT);
1308 ArgFlags.push_back(Flags);
1309 }
1310
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001311 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001312 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001313 unsigned NumBytes;
1314 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1315 return false;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001316
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001317 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1318 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001319 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001320 unsigned CallOpc;
1321 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001322 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001323 else
1324 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001325 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001326 .addExternalSymbol(TLI.getLibcallName(Call));
1327
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001328 // Add implicit physical register uses to the call.
1329 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1330 MIB.addReg(RegArgs[i]);
1331
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001332 // Finish off the call including any return values.
1333 SmallVector<unsigned, 4> UsedRegs;
1334 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001335
1336 // Set all unused physreg defs as dead.
1337 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001338
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001339 return true;
1340}
1341
Eric Christopherf9764fa2010-09-30 20:49:44 +00001342bool ARMFastISel::SelectCall(const Instruction *I) {
1343 const CallInst *CI = cast<CallInst>(I);
1344 const Value *Callee = CI->getCalledValue();
1345
1346 // Can't handle inline asm or worry about intrinsics yet.
1347 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1348
Eric Christophere6ca6772010-10-01 21:33:12 +00001349 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001350 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001351 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1352 return false;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001353
1354 // Check the calling convention.
1355 ImmutableCallSite CS(CI);
1356 CallingConv::ID CC = CS.getCallingConv();
1357 // TODO: Avoid some calling conventions?
1358 if (CC != CallingConv::C) {
Eric Christophere540a6f2010-10-05 23:50:58 +00001359 // errs() << "Can't handle calling convention: " << CC << "\n";
Eric Christopherf9764fa2010-09-30 20:49:44 +00001360 return false;
1361 }
1362
1363 // Let SDISel handle vararg functions.
1364 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1365 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1366 if (FTy->isVarArg())
1367 return false;
1368
1369 // Handle *simple* calls for now.
1370 const Type *RetTy = I->getType();
1371 EVT RetVT;
1372 if (RetTy->isVoidTy())
1373 RetVT = MVT::isVoid;
1374 else if (!isTypeLegal(RetTy, RetVT))
1375 return false;
1376
1377 // For now we're using BLX etc on the assumption that we have v5t ops.
1378 // TODO: Maybe?
1379 if (!Subtarget->hasV5TOps()) return false;
1380
1381 // Set up the argument vectors.
1382 SmallVector<Value*, 8> Args;
1383 SmallVector<unsigned, 8> ArgRegs;
1384 SmallVector<EVT, 8> ArgVTs;
1385 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1386 Args.reserve(CS.arg_size());
1387 ArgRegs.reserve(CS.arg_size());
1388 ArgVTs.reserve(CS.arg_size());
1389 ArgFlags.reserve(CS.arg_size());
1390 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1391 i != e; ++i) {
1392 unsigned Arg = getRegForValue(*i);
1393
1394 if (Arg == 0)
1395 return false;
1396 ISD::ArgFlagsTy Flags;
1397 unsigned AttrInd = i - CS.arg_begin() + 1;
1398 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1399 Flags.setSExt();
1400 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1401 Flags.setZExt();
1402
1403 // FIXME: Only handle *easy* calls for now.
1404 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1405 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1406 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1407 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1408 return false;
1409
1410 const Type *ArgTy = (*i)->getType();
1411 EVT ArgVT;
1412 if (!isTypeLegal(ArgTy, ArgVT))
1413 return false;
1414 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1415 Flags.setOrigAlign(OriginalAlignment);
1416
1417 Args.push_back(*i);
1418 ArgRegs.push_back(Arg);
1419 ArgVTs.push_back(ArgVT);
1420 ArgFlags.push_back(Flags);
1421 }
1422
1423 // Handle the arguments now that we've gotten them.
1424 SmallVector<unsigned, 4> RegArgs;
1425 unsigned NumBytes;
1426 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1427 return false;
1428
1429 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1430 // TODO: Turn this into the table of arm call ops.
1431 MachineInstrBuilder MIB;
1432 unsigned CallOpc;
1433 if(isThumb)
1434 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1435 else
1436 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1437 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1438 .addGlobalAddress(GV, 0, 0);
1439
1440 // Add implicit physical register uses to the call.
1441 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1442 MIB.addReg(RegArgs[i]);
1443
1444 // Finish off the call including any return values.
1445 SmallVector<unsigned, 4> UsedRegs;
1446 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1447
1448 // Set all unused physreg defs as dead.
1449 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1450
1451 return true;
1452
1453}
1454
Eric Christopher56d2b722010-09-02 23:43:26 +00001455// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001456bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001457 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001458 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001459
Eric Christopherab695882010-07-21 22:26:11 +00001460 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001461 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001462 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001463 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001464 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001465 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001466 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001467 case Instruction::ICmp:
1468 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001469 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001470 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001471 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001472 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001473 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001474 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001475 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001476 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001477 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001478 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001479 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001480 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001481 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001482 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001483 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001484 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001485 return SelectSDiv(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001486 case Instruction::Call:
1487 return SelectCall(I);
Eric Christopherab695882010-07-21 22:26:11 +00001488 default: break;
1489 }
1490 return false;
1491}
1492
1493namespace llvm {
1494 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +00001495 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001496 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001497 }
1498}