blob: cbfad2dace29163e8133f88e04f3a939d9084da4 [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
Eric Christophera3224252010-10-15 21:32:12 +000057 typedef struct AddrBase {
58 unsigned Reg;
59 unsigned FrameIndex;
60 } AddrBase;
61
Eric Christopherab695882010-07-21 22:26:11 +000062 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
63 /// make the right decision when generating code for different targets.
64 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000065 const TargetMachine &TM;
66 const TargetInstrInfo &TII;
67 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000068 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000069
Eric Christopher8cf6c602010-09-29 22:24:45 +000070 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000071 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000072 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000073
Eric Christopherab695882010-07-21 22:26:11 +000074 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000075 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000076 : FastISel(funcInfo),
77 TM(funcInfo.MF->getTarget()),
78 TII(*TM.getInstrInfo()),
79 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000080 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000081 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000082 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000083 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000084 }
85
Eric Christophercb592292010-08-20 00:20:31 +000086 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000087 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
88 const TargetRegisterClass *RC);
89 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
90 const TargetRegisterClass *RC,
91 unsigned Op0, bool Op0IsKill);
92 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
93 const TargetRegisterClass *RC,
94 unsigned Op0, bool Op0IsKill,
95 unsigned Op1, bool Op1IsKill);
96 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
97 const TargetRegisterClass *RC,
98 unsigned Op0, bool Op0IsKill,
99 uint64_t Imm);
100 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
101 const TargetRegisterClass *RC,
102 unsigned Op0, bool Op0IsKill,
103 const ConstantFP *FPImm);
104 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
105 const TargetRegisterClass *RC,
106 uint64_t Imm);
107 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
108 const TargetRegisterClass *RC,
109 unsigned Op0, bool Op0IsKill,
110 unsigned Op1, bool Op1IsKill,
111 uint64_t Imm);
112 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
113 unsigned Op0, bool Op0IsKill,
114 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000115
Eric Christophercb592292010-08-20 00:20:31 +0000116 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000117 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000118 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000119 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000120
121 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000122
Eric Christopher83007122010-08-23 21:44:12 +0000123 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000124 private:
Eric Christopher43b62be2010-09-27 06:02:23 +0000125 virtual bool SelectLoad(const Instruction *I);
126 virtual bool SelectStore(const Instruction *I);
127 virtual bool SelectBranch(const Instruction *I);
128 virtual bool SelectCmp(const Instruction *I);
129 virtual bool SelectFPExt(const Instruction *I);
130 virtual bool SelectFPTrunc(const Instruction *I);
131 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
132 virtual bool SelectSIToFP(const Instruction *I);
133 virtual bool SelectFPToSI(const Instruction *I);
134 virtual bool SelectSDiv(const Instruction *I);
Eric Christopher6a880d62010-10-11 08:37:26 +0000135 virtual bool SelectSRem(const Instruction *I);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000136 virtual bool SelectCall(const Instruction *I);
Eric Christopher3bbd3962010-10-11 08:27:59 +0000137 virtual bool SelectSelect(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000138
Eric Christopher83007122010-08-23 21:44:12 +0000139 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000140 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000141 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000142 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christophera3224252010-10-15 21:32:12 +0000143 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, AddrBase Base, int Offset);
144 bool ARMEmitStore(EVT VT, unsigned SrcReg, AddrBase Base, int Offset);
145 bool ARMComputeRegOffset(const Value *Obj, AddrBase &Base, int &Offset);
146 void ARMSimplifyRegOffset(AddrBase &Base, int &Offset, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000147 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000148 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000149 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000150 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000151 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000152
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000153 // Call handling routines.
154 private:
155 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000156 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000157 SmallVectorImpl<unsigned> &ArgRegs,
158 SmallVectorImpl<EVT> &ArgVTs,
159 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
160 SmallVectorImpl<unsigned> &RegArgs,
161 CallingConv::ID CC,
162 unsigned &NumBytes);
163 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
164 const Instruction *I, CallingConv::ID CC,
165 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000166 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000167
168 // OptionalDef handling routines.
169 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000170 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
171 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
172};
Eric Christopherab695882010-07-21 22:26:11 +0000173
174} // end anonymous namespace
175
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000176#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000177
Eric Christopher456144e2010-08-19 00:37:05 +0000178// DefinesOptionalPredicate - This is different from DefinesPredicate in that
179// we don't care about implicit defs here, just places we'll need to add a
180// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
181bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
182 const TargetInstrDesc &TID = MI->getDesc();
183 if (!TID.hasOptionalDef())
184 return false;
185
186 // Look to see if our OptionalDef is defining CPSR or CCR.
187 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
188 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000189 if (!MO.isReg() || !MO.isDef()) continue;
190 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000191 *CPSR = true;
192 }
193 return true;
194}
195
196// If the machine is predicable go ahead and add the predicate operands, if
197// it needs default CC operands add those.
198const MachineInstrBuilder &
199ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
200 MachineInstr *MI = &*MIB;
201
202 // Do we use a predicate?
203 if (TII.isPredicable(MI))
204 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000205
Eric Christopher456144e2010-08-19 00:37:05 +0000206 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
207 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000208 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000209 if (DefinesOptionalPredicate(MI, &CPSR)) {
210 if (CPSR)
211 AddDefaultT1CC(MIB);
212 else
213 AddDefaultCC(MIB);
214 }
215 return MIB;
216}
217
Eric Christopher0fe7d542010-08-17 01:25:29 +0000218unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
219 const TargetRegisterClass* RC) {
220 unsigned ResultReg = createResultReg(RC);
221 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
222
Eric Christopher456144e2010-08-19 00:37:05 +0000223 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000224 return ResultReg;
225}
226
227unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
228 const TargetRegisterClass *RC,
229 unsigned Op0, bool Op0IsKill) {
230 unsigned ResultReg = createResultReg(RC);
231 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
232
233 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000234 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000235 .addReg(Op0, Op0IsKill * RegState::Kill));
236 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000237 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000238 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000239 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000240 TII.get(TargetOpcode::COPY), ResultReg)
241 .addReg(II.ImplicitDefs[0]));
242 }
243 return ResultReg;
244}
245
246unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
247 const TargetRegisterClass *RC,
248 unsigned Op0, bool Op0IsKill,
249 unsigned Op1, bool Op1IsKill) {
250 unsigned ResultReg = createResultReg(RC);
251 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
252
253 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000254 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000255 .addReg(Op0, Op0IsKill * RegState::Kill)
256 .addReg(Op1, Op1IsKill * RegState::Kill));
257 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000258 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000259 .addReg(Op0, Op0IsKill * RegState::Kill)
260 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000261 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000262 TII.get(TargetOpcode::COPY), ResultReg)
263 .addReg(II.ImplicitDefs[0]));
264 }
265 return ResultReg;
266}
267
268unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
269 const TargetRegisterClass *RC,
270 unsigned Op0, bool Op0IsKill,
271 uint64_t Imm) {
272 unsigned ResultReg = createResultReg(RC);
273 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
274
275 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000276 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000277 .addReg(Op0, Op0IsKill * RegState::Kill)
278 .addImm(Imm));
279 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000280 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000281 .addReg(Op0, Op0IsKill * RegState::Kill)
282 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000284 TII.get(TargetOpcode::COPY), ResultReg)
285 .addReg(II.ImplicitDefs[0]));
286 }
287 return ResultReg;
288}
289
290unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
291 const TargetRegisterClass *RC,
292 unsigned Op0, bool Op0IsKill,
293 const ConstantFP *FPImm) {
294 unsigned ResultReg = createResultReg(RC);
295 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
296
297 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000298 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000299 .addReg(Op0, Op0IsKill * RegState::Kill)
300 .addFPImm(FPImm));
301 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000302 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000303 .addReg(Op0, Op0IsKill * RegState::Kill)
304 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000306 TII.get(TargetOpcode::COPY), ResultReg)
307 .addReg(II.ImplicitDefs[0]));
308 }
309 return ResultReg;
310}
311
312unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
313 const TargetRegisterClass *RC,
314 unsigned Op0, bool Op0IsKill,
315 unsigned Op1, bool Op1IsKill,
316 uint64_t Imm) {
317 unsigned ResultReg = createResultReg(RC);
318 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
319
320 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000321 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000322 .addReg(Op0, Op0IsKill * RegState::Kill)
323 .addReg(Op1, Op1IsKill * RegState::Kill)
324 .addImm(Imm));
325 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000327 .addReg(Op0, Op0IsKill * RegState::Kill)
328 .addReg(Op1, Op1IsKill * RegState::Kill)
329 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000331 TII.get(TargetOpcode::COPY), ResultReg)
332 .addReg(II.ImplicitDefs[0]));
333 }
334 return ResultReg;
335}
336
337unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
338 const TargetRegisterClass *RC,
339 uint64_t Imm) {
340 unsigned ResultReg = createResultReg(RC);
341 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000342
Eric Christopher0fe7d542010-08-17 01:25:29 +0000343 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000344 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000345 .addImm(Imm));
346 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000348 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000350 TII.get(TargetOpcode::COPY), ResultReg)
351 .addReg(II.ImplicitDefs[0]));
352 }
353 return ResultReg;
354}
355
356unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
357 unsigned Op0, bool Op0IsKill,
358 uint32_t Idx) {
359 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
360 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
361 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000362 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000363 DL, TII.get(TargetOpcode::COPY), ResultReg)
364 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
365 return ResultReg;
366}
367
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000368// TODO: Don't worry about 64-bit now, but when this is fixed remove the
369// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000370unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000371 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000372
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000373 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
375 TII.get(ARM::VMOVRS), MoveReg)
376 .addReg(SrcReg));
377 return MoveReg;
378}
379
380unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000381 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000382
Eric Christopheraa3ace12010-09-09 20:49:25 +0000383 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
384 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000385 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000386 .addReg(SrcReg));
387 return MoveReg;
388}
389
Eric Christopher9ed58df2010-09-09 00:19:41 +0000390// For double width floating point we need to materialize two constants
391// (the high and the low) into integer registers then use a move to get
392// the combined constant into an FP reg.
393unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
394 const APFloat Val = CFP->getValueAPF();
395 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000396
Eric Christopher9ed58df2010-09-09 00:19:41 +0000397 // This checks to see if we can use VFP3 instructions to materialize
398 // a constant, otherwise we have to go through the constant pool.
399 if (TLI.isFPImmLegal(Val, VT)) {
400 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
401 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
402 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
403 DestReg)
404 .addFPImm(CFP));
405 return DestReg;
406 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000407
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000408 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000409 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000410
Eric Christopher238bb162010-09-09 23:50:00 +0000411 // MachineConstantPool wants an explicit alignment.
412 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
413 if (Align == 0) {
414 // TODO: Figure out if this is correct.
415 Align = TD.getTypeAllocSize(CFP->getType());
416 }
417 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
418 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
419 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000420
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000421 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000422 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
423 DestReg)
424 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000425 .addReg(0));
426 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000427}
428
Eric Christopher744c7c82010-09-28 22:47:54 +0000429unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000430
Eric Christopher744c7c82010-09-28 22:47:54 +0000431 // For now 32-bit only.
432 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000433
Eric Christopher56d2b722010-09-02 23:43:26 +0000434 // MachineConstantPool wants an explicit alignment.
435 unsigned Align = TD.getPrefTypeAlignment(C->getType());
436 if (Align == 0) {
437 // TODO: Figure out if this is correct.
438 Align = TD.getTypeAllocSize(C->getType());
439 }
440 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000441 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000442
Eric Christopher56d2b722010-09-02 23:43:26 +0000443 if (isThumb)
444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000445 TII.get(ARM::t2LDRpci), DestReg)
446 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000447 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000448 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000449 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000450 TII.get(ARM::LDRcp), DestReg)
451 .addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000452 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000453
Eric Christopher56d2b722010-09-02 23:43:26 +0000454 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000455}
456
Eric Christopherc9932f62010-10-01 23:24:42 +0000457unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000458 // For now 32-bit only.
459 if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000460
Eric Christopher890dbbe2010-10-02 00:32:44 +0000461 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000462
Eric Christopher890dbbe2010-10-02 00:32:44 +0000463 // TODO: No external globals for now.
464 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000465
Eric Christopher890dbbe2010-10-02 00:32:44 +0000466 // TODO: Need more magic for ARM PIC.
467 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000468
Eric Christopher890dbbe2010-10-02 00:32:44 +0000469 // MachineConstantPool wants an explicit alignment.
470 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
471 if (Align == 0) {
472 // TODO: Figure out if this is correct.
473 Align = TD.getTypeAllocSize(GV->getType());
474 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000475
Eric Christopher890dbbe2010-10-02 00:32:44 +0000476 // Grab index.
477 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
478 unsigned Id = AFI->createConstPoolEntryUId();
479 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
480 ARMCP::CPValue, PCAdj);
481 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000482
Eric Christopher890dbbe2010-10-02 00:32:44 +0000483 // Load value.
484 MachineInstrBuilder MIB;
485 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
486 if (isThumb) {
487 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
488 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
489 .addConstantPoolIndex(Idx);
490 if (RelocM == Reloc::PIC_)
491 MIB.addImm(Id);
492 } else {
493 // The extra reg and immediate are for addrmode2.
494 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
495 DestReg)
496 .addConstantPoolIndex(Idx)
497 .addReg(0).addImm(0);
498 }
499 AddOptionalDefs(MIB);
500 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000501}
502
Eric Christopher9ed58df2010-09-09 00:19:41 +0000503unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
504 EVT VT = TLI.getValueType(C->getType(), true);
505
506 // Only handle simple types.
507 if (!VT.isSimple()) return 0;
508
509 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
510 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000511 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
512 return ARMMaterializeGV(GV, VT);
513 else if (isa<ConstantInt>(C))
514 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000515
Eric Christopherc9932f62010-10-01 23:24:42 +0000516 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000517}
518
Eric Christopherf9764fa2010-09-30 20:49:44 +0000519unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
520 // Don't handle dynamic allocas.
521 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000522
Eric Christopherf9764fa2010-09-30 20:49:44 +0000523 EVT VT;
524 if (!isTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000525
Eric Christopherf9764fa2010-09-30 20:49:44 +0000526 DenseMap<const AllocaInst*, int>::iterator SI =
527 FuncInfo.StaticAllocaMap.find(AI);
528
529 // This will get lowered later into the correct offsets and registers
530 // via rewriteXFrameIndex.
531 if (SI != FuncInfo.StaticAllocaMap.end()) {
532 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
533 unsigned ResultReg = createResultReg(RC);
534 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
535 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
536 TII.get(Opc), ResultReg)
537 .addFrameIndex(SI->second)
538 .addImm(0));
539 return ResultReg;
540 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000541
Eric Christopherf9764fa2010-09-30 20:49:44 +0000542 return 0;
543}
544
Eric Christopherb1cc8482010-08-25 07:23:49 +0000545bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
546 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000547
Eric Christopherb1cc8482010-08-25 07:23:49 +0000548 // Only handle simple types.
549 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000550
Eric Christopherdc908042010-08-31 01:28:42 +0000551 // Handle all legal types, i.e. a register that will directly hold this
552 // value.
553 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000554}
555
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000556bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
557 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000558
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000559 // If this is a type than can be sign or zero-extended to a basic operation
560 // go ahead and accept it now.
561 if (VT == MVT::i8 || VT == MVT::i16)
562 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000563
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000564 return false;
565}
566
Eric Christophercb0b04b2010-08-24 00:07:24 +0000567// Computes the Reg+Offset to get to an object.
Eric Christophera3224252010-10-15 21:32:12 +0000568bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, AddrBase &Base,
Eric Christopher83007122010-08-23 21:44:12 +0000569 int &Offset) {
570 // Some boilerplate from the X86 FastISel.
571 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000572 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000573 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000574 // Don't walk into other basic blocks; it's possible we haven't
575 // visited them yet, so the instructions may not yet be assigned
576 // virtual registers.
577 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
578 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000579 Opcode = I->getOpcode();
580 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000581 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000582 Opcode = C->getOpcode();
583 U = C;
584 }
585
Eric Christophercb0b04b2010-08-24 00:07:24 +0000586 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000587 if (Ty->getAddressSpace() > 255)
588 // Fast instruction selection doesn't support the special
589 // address spaces.
590 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000591
Eric Christopher83007122010-08-23 21:44:12 +0000592 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000593 default:
Eric Christopher83007122010-08-23 21:44:12 +0000594 break;
Eric Christopher55324332010-10-12 00:43:21 +0000595 case Instruction::BitCast: {
596 // Look through bitcasts.
Eric Christophera3224252010-10-15 21:32:12 +0000597 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000598 }
599 case Instruction::IntToPtr: {
600 // Look past no-op inttoptrs.
601 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000602 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000603 break;
604 }
605 case Instruction::PtrToInt: {
606 // Look past no-op ptrtoints.
607 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000608 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000609 break;
610 }
Eric Christophereae84392010-10-14 09:29:41 +0000611 case Instruction::GetElementPtr: {
612 int SavedOffset = Offset;
Eric Christophera3224252010-10-15 21:32:12 +0000613 AddrBase SavedBase = Base;
Eric Christophereae84392010-10-14 09:29:41 +0000614 int TmpOffset = Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000615
Eric Christophereae84392010-10-14 09:29:41 +0000616 // Iterate through the GEP folding the constants into offsets where
617 // we can.
618 gep_type_iterator GTI = gep_type_begin(U);
619 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
620 i != e; ++i, ++GTI) {
621 const Value *Op = *i;
622 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
623 const StructLayout *SL = TD.getStructLayout(STy);
624 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
625 TmpOffset += SL->getElementOffset(Idx);
626 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000627 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
628 SmallVector<const Value *, 4> Worklist;
629 Worklist.push_back(Op);
630 do {
631 Op = Worklist.pop_back_val();
632 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
633 // Constant-offset addressing.
634 TmpOffset += CI->getSExtValue() * S;
635 } else if (0 && isa<AddOperator>(Op) &&
636 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
637 // An add with a constant operand. Fold the constant.
638 ConstantInt *CI =
639 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
640 TmpOffset += CI->getSExtValue() * S;
641 // Add the other operand back to the work list.
642 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
643 } else
644 goto unsupported_gep;
645 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000646 }
647 }
Eric Christopher2896df82010-10-15 18:02:07 +0000648
649 // Try to grab the base operand now.
Eric Christophereae84392010-10-14 09:29:41 +0000650 Offset = TmpOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000651 if (ARMComputeRegOffset(U->getOperand(0), Base, Offset)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000652
653 // We failed, restore everything and try the other options.
Eric Christophereae84392010-10-14 09:29:41 +0000654 Offset = SavedOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000655 Base = SavedBase;
Eric Christopher2896df82010-10-15 18:02:07 +0000656
Eric Christophereae84392010-10-14 09:29:41 +0000657 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000658 break;
659 }
Eric Christopher83007122010-08-23 21:44:12 +0000660 case Instruction::Alloca: {
Eric Christophereae84392010-10-14 09:29:41 +0000661 // TODO: Fix this to do intermediate loads, etc.
662 if (Offset != 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000663
Eric Christopher15418772010-10-12 05:39:06 +0000664 const AllocaInst *AI = cast<AllocaInst>(Obj);
665 DenseMap<const AllocaInst*, int>::iterator SI =
666 FuncInfo.StaticAllocaMap.find(AI);
667 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christophera3224252010-10-15 21:32:12 +0000668 Base.Reg = ARM::SP;
669 Base.FrameIndex = SI->second;
Eric Christopher15418772010-10-12 05:39:06 +0000670 return true;
671 }
Eric Christopher050d16c2010-10-11 21:37:35 +0000672 // Don't handle dynamic allocas.
Eric Christopher5f9e8b92010-10-11 22:01:22 +0000673 assert(!FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Obj)) &&
674 "Alloca should have been handled earlier!");
Eric Christopherf06f3092010-08-24 00:50:47 +0000675 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000676 }
677 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000678
Eric Christophera9c57512010-10-13 21:41:51 +0000679 // Materialize the global variable's address into a reg which can
680 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000681 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000682 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
683 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000684
Eric Christophera3224252010-10-15 21:32:12 +0000685 Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000686 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000687 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000688
Eric Christophercb0b04b2010-08-24 00:07:24 +0000689 // Try to get this in a register if nothing else has worked.
Eric Christophera3224252010-10-15 21:32:12 +0000690 if (Base.Reg == 0) Base.Reg = getRegForValue(Obj);
691 return Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000692}
693
Eric Christophera3224252010-10-15 21:32:12 +0000694void ARMFastISel::ARMSimplifyRegOffset(AddrBase &Base, int &Offset, EVT VT) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000695
696 // Since the offset may be too large for the load instruction
697 // get the reg+offset into a register.
Eric Christophera3224252010-10-15 21:32:12 +0000698 if (Base.Reg != ARM::SP && Offset != 0) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000699 ARMCC::CondCodes Pred = ARMCC::AL;
700 unsigned PredReg = 0;
701
Eric Christopher2896df82010-10-15 18:02:07 +0000702 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
703 ARM::GPRRegisterClass;
704 unsigned BaseReg = createResultReg(RC);
705
Eric Christophereaa204b2010-09-02 01:39:14 +0000706 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000707 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christophera3224252010-10-15 21:32:12 +0000708 BaseReg, Base.Reg, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000709 static_cast<const ARMBaseInstrInfo&>(TII));
710 else {
711 assert(AFI->isThumb2Function());
712 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christophera3224252010-10-15 21:32:12 +0000713 BaseReg, Base.Reg, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000714 static_cast<const ARMBaseInstrInfo&>(TII));
715 }
Eric Christophereae84392010-10-14 09:29:41 +0000716 Offset = 0;
Eric Christophera3224252010-10-15 21:32:12 +0000717 Base.Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000718 }
Eric Christopher83007122010-08-23 21:44:12 +0000719}
720
Eric Christopherb1cc8482010-08-25 07:23:49 +0000721bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
Eric Christophera3224252010-10-15 21:32:12 +0000722 AddrBase Base, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000723
Eric Christopherb1cc8482010-08-25 07:23:49 +0000724 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000725 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000726 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000727 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000728 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000729 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000730 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000731 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000732 case MVT::i16:
Eric Christopher7a56f332010-10-08 01:13:17 +0000733 Opc = isThumb ? ARM::t2LDRHi8 : ARM::LDRH;
734 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000735 VT = MVT::i32;
736 break;
737 case MVT::i8:
Eric Christopher7a56f332010-10-08 01:13:17 +0000738 Opc = isThumb ? ARM::t2LDRBi8 : ARM::LDRB;
739 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000740 VT = MVT::i32;
741 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000742 case MVT::i32:
Eric Christopher7a56f332010-10-08 01:13:17 +0000743 Opc = isThumb ? ARM::t2LDRi8 : ARM::LDR;
744 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000745 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000746 case MVT::f32:
747 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000748 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000749 isFloat = true;
750 break;
751 case MVT::f64:
752 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000753 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000754 isFloat = true;
755 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000756 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000757
Eric Christopheree56ea62010-10-07 05:50:44 +0000758 ResultReg = createResultReg(RC);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000759
Eric Christopher7a56f332010-10-08 01:13:17 +0000760 // For now with the additions above the offset should be zero - thus we
761 // can always fit into an i8.
Eric Christophera3224252010-10-15 21:32:12 +0000762 assert((Base.Reg == ARM::SP || Offset == 0) &&
Eric Christopher15418772010-10-12 05:39:06 +0000763 "Offset not zero and not a stack load!");
Eric Christopherdccd2c32010-10-11 08:38:55 +0000764
Eric Christopherc9a91fd2010-10-15 23:07:10 +0000765 if (Base.Reg == ARM::SP && Offset == 0)
Eric Christopher15418772010-10-12 05:39:06 +0000766 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christophera3224252010-10-15 21:32:12 +0000767 ResultReg, Base.FrameIndex, RC,
Eric Christopher15418772010-10-12 05:39:06 +0000768 TM.getRegisterInfo());
Eric Christopher7a56f332010-10-08 01:13:17 +0000769 // The thumb and floating point instructions both take 2 operands, ARM takes
770 // another register.
Eric Christopher15418772010-10-12 05:39:06 +0000771 else if (isFloat || isThumb)
Eric Christopher6dab1372010-09-18 01:59:37 +0000772 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
773 TII.get(Opc), ResultReg)
Eric Christophera3224252010-10-15 21:32:12 +0000774 .addReg(Base.Reg).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000775 else
776 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
777 TII.get(Opc), ResultReg)
Eric Christophera3224252010-10-15 21:32:12 +0000778 .addReg(Base.Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000779 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000780}
781
Eric Christopher43b62be2010-09-27 06:02:23 +0000782bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000783 // Verify we have a legal type before going any further.
784 EVT VT;
785 if (!isLoadTypeLegal(I->getType(), VT))
786 return false;
787
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000788 // Our register and offset with innocuous defaults.
Eric Christophera3224252010-10-15 21:32:12 +0000789 AddrBase Base = { 0, 0 };
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000790 int Offset = 0;
791
792 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000793 if (!ARMComputeRegOffset(I->getOperand(0), Base, Offset))
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000794 return false;
795
Eric Christophera3224252010-10-15 21:32:12 +0000796 ARMSimplifyRegOffset(Base, Offset, VT);
Eric Christophereae84392010-10-14 09:29:41 +0000797
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000798 unsigned ResultReg;
Eric Christophera3224252010-10-15 21:32:12 +0000799 if (!ARMEmitLoad(VT, ResultReg, Base, Offset)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000800
801 UpdateValueMap(I, ResultReg);
802 return true;
803}
804
Eric Christopher318b6ee2010-09-02 00:53:56 +0000805bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
Eric Christophera3224252010-10-15 21:32:12 +0000806 AddrBase Base, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000807 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000808 bool isFloat = false;
Eric Christopher15418772010-10-12 05:39:06 +0000809 // VT is set here only for use in the alloca stores below - those are promoted
810 // to reg size always.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000811 switch (VT.getSimpleVT().SimpleTy) {
812 default: return false;
813 case MVT::i1:
Eric Christopher2896df82010-10-15 18:02:07 +0000814 case MVT::i8:
Eric Christopher15418772010-10-12 05:39:06 +0000815 VT = MVT::i32;
816 StrOpc = isThumb ? ARM::t2STRBi8 : ARM::STRB;
817 break;
818 case MVT::i16:
819 VT = MVT::i32;
820 StrOpc = isThumb ? ARM::t2STRHi8 : ARM::STRH;
821 break;
Eric Christophere93417b2010-10-08 23:52:16 +0000822 case MVT::i32: StrOpc = isThumb ? ARM::t2STRi8 : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000823 case MVT::f32:
824 if (!Subtarget->hasVFP2()) return false;
825 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000826 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000827 break;
828 case MVT::f64:
829 if (!Subtarget->hasVFP2()) return false;
830 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000831 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000832 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000833 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000834
Eric Christopherc9a91fd2010-10-15 23:07:10 +0000835 if (Base.Reg == ARM::SP && Offset == 0)
Eric Christopher15418772010-10-12 05:39:06 +0000836 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christophera3224252010-10-15 21:32:12 +0000837 SrcReg, true /*isKill*/, Base.FrameIndex,
Eric Christopher15418772010-10-12 05:39:06 +0000838 TLI.getRegClassFor(VT), TM.getRegisterInfo());
Eric Christopherb74558a2010-09-18 01:23:38 +0000839 // The thumb addressing mode has operands swapped from the arm addressing
840 // mode, the floating point one only has two operands.
Eric Christopher315030c2010-10-15 22:32:37 +0000841 else if (isFloat || isThumb)
Eric Christopherb74558a2010-09-18 01:23:38 +0000842 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000843 TII.get(StrOpc))
Eric Christophera3224252010-10-15 21:32:12 +0000844 .addReg(SrcReg).addReg(Base.Reg).addImm(Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000845 else
846 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000847 TII.get(StrOpc))
Eric Christophera3224252010-10-15 21:32:12 +0000848 .addReg(SrcReg).addReg(Base.Reg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000849
Eric Christopher318b6ee2010-09-02 00:53:56 +0000850 return true;
851}
852
Eric Christopher43b62be2010-09-27 06:02:23 +0000853bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000854 Value *Op0 = I->getOperand(0);
855 unsigned SrcReg = 0;
856
Eric Christopher543cf052010-09-01 22:16:27 +0000857 // Yay type legalization
858 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000859 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000860 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000861
Eric Christopher1b61ef42010-09-02 01:48:11 +0000862 // Get the value to be stored into a register.
863 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000864 if (SrcReg == 0)
865 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000866
Eric Christopher318b6ee2010-09-02 00:53:56 +0000867 // Our register and offset with innocuous defaults.
Eric Christophera3224252010-10-15 21:32:12 +0000868 AddrBase Base = { 0, 0 };
Eric Christopher318b6ee2010-09-02 00:53:56 +0000869 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000870
Eric Christopher318b6ee2010-09-02 00:53:56 +0000871 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000872 if (!ARMComputeRegOffset(I->getOperand(1), Base, Offset))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000873 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000874
Eric Christophera3224252010-10-15 21:32:12 +0000875 ARMSimplifyRegOffset(Base, Offset, VT);
Eric Christophereae84392010-10-14 09:29:41 +0000876
Eric Christophera3224252010-10-15 21:32:12 +0000877 if (!ARMEmitStore(VT, SrcReg, Base, Offset)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000878
Eric Christophera5b1e682010-09-17 22:28:18 +0000879 return true;
880}
881
882static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
883 switch (Pred) {
884 // Needs two compares...
885 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000886 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000887 default:
888 assert(false && "Unhandled CmpInst::Predicate!");
889 return ARMCC::AL;
890 case CmpInst::ICMP_EQ:
891 case CmpInst::FCMP_OEQ:
892 return ARMCC::EQ;
893 case CmpInst::ICMP_SGT:
894 case CmpInst::FCMP_OGT:
895 return ARMCC::GT;
896 case CmpInst::ICMP_SGE:
897 case CmpInst::FCMP_OGE:
898 return ARMCC::GE;
899 case CmpInst::ICMP_UGT:
900 case CmpInst::FCMP_UGT:
901 return ARMCC::HI;
902 case CmpInst::FCMP_OLT:
903 return ARMCC::MI;
904 case CmpInst::ICMP_ULE:
905 case CmpInst::FCMP_OLE:
906 return ARMCC::LS;
907 case CmpInst::FCMP_ORD:
908 return ARMCC::VC;
909 case CmpInst::FCMP_UNO:
910 return ARMCC::VS;
911 case CmpInst::FCMP_UGE:
912 return ARMCC::PL;
913 case CmpInst::ICMP_SLT:
914 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000915 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000916 case CmpInst::ICMP_SLE:
917 case CmpInst::FCMP_ULE:
918 return ARMCC::LE;
919 case CmpInst::FCMP_UNE:
920 case CmpInst::ICMP_NE:
921 return ARMCC::NE;
922 case CmpInst::ICMP_UGE:
923 return ARMCC::HS;
924 case CmpInst::ICMP_ULT:
925 return ARMCC::LO;
926 }
Eric Christopher543cf052010-09-01 22:16:27 +0000927}
928
Eric Christopher43b62be2010-09-27 06:02:23 +0000929bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000930 const BranchInst *BI = cast<BranchInst>(I);
931 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
932 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000933
Eric Christophere5734102010-09-03 00:35:47 +0000934 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000935 // TODO: Try to avoid the re-computation in some places.
936 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000937 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000938
Eric Christopher229207a2010-09-29 01:14:47 +0000939 // Re-set the flags just in case.
940 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
941 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
942 .addReg(CondReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000943
Eric Christophere5734102010-09-03 00:35:47 +0000944 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000945 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000946 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000947 FastEmitBranch(FBB, DL);
948 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000949 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000950}
951
Eric Christopher43b62be2010-09-27 06:02:23 +0000952bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000953 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000954
Eric Christopherd43393a2010-09-08 23:13:45 +0000955 EVT VT;
956 const Type *Ty = CI->getOperand(0)->getType();
957 if (!isTypeLegal(Ty, VT))
958 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000959
Eric Christopherd43393a2010-09-08 23:13:45 +0000960 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
961 if (isFloat && !Subtarget->hasVFP2())
962 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000963
Eric Christopherd43393a2010-09-08 23:13:45 +0000964 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000965 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +0000966 switch (VT.getSimpleVT().SimpleTy) {
967 default: return false;
968 // TODO: Verify compares.
969 case MVT::f32:
970 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +0000971 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000972 break;
973 case MVT::f64:
974 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +0000975 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000976 break;
977 case MVT::i32:
978 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +0000979 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000980 break;
981 }
982
Eric Christopher229207a2010-09-29 01:14:47 +0000983 // Get the compare predicate.
984 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +0000985
Eric Christopher229207a2010-09-29 01:14:47 +0000986 // We may not handle every CC for now.
987 if (ARMPred == ARMCC::AL) return false;
988
Eric Christopherd43393a2010-09-08 23:13:45 +0000989 unsigned Arg1 = getRegForValue(CI->getOperand(0));
990 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000991
Eric Christopherd43393a2010-09-08 23:13:45 +0000992 unsigned Arg2 = getRegForValue(CI->getOperand(1));
993 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000994
Eric Christopherd43393a2010-09-08 23:13:45 +0000995 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
996 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000997
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000998 // For floating point we need to move the result to a comparison register
999 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001000 if (isFloat)
1001 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1002 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001003
Eric Christopher229207a2010-09-29 01:14:47 +00001004 // Now set a register based on the comparison. Explicitly set the predicates
1005 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001006 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001007 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001008 : ARM::GPRRegisterClass;
1009 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001010 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001011 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001012 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1013 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1014 .addReg(ZeroReg).addImm(1)
1015 .addImm(ARMPred).addReg(CondReg);
1016
Eric Christophera5b1e682010-09-17 22:28:18 +00001017 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001018 return true;
1019}
1020
Eric Christopher43b62be2010-09-27 06:02:23 +00001021bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001022 // Make sure we have VFP and that we're extending float to double.
1023 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001024
Eric Christopher46203602010-09-09 00:26:48 +00001025 Value *V = I->getOperand(0);
1026 if (!I->getType()->isDoubleTy() ||
1027 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001028
Eric Christopher46203602010-09-09 00:26:48 +00001029 unsigned Op = getRegForValue(V);
1030 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001031
Eric Christopher46203602010-09-09 00:26:48 +00001032 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001033 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001034 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001035 .addReg(Op));
1036 UpdateValueMap(I, Result);
1037 return true;
1038}
1039
Eric Christopher43b62be2010-09-27 06:02:23 +00001040bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001041 // Make sure we have VFP and that we're truncating double to float.
1042 if (!Subtarget->hasVFP2()) return false;
1043
1044 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001045 if (!(I->getType()->isFloatTy() &&
1046 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001047
1048 unsigned Op = getRegForValue(V);
1049 if (Op == 0) return false;
1050
1051 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001052 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001053 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001054 .addReg(Op));
1055 UpdateValueMap(I, Result);
1056 return true;
1057}
1058
Eric Christopher43b62be2010-09-27 06:02:23 +00001059bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001060 // Make sure we have VFP.
1061 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001062
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001063 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001064 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001065 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001066 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001067
Eric Christopher9a040492010-09-09 18:54:59 +00001068 unsigned Op = getRegForValue(I->getOperand(0));
1069 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001070
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001071 // The conversion routine works on fp-reg to fp-reg and the operand above
1072 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001073 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001074 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001075
Eric Christopher9a040492010-09-09 18:54:59 +00001076 unsigned Opc;
1077 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1078 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1079 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001080
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001081 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001082 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1083 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001084 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001085 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001086 return true;
1087}
1088
Eric Christopher43b62be2010-09-27 06:02:23 +00001089bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001090 // Make sure we have VFP.
1091 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001092
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001093 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001094 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001095 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001096 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001097
Eric Christopher9a040492010-09-09 18:54:59 +00001098 unsigned Op = getRegForValue(I->getOperand(0));
1099 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001100
Eric Christopher9a040492010-09-09 18:54:59 +00001101 unsigned Opc;
1102 const Type *OpTy = I->getOperand(0)->getType();
1103 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1104 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1105 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001106
Eric Christopher022b7fb2010-10-05 23:13:24 +00001107 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1108 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001109 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1110 ResultReg)
1111 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001112
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001113 // This result needs to be in an integer register, but the conversion only
1114 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001115 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001116 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001117
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001118 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001119 return true;
1120}
1121
Eric Christopher3bbd3962010-10-11 08:27:59 +00001122bool ARMFastISel::SelectSelect(const Instruction *I) {
1123 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1124 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1125 return false;
1126
1127 // Things need to be register sized for register moves.
1128 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1129 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1130
1131 unsigned CondReg = getRegForValue(I->getOperand(0));
1132 if (CondReg == 0) return false;
1133 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1134 if (Op1Reg == 0) return false;
1135 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1136 if (Op2Reg == 0) return false;
1137
1138 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1139 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1140 .addReg(CondReg).addImm(1));
1141 unsigned ResultReg = createResultReg(RC);
1142 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1143 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1144 .addReg(Op1Reg).addReg(Op2Reg)
1145 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1146 UpdateValueMap(I, ResultReg);
1147 return true;
1148}
1149
Eric Christopher08637852010-09-30 22:34:19 +00001150bool ARMFastISel::SelectSDiv(const Instruction *I) {
1151 EVT VT;
1152 const Type *Ty = I->getType();
1153 if (!isTypeLegal(Ty, VT))
1154 return false;
1155
1156 // If we have integer div support we should have selected this automagically.
1157 // In case we have a real miss go ahead and return false and we'll pick
1158 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001159 if (Subtarget->hasDivide()) return false;
1160
Eric Christopher08637852010-09-30 22:34:19 +00001161 // Otherwise emit a libcall.
1162 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001163 if (VT == MVT::i8)
1164 LC = RTLIB::SDIV_I8;
1165 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001166 LC = RTLIB::SDIV_I16;
1167 else if (VT == MVT::i32)
1168 LC = RTLIB::SDIV_I32;
1169 else if (VT == MVT::i64)
1170 LC = RTLIB::SDIV_I64;
1171 else if (VT == MVT::i128)
1172 LC = RTLIB::SDIV_I128;
1173 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001174
Eric Christopher08637852010-09-30 22:34:19 +00001175 return ARMEmitLibcall(I, LC);
1176}
1177
Eric Christopher6a880d62010-10-11 08:37:26 +00001178bool ARMFastISel::SelectSRem(const Instruction *I) {
1179 EVT VT;
1180 const Type *Ty = I->getType();
1181 if (!isTypeLegal(Ty, VT))
1182 return false;
1183
1184 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1185 if (VT == MVT::i8)
1186 LC = RTLIB::SREM_I8;
1187 else if (VT == MVT::i16)
1188 LC = RTLIB::SREM_I16;
1189 else if (VT == MVT::i32)
1190 LC = RTLIB::SREM_I32;
1191 else if (VT == MVT::i64)
1192 LC = RTLIB::SREM_I64;
1193 else if (VT == MVT::i128)
1194 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001195 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001196
Eric Christopher6a880d62010-10-11 08:37:26 +00001197 return ARMEmitLibcall(I, LC);
1198}
1199
Eric Christopher43b62be2010-09-27 06:02:23 +00001200bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001201 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001202
Eric Christopherbc39b822010-09-09 00:53:57 +00001203 // We can get here in the case when we want to use NEON for our fp
1204 // operations, but can't figure out how to. Just use the vfp instructions
1205 // if we have them.
1206 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001207 const Type *Ty = I->getType();
1208 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1209 if (isFloat && !Subtarget->hasVFP2())
1210 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001211
Eric Christopherbc39b822010-09-09 00:53:57 +00001212 unsigned Op1 = getRegForValue(I->getOperand(0));
1213 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001214
Eric Christopherbc39b822010-09-09 00:53:57 +00001215 unsigned Op2 = getRegForValue(I->getOperand(1));
1216 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001217
Eric Christopherbc39b822010-09-09 00:53:57 +00001218 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +00001219 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1220 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001221 switch (ISDOpcode) {
1222 default: return false;
1223 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001224 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001225 break;
1226 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001227 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001228 break;
1229 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001230 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001231 break;
1232 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001233 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001234 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1235 TII.get(Opc), ResultReg)
1236 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001237 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001238 return true;
1239}
1240
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001241// Call Handling Code
1242
1243// This is largely taken directly from CCAssignFnForNode - we don't support
1244// varargs in FastISel so that part has been removed.
1245// TODO: We may not support all of this.
1246CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1247 switch (CC) {
1248 default:
1249 llvm_unreachable("Unsupported calling convention");
1250 case CallingConv::C:
1251 case CallingConv::Fast:
1252 // Use target triple & subtarget features to do actual dispatch.
1253 if (Subtarget->isAAPCS_ABI()) {
1254 if (Subtarget->hasVFP2() &&
1255 FloatABIType == FloatABI::Hard)
1256 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1257 else
1258 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1259 } else
1260 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1261 case CallingConv::ARM_AAPCS_VFP:
1262 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1263 case CallingConv::ARM_AAPCS:
1264 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1265 case CallingConv::ARM_APCS:
1266 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1267 }
1268}
1269
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001270bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1271 SmallVectorImpl<unsigned> &ArgRegs,
1272 SmallVectorImpl<EVT> &ArgVTs,
1273 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1274 SmallVectorImpl<unsigned> &RegArgs,
1275 CallingConv::ID CC,
1276 unsigned &NumBytes) {
1277 SmallVector<CCValAssign, 16> ArgLocs;
1278 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1279 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1280
1281 // Get a count of how many bytes are to be pushed on the stack.
1282 NumBytes = CCInfo.getNextStackOffset();
1283
1284 // Issue CALLSEQ_START
1285 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1287 TII.get(AdjStackDown))
1288 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001289
1290 // Process the args.
1291 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1292 CCValAssign &VA = ArgLocs[i];
1293 unsigned Arg = ArgRegs[VA.getValNo()];
1294 EVT ArgVT = ArgVTs[VA.getValNo()];
1295
Eric Christopherf9764fa2010-09-30 20:49:44 +00001296 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001297 switch (VA.getLocInfo()) {
1298 case CCValAssign::Full: break;
1299 default:
Eric Christopher11077342010-10-07 05:14:08 +00001300 // TODO: Handle arg promotion.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001301 return false;
1302 }
1303
1304 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001305 // TODO: We need custom lowering for f64 args.
1306 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001307 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001308 VA.getLocReg())
1309 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001310 RegArgs.push_back(VA.getLocReg());
1311 } else {
1312 // Need to store
1313 return false;
1314 }
1315 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001316
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001317 return true;
1318}
1319
1320bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1321 const Instruction *I, CallingConv::ID CC,
1322 unsigned &NumBytes) {
1323 // Issue CALLSEQ_END
1324 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001325 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1326 TII.get(AdjStackUp))
1327 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001328
1329 // Now the return value.
1330 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1331 SmallVector<CCValAssign, 16> RVLocs;
1332 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1333 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1334
1335 // Copy all of the result registers out of their specified physreg.
Eric Christopher14df8822010-10-01 00:00:11 +00001336 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1337 // For this move we copy into two registers and then move into the
1338 // double fp reg we want.
1339 // TODO: Are the copies necessary?
1340 TargetRegisterClass *CopyRC = TLI.getRegClassFor(MVT::i32);
1341 unsigned Copy1 = createResultReg(CopyRC);
1342 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1343 Copy1).addReg(RVLocs[0].getLocReg());
1344 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001345
Eric Christopher14df8822010-10-01 00:00:11 +00001346 unsigned Copy2 = createResultReg(CopyRC);
1347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1348 Copy2).addReg(RVLocs[1].getLocReg());
1349 UsedRegs.push_back(RVLocs[1].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001350
Eric Christopher14df8822010-10-01 00:00:11 +00001351 EVT DestVT = RVLocs[0].getValVT();
1352 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1353 unsigned ResultReg = createResultReg(DstRC);
1354 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1355 TII.get(ARM::VMOVDRR), ResultReg)
1356 .addReg(Copy1).addReg(Copy2));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001357
1358 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001359 UpdateValueMap(I, ResultReg);
1360 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001361 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001362 EVT CopyVT = RVLocs[0].getValVT();
1363 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001364
Eric Christopher14df8822010-10-01 00:00:11 +00001365 unsigned ResultReg = createResultReg(DstRC);
1366 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1367 ResultReg).addReg(RVLocs[0].getLocReg());
1368 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001369
Eric Christopherdccd2c32010-10-11 08:38:55 +00001370 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001371 UpdateValueMap(I, ResultReg);
1372 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001373 }
1374
Eric Christopherdccd2c32010-10-11 08:38:55 +00001375 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001376}
1377
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001378// A quick function that will emit a call for a named libcall in F with the
1379// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001380// can emit a call for any libcall we can produce. This is an abridged version
1381// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001382// like computed function pointers or strange arguments at call sites.
1383// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1384// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001385bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1386 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001387
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001388 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001389 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001390 EVT RetVT;
1391 if (RetTy->isVoidTy())
1392 RetVT = MVT::isVoid;
1393 else if (!isTypeLegal(RetTy, RetVT))
1394 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001395
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001396 // For now we're using BLX etc on the assumption that we have v5t ops.
1397 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001398
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001399 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001400 SmallVector<Value*, 8> Args;
1401 SmallVector<unsigned, 8> ArgRegs;
1402 SmallVector<EVT, 8> ArgVTs;
1403 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1404 Args.reserve(I->getNumOperands());
1405 ArgRegs.reserve(I->getNumOperands());
1406 ArgVTs.reserve(I->getNumOperands());
1407 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001408 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001409 Value *Op = I->getOperand(i);
1410 unsigned Arg = getRegForValue(Op);
1411 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001412
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001413 const Type *ArgTy = Op->getType();
1414 EVT ArgVT;
1415 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001416
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001417 ISD::ArgFlagsTy Flags;
1418 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1419 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001420
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001421 Args.push_back(Op);
1422 ArgRegs.push_back(Arg);
1423 ArgVTs.push_back(ArgVT);
1424 ArgFlags.push_back(Flags);
1425 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001426
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001427 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001428 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001429 unsigned NumBytes;
1430 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1431 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001432
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001433 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001434 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001435 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001436 unsigned CallOpc;
1437 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001438 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001439 else
1440 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001441 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001442 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001443
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001444 // Add implicit physical register uses to the call.
1445 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1446 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001447
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001448 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001449 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001450 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001451
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001452 // Set all unused physreg defs as dead.
1453 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001454
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001455 return true;
1456}
1457
Eric Christopherf9764fa2010-09-30 20:49:44 +00001458bool ARMFastISel::SelectCall(const Instruction *I) {
1459 const CallInst *CI = cast<CallInst>(I);
1460 const Value *Callee = CI->getCalledValue();
1461
1462 // Can't handle inline asm or worry about intrinsics yet.
1463 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1464
Eric Christophere6ca6772010-10-01 21:33:12 +00001465 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001466 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001467 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1468 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001469
Eric Christopherf9764fa2010-09-30 20:49:44 +00001470 // Check the calling convention.
1471 ImmutableCallSite CS(CI);
1472 CallingConv::ID CC = CS.getCallingConv();
1473 // TODO: Avoid some calling conventions?
1474 if (CC != CallingConv::C) {
Eric Christophere540a6f2010-10-05 23:50:58 +00001475 // errs() << "Can't handle calling convention: " << CC << "\n";
Eric Christopherf9764fa2010-09-30 20:49:44 +00001476 return false;
1477 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001478
Eric Christopherf9764fa2010-09-30 20:49:44 +00001479 // Let SDISel handle vararg functions.
1480 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1481 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1482 if (FTy->isVarArg())
1483 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001484
Eric Christopherf9764fa2010-09-30 20:49:44 +00001485 // Handle *simple* calls for now.
1486 const Type *RetTy = I->getType();
1487 EVT RetVT;
1488 if (RetTy->isVoidTy())
1489 RetVT = MVT::isVoid;
1490 else if (!isTypeLegal(RetTy, RetVT))
1491 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001492
Eric Christopherf9764fa2010-09-30 20:49:44 +00001493 // For now we're using BLX etc on the assumption that we have v5t ops.
1494 // TODO: Maybe?
1495 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001496
Eric Christopherf9764fa2010-09-30 20:49:44 +00001497 // Set up the argument vectors.
1498 SmallVector<Value*, 8> Args;
1499 SmallVector<unsigned, 8> ArgRegs;
1500 SmallVector<EVT, 8> ArgVTs;
1501 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1502 Args.reserve(CS.arg_size());
1503 ArgRegs.reserve(CS.arg_size());
1504 ArgVTs.reserve(CS.arg_size());
1505 ArgFlags.reserve(CS.arg_size());
1506 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1507 i != e; ++i) {
1508 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001509
Eric Christopherf9764fa2010-09-30 20:49:44 +00001510 if (Arg == 0)
1511 return false;
1512 ISD::ArgFlagsTy Flags;
1513 unsigned AttrInd = i - CS.arg_begin() + 1;
1514 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1515 Flags.setSExt();
1516 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1517 Flags.setZExt();
1518
1519 // FIXME: Only handle *easy* calls for now.
1520 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1521 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1522 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1523 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1524 return false;
1525
1526 const Type *ArgTy = (*i)->getType();
1527 EVT ArgVT;
1528 if (!isTypeLegal(ArgTy, ArgVT))
1529 return false;
1530 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1531 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001532
Eric Christopherf9764fa2010-09-30 20:49:44 +00001533 Args.push_back(*i);
1534 ArgRegs.push_back(Arg);
1535 ArgVTs.push_back(ArgVT);
1536 ArgFlags.push_back(Flags);
1537 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001538
Eric Christopherf9764fa2010-09-30 20:49:44 +00001539 // Handle the arguments now that we've gotten them.
1540 SmallVector<unsigned, 4> RegArgs;
1541 unsigned NumBytes;
1542 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1543 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001544
Eric Christopherf9764fa2010-09-30 20:49:44 +00001545 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001546 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001547 MachineInstrBuilder MIB;
1548 unsigned CallOpc;
1549 if(isThumb)
1550 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1551 else
1552 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1553 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1554 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001555
Eric Christopherf9764fa2010-09-30 20:49:44 +00001556 // Add implicit physical register uses to the call.
1557 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1558 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001559
Eric Christopherf9764fa2010-09-30 20:49:44 +00001560 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001561 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001562 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001563
Eric Christopherf9764fa2010-09-30 20:49:44 +00001564 // Set all unused physreg defs as dead.
1565 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001566
Eric Christopherf9764fa2010-09-30 20:49:44 +00001567 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001568
Eric Christopherf9764fa2010-09-30 20:49:44 +00001569}
1570
Eric Christopher56d2b722010-09-02 23:43:26 +00001571// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001572bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001573 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001574 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001575
Eric Christopherab695882010-07-21 22:26:11 +00001576 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001577 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001578 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001579 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001580 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001581 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001582 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001583 case Instruction::ICmp:
1584 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001585 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001586 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001587 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001588 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001589 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001590 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001591 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001592 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001593 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001594 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001595 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001596 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001597 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001598 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001599 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001600 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001601 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001602 case Instruction::SRem:
1603 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001604 case Instruction::Call:
1605 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001606 case Instruction::Select:
1607 return SelectSelect(I);
Eric Christopherab695882010-07-21 22:26:11 +00001608 default: break;
1609 }
1610 return false;
1611}
1612
1613namespace llvm {
1614 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001615 // Completely untested on non-darwin.
1616 const TargetMachine &TM = funcInfo.MF->getTarget();
1617 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher8ff9a9d2010-10-11 20:26:21 +00001618 if (Subtarget->isTargetDarwin() && EnableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001619 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001620 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001621 }
1622}