blob: 110aa9804cdab2a9eeb421bd53ce05e19e189cb3 [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 Christopher6e5367d2010-10-18 22:53:53 +000051DisableARMFastISel("disable-arm-fast-isel",
52 cl::desc("Turn off 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
59 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
60 /// make the right decision when generating code for different targets.
61 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000062 const TargetMachine &TM;
63 const TargetInstrInfo &TII;
64 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000065 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000066
Eric Christopher8cf6c602010-09-29 22:24:45 +000067 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000068 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000069 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000070
Eric Christopherab695882010-07-21 22:26:11 +000071 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000072 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000073 : FastISel(funcInfo),
74 TM(funcInfo.MF->getTarget()),
75 TII(*TM.getInstrInfo()),
76 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000077 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000078 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000079 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000080 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000081 }
82
Eric Christophercb592292010-08-20 00:20:31 +000083 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000084 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
85 const TargetRegisterClass *RC);
86 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill);
89 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
90 const TargetRegisterClass *RC,
91 unsigned Op0, bool Op0IsKill,
92 unsigned Op1, bool Op1IsKill);
93 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
94 const TargetRegisterClass *RC,
95 unsigned Op0, bool Op0IsKill,
96 uint64_t Imm);
97 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill,
100 const ConstantFP *FPImm);
101 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
102 const TargetRegisterClass *RC,
103 uint64_t Imm);
104 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
105 const TargetRegisterClass *RC,
106 unsigned Op0, bool Op0IsKill,
107 unsigned Op1, bool Op1IsKill,
108 uint64_t Imm);
109 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
110 unsigned Op0, bool Op0IsKill,
111 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000112
Eric Christophercb592292010-08-20 00:20:31 +0000113 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000114 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000115 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000116 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000117
118 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000119
Eric Christopher83007122010-08-23 21:44:12 +0000120 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000121 private:
Eric Christopher17787722010-10-21 21:47:51 +0000122 bool SelectLoad(const Instruction *I);
123 bool SelectStore(const Instruction *I);
124 bool SelectBranch(const Instruction *I);
125 bool SelectCmp(const Instruction *I);
126 bool SelectFPExt(const Instruction *I);
127 bool SelectFPTrunc(const Instruction *I);
128 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
129 bool SelectSIToFP(const Instruction *I);
130 bool SelectFPToSI(const Instruction *I);
131 bool SelectSDiv(const Instruction *I);
132 bool SelectSRem(const Instruction *I);
133 bool SelectCall(const Instruction *I);
134 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000135 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000136
Eric Christopher83007122010-08-23 21:44:12 +0000137 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000138 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000139 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000140 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher404be0c2010-10-17 11:08:44 +0000141 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Base, int Offset);
142 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Base, int Offset);
143 bool ARMComputeRegOffset(const Value *Obj, unsigned &Base, int &Offset);
144 void ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000145 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000146 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000147 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000148 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000149 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000150
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000151 // Call handling routines.
152 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000153 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
154 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000155 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.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000198// TODO: If we want to support thumb1 then we'll need to deal with optional
199// CPSR defs that need to be added before the remaining operands. See s_cc_out
200// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000201const MachineInstrBuilder &
202ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
203 MachineInstr *MI = &*MIB;
204
205 // Do we use a predicate?
206 if (TII.isPredicable(MI))
207 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000208
Eric Christopher456144e2010-08-19 00:37:05 +0000209 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
210 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000211 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000212 if (DefinesOptionalPredicate(MI, &CPSR)) {
213 if (CPSR)
214 AddDefaultT1CC(MIB);
215 else
216 AddDefaultCC(MIB);
217 }
218 return MIB;
219}
220
Eric Christopher0fe7d542010-08-17 01:25:29 +0000221unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
222 const TargetRegisterClass* RC) {
223 unsigned ResultReg = createResultReg(RC);
224 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
225
Eric Christopher456144e2010-08-19 00:37:05 +0000226 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000227 return ResultReg;
228}
229
230unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
231 const TargetRegisterClass *RC,
232 unsigned Op0, bool Op0IsKill) {
233 unsigned ResultReg = createResultReg(RC);
234 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
235
236 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000237 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000238 .addReg(Op0, Op0IsKill * RegState::Kill));
239 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000240 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000241 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000242 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000243 TII.get(TargetOpcode::COPY), ResultReg)
244 .addReg(II.ImplicitDefs[0]));
245 }
246 return ResultReg;
247}
248
249unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
250 const TargetRegisterClass *RC,
251 unsigned Op0, bool Op0IsKill,
252 unsigned Op1, bool Op1IsKill) {
253 unsigned ResultReg = createResultReg(RC);
254 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
255
256 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000257 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000258 .addReg(Op0, Op0IsKill * RegState::Kill)
259 .addReg(Op1, Op1IsKill * RegState::Kill));
260 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000261 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000262 .addReg(Op0, Op0IsKill * RegState::Kill)
263 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000264 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000265 TII.get(TargetOpcode::COPY), ResultReg)
266 .addReg(II.ImplicitDefs[0]));
267 }
268 return ResultReg;
269}
270
271unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
272 const TargetRegisterClass *RC,
273 unsigned Op0, bool Op0IsKill,
274 uint64_t Imm) {
275 unsigned ResultReg = createResultReg(RC);
276 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
277
278 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000279 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000280 .addReg(Op0, Op0IsKill * RegState::Kill)
281 .addImm(Imm));
282 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000284 .addReg(Op0, Op0IsKill * RegState::Kill)
285 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000287 TII.get(TargetOpcode::COPY), ResultReg)
288 .addReg(II.ImplicitDefs[0]));
289 }
290 return ResultReg;
291}
292
293unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
294 const TargetRegisterClass *RC,
295 unsigned Op0, bool Op0IsKill,
296 const ConstantFP *FPImm) {
297 unsigned ResultReg = createResultReg(RC);
298 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
299
300 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000302 .addReg(Op0, Op0IsKill * RegState::Kill)
303 .addFPImm(FPImm));
304 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000306 .addReg(Op0, Op0IsKill * RegState::Kill)
307 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 TII.get(TargetOpcode::COPY), ResultReg)
310 .addReg(II.ImplicitDefs[0]));
311 }
312 return ResultReg;
313}
314
315unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
316 const TargetRegisterClass *RC,
317 unsigned Op0, bool Op0IsKill,
318 unsigned Op1, bool Op1IsKill,
319 uint64_t Imm) {
320 unsigned ResultReg = createResultReg(RC);
321 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
322
323 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000325 .addReg(Op0, Op0IsKill * RegState::Kill)
326 .addReg(Op1, Op1IsKill * RegState::Kill)
327 .addImm(Imm));
328 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000330 .addReg(Op0, Op0IsKill * RegState::Kill)
331 .addReg(Op1, Op1IsKill * RegState::Kill)
332 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000334 TII.get(TargetOpcode::COPY), ResultReg)
335 .addReg(II.ImplicitDefs[0]));
336 }
337 return ResultReg;
338}
339
340unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
341 const TargetRegisterClass *RC,
342 uint64_t Imm) {
343 unsigned ResultReg = createResultReg(RC);
344 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000345
Eric Christopher0fe7d542010-08-17 01:25:29 +0000346 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000348 .addImm(Imm));
349 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000350 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000351 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000353 TII.get(TargetOpcode::COPY), ResultReg)
354 .addReg(II.ImplicitDefs[0]));
355 }
356 return ResultReg;
357}
358
359unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
360 unsigned Op0, bool Op0IsKill,
361 uint32_t Idx) {
362 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
363 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
364 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000365 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000366 DL, TII.get(TargetOpcode::COPY), ResultReg)
367 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
368 return ResultReg;
369}
370
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000371// TODO: Don't worry about 64-bit now, but when this is fixed remove the
372// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000373unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000374 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000375
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000376 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
378 TII.get(ARM::VMOVRS), MoveReg)
379 .addReg(SrcReg));
380 return MoveReg;
381}
382
383unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000384 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000385
Eric Christopheraa3ace12010-09-09 20:49:25 +0000386 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
387 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000388 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000389 .addReg(SrcReg));
390 return MoveReg;
391}
392
Eric Christopher9ed58df2010-09-09 00:19:41 +0000393// For double width floating point we need to materialize two constants
394// (the high and the low) into integer registers then use a move to get
395// the combined constant into an FP reg.
396unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
397 const APFloat Val = CFP->getValueAPF();
398 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000399
Eric Christopher9ed58df2010-09-09 00:19:41 +0000400 // This checks to see if we can use VFP3 instructions to materialize
401 // a constant, otherwise we have to go through the constant pool.
402 if (TLI.isFPImmLegal(Val, VT)) {
403 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
404 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
405 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
406 DestReg)
407 .addFPImm(CFP));
408 return DestReg;
409 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000410
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000411 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000412 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000413
Eric Christopher238bb162010-09-09 23:50:00 +0000414 // MachineConstantPool wants an explicit alignment.
415 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
416 if (Align == 0) {
417 // TODO: Figure out if this is correct.
418 Align = TD.getTypeAllocSize(CFP->getType());
419 }
420 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
421 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
422 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000423
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000424 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000425 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
426 DestReg)
427 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000428 .addReg(0));
429 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000430}
431
Eric Christopher744c7c82010-09-28 22:47:54 +0000432unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000433
Eric Christopher744c7c82010-09-28 22:47:54 +0000434 // For now 32-bit only.
435 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000436
Eric Christopher56d2b722010-09-02 23:43:26 +0000437 // MachineConstantPool wants an explicit alignment.
438 unsigned Align = TD.getPrefTypeAlignment(C->getType());
439 if (Align == 0) {
440 // TODO: Figure out if this is correct.
441 Align = TD.getTypeAllocSize(C->getType());
442 }
443 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000444 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000445
Eric Christopher56d2b722010-09-02 23:43:26 +0000446 if (isThumb)
447 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000448 TII.get(ARM::t2LDRpci), DestReg)
449 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000450 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000451 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000452 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000453 TII.get(ARM::LDRcp), DestReg)
454 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000455 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000456
Eric Christopher56d2b722010-09-02 23:43:26 +0000457 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000458}
459
Eric Christopherc9932f62010-10-01 23:24:42 +0000460unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000461 // For now 32-bit only.
462 if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000463
Eric Christopher890dbbe2010-10-02 00:32:44 +0000464 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000465
Eric Christopher890dbbe2010-10-02 00:32:44 +0000466 // TODO: No external globals for now.
467 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000468
Eric Christopher890dbbe2010-10-02 00:32:44 +0000469 // TODO: Need more magic for ARM PIC.
470 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000471
Eric Christopher890dbbe2010-10-02 00:32:44 +0000472 // MachineConstantPool wants an explicit alignment.
473 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
474 if (Align == 0) {
475 // TODO: Figure out if this is correct.
476 Align = TD.getTypeAllocSize(GV->getType());
477 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000478
Eric Christopher890dbbe2010-10-02 00:32:44 +0000479 // Grab index.
480 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
481 unsigned Id = AFI->createConstPoolEntryUId();
482 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
483 ARMCP::CPValue, PCAdj);
484 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000485
Eric Christopher890dbbe2010-10-02 00:32:44 +0000486 // Load value.
487 MachineInstrBuilder MIB;
488 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
489 if (isThumb) {
490 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
491 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
492 .addConstantPoolIndex(Idx);
493 if (RelocM == Reloc::PIC_)
494 MIB.addImm(Id);
495 } else {
496 // The extra reg and immediate are for addrmode2.
497 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
498 DestReg)
499 .addConstantPoolIndex(Idx)
500 .addReg(0).addImm(0);
501 }
502 AddOptionalDefs(MIB);
503 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000504}
505
Eric Christopher9ed58df2010-09-09 00:19:41 +0000506unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
507 EVT VT = TLI.getValueType(C->getType(), true);
508
509 // Only handle simple types.
510 if (!VT.isSimple()) return 0;
511
512 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
513 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000514 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
515 return ARMMaterializeGV(GV, VT);
516 else if (isa<ConstantInt>(C))
517 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000518
Eric Christopherc9932f62010-10-01 23:24:42 +0000519 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000520}
521
Eric Christopherf9764fa2010-09-30 20:49:44 +0000522unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
523 // Don't handle dynamic allocas.
524 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000525
Eric Christopherf9764fa2010-09-30 20:49:44 +0000526 EVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000527 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000528
Eric Christopherf9764fa2010-09-30 20:49:44 +0000529 DenseMap<const AllocaInst*, int>::iterator SI =
530 FuncInfo.StaticAllocaMap.find(AI);
531
532 // This will get lowered later into the correct offsets and registers
533 // via rewriteXFrameIndex.
534 if (SI != FuncInfo.StaticAllocaMap.end()) {
535 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
536 unsigned ResultReg = createResultReg(RC);
537 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
538 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
539 TII.get(Opc), ResultReg)
540 .addFrameIndex(SI->second)
541 .addImm(0));
542 return ResultReg;
543 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000544
Eric Christopherf9764fa2010-09-30 20:49:44 +0000545 return 0;
546}
547
Eric Christopherb1cc8482010-08-25 07:23:49 +0000548bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
549 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000550
Eric Christopherb1cc8482010-08-25 07:23:49 +0000551 // Only handle simple types.
552 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000553
Eric Christopherdc908042010-08-31 01:28:42 +0000554 // Handle all legal types, i.e. a register that will directly hold this
555 // value.
556 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000557}
558
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000559bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
560 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000561
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000562 // If this is a type than can be sign or zero-extended to a basic operation
563 // go ahead and accept it now.
564 if (VT == MVT::i8 || VT == MVT::i16)
565 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000566
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000567 return false;
568}
569
Eric Christophercb0b04b2010-08-24 00:07:24 +0000570// Computes the Reg+Offset to get to an object.
Eric Christopher404be0c2010-10-17 11:08:44 +0000571bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Base,
Eric Christopher83007122010-08-23 21:44:12 +0000572 int &Offset) {
573 // Some boilerplate from the X86 FastISel.
574 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000575 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000576 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000577 // Don't walk into other basic blocks; it's possible we haven't
578 // visited them yet, so the instructions may not yet be assigned
579 // virtual registers.
580 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
581 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000582 Opcode = I->getOpcode();
583 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000584 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000585 Opcode = C->getOpcode();
586 U = C;
587 }
588
Eric Christophercb0b04b2010-08-24 00:07:24 +0000589 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000590 if (Ty->getAddressSpace() > 255)
591 // Fast instruction selection doesn't support the special
592 // address spaces.
593 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000594
Eric Christopher83007122010-08-23 21:44:12 +0000595 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000596 default:
Eric Christopher83007122010-08-23 21:44:12 +0000597 break;
Eric Christopher55324332010-10-12 00:43:21 +0000598 case Instruction::BitCast: {
599 // Look through bitcasts.
Eric Christophera3224252010-10-15 21:32:12 +0000600 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000601 }
602 case Instruction::IntToPtr: {
603 // Look past no-op inttoptrs.
604 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000605 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000606 break;
607 }
608 case Instruction::PtrToInt: {
609 // Look past no-op ptrtoints.
610 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000611 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000612 break;
613 }
Eric Christophereae84392010-10-14 09:29:41 +0000614 case Instruction::GetElementPtr: {
615 int SavedOffset = Offset;
Eric Christopher404be0c2010-10-17 11:08:44 +0000616 unsigned SavedBase = Base;
Eric Christophereae84392010-10-14 09:29:41 +0000617 int TmpOffset = Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000618
Eric Christophereae84392010-10-14 09:29:41 +0000619 // Iterate through the GEP folding the constants into offsets where
620 // we can.
621 gep_type_iterator GTI = gep_type_begin(U);
622 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
623 i != e; ++i, ++GTI) {
624 const Value *Op = *i;
625 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
626 const StructLayout *SL = TD.getStructLayout(STy);
627 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
628 TmpOffset += SL->getElementOffset(Idx);
629 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000630 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
631 SmallVector<const Value *, 4> Worklist;
632 Worklist.push_back(Op);
633 do {
634 Op = Worklist.pop_back_val();
635 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
636 // Constant-offset addressing.
637 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000638 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000639 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
640 // An add with a constant operand. Fold the constant.
641 ConstantInt *CI =
642 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
643 TmpOffset += CI->getSExtValue() * S;
644 // Add the other operand back to the work list.
645 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
646 } else
647 goto unsupported_gep;
648 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000649 }
650 }
Eric Christopher2896df82010-10-15 18:02:07 +0000651
652 // Try to grab the base operand now.
Eric Christophereae84392010-10-14 09:29:41 +0000653 Offset = TmpOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000654 if (ARMComputeRegOffset(U->getOperand(0), Base, Offset)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000655
656 // We failed, restore everything and try the other options.
Eric Christophereae84392010-10-14 09:29:41 +0000657 Offset = SavedOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000658 Base = SavedBase;
Eric Christopher2896df82010-10-15 18:02:07 +0000659
Eric Christophereae84392010-10-14 09:29:41 +0000660 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000661 break;
662 }
Eric Christopher83007122010-08-23 21:44:12 +0000663 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000664 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopherd56d61a2010-10-17 01:51:42 +0000665 unsigned Reg = TargetMaterializeAlloca(AI);
666
667 if (Reg == 0) return false;
668
Eric Christopher404be0c2010-10-17 11:08:44 +0000669 Base = Reg;
Eric Christopherd56d61a2010-10-17 01:51:42 +0000670 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000671 }
672 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000673
Eric Christophera9c57512010-10-13 21:41:51 +0000674 // Materialize the global variable's address into a reg which can
675 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000676 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000677 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
678 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000679
Eric Christopher404be0c2010-10-17 11:08:44 +0000680 Base = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000681 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000682 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000683
Eric Christophercb0b04b2010-08-24 00:07:24 +0000684 // Try to get this in a register if nothing else has worked.
Eric Christopher61d69da2010-11-02 01:22:45 +0000685 if (Base == 0) Base = getRegForValue(Obj);
Eric Christopher404be0c2010-10-17 11:08:44 +0000686 return Base != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000687}
688
Eric Christopher404be0c2010-10-17 11:08:44 +0000689void ARMFastISel::ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000690
Eric Christopher212ae932010-10-21 19:40:30 +0000691 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000692
Eric Christopher212ae932010-10-21 19:40:30 +0000693 bool needsLowering = false;
694 switch (VT.getSimpleVT().SimpleTy) {
695 default:
696 assert(false && "Unhandled load/store type!");
697 case MVT::i1:
698 case MVT::i8:
699 case MVT::i16:
700 case MVT::i32:
701 // Integer loads/stores handle 12-bit offsets.
702 needsLowering = ((Offset & 0xfff) != Offset);
703 break;
704 case MVT::f32:
705 case MVT::f64:
706 // Floating point operands handle 8-bit offsets.
707 needsLowering = ((Offset & 0xff) != Offset);
708 break;
709 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000710
Eric Christopher212ae932010-10-21 19:40:30 +0000711 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000712 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000713 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000714 ARMCC::CondCodes Pred = ARMCC::AL;
715 unsigned PredReg = 0;
716
Eric Christopher2896df82010-10-15 18:02:07 +0000717 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
718 ARM::GPRRegisterClass;
719 unsigned BaseReg = createResultReg(RC);
720
Eric Christophereaa204b2010-09-02 01:39:14 +0000721 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000722 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher404be0c2010-10-17 11:08:44 +0000723 BaseReg, Base, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000724 static_cast<const ARMBaseInstrInfo&>(TII));
725 else {
726 assert(AFI->isThumb2Function());
727 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher404be0c2010-10-17 11:08:44 +0000728 BaseReg, Base, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000729 static_cast<const ARMBaseInstrInfo&>(TII));
730 }
Eric Christophereae84392010-10-14 09:29:41 +0000731 Offset = 0;
Eric Christopher404be0c2010-10-17 11:08:44 +0000732 Base = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000733 }
Eric Christopher83007122010-08-23 21:44:12 +0000734}
735
Eric Christopherb1cc8482010-08-25 07:23:49 +0000736bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
Eric Christopher404be0c2010-10-17 11:08:44 +0000737 unsigned Base, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000738
Eric Christopherb1cc8482010-08-25 07:23:49 +0000739 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000740 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000741 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000742 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000743 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000744 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000745 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000746 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000747 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000748 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000749 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000750 break;
751 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000752 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000753 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000754 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000755 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000756 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000757 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000758 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000759 case MVT::f32:
760 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000761 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000762 isFloat = true;
763 break;
764 case MVT::f64:
765 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000766 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000767 isFloat = true;
768 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000769 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000770
Eric Christopheree56ea62010-10-07 05:50:44 +0000771 ResultReg = createResultReg(RC);
Jim Grosbach6b156392010-10-27 21:39:08 +0000772
Eric Christopher212ae932010-10-21 19:40:30 +0000773 ARMSimplifyRegOffset(Base, Offset, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000774
Eric Christopher212ae932010-10-21 19:40:30 +0000775 // addrmode5 output depends on the selection dag addressing dividing the
776 // offset by 4 that it then later multiplies. Do this here as well.
777 if (isFloat)
778 Offset /= 4;
Jim Grosbach6b156392010-10-27 21:39:08 +0000779
Jim Grosbach3e556122010-10-26 22:37:02 +0000780 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
781 TII.get(Opc), ResultReg)
782 .addReg(Base).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000783 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000784}
785
Eric Christopher43b62be2010-09-27 06:02:23 +0000786bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000787 // Verify we have a legal type before going any further.
788 EVT VT;
789 if (!isLoadTypeLegal(I->getType(), VT))
790 return false;
791
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000792 // Our register and offset with innocuous defaults.
Eric Christopher404be0c2010-10-17 11:08:44 +0000793 unsigned Base = 0;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000794 int Offset = 0;
795
796 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000797 if (!ARMComputeRegOffset(I->getOperand(0), Base, Offset))
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000798 return false;
799
800 unsigned ResultReg;
Eric Christophera3224252010-10-15 21:32:12 +0000801 if (!ARMEmitLoad(VT, ResultReg, Base, Offset)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000802
803 UpdateValueMap(I, ResultReg);
804 return true;
805}
806
Eric Christopher318b6ee2010-09-02 00:53:56 +0000807bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
Eric Christopher404be0c2010-10-17 11:08:44 +0000808 unsigned Base, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000809 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000810 bool isFloat = false;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000811 bool needReg0Op = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000812 switch (VT.getSimpleVT().SimpleTy) {
813 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000814 case MVT::i1: {
815 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
816 ARM::GPRRegisterClass);
817 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
818 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
819 TII.get(Opc), Res)
820 .addReg(SrcReg).addImm(1));
821 SrcReg = Res;
822 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000823 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000824 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000825 break;
826 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000827 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000828 needReg0Op = true;
Eric Christopher15418772010-10-12 05:39:06 +0000829 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000830 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000831 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000832 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000833 case MVT::f32:
834 if (!Subtarget->hasVFP2()) return false;
835 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000836 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000837 break;
838 case MVT::f64:
839 if (!Subtarget->hasVFP2()) return false;
840 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000841 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000842 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000843 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000844
Eric Christopher212ae932010-10-21 19:40:30 +0000845 ARMSimplifyRegOffset(Base, Offset, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000846
Eric Christopher212ae932010-10-21 19:40:30 +0000847 // addrmode5 output depends on the selection dag addressing dividing the
848 // offset by 4 that it then later multiplies. Do this here as well.
849 if (isFloat)
850 Offset /= 4;
Jim Grosbach6b156392010-10-27 21:39:08 +0000851
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000852 // FIXME: The 'needReg0Op' bit goes away once STRH is converted to
853 // not use the mega-addrmode stuff.
854 if (!needReg0Op)
Eric Christopherb74558a2010-09-18 01:23:38 +0000855 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000856 TII.get(StrOpc))
Eric Christopher404be0c2010-10-17 11:08:44 +0000857 .addReg(SrcReg).addReg(Base).addImm(Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000858 else
859 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000860 TII.get(StrOpc))
Eric Christopher404be0c2010-10-17 11:08:44 +0000861 .addReg(SrcReg).addReg(Base).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000862
Eric Christopher318b6ee2010-09-02 00:53:56 +0000863 return true;
864}
865
Eric Christopher43b62be2010-09-27 06:02:23 +0000866bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000867 Value *Op0 = I->getOperand(0);
868 unsigned SrcReg = 0;
869
Eric Christopher543cf052010-09-01 22:16:27 +0000870 // Yay type legalization
871 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000872 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000873 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000874
Eric Christopher1b61ef42010-09-02 01:48:11 +0000875 // Get the value to be stored into a register.
876 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000877 if (SrcReg == 0)
878 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000879
Eric Christopher318b6ee2010-09-02 00:53:56 +0000880 // Our register and offset with innocuous defaults.
Eric Christopher404be0c2010-10-17 11:08:44 +0000881 unsigned Base = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000882 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000883
Eric Christopher318b6ee2010-09-02 00:53:56 +0000884 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000885 if (!ARMComputeRegOffset(I->getOperand(1), Base, Offset))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000886 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000887
Eric Christophera3224252010-10-15 21:32:12 +0000888 if (!ARMEmitStore(VT, SrcReg, Base, Offset)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000889
Eric Christophera5b1e682010-09-17 22:28:18 +0000890 return true;
891}
892
893static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
894 switch (Pred) {
895 // Needs two compares...
896 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000897 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000898 default:
Eric Christopher4053e632010-11-02 01:24:49 +0000899 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +0000900 return ARMCC::AL;
901 case CmpInst::ICMP_EQ:
902 case CmpInst::FCMP_OEQ:
903 return ARMCC::EQ;
904 case CmpInst::ICMP_SGT:
905 case CmpInst::FCMP_OGT:
906 return ARMCC::GT;
907 case CmpInst::ICMP_SGE:
908 case CmpInst::FCMP_OGE:
909 return ARMCC::GE;
910 case CmpInst::ICMP_UGT:
911 case CmpInst::FCMP_UGT:
912 return ARMCC::HI;
913 case CmpInst::FCMP_OLT:
914 return ARMCC::MI;
915 case CmpInst::ICMP_ULE:
916 case CmpInst::FCMP_OLE:
917 return ARMCC::LS;
918 case CmpInst::FCMP_ORD:
919 return ARMCC::VC;
920 case CmpInst::FCMP_UNO:
921 return ARMCC::VS;
922 case CmpInst::FCMP_UGE:
923 return ARMCC::PL;
924 case CmpInst::ICMP_SLT:
925 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000926 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000927 case CmpInst::ICMP_SLE:
928 case CmpInst::FCMP_ULE:
929 return ARMCC::LE;
930 case CmpInst::FCMP_UNE:
931 case CmpInst::ICMP_NE:
932 return ARMCC::NE;
933 case CmpInst::ICMP_UGE:
934 return ARMCC::HS;
935 case CmpInst::ICMP_ULT:
936 return ARMCC::LO;
937 }
Eric Christopher543cf052010-09-01 22:16:27 +0000938}
939
Eric Christopher43b62be2010-09-27 06:02:23 +0000940bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000941 const BranchInst *BI = cast<BranchInst>(I);
942 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
943 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000944
Eric Christophere5734102010-09-03 00:35:47 +0000945 // Simple branch support.
Eric Christopher0e6233b2010-10-29 21:08:19 +0000946
947 // If we can, avoid recomputing the compare - redoing it could lead to wonky
948 // behavior.
949 // TODO: Factor this out.
950 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
951 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher76d61472010-10-30 21:25:26 +0000952 EVT VT;
Eric Christopher0e6233b2010-10-29 21:08:19 +0000953 const Type *Ty = CI->getOperand(0)->getType();
Eric Christopher76d61472010-10-30 21:25:26 +0000954 if (!isTypeLegal(Ty, VT))
955 return false;
956
Eric Christopher0e6233b2010-10-29 21:08:19 +0000957 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
958 if (isFloat && !Subtarget->hasVFP2())
959 return false;
960
961 unsigned CmpOpc;
962 unsigned CondReg;
963 switch (VT.getSimpleVT().SimpleTy) {
964 default: return false;
965 // TODO: Verify compares.
966 case MVT::f32:
967 CmpOpc = ARM::VCMPES;
968 CondReg = ARM::FPSCR;
969 break;
970 case MVT::f64:
971 CmpOpc = ARM::VCMPED;
972 CondReg = ARM::FPSCR;
973 break;
974 case MVT::i32:
975 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
976 CondReg = ARM::CPSR;
977 break;
978 }
979
980 // Get the compare predicate.
981 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
982
983 // We may not handle every CC for now.
984 if (ARMPred == ARMCC::AL) return false;
985
986 unsigned Arg1 = getRegForValue(CI->getOperand(0));
987 if (Arg1 == 0) return false;
988
989 unsigned Arg2 = getRegForValue(CI->getOperand(1));
990 if (Arg2 == 0) return false;
991
992 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
993 TII.get(CmpOpc))
994 .addReg(Arg1).addReg(Arg2));
995
996 // For floating point we need to move the result to a comparison register
997 // that we can then use for branches.
998 if (isFloat)
999 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1000 TII.get(ARM::FMSTAT)));
1001
1002 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1003 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1004 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1005 FastEmitBranch(FBB, DL);
1006 FuncInfo.MBB->addSuccessor(TBB);
1007 return true;
1008 }
1009 }
1010
1011 unsigned CmpReg = getRegForValue(BI->getCondition());
1012 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001013
Eric Christopher229207a2010-09-29 01:14:47 +00001014 // Re-set the flags just in case.
1015 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1016 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001017 .addReg(CmpReg).addImm(0));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001018
Eric Christophere5734102010-09-03 00:35:47 +00001019 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001020 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001021 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001022 FastEmitBranch(FBB, DL);
1023 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001024 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001025}
1026
Eric Christopher43b62be2010-09-27 06:02:23 +00001027bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001028 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001029
Eric Christopherd43393a2010-09-08 23:13:45 +00001030 EVT VT;
1031 const Type *Ty = CI->getOperand(0)->getType();
1032 if (!isTypeLegal(Ty, VT))
1033 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001034
Eric Christopherd43393a2010-09-08 23:13:45 +00001035 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1036 if (isFloat && !Subtarget->hasVFP2())
1037 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001038
Eric Christopherd43393a2010-09-08 23:13:45 +00001039 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001040 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +00001041 switch (VT.getSimpleVT().SimpleTy) {
1042 default: return false;
1043 // TODO: Verify compares.
1044 case MVT::f32:
1045 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001046 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001047 break;
1048 case MVT::f64:
1049 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001050 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001051 break;
1052 case MVT::i32:
1053 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001054 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001055 break;
1056 }
1057
Eric Christopher229207a2010-09-29 01:14:47 +00001058 // Get the compare predicate.
1059 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001060
Eric Christopher229207a2010-09-29 01:14:47 +00001061 // We may not handle every CC for now.
1062 if (ARMPred == ARMCC::AL) return false;
1063
Eric Christopherd43393a2010-09-08 23:13:45 +00001064 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1065 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001066
Eric Christopherd43393a2010-09-08 23:13:45 +00001067 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1068 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001069
Eric Christopherd43393a2010-09-08 23:13:45 +00001070 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1071 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001072
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001073 // For floating point we need to move the result to a comparison register
1074 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001075 if (isFloat)
1076 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1077 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001078
Eric Christopher229207a2010-09-29 01:14:47 +00001079 // Now set a register based on the comparison. Explicitly set the predicates
1080 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001081 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001082 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001083 : ARM::GPRRegisterClass;
1084 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001085 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001086 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001087 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1088 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1089 .addReg(ZeroReg).addImm(1)
1090 .addImm(ARMPred).addReg(CondReg);
1091
Eric Christophera5b1e682010-09-17 22:28:18 +00001092 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001093 return true;
1094}
1095
Eric Christopher43b62be2010-09-27 06:02:23 +00001096bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001097 // Make sure we have VFP and that we're extending float to double.
1098 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001099
Eric Christopher46203602010-09-09 00:26:48 +00001100 Value *V = I->getOperand(0);
1101 if (!I->getType()->isDoubleTy() ||
1102 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001103
Eric Christopher46203602010-09-09 00:26:48 +00001104 unsigned Op = getRegForValue(V);
1105 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001106
Eric Christopher46203602010-09-09 00:26:48 +00001107 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001108 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001109 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001110 .addReg(Op));
1111 UpdateValueMap(I, Result);
1112 return true;
1113}
1114
Eric Christopher43b62be2010-09-27 06:02:23 +00001115bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001116 // Make sure we have VFP and that we're truncating double to float.
1117 if (!Subtarget->hasVFP2()) return false;
1118
1119 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001120 if (!(I->getType()->isFloatTy() &&
1121 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001122
1123 unsigned Op = getRegForValue(V);
1124 if (Op == 0) return false;
1125
1126 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001127 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001128 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001129 .addReg(Op));
1130 UpdateValueMap(I, Result);
1131 return true;
1132}
1133
Eric Christopher43b62be2010-09-27 06:02:23 +00001134bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001135 // Make sure we have VFP.
1136 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001137
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001138 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001139 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001140 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001141 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001142
Eric Christopher9a040492010-09-09 18:54:59 +00001143 unsigned Op = getRegForValue(I->getOperand(0));
1144 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001145
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001146 // The conversion routine works on fp-reg to fp-reg and the operand above
1147 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001148 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001149 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001150
Eric Christopher9a040492010-09-09 18:54:59 +00001151 unsigned Opc;
1152 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1153 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1154 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001155
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001156 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001157 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1158 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001159 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001160 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001161 return true;
1162}
1163
Eric Christopher43b62be2010-09-27 06:02:23 +00001164bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001165 // Make sure we have VFP.
1166 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001167
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001168 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001169 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001170 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001171 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001172
Eric Christopher9a040492010-09-09 18:54:59 +00001173 unsigned Op = getRegForValue(I->getOperand(0));
1174 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001175
Eric Christopher9a040492010-09-09 18:54:59 +00001176 unsigned Opc;
1177 const Type *OpTy = I->getOperand(0)->getType();
1178 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1179 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1180 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001181
Eric Christopher022b7fb2010-10-05 23:13:24 +00001182 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1183 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001184 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1185 ResultReg)
1186 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001187
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001188 // This result needs to be in an integer register, but the conversion only
1189 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001190 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001191 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001192
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001193 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001194 return true;
1195}
1196
Eric Christopher3bbd3962010-10-11 08:27:59 +00001197bool ARMFastISel::SelectSelect(const Instruction *I) {
1198 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1199 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1200 return false;
1201
1202 // Things need to be register sized for register moves.
1203 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1204 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1205
1206 unsigned CondReg = getRegForValue(I->getOperand(0));
1207 if (CondReg == 0) return false;
1208 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1209 if (Op1Reg == 0) return false;
1210 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1211 if (Op2Reg == 0) return false;
1212
1213 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1214 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1215 .addReg(CondReg).addImm(1));
1216 unsigned ResultReg = createResultReg(RC);
1217 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1218 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1219 .addReg(Op1Reg).addReg(Op2Reg)
1220 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1221 UpdateValueMap(I, ResultReg);
1222 return true;
1223}
1224
Eric Christopher08637852010-09-30 22:34:19 +00001225bool ARMFastISel::SelectSDiv(const Instruction *I) {
1226 EVT VT;
1227 const Type *Ty = I->getType();
1228 if (!isTypeLegal(Ty, VT))
1229 return false;
1230
1231 // If we have integer div support we should have selected this automagically.
1232 // In case we have a real miss go ahead and return false and we'll pick
1233 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001234 if (Subtarget->hasDivide()) return false;
1235
Eric Christopher08637852010-09-30 22:34:19 +00001236 // Otherwise emit a libcall.
1237 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001238 if (VT == MVT::i8)
1239 LC = RTLIB::SDIV_I8;
1240 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001241 LC = RTLIB::SDIV_I16;
1242 else if (VT == MVT::i32)
1243 LC = RTLIB::SDIV_I32;
1244 else if (VT == MVT::i64)
1245 LC = RTLIB::SDIV_I64;
1246 else if (VT == MVT::i128)
1247 LC = RTLIB::SDIV_I128;
1248 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001249
Eric Christopher08637852010-09-30 22:34:19 +00001250 return ARMEmitLibcall(I, LC);
1251}
1252
Eric Christopher6a880d62010-10-11 08:37:26 +00001253bool ARMFastISel::SelectSRem(const Instruction *I) {
1254 EVT VT;
1255 const Type *Ty = I->getType();
1256 if (!isTypeLegal(Ty, VT))
1257 return false;
1258
1259 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1260 if (VT == MVT::i8)
1261 LC = RTLIB::SREM_I8;
1262 else if (VT == MVT::i16)
1263 LC = RTLIB::SREM_I16;
1264 else if (VT == MVT::i32)
1265 LC = RTLIB::SREM_I32;
1266 else if (VT == MVT::i64)
1267 LC = RTLIB::SREM_I64;
1268 else if (VT == MVT::i128)
1269 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001270 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001271
Eric Christopher6a880d62010-10-11 08:37:26 +00001272 return ARMEmitLibcall(I, LC);
1273}
1274
Eric Christopher43b62be2010-09-27 06:02:23 +00001275bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001276 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001277
Eric Christopherbc39b822010-09-09 00:53:57 +00001278 // We can get here in the case when we want to use NEON for our fp
1279 // operations, but can't figure out how to. Just use the vfp instructions
1280 // if we have them.
1281 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001282 const Type *Ty = I->getType();
1283 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1284 if (isFloat && !Subtarget->hasVFP2())
1285 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001286
Eric Christopherbc39b822010-09-09 00:53:57 +00001287 unsigned Op1 = getRegForValue(I->getOperand(0));
1288 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001289
Eric Christopherbc39b822010-09-09 00:53:57 +00001290 unsigned Op2 = getRegForValue(I->getOperand(1));
1291 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001292
Eric Christopherbc39b822010-09-09 00:53:57 +00001293 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +00001294 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1295 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001296 switch (ISDOpcode) {
1297 default: return false;
1298 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001299 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001300 break;
1301 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001302 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001303 break;
1304 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001305 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001306 break;
1307 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001308 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1310 TII.get(Opc), ResultReg)
1311 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001312 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001313 return true;
1314}
1315
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001316// Call Handling Code
1317
Eric Christopherfa87d662010-10-18 02:17:53 +00001318bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1319 EVT SrcVT, unsigned &ResultReg) {
1320 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1321 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001322
Eric Christopherfa87d662010-10-18 02:17:53 +00001323 if (RR != 0) {
1324 ResultReg = RR;
1325 return true;
1326 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001327 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001328}
1329
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001330// This is largely taken directly from CCAssignFnForNode - we don't support
1331// varargs in FastISel so that part has been removed.
1332// TODO: We may not support all of this.
1333CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1334 switch (CC) {
1335 default:
1336 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001337 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001338 // Ignore fastcc. Silence compiler warnings.
1339 (void)RetFastCC_ARM_APCS;
1340 (void)FastCC_ARM_APCS;
1341 // Fallthrough
1342 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001343 // Use target triple & subtarget features to do actual dispatch.
1344 if (Subtarget->isAAPCS_ABI()) {
1345 if (Subtarget->hasVFP2() &&
1346 FloatABIType == FloatABI::Hard)
1347 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1348 else
1349 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1350 } else
1351 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1352 case CallingConv::ARM_AAPCS_VFP:
1353 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1354 case CallingConv::ARM_AAPCS:
1355 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1356 case CallingConv::ARM_APCS:
1357 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1358 }
1359}
1360
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001361bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1362 SmallVectorImpl<unsigned> &ArgRegs,
1363 SmallVectorImpl<EVT> &ArgVTs,
1364 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1365 SmallVectorImpl<unsigned> &RegArgs,
1366 CallingConv::ID CC,
1367 unsigned &NumBytes) {
1368 SmallVector<CCValAssign, 16> ArgLocs;
1369 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1370 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1371
1372 // Get a count of how many bytes are to be pushed on the stack.
1373 NumBytes = CCInfo.getNextStackOffset();
1374
1375 // Issue CALLSEQ_START
1376 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1378 TII.get(AdjStackDown))
1379 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001380
1381 // Process the args.
1382 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1383 CCValAssign &VA = ArgLocs[i];
1384 unsigned Arg = ArgRegs[VA.getValNo()];
1385 EVT ArgVT = ArgVTs[VA.getValNo()];
1386
Eric Christophera4633f52010-10-23 09:37:17 +00001387 // We don't handle NEON parameters yet.
1388 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() > 64)
1389 return false;
1390
Eric Christopherf9764fa2010-09-30 20:49:44 +00001391 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001392 switch (VA.getLocInfo()) {
1393 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001394 case CCValAssign::SExt: {
1395 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1396 Arg, ArgVT, Arg);
1397 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
1398 Emitted = true;
1399 ArgVT = VA.getLocVT();
1400 break;
1401 }
1402 case CCValAssign::ZExt: {
1403 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1404 Arg, ArgVT, Arg);
1405 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
1406 Emitted = true;
1407 ArgVT = VA.getLocVT();
1408 break;
1409 }
1410 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001411 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1412 Arg, ArgVT, Arg);
1413 if (!Emitted)
1414 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1415 Arg, ArgVT, Arg);
1416 if (!Emitted)
1417 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1418 Arg, ArgVT, Arg);
1419
1420 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
1421 ArgVT = VA.getLocVT();
1422 break;
1423 }
1424 case CCValAssign::BCvt: {
1425 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(),
1426 VA.getLocVT().getSimpleVT(),
1427 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
1428 assert(BC != 0 && "Failed to emit a bitcast!");
1429 Arg = BC;
1430 ArgVT = VA.getLocVT();
1431 break;
1432 }
1433 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001434 }
1435
1436 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001437 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001438 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001439 VA.getLocReg())
1440 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001441 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001442 } else if (VA.needsCustom()) {
1443 // TODO: We need custom lowering for vector (v2f64) args.
1444 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001445
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001446 CCValAssign &NextVA = ArgLocs[++i];
1447
1448 // TODO: Only handle register args for now.
1449 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1450
1451 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1452 TII.get(ARM::VMOVRRD), VA.getLocReg())
1453 .addReg(NextVA.getLocReg(), RegState::Define)
1454 .addReg(Arg));
1455 RegArgs.push_back(VA.getLocReg());
1456 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001457 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001458 assert(VA.isMemLoc());
1459 // Need to store on the stack.
1460 unsigned Base = ARM::SP;
1461 int Offset = VA.getLocMemOffset();
1462
1463 if (!ARMEmitStore(ArgVT, Arg, Base, Offset)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001464 }
1465 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001466 return true;
1467}
1468
1469bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1470 const Instruction *I, CallingConv::ID CC,
1471 unsigned &NumBytes) {
1472 // Issue CALLSEQ_END
1473 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001474 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1475 TII.get(AdjStackUp))
1476 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001477
1478 // Now the return value.
1479 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1480 SmallVector<CCValAssign, 16> RVLocs;
1481 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1482 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1483
1484 // Copy all of the result registers out of their specified physreg.
Eric Christopher14df8822010-10-01 00:00:11 +00001485 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1486 // For this move we copy into two registers and then move into the
1487 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001488 EVT DestVT = RVLocs[0].getValVT();
1489 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1490 unsigned ResultReg = createResultReg(DstRC);
1491 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1492 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001493 .addReg(RVLocs[0].getLocReg())
1494 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001495
Eric Christopher3659ac22010-10-20 08:02:24 +00001496 UsedRegs.push_back(RVLocs[0].getLocReg());
1497 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001498
Eric Christopherdccd2c32010-10-11 08:38:55 +00001499 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001500 UpdateValueMap(I, ResultReg);
1501 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001502 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001503 EVT CopyVT = RVLocs[0].getValVT();
1504 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001505
Eric Christopher14df8822010-10-01 00:00:11 +00001506 unsigned ResultReg = createResultReg(DstRC);
1507 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1508 ResultReg).addReg(RVLocs[0].getLocReg());
1509 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001510
Eric Christopherdccd2c32010-10-11 08:38:55 +00001511 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001512 UpdateValueMap(I, ResultReg);
1513 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001514 }
1515
Eric Christopherdccd2c32010-10-11 08:38:55 +00001516 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001517}
1518
Eric Christopher4f512ef2010-10-22 01:28:00 +00001519bool ARMFastISel::SelectRet(const Instruction *I) {
1520 const ReturnInst *Ret = cast<ReturnInst>(I);
1521 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001522
Eric Christopher4f512ef2010-10-22 01:28:00 +00001523 if (!FuncInfo.CanLowerReturn)
1524 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001525
Eric Christopher4f512ef2010-10-22 01:28:00 +00001526 if (F.isVarArg())
1527 return false;
1528
1529 CallingConv::ID CC = F.getCallingConv();
1530 if (Ret->getNumOperands() > 0) {
1531 SmallVector<ISD::OutputArg, 4> Outs;
1532 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1533 Outs, TLI);
1534
1535 // Analyze operands of the call, assigning locations to each operand.
1536 SmallVector<CCValAssign, 16> ValLocs;
1537 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1538 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1539
1540 const Value *RV = Ret->getOperand(0);
1541 unsigned Reg = getRegForValue(RV);
1542 if (Reg == 0)
1543 return false;
1544
1545 // Only handle a single return value for now.
1546 if (ValLocs.size() != 1)
1547 return false;
1548
1549 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001550
Eric Christopher4f512ef2010-10-22 01:28:00 +00001551 // Don't bother handling odd stuff for now.
1552 if (VA.getLocInfo() != CCValAssign::Full)
1553 return false;
1554 // Only handle register returns for now.
1555 if (!VA.isRegLoc())
1556 return false;
1557 // TODO: For now, don't try to handle cases where getLocInfo()
1558 // says Full but the types don't match.
1559 if (VA.getValVT() != TLI.getValueType(RV->getType()))
1560 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001561
Eric Christopher4f512ef2010-10-22 01:28:00 +00001562 // Make the copy.
1563 unsigned SrcReg = Reg + VA.getValNo();
1564 unsigned DstReg = VA.getLocReg();
1565 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1566 // Avoid a cross-class copy. This is very unlikely.
1567 if (!SrcRC->contains(DstReg))
1568 return false;
1569 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1570 DstReg).addReg(SrcReg);
1571
1572 // Mark the register as live out of the function.
1573 MRI.addLiveOut(VA.getLocReg());
1574 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001575
Eric Christopher4f512ef2010-10-22 01:28:00 +00001576 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1577 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1578 TII.get(RetOpc)));
1579 return true;
1580}
1581
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001582// A quick function that will emit a call for a named libcall in F with the
1583// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001584// can emit a call for any libcall we can produce. This is an abridged version
1585// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001586// like computed function pointers or strange arguments at call sites.
1587// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1588// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001589bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1590 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001591
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001592 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001593 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001594 EVT RetVT;
1595 if (RetTy->isVoidTy())
1596 RetVT = MVT::isVoid;
1597 else if (!isTypeLegal(RetTy, RetVT))
1598 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001599
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001600 // For now we're using BLX etc on the assumption that we have v5t ops.
1601 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001602
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001603 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001604 SmallVector<Value*, 8> Args;
1605 SmallVector<unsigned, 8> ArgRegs;
1606 SmallVector<EVT, 8> ArgVTs;
1607 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1608 Args.reserve(I->getNumOperands());
1609 ArgRegs.reserve(I->getNumOperands());
1610 ArgVTs.reserve(I->getNumOperands());
1611 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001612 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001613 Value *Op = I->getOperand(i);
1614 unsigned Arg = getRegForValue(Op);
1615 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001616
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001617 const Type *ArgTy = Op->getType();
1618 EVT ArgVT;
1619 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001620
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001621 ISD::ArgFlagsTy Flags;
1622 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1623 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001624
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001625 Args.push_back(Op);
1626 ArgRegs.push_back(Arg);
1627 ArgVTs.push_back(ArgVT);
1628 ArgFlags.push_back(Flags);
1629 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001630
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001631 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001632 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001633 unsigned NumBytes;
1634 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1635 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001636
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001637 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001638 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001639 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001640 unsigned CallOpc;
1641 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001642 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001643 else
1644 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001645 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001646 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001647
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001648 // Add implicit physical register uses to the call.
1649 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1650 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001651
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001652 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001653 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001654 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001655
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001656 // Set all unused physreg defs as dead.
1657 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001658
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001659 return true;
1660}
1661
Eric Christopherf9764fa2010-09-30 20:49:44 +00001662bool ARMFastISel::SelectCall(const Instruction *I) {
1663 const CallInst *CI = cast<CallInst>(I);
1664 const Value *Callee = CI->getCalledValue();
1665
1666 // Can't handle inline asm or worry about intrinsics yet.
1667 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1668
Eric Christophere6ca6772010-10-01 21:33:12 +00001669 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001670 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001671 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1672 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001673
Eric Christopherf9764fa2010-09-30 20:49:44 +00001674 // Check the calling convention.
1675 ImmutableCallSite CS(CI);
1676 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001677
Eric Christopherf9764fa2010-09-30 20:49:44 +00001678 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001679
Eric Christopherf9764fa2010-09-30 20:49:44 +00001680 // Let SDISel handle vararg functions.
1681 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1682 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1683 if (FTy->isVarArg())
1684 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001685
Eric Christopherf9764fa2010-09-30 20:49:44 +00001686 // Handle *simple* calls for now.
1687 const Type *RetTy = I->getType();
1688 EVT RetVT;
1689 if (RetTy->isVoidTy())
1690 RetVT = MVT::isVoid;
1691 else if (!isTypeLegal(RetTy, RetVT))
1692 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001693
Eric Christopherf9764fa2010-09-30 20:49:44 +00001694 // For now we're using BLX etc on the assumption that we have v5t ops.
1695 // TODO: Maybe?
1696 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001697
Eric Christopherf9764fa2010-09-30 20:49:44 +00001698 // Set up the argument vectors.
1699 SmallVector<Value*, 8> Args;
1700 SmallVector<unsigned, 8> ArgRegs;
1701 SmallVector<EVT, 8> ArgVTs;
1702 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1703 Args.reserve(CS.arg_size());
1704 ArgRegs.reserve(CS.arg_size());
1705 ArgVTs.reserve(CS.arg_size());
1706 ArgFlags.reserve(CS.arg_size());
1707 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1708 i != e; ++i) {
1709 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001710
Eric Christopherf9764fa2010-09-30 20:49:44 +00001711 if (Arg == 0)
1712 return false;
1713 ISD::ArgFlagsTy Flags;
1714 unsigned AttrInd = i - CS.arg_begin() + 1;
1715 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1716 Flags.setSExt();
1717 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1718 Flags.setZExt();
1719
1720 // FIXME: Only handle *easy* calls for now.
1721 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1722 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1723 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1724 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1725 return false;
1726
1727 const Type *ArgTy = (*i)->getType();
1728 EVT ArgVT;
1729 if (!isTypeLegal(ArgTy, ArgVT))
1730 return false;
1731 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1732 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001733
Eric Christopherf9764fa2010-09-30 20:49:44 +00001734 Args.push_back(*i);
1735 ArgRegs.push_back(Arg);
1736 ArgVTs.push_back(ArgVT);
1737 ArgFlags.push_back(Flags);
1738 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001739
Eric Christopherf9764fa2010-09-30 20:49:44 +00001740 // Handle the arguments now that we've gotten them.
1741 SmallVector<unsigned, 4> RegArgs;
1742 unsigned NumBytes;
1743 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1744 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001745
Eric Christopherf9764fa2010-09-30 20:49:44 +00001746 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001747 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001748 MachineInstrBuilder MIB;
1749 unsigned CallOpc;
1750 if(isThumb)
1751 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1752 else
1753 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1754 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1755 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001756
Eric Christopherf9764fa2010-09-30 20:49:44 +00001757 // Add implicit physical register uses to the call.
1758 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1759 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001760
Eric Christopherf9764fa2010-09-30 20:49:44 +00001761 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001762 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001763 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001764
Eric Christopherf9764fa2010-09-30 20:49:44 +00001765 // Set all unused physreg defs as dead.
1766 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001767
Eric Christopherf9764fa2010-09-30 20:49:44 +00001768 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001769
Eric Christopherf9764fa2010-09-30 20:49:44 +00001770}
1771
Eric Christopher56d2b722010-09-02 23:43:26 +00001772// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001773bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001774
Eric Christopherab695882010-07-21 22:26:11 +00001775 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001776 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001777 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001778 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001779 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001780 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001781 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001782 case Instruction::ICmp:
1783 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001784 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001785 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001786 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001787 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001788 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001789 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001790 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001791 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001792 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001793 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001794 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001795 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001796 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001797 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001798 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001799 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001800 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001801 case Instruction::SRem:
1802 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001803 case Instruction::Call:
1804 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001805 case Instruction::Select:
1806 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001807 case Instruction::Ret:
1808 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001809 default: break;
1810 }
1811 return false;
1812}
1813
1814namespace llvm {
1815 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001816 // Completely untested on non-darwin.
1817 const TargetMachine &TM = funcInfo.MF->getTarget();
Eric Christopheraaa8df42010-11-02 01:21:28 +00001818
1819 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001820 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopheraaa8df42010-11-02 01:21:28 +00001821 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
1822 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001823 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001824 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001825 }
1826}