blob: 97d813da21348d164d2d0893d400090560f15e90 [file] [log] [blame]
Eric Christopherab695882010-07-21 22:26:11 +00001//===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ARM-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// ARMGenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ARM.h"
Eric Christopher456144e2010-08-19 00:37:05 +000017#include "ARMBaseInstrInfo.h"
Eric Christopherd10cd7b2010-09-10 23:18:12 +000018#include "ARMCallingConv.h"
Eric Christopherab695882010-07-21 22:26:11 +000019#include "ARMRegisterInfo.h"
20#include "ARMTargetMachine.h"
21#include "ARMSubtarget.h"
Eric Christopherc9932f62010-10-01 23:24:42 +000022#include "ARMConstantPoolValue.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.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);
Chad Rosier0d7b2312011-11-02 00:18:48 +0000170 bool SelectTrunc(const Instruction *I);
171 bool SelectIntExt(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000172
Eric Christopher83007122010-08-23 21:44:12 +0000173 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000174 private:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000175 bool isTypeLegal(Type *Ty, MVT &VT);
176 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosierade62002011-10-26 23:25:44 +0000177 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value);
Eric Christopher0d581222010-11-19 22:30:02 +0000178 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
179 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
180 bool ARMComputeAddress(const Value *Obj, Address &Addr);
181 void ARMSimplifyAddress(Address &Addr, EVT VT);
Chad Rosier87633022011-11-02 17:20:24 +0000182 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000183 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000184 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000185 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000186 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000187 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000188 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000189
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000190 // Call handling routines.
191 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000192 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
193 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000194 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000195 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000196 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000197 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000198 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
199 SmallVectorImpl<unsigned> &RegArgs,
200 CallingConv::ID CC,
201 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000202 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000203 const Instruction *I, CallingConv::ID CC,
204 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000205 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000206
207 // OptionalDef handling routines.
208 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000209 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000210 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
211 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000212 void AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000213 const MachineInstrBuilder &MIB,
214 unsigned Flags);
Eric Christopher456144e2010-08-19 00:37:05 +0000215};
Eric Christopherab695882010-07-21 22:26:11 +0000216
217} // end anonymous namespace
218
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000219#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000220
Eric Christopher456144e2010-08-19 00:37:05 +0000221// DefinesOptionalPredicate - This is different from DefinesPredicate in that
222// we don't care about implicit defs here, just places we'll need to add a
223// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
224bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Chenge837dea2011-06-28 19:10:37 +0000225 const MCInstrDesc &MCID = MI->getDesc();
226 if (!MCID.hasOptionalDef())
Eric Christopher456144e2010-08-19 00:37:05 +0000227 return false;
228
229 // Look to see if our OptionalDef is defining CPSR or CCR.
230 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
231 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000232 if (!MO.isReg() || !MO.isDef()) continue;
233 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000234 *CPSR = true;
235 }
236 return true;
237}
238
Eric Christopheraf3dce52011-03-12 01:09:29 +0000239bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000240 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher299bbb22011-04-29 00:03:10 +0000241
Eric Christopheraf3dce52011-03-12 01:09:29 +0000242 // If we're a thumb2 or not NEON function we were handled via isPredicable.
Evan Chenge837dea2011-06-28 19:10:37 +0000243 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopheraf3dce52011-03-12 01:09:29 +0000244 AFI->isThumb2Function())
245 return false;
Eric Christopher299bbb22011-04-29 00:03:10 +0000246
Evan Chenge837dea2011-06-28 19:10:37 +0000247 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
248 if (MCID.OpInfo[i].isPredicate())
Eric Christopheraf3dce52011-03-12 01:09:29 +0000249 return true;
Eric Christopher299bbb22011-04-29 00:03:10 +0000250
Eric Christopheraf3dce52011-03-12 01:09:29 +0000251 return false;
252}
253
Eric Christopher456144e2010-08-19 00:37:05 +0000254// If the machine is predicable go ahead and add the predicate operands, if
255// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000256// TODO: If we want to support thumb1 then we'll need to deal with optional
257// CPSR defs that need to be added before the remaining operands. See s_cc_out
258// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000259const MachineInstrBuilder &
260ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
261 MachineInstr *MI = &*MIB;
262
Eric Christopheraf3dce52011-03-12 01:09:29 +0000263 // Do we use a predicate? or...
264 // Are we NEON in ARM mode and have a predicate operand? If so, I know
265 // we're not predicable but add it anyways.
266 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000267 AddDefaultPred(MIB);
Eric Christopher299bbb22011-04-29 00:03:10 +0000268
Eric Christopher456144e2010-08-19 00:37:05 +0000269 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
270 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000271 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000272 if (DefinesOptionalPredicate(MI, &CPSR)) {
273 if (CPSR)
274 AddDefaultT1CC(MIB);
275 else
276 AddDefaultCC(MIB);
277 }
278 return MIB;
279}
280
Eric Christopher0fe7d542010-08-17 01:25:29 +0000281unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
282 const TargetRegisterClass* RC) {
283 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000284 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000285
Eric Christopher456144e2010-08-19 00:37:05 +0000286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000287 return ResultReg;
288}
289
290unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
291 const TargetRegisterClass *RC,
292 unsigned Op0, bool Op0IsKill) {
293 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000294 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000295
296 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000298 .addReg(Op0, Op0IsKill * RegState::Kill));
299 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000300 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000301 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000302 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000303 TII.get(TargetOpcode::COPY), ResultReg)
304 .addReg(II.ImplicitDefs[0]));
305 }
306 return ResultReg;
307}
308
309unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
310 const TargetRegisterClass *RC,
311 unsigned Op0, bool Op0IsKill,
312 unsigned Op1, bool Op1IsKill) {
313 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000314 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000315
316 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000317 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000318 .addReg(Op0, Op0IsKill * RegState::Kill)
319 .addReg(Op1, Op1IsKill * RegState::Kill));
320 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000321 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000322 .addReg(Op0, Op0IsKill * RegState::Kill)
323 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000325 TII.get(TargetOpcode::COPY), ResultReg)
326 .addReg(II.ImplicitDefs[0]));
327 }
328 return ResultReg;
329}
330
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000331unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
332 const TargetRegisterClass *RC,
333 unsigned Op0, bool Op0IsKill,
334 unsigned Op1, bool Op1IsKill,
335 unsigned Op2, bool Op2IsKill) {
336 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000337 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000338
339 if (II.getNumDefs() >= 1)
340 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
341 .addReg(Op0, Op0IsKill * RegState::Kill)
342 .addReg(Op1, Op1IsKill * RegState::Kill)
343 .addReg(Op2, Op2IsKill * RegState::Kill));
344 else {
345 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
346 .addReg(Op0, Op0IsKill * RegState::Kill)
347 .addReg(Op1, Op1IsKill * RegState::Kill)
348 .addReg(Op2, Op2IsKill * RegState::Kill));
349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
350 TII.get(TargetOpcode::COPY), ResultReg)
351 .addReg(II.ImplicitDefs[0]));
352 }
353 return ResultReg;
354}
355
Eric Christopher0fe7d542010-08-17 01:25:29 +0000356unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
357 const TargetRegisterClass *RC,
358 unsigned Op0, bool Op0IsKill,
359 uint64_t Imm) {
360 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000361 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000362
363 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000364 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000365 .addReg(Op0, Op0IsKill * RegState::Kill)
366 .addImm(Imm));
367 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000368 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000369 .addReg(Op0, Op0IsKill * RegState::Kill)
370 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000371 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000372 TII.get(TargetOpcode::COPY), ResultReg)
373 .addReg(II.ImplicitDefs[0]));
374 }
375 return ResultReg;
376}
377
378unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
379 const TargetRegisterClass *RC,
380 unsigned Op0, bool Op0IsKill,
381 const ConstantFP *FPImm) {
382 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000383 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000384
385 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000386 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000387 .addReg(Op0, Op0IsKill * RegState::Kill)
388 .addFPImm(FPImm));
389 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000390 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000391 .addReg(Op0, Op0IsKill * RegState::Kill)
392 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000393 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000394 TII.get(TargetOpcode::COPY), ResultReg)
395 .addReg(II.ImplicitDefs[0]));
396 }
397 return ResultReg;
398}
399
400unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
401 const TargetRegisterClass *RC,
402 unsigned Op0, bool Op0IsKill,
403 unsigned Op1, bool Op1IsKill,
404 uint64_t Imm) {
405 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000406 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000407
408 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000409 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000410 .addReg(Op0, Op0IsKill * RegState::Kill)
411 .addReg(Op1, Op1IsKill * RegState::Kill)
412 .addImm(Imm));
413 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000414 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000415 .addReg(Op0, Op0IsKill * RegState::Kill)
416 .addReg(Op1, Op1IsKill * RegState::Kill)
417 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000418 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000419 TII.get(TargetOpcode::COPY), ResultReg)
420 .addReg(II.ImplicitDefs[0]));
421 }
422 return ResultReg;
423}
424
425unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
426 const TargetRegisterClass *RC,
427 uint64_t Imm) {
428 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000429 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000430
Eric Christopher0fe7d542010-08-17 01:25:29 +0000431 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000432 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000433 .addImm(Imm));
434 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000435 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000436 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000437 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000438 TII.get(TargetOpcode::COPY), ResultReg)
439 .addReg(II.ImplicitDefs[0]));
440 }
441 return ResultReg;
442}
443
Eric Christopherd94bc542011-04-29 22:07:50 +0000444unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
445 const TargetRegisterClass *RC,
446 uint64_t Imm1, uint64_t Imm2) {
447 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000448 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher471e4222011-06-08 23:55:35 +0000449
Eric Christopherd94bc542011-04-29 22:07:50 +0000450 if (II.getNumDefs() >= 1)
451 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
452 .addImm(Imm1).addImm(Imm2));
453 else {
454 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
455 .addImm(Imm1).addImm(Imm2));
Eric Christopher471e4222011-06-08 23:55:35 +0000456 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherd94bc542011-04-29 22:07:50 +0000457 TII.get(TargetOpcode::COPY),
458 ResultReg)
459 .addReg(II.ImplicitDefs[0]));
460 }
461 return ResultReg;
462}
463
Eric Christopher0fe7d542010-08-17 01:25:29 +0000464unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
465 unsigned Op0, bool Op0IsKill,
466 uint32_t Idx) {
467 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
468 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
469 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000470 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000471 DL, TII.get(TargetOpcode::COPY), ResultReg)
472 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
473 return ResultReg;
474}
475
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000476// TODO: Don't worry about 64-bit now, but when this is fixed remove the
477// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000478unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000479 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000480
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000481 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
482 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
483 TII.get(ARM::VMOVRS), MoveReg)
484 .addReg(SrcReg));
485 return MoveReg;
486}
487
488unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000489 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000490
Eric Christopheraa3ace12010-09-09 20:49:25 +0000491 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
492 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000493 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000494 .addReg(SrcReg));
495 return MoveReg;
496}
497
Eric Christopher9ed58df2010-09-09 00:19:41 +0000498// For double width floating point we need to materialize two constants
499// (the high and the low) into integer registers then use a move to get
500// the combined constant into an FP reg.
501unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
502 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000503 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000504
Eric Christopher9ed58df2010-09-09 00:19:41 +0000505 // This checks to see if we can use VFP3 instructions to materialize
506 // a constant, otherwise we have to go through the constant pool.
507 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000508 int Imm;
509 unsigned Opc;
510 if (is64bit) {
511 Imm = ARM_AM::getFP64Imm(Val);
512 Opc = ARM::FCONSTD;
513 } else {
514 Imm = ARM_AM::getFP32Imm(Val);
515 Opc = ARM::FCONSTS;
516 }
Eric Christopher9ed58df2010-09-09 00:19:41 +0000517 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
518 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
519 DestReg)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000520 .addImm(Imm));
Eric Christopher9ed58df2010-09-09 00:19:41 +0000521 return DestReg;
522 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000523
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000524 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000525 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000526
Eric Christopher238bb162010-09-09 23:50:00 +0000527 // MachineConstantPool wants an explicit alignment.
528 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
529 if (Align == 0) {
530 // TODO: Figure out if this is correct.
531 Align = TD.getTypeAllocSize(CFP->getType());
532 }
533 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
534 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
535 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000536
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000537 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000538 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
539 DestReg)
540 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000541 .addReg(0));
542 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000543}
544
Eric Christopher744c7c82010-09-28 22:47:54 +0000545unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000546
Eric Christopher744c7c82010-09-28 22:47:54 +0000547 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000548 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000549
Eric Christophere5b13cf2010-11-03 20:21:17 +0000550 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
551
552 // If we can do this in a single instruction without a constant pool entry
553 // do so now.
554 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000555 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000556 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
557 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000558 TII.get(Opc), DestReg)
559 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000560 return DestReg;
561 }
562
Eric Christopher56d2b722010-09-02 23:43:26 +0000563 // MachineConstantPool wants an explicit alignment.
564 unsigned Align = TD.getPrefTypeAlignment(C->getType());
565 if (Align == 0) {
566 // TODO: Figure out if this is correct.
567 Align = TD.getTypeAllocSize(C->getType());
568 }
569 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000570
Eric Christopher56d2b722010-09-02 23:43:26 +0000571 if (isThumb)
572 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000573 TII.get(ARM::t2LDRpci), DestReg)
574 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000575 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000576 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000577 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000578 TII.get(ARM::LDRcp), DestReg)
579 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000580 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000581
Eric Christopher56d2b722010-09-02 23:43:26 +0000582 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000583}
584
Eric Christopherc9932f62010-10-01 23:24:42 +0000585unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000586 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000587 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000588
Eric Christopher890dbbe2010-10-02 00:32:44 +0000589 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000590
Eric Christopher890dbbe2010-10-02 00:32:44 +0000591 // TODO: Need more magic for ARM PIC.
592 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000593
Eric Christopher890dbbe2010-10-02 00:32:44 +0000594 // MachineConstantPool wants an explicit alignment.
595 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
596 if (Align == 0) {
597 // TODO: Figure out if this is correct.
598 Align = TD.getTypeAllocSize(GV->getType());
599 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000600
Eric Christopher890dbbe2010-10-02 00:32:44 +0000601 // Grab index.
602 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000603 unsigned Id = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +0000604 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
605 ARMCP::CPValue,
606 PCAdj);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000607 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000608
Eric Christopher890dbbe2010-10-02 00:32:44 +0000609 // Load value.
610 MachineInstrBuilder MIB;
611 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
612 if (isThumb) {
613 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
614 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
615 .addConstantPoolIndex(Idx);
616 if (RelocM == Reloc::PIC_)
617 MIB.addImm(Id);
618 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000619 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000620 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
621 DestReg)
622 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000623 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000624 }
625 AddOptionalDefs(MIB);
Eli Friedmand6412c92011-06-03 01:13:19 +0000626
627 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
628 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
629 if (isThumb)
Jim Grosbachb04546f2011-09-13 20:30:37 +0000630 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
631 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedmand6412c92011-06-03 01:13:19 +0000632 .addReg(DestReg)
633 .addImm(0);
634 else
635 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
636 NewDestReg)
637 .addReg(DestReg)
638 .addImm(0);
639 DestReg = NewDestReg;
640 AddOptionalDefs(MIB);
641 }
642
Eric Christopher890dbbe2010-10-02 00:32:44 +0000643 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000644}
645
Eric Christopher9ed58df2010-09-09 00:19:41 +0000646unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
647 EVT VT = TLI.getValueType(C->getType(), true);
648
649 // Only handle simple types.
650 if (!VT.isSimple()) return 0;
651
652 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
653 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000654 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
655 return ARMMaterializeGV(GV, VT);
656 else if (isa<ConstantInt>(C))
657 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000658
Eric Christopherc9932f62010-10-01 23:24:42 +0000659 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000660}
661
Eric Christopherf9764fa2010-09-30 20:49:44 +0000662unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
663 // Don't handle dynamic allocas.
664 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000665
Duncan Sands1440e8b2010-11-03 11:35:31 +0000666 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000667 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000668
Eric Christopherf9764fa2010-09-30 20:49:44 +0000669 DenseMap<const AllocaInst*, int>::iterator SI =
670 FuncInfo.StaticAllocaMap.find(AI);
671
672 // This will get lowered later into the correct offsets and registers
673 // via rewriteXFrameIndex.
674 if (SI != FuncInfo.StaticAllocaMap.end()) {
675 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
676 unsigned ResultReg = createResultReg(RC);
677 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
678 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
679 TII.get(Opc), ResultReg)
680 .addFrameIndex(SI->second)
681 .addImm(0));
682 return ResultReg;
683 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000684
Eric Christopherf9764fa2010-09-30 20:49:44 +0000685 return 0;
686}
687
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000688bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000689 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000690
Eric Christopherb1cc8482010-08-25 07:23:49 +0000691 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000692 if (evt == MVT::Other || !evt.isSimple()) return false;
693 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000694
Eric Christopherdc908042010-08-31 01:28:42 +0000695 // Handle all legal types, i.e. a register that will directly hold this
696 // value.
697 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000698}
699
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000700bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000701 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000702
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000703 // If this is a type than can be sign or zero-extended to a basic operation
704 // go ahead and accept it now.
705 if (VT == MVT::i8 || VT == MVT::i16)
706 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000707
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000708 return false;
709}
710
Eric Christopher88de86b2010-11-19 22:36:41 +0000711// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000712bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000713 // Some boilerplate from the X86 FastISel.
714 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000715 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000716 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000717 // Don't walk into other basic blocks unless the object is an alloca from
718 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000719 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
720 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
721 Opcode = I->getOpcode();
722 U = I;
723 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000724 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000725 Opcode = C->getOpcode();
726 U = C;
727 }
728
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000729 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000730 if (Ty->getAddressSpace() > 255)
731 // Fast instruction selection doesn't support the special
732 // address spaces.
733 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000734
Eric Christopher83007122010-08-23 21:44:12 +0000735 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000736 default:
Eric Christopher83007122010-08-23 21:44:12 +0000737 break;
Eric Christopher55324332010-10-12 00:43:21 +0000738 case Instruction::BitCast: {
739 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000740 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000741 }
742 case Instruction::IntToPtr: {
743 // Look past no-op inttoptrs.
744 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000745 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000746 break;
747 }
748 case Instruction::PtrToInt: {
749 // Look past no-op ptrtoints.
750 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000751 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000752 break;
753 }
Eric Christophereae84392010-10-14 09:29:41 +0000754 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000755 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000756 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000757
Eric Christophereae84392010-10-14 09:29:41 +0000758 // Iterate through the GEP folding the constants into offsets where
759 // we can.
760 gep_type_iterator GTI = gep_type_begin(U);
761 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
762 i != e; ++i, ++GTI) {
763 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000764 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Eric Christophereae84392010-10-14 09:29:41 +0000765 const StructLayout *SL = TD.getStructLayout(STy);
766 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
767 TmpOffset += SL->getElementOffset(Idx);
768 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000769 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000770 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000771 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
772 // Constant-offset addressing.
773 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000774 break;
775 }
776 if (isa<AddOperator>(Op) &&
777 (!isa<Instruction>(Op) ||
778 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
779 == FuncInfo.MBB) &&
780 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
Eric Christopher299bbb22011-04-29 00:03:10 +0000781 // An add (in the same block) with a constant operand. Fold the
Eric Christopher7244d7c2011-03-22 19:39:17 +0000782 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000783 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000784 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000785 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000786 // Iterate on the other operand.
787 Op = cast<AddOperator>(Op)->getOperand(0);
788 continue;
Eric Christopher299bbb22011-04-29 00:03:10 +0000789 }
Eric Christopher7244d7c2011-03-22 19:39:17 +0000790 // Unsupported
791 goto unsupported_gep;
792 }
Eric Christophereae84392010-10-14 09:29:41 +0000793 }
794 }
Eric Christopher2896df82010-10-15 18:02:07 +0000795
796 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000797 Addr.Offset = TmpOffset;
798 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000799
800 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000801 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000802
Eric Christophereae84392010-10-14 09:29:41 +0000803 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000804 break;
805 }
Eric Christopher83007122010-08-23 21:44:12 +0000806 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000807 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000808 DenseMap<const AllocaInst*, int>::iterator SI =
809 FuncInfo.StaticAllocaMap.find(AI);
810 if (SI != FuncInfo.StaticAllocaMap.end()) {
811 Addr.BaseType = Address::FrameIndexBase;
812 Addr.Base.FI = SI->second;
813 return true;
814 }
815 break;
Eric Christopher83007122010-08-23 21:44:12 +0000816 }
817 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000818
Eric Christophera9c57512010-10-13 21:41:51 +0000819 // Materialize the global variable's address into a reg which can
820 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000821 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000822 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
823 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000824
Eric Christopher0d581222010-11-19 22:30:02 +0000825 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000826 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000827 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000828
Eric Christophercb0b04b2010-08-24 00:07:24 +0000829 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000830 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
831 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000832}
833
Eric Christopher0d581222010-11-19 22:30:02 +0000834void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000835
Eric Christopher212ae932010-10-21 19:40:30 +0000836 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000837
Eric Christopher212ae932010-10-21 19:40:30 +0000838 bool needsLowering = false;
839 switch (VT.getSimpleVT().SimpleTy) {
840 default:
841 assert(false && "Unhandled load/store type!");
842 case MVT::i1:
843 case MVT::i8:
844 case MVT::i16:
845 case MVT::i32:
846 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000847 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000848 break;
849 case MVT::f32:
850 case MVT::f64:
851 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000852 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000853 break;
854 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000855
Eric Christopher827656d2010-11-20 22:38:27 +0000856 // If this is a stack pointer and the offset needs to be simplified then
857 // put the alloca address into a register, set the base type back to
858 // register and continue. This should almost never happen.
859 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
860 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
861 ARM::GPRRegisterClass;
862 unsigned ResultReg = createResultReg(RC);
863 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
864 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
865 TII.get(Opc), ResultReg)
866 .addFrameIndex(Addr.Base.FI)
867 .addImm(0));
868 Addr.Base.Reg = ResultReg;
869 Addr.BaseType = Address::RegBase;
870 }
871
Eric Christopher212ae932010-10-21 19:40:30 +0000872 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000873 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000874 if (needsLowering) {
Eli Friedman9ebf57a2011-04-29 21:22:56 +0000875 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
876 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopher0d581222010-11-19 22:30:02 +0000877 Addr.Offset = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000878 }
Eric Christopher83007122010-08-23 21:44:12 +0000879}
880
Eric Christopher564857f2010-12-01 01:40:24 +0000881void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000882 const MachineInstrBuilder &MIB,
883 unsigned Flags) {
Eric Christopher564857f2010-12-01 01:40:24 +0000884 // addrmode5 output depends on the selection dag addressing dividing the
885 // offset by 4 that it then later multiplies. Do this here as well.
886 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
887 VT.getSimpleVT().SimpleTy == MVT::f64)
888 Addr.Offset /= 4;
Eric Christopher299bbb22011-04-29 00:03:10 +0000889
Eric Christopher564857f2010-12-01 01:40:24 +0000890 // Frame base works a bit differently. Handle it separately.
891 if (Addr.BaseType == Address::FrameIndexBase) {
892 int FI = Addr.Base.FI;
893 int Offset = Addr.Offset;
894 MachineMemOperand *MMO =
895 FuncInfo.MF->getMachineMemOperand(
896 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000897 Flags,
Eric Christopher564857f2010-12-01 01:40:24 +0000898 MFI.getObjectSize(FI),
899 MFI.getObjectAlignment(FI));
900 // Now add the rest of the operands.
901 MIB.addFrameIndex(FI);
902
903 // ARM halfword load/stores need an additional operand.
904 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
905
906 MIB.addImm(Addr.Offset);
907 MIB.addMemOperand(MMO);
908 } else {
909 // Now add the rest of the operands.
910 MIB.addReg(Addr.Base.Reg);
Eric Christopher299bbb22011-04-29 00:03:10 +0000911
Eric Christopher564857f2010-12-01 01:40:24 +0000912 // ARM halfword load/stores need an additional operand.
913 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
914
915 MIB.addImm(Addr.Offset);
916 }
917 AddOptionalDefs(MIB);
918}
919
Eric Christopher0d581222010-11-19 22:30:02 +0000920bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000921
Eric Christopherb1cc8482010-08-25 07:23:49 +0000922 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000923 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000924 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000925 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000926 // This is mostly going to be Neon/vector support.
927 default: return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000928 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000929 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000930 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000931 break;
932 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000933 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000934 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000935 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000936 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000937 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000938 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000939 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000940 case MVT::f32:
941 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000942 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000943 break;
944 case MVT::f64:
945 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000946 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000947 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000948 }
Eric Christopher564857f2010-12-01 01:40:24 +0000949 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000950 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000951
Eric Christopher564857f2010-12-01 01:40:24 +0000952 // Create the base instruction, then add the operands.
953 ResultReg = createResultReg(RC);
954 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
955 TII.get(Opc), ResultReg);
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000956 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad);
Eric Christopherdc908042010-08-31 01:28:42 +0000957 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000958}
959
Eric Christopher43b62be2010-09-27 06:02:23 +0000960bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000961 // Atomic loads need special handling.
962 if (cast<LoadInst>(I)->isAtomic())
963 return false;
964
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000965 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000966 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000967 if (!isLoadTypeLegal(I->getType(), VT))
968 return false;
969
Eric Christopher564857f2010-12-01 01:40:24 +0000970 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000971 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +0000972 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000973
974 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000975 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000976 UpdateValueMap(I, ResultReg);
977 return true;
978}
979
Eric Christopher0d581222010-11-19 22:30:02 +0000980bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000981 unsigned StrOpc;
982 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000983 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000984 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000985 case MVT::i1: {
986 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
987 ARM::GPRRegisterClass);
988 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
989 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
990 TII.get(Opc), Res)
991 .addReg(SrcReg).addImm(1));
992 SrcReg = Res;
993 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000994 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000995 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000996 break;
997 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000998 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000999 break;
Eric Christopher47650ec2010-10-16 01:10:35 +00001000 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001001 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +00001002 break;
Eric Christopher56d2b722010-09-02 23:43:26 +00001003 case MVT::f32:
1004 if (!Subtarget->hasVFP2()) return false;
1005 StrOpc = ARM::VSTRS;
1006 break;
1007 case MVT::f64:
1008 if (!Subtarget->hasVFP2()) return false;
1009 StrOpc = ARM::VSTRD;
1010 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001011 }
Eric Christopher564857f2010-12-01 01:40:24 +00001012 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +00001013 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +00001014
Eric Christopher564857f2010-12-01 01:40:24 +00001015 // Create the base instruction, then add the operands.
1016 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1017 TII.get(StrOpc))
1018 .addReg(SrcReg, getKillRegState(true));
Cameron Zwarichc152aa62011-05-28 20:34:49 +00001019 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore);
Eric Christopher318b6ee2010-09-02 00:53:56 +00001020 return true;
1021}
1022
Eric Christopher43b62be2010-09-27 06:02:23 +00001023bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001024 Value *Op0 = I->getOperand(0);
1025 unsigned SrcReg = 0;
1026
Eli Friedman4136d232011-09-02 22:33:24 +00001027 // Atomic stores need special handling.
1028 if (cast<StoreInst>(I)->isAtomic())
1029 return false;
1030
Eric Christopher564857f2010-12-01 01:40:24 +00001031 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001032 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001033 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +00001034 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001035
Eric Christopher1b61ef42010-09-02 01:48:11 +00001036 // Get the value to be stored into a register.
1037 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +00001038 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001039
Eric Christopher564857f2010-12-01 01:40:24 +00001040 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001041 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +00001042 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +00001043 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001044
Eric Christopher0d581222010-11-19 22:30:02 +00001045 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +00001046 return true;
1047}
1048
1049static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1050 switch (Pred) {
1051 // Needs two compares...
1052 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001053 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +00001054 default:
Eric Christopher4053e632010-11-02 01:24:49 +00001055 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +00001056 return ARMCC::AL;
1057 case CmpInst::ICMP_EQ:
1058 case CmpInst::FCMP_OEQ:
1059 return ARMCC::EQ;
1060 case CmpInst::ICMP_SGT:
1061 case CmpInst::FCMP_OGT:
1062 return ARMCC::GT;
1063 case CmpInst::ICMP_SGE:
1064 case CmpInst::FCMP_OGE:
1065 return ARMCC::GE;
1066 case CmpInst::ICMP_UGT:
1067 case CmpInst::FCMP_UGT:
1068 return ARMCC::HI;
1069 case CmpInst::FCMP_OLT:
1070 return ARMCC::MI;
1071 case CmpInst::ICMP_ULE:
1072 case CmpInst::FCMP_OLE:
1073 return ARMCC::LS;
1074 case CmpInst::FCMP_ORD:
1075 return ARMCC::VC;
1076 case CmpInst::FCMP_UNO:
1077 return ARMCC::VS;
1078 case CmpInst::FCMP_UGE:
1079 return ARMCC::PL;
1080 case CmpInst::ICMP_SLT:
1081 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001082 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001083 case CmpInst::ICMP_SLE:
1084 case CmpInst::FCMP_ULE:
1085 return ARMCC::LE;
1086 case CmpInst::FCMP_UNE:
1087 case CmpInst::ICMP_NE:
1088 return ARMCC::NE;
1089 case CmpInst::ICMP_UGE:
1090 return ARMCC::HS;
1091 case CmpInst::ICMP_ULT:
1092 return ARMCC::LO;
1093 }
Eric Christopher543cf052010-09-01 22:16:27 +00001094}
1095
Eric Christopher43b62be2010-09-27 06:02:23 +00001096bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001097 const BranchInst *BI = cast<BranchInst>(I);
1098 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1099 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001100
Eric Christophere5734102010-09-03 00:35:47 +00001101 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001102
Eric Christopher0e6233b2010-10-29 21:08:19 +00001103 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1104 // behavior.
Eric Christopher0e6233b2010-10-29 21:08:19 +00001105 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosier75698f32011-10-26 23:17:28 +00001106 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001107
1108 // Get the compare predicate.
Eric Christopher632ae892011-04-29 21:56:31 +00001109 // Try to take advantage of fallthrough opportunities.
1110 CmpInst::Predicate Predicate = CI->getPredicate();
1111 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1112 std::swap(TBB, FBB);
1113 Predicate = CmpInst::getInversePredicate(Predicate);
1114 }
1115
1116 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher0e6233b2010-10-29 21:08:19 +00001117
1118 // We may not handle every CC for now.
1119 if (ARMPred == ARMCC::AL) return false;
1120
Chad Rosier75698f32011-10-26 23:17:28 +00001121 // Emit the compare.
Chad Rosierade62002011-10-26 23:25:44 +00001122 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1)))
Chad Rosier75698f32011-10-26 23:17:28 +00001123 return false;
Jim Grosbach16cb3762010-11-09 19:22:26 +00001124
Eric Christopher0e6233b2010-10-29 21:08:19 +00001125 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1126 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1127 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1128 FastEmitBranch(FBB, DL);
1129 FuncInfo.MBB->addSuccessor(TBB);
1130 return true;
1131 }
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001132 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1133 MVT SourceVT;
1134 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedman76927d732011-05-25 23:49:02 +00001135 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001136 unsigned TstOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1137 unsigned OpReg = getRegForValue(TI->getOperand(0));
1138 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1139 TII.get(TstOpc))
1140 .addReg(OpReg).addImm(1));
1141
1142 unsigned CCMode = ARMCC::NE;
1143 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1144 std::swap(TBB, FBB);
1145 CCMode = ARMCC::EQ;
1146 }
1147
1148 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1149 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1150 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1151
1152 FastEmitBranch(FBB, DL);
1153 FuncInfo.MBB->addSuccessor(TBB);
1154 return true;
1155 }
Chad Rosier6d64b3a2011-10-27 00:21:16 +00001156 } else if (const ConstantInt *CI =
1157 dyn_cast<ConstantInt>(BI->getCondition())) {
1158 uint64_t Imm = CI->getZExtValue();
1159 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1160 FastEmitBranch(Target, DL);
1161 return true;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001162 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001163
Eric Christopher0e6233b2010-10-29 21:08:19 +00001164 unsigned CmpReg = getRegForValue(BI->getCondition());
1165 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001166
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001167 // We've been divorced from our compare! Our block was split, and
1168 // now our compare lives in a predecessor block. We musn't
1169 // re-compare here, as the children of the compare aren't guaranteed
1170 // live across the block boundary (we *could* check for this).
1171 // Regardless, the compare has been done in the predecessor block,
1172 // and it left a value for us in a virtual register. Ergo, we test
1173 // the one-bit value left in the virtual register.
1174 unsigned TstOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1175 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1176 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001177
Eric Christopher7a20a372011-04-28 16:52:09 +00001178 unsigned CCMode = ARMCC::NE;
1179 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1180 std::swap(TBB, FBB);
1181 CCMode = ARMCC::EQ;
1182 }
1183
Eric Christophere5734102010-09-03 00:35:47 +00001184 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001185 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher7a20a372011-04-28 16:52:09 +00001186 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001187 FastEmitBranch(FBB, DL);
1188 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001189 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001190}
1191
Chad Rosierade62002011-10-26 23:25:44 +00001192bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001193 MVT VT;
Chad Rosierade62002011-10-26 23:25:44 +00001194 Type *Ty = Src1Value->getType();
Eric Christopherd43393a2010-09-08 23:13:45 +00001195 if (!isTypeLegal(Ty, VT))
1196 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001197
Chad Rosierade62002011-10-26 23:25:44 +00001198 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1199 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherd43393a2010-09-08 23:13:45 +00001200 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001201
Eric Christopherd43393a2010-09-08 23:13:45 +00001202 unsigned CmpOpc;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001203 switch (VT.SimpleTy) {
Chad Rosier8ff26642011-10-26 23:34:37 +00001204 // TODO: Add support for non-legal types (i.e., i1, i8, i16).
Eric Christopherd43393a2010-09-08 23:13:45 +00001205 default: return false;
1206 // TODO: Verify compares.
1207 case MVT::f32:
1208 CmpOpc = ARM::VCMPES;
1209 break;
1210 case MVT::f64:
1211 CmpOpc = ARM::VCMPED;
1212 break;
1213 case MVT::i32:
1214 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
1215 break;
1216 }
1217
Chad Rosier530f7ce2011-10-26 22:47:55 +00001218 unsigned Src1 = getRegForValue(Src1Value);
1219 if (Src1 == 0) return false;
1220
1221 unsigned Src2 = getRegForValue(Src2Value);
1222 if (Src2 == 0) return false;
1223
1224 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1225 .addReg(Src1).addReg(Src2));
Chad Rosierade62002011-10-26 23:25:44 +00001226
1227 // For floating point we need to move the result to a comparison register
1228 // that we can then use for branches.
1229 if (Ty->isFloatTy() || Ty->isDoubleTy())
1230 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1231 TII.get(ARM::FMSTAT)));
Chad Rosier530f7ce2011-10-26 22:47:55 +00001232 return true;
1233}
1234
1235bool ARMFastISel::SelectCmp(const Instruction *I) {
1236 const CmpInst *CI = cast<CmpInst>(I);
Chad Rosierade62002011-10-26 23:25:44 +00001237 Type *Ty = CI->getOperand(0)->getType();
Chad Rosier530f7ce2011-10-26 22:47:55 +00001238
Eric Christopher229207a2010-09-29 01:14:47 +00001239 // Get the compare predicate.
1240 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001241
Eric Christopher229207a2010-09-29 01:14:47 +00001242 // We may not handle every CC for now.
1243 if (ARMPred == ARMCC::AL) return false;
1244
Chad Rosier530f7ce2011-10-26 22:47:55 +00001245 // Emit the compare.
Chad Rosierade62002011-10-26 23:25:44 +00001246 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1)))
Chad Rosier530f7ce2011-10-26 22:47:55 +00001247 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001248
Eric Christopher229207a2010-09-29 01:14:47 +00001249 // Now set a register based on the comparison. Explicitly set the predicates
1250 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001251 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001252 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001253 : ARM::GPRRegisterClass;
1254 unsigned DestReg = createResultReg(RC);
Chad Rosierade62002011-10-26 23:25:44 +00001255 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001256 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosierade62002011-10-26 23:25:44 +00001257 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
Chad Rosier530f7ce2011-10-26 22:47:55 +00001258 unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR;
Eric Christopher229207a2010-09-29 01:14:47 +00001259 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1260 .addReg(ZeroReg).addImm(1)
1261 .addImm(ARMPred).addReg(CondReg);
1262
Eric Christophera5b1e682010-09-17 22:28:18 +00001263 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001264 return true;
1265}
1266
Eric Christopher43b62be2010-09-27 06:02:23 +00001267bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001268 // Make sure we have VFP and that we're extending float to double.
1269 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001270
Eric Christopher46203602010-09-09 00:26:48 +00001271 Value *V = I->getOperand(0);
1272 if (!I->getType()->isDoubleTy() ||
1273 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001274
Eric Christopher46203602010-09-09 00:26:48 +00001275 unsigned Op = getRegForValue(V);
1276 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001277
Eric Christopher46203602010-09-09 00:26:48 +00001278 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001279 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001280 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001281 .addReg(Op));
1282 UpdateValueMap(I, Result);
1283 return true;
1284}
1285
Eric Christopher43b62be2010-09-27 06:02:23 +00001286bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001287 // Make sure we have VFP and that we're truncating double to float.
1288 if (!Subtarget->hasVFP2()) return false;
1289
1290 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001291 if (!(I->getType()->isFloatTy() &&
1292 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001293
1294 unsigned Op = getRegForValue(V);
1295 if (Op == 0) return false;
1296
1297 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001298 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001299 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001300 .addReg(Op));
1301 UpdateValueMap(I, Result);
1302 return true;
1303}
1304
Eric Christopher43b62be2010-09-27 06:02:23 +00001305bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001306 // Make sure we have VFP.
1307 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001308
Duncan Sands1440e8b2010-11-03 11:35:31 +00001309 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001310 Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001311 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001312 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001313
Eli Friedman783c6642011-05-25 19:09:45 +00001314 // FIXME: Handle sign-extension where necessary.
1315 if (!I->getOperand(0)->getType()->isIntegerTy(32))
1316 return false;
1317
Eric Christopher9a040492010-09-09 18:54:59 +00001318 unsigned Op = getRegForValue(I->getOperand(0));
1319 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001320
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001321 // The conversion routine works on fp-reg to fp-reg and the operand above
1322 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001323 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001324 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001325
Eric Christopher9a040492010-09-09 18:54:59 +00001326 unsigned Opc;
1327 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1328 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001329 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001330
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001331 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001332 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1333 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001334 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001335 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001336 return true;
1337}
1338
Eric Christopher43b62be2010-09-27 06:02:23 +00001339bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001340 // Make sure we have VFP.
1341 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001342
Duncan Sands1440e8b2010-11-03 11:35:31 +00001343 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001344 Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001345 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001346 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001347
Eric Christopher9a040492010-09-09 18:54:59 +00001348 unsigned Op = getRegForValue(I->getOperand(0));
1349 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001350
Eric Christopher9a040492010-09-09 18:54:59 +00001351 unsigned Opc;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001352 Type *OpTy = I->getOperand(0)->getType();
Eric Christopher9a040492010-09-09 18:54:59 +00001353 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1354 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001355 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001356
Eric Christopher022b7fb2010-10-05 23:13:24 +00001357 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1358 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1360 ResultReg)
1361 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001362
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001363 // This result needs to be in an integer register, but the conversion only
1364 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001365 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001366 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001367
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001368 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001369 return true;
1370}
1371
Eric Christopher3bbd3962010-10-11 08:27:59 +00001372bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001373 MVT VT;
1374 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001375 return false;
1376
1377 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001378 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001379 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1380
1381 unsigned CondReg = getRegForValue(I->getOperand(0));
1382 if (CondReg == 0) return false;
1383 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1384 if (Op1Reg == 0) return false;
1385 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1386 if (Op2Reg == 0) return false;
1387
1388 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1389 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1390 .addReg(CondReg).addImm(1));
1391 unsigned ResultReg = createResultReg(RC);
1392 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1393 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1394 .addReg(Op1Reg).addReg(Op2Reg)
1395 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1396 UpdateValueMap(I, ResultReg);
1397 return true;
1398}
1399
Eric Christopher08637852010-09-30 22:34:19 +00001400bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001401 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001402 Type *Ty = I->getType();
Eric Christopher08637852010-09-30 22:34:19 +00001403 if (!isTypeLegal(Ty, VT))
1404 return false;
1405
1406 // If we have integer div support we should have selected this automagically.
1407 // In case we have a real miss go ahead and return false and we'll pick
1408 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001409 if (Subtarget->hasDivide()) return false;
1410
Eric Christopher08637852010-09-30 22:34:19 +00001411 // Otherwise emit a libcall.
1412 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001413 if (VT == MVT::i8)
1414 LC = RTLIB::SDIV_I8;
1415 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001416 LC = RTLIB::SDIV_I16;
1417 else if (VT == MVT::i32)
1418 LC = RTLIB::SDIV_I32;
1419 else if (VT == MVT::i64)
1420 LC = RTLIB::SDIV_I64;
1421 else if (VT == MVT::i128)
1422 LC = RTLIB::SDIV_I128;
1423 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001424
Eric Christopher08637852010-09-30 22:34:19 +00001425 return ARMEmitLibcall(I, LC);
1426}
1427
Eric Christopher6a880d62010-10-11 08:37:26 +00001428bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001429 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001430 Type *Ty = I->getType();
Eric Christopher6a880d62010-10-11 08:37:26 +00001431 if (!isTypeLegal(Ty, VT))
1432 return false;
1433
1434 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1435 if (VT == MVT::i8)
1436 LC = RTLIB::SREM_I8;
1437 else if (VT == MVT::i16)
1438 LC = RTLIB::SREM_I16;
1439 else if (VT == MVT::i32)
1440 LC = RTLIB::SREM_I32;
1441 else if (VT == MVT::i64)
1442 LC = RTLIB::SREM_I64;
1443 else if (VT == MVT::i128)
1444 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001445 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001446
Eric Christopher6a880d62010-10-11 08:37:26 +00001447 return ARMEmitLibcall(I, LC);
1448}
1449
Eric Christopher43b62be2010-09-27 06:02:23 +00001450bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001451 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001452
Eric Christopherbc39b822010-09-09 00:53:57 +00001453 // We can get here in the case when we want to use NEON for our fp
1454 // operations, but can't figure out how to. Just use the vfp instructions
1455 // if we have them.
1456 // FIXME: It'd be nice to use NEON instructions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001457 Type *Ty = I->getType();
Eric Christopherbd6bf082010-09-09 01:02:03 +00001458 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1459 if (isFloat && !Subtarget->hasVFP2())
1460 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001461
Eric Christopherbc39b822010-09-09 00:53:57 +00001462 unsigned Op1 = getRegForValue(I->getOperand(0));
1463 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001464
Eric Christopherbc39b822010-09-09 00:53:57 +00001465 unsigned Op2 = getRegForValue(I->getOperand(1));
1466 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001467
Eric Christopherbc39b822010-09-09 00:53:57 +00001468 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001469 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001470 switch (ISDOpcode) {
1471 default: return false;
1472 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001473 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001474 break;
1475 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001476 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001477 break;
1478 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001479 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001480 break;
1481 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001482 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001483 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1484 TII.get(Opc), ResultReg)
1485 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001486 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001487 return true;
1488}
1489
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001490// Call Handling Code
1491
Eric Christopherfa87d662010-10-18 02:17:53 +00001492bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1493 EVT SrcVT, unsigned &ResultReg) {
1494 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1495 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001496
Eric Christopherfa87d662010-10-18 02:17:53 +00001497 if (RR != 0) {
1498 ResultReg = RR;
1499 return true;
1500 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001501 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001502}
1503
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001504// This is largely taken directly from CCAssignFnForNode - we don't support
1505// varargs in FastISel so that part has been removed.
1506// TODO: We may not support all of this.
1507CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1508 switch (CC) {
1509 default:
1510 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001511 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001512 // Ignore fastcc. Silence compiler warnings.
1513 (void)RetFastCC_ARM_APCS;
1514 (void)FastCC_ARM_APCS;
1515 // Fallthrough
1516 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001517 // Use target triple & subtarget features to do actual dispatch.
1518 if (Subtarget->isAAPCS_ABI()) {
1519 if (Subtarget->hasVFP2() &&
1520 FloatABIType == FloatABI::Hard)
1521 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1522 else
1523 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1524 } else
1525 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1526 case CallingConv::ARM_AAPCS_VFP:
1527 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1528 case CallingConv::ARM_AAPCS:
1529 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1530 case CallingConv::ARM_APCS:
1531 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1532 }
1533}
1534
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001535bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1536 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001537 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001538 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1539 SmallVectorImpl<unsigned> &RegArgs,
1540 CallingConv::ID CC,
1541 unsigned &NumBytes) {
1542 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001543 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001544 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1545
1546 // Get a count of how many bytes are to be pushed on the stack.
1547 NumBytes = CCInfo.getNextStackOffset();
1548
1549 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001550 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001551 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1552 TII.get(AdjStackDown))
1553 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001554
1555 // Process the args.
1556 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1557 CCValAssign &VA = ArgLocs[i];
1558 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001559 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001560
Eric Christopher4a2b3162011-01-27 05:44:56 +00001561 // We don't handle NEON/vector parameters yet.
1562 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001563 return false;
1564
Eric Christopherf9764fa2010-09-30 20:49:44 +00001565 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001566 switch (VA.getLocInfo()) {
1567 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001568 case CCValAssign::SExt: {
1569 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1570 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001571 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001572 Emitted = true;
1573 ArgVT = VA.getLocVT();
1574 break;
1575 }
1576 case CCValAssign::ZExt: {
1577 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1578 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001579 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001580 Emitted = true;
1581 ArgVT = VA.getLocVT();
1582 break;
1583 }
1584 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001585 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1586 Arg, ArgVT, Arg);
1587 if (!Emitted)
1588 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1589 Arg, ArgVT, Arg);
1590 if (!Emitted)
1591 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1592 Arg, ArgVT, Arg);
1593
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001594 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001595 ArgVT = VA.getLocVT();
1596 break;
1597 }
1598 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001599 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001600 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001601 assert(BC != 0 && "Failed to emit a bitcast!");
1602 Arg = BC;
1603 ArgVT = VA.getLocVT();
1604 break;
1605 }
1606 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001607 }
1608
1609 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001610 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001611 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001612 VA.getLocReg())
1613 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001614 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001615 } else if (VA.needsCustom()) {
1616 // TODO: We need custom lowering for vector (v2f64) args.
1617 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001618
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001619 CCValAssign &NextVA = ArgLocs[++i];
1620
1621 // TODO: Only handle register args for now.
1622 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1623
1624 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1625 TII.get(ARM::VMOVRRD), VA.getLocReg())
1626 .addReg(NextVA.getLocReg(), RegState::Define)
1627 .addReg(Arg));
1628 RegArgs.push_back(VA.getLocReg());
1629 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001630 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001631 assert(VA.isMemLoc());
1632 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001633 Address Addr;
1634 Addr.BaseType = Address::RegBase;
1635 Addr.Base.Reg = ARM::SP;
1636 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001637
Eric Christopher0d581222010-11-19 22:30:02 +00001638 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001639 }
1640 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001641 return true;
1642}
1643
Duncan Sands1440e8b2010-11-03 11:35:31 +00001644bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001645 const Instruction *I, CallingConv::ID CC,
1646 unsigned &NumBytes) {
1647 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001648 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001649 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1650 TII.get(AdjStackUp))
1651 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001652
1653 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001654 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001655 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001656 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001657 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1658
1659 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001660 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001661 // For this move we copy into two registers and then move into the
1662 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001663 EVT DestVT = RVLocs[0].getValVT();
1664 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1665 unsigned ResultReg = createResultReg(DstRC);
1666 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1667 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001668 .addReg(RVLocs[0].getLocReg())
1669 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001670
Eric Christopher3659ac22010-10-20 08:02:24 +00001671 UsedRegs.push_back(RVLocs[0].getLocReg());
1672 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001673
Eric Christopherdccd2c32010-10-11 08:38:55 +00001674 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001675 UpdateValueMap(I, ResultReg);
1676 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001677 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001678 EVT CopyVT = RVLocs[0].getValVT();
1679 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001680
Eric Christopher14df8822010-10-01 00:00:11 +00001681 unsigned ResultReg = createResultReg(DstRC);
1682 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1683 ResultReg).addReg(RVLocs[0].getLocReg());
1684 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001685
Eric Christopherdccd2c32010-10-11 08:38:55 +00001686 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001687 UpdateValueMap(I, ResultReg);
1688 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001689 }
1690
Eric Christopherdccd2c32010-10-11 08:38:55 +00001691 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001692}
1693
Eric Christopher4f512ef2010-10-22 01:28:00 +00001694bool ARMFastISel::SelectRet(const Instruction *I) {
1695 const ReturnInst *Ret = cast<ReturnInst>(I);
1696 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001697
Eric Christopher4f512ef2010-10-22 01:28:00 +00001698 if (!FuncInfo.CanLowerReturn)
1699 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001700
Eric Christopher4f512ef2010-10-22 01:28:00 +00001701 if (F.isVarArg())
1702 return false;
1703
1704 CallingConv::ID CC = F.getCallingConv();
1705 if (Ret->getNumOperands() > 0) {
1706 SmallVector<ISD::OutputArg, 4> Outs;
1707 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1708 Outs, TLI);
1709
1710 // Analyze operands of the call, assigning locations to each operand.
1711 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbachb04546f2011-09-13 20:30:37 +00001712 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Eric Christopher4f512ef2010-10-22 01:28:00 +00001713 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1714
1715 const Value *RV = Ret->getOperand(0);
1716 unsigned Reg = getRegForValue(RV);
1717 if (Reg == 0)
1718 return false;
1719
1720 // Only handle a single return value for now.
1721 if (ValLocs.size() != 1)
1722 return false;
1723
1724 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001725
Eric Christopher4f512ef2010-10-22 01:28:00 +00001726 // Don't bother handling odd stuff for now.
Chad Rosier3a7572f2011-10-17 22:54:23 +00001727 // FIXME: Should be able to handle i1, i8, and/or i16 return types.
Eric Christopher4f512ef2010-10-22 01:28:00 +00001728 if (VA.getLocInfo() != CCValAssign::Full)
1729 return false;
1730 // Only handle register returns for now.
1731 if (!VA.isRegLoc())
1732 return false;
1733 // TODO: For now, don't try to handle cases where getLocInfo()
1734 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001735 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001736 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001737
Eric Christopher4f512ef2010-10-22 01:28:00 +00001738 // Make the copy.
1739 unsigned SrcReg = Reg + VA.getValNo();
1740 unsigned DstReg = VA.getLocReg();
1741 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1742 // Avoid a cross-class copy. This is very unlikely.
1743 if (!SrcRC->contains(DstReg))
1744 return false;
1745 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1746 DstReg).addReg(SrcReg);
1747
1748 // Mark the register as live out of the function.
1749 MRI.addLiveOut(VA.getLocReg());
1750 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001751
Eric Christopher4f512ef2010-10-22 01:28:00 +00001752 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1753 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1754 TII.get(RetOpc)));
1755 return true;
1756}
1757
Eric Christopher872f4a22011-02-22 01:37:10 +00001758unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1759
Eric Christopher872f4a22011-02-22 01:37:10 +00001760 // Darwin needs the r9 versions of the opcodes.
1761 bool isDarwin = Subtarget->isTargetDarwin();
Eric Christopher04356612011-04-05 00:39:26 +00001762 if (isThumb) {
Eric Christopher872f4a22011-02-22 01:37:10 +00001763 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1764 } else {
1765 return isDarwin ? ARM::BLr9 : ARM::BL;
1766 }
1767}
1768
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001769// A quick function that will emit a call for a named libcall in F with the
1770// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001771// can emit a call for any libcall we can produce. This is an abridged version
1772// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001773// like computed function pointers or strange arguments at call sites.
1774// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1775// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001776bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1777 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001778
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001779 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001780 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001781 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001782 if (RetTy->isVoidTy())
1783 RetVT = MVT::isVoid;
1784 else if (!isTypeLegal(RetTy, RetVT))
1785 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001786
Eric Christopher836c6242010-12-15 23:47:29 +00001787 // TODO: For now if we have long calls specified we don't handle the call.
1788 if (EnableARMLongCalls) return false;
1789
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001790 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001791 SmallVector<Value*, 8> Args;
1792 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001793 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001794 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1795 Args.reserve(I->getNumOperands());
1796 ArgRegs.reserve(I->getNumOperands());
1797 ArgVTs.reserve(I->getNumOperands());
1798 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001799 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001800 Value *Op = I->getOperand(i);
1801 unsigned Arg = getRegForValue(Op);
1802 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001803
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001804 Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001805 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001806 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001807
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001808 ISD::ArgFlagsTy Flags;
1809 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1810 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001811
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001812 Args.push_back(Op);
1813 ArgRegs.push_back(Arg);
1814 ArgVTs.push_back(ArgVT);
1815 ArgFlags.push_back(Flags);
1816 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001817
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001818 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001819 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001820 unsigned NumBytes;
1821 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1822 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001823
Eric Christopher6344a5f2011-04-29 00:07:20 +00001824 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001825 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001826 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001827 unsigned CallOpc = ARMSelectCallOp(NULL);
1828 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001829 // Explicitly adding the predicate here.
1830 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1831 TII.get(CallOpc)))
1832 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00001833 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001834 // Explicitly adding the predicate here.
1835 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1836 TII.get(CallOpc))
1837 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001838
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001839 // Add implicit physical register uses to the call.
1840 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1841 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001842
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001843 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001844 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001845 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001846
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001847 // Set all unused physreg defs as dead.
1848 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001849
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001850 return true;
1851}
1852
Eric Christopherf9764fa2010-09-30 20:49:44 +00001853bool ARMFastISel::SelectCall(const Instruction *I) {
1854 const CallInst *CI = cast<CallInst>(I);
1855 const Value *Callee = CI->getCalledValue();
1856
1857 // Can't handle inline asm or worry about intrinsics yet.
1858 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1859
Eric Christopher52f6c032011-05-02 20:16:33 +00001860 // Only handle global variable Callees.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001861 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christopher52f6c032011-05-02 20:16:33 +00001862 if (!GV)
Eric Christophere6ca6772010-10-01 21:33:12 +00001863 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001864
Eric Christopherf9764fa2010-09-30 20:49:44 +00001865 // Check the calling convention.
1866 ImmutableCallSite CS(CI);
1867 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001868
Eric Christopherf9764fa2010-09-30 20:49:44 +00001869 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001870
Eric Christopherf9764fa2010-09-30 20:49:44 +00001871 // Let SDISel handle vararg functions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001872 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1873 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eric Christopherf9764fa2010-09-30 20:49:44 +00001874 if (FTy->isVarArg())
1875 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001876
Eric Christopherf9764fa2010-09-30 20:49:44 +00001877 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001878 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001879 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001880 if (RetTy->isVoidTy())
1881 RetVT = MVT::isVoid;
1882 else if (!isTypeLegal(RetTy, RetVT))
1883 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001884
Eric Christopher836c6242010-12-15 23:47:29 +00001885 // TODO: For now if we have long calls specified we don't handle the call.
1886 if (EnableARMLongCalls) return false;
Eric Christopher299bbb22011-04-29 00:03:10 +00001887
Eric Christopherf9764fa2010-09-30 20:49:44 +00001888 // Set up the argument vectors.
1889 SmallVector<Value*, 8> Args;
1890 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001891 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001892 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1893 Args.reserve(CS.arg_size());
1894 ArgRegs.reserve(CS.arg_size());
1895 ArgVTs.reserve(CS.arg_size());
1896 ArgFlags.reserve(CS.arg_size());
1897 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1898 i != e; ++i) {
1899 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001900
Eric Christopherf9764fa2010-09-30 20:49:44 +00001901 if (Arg == 0)
1902 return false;
1903 ISD::ArgFlagsTy Flags;
1904 unsigned AttrInd = i - CS.arg_begin() + 1;
1905 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1906 Flags.setSExt();
1907 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1908 Flags.setZExt();
1909
1910 // FIXME: Only handle *easy* calls for now.
1911 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1912 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1913 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1914 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1915 return false;
1916
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001917 Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001918 MVT ArgVT;
Chad Rosier3a7572f2011-10-17 22:54:23 +00001919 // FIXME: Should be able to handle i1, i8, and/or i16 parameters.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001920 if (!isTypeLegal(ArgTy, ArgVT))
1921 return false;
1922 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1923 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001924
Eric Christopherf9764fa2010-09-30 20:49:44 +00001925 Args.push_back(*i);
1926 ArgRegs.push_back(Arg);
1927 ArgVTs.push_back(ArgVT);
1928 ArgFlags.push_back(Flags);
1929 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001930
Eric Christopherf9764fa2010-09-30 20:49:44 +00001931 // Handle the arguments now that we've gotten them.
1932 SmallVector<unsigned, 4> RegArgs;
1933 unsigned NumBytes;
1934 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1935 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001936
Eric Christopher6344a5f2011-04-29 00:07:20 +00001937 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001938 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001939 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001940 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00001941 // Explicitly adding the predicate here.
Eric Christopher872f4a22011-02-22 01:37:10 +00001942 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001943 // Explicitly adding the predicate here.
1944 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1945 TII.get(CallOpc)))
1946 .addGlobalAddress(GV, 0, 0);
Eric Christopher872f4a22011-02-22 01:37:10 +00001947 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001948 // Explicitly adding the predicate here.
1949 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1950 TII.get(CallOpc))
1951 .addGlobalAddress(GV, 0, 0));
Eric Christopher299bbb22011-04-29 00:03:10 +00001952
Eric Christopherf9764fa2010-09-30 20:49:44 +00001953 // Add implicit physical register uses to the call.
1954 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1955 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001956
Eric Christopherf9764fa2010-09-30 20:49:44 +00001957 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001958 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001959 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001960
Eric Christopherf9764fa2010-09-30 20:49:44 +00001961 // Set all unused physreg defs as dead.
1962 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001963
Eric Christopherf9764fa2010-09-30 20:49:44 +00001964 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001965
Eric Christopherf9764fa2010-09-30 20:49:44 +00001966}
1967
Chad Rosier0d7b2312011-11-02 00:18:48 +00001968bool ARMFastISel::SelectTrunc(const Instruction *I) {
1969 // The high bits for a type smaller than the register size are assumed to be
1970 // undefined.
1971 Value *Op = I->getOperand(0);
1972
1973 EVT SrcVT, DestVT;
1974 SrcVT = TLI.getValueType(Op->getType(), true);
1975 DestVT = TLI.getValueType(I->getType(), true);
1976
1977 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1978 return false;
1979 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1980 return false;
1981
1982 unsigned SrcReg = getRegForValue(Op);
1983 if (!SrcReg) return false;
1984
1985 // Because the high bits are undefined, a truncate doesn't generate
1986 // any code.
1987 UpdateValueMap(I, SrcReg);
1988 return true;
1989}
1990
Chad Rosier87633022011-11-02 17:20:24 +00001991unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
1992 bool isZExt) {
Eli Friedman76927d732011-05-25 23:49:02 +00001993 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier87633022011-11-02 17:20:24 +00001994 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00001995
1996 unsigned Opc;
Eli Friedman76927d732011-05-25 23:49:02 +00001997 bool isBoolZext = false;
Chad Rosier87633022011-11-02 17:20:24 +00001998 if (!SrcVT.isSimple()) return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00001999 switch (SrcVT.getSimpleVT().SimpleTy) {
Chad Rosier87633022011-11-02 17:20:24 +00002000 default: return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002001 case MVT::i16:
Chad Rosier87633022011-11-02 17:20:24 +00002002 if (!Subtarget->hasV6Ops()) return 0;
2003 if (isZExt)
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002004 Opc = isThumb ? ARM::t2UXTH : ARM::UXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002005 else
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002006 Opc = isThumb ? ARM::t2SXTH : ARM::SXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002007 break;
2008 case MVT::i8:
Chad Rosier87633022011-11-02 17:20:24 +00002009 if (!Subtarget->hasV6Ops()) return 0;
2010 if (isZExt)
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002011 Opc = isThumb ? ARM::t2UXTB : ARM::UXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002012 else
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002013 Opc = isThumb ? ARM::t2SXTB : ARM::SXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002014 break;
2015 case MVT::i1:
Chad Rosier87633022011-11-02 17:20:24 +00002016 if (isZExt) {
Eli Friedman76927d732011-05-25 23:49:02 +00002017 Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
2018 isBoolZext = true;
2019 break;
2020 }
Chad Rosier87633022011-11-02 17:20:24 +00002021 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002022 }
2023
Chad Rosier87633022011-11-02 17:20:24 +00002024 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eli Friedman76927d732011-05-25 23:49:02 +00002025 MachineInstrBuilder MIB;
Chad Rosier87633022011-11-02 17:20:24 +00002026 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
Eli Friedman76927d732011-05-25 23:49:02 +00002027 .addReg(SrcReg);
2028 if (isBoolZext)
2029 MIB.addImm(1);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002030 else
2031 MIB.addImm(0);
Eli Friedman76927d732011-05-25 23:49:02 +00002032 AddOptionalDefs(MIB);
Chad Rosier87633022011-11-02 17:20:24 +00002033 return ResultReg;
2034}
2035
2036bool ARMFastISel::SelectIntExt(const Instruction *I) {
2037 // On ARM, in general, integer casts don't involve legal types; this code
2038 // handles promotable integers.
2039 // FIXME: We could save an instruction in many cases by special-casing
2040 // load instructions.
2041 Type *DestTy = I->getType();
2042 Value *Src = I->getOperand(0);
2043 Type *SrcTy = Src->getType();
2044
2045 EVT SrcVT, DestVT;
2046 SrcVT = TLI.getValueType(SrcTy, true);
2047 DestVT = TLI.getValueType(DestTy, true);
2048
2049 bool isZExt = isa<ZExtInst>(I);
2050 unsigned SrcReg = getRegForValue(Src);
2051 if (!SrcReg) return false;
2052
2053 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2054 if (ResultReg == 0) return false;
2055 UpdateValueMap(I, ResultReg);
Eli Friedman76927d732011-05-25 23:49:02 +00002056 return true;
2057}
2058
Eric Christopher56d2b722010-09-02 23:43:26 +00002059// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00002060bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00002061
Eric Christopherab695882010-07-21 22:26:11 +00002062 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00002063 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00002064 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00002065 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00002066 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00002067 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00002068 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00002069 case Instruction::ICmp:
2070 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00002071 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00002072 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00002073 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00002074 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00002075 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002076 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00002077 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002078 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00002079 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00002080 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00002081 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00002082 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00002083 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00002084 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00002085 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002086 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00002087 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00002088 case Instruction::SRem:
2089 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002090 case Instruction::Call:
2091 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00002092 case Instruction::Select:
2093 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00002094 case Instruction::Ret:
2095 return SelectRet(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002096 case Instruction::Trunc:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002097 return SelectTrunc(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002098 case Instruction::ZExt:
2099 case Instruction::SExt:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002100 return SelectIntExt(I);
Eric Christopherab695882010-07-21 22:26:11 +00002101 default: break;
2102 }
2103 return false;
2104}
2105
2106namespace llvm {
2107 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00002108 // Completely untested on non-darwin.
2109 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002110
Eric Christopheraaa8df42010-11-02 01:21:28 +00002111 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00002112 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002113 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00002114 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00002115 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00002116 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00002117 }
2118}