blob: d1f3aed76140395ff53d61dd7bcce6217a5d9952 [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"
Eric Christopherd56d61a2010-10-17 01:51:42 +000036#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000040#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000043#include "llvm/Target/TargetData.h"
44#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetLowering.h"
46#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000047#include "llvm/Target/TargetOptions.h"
48using namespace llvm;
49
Eric Christopher038fea52010-08-17 00:46:57 +000050static cl::opt<bool>
Eric Christopher8ff9a9d2010-10-11 20:26:21 +000051EnableARMFastISel("arm-fast-isel",
52 cl::desc("Turn on experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000053 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000054
Eric Christopherab695882010-07-21 22:26:11 +000055namespace {
56
57class ARMFastISel : public FastISel {
58
Eric Christophera3224252010-10-15 21:32:12 +000059 typedef struct AddrBase {
60 unsigned Reg;
61 unsigned FrameIndex;
62 } AddrBase;
63
Eric Christopherab695882010-07-21 22:26:11 +000064 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
65 /// make the right decision when generating code for different targets.
66 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000067 const TargetMachine &TM;
68 const TargetInstrInfo &TII;
69 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000070 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000071
Eric Christopher8cf6c602010-09-29 22:24:45 +000072 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000073 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000074 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000075
Eric Christopherab695882010-07-21 22:26:11 +000076 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000077 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000078 : FastISel(funcInfo),
79 TM(funcInfo.MF->getTarget()),
80 TII(*TM.getInstrInfo()),
81 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000082 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000083 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000084 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000085 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000086 }
87
Eric Christophercb592292010-08-20 00:20:31 +000088 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000089 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
90 const TargetRegisterClass *RC);
91 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
92 const TargetRegisterClass *RC,
93 unsigned Op0, bool Op0IsKill);
94 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
95 const TargetRegisterClass *RC,
96 unsigned Op0, bool Op0IsKill,
97 unsigned Op1, bool Op1IsKill);
98 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
99 const TargetRegisterClass *RC,
100 unsigned Op0, bool Op0IsKill,
101 uint64_t Imm);
102 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
103 const TargetRegisterClass *RC,
104 unsigned Op0, bool Op0IsKill,
105 const ConstantFP *FPImm);
106 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
107 const TargetRegisterClass *RC,
108 uint64_t Imm);
109 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC,
111 unsigned Op0, bool Op0IsKill,
112 unsigned Op1, bool Op1IsKill,
113 uint64_t Imm);
114 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
115 unsigned Op0, bool Op0IsKill,
116 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000117
Eric Christophercb592292010-08-20 00:20:31 +0000118 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000119 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000120 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000121 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000122
123 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000124
Eric Christopher83007122010-08-23 21:44:12 +0000125 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000126 private:
Eric Christopher43b62be2010-09-27 06:02:23 +0000127 virtual bool SelectLoad(const Instruction *I);
128 virtual bool SelectStore(const Instruction *I);
129 virtual bool SelectBranch(const Instruction *I);
130 virtual bool SelectCmp(const Instruction *I);
131 virtual bool SelectFPExt(const Instruction *I);
132 virtual bool SelectFPTrunc(const Instruction *I);
133 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
134 virtual bool SelectSIToFP(const Instruction *I);
135 virtual bool SelectFPToSI(const Instruction *I);
136 virtual bool SelectSDiv(const Instruction *I);
Eric Christopher6a880d62010-10-11 08:37:26 +0000137 virtual bool SelectSRem(const Instruction *I);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000138 virtual bool SelectCall(const Instruction *I);
Eric Christopher3bbd3962010-10-11 08:27:59 +0000139 virtual bool SelectSelect(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000140
Eric Christopher83007122010-08-23 21:44:12 +0000141 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000142 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000143 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000144 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christophera3224252010-10-15 21:32:12 +0000145 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, AddrBase Base, int Offset);
146 bool ARMEmitStore(EVT VT, unsigned SrcReg, AddrBase Base, int Offset);
147 bool ARMComputeRegOffset(const Value *Obj, AddrBase &Base, int &Offset);
148 void ARMSimplifyRegOffset(AddrBase &Base, int &Offset, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000149 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000150 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000151 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000152 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000153 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000154
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000155 // Call handling routines.
156 private:
157 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000158 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000159 SmallVectorImpl<unsigned> &ArgRegs,
160 SmallVectorImpl<EVT> &ArgVTs,
161 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
162 SmallVectorImpl<unsigned> &RegArgs,
163 CallingConv::ID CC,
164 unsigned &NumBytes);
165 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
166 const Instruction *I, CallingConv::ID CC,
167 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000168 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000169
170 // OptionalDef handling routines.
171 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000172 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
173 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
174};
Eric Christopherab695882010-07-21 22:26:11 +0000175
176} // end anonymous namespace
177
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000178#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000179
Eric Christopher456144e2010-08-19 00:37:05 +0000180// DefinesOptionalPredicate - This is different from DefinesPredicate in that
181// we don't care about implicit defs here, just places we'll need to add a
182// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
183bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
184 const TargetInstrDesc &TID = MI->getDesc();
185 if (!TID.hasOptionalDef())
186 return false;
187
188 // Look to see if our OptionalDef is defining CPSR or CCR.
189 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
190 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000191 if (!MO.isReg() || !MO.isDef()) continue;
192 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000193 *CPSR = true;
194 }
195 return true;
196}
197
198// If the machine is predicable go ahead and add the predicate operands, if
199// it needs default CC operands add those.
200const MachineInstrBuilder &
201ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
202 MachineInstr *MI = &*MIB;
203
204 // Do we use a predicate?
205 if (TII.isPredicable(MI))
206 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000207
Eric Christopher456144e2010-08-19 00:37:05 +0000208 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
209 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000210 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000211 if (DefinesOptionalPredicate(MI, &CPSR)) {
212 if (CPSR)
213 AddDefaultT1CC(MIB);
214 else
215 AddDefaultCC(MIB);
216 }
217 return MIB;
218}
219
Eric Christopher0fe7d542010-08-17 01:25:29 +0000220unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
221 const TargetRegisterClass* RC) {
222 unsigned ResultReg = createResultReg(RC);
223 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
224
Eric Christopher456144e2010-08-19 00:37:05 +0000225 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000226 return ResultReg;
227}
228
229unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
230 const TargetRegisterClass *RC,
231 unsigned Op0, bool Op0IsKill) {
232 unsigned ResultReg = createResultReg(RC);
233 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
234
235 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000237 .addReg(Op0, Op0IsKill * RegState::Kill));
238 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000239 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000240 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000241 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000242 TII.get(TargetOpcode::COPY), ResultReg)
243 .addReg(II.ImplicitDefs[0]));
244 }
245 return ResultReg;
246}
247
248unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
249 const TargetRegisterClass *RC,
250 unsigned Op0, bool Op0IsKill,
251 unsigned Op1, bool Op1IsKill) {
252 unsigned ResultReg = createResultReg(RC);
253 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
254
255 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000256 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000257 .addReg(Op0, Op0IsKill * RegState::Kill)
258 .addReg(Op1, Op1IsKill * RegState::Kill));
259 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000260 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000261 .addReg(Op0, Op0IsKill * RegState::Kill)
262 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000263 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000264 TII.get(TargetOpcode::COPY), ResultReg)
265 .addReg(II.ImplicitDefs[0]));
266 }
267 return ResultReg;
268}
269
270unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
271 const TargetRegisterClass *RC,
272 unsigned Op0, bool Op0IsKill,
273 uint64_t Imm) {
274 unsigned ResultReg = createResultReg(RC);
275 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
276
277 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000278 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000279 .addReg(Op0, Op0IsKill * RegState::Kill)
280 .addImm(Imm));
281 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000282 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000283 .addReg(Op0, Op0IsKill * RegState::Kill)
284 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000285 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000286 TII.get(TargetOpcode::COPY), ResultReg)
287 .addReg(II.ImplicitDefs[0]));
288 }
289 return ResultReg;
290}
291
292unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
293 const TargetRegisterClass *RC,
294 unsigned Op0, bool Op0IsKill,
295 const ConstantFP *FPImm) {
296 unsigned ResultReg = createResultReg(RC);
297 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
298
299 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000300 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000301 .addReg(Op0, Op0IsKill * RegState::Kill)
302 .addFPImm(FPImm));
303 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 .addReg(Op0, Op0IsKill * RegState::Kill)
306 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000308 TII.get(TargetOpcode::COPY), ResultReg)
309 .addReg(II.ImplicitDefs[0]));
310 }
311 return ResultReg;
312}
313
314unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
315 const TargetRegisterClass *RC,
316 unsigned Op0, bool Op0IsKill,
317 unsigned Op1, bool Op1IsKill,
318 uint64_t Imm) {
319 unsigned ResultReg = createResultReg(RC);
320 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
321
322 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000323 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000324 .addReg(Op0, Op0IsKill * RegState::Kill)
325 .addReg(Op1, Op1IsKill * RegState::Kill)
326 .addImm(Imm));
327 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000329 .addReg(Op0, Op0IsKill * RegState::Kill)
330 .addReg(Op1, Op1IsKill * RegState::Kill)
331 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000332 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000333 TII.get(TargetOpcode::COPY), ResultReg)
334 .addReg(II.ImplicitDefs[0]));
335 }
336 return ResultReg;
337}
338
339unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
340 const TargetRegisterClass *RC,
341 uint64_t Imm) {
342 unsigned ResultReg = createResultReg(RC);
343 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000344
Eric Christopher0fe7d542010-08-17 01:25:29 +0000345 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000346 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000347 .addImm(Imm));
348 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000350 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000351 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000352 TII.get(TargetOpcode::COPY), ResultReg)
353 .addReg(II.ImplicitDefs[0]));
354 }
355 return ResultReg;
356}
357
358unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
359 unsigned Op0, bool Op0IsKill,
360 uint32_t Idx) {
361 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
362 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
363 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000364 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000365 DL, TII.get(TargetOpcode::COPY), ResultReg)
366 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
367 return ResultReg;
368}
369
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000370// TODO: Don't worry about 64-bit now, but when this is fixed remove the
371// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000372unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000373 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000374
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000375 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
377 TII.get(ARM::VMOVRS), MoveReg)
378 .addReg(SrcReg));
379 return MoveReg;
380}
381
382unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000383 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000384
Eric Christopheraa3ace12010-09-09 20:49:25 +0000385 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
386 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000387 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000388 .addReg(SrcReg));
389 return MoveReg;
390}
391
Eric Christopher9ed58df2010-09-09 00:19:41 +0000392// For double width floating point we need to materialize two constants
393// (the high and the low) into integer registers then use a move to get
394// the combined constant into an FP reg.
395unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
396 const APFloat Val = CFP->getValueAPF();
397 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000398
Eric Christopher9ed58df2010-09-09 00:19:41 +0000399 // This checks to see if we can use VFP3 instructions to materialize
400 // a constant, otherwise we have to go through the constant pool.
401 if (TLI.isFPImmLegal(Val, VT)) {
402 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
403 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
404 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
405 DestReg)
406 .addFPImm(CFP));
407 return DestReg;
408 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000409
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000410 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000411 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000412
Eric Christopher238bb162010-09-09 23:50:00 +0000413 // MachineConstantPool wants an explicit alignment.
414 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
415 if (Align == 0) {
416 // TODO: Figure out if this is correct.
417 Align = TD.getTypeAllocSize(CFP->getType());
418 }
419 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
420 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
421 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000422
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000423 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000424 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
425 DestReg)
426 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000427 .addReg(0));
428 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000429}
430
Eric Christopher744c7c82010-09-28 22:47:54 +0000431unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000432
Eric Christopher744c7c82010-09-28 22:47:54 +0000433 // For now 32-bit only.
434 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000435
Eric Christopher56d2b722010-09-02 23:43:26 +0000436 // MachineConstantPool wants an explicit alignment.
437 unsigned Align = TD.getPrefTypeAlignment(C->getType());
438 if (Align == 0) {
439 // TODO: Figure out if this is correct.
440 Align = TD.getTypeAllocSize(C->getType());
441 }
442 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000443 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000444
Eric Christopher56d2b722010-09-02 23:43:26 +0000445 if (isThumb)
446 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000447 TII.get(ARM::t2LDRpci), DestReg)
448 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000449 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000450 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000451 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000452 TII.get(ARM::LDRcp), DestReg)
453 .addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000454 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000455
Eric Christopher56d2b722010-09-02 23:43:26 +0000456 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000457}
458
Eric Christopherc9932f62010-10-01 23:24:42 +0000459unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000460 // For now 32-bit only.
461 if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000462
Eric Christopher890dbbe2010-10-02 00:32:44 +0000463 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000464
Eric Christopher890dbbe2010-10-02 00:32:44 +0000465 // TODO: No external globals for now.
466 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000467
Eric Christopher890dbbe2010-10-02 00:32:44 +0000468 // TODO: Need more magic for ARM PIC.
469 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000470
Eric Christopher890dbbe2010-10-02 00:32:44 +0000471 // MachineConstantPool wants an explicit alignment.
472 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
473 if (Align == 0) {
474 // TODO: Figure out if this is correct.
475 Align = TD.getTypeAllocSize(GV->getType());
476 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000477
Eric Christopher890dbbe2010-10-02 00:32:44 +0000478 // Grab index.
479 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
480 unsigned Id = AFI->createConstPoolEntryUId();
481 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
482 ARMCP::CPValue, PCAdj);
483 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000484
Eric Christopher890dbbe2010-10-02 00:32:44 +0000485 // Load value.
486 MachineInstrBuilder MIB;
487 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
488 if (isThumb) {
489 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
490 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
491 .addConstantPoolIndex(Idx);
492 if (RelocM == Reloc::PIC_)
493 MIB.addImm(Id);
494 } else {
495 // The extra reg and immediate are for addrmode2.
496 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
497 DestReg)
498 .addConstantPoolIndex(Idx)
499 .addReg(0).addImm(0);
500 }
501 AddOptionalDefs(MIB);
502 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000503}
504
Eric Christopher9ed58df2010-09-09 00:19:41 +0000505unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
506 EVT VT = TLI.getValueType(C->getType(), true);
507
508 // Only handle simple types.
509 if (!VT.isSimple()) return 0;
510
511 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
512 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000513 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
514 return ARMMaterializeGV(GV, VT);
515 else if (isa<ConstantInt>(C))
516 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000517
Eric Christopherc9932f62010-10-01 23:24:42 +0000518 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000519}
520
Eric Christopherf9764fa2010-09-30 20:49:44 +0000521unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
522 // Don't handle dynamic allocas.
523 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000524
Eric Christopherf9764fa2010-09-30 20:49:44 +0000525 EVT VT;
526 if (!isTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000527
Eric Christopherf9764fa2010-09-30 20:49:44 +0000528 DenseMap<const AllocaInst*, int>::iterator SI =
529 FuncInfo.StaticAllocaMap.find(AI);
530
531 // This will get lowered later into the correct offsets and registers
532 // via rewriteXFrameIndex.
533 if (SI != FuncInfo.StaticAllocaMap.end()) {
534 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
535 unsigned ResultReg = createResultReg(RC);
536 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
537 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
538 TII.get(Opc), ResultReg)
539 .addFrameIndex(SI->second)
540 .addImm(0));
541 return ResultReg;
542 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000543
Eric Christopherf9764fa2010-09-30 20:49:44 +0000544 return 0;
545}
546
Eric Christopherb1cc8482010-08-25 07:23:49 +0000547bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
548 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000549
Eric Christopherb1cc8482010-08-25 07:23:49 +0000550 // Only handle simple types.
551 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000552
Eric Christopherdc908042010-08-31 01:28:42 +0000553 // Handle all legal types, i.e. a register that will directly hold this
554 // value.
555 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000556}
557
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000558bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
559 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000560
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000561 // If this is a type than can be sign or zero-extended to a basic operation
562 // go ahead and accept it now.
563 if (VT == MVT::i8 || VT == MVT::i16)
564 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000565
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000566 return false;
567}
568
Eric Christophercb0b04b2010-08-24 00:07:24 +0000569// Computes the Reg+Offset to get to an object.
Eric Christophera3224252010-10-15 21:32:12 +0000570bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, AddrBase &Base,
Eric Christopher83007122010-08-23 21:44:12 +0000571 int &Offset) {
572 // Some boilerplate from the X86 FastISel.
573 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000574 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000575 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000576 // Don't walk into other basic blocks; it's possible we haven't
577 // visited them yet, so the instructions may not yet be assigned
578 // virtual registers.
579 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
580 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000581 Opcode = I->getOpcode();
582 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000583 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000584 Opcode = C->getOpcode();
585 U = C;
586 }
587
Eric Christophercb0b04b2010-08-24 00:07:24 +0000588 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000589 if (Ty->getAddressSpace() > 255)
590 // Fast instruction selection doesn't support the special
591 // address spaces.
592 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000593
Eric Christopher83007122010-08-23 21:44:12 +0000594 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000595 default:
Eric Christopher83007122010-08-23 21:44:12 +0000596 break;
Eric Christopher55324332010-10-12 00:43:21 +0000597 case Instruction::BitCast: {
598 // Look through bitcasts.
Eric Christophera3224252010-10-15 21:32:12 +0000599 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000600 }
601 case Instruction::IntToPtr: {
602 // Look past no-op inttoptrs.
603 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000604 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000605 break;
606 }
607 case Instruction::PtrToInt: {
608 // Look past no-op ptrtoints.
609 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000610 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000611 break;
612 }
Eric Christophereae84392010-10-14 09:29:41 +0000613 case Instruction::GetElementPtr: {
614 int SavedOffset = Offset;
Eric Christophera3224252010-10-15 21:32:12 +0000615 AddrBase SavedBase = Base;
Eric Christophereae84392010-10-14 09:29:41 +0000616 int TmpOffset = Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000617
Eric Christophereae84392010-10-14 09:29:41 +0000618 // Iterate through the GEP folding the constants into offsets where
619 // we can.
620 gep_type_iterator GTI = gep_type_begin(U);
621 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
622 i != e; ++i, ++GTI) {
623 const Value *Op = *i;
624 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
625 const StructLayout *SL = TD.getStructLayout(STy);
626 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
627 TmpOffset += SL->getElementOffset(Idx);
628 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000629 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
630 SmallVector<const Value *, 4> Worklist;
631 Worklist.push_back(Op);
632 do {
633 Op = Worklist.pop_back_val();
634 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
635 // Constant-offset addressing.
636 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000637 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000638 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
639 // An add with a constant operand. Fold the constant.
640 ConstantInt *CI =
641 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
642 TmpOffset += CI->getSExtValue() * S;
643 // Add the other operand back to the work list.
644 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
645 } else
646 goto unsupported_gep;
647 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000648 }
649 }
Eric Christopher2896df82010-10-15 18:02:07 +0000650
651 // Try to grab the base operand now.
Eric Christophereae84392010-10-14 09:29:41 +0000652 Offset = TmpOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000653 if (ARMComputeRegOffset(U->getOperand(0), Base, Offset)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000654
655 // We failed, restore everything and try the other options.
Eric Christophereae84392010-10-14 09:29:41 +0000656 Offset = SavedOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000657 Base = SavedBase;
Eric Christopher2896df82010-10-15 18:02:07 +0000658
Eric Christophereae84392010-10-14 09:29:41 +0000659 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000660 break;
661 }
Eric Christopher83007122010-08-23 21:44:12 +0000662 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000663 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopherd56d61a2010-10-17 01:51:42 +0000664 unsigned Reg = TargetMaterializeAlloca(AI);
665
666 if (Reg == 0) return false;
667
668 Base.Reg = Reg;
669 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000670 }
671 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000672
Eric Christophera9c57512010-10-13 21:41:51 +0000673 // Materialize the global variable's address into a reg which can
674 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000675 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000676 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
677 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000678
Eric Christophera3224252010-10-15 21:32:12 +0000679 Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000680 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000681 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000682
Eric Christophercb0b04b2010-08-24 00:07:24 +0000683 // Try to get this in a register if nothing else has worked.
Eric Christophera3224252010-10-15 21:32:12 +0000684 if (Base.Reg == 0) Base.Reg = getRegForValue(Obj);
685 return Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000686}
687
Eric Christophera3224252010-10-15 21:32:12 +0000688void ARMFastISel::ARMSimplifyRegOffset(AddrBase &Base, int &Offset, EVT VT) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000689
690 // Since the offset may be too large for the load instruction
691 // get the reg+offset into a register.
Eric Christophera3224252010-10-15 21:32:12 +0000692 if (Base.Reg != ARM::SP && Offset != 0) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000693 ARMCC::CondCodes Pred = ARMCC::AL;
694 unsigned PredReg = 0;
695
Eric Christopher2896df82010-10-15 18:02:07 +0000696 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
697 ARM::GPRRegisterClass;
698 unsigned BaseReg = createResultReg(RC);
699
Eric Christophereaa204b2010-09-02 01:39:14 +0000700 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000701 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christophera3224252010-10-15 21:32:12 +0000702 BaseReg, Base.Reg, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000703 static_cast<const ARMBaseInstrInfo&>(TII));
704 else {
705 assert(AFI->isThumb2Function());
706 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christophera3224252010-10-15 21:32:12 +0000707 BaseReg, Base.Reg, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000708 static_cast<const ARMBaseInstrInfo&>(TII));
709 }
Eric Christophereae84392010-10-14 09:29:41 +0000710 Offset = 0;
Eric Christophera3224252010-10-15 21:32:12 +0000711 Base.Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000712 }
Eric Christopher83007122010-08-23 21:44:12 +0000713}
714
Eric Christopherb1cc8482010-08-25 07:23:49 +0000715bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
Eric Christophera3224252010-10-15 21:32:12 +0000716 AddrBase Base, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000717
Eric Christopherb1cc8482010-08-25 07:23:49 +0000718 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000719 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000720 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000721 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000722 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000723 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000724 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000725 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000726 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000727 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000728 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000729 VT = MVT::i32;
730 break;
731 case MVT::i8:
Eric Christopher45c60712010-10-17 01:40:27 +0000732 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRB;
Eric Christopher7a56f332010-10-08 01:13:17 +0000733 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000734 VT = MVT::i32;
735 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000736 case MVT::i32:
Eric Christopher45c60712010-10-17 01:40:27 +0000737 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDR;
Eric Christopher7a56f332010-10-08 01:13:17 +0000738 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000739 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000740 case MVT::f32:
741 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000742 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000743 isFloat = true;
744 break;
745 case MVT::f64:
746 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000747 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000748 isFloat = true;
749 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000750 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000751
Eric Christopheree56ea62010-10-07 05:50:44 +0000752 ResultReg = createResultReg(RC);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000753
Eric Christopher7a56f332010-10-08 01:13:17 +0000754 // For now with the additions above the offset should be zero - thus we
Eric Christopher7208dbf2010-10-17 01:42:53 +0000755 // can always fit into an i12.
Eric Christophera3224252010-10-15 21:32:12 +0000756 assert((Base.Reg == ARM::SP || Offset == 0) &&
Eric Christopher15418772010-10-12 05:39:06 +0000757 "Offset not zero and not a stack load!");
Eric Christopherdccd2c32010-10-11 08:38:55 +0000758
Eric Christopherc9a91fd2010-10-15 23:07:10 +0000759 if (Base.Reg == ARM::SP && Offset == 0)
Eric Christopher15418772010-10-12 05:39:06 +0000760 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christophera3224252010-10-15 21:32:12 +0000761 ResultReg, Base.FrameIndex, RC,
Eric Christopher15418772010-10-12 05:39:06 +0000762 TM.getRegisterInfo());
Eric Christopherd56d61a2010-10-17 01:51:42 +0000763 else if (Base.Reg == ARM::SP) {
764 // TODO: This won't work for NEON.
765 unsigned FI = Base.FrameIndex;
766 MachineMemOperand *MMO =
767 FuncInfo.MF->getMachineMemOperand(
768 MachinePointerInfo::getFixedStack(FI, Offset),
769 MachineMemOperand::MOLoad,
770 MFI.getObjectSize(FI),
771 MFI.getObjectAlignment(FI));
772 if (isFloat || isThumb)
773 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
774 TII.get(Opc), ResultReg)
775 .addFrameIndex(FI).addImm(Offset).addMemOperand(MMO));
776 else
777 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
778 TII.get(Opc), ResultReg)
779 .addFrameIndex(FI).addReg(0).addImm(Offset).addMemOperand(MMO));
780 }
Eric Christopher7a56f332010-10-08 01:13:17 +0000781 // The thumb and floating point instructions both take 2 operands, ARM takes
782 // another register.
Eric Christopher15418772010-10-12 05:39:06 +0000783 else if (isFloat || isThumb)
Eric Christopher6dab1372010-09-18 01:59:37 +0000784 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
785 TII.get(Opc), ResultReg)
Eric Christophera3224252010-10-15 21:32:12 +0000786 .addReg(Base.Reg).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000787 else
788 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
789 TII.get(Opc), ResultReg)
Eric Christophera3224252010-10-15 21:32:12 +0000790 .addReg(Base.Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000791 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000792}
793
Eric Christopher43b62be2010-09-27 06:02:23 +0000794bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000795 // Verify we have a legal type before going any further.
796 EVT VT;
797 if (!isLoadTypeLegal(I->getType(), VT))
798 return false;
799
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000800 // Our register and offset with innocuous defaults.
Eric Christophera3224252010-10-15 21:32:12 +0000801 AddrBase Base = { 0, 0 };
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000802 int Offset = 0;
803
804 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000805 if (!ARMComputeRegOffset(I->getOperand(0), Base, Offset))
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000806 return false;
807
Eric Christophera3224252010-10-15 21:32:12 +0000808 ARMSimplifyRegOffset(Base, Offset, VT);
Eric Christophereae84392010-10-14 09:29:41 +0000809
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000810 unsigned ResultReg;
Eric Christophera3224252010-10-15 21:32:12 +0000811 if (!ARMEmitLoad(VT, ResultReg, Base, Offset)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000812
813 UpdateValueMap(I, ResultReg);
814 return true;
815}
816
Eric Christopher318b6ee2010-09-02 00:53:56 +0000817bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
Eric Christophera3224252010-10-15 21:32:12 +0000818 AddrBase Base, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000819 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000820 bool isFloat = false;
Eric Christopher15418772010-10-12 05:39:06 +0000821 // VT is set here only for use in the alloca stores below - those are promoted
822 // to reg size always.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000823 switch (VT.getSimpleVT().SimpleTy) {
824 default: return false;
825 case MVT::i1:
Eric Christopher2896df82010-10-15 18:02:07 +0000826 case MVT::i8:
Eric Christopher15418772010-10-12 05:39:06 +0000827 VT = MVT::i32;
Eric Christopher45c60712010-10-17 01:40:27 +0000828 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRB;
Eric Christopher15418772010-10-12 05:39:06 +0000829 break;
830 case MVT::i16:
831 VT = MVT::i32;
Eric Christopher45c60712010-10-17 01:40:27 +0000832 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000833 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000834 case MVT::i32:
Eric Christopher45c60712010-10-17 01:40:27 +0000835 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STR;
Eric Christopher47650ec2010-10-16 01:10:35 +0000836 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000837 case MVT::f32:
838 if (!Subtarget->hasVFP2()) return false;
839 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000840 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000841 break;
842 case MVT::f64:
843 if (!Subtarget->hasVFP2()) return false;
844 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000845 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000846 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000847 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000848
Eric Christopherc9a91fd2010-10-15 23:07:10 +0000849 if (Base.Reg == ARM::SP && Offset == 0)
Eric Christopher15418772010-10-12 05:39:06 +0000850 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christophera3224252010-10-15 21:32:12 +0000851 SrcReg, true /*isKill*/, Base.FrameIndex,
Eric Christopher15418772010-10-12 05:39:06 +0000852 TLI.getRegClassFor(VT), TM.getRegisterInfo());
Eric Christopherd56d61a2010-10-17 01:51:42 +0000853 else if (Base.Reg == ARM::SP) {
854 // TODO: This won't work for NEON.
855 unsigned FI = Base.FrameIndex;
856 MachineMemOperand *MMO =
857 FuncInfo.MF->getMachineMemOperand(
858 MachinePointerInfo::getFixedStack(FI, Offset),
859 MachineMemOperand::MOStore,
860 MFI.getObjectSize(FI),
861 MFI.getObjectAlignment(FI));
862 if (isFloat || isThumb)
863 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
864 TII.get(StrOpc))
865 .addReg(SrcReg, getKillRegState(true))
866 .addFrameIndex(FI).addImm(Offset).addMemOperand(MMO));
867 else
868 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
869 TII.get(StrOpc))
870 .addReg(SrcReg, getKillRegState(true))
871 .addFrameIndex(FI).addReg(0).addImm(Offset).addMemOperand(MMO));
872 }
Eric Christopherb74558a2010-09-18 01:23:38 +0000873 // The thumb addressing mode has operands swapped from the arm addressing
874 // mode, the floating point one only has two operands.
Eric Christopher315030c2010-10-15 22:32:37 +0000875 else if (isFloat || isThumb)
Eric Christopherb74558a2010-09-18 01:23:38 +0000876 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000877 TII.get(StrOpc))
Eric Christophera3224252010-10-15 21:32:12 +0000878 .addReg(SrcReg).addReg(Base.Reg).addImm(Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000879 else
880 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000881 TII.get(StrOpc))
Eric Christophera3224252010-10-15 21:32:12 +0000882 .addReg(SrcReg).addReg(Base.Reg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000883
Eric Christopher318b6ee2010-09-02 00:53:56 +0000884 return true;
885}
886
Eric Christopher43b62be2010-09-27 06:02:23 +0000887bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000888 Value *Op0 = I->getOperand(0);
889 unsigned SrcReg = 0;
890
Eric Christopher543cf052010-09-01 22:16:27 +0000891 // Yay type legalization
892 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000893 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000894 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000895
Eric Christopher1b61ef42010-09-02 01:48:11 +0000896 // Get the value to be stored into a register.
897 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000898 if (SrcReg == 0)
899 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000900
Eric Christopher318b6ee2010-09-02 00:53:56 +0000901 // Our register and offset with innocuous defaults.
Eric Christophera3224252010-10-15 21:32:12 +0000902 AddrBase Base = { 0, 0 };
Eric Christopher318b6ee2010-09-02 00:53:56 +0000903 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000904
Eric Christopher318b6ee2010-09-02 00:53:56 +0000905 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000906 if (!ARMComputeRegOffset(I->getOperand(1), Base, Offset))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000907 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000908
Eric Christophera3224252010-10-15 21:32:12 +0000909 ARMSimplifyRegOffset(Base, Offset, VT);
Eric Christophereae84392010-10-14 09:29:41 +0000910
Eric Christophera3224252010-10-15 21:32:12 +0000911 if (!ARMEmitStore(VT, SrcReg, Base, Offset)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000912
Eric Christophera5b1e682010-09-17 22:28:18 +0000913 return true;
914}
915
916static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
917 switch (Pred) {
918 // Needs two compares...
919 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000920 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000921 default:
922 assert(false && "Unhandled CmpInst::Predicate!");
923 return ARMCC::AL;
924 case CmpInst::ICMP_EQ:
925 case CmpInst::FCMP_OEQ:
926 return ARMCC::EQ;
927 case CmpInst::ICMP_SGT:
928 case CmpInst::FCMP_OGT:
929 return ARMCC::GT;
930 case CmpInst::ICMP_SGE:
931 case CmpInst::FCMP_OGE:
932 return ARMCC::GE;
933 case CmpInst::ICMP_UGT:
934 case CmpInst::FCMP_UGT:
935 return ARMCC::HI;
936 case CmpInst::FCMP_OLT:
937 return ARMCC::MI;
938 case CmpInst::ICMP_ULE:
939 case CmpInst::FCMP_OLE:
940 return ARMCC::LS;
941 case CmpInst::FCMP_ORD:
942 return ARMCC::VC;
943 case CmpInst::FCMP_UNO:
944 return ARMCC::VS;
945 case CmpInst::FCMP_UGE:
946 return ARMCC::PL;
947 case CmpInst::ICMP_SLT:
948 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000949 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000950 case CmpInst::ICMP_SLE:
951 case CmpInst::FCMP_ULE:
952 return ARMCC::LE;
953 case CmpInst::FCMP_UNE:
954 case CmpInst::ICMP_NE:
955 return ARMCC::NE;
956 case CmpInst::ICMP_UGE:
957 return ARMCC::HS;
958 case CmpInst::ICMP_ULT:
959 return ARMCC::LO;
960 }
Eric Christopher543cf052010-09-01 22:16:27 +0000961}
962
Eric Christopher43b62be2010-09-27 06:02:23 +0000963bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000964 const BranchInst *BI = cast<BranchInst>(I);
965 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
966 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000967
Eric Christophere5734102010-09-03 00:35:47 +0000968 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000969 // TODO: Try to avoid the re-computation in some places.
970 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000971 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000972
Eric Christopher229207a2010-09-29 01:14:47 +0000973 // Re-set the flags just in case.
974 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
975 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
976 .addReg(CondReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000977
Eric Christophere5734102010-09-03 00:35:47 +0000978 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000979 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000980 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000981 FastEmitBranch(FBB, DL);
982 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000983 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000984}
985
Eric Christopher43b62be2010-09-27 06:02:23 +0000986bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000987 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000988
Eric Christopherd43393a2010-09-08 23:13:45 +0000989 EVT VT;
990 const Type *Ty = CI->getOperand(0)->getType();
991 if (!isTypeLegal(Ty, VT))
992 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000993
Eric Christopherd43393a2010-09-08 23:13:45 +0000994 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
995 if (isFloat && !Subtarget->hasVFP2())
996 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000997
Eric Christopherd43393a2010-09-08 23:13:45 +0000998 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000999 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +00001000 switch (VT.getSimpleVT().SimpleTy) {
1001 default: return false;
1002 // TODO: Verify compares.
1003 case MVT::f32:
1004 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001005 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001006 break;
1007 case MVT::f64:
1008 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001009 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001010 break;
1011 case MVT::i32:
1012 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001013 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001014 break;
1015 }
1016
Eric Christopher229207a2010-09-29 01:14:47 +00001017 // Get the compare predicate.
1018 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001019
Eric Christopher229207a2010-09-29 01:14:47 +00001020 // We may not handle every CC for now.
1021 if (ARMPred == ARMCC::AL) return false;
1022
Eric Christopherd43393a2010-09-08 23:13:45 +00001023 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1024 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001025
Eric Christopherd43393a2010-09-08 23:13:45 +00001026 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1027 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001028
Eric Christopherd43393a2010-09-08 23:13:45 +00001029 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1030 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001031
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001032 // For floating point we need to move the result to a comparison register
1033 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001034 if (isFloat)
1035 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1036 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001037
Eric Christopher229207a2010-09-29 01:14:47 +00001038 // Now set a register based on the comparison. Explicitly set the predicates
1039 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001040 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001041 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001042 : ARM::GPRRegisterClass;
1043 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001044 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001045 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001046 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1048 .addReg(ZeroReg).addImm(1)
1049 .addImm(ARMPred).addReg(CondReg);
1050
Eric Christophera5b1e682010-09-17 22:28:18 +00001051 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001052 return true;
1053}
1054
Eric Christopher43b62be2010-09-27 06:02:23 +00001055bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001056 // Make sure we have VFP and that we're extending float to double.
1057 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001058
Eric Christopher46203602010-09-09 00:26:48 +00001059 Value *V = I->getOperand(0);
1060 if (!I->getType()->isDoubleTy() ||
1061 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001062
Eric Christopher46203602010-09-09 00:26:48 +00001063 unsigned Op = getRegForValue(V);
1064 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001065
Eric Christopher46203602010-09-09 00:26:48 +00001066 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001067 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001068 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001069 .addReg(Op));
1070 UpdateValueMap(I, Result);
1071 return true;
1072}
1073
Eric Christopher43b62be2010-09-27 06:02:23 +00001074bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001075 // Make sure we have VFP and that we're truncating double to float.
1076 if (!Subtarget->hasVFP2()) return false;
1077
1078 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001079 if (!(I->getType()->isFloatTy() &&
1080 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001081
1082 unsigned Op = getRegForValue(V);
1083 if (Op == 0) return false;
1084
1085 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001086 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001087 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001088 .addReg(Op));
1089 UpdateValueMap(I, Result);
1090 return true;
1091}
1092
Eric Christopher43b62be2010-09-27 06:02:23 +00001093bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001094 // Make sure we have VFP.
1095 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001096
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001097 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001098 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001099 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001100 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001101
Eric Christopher9a040492010-09-09 18:54:59 +00001102 unsigned Op = getRegForValue(I->getOperand(0));
1103 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001104
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001105 // The conversion routine works on fp-reg to fp-reg and the operand above
1106 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001107 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001108 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001109
Eric Christopher9a040492010-09-09 18:54:59 +00001110 unsigned Opc;
1111 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1112 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1113 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001114
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001115 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001116 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1117 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001118 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001119 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001120 return true;
1121}
1122
Eric Christopher43b62be2010-09-27 06:02:23 +00001123bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001124 // Make sure we have VFP.
1125 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001126
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001127 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001128 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001129 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001130 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001131
Eric Christopher9a040492010-09-09 18:54:59 +00001132 unsigned Op = getRegForValue(I->getOperand(0));
1133 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001134
Eric Christopher9a040492010-09-09 18:54:59 +00001135 unsigned Opc;
1136 const Type *OpTy = I->getOperand(0)->getType();
1137 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1138 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1139 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001140
Eric Christopher022b7fb2010-10-05 23:13:24 +00001141 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1142 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001143 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1144 ResultReg)
1145 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001146
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001147 // This result needs to be in an integer register, but the conversion only
1148 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001149 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001150 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001151
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001152 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001153 return true;
1154}
1155
Eric Christopher3bbd3962010-10-11 08:27:59 +00001156bool ARMFastISel::SelectSelect(const Instruction *I) {
1157 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1158 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1159 return false;
1160
1161 // Things need to be register sized for register moves.
1162 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1163 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1164
1165 unsigned CondReg = getRegForValue(I->getOperand(0));
1166 if (CondReg == 0) return false;
1167 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1168 if (Op1Reg == 0) return false;
1169 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1170 if (Op2Reg == 0) return false;
1171
1172 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1173 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1174 .addReg(CondReg).addImm(1));
1175 unsigned ResultReg = createResultReg(RC);
1176 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1177 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1178 .addReg(Op1Reg).addReg(Op2Reg)
1179 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1180 UpdateValueMap(I, ResultReg);
1181 return true;
1182}
1183
Eric Christopher08637852010-09-30 22:34:19 +00001184bool ARMFastISel::SelectSDiv(const Instruction *I) {
1185 EVT VT;
1186 const Type *Ty = I->getType();
1187 if (!isTypeLegal(Ty, VT))
1188 return false;
1189
1190 // If we have integer div support we should have selected this automagically.
1191 // In case we have a real miss go ahead and return false and we'll pick
1192 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001193 if (Subtarget->hasDivide()) return false;
1194
Eric Christopher08637852010-09-30 22:34:19 +00001195 // Otherwise emit a libcall.
1196 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001197 if (VT == MVT::i8)
1198 LC = RTLIB::SDIV_I8;
1199 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001200 LC = RTLIB::SDIV_I16;
1201 else if (VT == MVT::i32)
1202 LC = RTLIB::SDIV_I32;
1203 else if (VT == MVT::i64)
1204 LC = RTLIB::SDIV_I64;
1205 else if (VT == MVT::i128)
1206 LC = RTLIB::SDIV_I128;
1207 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001208
Eric Christopher08637852010-09-30 22:34:19 +00001209 return ARMEmitLibcall(I, LC);
1210}
1211
Eric Christopher6a880d62010-10-11 08:37:26 +00001212bool ARMFastISel::SelectSRem(const Instruction *I) {
1213 EVT VT;
1214 const Type *Ty = I->getType();
1215 if (!isTypeLegal(Ty, VT))
1216 return false;
1217
1218 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1219 if (VT == MVT::i8)
1220 LC = RTLIB::SREM_I8;
1221 else if (VT == MVT::i16)
1222 LC = RTLIB::SREM_I16;
1223 else if (VT == MVT::i32)
1224 LC = RTLIB::SREM_I32;
1225 else if (VT == MVT::i64)
1226 LC = RTLIB::SREM_I64;
1227 else if (VT == MVT::i128)
1228 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001229 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001230
Eric Christopher6a880d62010-10-11 08:37:26 +00001231 return ARMEmitLibcall(I, LC);
1232}
1233
Eric Christopher43b62be2010-09-27 06:02:23 +00001234bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001235 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001236
Eric Christopherbc39b822010-09-09 00:53:57 +00001237 // We can get here in the case when we want to use NEON for our fp
1238 // operations, but can't figure out how to. Just use the vfp instructions
1239 // if we have them.
1240 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001241 const Type *Ty = I->getType();
1242 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1243 if (isFloat && !Subtarget->hasVFP2())
1244 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001245
Eric Christopherbc39b822010-09-09 00:53:57 +00001246 unsigned Op1 = getRegForValue(I->getOperand(0));
1247 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001248
Eric Christopherbc39b822010-09-09 00:53:57 +00001249 unsigned Op2 = getRegForValue(I->getOperand(1));
1250 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001251
Eric Christopherbc39b822010-09-09 00:53:57 +00001252 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +00001253 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1254 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001255 switch (ISDOpcode) {
1256 default: return false;
1257 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001258 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001259 break;
1260 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001261 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001262 break;
1263 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001264 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001265 break;
1266 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001267 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001268 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1269 TII.get(Opc), ResultReg)
1270 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001271 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001272 return true;
1273}
1274
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001275// Call Handling Code
1276
1277// This is largely taken directly from CCAssignFnForNode - we don't support
1278// varargs in FastISel so that part has been removed.
1279// TODO: We may not support all of this.
1280CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1281 switch (CC) {
1282 default:
1283 llvm_unreachable("Unsupported calling convention");
1284 case CallingConv::C:
1285 case CallingConv::Fast:
1286 // Use target triple & subtarget features to do actual dispatch.
1287 if (Subtarget->isAAPCS_ABI()) {
1288 if (Subtarget->hasVFP2() &&
1289 FloatABIType == FloatABI::Hard)
1290 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1291 else
1292 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1293 } else
1294 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1295 case CallingConv::ARM_AAPCS_VFP:
1296 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1297 case CallingConv::ARM_AAPCS:
1298 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1299 case CallingConv::ARM_APCS:
1300 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1301 }
1302}
1303
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001304bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1305 SmallVectorImpl<unsigned> &ArgRegs,
1306 SmallVectorImpl<EVT> &ArgVTs,
1307 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1308 SmallVectorImpl<unsigned> &RegArgs,
1309 CallingConv::ID CC,
1310 unsigned &NumBytes) {
1311 SmallVector<CCValAssign, 16> ArgLocs;
1312 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1313 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1314
1315 // Get a count of how many bytes are to be pushed on the stack.
1316 NumBytes = CCInfo.getNextStackOffset();
1317
1318 // Issue CALLSEQ_START
1319 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1321 TII.get(AdjStackDown))
1322 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001323
1324 // Process the args.
1325 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1326 CCValAssign &VA = ArgLocs[i];
1327 unsigned Arg = ArgRegs[VA.getValNo()];
1328 EVT ArgVT = ArgVTs[VA.getValNo()];
1329
Eric Christopherf9764fa2010-09-30 20:49:44 +00001330 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001331 switch (VA.getLocInfo()) {
1332 case CCValAssign::Full: break;
1333 default:
Eric Christopher11077342010-10-07 05:14:08 +00001334 // TODO: Handle arg promotion.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001335 return false;
1336 }
1337
1338 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001339 // TODO: We need custom lowering for f64 args.
1340 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001341 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001342 VA.getLocReg())
1343 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001344 RegArgs.push_back(VA.getLocReg());
1345 } else {
1346 // Need to store
1347 return false;
1348 }
1349 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001350
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001351 return true;
1352}
1353
1354bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1355 const Instruction *I, CallingConv::ID CC,
1356 unsigned &NumBytes) {
1357 // Issue CALLSEQ_END
1358 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1360 TII.get(AdjStackUp))
1361 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001362
1363 // Now the return value.
1364 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1365 SmallVector<CCValAssign, 16> RVLocs;
1366 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1367 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1368
1369 // Copy all of the result registers out of their specified physreg.
Eric Christopher14df8822010-10-01 00:00:11 +00001370 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1371 // For this move we copy into two registers and then move into the
1372 // double fp reg we want.
1373 // TODO: Are the copies necessary?
1374 TargetRegisterClass *CopyRC = TLI.getRegClassFor(MVT::i32);
1375 unsigned Copy1 = createResultReg(CopyRC);
1376 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1377 Copy1).addReg(RVLocs[0].getLocReg());
1378 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001379
Eric Christopher14df8822010-10-01 00:00:11 +00001380 unsigned Copy2 = createResultReg(CopyRC);
1381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1382 Copy2).addReg(RVLocs[1].getLocReg());
1383 UsedRegs.push_back(RVLocs[1].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001384
Eric Christopher14df8822010-10-01 00:00:11 +00001385 EVT DestVT = RVLocs[0].getValVT();
1386 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1387 unsigned ResultReg = createResultReg(DstRC);
1388 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1389 TII.get(ARM::VMOVDRR), ResultReg)
1390 .addReg(Copy1).addReg(Copy2));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001391
1392 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001393 UpdateValueMap(I, ResultReg);
1394 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001395 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001396 EVT CopyVT = RVLocs[0].getValVT();
1397 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001398
Eric Christopher14df8822010-10-01 00:00:11 +00001399 unsigned ResultReg = createResultReg(DstRC);
1400 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1401 ResultReg).addReg(RVLocs[0].getLocReg());
1402 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001403
Eric Christopherdccd2c32010-10-11 08:38:55 +00001404 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001405 UpdateValueMap(I, ResultReg);
1406 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001407 }
1408
Eric Christopherdccd2c32010-10-11 08:38:55 +00001409 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001410}
1411
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001412// A quick function that will emit a call for a named libcall in F with the
1413// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001414// can emit a call for any libcall we can produce. This is an abridged version
1415// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001416// like computed function pointers or strange arguments at call sites.
1417// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1418// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001419bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1420 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001421
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001422 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001423 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001424 EVT RetVT;
1425 if (RetTy->isVoidTy())
1426 RetVT = MVT::isVoid;
1427 else if (!isTypeLegal(RetTy, RetVT))
1428 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001429
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001430 // For now we're using BLX etc on the assumption that we have v5t ops.
1431 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001432
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001433 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001434 SmallVector<Value*, 8> Args;
1435 SmallVector<unsigned, 8> ArgRegs;
1436 SmallVector<EVT, 8> ArgVTs;
1437 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1438 Args.reserve(I->getNumOperands());
1439 ArgRegs.reserve(I->getNumOperands());
1440 ArgVTs.reserve(I->getNumOperands());
1441 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001442 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001443 Value *Op = I->getOperand(i);
1444 unsigned Arg = getRegForValue(Op);
1445 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001446
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001447 const Type *ArgTy = Op->getType();
1448 EVT ArgVT;
1449 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001450
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001451 ISD::ArgFlagsTy Flags;
1452 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1453 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001454
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001455 Args.push_back(Op);
1456 ArgRegs.push_back(Arg);
1457 ArgVTs.push_back(ArgVT);
1458 ArgFlags.push_back(Flags);
1459 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001460
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001461 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001462 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001463 unsigned NumBytes;
1464 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1465 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001466
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001467 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001468 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001469 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001470 unsigned CallOpc;
1471 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001472 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001473 else
1474 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001475 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001476 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001477
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001478 // Add implicit physical register uses to the call.
1479 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1480 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001481
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001482 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001483 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001484 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001485
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001486 // Set all unused physreg defs as dead.
1487 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001488
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001489 return true;
1490}
1491
Eric Christopherf9764fa2010-09-30 20:49:44 +00001492bool ARMFastISel::SelectCall(const Instruction *I) {
1493 const CallInst *CI = cast<CallInst>(I);
1494 const Value *Callee = CI->getCalledValue();
1495
1496 // Can't handle inline asm or worry about intrinsics yet.
1497 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1498
Eric Christophere6ca6772010-10-01 21:33:12 +00001499 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001500 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001501 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1502 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001503
Eric Christopherf9764fa2010-09-30 20:49:44 +00001504 // Check the calling convention.
1505 ImmutableCallSite CS(CI);
1506 CallingConv::ID CC = CS.getCallingConv();
1507 // TODO: Avoid some calling conventions?
1508 if (CC != CallingConv::C) {
Eric Christophere540a6f2010-10-05 23:50:58 +00001509 // errs() << "Can't handle calling convention: " << CC << "\n";
Eric Christopherf9764fa2010-09-30 20:49:44 +00001510 return false;
1511 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001512
Eric Christopherf9764fa2010-09-30 20:49:44 +00001513 // Let SDISel handle vararg functions.
1514 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1515 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1516 if (FTy->isVarArg())
1517 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001518
Eric Christopherf9764fa2010-09-30 20:49:44 +00001519 // Handle *simple* calls for now.
1520 const Type *RetTy = I->getType();
1521 EVT RetVT;
1522 if (RetTy->isVoidTy())
1523 RetVT = MVT::isVoid;
1524 else if (!isTypeLegal(RetTy, RetVT))
1525 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001526
Eric Christopherf9764fa2010-09-30 20:49:44 +00001527 // For now we're using BLX etc on the assumption that we have v5t ops.
1528 // TODO: Maybe?
1529 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001530
Eric Christopherf9764fa2010-09-30 20:49:44 +00001531 // Set up the argument vectors.
1532 SmallVector<Value*, 8> Args;
1533 SmallVector<unsigned, 8> ArgRegs;
1534 SmallVector<EVT, 8> ArgVTs;
1535 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1536 Args.reserve(CS.arg_size());
1537 ArgRegs.reserve(CS.arg_size());
1538 ArgVTs.reserve(CS.arg_size());
1539 ArgFlags.reserve(CS.arg_size());
1540 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1541 i != e; ++i) {
1542 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001543
Eric Christopherf9764fa2010-09-30 20:49:44 +00001544 if (Arg == 0)
1545 return false;
1546 ISD::ArgFlagsTy Flags;
1547 unsigned AttrInd = i - CS.arg_begin() + 1;
1548 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1549 Flags.setSExt();
1550 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1551 Flags.setZExt();
1552
1553 // FIXME: Only handle *easy* calls for now.
1554 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1555 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1556 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1557 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1558 return false;
1559
1560 const Type *ArgTy = (*i)->getType();
1561 EVT ArgVT;
1562 if (!isTypeLegal(ArgTy, ArgVT))
1563 return false;
1564 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1565 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001566
Eric Christopherf9764fa2010-09-30 20:49:44 +00001567 Args.push_back(*i);
1568 ArgRegs.push_back(Arg);
1569 ArgVTs.push_back(ArgVT);
1570 ArgFlags.push_back(Flags);
1571 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001572
Eric Christopherf9764fa2010-09-30 20:49:44 +00001573 // Handle the arguments now that we've gotten them.
1574 SmallVector<unsigned, 4> RegArgs;
1575 unsigned NumBytes;
1576 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1577 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001578
Eric Christopherf9764fa2010-09-30 20:49:44 +00001579 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001580 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001581 MachineInstrBuilder MIB;
1582 unsigned CallOpc;
1583 if(isThumb)
1584 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1585 else
1586 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1587 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1588 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001589
Eric Christopherf9764fa2010-09-30 20:49:44 +00001590 // Add implicit physical register uses to the call.
1591 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1592 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001593
Eric Christopherf9764fa2010-09-30 20:49:44 +00001594 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001595 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001596 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001597
Eric Christopherf9764fa2010-09-30 20:49:44 +00001598 // Set all unused physreg defs as dead.
1599 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001600
Eric Christopherf9764fa2010-09-30 20:49:44 +00001601 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001602
Eric Christopherf9764fa2010-09-30 20:49:44 +00001603}
1604
Eric Christopher56d2b722010-09-02 23:43:26 +00001605// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001606bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001607 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001608 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001609
Eric Christopherab695882010-07-21 22:26:11 +00001610 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001611 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001612 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001613 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001614 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001615 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001616 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001617 case Instruction::ICmp:
1618 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001619 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001620 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001621 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001622 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001623 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001624 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001625 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001626 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001627 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001628 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001629 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001630 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001631 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001632 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001633 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001634 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001635 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001636 case Instruction::SRem:
1637 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001638 case Instruction::Call:
1639 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001640 case Instruction::Select:
1641 return SelectSelect(I);
Eric Christopherab695882010-07-21 22:26:11 +00001642 default: break;
1643 }
1644 return false;
1645}
1646
1647namespace llvm {
1648 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001649 // Completely untested on non-darwin.
1650 const TargetMachine &TM = funcInfo.MF->getTarget();
1651 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher8ff9a9d2010-10-11 20:26:21 +00001652 if (Subtarget->isTargetDarwin() && EnableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001653 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001654 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001655 }
1656}