blob: 229c129ea85b5e0572f93f2892f648f9234b0f55 [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"
Eli Friedmanc573e2c2011-04-29 22:48:03 +000017#include "ARMAddressingModes.h"
Eric Christopher456144e2010-08-19 00:37:05 +000018#include "ARMBaseInstrInfo.h"
Eric Christopherd10cd7b2010-09-10 23:18:12 +000019#include "ARMCallingConv.h"
Eric Christopherab695882010-07-21 22:26:11 +000020#include "ARMRegisterInfo.h"
21#include "ARMTargetMachine.h"
22#include "ARMSubtarget.h"
Eric Christopherc9932f62010-10-01 23:24:42 +000023#include "ARMConstantPoolValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000024#include "llvm/CallingConv.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/Instructions.h"
28#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000029#include "llvm/Module.h"
Jay Foad562b84b2011-04-11 09:35:34 +000030#include "llvm/Operator.h"
Eric Christopherab695882010-07-21 22:26:11 +000031#include "llvm/CodeGen/Analysis.h"
32#include "llvm/CodeGen/FastISel.h"
33#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
35#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000036#include "llvm/CodeGen/MachineConstantPool.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000038#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000040#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000042#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000043#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000045#include "llvm/Target/TargetData.h"
46#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetLowering.h"
48#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000049#include "llvm/Target/TargetOptions.h"
50using namespace llvm;
51
Eric Christopher038fea52010-08-17 00:46:57 +000052static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000053DisableARMFastISel("disable-arm-fast-isel",
54 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000055 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000056
Eric Christopher836c6242010-12-15 23:47:29 +000057extern cl::opt<bool> EnableARMLongCalls;
58
Eric Christopherab695882010-07-21 22:26:11 +000059namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000060
Eric Christopher0d581222010-11-19 22:30:02 +000061 // All possible address modes, plus some.
62 typedef struct Address {
63 enum {
64 RegBase,
65 FrameIndexBase
66 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000067
Eric Christopher0d581222010-11-19 22:30:02 +000068 union {
69 unsigned Reg;
70 int FI;
71 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000072
Eric Christopher0d581222010-11-19 22:30:02 +000073 int Offset;
Eric Christopher827656d2010-11-20 22:38:27 +000074
Eric Christopher0d581222010-11-19 22:30:02 +000075 // Innocuous defaults for our address.
76 Address()
Jim Grosbach0c720762011-05-16 22:24:07 +000077 : BaseType(RegBase), Offset(0) {
Eric Christopher0d581222010-11-19 22:30:02 +000078 Base.Reg = 0;
79 }
80 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000081
82class ARMFastISel : public FastISel {
83
84 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
85 /// make the right decision when generating code for different targets.
86 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000087 const TargetMachine &TM;
88 const TargetInstrInfo &TII;
89 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000090 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000091
Eric Christopher8cf6c602010-09-29 22:24:45 +000092 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000093 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000094 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000095
Eric Christopherab695882010-07-21 22:26:11 +000096 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000097 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000098 : FastISel(funcInfo),
99 TM(funcInfo.MF->getTarget()),
100 TII(*TM.getInstrInfo()),
101 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000102 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000103 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +0000104 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000105 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000106 }
107
Eric Christophercb592292010-08-20 00:20:31 +0000108 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000109 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC);
111 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill);
114 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 unsigned Op1, bool Op1IsKill);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000118 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
121 unsigned Op1, bool Op1IsKill,
122 unsigned Op2, bool Op2IsKill);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000123 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
124 const TargetRegisterClass *RC,
125 unsigned Op0, bool Op0IsKill,
126 uint64_t Imm);
127 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
128 const TargetRegisterClass *RC,
129 unsigned Op0, bool Op0IsKill,
130 const ConstantFP *FPImm);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000131 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
132 const TargetRegisterClass *RC,
133 unsigned Op0, bool Op0IsKill,
134 unsigned Op1, bool Op1IsKill,
135 uint64_t Imm);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000136 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
137 const TargetRegisterClass *RC,
138 uint64_t Imm);
Eric Christopherd94bc542011-04-29 22:07:50 +0000139 virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
140 const TargetRegisterClass *RC,
141 uint64_t Imm1, uint64_t Imm2);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000142
Eric Christopher0fe7d542010-08-17 01:25:29 +0000143 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
144 unsigned Op0, bool Op0IsKill,
145 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000146
Eric Christophercb592292010-08-20 00:20:31 +0000147 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000148 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000149 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000150 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000151
152 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000153
Eric Christopher83007122010-08-23 21:44:12 +0000154 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000155 private:
Eric Christopher17787722010-10-21 21:47:51 +0000156 bool SelectLoad(const Instruction *I);
157 bool SelectStore(const Instruction *I);
158 bool SelectBranch(const Instruction *I);
159 bool SelectCmp(const Instruction *I);
160 bool SelectFPExt(const Instruction *I);
161 bool SelectFPTrunc(const Instruction *I);
162 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
163 bool SelectSIToFP(const Instruction *I);
164 bool SelectFPToSI(const Instruction *I);
165 bool SelectSDiv(const Instruction *I);
166 bool SelectSRem(const Instruction *I);
167 bool SelectCall(const Instruction *I);
168 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000169 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000170
Eric Christopher83007122010-08-23 21:44:12 +0000171 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000172 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000173 bool isTypeLegal(const Type *Ty, MVT &VT);
174 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher0d581222010-11-19 22:30:02 +0000175 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
176 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
177 bool ARMComputeAddress(const Value *Obj, Address &Addr);
178 void ARMSimplifyAddress(Address &Addr, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000179 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000180 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000181 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000182 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000183 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000184 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000185
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000186 // Call handling routines.
187 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000188 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
189 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000190 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000191 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000192 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000193 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000194 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
195 SmallVectorImpl<unsigned> &RegArgs,
196 CallingConv::ID CC,
197 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000198 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000199 const Instruction *I, CallingConv::ID CC,
200 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000201 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000202
203 // OptionalDef handling routines.
204 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000205 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000206 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
207 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000208 void AddLoadStoreOperands(EVT VT, Address &Addr,
209 const MachineInstrBuilder &MIB);
Eric Christopher456144e2010-08-19 00:37:05 +0000210};
Eric Christopherab695882010-07-21 22:26:11 +0000211
212} // end anonymous namespace
213
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000214#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000215
Eric Christopher456144e2010-08-19 00:37:05 +0000216// DefinesOptionalPredicate - This is different from DefinesPredicate in that
217// we don't care about implicit defs here, just places we'll need to add a
218// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
219bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
220 const TargetInstrDesc &TID = MI->getDesc();
221 if (!TID.hasOptionalDef())
222 return false;
223
224 // Look to see if our OptionalDef is defining CPSR or CCR.
225 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
226 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000227 if (!MO.isReg() || !MO.isDef()) continue;
228 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000229 *CPSR = true;
230 }
231 return true;
232}
233
Eric Christopheraf3dce52011-03-12 01:09:29 +0000234bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
235 const TargetInstrDesc &TID = MI->getDesc();
Eric Christopher299bbb22011-04-29 00:03:10 +0000236
Eric Christopheraf3dce52011-03-12 01:09:29 +0000237 // If we're a thumb2 or not NEON function we were handled via isPredicable.
238 if ((TID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
239 AFI->isThumb2Function())
240 return false;
Eric Christopher299bbb22011-04-29 00:03:10 +0000241
Eric Christopheraf3dce52011-03-12 01:09:29 +0000242 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i)
243 if (TID.OpInfo[i].isPredicate())
244 return true;
Eric Christopher299bbb22011-04-29 00:03:10 +0000245
Eric Christopheraf3dce52011-03-12 01:09:29 +0000246 return false;
247}
248
Eric Christopher456144e2010-08-19 00:37:05 +0000249// If the machine is predicable go ahead and add the predicate operands, if
250// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000251// TODO: If we want to support thumb1 then we'll need to deal with optional
252// CPSR defs that need to be added before the remaining operands. See s_cc_out
253// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000254const MachineInstrBuilder &
255ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
256 MachineInstr *MI = &*MIB;
257
Eric Christopheraf3dce52011-03-12 01:09:29 +0000258 // Do we use a predicate? or...
259 // Are we NEON in ARM mode and have a predicate operand? If so, I know
260 // we're not predicable but add it anyways.
261 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000262 AddDefaultPred(MIB);
Eric Christopher299bbb22011-04-29 00:03:10 +0000263
Eric Christopher456144e2010-08-19 00:37:05 +0000264 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
265 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000266 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000267 if (DefinesOptionalPredicate(MI, &CPSR)) {
268 if (CPSR)
269 AddDefaultT1CC(MIB);
270 else
271 AddDefaultCC(MIB);
272 }
273 return MIB;
274}
275
Eric Christopher0fe7d542010-08-17 01:25:29 +0000276unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
277 const TargetRegisterClass* RC) {
278 unsigned ResultReg = createResultReg(RC);
279 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
280
Eric Christopher456144e2010-08-19 00:37:05 +0000281 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000282 return ResultReg;
283}
284
285unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
286 const TargetRegisterClass *RC,
287 unsigned Op0, bool Op0IsKill) {
288 unsigned ResultReg = createResultReg(RC);
289 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
290
291 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293 .addReg(Op0, Op0IsKill * RegState::Kill));
294 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000295 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000296 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000298 TII.get(TargetOpcode::COPY), ResultReg)
299 .addReg(II.ImplicitDefs[0]));
300 }
301 return ResultReg;
302}
303
304unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
305 const TargetRegisterClass *RC,
306 unsigned Op0, bool Op0IsKill,
307 unsigned Op1, bool Op1IsKill) {
308 unsigned ResultReg = createResultReg(RC);
309 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
310
311 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000312 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000313 .addReg(Op0, Op0IsKill * RegState::Kill)
314 .addReg(Op1, Op1IsKill * RegState::Kill));
315 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000316 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000317 .addReg(Op0, Op0IsKill * RegState::Kill)
318 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000319 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000320 TII.get(TargetOpcode::COPY), ResultReg)
321 .addReg(II.ImplicitDefs[0]));
322 }
323 return ResultReg;
324}
325
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000326unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
327 const TargetRegisterClass *RC,
328 unsigned Op0, bool Op0IsKill,
329 unsigned Op1, bool Op1IsKill,
330 unsigned Op2, bool Op2IsKill) {
331 unsigned ResultReg = createResultReg(RC);
332 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
333
334 if (II.getNumDefs() >= 1)
335 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
336 .addReg(Op0, Op0IsKill * RegState::Kill)
337 .addReg(Op1, Op1IsKill * RegState::Kill)
338 .addReg(Op2, Op2IsKill * RegState::Kill));
339 else {
340 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
341 .addReg(Op0, Op0IsKill * RegState::Kill)
342 .addReg(Op1, Op1IsKill * RegState::Kill)
343 .addReg(Op2, Op2IsKill * RegState::Kill));
344 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
345 TII.get(TargetOpcode::COPY), ResultReg)
346 .addReg(II.ImplicitDefs[0]));
347 }
348 return ResultReg;
349}
350
Eric Christopher0fe7d542010-08-17 01:25:29 +0000351unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
352 const TargetRegisterClass *RC,
353 unsigned Op0, bool Op0IsKill,
354 uint64_t Imm) {
355 unsigned ResultReg = createResultReg(RC);
356 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
357
358 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000360 .addReg(Op0, Op0IsKill * RegState::Kill)
361 .addImm(Imm));
362 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000363 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000364 .addReg(Op0, Op0IsKill * RegState::Kill)
365 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000366 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000367 TII.get(TargetOpcode::COPY), ResultReg)
368 .addReg(II.ImplicitDefs[0]));
369 }
370 return ResultReg;
371}
372
373unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
374 const TargetRegisterClass *RC,
375 unsigned Op0, bool Op0IsKill,
376 const ConstantFP *FPImm) {
377 unsigned ResultReg = createResultReg(RC);
378 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
379
380 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000381 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000382 .addReg(Op0, Op0IsKill * RegState::Kill)
383 .addFPImm(FPImm));
384 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000385 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000386 .addReg(Op0, Op0IsKill * RegState::Kill)
387 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000388 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000389 TII.get(TargetOpcode::COPY), ResultReg)
390 .addReg(II.ImplicitDefs[0]));
391 }
392 return ResultReg;
393}
394
395unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
396 const TargetRegisterClass *RC,
397 unsigned Op0, bool Op0IsKill,
398 unsigned Op1, bool Op1IsKill,
399 uint64_t Imm) {
400 unsigned ResultReg = createResultReg(RC);
401 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
402
403 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000404 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000405 .addReg(Op0, Op0IsKill * RegState::Kill)
406 .addReg(Op1, Op1IsKill * RegState::Kill)
407 .addImm(Imm));
408 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000409 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000410 .addReg(Op0, Op0IsKill * RegState::Kill)
411 .addReg(Op1, Op1IsKill * RegState::Kill)
412 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000413 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000414 TII.get(TargetOpcode::COPY), ResultReg)
415 .addReg(II.ImplicitDefs[0]));
416 }
417 return ResultReg;
418}
419
420unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
421 const TargetRegisterClass *RC,
422 uint64_t Imm) {
423 unsigned ResultReg = createResultReg(RC);
424 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000425
Eric Christopher0fe7d542010-08-17 01:25:29 +0000426 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000427 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000428 .addImm(Imm));
429 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000430 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000431 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000432 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000433 TII.get(TargetOpcode::COPY), ResultReg)
434 .addReg(II.ImplicitDefs[0]));
435 }
436 return ResultReg;
437}
438
Eric Christopherd94bc542011-04-29 22:07:50 +0000439unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
440 const TargetRegisterClass *RC,
441 uint64_t Imm1, uint64_t Imm2) {
442 unsigned ResultReg = createResultReg(RC);
443 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
444
445 if (II.getNumDefs() >= 1)
446 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
447 .addImm(Imm1).addImm(Imm2));
448 else {
449 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
450 .addImm(Imm1).addImm(Imm2));
451 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
452 TII.get(TargetOpcode::COPY),
453 ResultReg)
454 .addReg(II.ImplicitDefs[0]));
455 }
456 return ResultReg;
457}
458
Eric Christopher0fe7d542010-08-17 01:25:29 +0000459unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
460 unsigned Op0, bool Op0IsKill,
461 uint32_t Idx) {
462 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
463 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
464 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000465 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000466 DL, TII.get(TargetOpcode::COPY), ResultReg)
467 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
468 return ResultReg;
469}
470
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000471// TODO: Don't worry about 64-bit now, but when this is fixed remove the
472// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000473unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000474 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000475
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000476 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
477 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
478 TII.get(ARM::VMOVRS), MoveReg)
479 .addReg(SrcReg));
480 return MoveReg;
481}
482
483unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000484 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000485
Eric Christopheraa3ace12010-09-09 20:49:25 +0000486 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
487 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000488 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000489 .addReg(SrcReg));
490 return MoveReg;
491}
492
Eric Christopher9ed58df2010-09-09 00:19:41 +0000493// For double width floating point we need to materialize two constants
494// (the high and the low) into integer registers then use a move to get
495// the combined constant into an FP reg.
496unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
497 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000498 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000499
Eric Christopher9ed58df2010-09-09 00:19:41 +0000500 // This checks to see if we can use VFP3 instructions to materialize
501 // a constant, otherwise we have to go through the constant pool.
502 if (TLI.isFPImmLegal(Val, VT)) {
503 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
504 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
505 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
506 DestReg)
507 .addFPImm(CFP));
508 return DestReg;
509 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000510
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000511 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000512 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000513
Eric Christopher238bb162010-09-09 23:50:00 +0000514 // MachineConstantPool wants an explicit alignment.
515 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
516 if (Align == 0) {
517 // TODO: Figure out if this is correct.
518 Align = TD.getTypeAllocSize(CFP->getType());
519 }
520 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
521 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
522 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000523
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000524 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000525 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
526 DestReg)
527 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000528 .addReg(0));
529 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000530}
531
Eric Christopher744c7c82010-09-28 22:47:54 +0000532unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000533
Eric Christopher744c7c82010-09-28 22:47:54 +0000534 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000535 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000536
Eric Christophere5b13cf2010-11-03 20:21:17 +0000537 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
538
539 // If we can do this in a single instruction without a constant pool entry
540 // do so now.
541 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000542 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000543 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
544 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000545 TII.get(Opc), DestReg)
546 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000547 return DestReg;
548 }
549
Eric Christopher56d2b722010-09-02 23:43:26 +0000550 // MachineConstantPool wants an explicit alignment.
551 unsigned Align = TD.getPrefTypeAlignment(C->getType());
552 if (Align == 0) {
553 // TODO: Figure out if this is correct.
554 Align = TD.getTypeAllocSize(C->getType());
555 }
556 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000557
Eric Christopher56d2b722010-09-02 23:43:26 +0000558 if (isThumb)
559 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000560 TII.get(ARM::t2LDRpci), DestReg)
561 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000562 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000563 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000564 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000565 TII.get(ARM::LDRcp), DestReg)
566 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000567 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000568
Eric Christopher56d2b722010-09-02 23:43:26 +0000569 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000570}
571
Eric Christopherc9932f62010-10-01 23:24:42 +0000572unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000573 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000574 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000575
Eric Christopher890dbbe2010-10-02 00:32:44 +0000576 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000577
Eric Christopher890dbbe2010-10-02 00:32:44 +0000578 // TODO: No external globals for now.
579 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000580
Eric Christopher890dbbe2010-10-02 00:32:44 +0000581 // TODO: Need more magic for ARM PIC.
582 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000583
Eric Christopher890dbbe2010-10-02 00:32:44 +0000584 // MachineConstantPool wants an explicit alignment.
585 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
586 if (Align == 0) {
587 // TODO: Figure out if this is correct.
588 Align = TD.getTypeAllocSize(GV->getType());
589 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000590
Eric Christopher890dbbe2010-10-02 00:32:44 +0000591 // Grab index.
592 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000593 unsigned Id = AFI->createPICLabelUId();
Eric Christopher890dbbe2010-10-02 00:32:44 +0000594 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
595 ARMCP::CPValue, PCAdj);
596 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000597
Eric Christopher890dbbe2010-10-02 00:32:44 +0000598 // Load value.
599 MachineInstrBuilder MIB;
600 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
601 if (isThumb) {
602 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
603 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
604 .addConstantPoolIndex(Idx);
605 if (RelocM == Reloc::PIC_)
606 MIB.addImm(Id);
607 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000608 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000609 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
610 DestReg)
611 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000612 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000613 }
614 AddOptionalDefs(MIB);
615 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000616}
617
Eric Christopher9ed58df2010-09-09 00:19:41 +0000618unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
619 EVT VT = TLI.getValueType(C->getType(), true);
620
621 // Only handle simple types.
622 if (!VT.isSimple()) return 0;
623
624 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
625 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000626 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
627 return ARMMaterializeGV(GV, VT);
628 else if (isa<ConstantInt>(C))
629 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000630
Eric Christopherc9932f62010-10-01 23:24:42 +0000631 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000632}
633
Eric Christopherf9764fa2010-09-30 20:49:44 +0000634unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
635 // Don't handle dynamic allocas.
636 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000637
Duncan Sands1440e8b2010-11-03 11:35:31 +0000638 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000639 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000640
Eric Christopherf9764fa2010-09-30 20:49:44 +0000641 DenseMap<const AllocaInst*, int>::iterator SI =
642 FuncInfo.StaticAllocaMap.find(AI);
643
644 // This will get lowered later into the correct offsets and registers
645 // via rewriteXFrameIndex.
646 if (SI != FuncInfo.StaticAllocaMap.end()) {
647 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
648 unsigned ResultReg = createResultReg(RC);
649 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
650 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
651 TII.get(Opc), ResultReg)
652 .addFrameIndex(SI->second)
653 .addImm(0));
654 return ResultReg;
655 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000656
Eric Christopherf9764fa2010-09-30 20:49:44 +0000657 return 0;
658}
659
Duncan Sands1440e8b2010-11-03 11:35:31 +0000660bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
661 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000662
Eric Christopherb1cc8482010-08-25 07:23:49 +0000663 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000664 if (evt == MVT::Other || !evt.isSimple()) return false;
665 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000666
Eric Christopherdc908042010-08-31 01:28:42 +0000667 // Handle all legal types, i.e. a register that will directly hold this
668 // value.
669 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000670}
671
Duncan Sands1440e8b2010-11-03 11:35:31 +0000672bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000673 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000674
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000675 // If this is a type than can be sign or zero-extended to a basic operation
676 // go ahead and accept it now.
677 if (VT == MVT::i8 || VT == MVT::i16)
678 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000679
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000680 return false;
681}
682
Eric Christopher88de86b2010-11-19 22:36:41 +0000683// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000684bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000685 // Some boilerplate from the X86 FastISel.
686 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000687 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000688 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000689 // Don't walk into other basic blocks unless the object is an alloca from
690 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000691 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
692 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
693 Opcode = I->getOpcode();
694 U = I;
695 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000696 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000697 Opcode = C->getOpcode();
698 U = C;
699 }
700
Eric Christophercb0b04b2010-08-24 00:07:24 +0000701 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000702 if (Ty->getAddressSpace() > 255)
703 // Fast instruction selection doesn't support the special
704 // address spaces.
705 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000706
Eric Christopher83007122010-08-23 21:44:12 +0000707 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000708 default:
Eric Christopher83007122010-08-23 21:44:12 +0000709 break;
Eric Christopher55324332010-10-12 00:43:21 +0000710 case Instruction::BitCast: {
711 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000712 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000713 }
714 case Instruction::IntToPtr: {
715 // Look past no-op inttoptrs.
716 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000717 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000718 break;
719 }
720 case Instruction::PtrToInt: {
721 // Look past no-op ptrtoints.
722 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000723 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000724 break;
725 }
Eric Christophereae84392010-10-14 09:29:41 +0000726 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000727 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000728 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000729
Eric Christophereae84392010-10-14 09:29:41 +0000730 // Iterate through the GEP folding the constants into offsets where
731 // we can.
732 gep_type_iterator GTI = gep_type_begin(U);
733 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
734 i != e; ++i, ++GTI) {
735 const Value *Op = *i;
736 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
737 const StructLayout *SL = TD.getStructLayout(STy);
738 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
739 TmpOffset += SL->getElementOffset(Idx);
740 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000741 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000742 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000743 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
744 // Constant-offset addressing.
745 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000746 break;
747 }
748 if (isa<AddOperator>(Op) &&
749 (!isa<Instruction>(Op) ||
750 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
751 == FuncInfo.MBB) &&
752 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
Eric Christopher299bbb22011-04-29 00:03:10 +0000753 // An add (in the same block) with a constant operand. Fold the
Eric Christopher7244d7c2011-03-22 19:39:17 +0000754 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000755 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000756 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000757 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000758 // Iterate on the other operand.
759 Op = cast<AddOperator>(Op)->getOperand(0);
760 continue;
Eric Christopher299bbb22011-04-29 00:03:10 +0000761 }
Eric Christopher7244d7c2011-03-22 19:39:17 +0000762 // Unsupported
763 goto unsupported_gep;
764 }
Eric Christophereae84392010-10-14 09:29:41 +0000765 }
766 }
Eric Christopher2896df82010-10-15 18:02:07 +0000767
768 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000769 Addr.Offset = TmpOffset;
770 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000771
772 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000773 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000774
Eric Christophereae84392010-10-14 09:29:41 +0000775 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000776 break;
777 }
Eric Christopher83007122010-08-23 21:44:12 +0000778 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000779 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000780 DenseMap<const AllocaInst*, int>::iterator SI =
781 FuncInfo.StaticAllocaMap.find(AI);
782 if (SI != FuncInfo.StaticAllocaMap.end()) {
783 Addr.BaseType = Address::FrameIndexBase;
784 Addr.Base.FI = SI->second;
785 return true;
786 }
787 break;
Eric Christopher83007122010-08-23 21:44:12 +0000788 }
789 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000790
Eric Christophera9c57512010-10-13 21:41:51 +0000791 // Materialize the global variable's address into a reg which can
792 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000793 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000794 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
795 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000796
Eric Christopher0d581222010-11-19 22:30:02 +0000797 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000798 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000799 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000800
Eric Christophercb0b04b2010-08-24 00:07:24 +0000801 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000802 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
803 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000804}
805
Eric Christopher0d581222010-11-19 22:30:02 +0000806void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000807
Eric Christopher212ae932010-10-21 19:40:30 +0000808 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000809
Eric Christopher212ae932010-10-21 19:40:30 +0000810 bool needsLowering = false;
811 switch (VT.getSimpleVT().SimpleTy) {
812 default:
813 assert(false && "Unhandled load/store type!");
814 case MVT::i1:
815 case MVT::i8:
816 case MVT::i16:
817 case MVT::i32:
818 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000819 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000820 break;
821 case MVT::f32:
822 case MVT::f64:
823 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000824 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000825 break;
826 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000827
Eric Christopher827656d2010-11-20 22:38:27 +0000828 // If this is a stack pointer and the offset needs to be simplified then
829 // put the alloca address into a register, set the base type back to
830 // register and continue. This should almost never happen.
831 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
832 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
833 ARM::GPRRegisterClass;
834 unsigned ResultReg = createResultReg(RC);
835 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
836 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
837 TII.get(Opc), ResultReg)
838 .addFrameIndex(Addr.Base.FI)
839 .addImm(0));
840 Addr.Base.Reg = ResultReg;
841 Addr.BaseType = Address::RegBase;
842 }
843
Eric Christopher212ae932010-10-21 19:40:30 +0000844 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000845 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000846 if (needsLowering) {
Eli Friedman9ebf57a2011-04-29 21:22:56 +0000847 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
848 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopher0d581222010-11-19 22:30:02 +0000849 Addr.Offset = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000850 }
Eric Christopher83007122010-08-23 21:44:12 +0000851}
852
Eric Christopher564857f2010-12-01 01:40:24 +0000853void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
854 const MachineInstrBuilder &MIB) {
855 // addrmode5 output depends on the selection dag addressing dividing the
856 // offset by 4 that it then later multiplies. Do this here as well.
857 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
858 VT.getSimpleVT().SimpleTy == MVT::f64)
859 Addr.Offset /= 4;
Eric Christopher299bbb22011-04-29 00:03:10 +0000860
Eric Christopher564857f2010-12-01 01:40:24 +0000861 // Frame base works a bit differently. Handle it separately.
862 if (Addr.BaseType == Address::FrameIndexBase) {
863 int FI = Addr.Base.FI;
864 int Offset = Addr.Offset;
865 MachineMemOperand *MMO =
866 FuncInfo.MF->getMachineMemOperand(
867 MachinePointerInfo::getFixedStack(FI, Offset),
868 MachineMemOperand::MOLoad,
869 MFI.getObjectSize(FI),
870 MFI.getObjectAlignment(FI));
871 // Now add the rest of the operands.
872 MIB.addFrameIndex(FI);
873
874 // ARM halfword load/stores need an additional operand.
875 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
876
877 MIB.addImm(Addr.Offset);
878 MIB.addMemOperand(MMO);
879 } else {
880 // Now add the rest of the operands.
881 MIB.addReg(Addr.Base.Reg);
Eric Christopher299bbb22011-04-29 00:03:10 +0000882
Eric Christopher564857f2010-12-01 01:40:24 +0000883 // ARM halfword load/stores need an additional operand.
884 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
885
886 MIB.addImm(Addr.Offset);
887 }
888 AddOptionalDefs(MIB);
889}
890
Eric Christopher0d581222010-11-19 22:30:02 +0000891bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000892
Eric Christopherb1cc8482010-08-25 07:23:49 +0000893 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000894 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000895 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000896 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000897 // This is mostly going to be Neon/vector support.
898 default: return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000899 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000900 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000901 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000902 break;
903 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000904 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000905 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000906 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000907 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000908 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000909 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000910 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000911 case MVT::f32:
912 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000913 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000914 break;
915 case MVT::f64:
916 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000917 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000918 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000919 }
Eric Christopher564857f2010-12-01 01:40:24 +0000920 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000921 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000922
Eric Christopher564857f2010-12-01 01:40:24 +0000923 // Create the base instruction, then add the operands.
924 ResultReg = createResultReg(RC);
925 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
926 TII.get(Opc), ResultReg);
927 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopherdc908042010-08-31 01:28:42 +0000928 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000929}
930
Eric Christopher43b62be2010-09-27 06:02:23 +0000931bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000932 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000933 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000934 if (!isLoadTypeLegal(I->getType(), VT))
935 return false;
936
Eric Christopher564857f2010-12-01 01:40:24 +0000937 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000938 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +0000939 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000940
941 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000942 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000943 UpdateValueMap(I, ResultReg);
944 return true;
945}
946
Eric Christopher0d581222010-11-19 22:30:02 +0000947bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000948 unsigned StrOpc;
949 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000950 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000951 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000952 case MVT::i1: {
953 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
954 ARM::GPRRegisterClass);
955 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
956 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
957 TII.get(Opc), Res)
958 .addReg(SrcReg).addImm(1));
959 SrcReg = Res;
960 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000961 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000962 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000963 break;
964 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000965 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000966 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000967 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000968 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000969 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000970 case MVT::f32:
971 if (!Subtarget->hasVFP2()) return false;
972 StrOpc = ARM::VSTRS;
973 break;
974 case MVT::f64:
975 if (!Subtarget->hasVFP2()) return false;
976 StrOpc = ARM::VSTRD;
977 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000978 }
Eric Christopher564857f2010-12-01 01:40:24 +0000979 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000980 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000981
Eric Christopher564857f2010-12-01 01:40:24 +0000982 // Create the base instruction, then add the operands.
983 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
984 TII.get(StrOpc))
985 .addReg(SrcReg, getKillRegState(true));
986 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000987 return true;
988}
989
Eric Christopher43b62be2010-09-27 06:02:23 +0000990bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000991 Value *Op0 = I->getOperand(0);
992 unsigned SrcReg = 0;
993
Eric Christopher564857f2010-12-01 01:40:24 +0000994 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000995 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000996 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000997 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000998
Eric Christopher1b61ef42010-09-02 01:48:11 +0000999 // Get the value to be stored into a register.
1000 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +00001001 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001002
Eric Christopher564857f2010-12-01 01:40:24 +00001003 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001004 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +00001005 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +00001006 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001007
Eric Christopher0d581222010-11-19 22:30:02 +00001008 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +00001009 return true;
1010}
1011
1012static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1013 switch (Pred) {
1014 // Needs two compares...
1015 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001016 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +00001017 default:
Eric Christopher4053e632010-11-02 01:24:49 +00001018 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +00001019 return ARMCC::AL;
1020 case CmpInst::ICMP_EQ:
1021 case CmpInst::FCMP_OEQ:
1022 return ARMCC::EQ;
1023 case CmpInst::ICMP_SGT:
1024 case CmpInst::FCMP_OGT:
1025 return ARMCC::GT;
1026 case CmpInst::ICMP_SGE:
1027 case CmpInst::FCMP_OGE:
1028 return ARMCC::GE;
1029 case CmpInst::ICMP_UGT:
1030 case CmpInst::FCMP_UGT:
1031 return ARMCC::HI;
1032 case CmpInst::FCMP_OLT:
1033 return ARMCC::MI;
1034 case CmpInst::ICMP_ULE:
1035 case CmpInst::FCMP_OLE:
1036 return ARMCC::LS;
1037 case CmpInst::FCMP_ORD:
1038 return ARMCC::VC;
1039 case CmpInst::FCMP_UNO:
1040 return ARMCC::VS;
1041 case CmpInst::FCMP_UGE:
1042 return ARMCC::PL;
1043 case CmpInst::ICMP_SLT:
1044 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001045 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001046 case CmpInst::ICMP_SLE:
1047 case CmpInst::FCMP_ULE:
1048 return ARMCC::LE;
1049 case CmpInst::FCMP_UNE:
1050 case CmpInst::ICMP_NE:
1051 return ARMCC::NE;
1052 case CmpInst::ICMP_UGE:
1053 return ARMCC::HS;
1054 case CmpInst::ICMP_ULT:
1055 return ARMCC::LO;
1056 }
Eric Christopher543cf052010-09-01 22:16:27 +00001057}
1058
Eric Christopher43b62be2010-09-27 06:02:23 +00001059bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001060 const BranchInst *BI = cast<BranchInst>(I);
1061 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1062 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001063
Eric Christophere5734102010-09-03 00:35:47 +00001064 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001065
Eric Christopher0e6233b2010-10-29 21:08:19 +00001066 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1067 // behavior.
1068 // TODO: Factor this out.
1069 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Eric Christopher632ae892011-04-29 21:56:31 +00001070 MVT SourceVT;
1071 const Type *Ty = CI->getOperand(0)->getType();
1072 if (CI->hasOneUse() && (CI->getParent() == I->getParent())
1073 && isTypeLegal(Ty, SourceVT)) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001074 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1075 if (isFloat && !Subtarget->hasVFP2())
1076 return false;
1077
1078 unsigned CmpOpc;
Eric Christopher632ae892011-04-29 21:56:31 +00001079 switch (SourceVT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001080 default: return false;
1081 // TODO: Verify compares.
1082 case MVT::f32:
1083 CmpOpc = ARM::VCMPES;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001084 break;
1085 case MVT::f64:
1086 CmpOpc = ARM::VCMPED;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001087 break;
1088 case MVT::i32:
1089 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001090 break;
1091 }
1092
1093 // Get the compare predicate.
Eric Christopher632ae892011-04-29 21:56:31 +00001094 // Try to take advantage of fallthrough opportunities.
1095 CmpInst::Predicate Predicate = CI->getPredicate();
1096 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1097 std::swap(TBB, FBB);
1098 Predicate = CmpInst::getInversePredicate(Predicate);
1099 }
1100
1101 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher0e6233b2010-10-29 21:08:19 +00001102
1103 // We may not handle every CC for now.
1104 if (ARMPred == ARMCC::AL) return false;
1105
1106 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1107 if (Arg1 == 0) return false;
1108
1109 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1110 if (Arg2 == 0) return false;
1111
1112 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1113 TII.get(CmpOpc))
1114 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001115
Eric Christopher0e6233b2010-10-29 21:08:19 +00001116 // For floating point we need to move the result to a comparison register
1117 // that we can then use for branches.
1118 if (isFloat)
1119 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1120 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001121
Eric Christopher0e6233b2010-10-29 21:08:19 +00001122 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1123 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1124 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1125 FastEmitBranch(FBB, DL);
1126 FuncInfo.MBB->addSuccessor(TBB);
1127 return true;
1128 }
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001129 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1130 MVT SourceVT;
1131 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1132 (isTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1133 unsigned TstOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1134 unsigned OpReg = getRegForValue(TI->getOperand(0));
1135 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1136 TII.get(TstOpc))
1137 .addReg(OpReg).addImm(1));
1138
1139 unsigned CCMode = ARMCC::NE;
1140 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1141 std::swap(TBB, FBB);
1142 CCMode = ARMCC::EQ;
1143 }
1144
1145 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1146 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1147 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1148
1149 FastEmitBranch(FBB, DL);
1150 FuncInfo.MBB->addSuccessor(TBB);
1151 return true;
1152 }
Eric Christopher0e6233b2010-10-29 21:08:19 +00001153 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001154
Eric Christopher0e6233b2010-10-29 21:08:19 +00001155 unsigned CmpReg = getRegForValue(BI->getCondition());
1156 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001157
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001158 // We've been divorced from our compare! Our block was split, and
1159 // now our compare lives in a predecessor block. We musn't
1160 // re-compare here, as the children of the compare aren't guaranteed
1161 // live across the block boundary (we *could* check for this).
1162 // Regardless, the compare has been done in the predecessor block,
1163 // and it left a value for us in a virtual register. Ergo, we test
1164 // the one-bit value left in the virtual register.
1165 unsigned TstOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1166 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1167 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001168
Eric Christopher7a20a372011-04-28 16:52:09 +00001169 unsigned CCMode = ARMCC::NE;
1170 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1171 std::swap(TBB, FBB);
1172 CCMode = ARMCC::EQ;
1173 }
1174
Eric Christophere5734102010-09-03 00:35:47 +00001175 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001176 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher7a20a372011-04-28 16:52:09 +00001177 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001178 FastEmitBranch(FBB, DL);
1179 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001180 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001181}
1182
Eric Christopher43b62be2010-09-27 06:02:23 +00001183bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001184 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001185
Duncan Sands1440e8b2010-11-03 11:35:31 +00001186 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001187 const Type *Ty = CI->getOperand(0)->getType();
1188 if (!isTypeLegal(Ty, VT))
1189 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001190
Eric Christopherd43393a2010-09-08 23:13:45 +00001191 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1192 if (isFloat && !Subtarget->hasVFP2())
1193 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001194
Eric Christopherd43393a2010-09-08 23:13:45 +00001195 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001196 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001197 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001198 default: return false;
1199 // TODO: Verify compares.
1200 case MVT::f32:
1201 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001202 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001203 break;
1204 case MVT::f64:
1205 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001206 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001207 break;
1208 case MVT::i32:
1209 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001210 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001211 break;
1212 }
1213
Eric Christopher229207a2010-09-29 01:14:47 +00001214 // Get the compare predicate.
1215 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001216
Eric Christopher229207a2010-09-29 01:14:47 +00001217 // We may not handle every CC for now.
1218 if (ARMPred == ARMCC::AL) return false;
1219
Eric Christopherd43393a2010-09-08 23:13:45 +00001220 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1221 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001222
Eric Christopherd43393a2010-09-08 23:13:45 +00001223 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1224 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001225
Eric Christopherd43393a2010-09-08 23:13:45 +00001226 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1227 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001228
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001229 // For floating point we need to move the result to a comparison register
1230 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001231 if (isFloat)
1232 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1233 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001234
Eric Christopher229207a2010-09-29 01:14:47 +00001235 // Now set a register based on the comparison. Explicitly set the predicates
1236 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001237 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001238 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001239 : ARM::GPRRegisterClass;
1240 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001241 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001242 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001243 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1245 .addReg(ZeroReg).addImm(1)
1246 .addImm(ARMPred).addReg(CondReg);
1247
Eric Christophera5b1e682010-09-17 22:28:18 +00001248 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001249 return true;
1250}
1251
Eric Christopher43b62be2010-09-27 06:02:23 +00001252bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001253 // Make sure we have VFP and that we're extending float to double.
1254 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001255
Eric Christopher46203602010-09-09 00:26:48 +00001256 Value *V = I->getOperand(0);
1257 if (!I->getType()->isDoubleTy() ||
1258 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001259
Eric Christopher46203602010-09-09 00:26:48 +00001260 unsigned Op = getRegForValue(V);
1261 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001262
Eric Christopher46203602010-09-09 00:26:48 +00001263 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001264 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001265 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001266 .addReg(Op));
1267 UpdateValueMap(I, Result);
1268 return true;
1269}
1270
Eric Christopher43b62be2010-09-27 06:02:23 +00001271bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001272 // Make sure we have VFP and that we're truncating double to float.
1273 if (!Subtarget->hasVFP2()) return false;
1274
1275 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001276 if (!(I->getType()->isFloatTy() &&
1277 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001278
1279 unsigned Op = getRegForValue(V);
1280 if (Op == 0) return false;
1281
1282 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001284 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001285 .addReg(Op));
1286 UpdateValueMap(I, Result);
1287 return true;
1288}
1289
Eric Christopher43b62be2010-09-27 06:02:23 +00001290bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001291 // Make sure we have VFP.
1292 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001293
Duncan Sands1440e8b2010-11-03 11:35:31 +00001294 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001295 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001296 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001297 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001298
Eric Christopher9a040492010-09-09 18:54:59 +00001299 unsigned Op = getRegForValue(I->getOperand(0));
1300 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001301
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001302 // The conversion routine works on fp-reg to fp-reg and the operand above
1303 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001304 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001305 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001306
Eric Christopher9a040492010-09-09 18:54:59 +00001307 unsigned Opc;
1308 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1309 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1310 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001311
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001312 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001313 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1314 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001315 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001316 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001317 return true;
1318}
1319
Eric Christopher43b62be2010-09-27 06:02:23 +00001320bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001321 // Make sure we have VFP.
1322 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001323
Duncan Sands1440e8b2010-11-03 11:35:31 +00001324 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001325 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001326 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001327 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001328
Eric Christopher9a040492010-09-09 18:54:59 +00001329 unsigned Op = getRegForValue(I->getOperand(0));
1330 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001331
Eric Christopher9a040492010-09-09 18:54:59 +00001332 unsigned Opc;
1333 const Type *OpTy = I->getOperand(0)->getType();
1334 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1335 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1336 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001337
Eric Christopher022b7fb2010-10-05 23:13:24 +00001338 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1339 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001340 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1341 ResultReg)
1342 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001343
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001344 // This result needs to be in an integer register, but the conversion only
1345 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001346 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001347 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001348
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001349 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001350 return true;
1351}
1352
Eric Christopher3bbd3962010-10-11 08:27:59 +00001353bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001354 MVT VT;
1355 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001356 return false;
1357
1358 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001359 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001360 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1361
1362 unsigned CondReg = getRegForValue(I->getOperand(0));
1363 if (CondReg == 0) return false;
1364 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1365 if (Op1Reg == 0) return false;
1366 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1367 if (Op2Reg == 0) return false;
1368
1369 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1370 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1371 .addReg(CondReg).addImm(1));
1372 unsigned ResultReg = createResultReg(RC);
1373 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1374 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1375 .addReg(Op1Reg).addReg(Op2Reg)
1376 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1377 UpdateValueMap(I, ResultReg);
1378 return true;
1379}
1380
Eric Christopher08637852010-09-30 22:34:19 +00001381bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001382 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001383 const Type *Ty = I->getType();
1384 if (!isTypeLegal(Ty, VT))
1385 return false;
1386
1387 // If we have integer div support we should have selected this automagically.
1388 // In case we have a real miss go ahead and return false and we'll pick
1389 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001390 if (Subtarget->hasDivide()) return false;
1391
Eric Christopher08637852010-09-30 22:34:19 +00001392 // Otherwise emit a libcall.
1393 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001394 if (VT == MVT::i8)
1395 LC = RTLIB::SDIV_I8;
1396 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001397 LC = RTLIB::SDIV_I16;
1398 else if (VT == MVT::i32)
1399 LC = RTLIB::SDIV_I32;
1400 else if (VT == MVT::i64)
1401 LC = RTLIB::SDIV_I64;
1402 else if (VT == MVT::i128)
1403 LC = RTLIB::SDIV_I128;
1404 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001405
Eric Christopher08637852010-09-30 22:34:19 +00001406 return ARMEmitLibcall(I, LC);
1407}
1408
Eric Christopher6a880d62010-10-11 08:37:26 +00001409bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001410 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001411 const Type *Ty = I->getType();
1412 if (!isTypeLegal(Ty, VT))
1413 return false;
1414
1415 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1416 if (VT == MVT::i8)
1417 LC = RTLIB::SREM_I8;
1418 else if (VT == MVT::i16)
1419 LC = RTLIB::SREM_I16;
1420 else if (VT == MVT::i32)
1421 LC = RTLIB::SREM_I32;
1422 else if (VT == MVT::i64)
1423 LC = RTLIB::SREM_I64;
1424 else if (VT == MVT::i128)
1425 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001426 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001427
Eric Christopher6a880d62010-10-11 08:37:26 +00001428 return ARMEmitLibcall(I, LC);
1429}
1430
Eric Christopher43b62be2010-09-27 06:02:23 +00001431bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001432 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001433
Eric Christopherbc39b822010-09-09 00:53:57 +00001434 // We can get here in the case when we want to use NEON for our fp
1435 // operations, but can't figure out how to. Just use the vfp instructions
1436 // if we have them.
1437 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001438 const Type *Ty = I->getType();
1439 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1440 if (isFloat && !Subtarget->hasVFP2())
1441 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001442
Eric Christopherbc39b822010-09-09 00:53:57 +00001443 unsigned Op1 = getRegForValue(I->getOperand(0));
1444 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001445
Eric Christopherbc39b822010-09-09 00:53:57 +00001446 unsigned Op2 = getRegForValue(I->getOperand(1));
1447 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001448
Eric Christopherbc39b822010-09-09 00:53:57 +00001449 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001450 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001451 switch (ISDOpcode) {
1452 default: return false;
1453 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001454 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001455 break;
1456 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001457 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001458 break;
1459 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001460 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001461 break;
1462 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001463 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001464 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1465 TII.get(Opc), ResultReg)
1466 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001467 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001468 return true;
1469}
1470
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001471// Call Handling Code
1472
Eric Christopherfa87d662010-10-18 02:17:53 +00001473bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1474 EVT SrcVT, unsigned &ResultReg) {
1475 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1476 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001477
Eric Christopherfa87d662010-10-18 02:17:53 +00001478 if (RR != 0) {
1479 ResultReg = RR;
1480 return true;
1481 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001482 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001483}
1484
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001485// This is largely taken directly from CCAssignFnForNode - we don't support
1486// varargs in FastISel so that part has been removed.
1487// TODO: We may not support all of this.
1488CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1489 switch (CC) {
1490 default:
1491 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001492 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001493 // Ignore fastcc. Silence compiler warnings.
1494 (void)RetFastCC_ARM_APCS;
1495 (void)FastCC_ARM_APCS;
1496 // Fallthrough
1497 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001498 // Use target triple & subtarget features to do actual dispatch.
1499 if (Subtarget->isAAPCS_ABI()) {
1500 if (Subtarget->hasVFP2() &&
1501 FloatABIType == FloatABI::Hard)
1502 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1503 else
1504 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1505 } else
1506 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1507 case CallingConv::ARM_AAPCS_VFP:
1508 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1509 case CallingConv::ARM_AAPCS:
1510 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1511 case CallingConv::ARM_APCS:
1512 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1513 }
1514}
1515
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001516bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1517 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001518 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001519 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1520 SmallVectorImpl<unsigned> &RegArgs,
1521 CallingConv::ID CC,
1522 unsigned &NumBytes) {
1523 SmallVector<CCValAssign, 16> ArgLocs;
1524 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1525 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1526
1527 // Get a count of how many bytes are to be pushed on the stack.
1528 NumBytes = CCInfo.getNextStackOffset();
1529
1530 // Issue CALLSEQ_START
1531 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001532 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1533 TII.get(AdjStackDown))
1534 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001535
1536 // Process the args.
1537 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1538 CCValAssign &VA = ArgLocs[i];
1539 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001540 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001541
Eric Christopher4a2b3162011-01-27 05:44:56 +00001542 // We don't handle NEON/vector parameters yet.
1543 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001544 return false;
1545
Eric Christopherf9764fa2010-09-30 20:49:44 +00001546 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001547 switch (VA.getLocInfo()) {
1548 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001549 case CCValAssign::SExt: {
1550 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1551 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001552 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001553 Emitted = true;
1554 ArgVT = VA.getLocVT();
1555 break;
1556 }
1557 case CCValAssign::ZExt: {
1558 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1559 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001560 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001561 Emitted = true;
1562 ArgVT = VA.getLocVT();
1563 break;
1564 }
1565 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001566 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1567 Arg, ArgVT, Arg);
1568 if (!Emitted)
1569 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1570 Arg, ArgVT, Arg);
1571 if (!Emitted)
1572 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1573 Arg, ArgVT, Arg);
1574
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001575 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001576 ArgVT = VA.getLocVT();
1577 break;
1578 }
1579 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001580 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001581 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001582 assert(BC != 0 && "Failed to emit a bitcast!");
1583 Arg = BC;
1584 ArgVT = VA.getLocVT();
1585 break;
1586 }
1587 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001588 }
1589
1590 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001591 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001592 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001593 VA.getLocReg())
1594 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001595 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001596 } else if (VA.needsCustom()) {
1597 // TODO: We need custom lowering for vector (v2f64) args.
1598 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001599
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001600 CCValAssign &NextVA = ArgLocs[++i];
1601
1602 // TODO: Only handle register args for now.
1603 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1604
1605 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1606 TII.get(ARM::VMOVRRD), VA.getLocReg())
1607 .addReg(NextVA.getLocReg(), RegState::Define)
1608 .addReg(Arg));
1609 RegArgs.push_back(VA.getLocReg());
1610 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001611 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001612 assert(VA.isMemLoc());
1613 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001614 Address Addr;
1615 Addr.BaseType = Address::RegBase;
1616 Addr.Base.Reg = ARM::SP;
1617 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001618
Eric Christopher0d581222010-11-19 22:30:02 +00001619 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001620 }
1621 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001622 return true;
1623}
1624
Duncan Sands1440e8b2010-11-03 11:35:31 +00001625bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001626 const Instruction *I, CallingConv::ID CC,
1627 unsigned &NumBytes) {
1628 // Issue CALLSEQ_END
1629 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001630 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1631 TII.get(AdjStackUp))
1632 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001633
1634 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001635 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001636 SmallVector<CCValAssign, 16> RVLocs;
1637 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1638 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1639
1640 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001641 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001642 // For this move we copy into two registers and then move into the
1643 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001644 EVT DestVT = RVLocs[0].getValVT();
1645 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1646 unsigned ResultReg = createResultReg(DstRC);
1647 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1648 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001649 .addReg(RVLocs[0].getLocReg())
1650 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001651
Eric Christopher3659ac22010-10-20 08:02:24 +00001652 UsedRegs.push_back(RVLocs[0].getLocReg());
1653 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001654
Eric Christopherdccd2c32010-10-11 08:38:55 +00001655 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001656 UpdateValueMap(I, ResultReg);
1657 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001658 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001659 EVT CopyVT = RVLocs[0].getValVT();
1660 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001661
Eric Christopher14df8822010-10-01 00:00:11 +00001662 unsigned ResultReg = createResultReg(DstRC);
1663 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1664 ResultReg).addReg(RVLocs[0].getLocReg());
1665 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001666
Eric Christopherdccd2c32010-10-11 08:38:55 +00001667 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001668 UpdateValueMap(I, ResultReg);
1669 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001670 }
1671
Eric Christopherdccd2c32010-10-11 08:38:55 +00001672 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001673}
1674
Eric Christopher4f512ef2010-10-22 01:28:00 +00001675bool ARMFastISel::SelectRet(const Instruction *I) {
1676 const ReturnInst *Ret = cast<ReturnInst>(I);
1677 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001678
Eric Christopher4f512ef2010-10-22 01:28:00 +00001679 if (!FuncInfo.CanLowerReturn)
1680 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001681
Eric Christopher4f512ef2010-10-22 01:28:00 +00001682 if (F.isVarArg())
1683 return false;
1684
1685 CallingConv::ID CC = F.getCallingConv();
1686 if (Ret->getNumOperands() > 0) {
1687 SmallVector<ISD::OutputArg, 4> Outs;
1688 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1689 Outs, TLI);
1690
1691 // Analyze operands of the call, assigning locations to each operand.
1692 SmallVector<CCValAssign, 16> ValLocs;
1693 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1694 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1695
1696 const Value *RV = Ret->getOperand(0);
1697 unsigned Reg = getRegForValue(RV);
1698 if (Reg == 0)
1699 return false;
1700
1701 // Only handle a single return value for now.
1702 if (ValLocs.size() != 1)
1703 return false;
1704
1705 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001706
Eric Christopher4f512ef2010-10-22 01:28:00 +00001707 // Don't bother handling odd stuff for now.
1708 if (VA.getLocInfo() != CCValAssign::Full)
1709 return false;
1710 // Only handle register returns for now.
1711 if (!VA.isRegLoc())
1712 return false;
1713 // TODO: For now, don't try to handle cases where getLocInfo()
1714 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001715 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001716 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001717
Eric Christopher4f512ef2010-10-22 01:28:00 +00001718 // Make the copy.
1719 unsigned SrcReg = Reg + VA.getValNo();
1720 unsigned DstReg = VA.getLocReg();
1721 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1722 // Avoid a cross-class copy. This is very unlikely.
1723 if (!SrcRC->contains(DstReg))
1724 return false;
1725 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1726 DstReg).addReg(SrcReg);
1727
1728 // Mark the register as live out of the function.
1729 MRI.addLiveOut(VA.getLocReg());
1730 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001731
Eric Christopher4f512ef2010-10-22 01:28:00 +00001732 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1733 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1734 TII.get(RetOpc)));
1735 return true;
1736}
1737
Eric Christopher872f4a22011-02-22 01:37:10 +00001738unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1739
Eric Christopher872f4a22011-02-22 01:37:10 +00001740 // Darwin needs the r9 versions of the opcodes.
1741 bool isDarwin = Subtarget->isTargetDarwin();
Eric Christopher04356612011-04-05 00:39:26 +00001742 if (isThumb) {
Eric Christopher872f4a22011-02-22 01:37:10 +00001743 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1744 } else {
1745 return isDarwin ? ARM::BLr9 : ARM::BL;
1746 }
1747}
1748
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001749// A quick function that will emit a call for a named libcall in F with the
1750// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001751// can emit a call for any libcall we can produce. This is an abridged version
1752// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001753// like computed function pointers or strange arguments at call sites.
1754// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1755// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001756bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1757 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001758
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001759 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001760 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001761 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001762 if (RetTy->isVoidTy())
1763 RetVT = MVT::isVoid;
1764 else if (!isTypeLegal(RetTy, RetVT))
1765 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001766
Eric Christopher836c6242010-12-15 23:47:29 +00001767 // TODO: For now if we have long calls specified we don't handle the call.
1768 if (EnableARMLongCalls) return false;
1769
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001770 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001771 SmallVector<Value*, 8> Args;
1772 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001773 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001774 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1775 Args.reserve(I->getNumOperands());
1776 ArgRegs.reserve(I->getNumOperands());
1777 ArgVTs.reserve(I->getNumOperands());
1778 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001779 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001780 Value *Op = I->getOperand(i);
1781 unsigned Arg = getRegForValue(Op);
1782 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001783
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001784 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001785 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001786 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001787
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001788 ISD::ArgFlagsTy Flags;
1789 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1790 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001791
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001792 Args.push_back(Op);
1793 ArgRegs.push_back(Arg);
1794 ArgVTs.push_back(ArgVT);
1795 ArgFlags.push_back(Flags);
1796 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001797
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001798 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001799 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001800 unsigned NumBytes;
1801 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1802 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001803
Eric Christopher6344a5f2011-04-29 00:07:20 +00001804 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001805 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001806 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001807 unsigned CallOpc = ARMSelectCallOp(NULL);
1808 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001809 // Explicitly adding the predicate here.
1810 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1811 TII.get(CallOpc)))
1812 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00001813 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001814 // Explicitly adding the predicate here.
1815 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1816 TII.get(CallOpc))
1817 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001818
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001819 // Add implicit physical register uses to the call.
1820 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1821 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001822
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001823 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001824 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001825 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001826
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001827 // Set all unused physreg defs as dead.
1828 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001829
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001830 return true;
1831}
1832
Eric Christopherf9764fa2010-09-30 20:49:44 +00001833bool ARMFastISel::SelectCall(const Instruction *I) {
1834 const CallInst *CI = cast<CallInst>(I);
1835 const Value *Callee = CI->getCalledValue();
1836
1837 // Can't handle inline asm or worry about intrinsics yet.
1838 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1839
Eric Christopher52f6c032011-05-02 20:16:33 +00001840 // Only handle global variable Callees.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001841 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christopher52f6c032011-05-02 20:16:33 +00001842 if (!GV)
Eric Christophere6ca6772010-10-01 21:33:12 +00001843 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001844
Eric Christopherf9764fa2010-09-30 20:49:44 +00001845 // Check the calling convention.
1846 ImmutableCallSite CS(CI);
1847 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001848
Eric Christopherf9764fa2010-09-30 20:49:44 +00001849 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001850
Eric Christopherf9764fa2010-09-30 20:49:44 +00001851 // Let SDISel handle vararg functions.
1852 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1853 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1854 if (FTy->isVarArg())
1855 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001856
Eric Christopherf9764fa2010-09-30 20:49:44 +00001857 // Handle *simple* calls for now.
1858 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001859 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001860 if (RetTy->isVoidTy())
1861 RetVT = MVT::isVoid;
1862 else if (!isTypeLegal(RetTy, RetVT))
1863 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001864
Eric Christopher836c6242010-12-15 23:47:29 +00001865 // TODO: For now if we have long calls specified we don't handle the call.
1866 if (EnableARMLongCalls) return false;
Eric Christopher299bbb22011-04-29 00:03:10 +00001867
Eric Christopherf9764fa2010-09-30 20:49:44 +00001868 // Set up the argument vectors.
1869 SmallVector<Value*, 8> Args;
1870 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001871 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001872 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1873 Args.reserve(CS.arg_size());
1874 ArgRegs.reserve(CS.arg_size());
1875 ArgVTs.reserve(CS.arg_size());
1876 ArgFlags.reserve(CS.arg_size());
1877 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1878 i != e; ++i) {
1879 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001880
Eric Christopherf9764fa2010-09-30 20:49:44 +00001881 if (Arg == 0)
1882 return false;
1883 ISD::ArgFlagsTy Flags;
1884 unsigned AttrInd = i - CS.arg_begin() + 1;
1885 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1886 Flags.setSExt();
1887 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1888 Flags.setZExt();
1889
1890 // FIXME: Only handle *easy* calls for now.
1891 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1892 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1893 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1894 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1895 return false;
1896
1897 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001898 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001899 if (!isTypeLegal(ArgTy, ArgVT))
1900 return false;
1901 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1902 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001903
Eric Christopherf9764fa2010-09-30 20:49:44 +00001904 Args.push_back(*i);
1905 ArgRegs.push_back(Arg);
1906 ArgVTs.push_back(ArgVT);
1907 ArgFlags.push_back(Flags);
1908 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001909
Eric Christopherf9764fa2010-09-30 20:49:44 +00001910 // Handle the arguments now that we've gotten them.
1911 SmallVector<unsigned, 4> RegArgs;
1912 unsigned NumBytes;
1913 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1914 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001915
Eric Christopher6344a5f2011-04-29 00:07:20 +00001916 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001917 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001918 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001919 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00001920 // Explicitly adding the predicate here.
Eric Christopher872f4a22011-02-22 01:37:10 +00001921 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001922 // Explicitly adding the predicate here.
1923 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1924 TII.get(CallOpc)))
1925 .addGlobalAddress(GV, 0, 0);
Eric Christopher872f4a22011-02-22 01:37:10 +00001926 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001927 // Explicitly adding the predicate here.
1928 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1929 TII.get(CallOpc))
1930 .addGlobalAddress(GV, 0, 0));
Eric Christopher299bbb22011-04-29 00:03:10 +00001931
Eric Christopherf9764fa2010-09-30 20:49:44 +00001932 // Add implicit physical register uses to the call.
1933 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1934 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001935
Eric Christopherf9764fa2010-09-30 20:49:44 +00001936 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001937 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001938 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001939
Eric Christopherf9764fa2010-09-30 20:49:44 +00001940 // Set all unused physreg defs as dead.
1941 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001942
Eric Christopherf9764fa2010-09-30 20:49:44 +00001943 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001944
Eric Christopherf9764fa2010-09-30 20:49:44 +00001945}
1946
Eric Christopher56d2b722010-09-02 23:43:26 +00001947// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001948bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001949
Eric Christopherab695882010-07-21 22:26:11 +00001950 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001951 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001952 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001953 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001954 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001955 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001956 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001957 case Instruction::ICmp:
1958 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001959 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001960 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001961 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001962 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001963 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001964 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001965 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001966 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001967 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001968 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001969 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001970 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001971 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001972 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001973 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001974 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001975 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001976 case Instruction::SRem:
1977 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001978 case Instruction::Call:
1979 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001980 case Instruction::Select:
1981 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001982 case Instruction::Ret:
1983 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001984 default: break;
1985 }
1986 return false;
1987}
1988
1989namespace llvm {
1990 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001991 // Completely untested on non-darwin.
1992 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001993
Eric Christopheraaa8df42010-11-02 01:21:28 +00001994 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001995 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001996 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001997 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001998 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001999 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00002000 }
2001}