Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1 | //===-- 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 Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 17 | #include "ARMBaseInstrInfo.h" |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 18 | #include "ARMCallingConv.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 19 | #include "ARMRegisterInfo.h" |
| 20 | #include "ARMTargetMachine.h" |
| 21 | #include "ARMSubtarget.h" |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 22 | #include "ARMConstantPoolValue.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 23 | #include "llvm/CallingConv.h" |
| 24 | #include "llvm/DerivedTypes.h" |
| 25 | #include "llvm/GlobalVariable.h" |
| 26 | #include "llvm/Instructions.h" |
| 27 | #include "llvm/IntrinsicInst.h" |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 28 | #include "llvm/Module.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/Analysis.h" |
| 30 | #include "llvm/CodeGen/FastISel.h" |
| 31 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 33 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 35 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eric Christopher | d56d61a | 2010-10-17 01:51:42 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineMemOperand.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eric Christopher | d56d61a | 2010-10-17 01:51:42 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CallSite.h" |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ErrorHandling.h" |
| 42 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
| 44 | #include "llvm/Target/TargetInstrInfo.h" |
| 45 | #include "llvm/Target/TargetLowering.h" |
| 46 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetOptions.h" |
| 48 | using namespace llvm; |
| 49 | |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 50 | static cl::opt<bool> |
Eric Christopher | 6e5367d | 2010-10-18 22:53:53 +0000 | [diff] [blame] | 51 | DisableARMFastISel("disable-arm-fast-isel", |
| 52 | cl::desc("Turn off experimental ARM fast-isel support"), |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 53 | cl::init(false), cl::Hidden); |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 54 | |
Eric Christopher | 836c624 | 2010-12-15 23:47:29 +0000 | [diff] [blame] | 55 | extern cl::opt<bool> EnableARMLongCalls; |
| 56 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 57 | namespace { |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 58 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 59 | // All possible address modes, plus some. |
| 60 | typedef struct Address { |
| 61 | enum { |
| 62 | RegBase, |
| 63 | FrameIndexBase |
| 64 | } BaseType; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 65 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 66 | union { |
| 67 | unsigned Reg; |
| 68 | int FI; |
| 69 | } Base; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 70 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 71 | int Offset; |
| 72 | unsigned Scale; |
| 73 | unsigned PlusReg; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 74 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 75 | // Innocuous defaults for our address. |
| 76 | Address() |
| 77 | : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) { |
| 78 | Base.Reg = 0; |
| 79 | } |
| 80 | } Address; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 81 | |
| 82 | class 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 Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 87 | const TargetMachine &TM; |
| 88 | const TargetInstrInfo &TII; |
| 89 | const TargetLowering &TLI; |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 90 | ARMFunctionInfo *AFI; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 91 | |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 92 | // Convenience variables to avoid some queries. |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 93 | bool isThumb; |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 94 | LLVMContext *Context; |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 95 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 96 | public: |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 97 | explicit ARMFastISel(FunctionLoweringInfo &funcInfo) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 98 | : FastISel(funcInfo), |
| 99 | TM(funcInfo.MF->getTarget()), |
| 100 | TII(*TM.getInstrInfo()), |
| 101 | TLI(*TM.getTargetLowering()) { |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 102 | Subtarget = &TM.getSubtarget<ARMSubtarget>(); |
Eric Christopher | 7fe55b7 | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 103 | AFI = funcInfo.MF->getInfo<ARMFunctionInfo>(); |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 104 | isThumb = AFI->isThumbFunction(); |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 105 | Context = &funcInfo.Fn->getContext(); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Eric Christopher | cb59229 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 108 | // Code from FastISel.cpp. |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 109 | 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); |
| 118 | virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode, |
| 119 | const TargetRegisterClass *RC, |
| 120 | unsigned Op0, bool Op0IsKill, |
| 121 | uint64_t Imm); |
| 122 | virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode, |
| 123 | const TargetRegisterClass *RC, |
| 124 | unsigned Op0, bool Op0IsKill, |
| 125 | const ConstantFP *FPImm); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 126 | virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode, |
| 127 | const TargetRegisterClass *RC, |
| 128 | unsigned Op0, bool Op0IsKill, |
| 129 | unsigned Op1, bool Op1IsKill, |
| 130 | uint64_t Imm); |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 131 | virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode, |
| 132 | const TargetRegisterClass *RC, |
| 133 | uint64_t Imm); |
| 134 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 135 | virtual unsigned FastEmitInst_extractsubreg(MVT RetVT, |
| 136 | unsigned Op0, bool Op0IsKill, |
| 137 | uint32_t Idx); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 138 | |
Eric Christopher | cb59229 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 139 | // Backend specific FastISel code. |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 140 | virtual bool TargetSelectInstruction(const Instruction *I); |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 141 | virtual unsigned TargetMaterializeConstant(const Constant *C); |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 142 | virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 143 | |
| 144 | #include "ARMGenFastISel.inc" |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 145 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 146 | // Instruction selection routines. |
Eric Christopher | 44bff90 | 2010-09-10 23:10:30 +0000 | [diff] [blame] | 147 | private: |
Eric Christopher | 1778772 | 2010-10-21 21:47:51 +0000 | [diff] [blame] | 148 | bool SelectLoad(const Instruction *I); |
| 149 | bool SelectStore(const Instruction *I); |
| 150 | bool SelectBranch(const Instruction *I); |
| 151 | bool SelectCmp(const Instruction *I); |
| 152 | bool SelectFPExt(const Instruction *I); |
| 153 | bool SelectFPTrunc(const Instruction *I); |
| 154 | bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode); |
| 155 | bool SelectSIToFP(const Instruction *I); |
| 156 | bool SelectFPToSI(const Instruction *I); |
| 157 | bool SelectSDiv(const Instruction *I); |
| 158 | bool SelectSRem(const Instruction *I); |
| 159 | bool SelectCall(const Instruction *I); |
| 160 | bool SelectSelect(const Instruction *I); |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 161 | bool SelectRet(const Instruction *I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 162 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 163 | // Utility routines. |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 164 | private: |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 165 | bool isTypeLegal(const Type *Ty, MVT &VT); |
| 166 | bool isLoadTypeLegal(const Type *Ty, MVT &VT); |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 167 | bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr); |
| 168 | bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr); |
| 169 | bool ARMComputeAddress(const Value *Obj, Address &Addr); |
| 170 | void ARMSimplifyAddress(Address &Addr, EVT VT); |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 171 | unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT); |
Eric Christopher | 744c7c8 | 2010-09-28 22:47:54 +0000 | [diff] [blame] | 172 | unsigned ARMMaterializeInt(const Constant *C, EVT VT); |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 173 | unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT); |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 174 | unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 175 | unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg); |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 176 | unsigned ARMSelectCallOp(const GlobalValue *GV); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 177 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 178 | // Call handling routines. |
| 179 | private: |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 180 | bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT, |
| 181 | unsigned &ResultReg); |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 182 | CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 183 | bool ProcessCallArgs(SmallVectorImpl<Value*> &Args, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 184 | SmallVectorImpl<unsigned> &ArgRegs, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 185 | SmallVectorImpl<MVT> &ArgVTs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 186 | SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, |
| 187 | SmallVectorImpl<unsigned> &RegArgs, |
| 188 | CallingConv::ID CC, |
| 189 | unsigned &NumBytes); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 190 | bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 191 | const Instruction *I, CallingConv::ID CC, |
| 192 | unsigned &NumBytes); |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 193 | bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call); |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 194 | |
| 195 | // OptionalDef handling routines. |
| 196 | private: |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 197 | bool isARMNEONPred(const MachineInstr *MI); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 198 | bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR); |
| 199 | const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB); |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 200 | void AddLoadStoreOperands(EVT VT, Address &Addr, |
| 201 | const MachineInstrBuilder &MIB); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 202 | }; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 203 | |
| 204 | } // end anonymous namespace |
| 205 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 206 | #include "ARMGenCallingConv.inc" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 207 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 208 | // DefinesOptionalPredicate - This is different from DefinesPredicate in that |
| 209 | // we don't care about implicit defs here, just places we'll need to add a |
| 210 | // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR. |
| 211 | bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) { |
| 212 | const TargetInstrDesc &TID = MI->getDesc(); |
| 213 | if (!TID.hasOptionalDef()) |
| 214 | return false; |
| 215 | |
| 216 | // Look to see if our OptionalDef is defining CPSR or CCR. |
| 217 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 218 | const MachineOperand &MO = MI->getOperand(i); |
Eric Christopher | f762fbe | 2010-08-20 00:36:24 +0000 | [diff] [blame] | 219 | if (!MO.isReg() || !MO.isDef()) continue; |
| 220 | if (MO.getReg() == ARM::CPSR) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 221 | *CPSR = true; |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 226 | bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) { |
| 227 | const TargetInstrDesc &TID = MI->getDesc(); |
| 228 | |
| 229 | // If we're a thumb2 or not NEON function we were handled via isPredicable. |
| 230 | if ((TID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON || |
| 231 | AFI->isThumb2Function()) |
| 232 | return false; |
| 233 | |
| 234 | for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) |
| 235 | if (TID.OpInfo[i].isPredicate()) |
| 236 | return true; |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 241 | // If the machine is predicable go ahead and add the predicate operands, if |
| 242 | // it needs default CC operands add those. |
Eric Christopher | aaa8df4 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 243 | // TODO: If we want to support thumb1 then we'll need to deal with optional |
| 244 | // CPSR defs that need to be added before the remaining operands. See s_cc_out |
| 245 | // for descriptions why. |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 246 | const MachineInstrBuilder & |
| 247 | ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) { |
| 248 | MachineInstr *MI = &*MIB; |
| 249 | |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 250 | // Do we use a predicate? or... |
| 251 | // Are we NEON in ARM mode and have a predicate operand? If so, I know |
| 252 | // we're not predicable but add it anyways. |
| 253 | if (TII.isPredicable(MI) || isARMNEONPred(MI)) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 254 | AddDefaultPred(MIB); |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 255 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 256 | // Do we optionally set a predicate? Preds is size > 0 iff the predicate |
| 257 | // defines CPSR. All other OptionalDefines in ARM are the CCR register. |
Eric Christopher | 979e0a1 | 2010-08-19 15:35:27 +0000 | [diff] [blame] | 258 | bool CPSR = false; |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 259 | if (DefinesOptionalPredicate(MI, &CPSR)) { |
| 260 | if (CPSR) |
| 261 | AddDefaultT1CC(MIB); |
| 262 | else |
| 263 | AddDefaultCC(MIB); |
| 264 | } |
| 265 | return MIB; |
| 266 | } |
| 267 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 268 | unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode, |
| 269 | const TargetRegisterClass* RC) { |
| 270 | unsigned ResultReg = createResultReg(RC); |
| 271 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 272 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 273 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 274 | return ResultReg; |
| 275 | } |
| 276 | |
| 277 | unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 278 | const TargetRegisterClass *RC, |
| 279 | unsigned Op0, bool Op0IsKill) { |
| 280 | unsigned ResultReg = createResultReg(RC); |
| 281 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 282 | |
| 283 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 284 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 285 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
| 286 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 287 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 288 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 289 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 290 | TII.get(TargetOpcode::COPY), ResultReg) |
| 291 | .addReg(II.ImplicitDefs[0])); |
| 292 | } |
| 293 | return ResultReg; |
| 294 | } |
| 295 | |
| 296 | unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 297 | const TargetRegisterClass *RC, |
| 298 | unsigned Op0, bool Op0IsKill, |
| 299 | unsigned Op1, bool Op1IsKill) { |
| 300 | unsigned ResultReg = createResultReg(RC); |
| 301 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 302 | |
| 303 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 304 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 305 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 306 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
| 307 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 308 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 309 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 310 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 311 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 312 | TII.get(TargetOpcode::COPY), ResultReg) |
| 313 | .addReg(II.ImplicitDefs[0])); |
| 314 | } |
| 315 | return ResultReg; |
| 316 | } |
| 317 | |
| 318 | unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 319 | const TargetRegisterClass *RC, |
| 320 | unsigned Op0, bool Op0IsKill, |
| 321 | uint64_t Imm) { |
| 322 | unsigned ResultReg = createResultReg(RC); |
| 323 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 324 | |
| 325 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 326 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 327 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 328 | .addImm(Imm)); |
| 329 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 330 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 331 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 332 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 333 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 334 | TII.get(TargetOpcode::COPY), ResultReg) |
| 335 | .addReg(II.ImplicitDefs[0])); |
| 336 | } |
| 337 | return ResultReg; |
| 338 | } |
| 339 | |
| 340 | unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 341 | const TargetRegisterClass *RC, |
| 342 | unsigned Op0, bool Op0IsKill, |
| 343 | const ConstantFP *FPImm) { |
| 344 | unsigned ResultReg = createResultReg(RC); |
| 345 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 346 | |
| 347 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 348 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 349 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 350 | .addFPImm(FPImm)); |
| 351 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 352 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 353 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 354 | .addFPImm(FPImm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 355 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 356 | TII.get(TargetOpcode::COPY), ResultReg) |
| 357 | .addReg(II.ImplicitDefs[0])); |
| 358 | } |
| 359 | return ResultReg; |
| 360 | } |
| 361 | |
| 362 | unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 363 | const TargetRegisterClass *RC, |
| 364 | unsigned Op0, bool Op0IsKill, |
| 365 | unsigned Op1, bool Op1IsKill, |
| 366 | uint64_t Imm) { |
| 367 | unsigned ResultReg = createResultReg(RC); |
| 368 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 369 | |
| 370 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 371 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 372 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 373 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 374 | .addImm(Imm)); |
| 375 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 376 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 377 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 378 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 379 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 380 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 381 | TII.get(TargetOpcode::COPY), ResultReg) |
| 382 | .addReg(II.ImplicitDefs[0])); |
| 383 | } |
| 384 | return ResultReg; |
| 385 | } |
| 386 | |
| 387 | unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 388 | const TargetRegisterClass *RC, |
| 389 | uint64_t Imm) { |
| 390 | unsigned ResultReg = createResultReg(RC); |
| 391 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 392 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 393 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 394 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 395 | .addImm(Imm)); |
| 396 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 397 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 398 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 399 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 400 | TII.get(TargetOpcode::COPY), ResultReg) |
| 401 | .addReg(II.ImplicitDefs[0])); |
| 402 | } |
| 403 | return ResultReg; |
| 404 | } |
| 405 | |
| 406 | unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT, |
| 407 | unsigned Op0, bool Op0IsKill, |
| 408 | uint32_t Idx) { |
| 409 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
| 410 | assert(TargetRegisterInfo::isVirtualRegister(Op0) && |
| 411 | "Cannot yet extract from physregs"); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 412 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 413 | DL, TII.get(TargetOpcode::COPY), ResultReg) |
| 414 | .addReg(Op0, getKillRegState(Op0IsKill), Idx)); |
| 415 | return ResultReg; |
| 416 | } |
| 417 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 418 | // TODO: Don't worry about 64-bit now, but when this is fixed remove the |
| 419 | // checks from the various callers. |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 420 | unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) { |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 421 | if (VT == MVT::f64) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 422 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 423 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); |
| 424 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 425 | TII.get(ARM::VMOVRS), MoveReg) |
| 426 | .addReg(SrcReg)); |
| 427 | return MoveReg; |
| 428 | } |
| 429 | |
| 430 | unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) { |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 431 | if (VT == MVT::i64) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 432 | |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 433 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); |
| 434 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 435 | TII.get(ARM::VMOVSR), MoveReg) |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 436 | .addReg(SrcReg)); |
| 437 | return MoveReg; |
| 438 | } |
| 439 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 440 | // For double width floating point we need to materialize two constants |
| 441 | // (the high and the low) into integer registers then use a move to get |
| 442 | // the combined constant into an FP reg. |
| 443 | unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) { |
| 444 | const APFloat Val = CFP->getValueAPF(); |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 445 | bool is64bit = VT == MVT::f64; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 446 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 447 | // This checks to see if we can use VFP3 instructions to materialize |
| 448 | // a constant, otherwise we have to go through the constant pool. |
| 449 | if (TLI.isFPImmLegal(Val, VT)) { |
| 450 | unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS; |
| 451 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 452 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 453 | DestReg) |
| 454 | .addFPImm(CFP)); |
| 455 | return DestReg; |
| 456 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 457 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 458 | // Require VFP2 for loading fp constants. |
Eric Christopher | 238bb16 | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 459 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 460 | |
Eric Christopher | 238bb16 | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 461 | // MachineConstantPool wants an explicit alignment. |
| 462 | unsigned Align = TD.getPrefTypeAlignment(CFP->getType()); |
| 463 | if (Align == 0) { |
| 464 | // TODO: Figure out if this is correct. |
| 465 | Align = TD.getTypeAllocSize(CFP->getType()); |
| 466 | } |
| 467 | unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align); |
| 468 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 469 | unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 470 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 471 | // The extra reg is for addrmode5. |
Eric Christopher | f5732c4 | 2010-09-28 00:35:09 +0000 | [diff] [blame] | 472 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 473 | DestReg) |
| 474 | .addConstantPoolIndex(Idx) |
Eric Christopher | 238bb16 | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 475 | .addReg(0)); |
| 476 | return DestReg; |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Eric Christopher | 744c7c8 | 2010-09-28 22:47:54 +0000 | [diff] [blame] | 479 | unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) { |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 480 | |
Eric Christopher | 744c7c8 | 2010-09-28 22:47:54 +0000 | [diff] [blame] | 481 | // For now 32-bit only. |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 482 | if (VT != MVT::i32) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 483 | |
Eric Christopher | e5b13cf | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 484 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 485 | |
| 486 | // If we can do this in a single instruction without a constant pool entry |
| 487 | // do so now. |
| 488 | const ConstantInt *CI = cast<ConstantInt>(C); |
Eric Christopher | 5e262bc | 2010-11-06 07:53:11 +0000 | [diff] [blame] | 489 | if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) { |
Eric Christopher | e5b13cf | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 490 | unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16; |
| 491 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Jim Grosbach | 3ea4daa | 2010-11-19 18:01:37 +0000 | [diff] [blame] | 492 | TII.get(Opc), DestReg) |
| 493 | .addImm(CI->getSExtValue())); |
Eric Christopher | e5b13cf | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 494 | return DestReg; |
| 495 | } |
| 496 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 497 | // MachineConstantPool wants an explicit alignment. |
| 498 | unsigned Align = TD.getPrefTypeAlignment(C->getType()); |
| 499 | if (Align == 0) { |
| 500 | // TODO: Figure out if this is correct. |
| 501 | Align = TD.getTypeAllocSize(C->getType()); |
| 502 | } |
| 503 | unsigned Idx = MCP.getConstantPoolIndex(C, Align); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 504 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 505 | if (isThumb) |
| 506 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | fd60980 | 2010-09-28 21:55:34 +0000 | [diff] [blame] | 507 | TII.get(ARM::t2LDRpci), DestReg) |
| 508 | .addConstantPoolIndex(Idx)); |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 509 | else |
Eric Christopher | d0c82a6 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 510 | // The extra immediate is for addrmode2. |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 511 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | fd60980 | 2010-09-28 21:55:34 +0000 | [diff] [blame] | 512 | TII.get(ARM::LDRcp), DestReg) |
| 513 | .addConstantPoolIndex(Idx) |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 514 | .addImm(0)); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 515 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 516 | return DestReg; |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 519 | unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) { |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 520 | // For now 32-bit only. |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 521 | if (VT != MVT::i32) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 522 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 523 | Reloc::Model RelocM = TM.getRelocationModel(); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 524 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 525 | // TODO: No external globals for now. |
| 526 | if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 527 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 528 | // TODO: Need more magic for ARM PIC. |
| 529 | if (!isThumb && (RelocM == Reloc::PIC_)) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 530 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 531 | // MachineConstantPool wants an explicit alignment. |
| 532 | unsigned Align = TD.getPrefTypeAlignment(GV->getType()); |
| 533 | if (Align == 0) { |
| 534 | // TODO: Figure out if this is correct. |
| 535 | Align = TD.getTypeAllocSize(GV->getType()); |
| 536 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 537 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 538 | // Grab index. |
| 539 | unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8); |
Evan Cheng | 5de5d4b | 2011-01-17 08:03:18 +0000 | [diff] [blame] | 540 | unsigned Id = AFI->createPICLabelUId(); |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 541 | ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id, |
| 542 | ARMCP::CPValue, PCAdj); |
| 543 | unsigned Idx = MCP.getConstantPoolIndex(CPV, Align); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 544 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 545 | // Load value. |
| 546 | MachineInstrBuilder MIB; |
| 547 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 548 | if (isThumb) { |
| 549 | unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic; |
| 550 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg) |
| 551 | .addConstantPoolIndex(Idx); |
| 552 | if (RelocM == Reloc::PIC_) |
| 553 | MIB.addImm(Id); |
| 554 | } else { |
Eric Christopher | d0c82a6 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 555 | // The extra immediate is for addrmode2. |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 556 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp), |
| 557 | DestReg) |
| 558 | .addConstantPoolIndex(Idx) |
Eric Christopher | d0c82a6 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 559 | .addImm(0); |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 560 | } |
| 561 | AddOptionalDefs(MIB); |
| 562 | return DestReg; |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 565 | unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) { |
| 566 | EVT VT = TLI.getValueType(C->getType(), true); |
| 567 | |
| 568 | // Only handle simple types. |
| 569 | if (!VT.isSimple()) return 0; |
| 570 | |
| 571 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
| 572 | return ARMMaterializeFP(CFP, VT); |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 573 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 574 | return ARMMaterializeGV(GV, VT); |
| 575 | else if (isa<ConstantInt>(C)) |
| 576 | return ARMMaterializeInt(C, VT); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 577 | |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 578 | return 0; |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 581 | unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) { |
| 582 | // Don't handle dynamic allocas. |
| 583 | if (!FuncInfo.StaticAllocaMap.count(AI)) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 584 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 585 | MVT VT; |
Eric Christopher | ec8bf97 | 2010-10-17 06:07:26 +0000 | [diff] [blame] | 586 | if (!isLoadTypeLegal(AI->getType(), VT)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 587 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 588 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 589 | FuncInfo.StaticAllocaMap.find(AI); |
| 590 | |
| 591 | // This will get lowered later into the correct offsets and registers |
| 592 | // via rewriteXFrameIndex. |
| 593 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 594 | TargetRegisterClass* RC = TLI.getRegClassFor(VT); |
| 595 | unsigned ResultReg = createResultReg(RC); |
| 596 | unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; |
| 597 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL, |
| 598 | TII.get(Opc), ResultReg) |
| 599 | .addFrameIndex(SI->second) |
| 600 | .addImm(0)); |
| 601 | return ResultReg; |
| 602 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 603 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 607 | bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) { |
| 608 | EVT evt = TLI.getValueType(Ty, true); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 609 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 610 | // Only handle simple types. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 611 | if (evt == MVT::Other || !evt.isSimple()) return false; |
| 612 | VT = evt.getSimpleVT(); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 613 | |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 614 | // Handle all legal types, i.e. a register that will directly hold this |
| 615 | // value. |
| 616 | return TLI.isTypeLegal(VT); |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 619 | bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) { |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 620 | if (isTypeLegal(Ty, VT)) return true; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 621 | |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 622 | // If this is a type than can be sign or zero-extended to a basic operation |
| 623 | // go ahead and accept it now. |
| 624 | if (VT == MVT::i8 || VT == MVT::i16) |
| 625 | return true; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 626 | |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 627 | return false; |
| 628 | } |
| 629 | |
Eric Christopher | 88de86b | 2010-11-19 22:36:41 +0000 | [diff] [blame] | 630 | // Computes the address to get to an object. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 631 | bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 632 | // Some boilerplate from the X86 FastISel. |
| 633 | const User *U = NULL; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 634 | unsigned Opcode = Instruction::UserOp1; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 635 | if (const Instruction *I = dyn_cast<Instruction>(Obj)) { |
Eric Christopher | 2d630d7 | 2010-11-19 22:37:58 +0000 | [diff] [blame] | 636 | // Don't walk into other basic blocks unless the object is an alloca from |
| 637 | // another block, otherwise it may not have a virtual register assigned. |
Eric Christopher | 76dda7e | 2010-11-15 21:11:06 +0000 | [diff] [blame] | 638 | if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) || |
| 639 | FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { |
| 640 | Opcode = I->getOpcode(); |
| 641 | U = I; |
| 642 | } |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 643 | } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 644 | Opcode = C->getOpcode(); |
| 645 | U = C; |
| 646 | } |
| 647 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 648 | if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 649 | if (Ty->getAddressSpace() > 255) |
| 650 | // Fast instruction selection doesn't support the special |
| 651 | // address spaces. |
| 652 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 653 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 654 | switch (Opcode) { |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 655 | default: |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 656 | break; |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 657 | case Instruction::BitCast: { |
| 658 | // Look through bitcasts. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 659 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 660 | } |
| 661 | case Instruction::IntToPtr: { |
| 662 | // Look past no-op inttoptrs. |
| 663 | if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 664 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 665 | break; |
| 666 | } |
| 667 | case Instruction::PtrToInt: { |
| 668 | // Look past no-op ptrtoints. |
| 669 | if (TLI.getValueType(U->getType()) == TLI.getPointerTy()) |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 670 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 671 | break; |
| 672 | } |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 673 | case Instruction::GetElementPtr: { |
Eric Christopher | b371658 | 2010-11-19 22:39:56 +0000 | [diff] [blame] | 674 | Address SavedAddr = Addr; |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 675 | int TmpOffset = Addr.Offset; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 676 | |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 677 | // Iterate through the GEP folding the constants into offsets where |
| 678 | // we can. |
| 679 | gep_type_iterator GTI = gep_type_begin(U); |
| 680 | for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); |
| 681 | i != e; ++i, ++GTI) { |
| 682 | const Value *Op = *i; |
| 683 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 684 | const StructLayout *SL = TD.getStructLayout(STy); |
| 685 | unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); |
| 686 | TmpOffset += SL->getElementOffset(Idx); |
| 687 | } else { |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 688 | uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType()); |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 689 | for (;;) { |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 690 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { |
| 691 | // Constant-offset addressing. |
| 692 | TmpOffset += CI->getSExtValue() * S; |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 693 | break; |
| 694 | } |
| 695 | if (isa<AddOperator>(Op) && |
| 696 | (!isa<Instruction>(Op) || |
| 697 | FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()] |
| 698 | == FuncInfo.MBB) && |
| 699 | isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) { |
| 700 | // An add (in the same block) with a constant operand. Fold the |
| 701 | // constant. |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 702 | ConstantInt *CI = |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 703 | cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 704 | TmpOffset += CI->getSExtValue() * S; |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 705 | // Iterate on the other operand. |
| 706 | Op = cast<AddOperator>(Op)->getOperand(0); |
| 707 | continue; |
| 708 | } |
| 709 | // Unsupported |
| 710 | goto unsupported_gep; |
| 711 | } |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 714 | |
| 715 | // Try to grab the base operand now. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 716 | Addr.Offset = TmpOffset; |
| 717 | if (ARMComputeAddress(U->getOperand(0), Addr)) return true; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 718 | |
| 719 | // We failed, restore everything and try the other options. |
Eric Christopher | b371658 | 2010-11-19 22:39:56 +0000 | [diff] [blame] | 720 | Addr = SavedAddr; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 721 | |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 722 | unsupported_gep: |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 723 | break; |
| 724 | } |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 725 | case Instruction::Alloca: { |
Eric Christopher | 1541877 | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 726 | const AllocaInst *AI = cast<AllocaInst>(Obj); |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 727 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 728 | FuncInfo.StaticAllocaMap.find(AI); |
| 729 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 730 | Addr.BaseType = Address::FrameIndexBase; |
| 731 | Addr.Base.FI = SI->second; |
| 732 | return true; |
| 733 | } |
| 734 | break; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 737 | |
Eric Christopher | a9c5751 | 2010-10-13 21:41:51 +0000 | [diff] [blame] | 738 | // Materialize the global variable's address into a reg which can |
| 739 | // then be used later to load the variable. |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 740 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) { |
Eric Christopher | ede42b0 | 2010-10-13 09:11:46 +0000 | [diff] [blame] | 741 | unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType())); |
| 742 | if (Tmp == 0) return false; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 743 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 744 | Addr.Base.Reg = Tmp; |
Eric Christopher | ede42b0 | 2010-10-13 09:11:46 +0000 | [diff] [blame] | 745 | return true; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 746 | } |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 747 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 748 | // Try to get this in a register if nothing else has worked. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 749 | if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj); |
| 750 | return Addr.Base.Reg != 0; |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 753 | void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) { |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 754 | |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 755 | assert(VT.isSimple() && "Non-simple types are invalid here!"); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 756 | |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 757 | bool needsLowering = false; |
| 758 | switch (VT.getSimpleVT().SimpleTy) { |
| 759 | default: |
| 760 | assert(false && "Unhandled load/store type!"); |
| 761 | case MVT::i1: |
| 762 | case MVT::i8: |
| 763 | case MVT::i16: |
| 764 | case MVT::i32: |
| 765 | // Integer loads/stores handle 12-bit offsets. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 766 | needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset); |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 767 | break; |
| 768 | case MVT::f32: |
| 769 | case MVT::f64: |
| 770 | // Floating point operands handle 8-bit offsets. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 771 | needsLowering = ((Addr.Offset & 0xff) != Addr.Offset); |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 772 | break; |
| 773 | } |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 774 | |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 775 | // If this is a stack pointer and the offset needs to be simplified then |
| 776 | // put the alloca address into a register, set the base type back to |
| 777 | // register and continue. This should almost never happen. |
| 778 | if (needsLowering && Addr.BaseType == Address::FrameIndexBase) { |
| 779 | TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass : |
| 780 | ARM::GPRRegisterClass; |
| 781 | unsigned ResultReg = createResultReg(RC); |
| 782 | unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; |
| 783 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL, |
| 784 | TII.get(Opc), ResultReg) |
| 785 | .addFrameIndex(Addr.Base.FI) |
| 786 | .addImm(0)); |
| 787 | Addr.Base.Reg = ResultReg; |
| 788 | Addr.BaseType = Address::RegBase; |
| 789 | } |
| 790 | |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 791 | // Since the offset is too large for the load/store instruction |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 792 | // get the reg+offset into a register. |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 793 | if (needsLowering) { |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 794 | ARMCC::CondCodes Pred = ARMCC::AL; |
| 795 | unsigned PredReg = 0; |
| 796 | |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 797 | TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass : |
| 798 | ARM::GPRRegisterClass; |
| 799 | unsigned BaseReg = createResultReg(RC); |
| 800 | |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 801 | if (!isThumb) |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 802 | emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 803 | BaseReg, Addr.Base.Reg, Addr.Offset, |
| 804 | Pred, PredReg, |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 805 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 806 | else { |
| 807 | assert(AFI->isThumb2Function()); |
| 808 | emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 809 | BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg, |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 810 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 811 | } |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 812 | Addr.Offset = 0; |
| 813 | Addr.Base.Reg = BaseReg; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 814 | } |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 817 | void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr, |
| 818 | const MachineInstrBuilder &MIB) { |
| 819 | // addrmode5 output depends on the selection dag addressing dividing the |
| 820 | // offset by 4 that it then later multiplies. Do this here as well. |
| 821 | if (VT.getSimpleVT().SimpleTy == MVT::f32 || |
| 822 | VT.getSimpleVT().SimpleTy == MVT::f64) |
| 823 | Addr.Offset /= 4; |
| 824 | |
| 825 | // Frame base works a bit differently. Handle it separately. |
| 826 | if (Addr.BaseType == Address::FrameIndexBase) { |
| 827 | int FI = Addr.Base.FI; |
| 828 | int Offset = Addr.Offset; |
| 829 | MachineMemOperand *MMO = |
| 830 | FuncInfo.MF->getMachineMemOperand( |
| 831 | MachinePointerInfo::getFixedStack(FI, Offset), |
| 832 | MachineMemOperand::MOLoad, |
| 833 | MFI.getObjectSize(FI), |
| 834 | MFI.getObjectAlignment(FI)); |
| 835 | // Now add the rest of the operands. |
| 836 | MIB.addFrameIndex(FI); |
| 837 | |
| 838 | // ARM halfword load/stores need an additional operand. |
| 839 | if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0); |
| 840 | |
| 841 | MIB.addImm(Addr.Offset); |
| 842 | MIB.addMemOperand(MMO); |
| 843 | } else { |
| 844 | // Now add the rest of the operands. |
| 845 | MIB.addReg(Addr.Base.Reg); |
| 846 | |
| 847 | // ARM halfword load/stores need an additional operand. |
| 848 | if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0); |
| 849 | |
| 850 | MIB.addImm(Addr.Offset); |
| 851 | } |
| 852 | AddOptionalDefs(MIB); |
| 853 | } |
| 854 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 855 | bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) { |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 856 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 857 | assert(VT.isSimple() && "Non-simple types are invalid here!"); |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 858 | unsigned Opc; |
Eric Christopher | ee56ea6 | 2010-10-07 05:50:44 +0000 | [diff] [blame] | 859 | TargetRegisterClass *RC; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 860 | switch (VT.getSimpleVT().SimpleTy) { |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 861 | // This is mostly going to be Neon/vector support. |
| 862 | default: return false; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 863 | case MVT::i16: |
Eric Christopher | 45c6071 | 2010-10-17 01:40:27 +0000 | [diff] [blame] | 864 | Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH; |
Eric Christopher | 7a56f33 | 2010-10-08 01:13:17 +0000 | [diff] [blame] | 865 | RC = ARM::GPRRegisterClass; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 866 | break; |
| 867 | case MVT::i8: |
Jim Grosbach | c1d3021 | 2010-10-27 00:19:44 +0000 | [diff] [blame] | 868 | Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12; |
Eric Christopher | 7a56f33 | 2010-10-08 01:13:17 +0000 | [diff] [blame] | 869 | RC = ARM::GPRRegisterClass; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 870 | break; |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 871 | case MVT::i32: |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 872 | Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12; |
Eric Christopher | 7a56f33 | 2010-10-08 01:13:17 +0000 | [diff] [blame] | 873 | RC = ARM::GPRRegisterClass; |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 874 | break; |
Eric Christopher | 6dab137 | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 875 | case MVT::f32: |
| 876 | Opc = ARM::VLDRS; |
Eric Christopher | ee56ea6 | 2010-10-07 05:50:44 +0000 | [diff] [blame] | 877 | RC = TLI.getRegClassFor(VT); |
Eric Christopher | 6dab137 | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 878 | break; |
| 879 | case MVT::f64: |
| 880 | Opc = ARM::VLDRD; |
Eric Christopher | ee56ea6 | 2010-10-07 05:50:44 +0000 | [diff] [blame] | 881 | RC = TLI.getRegClassFor(VT); |
Eric Christopher | 6dab137 | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 882 | break; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 883 | } |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 884 | // Simplify this down to something we can handle. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 885 | ARMSimplifyAddress(Addr, VT); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 886 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 887 | // Create the base instruction, then add the operands. |
| 888 | ResultReg = createResultReg(RC); |
| 889 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 890 | TII.get(Opc), ResultReg); |
| 891 | AddLoadStoreOperands(VT, Addr, MIB); |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 892 | return true; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 895 | bool ARMFastISel::SelectLoad(const Instruction *I) { |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 896 | // Verify we have a legal type before going any further. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 897 | MVT VT; |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 898 | if (!isLoadTypeLegal(I->getType(), VT)) |
| 899 | return false; |
| 900 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 901 | // See if we can handle this address. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 902 | Address Addr; |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 903 | if (!ARMComputeAddress(I->getOperand(0), Addr)) return false; |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 904 | |
| 905 | unsigned ResultReg; |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 906 | if (!ARMEmitLoad(VT, ResultReg, Addr)) return false; |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 907 | UpdateValueMap(I, ResultReg); |
| 908 | return true; |
| 909 | } |
| 910 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 911 | bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) { |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 912 | unsigned StrOpc; |
| 913 | switch (VT.getSimpleVT().SimpleTy) { |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 914 | // This is mostly going to be Neon/vector support. |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 915 | default: return false; |
Eric Christopher | 4c91412 | 2010-11-02 23:59:09 +0000 | [diff] [blame] | 916 | case MVT::i1: { |
| 917 | unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass : |
| 918 | ARM::GPRRegisterClass); |
| 919 | unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri; |
| 920 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 921 | TII.get(Opc), Res) |
| 922 | .addReg(SrcReg).addImm(1)); |
| 923 | SrcReg = Res; |
| 924 | } // Fallthrough here. |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 925 | case MVT::i8: |
Jim Grosbach | 7e3383c | 2010-10-27 23:12:14 +0000 | [diff] [blame] | 926 | StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12; |
Eric Christopher | 1541877 | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 927 | break; |
| 928 | case MVT::i16: |
Eric Christopher | 45c6071 | 2010-10-17 01:40:27 +0000 | [diff] [blame] | 929 | StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH; |
Eric Christopher | 1541877 | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 930 | break; |
Eric Christopher | 47650ec | 2010-10-16 01:10:35 +0000 | [diff] [blame] | 931 | case MVT::i32: |
Jim Grosbach | 7e3383c | 2010-10-27 23:12:14 +0000 | [diff] [blame] | 932 | StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12; |
Eric Christopher | 47650ec | 2010-10-16 01:10:35 +0000 | [diff] [blame] | 933 | break; |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 934 | case MVT::f32: |
| 935 | if (!Subtarget->hasVFP2()) return false; |
| 936 | StrOpc = ARM::VSTRS; |
| 937 | break; |
| 938 | case MVT::f64: |
| 939 | if (!Subtarget->hasVFP2()) return false; |
| 940 | StrOpc = ARM::VSTRD; |
| 941 | break; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 942 | } |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 943 | // Simplify this down to something we can handle. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 944 | ARMSimplifyAddress(Addr, VT); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 945 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 946 | // Create the base instruction, then add the operands. |
| 947 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 948 | TII.get(StrOpc)) |
| 949 | .addReg(SrcReg, getKillRegState(true)); |
| 950 | AddLoadStoreOperands(VT, Addr, MIB); |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 951 | return true; |
| 952 | } |
| 953 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 954 | bool ARMFastISel::SelectStore(const Instruction *I) { |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 955 | Value *Op0 = I->getOperand(0); |
| 956 | unsigned SrcReg = 0; |
| 957 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 958 | // Verify we have a legal type before going any further. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 959 | MVT VT; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 960 | if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 961 | return false; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 962 | |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 963 | // Get the value to be stored into a register. |
| 964 | SrcReg = getRegForValue(Op0); |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 965 | if (SrcReg == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 966 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 967 | // See if we can handle this address. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 968 | Address Addr; |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 969 | if (!ARMComputeAddress(I->getOperand(1), Addr)) |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 970 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 971 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 972 | if (!ARMEmitStore(VT, SrcReg, Addr)) return false; |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
| 976 | static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) { |
| 977 | switch (Pred) { |
| 978 | // Needs two compares... |
| 979 | case CmpInst::FCMP_ONE: |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 980 | case CmpInst::FCMP_UEQ: |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 981 | default: |
Eric Christopher | 4053e63 | 2010-11-02 01:24:49 +0000 | [diff] [blame] | 982 | // AL is our "false" for now. The other two need more compares. |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 983 | return ARMCC::AL; |
| 984 | case CmpInst::ICMP_EQ: |
| 985 | case CmpInst::FCMP_OEQ: |
| 986 | return ARMCC::EQ; |
| 987 | case CmpInst::ICMP_SGT: |
| 988 | case CmpInst::FCMP_OGT: |
| 989 | return ARMCC::GT; |
| 990 | case CmpInst::ICMP_SGE: |
| 991 | case CmpInst::FCMP_OGE: |
| 992 | return ARMCC::GE; |
| 993 | case CmpInst::ICMP_UGT: |
| 994 | case CmpInst::FCMP_UGT: |
| 995 | return ARMCC::HI; |
| 996 | case CmpInst::FCMP_OLT: |
| 997 | return ARMCC::MI; |
| 998 | case CmpInst::ICMP_ULE: |
| 999 | case CmpInst::FCMP_OLE: |
| 1000 | return ARMCC::LS; |
| 1001 | case CmpInst::FCMP_ORD: |
| 1002 | return ARMCC::VC; |
| 1003 | case CmpInst::FCMP_UNO: |
| 1004 | return ARMCC::VS; |
| 1005 | case CmpInst::FCMP_UGE: |
| 1006 | return ARMCC::PL; |
| 1007 | case CmpInst::ICMP_SLT: |
| 1008 | case CmpInst::FCMP_ULT: |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1009 | return ARMCC::LT; |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1010 | case CmpInst::ICMP_SLE: |
| 1011 | case CmpInst::FCMP_ULE: |
| 1012 | return ARMCC::LE; |
| 1013 | case CmpInst::FCMP_UNE: |
| 1014 | case CmpInst::ICMP_NE: |
| 1015 | return ARMCC::NE; |
| 1016 | case CmpInst::ICMP_UGE: |
| 1017 | return ARMCC::HS; |
| 1018 | case CmpInst::ICMP_ULT: |
| 1019 | return ARMCC::LO; |
| 1020 | } |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1023 | bool ARMFastISel::SelectBranch(const Instruction *I) { |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1024 | const BranchInst *BI = cast<BranchInst>(I); |
| 1025 | MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; |
| 1026 | MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1027 | |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1028 | // Simple branch support. |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1029 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1030 | // If we can, avoid recomputing the compare - redoing it could lead to wonky |
| 1031 | // behavior. |
| 1032 | // TODO: Factor this out. |
| 1033 | if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { |
| 1034 | if (CI->hasOneUse() && (CI->getParent() == I->getParent())) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1035 | MVT VT; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1036 | const Type *Ty = CI->getOperand(0)->getType(); |
Eric Christopher | 76d6147 | 2010-10-30 21:25:26 +0000 | [diff] [blame] | 1037 | if (!isTypeLegal(Ty, VT)) |
| 1038 | return false; |
| 1039 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1040 | bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); |
| 1041 | if (isFloat && !Subtarget->hasVFP2()) |
| 1042 | return false; |
| 1043 | |
| 1044 | unsigned CmpOpc; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1045 | switch (VT.SimpleTy) { |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1046 | default: return false; |
| 1047 | // TODO: Verify compares. |
| 1048 | case MVT::f32: |
| 1049 | CmpOpc = ARM::VCMPES; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1050 | break; |
| 1051 | case MVT::f64: |
| 1052 | CmpOpc = ARM::VCMPED; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1053 | break; |
| 1054 | case MVT::i32: |
| 1055 | CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | } |
| 1058 | |
| 1059 | // Get the compare predicate. |
| 1060 | ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate()); |
| 1061 | |
| 1062 | // We may not handle every CC for now. |
| 1063 | if (ARMPred == ARMCC::AL) return false; |
| 1064 | |
| 1065 | unsigned Arg1 = getRegForValue(CI->getOperand(0)); |
| 1066 | if (Arg1 == 0) return false; |
| 1067 | |
| 1068 | unsigned Arg2 = getRegForValue(CI->getOperand(1)); |
| 1069 | if (Arg2 == 0) return false; |
| 1070 | |
| 1071 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1072 | TII.get(CmpOpc)) |
| 1073 | .addReg(Arg1).addReg(Arg2)); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1074 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1075 | // For floating point we need to move the result to a comparison register |
| 1076 | // that we can then use for branches. |
| 1077 | if (isFloat) |
| 1078 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1079 | TII.get(ARM::FMSTAT))); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1080 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1081 | unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc; |
| 1082 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) |
| 1083 | .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR); |
| 1084 | FastEmitBranch(FBB, DL); |
| 1085 | FuncInfo.MBB->addSuccessor(TBB); |
| 1086 | return true; |
| 1087 | } |
| 1088 | } |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1089 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1090 | unsigned CmpReg = getRegForValue(BI->getCondition()); |
| 1091 | if (CmpReg == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1092 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1093 | // Re-set the flags just in case. |
| 1094 | unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri; |
| 1095 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
Eric Christopher | 000cf70 | 2010-11-03 04:29:11 +0000 | [diff] [blame] | 1096 | .addReg(CmpReg).addImm(0)); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1097 | |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1098 | unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc; |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1099 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) |
Eric Christopher | 000cf70 | 2010-11-03 04:29:11 +0000 | [diff] [blame] | 1100 | .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR); |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1101 | FastEmitBranch(FBB, DL); |
| 1102 | FuncInfo.MBB->addSuccessor(TBB); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1103 | return true; |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1106 | bool ARMFastISel::SelectCmp(const Instruction *I) { |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1107 | const CmpInst *CI = cast<CmpInst>(I); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1108 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1109 | MVT VT; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1110 | const Type *Ty = CI->getOperand(0)->getType(); |
| 1111 | if (!isTypeLegal(Ty, VT)) |
| 1112 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1113 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1114 | bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); |
| 1115 | if (isFloat && !Subtarget->hasVFP2()) |
| 1116 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1117 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1118 | unsigned CmpOpc; |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1119 | unsigned CondReg; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1120 | switch (VT.SimpleTy) { |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1121 | default: return false; |
| 1122 | // TODO: Verify compares. |
| 1123 | case MVT::f32: |
| 1124 | CmpOpc = ARM::VCMPES; |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1125 | CondReg = ARM::FPSCR; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1126 | break; |
| 1127 | case MVT::f64: |
| 1128 | CmpOpc = ARM::VCMPED; |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1129 | CondReg = ARM::FPSCR; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1130 | break; |
| 1131 | case MVT::i32: |
| 1132 | CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr; |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1133 | CondReg = ARM::CPSR; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1134 | break; |
| 1135 | } |
| 1136 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1137 | // Get the compare predicate. |
| 1138 | ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate()); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1139 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1140 | // We may not handle every CC for now. |
| 1141 | if (ARMPred == ARMCC::AL) return false; |
| 1142 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1143 | unsigned Arg1 = getRegForValue(CI->getOperand(0)); |
| 1144 | if (Arg1 == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1145 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1146 | unsigned Arg2 = getRegForValue(CI->getOperand(1)); |
| 1147 | if (Arg2 == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1148 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1149 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
| 1150 | .addReg(Arg1).addReg(Arg2)); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1151 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1152 | // For floating point we need to move the result to a comparison register |
| 1153 | // that we can then use for branches. |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1154 | if (isFloat) |
| 1155 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1156 | TII.get(ARM::FMSTAT))); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1157 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1158 | // Now set a register based on the comparison. Explicitly set the predicates |
| 1159 | // here. |
Eric Christopher | 338c253 | 2010-10-07 05:31:49 +0000 | [diff] [blame] | 1160 | unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1161 | TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass |
Eric Christopher | 5d18d92 | 2010-10-07 05:39:19 +0000 | [diff] [blame] | 1162 | : ARM::GPRRegisterClass; |
| 1163 | unsigned DestReg = createResultReg(RC); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1164 | Constant *Zero |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 1165 | = ConstantInt::get(Type::getInt32Ty(*Context), 0); |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1166 | unsigned ZeroReg = TargetMaterializeConstant(Zero); |
| 1167 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg) |
| 1168 | .addReg(ZeroReg).addImm(1) |
| 1169 | .addImm(ARMPred).addReg(CondReg); |
| 1170 | |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1171 | UpdateValueMap(I, DestReg); |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1172 | return true; |
| 1173 | } |
| 1174 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1175 | bool ARMFastISel::SelectFPExt(const Instruction *I) { |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1176 | // Make sure we have VFP and that we're extending float to double. |
| 1177 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1178 | |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1179 | Value *V = I->getOperand(0); |
| 1180 | if (!I->getType()->isDoubleTy() || |
| 1181 | !V->getType()->isFloatTy()) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1182 | |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1183 | unsigned Op = getRegForValue(V); |
| 1184 | if (Op == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1185 | |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1186 | unsigned Result = createResultReg(ARM::DPRRegisterClass); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1187 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | ef2fdd2 | 2010-09-09 20:36:19 +0000 | [diff] [blame] | 1188 | TII.get(ARM::VCVTDS), Result) |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1189 | .addReg(Op)); |
| 1190 | UpdateValueMap(I, Result); |
| 1191 | return true; |
| 1192 | } |
| 1193 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1194 | bool ARMFastISel::SelectFPTrunc(const Instruction *I) { |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1195 | // Make sure we have VFP and that we're truncating double to float. |
| 1196 | if (!Subtarget->hasVFP2()) return false; |
| 1197 | |
| 1198 | Value *V = I->getOperand(0); |
Eric Christopher | 022b7fb | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1199 | if (!(I->getType()->isFloatTy() && |
| 1200 | V->getType()->isDoubleTy())) return false; |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1201 | |
| 1202 | unsigned Op = getRegForValue(V); |
| 1203 | if (Op == 0) return false; |
| 1204 | |
| 1205 | unsigned Result = createResultReg(ARM::SPRRegisterClass); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1206 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | ef2fdd2 | 2010-09-09 20:36:19 +0000 | [diff] [blame] | 1207 | TII.get(ARM::VCVTSD), Result) |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1208 | .addReg(Op)); |
| 1209 | UpdateValueMap(I, Result); |
| 1210 | return true; |
| 1211 | } |
| 1212 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1213 | bool ARMFastISel::SelectSIToFP(const Instruction *I) { |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1214 | // Make sure we have VFP. |
| 1215 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1216 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1217 | MVT DstVT; |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1218 | const Type *Ty = I->getType(); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1219 | if (!isTypeLegal(Ty, DstVT)) |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1220 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1221 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1222 | unsigned Op = getRegForValue(I->getOperand(0)); |
| 1223 | if (Op == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1224 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1225 | // The conversion routine works on fp-reg to fp-reg and the operand above |
| 1226 | // was an integer, move it to the fp registers if possible. |
Eric Christopher | 022b7fb | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1227 | unsigned FP = ARMMoveToFPReg(MVT::f32, Op); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1228 | if (FP == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1229 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1230 | unsigned Opc; |
| 1231 | if (Ty->isFloatTy()) Opc = ARM::VSITOS; |
| 1232 | else if (Ty->isDoubleTy()) Opc = ARM::VSITOD; |
| 1233 | else return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1234 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1235 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT)); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1236 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 1237 | ResultReg) |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1238 | .addReg(FP)); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1239 | UpdateValueMap(I, ResultReg); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1240 | return true; |
| 1241 | } |
| 1242 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1243 | bool ARMFastISel::SelectFPToSI(const Instruction *I) { |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1244 | // Make sure we have VFP. |
| 1245 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1246 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1247 | MVT DstVT; |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1248 | const Type *RetTy = I->getType(); |
Eric Christopher | 920a208 | 2010-09-10 00:35:09 +0000 | [diff] [blame] | 1249 | if (!isTypeLegal(RetTy, DstVT)) |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1250 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1251 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1252 | unsigned Op = getRegForValue(I->getOperand(0)); |
| 1253 | if (Op == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1254 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1255 | unsigned Opc; |
| 1256 | const Type *OpTy = I->getOperand(0)->getType(); |
| 1257 | if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS; |
| 1258 | else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD; |
| 1259 | else return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1260 | |
Eric Christopher | 022b7fb | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1261 | // f64->s32 or f32->s32 both need an intermediate f32 reg. |
| 1262 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32)); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1263 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 1264 | ResultReg) |
| 1265 | .addReg(Op)); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1266 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1267 | // This result needs to be in an integer register, but the conversion only |
| 1268 | // takes place in fp-regs. |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1269 | unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1270 | if (IntReg == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1271 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1272 | UpdateValueMap(I, IntReg); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1273 | return true; |
| 1274 | } |
| 1275 | |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1276 | bool ARMFastISel::SelectSelect(const Instruction *I) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1277 | MVT VT; |
| 1278 | if (!isTypeLegal(I->getType(), VT)) |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1279 | return false; |
| 1280 | |
| 1281 | // Things need to be register sized for register moves. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1282 | if (VT != MVT::i32) return false; |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1283 | const TargetRegisterClass *RC = TLI.getRegClassFor(VT); |
| 1284 | |
| 1285 | unsigned CondReg = getRegForValue(I->getOperand(0)); |
| 1286 | if (CondReg == 0) return false; |
| 1287 | unsigned Op1Reg = getRegForValue(I->getOperand(1)); |
| 1288 | if (Op1Reg == 0) return false; |
| 1289 | unsigned Op2Reg = getRegForValue(I->getOperand(2)); |
| 1290 | if (Op2Reg == 0) return false; |
| 1291 | |
| 1292 | unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri; |
| 1293 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
| 1294 | .addReg(CondReg).addImm(1)); |
| 1295 | unsigned ResultReg = createResultReg(RC); |
| 1296 | unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr; |
| 1297 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg) |
| 1298 | .addReg(Op1Reg).addReg(Op2Reg) |
| 1299 | .addImm(ARMCC::EQ).addReg(ARM::CPSR); |
| 1300 | UpdateValueMap(I, ResultReg); |
| 1301 | return true; |
| 1302 | } |
| 1303 | |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1304 | bool ARMFastISel::SelectSDiv(const Instruction *I) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1305 | MVT VT; |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1306 | const Type *Ty = I->getType(); |
| 1307 | if (!isTypeLegal(Ty, VT)) |
| 1308 | return false; |
| 1309 | |
| 1310 | // If we have integer div support we should have selected this automagically. |
| 1311 | // In case we have a real miss go ahead and return false and we'll pick |
| 1312 | // it up later. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1313 | if (Subtarget->hasDivide()) return false; |
| 1314 | |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1315 | // Otherwise emit a libcall. |
| 1316 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Eric Christopher | 7bdc4de | 2010-10-11 08:31:54 +0000 | [diff] [blame] | 1317 | if (VT == MVT::i8) |
| 1318 | LC = RTLIB::SDIV_I8; |
| 1319 | else if (VT == MVT::i16) |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1320 | LC = RTLIB::SDIV_I16; |
| 1321 | else if (VT == MVT::i32) |
| 1322 | LC = RTLIB::SDIV_I32; |
| 1323 | else if (VT == MVT::i64) |
| 1324 | LC = RTLIB::SDIV_I64; |
| 1325 | else if (VT == MVT::i128) |
| 1326 | LC = RTLIB::SDIV_I128; |
| 1327 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!"); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1328 | |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1329 | return ARMEmitLibcall(I, LC); |
| 1330 | } |
| 1331 | |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1332 | bool ARMFastISel::SelectSRem(const Instruction *I) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1333 | MVT VT; |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1334 | const Type *Ty = I->getType(); |
| 1335 | if (!isTypeLegal(Ty, VT)) |
| 1336 | return false; |
| 1337 | |
| 1338 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
| 1339 | if (VT == MVT::i8) |
| 1340 | LC = RTLIB::SREM_I8; |
| 1341 | else if (VT == MVT::i16) |
| 1342 | LC = RTLIB::SREM_I16; |
| 1343 | else if (VT == MVT::i32) |
| 1344 | LC = RTLIB::SREM_I32; |
| 1345 | else if (VT == MVT::i64) |
| 1346 | LC = RTLIB::SREM_I64; |
| 1347 | else if (VT == MVT::i128) |
| 1348 | LC = RTLIB::SREM_I128; |
Eric Christopher | a1640d9 | 2010-10-11 08:40:05 +0000 | [diff] [blame] | 1349 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!"); |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 1350 | |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1351 | return ARMEmitLibcall(I, LC); |
| 1352 | } |
| 1353 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1354 | bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) { |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1355 | EVT VT = TLI.getValueType(I->getType(), true); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1356 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1357 | // We can get here in the case when we want to use NEON for our fp |
| 1358 | // operations, but can't figure out how to. Just use the vfp instructions |
| 1359 | // if we have them. |
| 1360 | // FIXME: It'd be nice to use NEON instructions. |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1361 | const Type *Ty = I->getType(); |
| 1362 | bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); |
| 1363 | if (isFloat && !Subtarget->hasVFP2()) |
| 1364 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1365 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1366 | unsigned Op1 = getRegForValue(I->getOperand(0)); |
| 1367 | if (Op1 == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1368 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1369 | unsigned Op2 = getRegForValue(I->getOperand(1)); |
| 1370 | if (Op2 == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1371 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1372 | unsigned Opc; |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 1373 | bool is64bit = VT == MVT::f64 || VT == MVT::i64; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1374 | switch (ISDOpcode) { |
| 1375 | default: return false; |
| 1376 | case ISD::FADD: |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1377 | Opc = is64bit ? ARM::VADDD : ARM::VADDS; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | case ISD::FSUB: |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1380 | Opc = is64bit ? ARM::VSUBD : ARM::VSUBS; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1381 | break; |
| 1382 | case ISD::FMUL: |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1383 | Opc = is64bit ? ARM::VMULD : ARM::VMULS; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1384 | break; |
| 1385 | } |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1386 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1387 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1388 | TII.get(Opc), ResultReg) |
| 1389 | .addReg(Op1).addReg(Op2)); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1390 | UpdateValueMap(I, ResultReg); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1391 | return true; |
| 1392 | } |
| 1393 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1394 | // Call Handling Code |
| 1395 | |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1396 | bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, |
| 1397 | EVT SrcVT, unsigned &ResultReg) { |
| 1398 | unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, |
| 1399 | Src, /*TODO: Kill=*/false); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1400 | |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1401 | if (RR != 0) { |
| 1402 | ResultReg = RR; |
| 1403 | return true; |
| 1404 | } else |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1405 | return false; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1408 | // This is largely taken directly from CCAssignFnForNode - we don't support |
| 1409 | // varargs in FastISel so that part has been removed. |
| 1410 | // TODO: We may not support all of this. |
| 1411 | CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) { |
| 1412 | switch (CC) { |
| 1413 | default: |
| 1414 | llvm_unreachable("Unsupported calling convention"); |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1415 | case CallingConv::Fast: |
Evan Cheng | 1f8b40d | 2010-10-22 18:57:05 +0000 | [diff] [blame] | 1416 | // Ignore fastcc. Silence compiler warnings. |
| 1417 | (void)RetFastCC_ARM_APCS; |
| 1418 | (void)FastCC_ARM_APCS; |
| 1419 | // Fallthrough |
| 1420 | case CallingConv::C: |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1421 | // Use target triple & subtarget features to do actual dispatch. |
| 1422 | if (Subtarget->isAAPCS_ABI()) { |
| 1423 | if (Subtarget->hasVFP2() && |
| 1424 | FloatABIType == FloatABI::Hard) |
| 1425 | return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); |
| 1426 | else |
| 1427 | return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); |
| 1428 | } else |
| 1429 | return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); |
| 1430 | case CallingConv::ARM_AAPCS_VFP: |
| 1431 | return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); |
| 1432 | case CallingConv::ARM_AAPCS: |
| 1433 | return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); |
| 1434 | case CallingConv::ARM_APCS: |
| 1435 | return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); |
| 1436 | } |
| 1437 | } |
| 1438 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1439 | bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args, |
| 1440 | SmallVectorImpl<unsigned> &ArgRegs, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1441 | SmallVectorImpl<MVT> &ArgVTs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1442 | SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, |
| 1443 | SmallVectorImpl<unsigned> &RegArgs, |
| 1444 | CallingConv::ID CC, |
| 1445 | unsigned &NumBytes) { |
| 1446 | SmallVector<CCValAssign, 16> ArgLocs; |
| 1447 | CCState CCInfo(CC, false, TM, ArgLocs, *Context); |
| 1448 | CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false)); |
| 1449 | |
| 1450 | // Get a count of how many bytes are to be pushed on the stack. |
| 1451 | NumBytes = CCInfo.getNextStackOffset(); |
| 1452 | |
| 1453 | // Issue CALLSEQ_START |
| 1454 | unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode(); |
Eric Christopher | fb0b892 | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1455 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1456 | TII.get(AdjStackDown)) |
| 1457 | .addImm(NumBytes)); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1458 | |
| 1459 | // Process the args. |
| 1460 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1461 | CCValAssign &VA = ArgLocs[i]; |
| 1462 | unsigned Arg = ArgRegs[VA.getValNo()]; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1463 | MVT ArgVT = ArgVTs[VA.getValNo()]; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1464 | |
Eric Christopher | 4a2b316 | 2011-01-27 05:44:56 +0000 | [diff] [blame] | 1465 | // We don't handle NEON/vector parameters yet. |
| 1466 | if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64) |
Eric Christopher | a4633f5 | 2010-10-23 09:37:17 +0000 | [diff] [blame] | 1467 | return false; |
| 1468 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1469 | // Handle arg promotion, etc. |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1470 | switch (VA.getLocInfo()) { |
| 1471 | case CCValAssign::Full: break; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1472 | case CCValAssign::SExt: { |
| 1473 | bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), |
| 1474 | Arg, ArgVT, Arg); |
Chris Lattner | 54c6d6f | 2011-01-05 18:41:05 +0000 | [diff] [blame] | 1475 | assert(Emitted && "Failed to emit a sext!"); (void)Emitted; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1476 | Emitted = true; |
| 1477 | ArgVT = VA.getLocVT(); |
| 1478 | break; |
| 1479 | } |
| 1480 | case CCValAssign::ZExt: { |
| 1481 | bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), |
| 1482 | Arg, ArgVT, Arg); |
Chris Lattner | 54c6d6f | 2011-01-05 18:41:05 +0000 | [diff] [blame] | 1483 | assert(Emitted && "Failed to emit a zext!"); (void)Emitted; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1484 | Emitted = true; |
| 1485 | ArgVT = VA.getLocVT(); |
| 1486 | break; |
| 1487 | } |
| 1488 | case CCValAssign::AExt: { |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1489 | bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), |
| 1490 | Arg, ArgVT, Arg); |
| 1491 | if (!Emitted) |
| 1492 | Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), |
| 1493 | Arg, ArgVT, Arg); |
| 1494 | if (!Emitted) |
| 1495 | Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), |
| 1496 | Arg, ArgVT, Arg); |
| 1497 | |
Chris Lattner | 54c6d6f | 2011-01-05 18:41:05 +0000 | [diff] [blame] | 1498 | assert(Emitted && "Failed to emit a aext!"); (void)Emitted; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1499 | ArgVT = VA.getLocVT(); |
| 1500 | break; |
| 1501 | } |
| 1502 | case CCValAssign::BCvt: { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1503 | unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1504 | /*TODO: Kill=*/false); |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1505 | assert(BC != 0 && "Failed to emit a bitcast!"); |
| 1506 | Arg = BC; |
| 1507 | ArgVT = VA.getLocVT(); |
| 1508 | break; |
| 1509 | } |
| 1510 | default: llvm_unreachable("Unknown arg promotion!"); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | // Now copy/store arg to correct locations. |
Eric Christopher | fb0b892 | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1514 | if (VA.isRegLoc() && !VA.needsCustom()) { |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1515 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1516 | VA.getLocReg()) |
| 1517 | .addReg(Arg); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1518 | RegArgs.push_back(VA.getLocReg()); |
Eric Christopher | 2d8f6fe | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1519 | } else if (VA.needsCustom()) { |
| 1520 | // TODO: We need custom lowering for vector (v2f64) args. |
| 1521 | if (VA.getLocVT() != MVT::f64) return false; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1522 | |
Eric Christopher | 2d8f6fe | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1523 | CCValAssign &NextVA = ArgLocs[++i]; |
| 1524 | |
| 1525 | // TODO: Only handle register args for now. |
| 1526 | if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false; |
| 1527 | |
| 1528 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1529 | TII.get(ARM::VMOVRRD), VA.getLocReg()) |
| 1530 | .addReg(NextVA.getLocReg(), RegState::Define) |
| 1531 | .addReg(Arg)); |
| 1532 | RegArgs.push_back(VA.getLocReg()); |
| 1533 | RegArgs.push_back(NextVA.getLocReg()); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1534 | } else { |
Eric Christopher | 5b92480 | 2010-10-21 20:09:54 +0000 | [diff] [blame] | 1535 | assert(VA.isMemLoc()); |
| 1536 | // Need to store on the stack. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1537 | Address Addr; |
| 1538 | Addr.BaseType = Address::RegBase; |
| 1539 | Addr.Base.Reg = ARM::SP; |
| 1540 | Addr.Offset = VA.getLocMemOffset(); |
Eric Christopher | 5b92480 | 2010-10-21 20:09:54 +0000 | [diff] [blame] | 1541 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1542 | if (!ARMEmitStore(ArgVT, Arg, Addr)) return false; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1543 | } |
| 1544 | } |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1545 | return true; |
| 1546 | } |
| 1547 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1548 | bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1549 | const Instruction *I, CallingConv::ID CC, |
| 1550 | unsigned &NumBytes) { |
| 1551 | // Issue CALLSEQ_END |
| 1552 | unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode(); |
Eric Christopher | fb0b892 | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1553 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1554 | TII.get(AdjStackUp)) |
| 1555 | .addImm(NumBytes).addImm(0)); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1556 | |
| 1557 | // Now the return value. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1558 | if (RetVT != MVT::isVoid) { |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1559 | SmallVector<CCValAssign, 16> RVLocs; |
| 1560 | CCState CCInfo(CC, false, TM, RVLocs, *Context); |
| 1561 | CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true)); |
| 1562 | |
| 1563 | // Copy all of the result registers out of their specified physreg. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1564 | if (RVLocs.size() == 2 && RetVT == MVT::f64) { |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1565 | // For this move we copy into two registers and then move into the |
| 1566 | // double fp reg we want. |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1567 | EVT DestVT = RVLocs[0].getValVT(); |
| 1568 | TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT); |
| 1569 | unsigned ResultReg = createResultReg(DstRC); |
| 1570 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1571 | TII.get(ARM::VMOVDRR), ResultReg) |
Eric Christopher | 3659ac2 | 2010-10-20 08:02:24 +0000 | [diff] [blame] | 1572 | .addReg(RVLocs[0].getLocReg()) |
| 1573 | .addReg(RVLocs[1].getLocReg())); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1574 | |
Eric Christopher | 3659ac2 | 2010-10-20 08:02:24 +0000 | [diff] [blame] | 1575 | UsedRegs.push_back(RVLocs[0].getLocReg()); |
| 1576 | UsedRegs.push_back(RVLocs[1].getLocReg()); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1577 | |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1578 | // Finally update the result. |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1579 | UpdateValueMap(I, ResultReg); |
| 1580 | } else { |
Jim Grosbach | 9536959 | 2010-10-13 23:34:31 +0000 | [diff] [blame] | 1581 | assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!"); |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1582 | EVT CopyVT = RVLocs[0].getValVT(); |
| 1583 | TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1584 | |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1585 | unsigned ResultReg = createResultReg(DstRC); |
| 1586 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1587 | ResultReg).addReg(RVLocs[0].getLocReg()); |
| 1588 | UsedRegs.push_back(RVLocs[0].getLocReg()); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1589 | |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1590 | // Finally update the result. |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1591 | UpdateValueMap(I, ResultReg); |
| 1592 | } |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1595 | return true; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1598 | bool ARMFastISel::SelectRet(const Instruction *I) { |
| 1599 | const ReturnInst *Ret = cast<ReturnInst>(I); |
| 1600 | const Function &F = *I->getParent()->getParent(); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1601 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1602 | if (!FuncInfo.CanLowerReturn) |
| 1603 | return false; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1604 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1605 | if (F.isVarArg()) |
| 1606 | return false; |
| 1607 | |
| 1608 | CallingConv::ID CC = F.getCallingConv(); |
| 1609 | if (Ret->getNumOperands() > 0) { |
| 1610 | SmallVector<ISD::OutputArg, 4> Outs; |
| 1611 | GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), |
| 1612 | Outs, TLI); |
| 1613 | |
| 1614 | // Analyze operands of the call, assigning locations to each operand. |
| 1615 | SmallVector<CCValAssign, 16> ValLocs; |
| 1616 | CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext()); |
| 1617 | CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */)); |
| 1618 | |
| 1619 | const Value *RV = Ret->getOperand(0); |
| 1620 | unsigned Reg = getRegForValue(RV); |
| 1621 | if (Reg == 0) |
| 1622 | return false; |
| 1623 | |
| 1624 | // Only handle a single return value for now. |
| 1625 | if (ValLocs.size() != 1) |
| 1626 | return false; |
| 1627 | |
| 1628 | CCValAssign &VA = ValLocs[0]; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1629 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1630 | // Don't bother handling odd stuff for now. |
| 1631 | if (VA.getLocInfo() != CCValAssign::Full) |
| 1632 | return false; |
| 1633 | // Only handle register returns for now. |
| 1634 | if (!VA.isRegLoc()) |
| 1635 | return false; |
| 1636 | // TODO: For now, don't try to handle cases where getLocInfo() |
| 1637 | // says Full but the types don't match. |
Duncan Sands | 1e96bab | 2010-11-04 10:49:57 +0000 | [diff] [blame] | 1638 | if (TLI.getValueType(RV->getType()) != VA.getValVT()) |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1639 | return false; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1640 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1641 | // Make the copy. |
| 1642 | unsigned SrcReg = Reg + VA.getValNo(); |
| 1643 | unsigned DstReg = VA.getLocReg(); |
| 1644 | const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg); |
| 1645 | // Avoid a cross-class copy. This is very unlikely. |
| 1646 | if (!SrcRC->contains(DstReg)) |
| 1647 | return false; |
| 1648 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1649 | DstReg).addReg(SrcReg); |
| 1650 | |
| 1651 | // Mark the register as live out of the function. |
| 1652 | MRI.addLiveOut(VA.getLocReg()); |
| 1653 | } |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1654 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1655 | unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET; |
| 1656 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1657 | TII.get(RetOpc))); |
| 1658 | return true; |
| 1659 | } |
| 1660 | |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1661 | unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) { |
| 1662 | |
| 1663 | // Depend our opcode for thumb on whether or not we're targeting an |
| 1664 | // externally callable function. For libcalls we'll just pass a NULL GV |
| 1665 | // in here. |
| 1666 | bool isExternal = false; |
| 1667 | if (!GV || GV->hasExternalLinkage()) isExternal = true; |
| 1668 | |
| 1669 | // Darwin needs the r9 versions of the opcodes. |
| 1670 | bool isDarwin = Subtarget->isTargetDarwin(); |
| 1671 | if (isThumb && isExternal) { |
| 1672 | return isDarwin ? ARM::tBLXi_r9 : ARM::tBLXi; |
| 1673 | } else if (isThumb) { |
| 1674 | return isDarwin ? ARM::tBLr9 : ARM::tBL; |
| 1675 | } else { |
| 1676 | return isDarwin ? ARM::BLr9 : ARM::BL; |
| 1677 | } |
| 1678 | } |
| 1679 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1680 | // A quick function that will emit a call for a named libcall in F with the |
| 1681 | // vector of passed arguments for the Instruction in I. We can assume that we |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1682 | // can emit a call for any libcall we can produce. This is an abridged version |
| 1683 | // of the full call infrastructure since we won't need to worry about things |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1684 | // like computed function pointers or strange arguments at call sites. |
| 1685 | // TODO: Try to unify this and the normal call bits for ARM, then try to unify |
| 1686 | // with X86. |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1687 | bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) { |
| 1688 | CallingConv::ID CC = TLI.getLibcallCallingConv(Call); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1689 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1690 | // Handle *simple* calls for now. |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1691 | const Type *RetTy = I->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1692 | MVT RetVT; |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1693 | if (RetTy->isVoidTy()) |
| 1694 | RetVT = MVT::isVoid; |
| 1695 | else if (!isTypeLegal(RetTy, RetVT)) |
| 1696 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1697 | |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1698 | // For now we're using BLX etc on the assumption that we have v5t ops. |
| 1699 | if (!Subtarget->hasV5TOps()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1700 | |
Eric Christopher | 836c624 | 2010-12-15 23:47:29 +0000 | [diff] [blame] | 1701 | // TODO: For now if we have long calls specified we don't handle the call. |
| 1702 | if (EnableARMLongCalls) return false; |
| 1703 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1704 | // Set up the argument vectors. |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1705 | SmallVector<Value*, 8> Args; |
| 1706 | SmallVector<unsigned, 8> ArgRegs; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1707 | SmallVector<MVT, 8> ArgVTs; |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1708 | SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; |
| 1709 | Args.reserve(I->getNumOperands()); |
| 1710 | ArgRegs.reserve(I->getNumOperands()); |
| 1711 | ArgVTs.reserve(I->getNumOperands()); |
| 1712 | ArgFlags.reserve(I->getNumOperands()); |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1713 | for (unsigned i = 0; i < I->getNumOperands(); ++i) { |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1714 | Value *Op = I->getOperand(i); |
| 1715 | unsigned Arg = getRegForValue(Op); |
| 1716 | if (Arg == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1717 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1718 | const Type *ArgTy = Op->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1719 | MVT ArgVT; |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1720 | if (!isTypeLegal(ArgTy, ArgVT)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1721 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1722 | ISD::ArgFlagsTy Flags; |
| 1723 | unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); |
| 1724 | Flags.setOrigAlign(OriginalAlignment); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1725 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1726 | Args.push_back(Op); |
| 1727 | ArgRegs.push_back(Arg); |
| 1728 | ArgVTs.push_back(ArgVT); |
| 1729 | ArgFlags.push_back(Flags); |
| 1730 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1731 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1732 | // Handle the arguments now that we've gotten them. |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1733 | SmallVector<unsigned, 4> RegArgs; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1734 | unsigned NumBytes; |
| 1735 | if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes)) |
| 1736 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1737 | |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1738 | // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1739 | // TODO: Turn this into the table of arm call ops. |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1740 | MachineInstrBuilder MIB; |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1741 | unsigned CallOpc = ARMSelectCallOp(NULL); |
| 1742 | if(isThumb) |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1743 | // Explicitly adding the predicate here. |
| 1744 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1745 | TII.get(CallOpc))) |
| 1746 | .addExternalSymbol(TLI.getLibcallName(Call)); |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1747 | else |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1748 | // Explicitly adding the predicate here. |
| 1749 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1750 | TII.get(CallOpc)) |
| 1751 | .addExternalSymbol(TLI.getLibcallName(Call))); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1752 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1753 | // Add implicit physical register uses to the call. |
| 1754 | for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) |
| 1755 | MIB.addReg(RegArgs[i]); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1756 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1757 | // Finish off the call including any return values. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1758 | SmallVector<unsigned, 4> UsedRegs; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1759 | if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1760 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1761 | // Set all unused physreg defs as dead. |
| 1762 | static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1763 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1764 | return true; |
| 1765 | } |
| 1766 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1767 | bool ARMFastISel::SelectCall(const Instruction *I) { |
| 1768 | const CallInst *CI = cast<CallInst>(I); |
| 1769 | const Value *Callee = CI->getCalledValue(); |
| 1770 | |
| 1771 | // Can't handle inline asm or worry about intrinsics yet. |
| 1772 | if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false; |
| 1773 | |
Eric Christopher | e6ca677 | 2010-10-01 21:33:12 +0000 | [diff] [blame] | 1774 | // Only handle global variable Callees that are direct calls. |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1775 | const GlobalValue *GV = dyn_cast<GlobalValue>(Callee); |
Eric Christopher | e6ca677 | 2010-10-01 21:33:12 +0000 | [diff] [blame] | 1776 | if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel())) |
| 1777 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1778 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1779 | // Check the calling convention. |
| 1780 | ImmutableCallSite CS(CI); |
| 1781 | CallingConv::ID CC = CS.getCallingConv(); |
Eric Christopher | 4cf34c6 | 2010-10-18 06:49:12 +0000 | [diff] [blame] | 1782 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1783 | // TODO: Avoid some calling conventions? |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1784 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1785 | // Let SDISel handle vararg functions. |
| 1786 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 1787 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 1788 | if (FTy->isVarArg()) |
| 1789 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1790 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1791 | // Handle *simple* calls for now. |
| 1792 | const Type *RetTy = I->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1793 | MVT RetVT; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1794 | if (RetTy->isVoidTy()) |
| 1795 | RetVT = MVT::isVoid; |
| 1796 | else if (!isTypeLegal(RetTy, RetVT)) |
| 1797 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1798 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1799 | // For now we're using BLX etc on the assumption that we have v5t ops. |
| 1800 | // TODO: Maybe? |
| 1801 | if (!Subtarget->hasV5TOps()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1802 | |
Eric Christopher | 836c624 | 2010-12-15 23:47:29 +0000 | [diff] [blame] | 1803 | // TODO: For now if we have long calls specified we don't handle the call. |
| 1804 | if (EnableARMLongCalls) return false; |
| 1805 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1806 | // Set up the argument vectors. |
| 1807 | SmallVector<Value*, 8> Args; |
| 1808 | SmallVector<unsigned, 8> ArgRegs; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1809 | SmallVector<MVT, 8> ArgVTs; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1810 | SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; |
| 1811 | Args.reserve(CS.arg_size()); |
| 1812 | ArgRegs.reserve(CS.arg_size()); |
| 1813 | ArgVTs.reserve(CS.arg_size()); |
| 1814 | ArgFlags.reserve(CS.arg_size()); |
| 1815 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 1816 | i != e; ++i) { |
| 1817 | unsigned Arg = getRegForValue(*i); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1818 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1819 | if (Arg == 0) |
| 1820 | return false; |
| 1821 | ISD::ArgFlagsTy Flags; |
| 1822 | unsigned AttrInd = i - CS.arg_begin() + 1; |
| 1823 | if (CS.paramHasAttr(AttrInd, Attribute::SExt)) |
| 1824 | Flags.setSExt(); |
| 1825 | if (CS.paramHasAttr(AttrInd, Attribute::ZExt)) |
| 1826 | Flags.setZExt(); |
| 1827 | |
| 1828 | // FIXME: Only handle *easy* calls for now. |
| 1829 | if (CS.paramHasAttr(AttrInd, Attribute::InReg) || |
| 1830 | CS.paramHasAttr(AttrInd, Attribute::StructRet) || |
| 1831 | CS.paramHasAttr(AttrInd, Attribute::Nest) || |
| 1832 | CS.paramHasAttr(AttrInd, Attribute::ByVal)) |
| 1833 | return false; |
| 1834 | |
| 1835 | const Type *ArgTy = (*i)->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1836 | MVT ArgVT; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1837 | if (!isTypeLegal(ArgTy, ArgVT)) |
| 1838 | return false; |
| 1839 | unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); |
| 1840 | Flags.setOrigAlign(OriginalAlignment); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1841 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1842 | Args.push_back(*i); |
| 1843 | ArgRegs.push_back(Arg); |
| 1844 | ArgVTs.push_back(ArgVT); |
| 1845 | ArgFlags.push_back(Flags); |
| 1846 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1847 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1848 | // Handle the arguments now that we've gotten them. |
| 1849 | SmallVector<unsigned, 4> RegArgs; |
| 1850 | unsigned NumBytes; |
| 1851 | if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes)) |
| 1852 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1853 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1854 | // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1855 | // TODO: Turn this into the table of arm call ops. |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1856 | MachineInstrBuilder MIB; |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1857 | unsigned CallOpc = ARMSelectCallOp(GV); |
Eric Christopher | 7bb5996 | 2010-11-29 21:56:23 +0000 | [diff] [blame] | 1858 | // Explicitly adding the predicate here. |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1859 | if(isThumb) |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1860 | // Explicitly adding the predicate here. |
| 1861 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1862 | TII.get(CallOpc))) |
| 1863 | .addGlobalAddress(GV, 0, 0); |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1864 | else |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1865 | // Explicitly adding the predicate here. |
| 1866 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1867 | TII.get(CallOpc)) |
| 1868 | .addGlobalAddress(GV, 0, 0)); |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1869 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1870 | // Add implicit physical register uses to the call. |
| 1871 | for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) |
| 1872 | MIB.addReg(RegArgs[i]); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1873 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1874 | // Finish off the call including any return values. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1875 | SmallVector<unsigned, 4> UsedRegs; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1876 | if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1877 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1878 | // Set all unused physreg defs as dead. |
| 1879 | static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1880 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1881 | return true; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1882 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 1885 | // TODO: SoftFP support. |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1886 | bool ARMFastISel::TargetSelectInstruction(const Instruction *I) { |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1887 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1888 | switch (I->getOpcode()) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 1889 | case Instruction::Load: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1890 | return SelectLoad(I); |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 1891 | case Instruction::Store: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1892 | return SelectStore(I); |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1893 | case Instruction::Br: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1894 | return SelectBranch(I); |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1895 | case Instruction::ICmp: |
| 1896 | case Instruction::FCmp: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1897 | return SelectCmp(I); |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1898 | case Instruction::FPExt: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1899 | return SelectFPExt(I); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1900 | case Instruction::FPTrunc: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1901 | return SelectFPTrunc(I); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1902 | case Instruction::SIToFP: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1903 | return SelectSIToFP(I); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1904 | case Instruction::FPToSI: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1905 | return SelectFPToSI(I); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1906 | case Instruction::FAdd: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1907 | return SelectBinaryOp(I, ISD::FADD); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1908 | case Instruction::FSub: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1909 | return SelectBinaryOp(I, ISD::FSUB); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1910 | case Instruction::FMul: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1911 | return SelectBinaryOp(I, ISD::FMUL); |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1912 | case Instruction::SDiv: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1913 | return SelectSDiv(I); |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1914 | case Instruction::SRem: |
| 1915 | return SelectSRem(I); |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1916 | case Instruction::Call: |
| 1917 | return SelectCall(I); |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1918 | case Instruction::Select: |
| 1919 | return SelectSelect(I); |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1920 | case Instruction::Ret: |
| 1921 | return SelectRet(I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1922 | default: break; |
| 1923 | } |
| 1924 | return false; |
| 1925 | } |
| 1926 | |
| 1927 | namespace llvm { |
| 1928 | llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) { |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 1929 | // Completely untested on non-darwin. |
| 1930 | const TargetMachine &TM = funcInfo.MF->getTarget(); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1931 | |
Eric Christopher | aaa8df4 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 1932 | // Darwin and thumb1 only for now. |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 1933 | const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>(); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1934 | if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() && |
Eric Christopher | aaa8df4 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 1935 | !DisableARMFastISel) |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 1936 | return new ARMFastISel(funcInfo); |
Evan Cheng | 0944795 | 2010-07-26 18:32:55 +0000 | [diff] [blame] | 1937 | return 0; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1938 | } |
| 1939 | } |