Eric Christopher | 84bdfd8 | 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" |
Craig Topper | a925326 | 2014-03-22 23:51:00 +0000 | [diff] [blame] | 17 | #include "ARMBaseRegisterInfo.h" |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 18 | #include "ARMCallingConv.h" |
Eric Christopher | 83a5ec8 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 19 | #include "ARMConstantPoolValue.h" |
Craig Topper | a925326 | 2014-03-22 23:51:00 +0000 | [diff] [blame] | 20 | #include "ARMISelLowering.h" |
| 21 | #include "ARMMachineFunctionInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "ARMSubtarget.h" |
Evan Cheng | a20cde3 | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 23 | #include "MCTargetDesc/ARMAddressingModes.h" |
JF Bastien | 3c6bb8e | 2013-06-11 22:13:46 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/Analysis.h" |
| 26 | #include "llvm/CodeGen/FastISel.h" |
| 27 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
| 28 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 30 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 31 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 33 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 34 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/CallingConv.h" |
| 36 | #include "llvm/IR/DataLayout.h" |
| 37 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 38 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 39 | #include "llvm/IR/GlobalVariable.h" |
| 40 | #include "llvm/IR/Instructions.h" |
| 41 | #include "llvm/IR/IntrinsicInst.h" |
| 42 | #include "llvm/IR/Module.h" |
| 43 | #include "llvm/IR/Operator.h" |
Eric Christopher | 663f499 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 44 | #include "llvm/Support/CommandLine.h" |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 45 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetInstrInfo.h" |
| 47 | #include "llvm/Target/TargetLowering.h" |
| 48 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 49 | #include "llvm/Target/TargetOptions.h" |
| 50 | using namespace llvm; |
| 51 | |
| 52 | namespace { |
Eric Christopher | 0a3c28b | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 53 | |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 54 | // All possible address modes, plus some. |
| 55 | typedef struct Address { |
| 56 | enum { |
| 57 | RegBase, |
| 58 | FrameIndexBase |
| 59 | } BaseType; |
Eric Christopher | 0a3c28b | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 60 | |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 61 | union { |
| 62 | unsigned Reg; |
| 63 | int FI; |
| 64 | } Base; |
Eric Christopher | 0a3c28b | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 65 | |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 66 | int Offset; |
Eric Christopher | 0a3c28b | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 67 | |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 68 | // Innocuous defaults for our address. |
| 69 | Address() |
Jim Grosbach | 4e98316 | 2011-05-16 22:24:07 +0000 | [diff] [blame] | 70 | : BaseType(RegBase), Offset(0) { |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 71 | Base.Reg = 0; |
| 72 | } |
| 73 | } Address; |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 74 | |
Craig Topper | 2669631 | 2014-03-18 07:27:13 +0000 | [diff] [blame] | 75 | class ARMFastISel final : public FastISel { |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 76 | |
| 77 | /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can |
| 78 | /// make the right decision when generating code for different targets. |
| 79 | const ARMSubtarget *Subtarget; |
Bill Wendling | 6c1d959 | 2013-12-30 05:17:29 +0000 | [diff] [blame] | 80 | Module &M; |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 81 | const TargetMachine &TM; |
| 82 | const TargetInstrInfo &TII; |
| 83 | const TargetLowering &TLI; |
Eric Christopher | 83a5ec8 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 84 | ARMFunctionInfo *AFI; |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 85 | |
Eric Christopher | b024be3 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 86 | // Convenience variables to avoid some queries. |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 87 | bool isThumb2; |
Eric Christopher | b024be3 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 88 | LLVMContext *Context; |
Eric Christopher | 6a0333c | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 89 | |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 90 | public: |
Bob Wilson | 3e6fa46 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 91 | explicit ARMFastISel(FunctionLoweringInfo &funcInfo, |
| 92 | const TargetLibraryInfo *libInfo) |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 93 | : FastISel(funcInfo, libInfo), |
Eric Christopher | c125e12 | 2015-01-29 00:19:37 +0000 | [diff] [blame] | 94 | Subtarget( |
| 95 | &static_cast<const ARMSubtarget &>(funcInfo.MF->getSubtarget())), |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 96 | M(const_cast<Module &>(*funcInfo.Fn->getParent())), |
Eric Christopher | c125e12 | 2015-01-29 00:19:37 +0000 | [diff] [blame] | 97 | TM(funcInfo.MF->getTarget()), TII(*Subtarget->getInstrInfo()), |
| 98 | TLI(*Subtarget->getTargetLowering()) { |
Eric Christopher | 8d03b8a | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 99 | AFI = funcInfo.MF->getInfo<ARMFunctionInfo>(); |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 100 | isThumb2 = AFI->isThumbFunction(); |
Eric Christopher | b024be3 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 101 | Context = &funcInfo.Fn->getContext(); |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Eric Christopher | d8e8a29 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 104 | // Code from FastISel.cpp. |
Craig Topper | fd1c925 | 2012-08-18 21:38:45 +0000 | [diff] [blame] | 105 | private: |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 106 | unsigned fastEmitInst_r(unsigned MachineInstOpcode, |
Craig Topper | fd1c925 | 2012-08-18 21:38:45 +0000 | [diff] [blame] | 107 | const TargetRegisterClass *RC, |
| 108 | unsigned Op0, bool Op0IsKill); |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 109 | unsigned fastEmitInst_rr(unsigned MachineInstOpcode, |
Craig Topper | fd1c925 | 2012-08-18 21:38:45 +0000 | [diff] [blame] | 110 | const TargetRegisterClass *RC, |
| 111 | unsigned Op0, bool Op0IsKill, |
| 112 | unsigned Op1, bool Op1IsKill); |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 113 | unsigned fastEmitInst_rrr(unsigned MachineInstOpcode, |
Craig Topper | fd1c925 | 2012-08-18 21:38:45 +0000 | [diff] [blame] | 114 | const TargetRegisterClass *RC, |
| 115 | unsigned Op0, bool Op0IsKill, |
| 116 | unsigned Op1, bool Op1IsKill, |
| 117 | unsigned Op2, bool Op2IsKill); |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 118 | unsigned fastEmitInst_ri(unsigned MachineInstOpcode, |
Craig Topper | fd1c925 | 2012-08-18 21:38:45 +0000 | [diff] [blame] | 119 | const TargetRegisterClass *RC, |
| 120 | unsigned Op0, bool Op0IsKill, |
| 121 | uint64_t Imm); |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 122 | unsigned fastEmitInst_rri(unsigned MachineInstOpcode, |
Craig Topper | fd1c925 | 2012-08-18 21:38:45 +0000 | [diff] [blame] | 123 | const TargetRegisterClass *RC, |
| 124 | unsigned Op0, bool Op0IsKill, |
| 125 | unsigned Op1, bool Op1IsKill, |
| 126 | uint64_t Imm); |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 127 | unsigned fastEmitInst_i(unsigned MachineInstOpcode, |
Craig Topper | fd1c925 | 2012-08-18 21:38:45 +0000 | [diff] [blame] | 128 | const TargetRegisterClass *RC, |
| 129 | uint64_t Imm); |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 130 | |
Eric Christopher | d8e8a29 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 131 | // Backend specific FastISel code. |
Craig Topper | fd1c925 | 2012-08-18 21:38:45 +0000 | [diff] [blame] | 132 | private: |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 133 | bool fastSelectInstruction(const Instruction *I) override; |
| 134 | unsigned fastMaterializeConstant(const Constant *C) override; |
| 135 | unsigned fastMaterializeAlloca(const AllocaInst *AI) override; |
Craig Topper | 6bc27bf | 2014-03-10 02:09:33 +0000 | [diff] [blame] | 136 | bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, |
| 137 | const LoadInst *LI) override; |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 138 | bool fastLowerArguments() override; |
Craig Topper | fd1c925 | 2012-08-18 21:38:45 +0000 | [diff] [blame] | 139 | private: |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 140 | #include "ARMGenFastISel.inc" |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 141 | |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 142 | // Instruction selection routines. |
Eric Christopher | cc766a2 | 2010-09-10 23:10:30 +0000 | [diff] [blame] | 143 | private: |
Eric Christopher | 2f8637d | 2010-10-21 21:47:51 +0000 | [diff] [blame] | 144 | bool SelectLoad(const Instruction *I); |
| 145 | bool SelectStore(const Instruction *I); |
| 146 | bool SelectBranch(const Instruction *I); |
Chad Rosier | ded4c99 | 2012-02-07 23:56:08 +0000 | [diff] [blame] | 147 | bool SelectIndirectBr(const Instruction *I); |
Eric Christopher | 2f8637d | 2010-10-21 21:47:51 +0000 | [diff] [blame] | 148 | bool SelectCmp(const Instruction *I); |
| 149 | bool SelectFPExt(const Instruction *I); |
| 150 | bool SelectFPTrunc(const Instruction *I); |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 151 | bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode); |
| 152 | bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode); |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 153 | bool SelectIToFP(const Instruction *I, bool isSigned); |
| 154 | bool SelectFPToI(const Instruction *I, bool isSigned); |
Chad Rosier | aaa55a8 | 2012-02-03 21:07:27 +0000 | [diff] [blame] | 155 | bool SelectDiv(const Instruction *I, bool isSigned); |
Chad Rosier | b84a4b4 | 2012-02-03 21:23:45 +0000 | [diff] [blame] | 156 | bool SelectRem(const Instruction *I, bool isSigned); |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 157 | bool SelectCall(const Instruction *I, const char *IntrMemName); |
| 158 | bool SelectIntrinsicCall(const IntrinsicInst &I); |
Eric Christopher | 2f8637d | 2010-10-21 21:47:51 +0000 | [diff] [blame] | 159 | bool SelectSelect(const Instruction *I); |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 160 | bool SelectRet(const Instruction *I); |
Chad Rosier | ee7e452 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 161 | bool SelectTrunc(const Instruction *I); |
| 162 | bool SelectIntExt(const Instruction *I); |
Jush Lu | 4705da9 | 2012-08-03 02:37:48 +0000 | [diff] [blame] | 163 | bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy); |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 164 | |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 165 | // Utility routines. |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 166 | private: |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 167 | bool isTypeLegal(Type *Ty, MVT &VT); |
| 168 | bool isLoadTypeLegal(Type *Ty, MVT &VT); |
Chad Rosier | 9cf803c | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 169 | bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value, |
David Blaikie | 3ef249c9 | 2015-01-30 23:04:39 +0000 | [diff] [blame] | 170 | bool isZExt); |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 171 | bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr, |
Chad Rosier | a26979b | 2011-12-14 17:26:05 +0000 | [diff] [blame] | 172 | unsigned Alignment = 0, bool isZExt = true, |
| 173 | bool allocReg = true); |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 174 | bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr, |
Bob Wilson | 80381f6 | 2011-12-04 00:52:23 +0000 | [diff] [blame] | 175 | unsigned Alignment = 0); |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 176 | bool ARMComputeAddress(const Value *Obj, Address &Addr); |
Chad Rosier | 150d35b | 2012-12-17 22:35:29 +0000 | [diff] [blame] | 177 | void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3); |
Chad Rosier | 057b6d3 | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 178 | bool ARMIsMemCpySmall(uint64_t Len); |
Chad Rosier | 9f5c68a | 2012-12-06 01:34:31 +0000 | [diff] [blame] | 179 | bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len, |
| 180 | unsigned Alignment); |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 181 | unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt); |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 182 | unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT); |
| 183 | unsigned ARMMaterializeInt(const Constant *C, MVT VT); |
| 184 | unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT); |
| 185 | unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg); |
| 186 | unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg); |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 187 | unsigned ARMSelectCallOp(bool UseReg); |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 188 | unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT); |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 189 | |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame] | 190 | const TargetLowering *getTargetLowering() { return &TLI; } |
Christian Pirker | 238c7c1 | 2014-05-12 11:19:20 +0000 | [diff] [blame] | 191 | |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 192 | // Call handling routines. |
| 193 | private: |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 194 | CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, |
| 195 | bool Return, |
| 196 | bool isVarArg); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 197 | bool ProcessCallArgs(SmallVectorImpl<Value*> &Args, |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 198 | SmallVectorImpl<unsigned> &ArgRegs, |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 199 | SmallVectorImpl<MVT> &ArgVTs, |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 200 | SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, |
| 201 | SmallVectorImpl<unsigned> &RegArgs, |
| 202 | CallingConv::ID CC, |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 203 | unsigned &NumBytes, |
| 204 | bool isVarArg); |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 205 | unsigned getLibcallReg(const Twine &Name); |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 206 | bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 207 | const Instruction *I, CallingConv::ID CC, |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 208 | unsigned &NumBytes, bool isVarArg); |
Eric Christopher | 7990df1 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 209 | bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call); |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 210 | |
| 211 | // OptionalDef handling routines. |
| 212 | private: |
Eric Christopher | 174d872 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 213 | bool isARMNEONPred(const MachineInstr *MI); |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 214 | bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR); |
| 215 | const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB); |
Chad Rosier | 150d35b | 2012-12-17 22:35:29 +0000 | [diff] [blame] | 216 | void AddLoadStoreOperands(MVT VT, Address &Addr, |
Cameron Zwarich | 6528a54 | 2011-05-28 20:34:49 +0000 | [diff] [blame] | 217 | const MachineInstrBuilder &MIB, |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 218 | unsigned Flags, bool useAM3); |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 219 | }; |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 220 | |
| 221 | } // end anonymous namespace |
| 222 | |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 223 | #include "ARMGenCallingConv.inc" |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 224 | |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 225 | // DefinesOptionalPredicate - This is different from DefinesPredicate in that |
| 226 | // we don't care about implicit defs here, just places we'll need to add a |
| 227 | // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR. |
| 228 | bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) { |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 229 | if (!MI->hasOptionalDef()) |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 230 | return false; |
| 231 | |
| 232 | // Look to see if our OptionalDef is defining CPSR or CCR. |
| 233 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 234 | const MachineOperand &MO = MI->getOperand(i); |
Eric Christopher | 985d9e4 | 2010-08-20 00:36:24 +0000 | [diff] [blame] | 235 | if (!MO.isReg() || !MO.isDef()) continue; |
| 236 | if (MO.getReg() == ARM::CPSR) |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 237 | *CPSR = true; |
| 238 | } |
| 239 | return true; |
| 240 | } |
| 241 | |
Eric Christopher | 174d872 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 242 | bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 243 | const MCInstrDesc &MCID = MI->getDesc(); |
Eric Christopher | 501d2e2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 244 | |
Joey Gouly | a5153cb | 2013-09-09 14:21:49 +0000 | [diff] [blame] | 245 | // If we're a thumb2 or not NEON function we'll be handled via isPredicable. |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 246 | if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON || |
Eric Christopher | 174d872 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 247 | AFI->isThumb2Function()) |
Joey Gouly | a5153cb | 2013-09-09 14:21:49 +0000 | [diff] [blame] | 248 | return MI->isPredicable(); |
Eric Christopher | 501d2e2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 249 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 250 | for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) |
| 251 | if (MCID.OpInfo[i].isPredicate()) |
Eric Christopher | 174d872 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 252 | return true; |
Eric Christopher | 501d2e2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 253 | |
Eric Christopher | 174d872 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 257 | // If the machine is predicable go ahead and add the predicate operands, if |
| 258 | // it needs default CC operands add those. |
Eric Christopher | e8fccc8 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 259 | // TODO: If we want to support thumb1 then we'll need to deal with optional |
| 260 | // CPSR defs that need to be added before the remaining operands. See s_cc_out |
| 261 | // for descriptions why. |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 262 | const MachineInstrBuilder & |
| 263 | ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) { |
| 264 | MachineInstr *MI = &*MIB; |
| 265 | |
Eric Christopher | 174d872 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 266 | // Do we use a predicate? or... |
| 267 | // Are we NEON in ARM mode and have a predicate operand? If so, I know |
| 268 | // we're not predicable but add it anyways. |
Joey Gouly | a5153cb | 2013-09-09 14:21:49 +0000 | [diff] [blame] | 269 | if (isARMNEONPred(MI)) |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 270 | AddDefaultPred(MIB); |
Eric Christopher | 501d2e2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 271 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 272 | // Do we optionally set a predicate? Preds is size > 0 iff the predicate |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 273 | // defines CPSR. All other OptionalDefines in ARM are the CCR register. |
Eric Christopher | a5d60c6 | 2010-08-19 15:35:27 +0000 | [diff] [blame] | 274 | bool CPSR = false; |
Eric Christopher | 0d274a0 | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 275 | if (DefinesOptionalPredicate(MI, &CPSR)) { |
| 276 | if (CPSR) |
| 277 | AddDefaultT1CC(MIB); |
| 278 | else |
| 279 | AddDefaultCC(MIB); |
| 280 | } |
| 281 | return MIB; |
| 282 | } |
| 283 | |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 284 | unsigned ARMFastISel::fastEmitInst_r(unsigned MachineInstOpcode, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 285 | const TargetRegisterClass *RC, |
| 286 | unsigned Op0, bool Op0IsKill) { |
| 287 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 288 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 289 | |
Jim Grosbach | 06c2a68 | 2013-08-16 23:37:31 +0000 | [diff] [blame] | 290 | // Make sure the input operand is sufficiently constrained to be legal |
| 291 | // for this instruction. |
| 292 | Op0 = constrainOperandRegClass(II, Op0, 1); |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 293 | if (II.getNumDefs() >= 1) { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 294 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, |
| 295 | ResultReg).addReg(Op0, Op0IsKill * RegState::Kill)); |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 296 | } else { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 297 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 298 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 299 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 300 | TII.get(TargetOpcode::COPY), ResultReg) |
| 301 | .addReg(II.ImplicitDefs[0])); |
| 302 | } |
| 303 | return ResultReg; |
| 304 | } |
| 305 | |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 306 | unsigned ARMFastISel::fastEmitInst_rr(unsigned MachineInstOpcode, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 307 | const TargetRegisterClass *RC, |
| 308 | unsigned Op0, bool Op0IsKill, |
| 309 | unsigned Op1, bool Op1IsKill) { |
| 310 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 311 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 312 | |
Jim Grosbach | 06c2a68 | 2013-08-16 23:37:31 +0000 | [diff] [blame] | 313 | // Make sure the input operands are sufficiently constrained to be legal |
| 314 | // for this instruction. |
| 315 | Op0 = constrainOperandRegClass(II, Op0, 1); |
| 316 | Op1 = constrainOperandRegClass(II, Op1, 2); |
| 317 | |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 318 | if (II.getNumDefs() >= 1) { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 319 | AddOptionalDefs( |
| 320 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| 321 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 322 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 323 | } else { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 324 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 325 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 326 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 327 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 328 | TII.get(TargetOpcode::COPY), ResultReg) |
| 329 | .addReg(II.ImplicitDefs[0])); |
| 330 | } |
| 331 | return ResultReg; |
| 332 | } |
| 333 | |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 334 | unsigned ARMFastISel::fastEmitInst_rrr(unsigned MachineInstOpcode, |
Cameron Zwarich | 53dd03d | 2011-03-30 23:01:21 +0000 | [diff] [blame] | 335 | const TargetRegisterClass *RC, |
| 336 | unsigned Op0, bool Op0IsKill, |
| 337 | unsigned Op1, bool Op1IsKill, |
| 338 | unsigned Op2, bool Op2IsKill) { |
| 339 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 340 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Cameron Zwarich | 53dd03d | 2011-03-30 23:01:21 +0000 | [diff] [blame] | 341 | |
Jim Grosbach | 06c2a68 | 2013-08-16 23:37:31 +0000 | [diff] [blame] | 342 | // Make sure the input operands are sufficiently constrained to be legal |
| 343 | // for this instruction. |
| 344 | Op0 = constrainOperandRegClass(II, Op0, 1); |
| 345 | Op1 = constrainOperandRegClass(II, Op1, 2); |
| 346 | Op2 = constrainOperandRegClass(II, Op1, 3); |
| 347 | |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 348 | if (II.getNumDefs() >= 1) { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 349 | AddOptionalDefs( |
| 350 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| 351 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 352 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 353 | .addReg(Op2, Op2IsKill * RegState::Kill)); |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 354 | } else { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 355 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
Cameron Zwarich | 53dd03d | 2011-03-30 23:01:21 +0000 | [diff] [blame] | 356 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 357 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 358 | .addReg(Op2, Op2IsKill * RegState::Kill)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 359 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Cameron Zwarich | 53dd03d | 2011-03-30 23:01:21 +0000 | [diff] [blame] | 360 | TII.get(TargetOpcode::COPY), ResultReg) |
| 361 | .addReg(II.ImplicitDefs[0])); |
| 362 | } |
| 363 | return ResultReg; |
| 364 | } |
| 365 | |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 366 | unsigned ARMFastISel::fastEmitInst_ri(unsigned MachineInstOpcode, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 367 | const TargetRegisterClass *RC, |
| 368 | unsigned Op0, bool Op0IsKill, |
| 369 | uint64_t Imm) { |
| 370 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 371 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 372 | |
Jim Grosbach | 06c2a68 | 2013-08-16 23:37:31 +0000 | [diff] [blame] | 373 | // Make sure the input operand is sufficiently constrained to be legal |
| 374 | // for this instruction. |
| 375 | Op0 = constrainOperandRegClass(II, Op0, 1); |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 376 | if (II.getNumDefs() >= 1) { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 377 | AddOptionalDefs( |
| 378 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| 379 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 380 | .addImm(Imm)); |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 381 | } else { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 382 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 383 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 384 | .addImm(Imm)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 385 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 386 | TII.get(TargetOpcode::COPY), ResultReg) |
| 387 | .addReg(II.ImplicitDefs[0])); |
| 388 | } |
| 389 | return ResultReg; |
| 390 | } |
| 391 | |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 392 | unsigned ARMFastISel::fastEmitInst_rri(unsigned MachineInstOpcode, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 393 | const TargetRegisterClass *RC, |
| 394 | unsigned Op0, bool Op0IsKill, |
| 395 | unsigned Op1, bool Op1IsKill, |
| 396 | uint64_t Imm) { |
| 397 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 398 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 399 | |
Jim Grosbach | 06c2a68 | 2013-08-16 23:37:31 +0000 | [diff] [blame] | 400 | // Make sure the input operands are sufficiently constrained to be legal |
| 401 | // for this instruction. |
| 402 | Op0 = constrainOperandRegClass(II, Op0, 1); |
| 403 | Op1 = constrainOperandRegClass(II, Op1, 2); |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 404 | if (II.getNumDefs() >= 1) { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 405 | AddOptionalDefs( |
| 406 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| 407 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 408 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 409 | .addImm(Imm)); |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 410 | } else { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 411 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 412 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 413 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 414 | .addImm(Imm)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 415 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 416 | TII.get(TargetOpcode::COPY), ResultReg) |
| 417 | .addReg(II.ImplicitDefs[0])); |
| 418 | } |
| 419 | return ResultReg; |
| 420 | } |
| 421 | |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 422 | unsigned ARMFastISel::fastEmitInst_i(unsigned MachineInstOpcode, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 423 | const TargetRegisterClass *RC, |
| 424 | uint64_t Imm) { |
| 425 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 426 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 427 | |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 428 | if (II.getNumDefs() >= 1) { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 429 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, |
| 430 | ResultReg).addImm(Imm)); |
Chad Rosier | 0bc5132 | 2012-02-15 17:36:21 +0000 | [diff] [blame] | 431 | } else { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 432 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 433 | .addImm(Imm)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 434 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 09f757d | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 435 | TII.get(TargetOpcode::COPY), ResultReg) |
| 436 | .addReg(II.ImplicitDefs[0])); |
| 437 | } |
| 438 | return ResultReg; |
| 439 | } |
| 440 | |
Eric Christopher | 860fc93 | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 441 | // TODO: Don't worry about 64-bit now, but when this is fixed remove the |
| 442 | // checks from the various callers. |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 443 | unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) { |
Duncan Sands | 1462777 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 444 | if (VT == MVT::f64) return 0; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 445 | |
Eric Christopher | 4bd7047 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 446 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 447 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Jim Grosbach | 6990e5f | 2012-03-01 22:47:09 +0000 | [diff] [blame] | 448 | TII.get(ARM::VMOVSR), MoveReg) |
Eric Christopher | 4bd7047 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 449 | .addReg(SrcReg)); |
| 450 | return MoveReg; |
| 451 | } |
| 452 | |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 453 | unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) { |
Duncan Sands | 1462777 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 454 | if (VT == MVT::i64) return 0; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 455 | |
Eric Christopher | 2cbe0fd | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 456 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 457 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Jim Grosbach | 6990e5f | 2012-03-01 22:47:09 +0000 | [diff] [blame] | 458 | TII.get(ARM::VMOVRS), MoveReg) |
Eric Christopher | 2cbe0fd | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 459 | .addReg(SrcReg)); |
| 460 | return MoveReg; |
| 461 | } |
| 462 | |
Eric Christopher | 3cf63f1 | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 463 | // For double width floating point we need to materialize two constants |
| 464 | // (the high and the low) into integer registers then use a move to get |
| 465 | // the combined constant into an FP reg. |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 466 | unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) { |
Eric Christopher | 3cf63f1 | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 467 | const APFloat Val = CFP->getValueAPF(); |
Duncan Sands | 1462777 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 468 | bool is64bit = VT == MVT::f64; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 469 | |
Eric Christopher | 3cf63f1 | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 470 | // This checks to see if we can use VFP3 instructions to materialize |
| 471 | // a constant, otherwise we have to go through the constant pool. |
| 472 | if (TLI.isFPImmLegal(Val, VT)) { |
Jim Grosbach | efc761a | 2011-09-30 00:50:06 +0000 | [diff] [blame] | 473 | int Imm; |
| 474 | unsigned Opc; |
| 475 | if (is64bit) { |
| 476 | Imm = ARM_AM::getFP64Imm(Val); |
| 477 | Opc = ARM::FCONSTD; |
| 478 | } else { |
| 479 | Imm = ARM_AM::getFP32Imm(Val); |
| 480 | Opc = ARM::FCONSTS; |
| 481 | } |
Eric Christopher | 3cf63f1 | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 482 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 483 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 484 | TII.get(Opc), DestReg).addImm(Imm)); |
Eric Christopher | 3cf63f1 | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 485 | return DestReg; |
| 486 | } |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 487 | |
Eric Christopher | 860fc93 | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 488 | // Require VFP2 for loading fp constants. |
Eric Christopher | 22fd29a | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 489 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 490 | |
Eric Christopher | 22fd29a | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 491 | // MachineConstantPool wants an explicit alignment. |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 492 | unsigned Align = DL.getPrefTypeAlignment(CFP->getType()); |
Eric Christopher | 22fd29a | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 493 | if (Align == 0) { |
| 494 | // TODO: Figure out if this is correct. |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 495 | Align = DL.getTypeAllocSize(CFP->getType()); |
Eric Christopher | 22fd29a | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 496 | } |
| 497 | unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align); |
| 498 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 499 | unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 500 | |
Eric Christopher | 860fc93 | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 501 | // The extra reg is for addrmode5. |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 502 | AddOptionalDefs( |
| 503 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) |
| 504 | .addConstantPoolIndex(Idx) |
| 505 | .addReg(0)); |
Eric Christopher | 22fd29a | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 506 | return DestReg; |
Eric Christopher | 3cf63f1 | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 509 | unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) { |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 510 | |
Chad Rosier | 67f9688 | 2011-11-04 22:29:00 +0000 | [diff] [blame] | 511 | if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1) |
Juergen Ributzka | 5df8603df | 2014-08-15 16:59:46 +0000 | [diff] [blame] | 512 | return 0; |
Eric Christopher | e4dd737 | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 513 | |
| 514 | // If we can do this in a single instruction without a constant pool entry |
| 515 | // do so now. |
| 516 | const ConstantInt *CI = cast<ConstantInt>(C); |
Chad Rosier | e8b8b77 | 2011-11-04 23:09:49 +0000 | [diff] [blame] | 517 | if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) { |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 518 | unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16; |
Chad Rosier | 2e82ad1 | 2012-11-27 01:06:49 +0000 | [diff] [blame] | 519 | const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass : |
| 520 | &ARM::GPRRegClass; |
| 521 | unsigned ImmReg = createResultReg(RC); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 522 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Chad Rosier | 67f9688 | 2011-11-04 22:29:00 +0000 | [diff] [blame] | 523 | TII.get(Opc), ImmReg) |
Chad Rosier | d0191a5 | 2011-11-05 20:16:15 +0000 | [diff] [blame] | 524 | .addImm(CI->getZExtValue())); |
Chad Rosier | 67f9688 | 2011-11-04 22:29:00 +0000 | [diff] [blame] | 525 | return ImmReg; |
Eric Christopher | e4dd737 | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Chad Rosier | 2a3503e | 2011-11-11 00:36:21 +0000 | [diff] [blame] | 528 | // Use MVN to emit negative constants. |
| 529 | if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) { |
| 530 | unsigned Imm = (unsigned)~(CI->getSExtValue()); |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 531 | bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : |
Chad Rosier | 2a3503e | 2011-11-11 00:36:21 +0000 | [diff] [blame] | 532 | (ARM_AM::getSOImmVal(Imm) != -1); |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 533 | if (UseImm) { |
Chad Rosier | 2a3503e | 2011-11-11 00:36:21 +0000 | [diff] [blame] | 534 | unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi; |
Juergen Ributzka | 2cbcf7a | 2014-08-13 21:39:18 +0000 | [diff] [blame] | 535 | const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass : |
| 536 | &ARM::GPRRegClass; |
| 537 | unsigned ImmReg = createResultReg(RC); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 538 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Chad Rosier | 2a3503e | 2011-11-11 00:36:21 +0000 | [diff] [blame] | 539 | TII.get(Opc), ImmReg) |
| 540 | .addImm(Imm)); |
| 541 | return ImmReg; |
| 542 | } |
| 543 | } |
| 544 | |
Juergen Ributzka | 5df8603df | 2014-08-15 16:59:46 +0000 | [diff] [blame] | 545 | unsigned ResultReg = 0; |
Juergen Ributzka | a5b0838 | 2014-08-13 21:42:19 +0000 | [diff] [blame] | 546 | if (Subtarget->useMovt(*FuncInfo.MF)) |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 547 | ResultReg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); |
Juergen Ributzka | 5df8603df | 2014-08-15 16:59:46 +0000 | [diff] [blame] | 548 | |
| 549 | if (ResultReg) |
| 550 | return ResultReg; |
Juergen Ributzka | a5b0838 | 2014-08-13 21:42:19 +0000 | [diff] [blame] | 551 | |
Chad Rosier | 2a3503e | 2011-11-11 00:36:21 +0000 | [diff] [blame] | 552 | // Load from constant pool. For now 32-bit only. |
Chad Rosier | 67f9688 | 2011-11-04 22:29:00 +0000 | [diff] [blame] | 553 | if (VT != MVT::i32) |
Juergen Ributzka | 5df8603df | 2014-08-15 16:59:46 +0000 | [diff] [blame] | 554 | return 0; |
Chad Rosier | 67f9688 | 2011-11-04 22:29:00 +0000 | [diff] [blame] | 555 | |
Eric Christopher | c3e118e | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 556 | // MachineConstantPool wants an explicit alignment. |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 557 | unsigned Align = DL.getPrefTypeAlignment(C->getType()); |
Eric Christopher | c3e118e | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 558 | if (Align == 0) { |
| 559 | // TODO: Figure out if this is correct. |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 560 | Align = DL.getTypeAllocSize(C->getType()); |
Eric Christopher | c3e118e | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 561 | } |
| 562 | unsigned Idx = MCP.getConstantPoolIndex(C, Align); |
Juergen Ributzka | 5df8603df | 2014-08-15 16:59:46 +0000 | [diff] [blame] | 563 | ResultReg = createResultReg(TLI.getRegClassFor(VT)); |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 564 | if (isThumb2) |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 565 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Juergen Ributzka | 5df8603df | 2014-08-15 16:59:46 +0000 | [diff] [blame] | 566 | TII.get(ARM::t2LDRpci), ResultReg) |
| 567 | .addConstantPoolIndex(Idx)); |
Tim Northover | e42fb07 | 2014-02-04 10:38:46 +0000 | [diff] [blame] | 568 | else { |
Eric Christopher | 22d0492 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 569 | // The extra immediate is for addrmode2. |
Juergen Ributzka | 5df8603df | 2014-08-15 16:59:46 +0000 | [diff] [blame] | 570 | ResultReg = constrainOperandRegClass(TII.get(ARM::LDRcp), ResultReg, 0); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 571 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Juergen Ributzka | 5df8603df | 2014-08-15 16:59:46 +0000 | [diff] [blame] | 572 | TII.get(ARM::LDRcp), ResultReg) |
| 573 | .addConstantPoolIndex(Idx) |
| 574 | .addImm(0)); |
Tim Northover | e42fb07 | 2014-02-04 10:38:46 +0000 | [diff] [blame] | 575 | } |
Juergen Ributzka | 5df8603df | 2014-08-15 16:59:46 +0000 | [diff] [blame] | 576 | return ResultReg; |
Eric Christopher | 92db201 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 579 | unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) { |
Eric Christopher | 7787f79 | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 580 | // For now 32-bit only. |
Duncan Sands | 1462777 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 581 | if (VT != MVT::i32) return 0; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 582 | |
Eric Christopher | 7787f79 | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 583 | Reloc::Model RelocM = TM.getRelocationModel(); |
Jush Lu | e87e559 | 2012-08-29 02:41:21 +0000 | [diff] [blame] | 584 | bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM); |
Craig Topper | 61e88f4 | 2014-11-21 05:58:21 +0000 | [diff] [blame] | 585 | const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass |
| 586 | : &ARM::GPRRegClass; |
Chad Rosier | 65710a7 | 2012-11-07 00:13:01 +0000 | [diff] [blame] | 587 | unsigned DestReg = createResultReg(RC); |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 588 | |
Tim Northover | d6a729b | 2014-01-06 14:28:05 +0000 | [diff] [blame] | 589 | // FastISel TLS support on non-MachO is broken, punt to SelectionDAG. |
JF Bastien | 18db1f2 | 2013-06-14 02:49:43 +0000 | [diff] [blame] | 590 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 591 | bool IsThreadLocal = GVar && GVar->isThreadLocal(); |
Tim Northover | d6a729b | 2014-01-06 14:28:05 +0000 | [diff] [blame] | 592 | if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0; |
JF Bastien | 18db1f2 | 2013-06-14 02:49:43 +0000 | [diff] [blame] | 593 | |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 594 | // Use movw+movt when possible, it avoids constant pool entries. |
Tim Northover | fa36dfe | 2013-11-26 12:45:05 +0000 | [diff] [blame] | 595 | // Non-darwin targets only support static movt relocations in FastISel. |
Eric Christopher | c1058df | 2014-07-04 01:55:26 +0000 | [diff] [blame] | 596 | if (Subtarget->useMovt(*FuncInfo.MF) && |
Tim Northover | d6a729b | 2014-01-06 14:28:05 +0000 | [diff] [blame] | 597 | (Subtarget->isTargetMachO() || RelocM == Reloc::Static)) { |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 598 | unsigned Opc; |
Tim Northover | db962e2c | 2013-11-25 16:24:52 +0000 | [diff] [blame] | 599 | unsigned char TF = 0; |
Tim Northover | d6a729b | 2014-01-06 14:28:05 +0000 | [diff] [blame] | 600 | if (Subtarget->isTargetMachO()) |
Tim Northover | db962e2c | 2013-11-25 16:24:52 +0000 | [diff] [blame] | 601 | TF = ARMII::MO_NONLAZY; |
| 602 | |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 603 | switch (RelocM) { |
| 604 | case Reloc::PIC_: |
| 605 | Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel; |
| 606 | break; |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 607 | default: |
| 608 | Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm; |
| 609 | break; |
| 610 | } |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 611 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 612 | TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF)); |
Eric Christopher | 7787f79 | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 613 | } else { |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 614 | // MachineConstantPool wants an explicit alignment. |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 615 | unsigned Align = DL.getPrefTypeAlignment(GV->getType()); |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 616 | if (Align == 0) { |
| 617 | // TODO: Figure out if this is correct. |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 618 | Align = DL.getTypeAllocSize(GV->getType()); |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 621 | if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_) |
| 622 | return ARMLowerPICELF(GV, Align, VT); |
| 623 | |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 624 | // Grab index. |
| 625 | unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : |
| 626 | (Subtarget->isThumb() ? 4 : 8); |
| 627 | unsigned Id = AFI->createPICLabelUId(); |
| 628 | ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id, |
| 629 | ARMCP::CPValue, |
| 630 | PCAdj); |
| 631 | unsigned Idx = MCP.getConstantPoolIndex(CPV, Align); |
| 632 | |
| 633 | // Load value. |
| 634 | MachineInstrBuilder MIB; |
| 635 | if (isThumb2) { |
| 636 | unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic; |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 637 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), |
| 638 | DestReg).addConstantPoolIndex(Idx); |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 639 | if (RelocM == Reloc::PIC_) |
| 640 | MIB.addImm(Id); |
Jush Lu | e87e559 | 2012-08-29 02:41:21 +0000 | [diff] [blame] | 641 | AddOptionalDefs(MIB); |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 642 | } else { |
| 643 | // The extra immediate is for addrmode2. |
Jim Grosbach | 5f71aab | 2013-08-26 20:07:29 +0000 | [diff] [blame] | 644 | DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 645 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 646 | TII.get(ARM::LDRcp), DestReg) |
| 647 | .addConstantPoolIndex(Idx) |
| 648 | .addImm(0); |
Jush Lu | e87e559 | 2012-08-29 02:41:21 +0000 | [diff] [blame] | 649 | AddOptionalDefs(MIB); |
| 650 | |
| 651 | if (RelocM == Reloc::PIC_) { |
| 652 | unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD; |
| 653 | unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 654 | |
| 655 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 656 | DbgLoc, TII.get(Opc), NewDestReg) |
Jush Lu | e87e559 | 2012-08-29 02:41:21 +0000 | [diff] [blame] | 657 | .addReg(DestReg) |
| 658 | .addImm(Id); |
| 659 | AddOptionalDefs(MIB); |
| 660 | return NewDestReg; |
| 661 | } |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 662 | } |
Eric Christopher | 7787f79 | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 663 | } |
Eli Friedman | 8658579 | 2011-06-03 01:13:19 +0000 | [diff] [blame] | 664 | |
Jush Lu | e87e559 | 2012-08-29 02:41:21 +0000 | [diff] [blame] | 665 | if (IsIndirect) { |
Jakob Stoklund Olesen | 68f034e | 2012-01-07 01:47:05 +0000 | [diff] [blame] | 666 | MachineInstrBuilder MIB; |
Eli Friedman | 8658579 | 2011-06-03 01:13:19 +0000 | [diff] [blame] | 667 | unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT)); |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 668 | if (isThumb2) |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 669 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Jim Grosbach | e7e2aca | 2011-09-13 20:30:37 +0000 | [diff] [blame] | 670 | TII.get(ARM::t2LDRi12), NewDestReg) |
Eli Friedman | 8658579 | 2011-06-03 01:13:19 +0000 | [diff] [blame] | 671 | .addReg(DestReg) |
| 672 | .addImm(0); |
| 673 | else |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 674 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 675 | TII.get(ARM::LDRi12), NewDestReg) |
| 676 | .addReg(DestReg) |
| 677 | .addImm(0); |
Eli Friedman | 8658579 | 2011-06-03 01:13:19 +0000 | [diff] [blame] | 678 | DestReg = NewDestReg; |
| 679 | AddOptionalDefs(MIB); |
| 680 | } |
| 681 | |
Eric Christopher | 7787f79 | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 682 | return DestReg; |
Eric Christopher | 83a5ec8 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 685 | unsigned ARMFastISel::fastMaterializeConstant(const Constant *C) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 686 | EVT CEVT = TLI.getValueType(DL, C->getType(), true); |
Patrik Hagglund | c494d24 | 2012-12-17 14:30:06 +0000 | [diff] [blame] | 687 | |
| 688 | // Only handle simple types. |
| 689 | if (!CEVT.isSimple()) return 0; |
| 690 | MVT VT = CEVT.getSimpleVT(); |
Eric Christopher | 3cf63f1 | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 691 | |
| 692 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
| 693 | return ARMMaterializeFP(CFP, VT); |
Eric Christopher | 83a5ec8 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 694 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 695 | return ARMMaterializeGV(GV, VT); |
| 696 | else if (isa<ConstantInt>(C)) |
| 697 | return ARMMaterializeInt(C, VT); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 698 | |
Eric Christopher | 83a5ec8 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 699 | return 0; |
Eric Christopher | 3cf63f1 | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Chad Rosier | 0eff3e5 | 2011-11-17 21:46:13 +0000 | [diff] [blame] | 702 | // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF); |
| 703 | |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 704 | unsigned ARMFastISel::fastMaterializeAlloca(const AllocaInst *AI) { |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 705 | // Don't handle dynamic allocas. |
| 706 | if (!FuncInfo.StaticAllocaMap.count(AI)) return 0; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 707 | |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 708 | MVT VT; |
Chad Rosier | 466d3d8 | 2012-05-11 16:41:38 +0000 | [diff] [blame] | 709 | if (!isLoadTypeLegal(AI->getType(), VT)) return 0; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 710 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 711 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 712 | FuncInfo.StaticAllocaMap.find(AI); |
| 713 | |
| 714 | // This will get lowered later into the correct offsets and registers |
| 715 | // via rewriteXFrameIndex. |
| 716 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
Tim Northover | 76fc8a4 | 2013-12-11 16:04:57 +0000 | [diff] [blame] | 717 | unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri; |
Craig Topper | 760b134 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 718 | const TargetRegisterClass* RC = TLI.getRegClassFor(VT); |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 719 | unsigned ResultReg = createResultReg(RC); |
Tim Northover | 76fc8a4 | 2013-12-11 16:04:57 +0000 | [diff] [blame] | 720 | ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0); |
| 721 | |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 722 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 723 | TII.get(Opc), ResultReg) |
| 724 | .addFrameIndex(SI->second) |
| 725 | .addImm(0)); |
| 726 | return ResultReg; |
| 727 | } |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 728 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 729 | return 0; |
| 730 | } |
| 731 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 732 | bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 733 | EVT evt = TLI.getValueType(DL, Ty, true); |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 734 | |
Eric Christopher | 761e7fb | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 735 | // Only handle simple types. |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 736 | if (evt == MVT::Other || !evt.isSimple()) return false; |
| 737 | VT = evt.getSimpleVT(); |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 738 | |
Eric Christopher | 901176a | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 739 | // Handle all legal types, i.e. a register that will directly hold this |
| 740 | // value. |
| 741 | return TLI.isTypeLegal(VT); |
Eric Christopher | 761e7fb | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 744 | bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) { |
Eric Christopher | 3ce9c4a | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 745 | if (isTypeLegal(Ty, VT)) return true; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 746 | |
Eric Christopher | 3ce9c4a | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 747 | // If this is a type than can be sign or zero-extended to a basic operation |
| 748 | // go ahead and accept it now. |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 749 | if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) |
Eric Christopher | 3ce9c4a | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 750 | return true; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 751 | |
Eric Christopher | 3ce9c4a | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 752 | return false; |
| 753 | } |
| 754 | |
Eric Christopher | 558b61e | 2010-11-19 22:36:41 +0000 | [diff] [blame] | 755 | // Computes the address to get to an object. |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 756 | bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) { |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 757 | // Some boilerplate from the X86 FastISel. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 758 | const User *U = nullptr; |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 759 | unsigned Opcode = Instruction::UserOp1; |
Eric Christopher | 9d4e471 | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 760 | if (const Instruction *I = dyn_cast<Instruction>(Obj)) { |
Eric Christopher | cee83d6 | 2010-11-19 22:37:58 +0000 | [diff] [blame] | 761 | // Don't walk into other basic blocks unless the object is an alloca from |
| 762 | // another block, otherwise it may not have a virtual register assigned. |
Eric Christopher | 9649437 | 2010-11-15 21:11:06 +0000 | [diff] [blame] | 763 | if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) || |
| 764 | FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { |
| 765 | Opcode = I->getOpcode(); |
| 766 | U = I; |
| 767 | } |
Eric Christopher | 9d4e471 | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 768 | } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 769 | Opcode = C->getOpcode(); |
| 770 | U = C; |
| 771 | } |
| 772 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 773 | if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 774 | if (Ty->getAddressSpace() > 255) |
| 775 | // Fast instruction selection doesn't support the special |
| 776 | // address spaces. |
| 777 | return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 778 | |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 779 | switch (Opcode) { |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 780 | default: |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 781 | break; |
Eric Christopher | 3931cf9 | 2013-07-12 22:08:24 +0000 | [diff] [blame] | 782 | case Instruction::BitCast: |
Eric Christopher | db3bcc9 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 783 | // Look through bitcasts. |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 784 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 3931cf9 | 2013-07-12 22:08:24 +0000 | [diff] [blame] | 785 | case Instruction::IntToPtr: |
Eric Christopher | db3bcc9 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 786 | // Look past no-op inttoptrs. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 787 | if (TLI.getValueType(DL, U->getOperand(0)->getType()) == |
| 788 | TLI.getPointerTy(DL)) |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 789 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | db3bcc9 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 790 | break; |
Eric Christopher | 3931cf9 | 2013-07-12 22:08:24 +0000 | [diff] [blame] | 791 | case Instruction::PtrToInt: |
Eric Christopher | db3bcc9 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 792 | // Look past no-op ptrtoints. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 793 | if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL)) |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 794 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | db3bcc9 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 795 | break; |
Eric Christopher | 21d0c17 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 796 | case Instruction::GetElementPtr: { |
Eric Christopher | 35e2d7f | 2010-11-19 22:39:56 +0000 | [diff] [blame] | 797 | Address SavedAddr = Addr; |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 798 | int TmpOffset = Addr.Offset; |
Eric Christopher | e4b3d6b | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 799 | |
Eric Christopher | 21d0c17 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 800 | // Iterate through the GEP folding the constants into offsets where |
| 801 | // we can. |
| 802 | gep_type_iterator GTI = gep_type_begin(U); |
| 803 | for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); |
| 804 | i != e; ++i, ++GTI) { |
| 805 | const Value *Op = *i; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 806 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 807 | const StructLayout *SL = DL.getStructLayout(STy); |
Eric Christopher | 21d0c17 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 808 | unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); |
| 809 | TmpOffset += SL->getElementOffset(Idx); |
| 810 | } else { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 811 | uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType()); |
Eric Christopher | a5a779e | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 812 | for (;;) { |
Eric Christopher | e4b3d6b | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 813 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { |
| 814 | // Constant-offset addressing. |
| 815 | TmpOffset += CI->getSExtValue() * S; |
Eric Christopher | a5a779e | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 816 | break; |
| 817 | } |
Bob Wilson | 9f3e6b2 | 2013-11-15 19:09:27 +0000 | [diff] [blame] | 818 | if (canFoldAddIntoGEP(U, Op)) { |
| 819 | // A compatible add with a constant operand. Fold the constant. |
Eric Christopher | e4b3d6b | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 820 | ConstantInt *CI = |
Eric Christopher | a5a779e | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 821 | cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); |
Eric Christopher | e4b3d6b | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 822 | TmpOffset += CI->getSExtValue() * S; |
Eric Christopher | a5a779e | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 823 | // Iterate on the other operand. |
| 824 | Op = cast<AddOperator>(Op)->getOperand(0); |
| 825 | continue; |
Eric Christopher | 501d2e2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 826 | } |
Eric Christopher | a5a779e | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 827 | // Unsupported |
| 828 | goto unsupported_gep; |
| 829 | } |
Eric Christopher | 21d0c17 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 830 | } |
| 831 | } |
Eric Christopher | e4b3d6b | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 832 | |
| 833 | // Try to grab the base operand now. |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 834 | Addr.Offset = TmpOffset; |
| 835 | if (ARMComputeAddress(U->getOperand(0), Addr)) return true; |
Eric Christopher | e4b3d6b | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 836 | |
| 837 | // We failed, restore everything and try the other options. |
Eric Christopher | 35e2d7f | 2010-11-19 22:39:56 +0000 | [diff] [blame] | 838 | Addr = SavedAddr; |
Eric Christopher | e4b3d6b | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 839 | |
Eric Christopher | 21d0c17 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 840 | unsupported_gep: |
Eric Christopher | 21d0c17 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 841 | break; |
| 842 | } |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 843 | case Instruction::Alloca: { |
Eric Christopher | 7cd5cda | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 844 | const AllocaInst *AI = cast<AllocaInst>(Obj); |
Eric Christopher | 0a3c28b | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 845 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 846 | FuncInfo.StaticAllocaMap.find(AI); |
| 847 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 848 | Addr.BaseType = Address::FrameIndexBase; |
| 849 | Addr.Base.FI = SI->second; |
| 850 | return true; |
| 851 | } |
| 852 | break; |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 855 | |
Eric Christopher | 9d4e471 | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 856 | // Try to get this in a register if nothing else has worked. |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 857 | if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj); |
| 858 | return Addr.Base.Reg != 0; |
Eric Christopher | 21d0c17 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Chad Rosier | 150d35b | 2012-12-17 22:35:29 +0000 | [diff] [blame] | 861 | void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) { |
Eric Christopher | 73bc5b0 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 862 | bool needsLowering = false; |
Chad Rosier | 150d35b | 2012-12-17 22:35:29 +0000 | [diff] [blame] | 863 | switch (VT.SimpleTy) { |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 864 | default: llvm_unreachable("Unhandled load/store type!"); |
Eric Christopher | 73bc5b0 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 865 | case MVT::i1: |
| 866 | case MVT::i8: |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 867 | case MVT::i16: |
Eric Christopher | 73bc5b0 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 868 | case MVT::i32: |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 869 | if (!useAM3) { |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 870 | // Integer loads/stores handle 12-bit offsets. |
| 871 | needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset); |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 872 | // Handle negative offsets. |
Chad Rosier | 45110fd | 2011-11-14 22:34:48 +0000 | [diff] [blame] | 873 | if (needsLowering && isThumb2) |
| 874 | needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 && |
| 875 | Addr.Offset > -256); |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 876 | } else { |
Chad Rosier | 5196efd | 2011-11-13 04:25:02 +0000 | [diff] [blame] | 877 | // ARM halfword load/stores and signed byte loads use +/-imm8 offsets. |
Chad Rosier | 2a1df88 | 2011-11-14 04:09:28 +0000 | [diff] [blame] | 878 | needsLowering = (Addr.Offset > 255 || Addr.Offset < -255); |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 879 | } |
Eric Christopher | 73bc5b0 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 880 | break; |
| 881 | case MVT::f32: |
| 882 | case MVT::f64: |
| 883 | // Floating point operands handle 8-bit offsets. |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 884 | needsLowering = ((Addr.Offset & 0xff) != Addr.Offset); |
Eric Christopher | 73bc5b0 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 885 | break; |
| 886 | } |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 887 | |
Eric Christopher | 0a3c28b | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 888 | // If this is a stack pointer and the offset needs to be simplified then |
| 889 | // put the alloca address into a register, set the base type back to |
| 890 | // register and continue. This should almost never happen. |
| 891 | if (needsLowering && Addr.BaseType == Address::FrameIndexBase) { |
Craig Topper | 61e88f4 | 2014-11-21 05:58:21 +0000 | [diff] [blame] | 892 | const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass |
| 893 | : &ARM::GPRRegClass; |
Eric Christopher | 0a3c28b | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 894 | unsigned ResultReg = createResultReg(RC); |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 895 | unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri; |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 896 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 0a3c28b | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 897 | TII.get(Opc), ResultReg) |
| 898 | .addFrameIndex(Addr.Base.FI) |
| 899 | .addImm(0)); |
| 900 | Addr.Base.Reg = ResultReg; |
| 901 | Addr.BaseType = Address::RegBase; |
| 902 | } |
| 903 | |
Eric Christopher | 73bc5b0 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 904 | // Since the offset is too large for the load/store instruction |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 905 | // get the reg+offset into a register. |
Eric Christopher | 73bc5b0 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 906 | if (needsLowering) { |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 907 | Addr.Base.Reg = fastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg, |
Eli Friedman | 86caced | 2011-04-29 21:22:56 +0000 | [diff] [blame] | 908 | /*Op0IsKill*/false, Addr.Offset, MVT::i32); |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 909 | Addr.Offset = 0; |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 910 | } |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Chad Rosier | 150d35b | 2012-12-17 22:35:29 +0000 | [diff] [blame] | 913 | void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr, |
Cameron Zwarich | 6528a54 | 2011-05-28 20:34:49 +0000 | [diff] [blame] | 914 | const MachineInstrBuilder &MIB, |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 915 | unsigned Flags, bool useAM3) { |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 916 | // addrmode5 output depends on the selection dag addressing dividing the |
| 917 | // offset by 4 that it then later multiplies. Do this here as well. |
Chad Rosier | 150d35b | 2012-12-17 22:35:29 +0000 | [diff] [blame] | 918 | if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64) |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 919 | Addr.Offset /= 4; |
Eric Christopher | 501d2e2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 920 | |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 921 | // Frame base works a bit differently. Handle it separately. |
| 922 | if (Addr.BaseType == Address::FrameIndexBase) { |
| 923 | int FI = Addr.Base.FI; |
| 924 | int Offset = Addr.Offset; |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 925 | MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand( |
| 926 | MachinePointerInfo::getFixedStack(*FuncInfo.MF, FI, Offset), Flags, |
| 927 | MFI.getObjectSize(FI), MFI.getObjectAlignment(FI)); |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 928 | // Now add the rest of the operands. |
| 929 | MIB.addFrameIndex(FI); |
| 930 | |
Bob Wilson | 80381f6 | 2011-12-04 00:52:23 +0000 | [diff] [blame] | 931 | // ARM halfword load/stores and signed byte loads need an additional |
| 932 | // operand. |
Chad Rosier | 2a1df88 | 2011-11-14 04:09:28 +0000 | [diff] [blame] | 933 | if (useAM3) { |
| 934 | signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset; |
| 935 | MIB.addReg(0); |
| 936 | MIB.addImm(Imm); |
| 937 | } else { |
| 938 | MIB.addImm(Addr.Offset); |
| 939 | } |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 940 | MIB.addMemOperand(MMO); |
| 941 | } else { |
| 942 | // Now add the rest of the operands. |
| 943 | MIB.addReg(Addr.Base.Reg); |
Eric Christopher | 501d2e2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 944 | |
Bob Wilson | 80381f6 | 2011-12-04 00:52:23 +0000 | [diff] [blame] | 945 | // ARM halfword load/stores and signed byte loads need an additional |
| 946 | // operand. |
Chad Rosier | 2a1df88 | 2011-11-14 04:09:28 +0000 | [diff] [blame] | 947 | if (useAM3) { |
| 948 | signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset; |
| 949 | MIB.addReg(0); |
| 950 | MIB.addImm(Imm); |
| 951 | } else { |
| 952 | MIB.addImm(Addr.Offset); |
| 953 | } |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 954 | } |
| 955 | AddOptionalDefs(MIB); |
| 956 | } |
| 957 | |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 958 | bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr, |
Chad Rosier | 563de60 | 2011-12-13 19:22:14 +0000 | [diff] [blame] | 959 | unsigned Alignment, bool isZExt, bool allocReg) { |
Eric Christopher | 901176a | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 960 | unsigned Opc; |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 961 | bool useAM3 = false; |
Chad Rosier | 563de60 | 2011-12-13 19:22:14 +0000 | [diff] [blame] | 962 | bool needVMOV = false; |
Craig Topper | 760b134 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 963 | const TargetRegisterClass *RC; |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 964 | switch (VT.SimpleTy) { |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 965 | // This is mostly going to be Neon/vector support. |
| 966 | default: return false; |
Chad Rosier | 023ede5 | 2011-11-11 02:38:59 +0000 | [diff] [blame] | 967 | case MVT::i1: |
Eric Christopher | 3ce9c4a | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 968 | case MVT::i8: |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 969 | if (isThumb2) { |
| 970 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 971 | Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8; |
| 972 | else |
| 973 | Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12; |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 974 | } else { |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 975 | if (isZExt) { |
| 976 | Opc = ARM::LDRBi12; |
| 977 | } else { |
| 978 | Opc = ARM::LDRSB; |
| 979 | useAM3 = true; |
| 980 | } |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 981 | } |
JF Bastien | 652fa6a | 2013-06-09 00:20:24 +0000 | [diff] [blame] | 982 | RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass; |
Eric Christopher | 3ce9c4a | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 983 | break; |
Chad Rosier | 2f27fab | 2011-11-09 21:30:12 +0000 | [diff] [blame] | 984 | case MVT::i16: |
Chad Rosier | 66bb178 | 2012-11-09 18:25:27 +0000 | [diff] [blame] | 985 | if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem()) |
Chad Rosier | 2364f58 | 2012-09-21 00:41:42 +0000 | [diff] [blame] | 986 | return false; |
| 987 | |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 988 | if (isThumb2) { |
| 989 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 990 | Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8; |
| 991 | else |
| 992 | Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12; |
| 993 | } else { |
| 994 | Opc = isZExt ? ARM::LDRH : ARM::LDRSH; |
| 995 | useAM3 = true; |
| 996 | } |
JF Bastien | 652fa6a | 2013-06-09 00:20:24 +0000 | [diff] [blame] | 997 | RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass; |
Chad Rosier | 2f27fab | 2011-11-09 21:30:12 +0000 | [diff] [blame] | 998 | break; |
Eric Christopher | 901176a | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 999 | case MVT::i32: |
Chad Rosier | 66bb178 | 2012-11-09 18:25:27 +0000 | [diff] [blame] | 1000 | if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem()) |
Chad Rosier | 8bf01fc | 2012-09-21 16:58:35 +0000 | [diff] [blame] | 1001 | return false; |
| 1002 | |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 1003 | if (isThumb2) { |
| 1004 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 1005 | Opc = ARM::t2LDRi8; |
| 1006 | else |
| 1007 | Opc = ARM::t2LDRi12; |
| 1008 | } else { |
| 1009 | Opc = ARM::LDRi12; |
| 1010 | } |
JF Bastien | 652fa6a | 2013-06-09 00:20:24 +0000 | [diff] [blame] | 1011 | RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass; |
Eric Christopher | 901176a | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 1012 | break; |
Eric Christopher | aef6499b | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 1013 | case MVT::f32: |
Chad Rosier | ded6160 | 2011-12-14 17:55:03 +0000 | [diff] [blame] | 1014 | if (!Subtarget->hasVFP2()) return false; |
Chad Rosier | 563de60 | 2011-12-13 19:22:14 +0000 | [diff] [blame] | 1015 | // Unaligned loads need special handling. Floats require word-alignment. |
| 1016 | if (Alignment && Alignment < 4) { |
| 1017 | needVMOV = true; |
| 1018 | VT = MVT::i32; |
| 1019 | Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12; |
JF Bastien | 652fa6a | 2013-06-09 00:20:24 +0000 | [diff] [blame] | 1020 | RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass; |
Chad Rosier | 563de60 | 2011-12-13 19:22:14 +0000 | [diff] [blame] | 1021 | } else { |
| 1022 | Opc = ARM::VLDRS; |
| 1023 | RC = TLI.getRegClassFor(VT); |
| 1024 | } |
Eric Christopher | aef6499b | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 1025 | break; |
| 1026 | case MVT::f64: |
Chad Rosier | ded6160 | 2011-12-14 17:55:03 +0000 | [diff] [blame] | 1027 | if (!Subtarget->hasVFP2()) return false; |
Chad Rosier | a26979b | 2011-12-14 17:26:05 +0000 | [diff] [blame] | 1028 | // FIXME: Unaligned loads need special handling. Doublewords require |
| 1029 | // word-alignment. |
| 1030 | if (Alignment && Alignment < 4) |
Chad Rosier | 563de60 | 2011-12-13 19:22:14 +0000 | [diff] [blame] | 1031 | return false; |
Chad Rosier | a26979b | 2011-12-14 17:26:05 +0000 | [diff] [blame] | 1032 | |
Eric Christopher | aef6499b | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 1033 | Opc = ARM::VLDRD; |
Eric Christopher | a2583ea | 2010-10-07 05:50:44 +0000 | [diff] [blame] | 1034 | RC = TLI.getRegClassFor(VT); |
Eric Christopher | aef6499b | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 1035 | break; |
Eric Christopher | 761e7fb | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 1036 | } |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1037 | // Simplify this down to something we can handle. |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1038 | ARMSimplifyAddress(Addr, VT, useAM3); |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1039 | |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1040 | // Create the base instruction, then add the operands. |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1041 | if (allocReg) |
| 1042 | ResultReg = createResultReg(RC); |
| 1043 | assert (ResultReg > 255 && "Expected an allocated virtual register."); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1044 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1045 | TII.get(Opc), ResultReg); |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1046 | AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3); |
Chad Rosier | 563de60 | 2011-12-13 19:22:14 +0000 | [diff] [blame] | 1047 | |
| 1048 | // If we had an unaligned load of a float we've converted it to an regular |
| 1049 | // load. Now we must move from the GRP to the FP register. |
| 1050 | if (needVMOV) { |
| 1051 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1052 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Chad Rosier | 563de60 | 2011-12-13 19:22:14 +0000 | [diff] [blame] | 1053 | TII.get(ARM::VMOVSR), MoveReg) |
| 1054 | .addReg(ResultReg)); |
| 1055 | ResultReg = MoveReg; |
| 1056 | } |
Eric Christopher | 901176a | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 1057 | return true; |
Eric Christopher | 761e7fb | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1060 | bool ARMFastISel::SelectLoad(const Instruction *I) { |
Eli Friedman | f3dd6da | 2011-09-02 22:33:24 +0000 | [diff] [blame] | 1061 | // Atomic loads need special handling. |
| 1062 | if (cast<LoadInst>(I)->isAtomic()) |
| 1063 | return false; |
| 1064 | |
Eric Christopher | 860fc93 | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1065 | // Verify we have a legal type before going any further. |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1066 | MVT VT; |
Eric Christopher | 860fc93 | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1067 | if (!isLoadTypeLegal(I->getType(), VT)) |
| 1068 | return false; |
| 1069 | |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1070 | // See if we can handle this address. |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1071 | Address Addr; |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1072 | if (!ARMComputeAddress(I->getOperand(0), Addr)) return false; |
Eric Christopher | 860fc93 | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1073 | |
| 1074 | unsigned ResultReg; |
Chad Rosier | 563de60 | 2011-12-13 19:22:14 +0000 | [diff] [blame] | 1075 | if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment())) |
| 1076 | return false; |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1077 | updateValueMap(I, ResultReg); |
Eric Christopher | 860fc93 | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1078 | return true; |
| 1079 | } |
| 1080 | |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 1081 | bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr, |
Bob Wilson | 80381f6 | 2011-12-04 00:52:23 +0000 | [diff] [blame] | 1082 | unsigned Alignment) { |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1083 | unsigned StrOpc; |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1084 | bool useAM3 = false; |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 1085 | switch (VT.SimpleTy) { |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1086 | // This is mostly going to be Neon/vector support. |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1087 | default: return false; |
Eric Christopher | 1e43892e | 2010-11-02 23:59:09 +0000 | [diff] [blame] | 1088 | case MVT::i1: { |
Craig Topper | 61e88f4 | 2014-11-21 05:58:21 +0000 | [diff] [blame] | 1089 | unsigned Res = createResultReg(isThumb2 ? &ARM::tGPRRegClass |
| 1090 | : &ARM::GPRRegClass); |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1091 | unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri; |
Joey Gouly | c7cda1c | 2013-08-23 15:20:56 +0000 | [diff] [blame] | 1092 | SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1093 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 1e43892e | 2010-11-02 23:59:09 +0000 | [diff] [blame] | 1094 | TII.get(Opc), Res) |
| 1095 | .addReg(SrcReg).addImm(1)); |
| 1096 | SrcReg = Res; |
| 1097 | } // Fallthrough here. |
Eric Christopher | e4b3d6b | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 1098 | case MVT::i8: |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 1099 | if (isThumb2) { |
| 1100 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 1101 | StrOpc = ARM::t2STRBi8; |
| 1102 | else |
| 1103 | StrOpc = ARM::t2STRBi12; |
| 1104 | } else { |
| 1105 | StrOpc = ARM::STRBi12; |
| 1106 | } |
Eric Christopher | 7cd5cda | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 1107 | break; |
| 1108 | case MVT::i16: |
Chad Rosier | 66bb178 | 2012-11-09 18:25:27 +0000 | [diff] [blame] | 1109 | if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem()) |
Chad Rosier | 2364f58 | 2012-09-21 00:41:42 +0000 | [diff] [blame] | 1110 | return false; |
| 1111 | |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 1112 | if (isThumb2) { |
| 1113 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 1114 | StrOpc = ARM::t2STRHi8; |
| 1115 | else |
| 1116 | StrOpc = ARM::t2STRHi12; |
| 1117 | } else { |
| 1118 | StrOpc = ARM::STRH; |
| 1119 | useAM3 = true; |
| 1120 | } |
Eric Christopher | 7cd5cda | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 1121 | break; |
Eric Christopher | c918d55 | 2010-10-16 01:10:35 +0000 | [diff] [blame] | 1122 | case MVT::i32: |
Chad Rosier | 66bb178 | 2012-11-09 18:25:27 +0000 | [diff] [blame] | 1123 | if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem()) |
Chad Rosier | 8bf01fc | 2012-09-21 16:58:35 +0000 | [diff] [blame] | 1124 | return false; |
| 1125 | |
Chad Rosier | adfd200 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 1126 | if (isThumb2) { |
| 1127 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 1128 | StrOpc = ARM::t2STRi8; |
| 1129 | else |
| 1130 | StrOpc = ARM::t2STRi12; |
| 1131 | } else { |
| 1132 | StrOpc = ARM::STRi12; |
| 1133 | } |
Eric Christopher | c918d55 | 2010-10-16 01:10:35 +0000 | [diff] [blame] | 1134 | break; |
Eric Christopher | c3e118e | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 1135 | case MVT::f32: |
| 1136 | if (!Subtarget->hasVFP2()) return false; |
Chad Rosier | c77830d | 2011-12-06 01:44:17 +0000 | [diff] [blame] | 1137 | // Unaligned stores need special handling. Floats require word-alignment. |
Chad Rosier | ec3b77e | 2011-12-03 02:21:57 +0000 | [diff] [blame] | 1138 | if (Alignment && Alignment < 4) { |
| 1139 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1140 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Chad Rosier | ec3b77e | 2011-12-03 02:21:57 +0000 | [diff] [blame] | 1141 | TII.get(ARM::VMOVRS), MoveReg) |
| 1142 | .addReg(SrcReg)); |
| 1143 | SrcReg = MoveReg; |
| 1144 | VT = MVT::i32; |
| 1145 | StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12; |
Chad Rosier | fce2891 | 2011-12-14 17:32:02 +0000 | [diff] [blame] | 1146 | } else { |
| 1147 | StrOpc = ARM::VSTRS; |
Chad Rosier | ec3b77e | 2011-12-03 02:21:57 +0000 | [diff] [blame] | 1148 | } |
Eric Christopher | c3e118e | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | case MVT::f64: |
| 1151 | if (!Subtarget->hasVFP2()) return false; |
Chad Rosier | c77830d | 2011-12-06 01:44:17 +0000 | [diff] [blame] | 1152 | // FIXME: Unaligned stores need special handling. Doublewords require |
| 1153 | // word-alignment. |
Chad Rosier | a26979b | 2011-12-14 17:26:05 +0000 | [diff] [blame] | 1154 | if (Alignment && Alignment < 4) |
Chad Rosier | ec3b77e | 2011-12-03 02:21:57 +0000 | [diff] [blame] | 1155 | return false; |
Chad Rosier | a26979b | 2011-12-14 17:26:05 +0000 | [diff] [blame] | 1156 | |
Eric Christopher | c3e118e | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 1157 | StrOpc = ARM::VSTRD; |
| 1158 | break; |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1159 | } |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1160 | // Simplify this down to something we can handle. |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1161 | ARMSimplifyAddress(Addr, VT, useAM3); |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1162 | |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1163 | // Create the base instruction, then add the operands. |
Joey Gouly | c7cda1c | 2013-08-23 15:20:56 +0000 | [diff] [blame] | 1164 | SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1165 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1166 | TII.get(StrOpc)) |
Chad Rosier | ce619dd | 2011-11-17 01:16:53 +0000 | [diff] [blame] | 1167 | .addReg(SrcReg); |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1168 | AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3); |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1169 | return true; |
| 1170 | } |
| 1171 | |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1172 | bool ARMFastISel::SelectStore(const Instruction *I) { |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1173 | Value *Op0 = I->getOperand(0); |
| 1174 | unsigned SrcReg = 0; |
| 1175 | |
Eli Friedman | f3dd6da | 2011-09-02 22:33:24 +0000 | [diff] [blame] | 1176 | // Atomic stores need special handling. |
| 1177 | if (cast<StoreInst>(I)->isAtomic()) |
| 1178 | return false; |
| 1179 | |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1180 | // Verify we have a legal type before going any further. |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1181 | MVT VT; |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1182 | if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) |
Eric Christopher | fde5a3d | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 1183 | return false; |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1184 | |
Eric Christopher | 92db201 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 1185 | // Get the value to be stored into a register. |
| 1186 | SrcReg = getRegForValue(Op0); |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1187 | if (SrcReg == 0) return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1188 | |
Eric Christopher | 119ff7f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1189 | // See if we can handle this address. |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1190 | Address Addr; |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1191 | if (!ARMComputeAddress(I->getOperand(1), Addr)) |
Eric Christopher | 74487fc | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1192 | return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1193 | |
Chad Rosier | ec3b77e | 2011-12-03 02:21:57 +0000 | [diff] [blame] | 1194 | if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment())) |
| 1195 | return false; |
Eric Christopher | 2ccc1aa | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1196 | return true; |
| 1197 | } |
| 1198 | |
| 1199 | static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) { |
| 1200 | switch (Pred) { |
| 1201 | // Needs two compares... |
| 1202 | case CmpInst::FCMP_ONE: |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1203 | case CmpInst::FCMP_UEQ: |
Eric Christopher | 2ccc1aa | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1204 | default: |
Eric Christopher | b2abb50 | 2010-11-02 01:24:49 +0000 | [diff] [blame] | 1205 | // AL is our "false" for now. The other two need more compares. |
Eric Christopher | 2ccc1aa | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1206 | return ARMCC::AL; |
| 1207 | case CmpInst::ICMP_EQ: |
| 1208 | case CmpInst::FCMP_OEQ: |
| 1209 | return ARMCC::EQ; |
| 1210 | case CmpInst::ICMP_SGT: |
| 1211 | case CmpInst::FCMP_OGT: |
| 1212 | return ARMCC::GT; |
| 1213 | case CmpInst::ICMP_SGE: |
| 1214 | case CmpInst::FCMP_OGE: |
| 1215 | return ARMCC::GE; |
| 1216 | case CmpInst::ICMP_UGT: |
| 1217 | case CmpInst::FCMP_UGT: |
| 1218 | return ARMCC::HI; |
| 1219 | case CmpInst::FCMP_OLT: |
| 1220 | return ARMCC::MI; |
| 1221 | case CmpInst::ICMP_ULE: |
| 1222 | case CmpInst::FCMP_OLE: |
| 1223 | return ARMCC::LS; |
| 1224 | case CmpInst::FCMP_ORD: |
| 1225 | return ARMCC::VC; |
| 1226 | case CmpInst::FCMP_UNO: |
| 1227 | return ARMCC::VS; |
| 1228 | case CmpInst::FCMP_UGE: |
| 1229 | return ARMCC::PL; |
| 1230 | case CmpInst::ICMP_SLT: |
| 1231 | case CmpInst::FCMP_ULT: |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1232 | return ARMCC::LT; |
Eric Christopher | 2ccc1aa | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1233 | case CmpInst::ICMP_SLE: |
| 1234 | case CmpInst::FCMP_ULE: |
| 1235 | return ARMCC::LE; |
| 1236 | case CmpInst::FCMP_UNE: |
| 1237 | case CmpInst::ICMP_NE: |
| 1238 | return ARMCC::NE; |
| 1239 | case CmpInst::ICMP_UGE: |
| 1240 | return ARMCC::HS; |
| 1241 | case CmpInst::ICMP_ULT: |
| 1242 | return ARMCC::LO; |
| 1243 | } |
Eric Christopher | fde5a3d | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1246 | bool ARMFastISel::SelectBranch(const Instruction *I) { |
Eric Christopher | 6aaed72 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1247 | const BranchInst *BI = cast<BranchInst>(I); |
| 1248 | MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; |
| 1249 | MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1250 | |
Eric Christopher | 6aaed72 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1251 | // Simple branch support. |
Jim Grosbach | 68147ee | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1252 | |
Eric Christopher | 5c308f8 | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1253 | // If we can, avoid recomputing the compare - redoing it could lead to wonky |
| 1254 | // behavior. |
Eric Christopher | 5c308f8 | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1255 | if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { |
Chad Rosier | eafbf3f | 2011-10-26 23:17:28 +0000 | [diff] [blame] | 1256 | if (CI->hasOneUse() && (CI->getParent() == I->getParent())) { |
Eric Christopher | 5c308f8 | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1257 | |
| 1258 | // Get the compare predicate. |
Eric Christopher | 26b8ac4 | 2011-04-29 21:56:31 +0000 | [diff] [blame] | 1259 | // Try to take advantage of fallthrough opportunities. |
| 1260 | CmpInst::Predicate Predicate = CI->getPredicate(); |
| 1261 | if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { |
| 1262 | std::swap(TBB, FBB); |
| 1263 | Predicate = CmpInst::getInversePredicate(Predicate); |
| 1264 | } |
| 1265 | |
| 1266 | ARMCC::CondCodes ARMPred = getComparePred(Predicate); |
Eric Christopher | 5c308f8 | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1267 | |
| 1268 | // We may not handle every CC for now. |
| 1269 | if (ARMPred == ARMCC::AL) return false; |
| 1270 | |
Chad Rosier | eafbf3f | 2011-10-26 23:17:28 +0000 | [diff] [blame] | 1271 | // Emit the compare. |
David Blaikie | 3ef249c9 | 2015-01-30 23:04:39 +0000 | [diff] [blame] | 1272 | if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned())) |
Chad Rosier | eafbf3f | 2011-10-26 23:17:28 +0000 | [diff] [blame] | 1273 | return false; |
Jim Grosbach | 68147ee | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1274 | |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1275 | unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1276 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc)) |
Eric Christopher | 5c308f8 | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1277 | .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR); |
Matthias Braun | ccfc9c8 | 2015-08-26 01:55:47 +0000 | [diff] [blame] | 1278 | finishCondBranch(BI->getParent(), TBB, FBB); |
Eric Christopher | 5c308f8 | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1279 | return true; |
| 1280 | } |
Eric Christopher | 8d46b47 | 2011-04-29 20:02:39 +0000 | [diff] [blame] | 1281 | } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) { |
| 1282 | MVT SourceVT; |
| 1283 | if (TI->hasOneUse() && TI->getParent() == I->getParent() && |
Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 1284 | (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) { |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1285 | unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; |
Eric Christopher | 8d46b47 | 2011-04-29 20:02:39 +0000 | [diff] [blame] | 1286 | unsigned OpReg = getRegForValue(TI->getOperand(0)); |
Jim Grosbach | 667b147 | 2013-08-26 20:22:05 +0000 | [diff] [blame] | 1287 | OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1288 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 8d46b47 | 2011-04-29 20:02:39 +0000 | [diff] [blame] | 1289 | TII.get(TstOpc)) |
| 1290 | .addReg(OpReg).addImm(1)); |
| 1291 | |
| 1292 | unsigned CCMode = ARMCC::NE; |
| 1293 | if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { |
| 1294 | std::swap(TBB, FBB); |
| 1295 | CCMode = ARMCC::EQ; |
| 1296 | } |
| 1297 | |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1298 | unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1299 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc)) |
Eric Christopher | 8d46b47 | 2011-04-29 20:02:39 +0000 | [diff] [blame] | 1300 | .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR); |
| 1301 | |
Matthias Braun | ccfc9c8 | 2015-08-26 01:55:47 +0000 | [diff] [blame] | 1302 | finishCondBranch(BI->getParent(), TBB, FBB); |
Eric Christopher | 8d46b47 | 2011-04-29 20:02:39 +0000 | [diff] [blame] | 1303 | return true; |
| 1304 | } |
Chad Rosier | d24e7e1d | 2011-10-27 00:21:16 +0000 | [diff] [blame] | 1305 | } else if (const ConstantInt *CI = |
| 1306 | dyn_cast<ConstantInt>(BI->getCondition())) { |
| 1307 | uint64_t Imm = CI->getZExtValue(); |
| 1308 | MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB; |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1309 | fastEmitBranch(Target, DbgLoc); |
Chad Rosier | d24e7e1d | 2011-10-27 00:21:16 +0000 | [diff] [blame] | 1310 | return true; |
Eric Christopher | 5c308f8 | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1311 | } |
Jim Grosbach | 68147ee | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1312 | |
Eric Christopher | 5c308f8 | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1313 | unsigned CmpReg = getRegForValue(BI->getCondition()); |
| 1314 | if (CmpReg == 0) return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1315 | |
Stuart Hastings | ebddfe6 | 2011-04-16 03:31:26 +0000 | [diff] [blame] | 1316 | // We've been divorced from our compare! Our block was split, and |
| 1317 | // now our compare lives in a predecessor block. We musn't |
| 1318 | // re-compare here, as the children of the compare aren't guaranteed |
| 1319 | // live across the block boundary (we *could* check for this). |
| 1320 | // Regardless, the compare has been done in the predecessor block, |
| 1321 | // and it left a value for us in a virtual register. Ergo, we test |
| 1322 | // the one-bit value left in the virtual register. |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1323 | unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; |
Jim Grosbach | 667b147 | 2013-08-26 20:22:05 +0000 | [diff] [blame] | 1324 | CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1325 | AddOptionalDefs( |
| 1326 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc)) |
| 1327 | .addReg(CmpReg) |
| 1328 | .addImm(1)); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1329 | |
Eric Christopher | 4f012fd | 2011-04-28 16:52:09 +0000 | [diff] [blame] | 1330 | unsigned CCMode = ARMCC::NE; |
| 1331 | if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { |
| 1332 | std::swap(TBB, FBB); |
| 1333 | CCMode = ARMCC::EQ; |
| 1334 | } |
| 1335 | |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1336 | unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1337 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc)) |
Eric Christopher | 4f012fd | 2011-04-28 16:52:09 +0000 | [diff] [blame] | 1338 | .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR); |
Matthias Braun | ccfc9c8 | 2015-08-26 01:55:47 +0000 | [diff] [blame] | 1339 | finishCondBranch(BI->getParent(), TBB, FBB); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1340 | return true; |
Eric Christopher | 6aaed72 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Chad Rosier | ded4c99 | 2012-02-07 23:56:08 +0000 | [diff] [blame] | 1343 | bool ARMFastISel::SelectIndirectBr(const Instruction *I) { |
| 1344 | unsigned AddrReg = getRegForValue(I->getOperand(0)); |
| 1345 | if (AddrReg == 0) return false; |
| 1346 | |
| 1347 | unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX; |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1348 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1349 | TII.get(Opc)).addReg(AddrReg)); |
Bill Wendling | 12cda50 | 2012-10-22 23:30:04 +0000 | [diff] [blame] | 1350 | |
| 1351 | const IndirectBrInst *IB = cast<IndirectBrInst>(I); |
Pete Cooper | ebcd748 | 2015-08-06 20:22:46 +0000 | [diff] [blame] | 1352 | for (const BasicBlock *SuccBB : IB->successors()) |
| 1353 | FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]); |
Bill Wendling | 12cda50 | 2012-10-22 23:30:04 +0000 | [diff] [blame] | 1354 | |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 1355 | return true; |
Chad Rosier | ded4c99 | 2012-02-07 23:56:08 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Chad Rosier | 9cf803c | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1358 | bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value, |
David Blaikie | 3ef249c9 | 2015-01-30 23:04:39 +0000 | [diff] [blame] | 1359 | bool isZExt) { |
Chad Rosier | 78127d3 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1360 | Type *Ty = Src1Value->getType(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1361 | EVT SrcEVT = TLI.getValueType(DL, Ty, true); |
Patrik Hagglund | c494d24 | 2012-12-17 14:30:06 +0000 | [diff] [blame] | 1362 | if (!SrcEVT.isSimple()) return false; |
| 1363 | MVT SrcVT = SrcEVT.getSimpleVT(); |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1364 | |
Chad Rosier | 78127d3 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1365 | bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy()); |
| 1366 | if (isFloat && !Subtarget->hasVFP2()) |
Eric Christopher | c3e9c40 | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1367 | return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1368 | |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1369 | // Check to see if the 2nd operand is a constant that we can encode directly |
| 1370 | // in the compare. |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1371 | int Imm = 0; |
| 1372 | bool UseImm = false; |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1373 | bool isNegativeImm = false; |
Chad Rosier | af13d76 | 2011-11-16 00:32:20 +0000 | [diff] [blame] | 1374 | // FIXME: At -O0 we don't have anything that canonicalizes operand order. |
| 1375 | // Thus, Src1Value may be a ConstantInt, but we're missing it. |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1376 | if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) { |
| 1377 | if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 || |
| 1378 | SrcVT == MVT::i1) { |
| 1379 | const APInt &CIVal = ConstInt->getValue(); |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1380 | Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue(); |
Chad Rosier | 26d0588 | 2012-03-15 22:54:20 +0000 | [diff] [blame] | 1381 | // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather |
Jim Grosbach | 1a59711 | 2014-04-03 23:43:18 +0000 | [diff] [blame] | 1382 | // then a cmn, because there is no way to represent 2147483648 as a |
Chad Rosier | 26d0588 | 2012-03-15 22:54:20 +0000 | [diff] [blame] | 1383 | // signed 32-bit int. |
| 1384 | if (Imm < 0 && Imm != (int)0x80000000) { |
| 1385 | isNegativeImm = true; |
| 1386 | Imm = -Imm; |
Chad Rosier | 3fbd094 | 2011-11-10 01:30:39 +0000 | [diff] [blame] | 1387 | } |
Chad Rosier | 26d0588 | 2012-03-15 22:54:20 +0000 | [diff] [blame] | 1388 | UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : |
| 1389 | (ARM_AM::getSOImmVal(Imm) != -1); |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1390 | } |
| 1391 | } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) { |
| 1392 | if (SrcVT == MVT::f32 || SrcVT == MVT::f64) |
| 1393 | if (ConstFP->isZero() && !ConstFP->isNegative()) |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1394 | UseImm = true; |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Eric Christopher | c3e9c40 | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1397 | unsigned CmpOpc; |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1398 | bool isICmp = true; |
Chad Rosier | 9cf803c | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1399 | bool needsExt = false; |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 1400 | switch (SrcVT.SimpleTy) { |
Eric Christopher | c3e9c40 | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1401 | default: return false; |
| 1402 | // TODO: Verify compares. |
| 1403 | case MVT::f32: |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1404 | isICmp = false; |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1405 | CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES; |
Eric Christopher | c3e9c40 | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1406 | break; |
| 1407 | case MVT::f64: |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1408 | isICmp = false; |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1409 | CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED; |
Eric Christopher | c3e9c40 | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1410 | break; |
Chad Rosier | 9cf803c | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1411 | case MVT::i1: |
| 1412 | case MVT::i8: |
| 1413 | case MVT::i16: |
| 1414 | needsExt = true; |
| 1415 | // Intentional fall-through. |
Eric Christopher | c3e9c40 | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1416 | case MVT::i32: |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1417 | if (isThumb2) { |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1418 | if (!UseImm) |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1419 | CmpOpc = ARM::t2CMPrr; |
| 1420 | else |
Bill Wendling | 4b79647 | 2012-06-11 08:07:26 +0000 | [diff] [blame] | 1421 | CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri; |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1422 | } else { |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1423 | if (!UseImm) |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1424 | CmpOpc = ARM::CMPrr; |
| 1425 | else |
Bill Wendling | 4b79647 | 2012-06-11 08:07:26 +0000 | [diff] [blame] | 1426 | CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri; |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1427 | } |
Eric Christopher | c3e9c40 | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1428 | break; |
| 1429 | } |
| 1430 | |
Chad Rosier | 9cf803c | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1431 | unsigned SrcReg1 = getRegForValue(Src1Value); |
| 1432 | if (SrcReg1 == 0) return false; |
Chad Rosier | 59a2019 | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1433 | |
Duncan Sands | 1233065 | 2011-11-28 10:31:27 +0000 | [diff] [blame] | 1434 | unsigned SrcReg2 = 0; |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1435 | if (!UseImm) { |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1436 | SrcReg2 = getRegForValue(Src2Value); |
| 1437 | if (SrcReg2 == 0) return false; |
| 1438 | } |
Chad Rosier | 9cf803c | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1439 | |
| 1440 | // We have i1, i8, or i16, we need to either zero extend or sign extend. |
| 1441 | if (needsExt) { |
Chad Rosier | a0d3c75 | 2012-02-16 22:45:33 +0000 | [diff] [blame] | 1442 | SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt); |
| 1443 | if (SrcReg1 == 0) return false; |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1444 | if (!UseImm) { |
Chad Rosier | a0d3c75 | 2012-02-16 22:45:33 +0000 | [diff] [blame] | 1445 | SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt); |
| 1446 | if (SrcReg2 == 0) return false; |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1447 | } |
Chad Rosier | 9cf803c | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1448 | } |
Chad Rosier | 59a2019 | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1449 | |
Jim Grosbach | d786679 | 2013-08-16 23:37:40 +0000 | [diff] [blame] | 1450 | const MCInstrDesc &II = TII.get(CmpOpc); |
| 1451 | SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0); |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1452 | if (!UseImm) { |
Jim Grosbach | d786679 | 2013-08-16 23:37:40 +0000 | [diff] [blame] | 1453 | SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1); |
David Blaikie | 3ef249c9 | 2015-01-30 23:04:39 +0000 | [diff] [blame] | 1454 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1455 | .addReg(SrcReg1).addReg(SrcReg2)); |
| 1456 | } else { |
| 1457 | MachineInstrBuilder MIB; |
David Blaikie | 3ef249c9 | 2015-01-30 23:04:39 +0000 | [diff] [blame] | 1458 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1459 | .addReg(SrcReg1); |
| 1460 | |
| 1461 | // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0. |
| 1462 | if (isICmp) |
Chad Rosier | e19b0a9 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1463 | MIB.addImm(Imm); |
Chad Rosier | 595d419 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1464 | AddOptionalDefs(MIB); |
| 1465 | } |
Chad Rosier | 78127d3 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1466 | |
| 1467 | // For floating point we need to move the result to a comparison register |
| 1468 | // that we can then use for branches. |
| 1469 | if (Ty->isFloatTy() || Ty->isDoubleTy()) |
David Blaikie | 3ef249c9 | 2015-01-30 23:04:39 +0000 | [diff] [blame] | 1470 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Chad Rosier | 78127d3 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1471 | TII.get(ARM::FMSTAT))); |
Chad Rosier | 59a2019 | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1472 | return true; |
| 1473 | } |
| 1474 | |
| 1475 | bool ARMFastISel::SelectCmp(const Instruction *I) { |
| 1476 | const CmpInst *CI = cast<CmpInst>(I); |
| 1477 | |
Eric Christopher | 3a7e8cd | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1478 | // Get the compare predicate. |
| 1479 | ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate()); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1480 | |
Eric Christopher | 3a7e8cd | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1481 | // We may not handle every CC for now. |
| 1482 | if (ARMPred == ARMCC::AL) return false; |
| 1483 | |
Chad Rosier | 59a2019 | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1484 | // Emit the compare. |
David Blaikie | 3ef249c9 | 2015-01-30 23:04:39 +0000 | [diff] [blame] | 1485 | if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned())) |
Chad Rosier | 59a2019 | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1486 | return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1487 | |
Eric Christopher | 3a7e8cd | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1488 | // Now set a register based on the comparison. Explicitly set the predicates |
| 1489 | // here. |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1490 | unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi; |
Craig Topper | 61e88f4 | 2014-11-21 05:58:21 +0000 | [diff] [blame] | 1491 | const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass |
| 1492 | : &ARM::GPRRegClass; |
Eric Christopher | 76a9752 | 2010-10-07 05:39:19 +0000 | [diff] [blame] | 1493 | unsigned DestReg = createResultReg(RC); |
Chad Rosier | 78127d3 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1494 | Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0); |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1495 | unsigned ZeroReg = fastMaterializeConstant(Zero); |
Chad Rosier | 377f1f2 | 2012-03-07 20:59:26 +0000 | [diff] [blame] | 1496 | // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR. |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1497 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg) |
Eric Christopher | 3a7e8cd | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1498 | .addReg(ZeroReg).addImm(1) |
Chad Rosier | 377f1f2 | 2012-03-07 20:59:26 +0000 | [diff] [blame] | 1499 | .addImm(ARMPred).addReg(ARM::CPSR); |
Eric Christopher | 3a7e8cd | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1500 | |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1501 | updateValueMap(I, DestReg); |
Eric Christopher | c3e9c40 | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1502 | return true; |
| 1503 | } |
| 1504 | |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1505 | bool ARMFastISel::SelectFPExt(const Instruction *I) { |
Eric Christopher | f14b9bf | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1506 | // Make sure we have VFP and that we're extending float to double. |
| 1507 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1508 | |
Eric Christopher | f14b9bf | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1509 | Value *V = I->getOperand(0); |
| 1510 | if (!I->getType()->isDoubleTy() || |
| 1511 | !V->getType()->isFloatTy()) return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1512 | |
Eric Christopher | f14b9bf | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1513 | unsigned Op = getRegForValue(V); |
| 1514 | if (Op == 0) return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1515 | |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1516 | unsigned Result = createResultReg(&ARM::DPRRegClass); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1517 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 82b05d7 | 2010-09-09 20:36:19 +0000 | [diff] [blame] | 1518 | TII.get(ARM::VCVTDS), Result) |
Eric Christopher | 5903c0b | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1519 | .addReg(Op)); |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1520 | updateValueMap(I, Result); |
Eric Christopher | 5903c0b | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1521 | return true; |
| 1522 | } |
| 1523 | |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1524 | bool ARMFastISel::SelectFPTrunc(const Instruction *I) { |
Eric Christopher | 5903c0b | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1525 | // Make sure we have VFP and that we're truncating double to float. |
| 1526 | if (!Subtarget->hasVFP2()) return false; |
| 1527 | |
| 1528 | Value *V = I->getOperand(0); |
Eric Christopher | 8cfc459 | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1529 | if (!(I->getType()->isFloatTy() && |
| 1530 | V->getType()->isDoubleTy())) return false; |
Eric Christopher | 5903c0b | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1531 | |
| 1532 | unsigned Op = getRegForValue(V); |
| 1533 | if (Op == 0) return false; |
| 1534 | |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1535 | unsigned Result = createResultReg(&ARM::SPRRegClass); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1536 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 82b05d7 | 2010-09-09 20:36:19 +0000 | [diff] [blame] | 1537 | TII.get(ARM::VCVTSD), Result) |
Eric Christopher | f14b9bf | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1538 | .addReg(Op)); |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1539 | updateValueMap(I, Result); |
Eric Christopher | f14b9bf | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1540 | return true; |
| 1541 | } |
| 1542 | |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 1543 | bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) { |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1544 | // Make sure we have VFP. |
| 1545 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1546 | |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1547 | MVT DstVT; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1548 | Type *Ty = I->getType(); |
Eric Christopher | 4bd7047 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1549 | if (!isTypeLegal(Ty, DstVT)) |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1550 | return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1551 | |
Chad Rosier | bf5f4be | 2011-11-03 02:04:59 +0000 | [diff] [blame] | 1552 | Value *Src = I->getOperand(0); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1553 | EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true); |
Patrik Hagglund | c494d24 | 2012-12-17 14:30:06 +0000 | [diff] [blame] | 1554 | if (!SrcEVT.isSimple()) |
| 1555 | return false; |
| 1556 | MVT SrcVT = SrcEVT.getSimpleVT(); |
Chad Rosier | bf5f4be | 2011-11-03 02:04:59 +0000 | [diff] [blame] | 1557 | if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) |
Eli Friedman | 5bbb756 | 2011-05-25 19:09:45 +0000 | [diff] [blame] | 1558 | return false; |
| 1559 | |
Chad Rosier | bf5f4be | 2011-11-03 02:04:59 +0000 | [diff] [blame] | 1560 | unsigned SrcReg = getRegForValue(Src); |
| 1561 | if (SrcReg == 0) return false; |
| 1562 | |
| 1563 | // Handle sign-extension. |
| 1564 | if (SrcVT == MVT::i16 || SrcVT == MVT::i8) { |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 1565 | SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32, |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 1566 | /*isZExt*/!isSigned); |
Chad Rosier | a0d3c75 | 2012-02-16 22:45:33 +0000 | [diff] [blame] | 1567 | if (SrcReg == 0) return false; |
Chad Rosier | bf5f4be | 2011-11-03 02:04:59 +0000 | [diff] [blame] | 1568 | } |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1569 | |
Eric Christopher | 860fc93 | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1570 | // The conversion routine works on fp-reg to fp-reg and the operand above |
| 1571 | // was an integer, move it to the fp registers if possible. |
Chad Rosier | bf5f4be | 2011-11-03 02:04:59 +0000 | [diff] [blame] | 1572 | unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg); |
Eric Christopher | 4bd7047 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1573 | if (FP == 0) return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1574 | |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1575 | unsigned Opc; |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 1576 | if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS; |
| 1577 | else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD; |
Chad Rosier | 17847ae | 2011-08-31 23:49:05 +0000 | [diff] [blame] | 1578 | else return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1579 | |
Eric Christopher | 4bd7047 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1580 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1581 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1582 | TII.get(Opc), ResultReg).addReg(FP)); |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1583 | updateValueMap(I, ResultReg); |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1584 | return true; |
| 1585 | } |
| 1586 | |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 1587 | bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) { |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1588 | // Make sure we have VFP. |
| 1589 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1590 | |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1591 | MVT DstVT; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1592 | Type *RetTy = I->getType(); |
Eric Christopher | 712bd0a | 2010-09-10 00:35:09 +0000 | [diff] [blame] | 1593 | if (!isTypeLegal(RetTy, DstVT)) |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1594 | return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1595 | |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1596 | unsigned Op = getRegForValue(I->getOperand(0)); |
| 1597 | if (Op == 0) return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1598 | |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1599 | unsigned Opc; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1600 | Type *OpTy = I->getOperand(0)->getType(); |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 1601 | if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS; |
| 1602 | else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD; |
Chad Rosier | 17847ae | 2011-08-31 23:49:05 +0000 | [diff] [blame] | 1603 | else return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1604 | |
Chad Rosier | 41f0e78 | 2012-02-03 20:27:51 +0000 | [diff] [blame] | 1605 | // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg. |
Eric Christopher | 8cfc459 | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1606 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1607 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1608 | TII.get(Opc), ResultReg).addReg(Op)); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1609 | |
Eric Christopher | 4bd7047 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1610 | // This result needs to be in an integer register, but the conversion only |
| 1611 | // takes place in fp-regs. |
Eric Christopher | 860fc93 | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1612 | unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg); |
Eric Christopher | 4bd7047 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1613 | if (IntReg == 0) return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1614 | |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1615 | updateValueMap(I, IntReg); |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1616 | return true; |
| 1617 | } |
| 1618 | |
Eric Christopher | 511aa31 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1619 | bool ARMFastISel::SelectSelect(const Instruction *I) { |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1620 | MVT VT; |
| 1621 | if (!isTypeLegal(I->getType(), VT)) |
Eric Christopher | 511aa31 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1622 | return false; |
| 1623 | |
| 1624 | // Things need to be register sized for register moves. |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1625 | if (VT != MVT::i32) return false; |
Eric Christopher | 511aa31 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1626 | |
| 1627 | unsigned CondReg = getRegForValue(I->getOperand(0)); |
| 1628 | if (CondReg == 0) return false; |
| 1629 | unsigned Op1Reg = getRegForValue(I->getOperand(1)); |
| 1630 | if (Op1Reg == 0) return false; |
Eric Christopher | 511aa31 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1631 | |
Chad Rosier | 7ddd63c | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1632 | // Check to see if we can use an immediate in the conditional move. |
| 1633 | int Imm = 0; |
| 1634 | bool UseImm = false; |
| 1635 | bool isNegativeImm = false; |
| 1636 | if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) { |
| 1637 | assert (VT == MVT::i32 && "Expecting an i32."); |
| 1638 | Imm = (int)ConstInt->getValue().getZExtValue(); |
| 1639 | if (Imm < 0) { |
| 1640 | isNegativeImm = true; |
| 1641 | Imm = ~Imm; |
| 1642 | } |
| 1643 | UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : |
| 1644 | (ARM_AM::getSOImmVal(Imm) != -1); |
| 1645 | } |
| 1646 | |
Duncan Sands | 1233065 | 2011-11-28 10:31:27 +0000 | [diff] [blame] | 1647 | unsigned Op2Reg = 0; |
Chad Rosier | 7ddd63c | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1648 | if (!UseImm) { |
| 1649 | Op2Reg = getRegForValue(I->getOperand(2)); |
| 1650 | if (Op2Reg == 0) return false; |
| 1651 | } |
| 1652 | |
Ahmed Bougacha | e8d0c4c | 2015-05-06 04:14:02 +0000 | [diff] [blame] | 1653 | unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; |
| 1654 | CondReg = constrainOperandRegClass(TII.get(TstOpc), CondReg, 0); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1655 | AddOptionalDefs( |
Ahmed Bougacha | e8d0c4c | 2015-05-06 04:14:02 +0000 | [diff] [blame] | 1656 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc)) |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1657 | .addReg(CondReg) |
Ahmed Bougacha | e8d0c4c | 2015-05-06 04:14:02 +0000 | [diff] [blame] | 1658 | .addImm(1)); |
Chad Rosier | 7ddd63c | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1659 | |
| 1660 | unsigned MovCCOpc; |
Chad Rosier | 2ec7db0 | 2012-11-27 21:46:46 +0000 | [diff] [blame] | 1661 | const TargetRegisterClass *RC; |
Chad Rosier | 7ddd63c | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1662 | if (!UseImm) { |
Chad Rosier | 2ec7db0 | 2012-11-27 21:46:46 +0000 | [diff] [blame] | 1663 | RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass; |
Chad Rosier | 7ddd63c | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1664 | MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr; |
| 1665 | } else { |
Chad Rosier | 2ec7db0 | 2012-11-27 21:46:46 +0000 | [diff] [blame] | 1666 | RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass; |
| 1667 | if (!isNegativeImm) |
Chad Rosier | 7ddd63c | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1668 | MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi; |
Chad Rosier | 2ec7db0 | 2012-11-27 21:46:46 +0000 | [diff] [blame] | 1669 | else |
Chad Rosier | 7ddd63c | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1670 | MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi; |
Chad Rosier | 7ddd63c | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1671 | } |
Eric Christopher | 511aa31 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1672 | unsigned ResultReg = createResultReg(RC); |
Jim Grosbach | d786679 | 2013-08-16 23:37:40 +0000 | [diff] [blame] | 1673 | if (!UseImm) { |
Jim Grosbach | 71a78f9 | 2013-08-20 19:12:42 +0000 | [diff] [blame] | 1674 | Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1); |
Jim Grosbach | d786679 | 2013-08-16 23:37:40 +0000 | [diff] [blame] | 1675 | Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1676 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), |
| 1677 | ResultReg) |
| 1678 | .addReg(Op2Reg) |
| 1679 | .addReg(Op1Reg) |
| 1680 | .addImm(ARMCC::NE) |
| 1681 | .addReg(ARM::CPSR); |
Jim Grosbach | d786679 | 2013-08-16 23:37:40 +0000 | [diff] [blame] | 1682 | } else { |
| 1683 | Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1684 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), |
| 1685 | ResultReg) |
| 1686 | .addReg(Op1Reg) |
| 1687 | .addImm(Imm) |
| 1688 | .addImm(ARMCC::EQ) |
| 1689 | .addReg(ARM::CPSR); |
Jim Grosbach | d786679 | 2013-08-16 23:37:40 +0000 | [diff] [blame] | 1690 | } |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1691 | updateValueMap(I, ResultReg); |
Eric Christopher | 511aa31 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1692 | return true; |
| 1693 | } |
| 1694 | |
Chad Rosier | aaa55a8 | 2012-02-03 21:07:27 +0000 | [diff] [blame] | 1695 | bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) { |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1696 | MVT VT; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1697 | Type *Ty = I->getType(); |
Eric Christopher | 56094ff | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1698 | if (!isTypeLegal(Ty, VT)) |
| 1699 | return false; |
| 1700 | |
| 1701 | // If we have integer div support we should have selected this automagically. |
| 1702 | // In case we have a real miss go ahead and return false and we'll pick |
| 1703 | // it up later. |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1704 | if (Subtarget->hasDivide()) return false; |
| 1705 | |
Eric Christopher | 56094ff | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1706 | // Otherwise emit a libcall. |
| 1707 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Eric Christopher | e11017c | 2010-10-11 08:31:54 +0000 | [diff] [blame] | 1708 | if (VT == MVT::i8) |
Chad Rosier | aaa55a8 | 2012-02-03 21:07:27 +0000 | [diff] [blame] | 1709 | LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8; |
Eric Christopher | e11017c | 2010-10-11 08:31:54 +0000 | [diff] [blame] | 1710 | else if (VT == MVT::i16) |
Chad Rosier | aaa55a8 | 2012-02-03 21:07:27 +0000 | [diff] [blame] | 1711 | LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16; |
Eric Christopher | 56094ff | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1712 | else if (VT == MVT::i32) |
Chad Rosier | aaa55a8 | 2012-02-03 21:07:27 +0000 | [diff] [blame] | 1713 | LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32; |
Eric Christopher | 56094ff | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1714 | else if (VT == MVT::i64) |
Chad Rosier | aaa55a8 | 2012-02-03 21:07:27 +0000 | [diff] [blame] | 1715 | LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64; |
Eric Christopher | 56094ff | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1716 | else if (VT == MVT::i128) |
Chad Rosier | aaa55a8 | 2012-02-03 21:07:27 +0000 | [diff] [blame] | 1717 | LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128; |
Eric Christopher | 56094ff | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1718 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!"); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1719 | |
Eric Christopher | 56094ff | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1720 | return ARMEmitLibcall(I, LC); |
| 1721 | } |
| 1722 | |
Chad Rosier | b84a4b4 | 2012-02-03 21:23:45 +0000 | [diff] [blame] | 1723 | bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) { |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1724 | MVT VT; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1725 | Type *Ty = I->getType(); |
Eric Christopher | eae1b38 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1726 | if (!isTypeLegal(Ty, VT)) |
| 1727 | return false; |
| 1728 | |
| 1729 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
| 1730 | if (VT == MVT::i8) |
Chad Rosier | b84a4b4 | 2012-02-03 21:23:45 +0000 | [diff] [blame] | 1731 | LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8; |
Eric Christopher | eae1b38 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1732 | else if (VT == MVT::i16) |
Chad Rosier | b84a4b4 | 2012-02-03 21:23:45 +0000 | [diff] [blame] | 1733 | LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16; |
Eric Christopher | eae1b38 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1734 | else if (VT == MVT::i32) |
Chad Rosier | b84a4b4 | 2012-02-03 21:23:45 +0000 | [diff] [blame] | 1735 | LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32; |
Eric Christopher | eae1b38 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1736 | else if (VT == MVT::i64) |
Chad Rosier | b84a4b4 | 2012-02-03 21:23:45 +0000 | [diff] [blame] | 1737 | LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64; |
Eric Christopher | eae1b38 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1738 | else if (VT == MVT::i128) |
Chad Rosier | b84a4b4 | 2012-02-03 21:23:45 +0000 | [diff] [blame] | 1739 | LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128; |
Eric Christopher | e1bcb43 | 2010-10-11 08:40:05 +0000 | [diff] [blame] | 1740 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!"); |
Eric Christopher | e4b3d6b | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 1741 | |
Eric Christopher | eae1b38 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1742 | return ARMEmitLibcall(I, LC); |
| 1743 | } |
| 1744 | |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 1745 | bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1746 | EVT DestVT = TLI.getValueType(DL, I->getType(), true); |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 1747 | |
| 1748 | // We can get here in the case when we have a binary operation on a non-legal |
| 1749 | // type and the target independent selector doesn't know how to handle it. |
| 1750 | if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1) |
| 1751 | return false; |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 1752 | |
Chad Rosier | bd47125 | 2012-02-08 02:29:21 +0000 | [diff] [blame] | 1753 | unsigned Opc; |
| 1754 | switch (ISDOpcode) { |
| 1755 | default: return false; |
| 1756 | case ISD::ADD: |
| 1757 | Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr; |
| 1758 | break; |
| 1759 | case ISD::OR: |
| 1760 | Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr; |
| 1761 | break; |
Chad Rosier | 0ee8c51 | 2012-02-08 02:45:44 +0000 | [diff] [blame] | 1762 | case ISD::SUB: |
| 1763 | Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr; |
| 1764 | break; |
Chad Rosier | bd47125 | 2012-02-08 02:29:21 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 1767 | unsigned SrcReg1 = getRegForValue(I->getOperand(0)); |
| 1768 | if (SrcReg1 == 0) return false; |
| 1769 | |
| 1770 | // TODO: Often the 2nd operand is an immediate, which can be encoded directly |
| 1771 | // in the instruction, rather then materializing the value in a register. |
| 1772 | unsigned SrcReg2 = getRegForValue(I->getOperand(1)); |
| 1773 | if (SrcReg2 == 0) return false; |
| 1774 | |
JF Bastien | 13969d0 | 2013-05-29 15:45:47 +0000 | [diff] [blame] | 1775 | unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass); |
Joey Gouly | c7cda1c | 2013-08-23 15:20:56 +0000 | [diff] [blame] | 1776 | SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1); |
| 1777 | SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1778 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 1779 | TII.get(Opc), ResultReg) |
| 1780 | .addReg(SrcReg1).addReg(SrcReg2)); |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1781 | updateValueMap(I, ResultReg); |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 1782 | return true; |
| 1783 | } |
| 1784 | |
| 1785 | bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1786 | EVT FPVT = TLI.getValueType(DL, I->getType(), true); |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 1787 | if (!FPVT.isSimple()) return false; |
| 1788 | MVT VT = FPVT.getSimpleVT(); |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1789 | |
Pete Cooper | d927c6e | 2015-05-06 16:39:17 +0000 | [diff] [blame] | 1790 | // FIXME: Support vector types where possible. |
| 1791 | if (VT.isVector()) |
| 1792 | return false; |
| 1793 | |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1794 | // We can get here in the case when we want to use NEON for our fp |
| 1795 | // operations, but can't figure out how to. Just use the vfp instructions |
| 1796 | // if we have them. |
| 1797 | // FIXME: It'd be nice to use NEON instructions. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1798 | Type *Ty = I->getType(); |
Eric Christopher | bd3d121 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1799 | bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); |
| 1800 | if (isFloat && !Subtarget->hasVFP2()) |
| 1801 | return false; |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1802 | |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1803 | unsigned Opc; |
Duncan Sands | 1462777 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 1804 | bool is64bit = VT == MVT::f64 || VT == MVT::i64; |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1805 | switch (ISDOpcode) { |
| 1806 | default: return false; |
| 1807 | case ISD::FADD: |
Eric Christopher | bd3d121 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1808 | Opc = is64bit ? ARM::VADDD : ARM::VADDS; |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1809 | break; |
| 1810 | case ISD::FSUB: |
Eric Christopher | bd3d121 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1811 | Opc = is64bit ? ARM::VSUBD : ARM::VSUBS; |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1812 | break; |
| 1813 | case ISD::FMUL: |
Eric Christopher | bd3d121 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1814 | Opc = is64bit ? ARM::VMULD : ARM::VMULS; |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1815 | break; |
| 1816 | } |
Chad Rosier | 80979b6 | 2011-11-16 18:39:44 +0000 | [diff] [blame] | 1817 | unsigned Op1 = getRegForValue(I->getOperand(0)); |
| 1818 | if (Op1 == 0) return false; |
| 1819 | |
| 1820 | unsigned Op2 = getRegForValue(I->getOperand(1)); |
| 1821 | if (Op2 == 0) return false; |
| 1822 | |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 1823 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy)); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1824 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1825 | TII.get(Opc), ResultReg) |
| 1826 | .addReg(Op1).addReg(Op2)); |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1827 | updateValueMap(I, ResultReg); |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1828 | return true; |
| 1829 | } |
| 1830 | |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1831 | // Call Handling Code |
| 1832 | |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 1833 | // This is largely taken directly from CCAssignFnForNode |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1834 | // TODO: We may not support all of this. |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 1835 | CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, |
| 1836 | bool Return, |
| 1837 | bool isVarArg) { |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1838 | switch (CC) { |
| 1839 | default: |
| 1840 | llvm_unreachable("Unsupported calling convention"); |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1841 | case CallingConv::Fast: |
Jush Lu | 26088cb | 2012-08-16 05:15:53 +0000 | [diff] [blame] | 1842 | if (Subtarget->hasVFP2() && !isVarArg) { |
| 1843 | if (!Subtarget->isAAPCS_ABI()) |
| 1844 | return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); |
| 1845 | // For AAPCS ABI targets, just use VFP variant of the calling convention. |
| 1846 | return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); |
| 1847 | } |
Evan Cheng | 21abfc9 | 2010-10-22 18:57:05 +0000 | [diff] [blame] | 1848 | // Fallthrough |
| 1849 | case CallingConv::C: |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1850 | // Use target triple & subtarget features to do actual dispatch. |
| 1851 | if (Subtarget->isAAPCS_ABI()) { |
| 1852 | if (Subtarget->hasVFP2() && |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 1853 | TM.Options.FloatABIType == FloatABI::Hard && !isVarArg) |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1854 | return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); |
| 1855 | else |
| 1856 | return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); |
Bob Wilson | 8823b84 | 2015-09-19 06:20:59 +0000 | [diff] [blame] | 1857 | } else { |
| 1858 | return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); |
| 1859 | } |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1860 | case CallingConv::ARM_AAPCS_VFP: |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 1861 | if (!isVarArg) |
| 1862 | return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); |
| 1863 | // Fall through to soft float variant, variadic functions don't |
| 1864 | // use hard floating point ABI. |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1865 | case CallingConv::ARM_AAPCS: |
| 1866 | return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); |
| 1867 | case CallingConv::ARM_APCS: |
| 1868 | return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); |
Eric Christopher | b332236 | 2012-08-03 00:05:53 +0000 | [diff] [blame] | 1869 | case CallingConv::GHC: |
| 1870 | if (Return) |
| 1871 | llvm_unreachable("Can't return in GHC call convention"); |
| 1872 | else |
| 1873 | return CC_ARM_APCS_GHC; |
Eric Christopher | 72497e5 | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1877 | bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args, |
| 1878 | SmallVectorImpl<unsigned> &ArgRegs, |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1879 | SmallVectorImpl<MVT> &ArgVTs, |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1880 | SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, |
| 1881 | SmallVectorImpl<unsigned> &RegArgs, |
| 1882 | CallingConv::ID CC, |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 1883 | unsigned &NumBytes, |
| 1884 | bool isVarArg) { |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1885 | SmallVector<CCValAssign, 16> ArgLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 1886 | CCState CCInfo(CC, isVarArg, *FuncInfo.MF, ArgLocs, *Context); |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 1887 | CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, |
| 1888 | CCAssignFnForCall(CC, false, isVarArg)); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1889 | |
Bill Wendling | 23f8c4a | 2012-03-16 23:11:07 +0000 | [diff] [blame] | 1890 | // Check that we can handle all of the arguments. If we can't, then bail out |
| 1891 | // now before we add code to the MBB. |
| 1892 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1893 | CCValAssign &VA = ArgLocs[i]; |
| 1894 | MVT ArgVT = ArgVTs[VA.getValNo()]; |
| 1895 | |
| 1896 | // We don't handle NEON/vector parameters yet. |
| 1897 | if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64) |
| 1898 | return false; |
| 1899 | |
| 1900 | // Now copy/store arg to correct locations. |
| 1901 | if (VA.isRegLoc() && !VA.needsCustom()) { |
| 1902 | continue; |
| 1903 | } else if (VA.needsCustom()) { |
| 1904 | // TODO: We need custom lowering for vector (v2f64) args. |
| 1905 | if (VA.getLocVT() != MVT::f64 || |
| 1906 | // TODO: Only handle register args for now. |
| 1907 | !VA.isRegLoc() || !ArgLocs[++i].isRegLoc()) |
| 1908 | return false; |
| 1909 | } else { |
Craig Topper | 5671010 | 2013-08-15 02:33:50 +0000 | [diff] [blame] | 1910 | switch (ArgVT.SimpleTy) { |
Bill Wendling | 23f8c4a | 2012-03-16 23:11:07 +0000 | [diff] [blame] | 1911 | default: |
| 1912 | return false; |
| 1913 | case MVT::i1: |
| 1914 | case MVT::i8: |
| 1915 | case MVT::i16: |
| 1916 | case MVT::i32: |
| 1917 | break; |
| 1918 | case MVT::f32: |
| 1919 | if (!Subtarget->hasVFP2()) |
| 1920 | return false; |
| 1921 | break; |
| 1922 | case MVT::f64: |
| 1923 | if (!Subtarget->hasVFP2()) |
| 1924 | return false; |
| 1925 | break; |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | // At the point, we are able to handle the call's arguments in fast isel. |
| 1931 | |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1932 | // Get a count of how many bytes are to be pushed on the stack. |
| 1933 | NumBytes = CCInfo.getNextStackOffset(); |
| 1934 | |
| 1935 | // Issue CALLSEQ_START |
Evan Cheng | 194c3dc | 2011-06-28 21:14:33 +0000 | [diff] [blame] | 1936 | unsigned AdjStackDown = TII.getCallFrameSetupOpcode(); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1937 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 71ef1af | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1938 | TII.get(AdjStackDown)) |
| 1939 | .addImm(NumBytes)); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1940 | |
| 1941 | // Process the args. |
| 1942 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1943 | CCValAssign &VA = ArgLocs[i]; |
Juergen Ributzka | 4c018a1 | 2014-08-01 18:04:14 +0000 | [diff] [blame] | 1944 | const Value *ArgVal = Args[VA.getValNo()]; |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1945 | unsigned Arg = ArgRegs[VA.getValNo()]; |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1946 | MVT ArgVT = ArgVTs[VA.getValNo()]; |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1947 | |
Bill Wendling | 23f8c4a | 2012-03-16 23:11:07 +0000 | [diff] [blame] | 1948 | assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) && |
| 1949 | "We don't handle NEON/vector parameters yet."); |
Eric Christopher | c9616f2 | 2010-10-23 09:37:17 +0000 | [diff] [blame] | 1950 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1951 | // Handle arg promotion, etc. |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1952 | switch (VA.getLocInfo()) { |
| 1953 | case CCValAssign::Full: break; |
Eric Christopher | c103c66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1954 | case CCValAssign::SExt: { |
Chad Rosier | 9fd0e55 | 2011-12-02 20:25:18 +0000 | [diff] [blame] | 1955 | MVT DestVT = VA.getLocVT(); |
Chad Rosier | 5b9c397 | 2012-02-14 22:29:48 +0000 | [diff] [blame] | 1956 | Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false); |
| 1957 | assert (Arg != 0 && "Failed to emit a sext"); |
Chad Rosier | 9fd0e55 | 2011-12-02 20:25:18 +0000 | [diff] [blame] | 1958 | ArgVT = DestVT; |
Eric Christopher | c103c66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | } |
Chad Rosier | d0191a5 | 2011-11-05 20:16:15 +0000 | [diff] [blame] | 1961 | case CCValAssign::AExt: |
| 1962 | // Intentional fall-through. Handle AExt and ZExt. |
Eric Christopher | c103c66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1963 | case CCValAssign::ZExt: { |
Chad Rosier | 9fd0e55 | 2011-12-02 20:25:18 +0000 | [diff] [blame] | 1964 | MVT DestVT = VA.getLocVT(); |
Chad Rosier | 5b9c397 | 2012-02-14 22:29:48 +0000 | [diff] [blame] | 1965 | Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true); |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 1966 | assert (Arg != 0 && "Failed to emit a zext"); |
Chad Rosier | 9fd0e55 | 2011-12-02 20:25:18 +0000 | [diff] [blame] | 1967 | ArgVT = DestVT; |
Eric Christopher | c103c66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1968 | break; |
| 1969 | } |
| 1970 | case CCValAssign::BCvt: { |
Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1971 | unsigned BC = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg, |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1972 | /*TODO: Kill=*/false); |
Eric Christopher | c103c66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1973 | assert(BC != 0 && "Failed to emit a bitcast!"); |
| 1974 | Arg = BC; |
| 1975 | ArgVT = VA.getLocVT(); |
| 1976 | break; |
| 1977 | } |
| 1978 | default: llvm_unreachable("Unknown arg promotion!"); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | // Now copy/store arg to correct locations. |
Eric Christopher | 71ef1af | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1982 | if (VA.isRegLoc() && !VA.needsCustom()) { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1983 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1984 | TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1985 | RegArgs.push_back(VA.getLocReg()); |
Eric Christopher | 4ac3ed0 | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1986 | } else if (VA.needsCustom()) { |
| 1987 | // TODO: We need custom lowering for vector (v2f64) args. |
Bill Wendling | 23f8c4a | 2012-03-16 23:11:07 +0000 | [diff] [blame] | 1988 | assert(VA.getLocVT() == MVT::f64 && |
| 1989 | "Custom lowering for v2f64 args not available"); |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1990 | |
Eric Christopher | 4ac3ed0 | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1991 | CCValAssign &NextVA = ArgLocs[++i]; |
| 1992 | |
Bill Wendling | 23f8c4a | 2012-03-16 23:11:07 +0000 | [diff] [blame] | 1993 | assert(VA.isRegLoc() && NextVA.isRegLoc() && |
| 1994 | "We only handle register args!"); |
Eric Christopher | 4ac3ed0 | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1995 | |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1996 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 4ac3ed0 | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1997 | TII.get(ARM::VMOVRRD), VA.getLocReg()) |
| 1998 | .addReg(NextVA.getLocReg(), RegState::Define) |
| 1999 | .addReg(Arg)); |
| 2000 | RegArgs.push_back(VA.getLocReg()); |
| 2001 | RegArgs.push_back(NextVA.getLocReg()); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2002 | } else { |
Eric Christopher | b353e4f | 2010-10-21 20:09:54 +0000 | [diff] [blame] | 2003 | assert(VA.isMemLoc()); |
| 2004 | // Need to store on the stack. |
Juergen Ributzka | 4c018a1 | 2014-08-01 18:04:14 +0000 | [diff] [blame] | 2005 | |
| 2006 | // Don't emit stores for undef values. |
| 2007 | if (isa<UndefValue>(ArgVal)) |
| 2008 | continue; |
| 2009 | |
Eric Christopher | fef5f31 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 2010 | Address Addr; |
| 2011 | Addr.BaseType = Address::RegBase; |
| 2012 | Addr.Base.Reg = ARM::SP; |
| 2013 | Addr.Offset = VA.getLocMemOffset(); |
Eric Christopher | b353e4f | 2010-10-21 20:09:54 +0000 | [diff] [blame] | 2014 | |
Bill Wendling | 23f8c4a | 2012-03-16 23:11:07 +0000 | [diff] [blame] | 2015 | bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet; |
| 2016 | assert(EmitRet && "Could not emit a store for argument!"); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
Bill Wendling | 23f8c4a | 2012-03-16 23:11:07 +0000 | [diff] [blame] | 2019 | |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2020 | return true; |
| 2021 | } |
| 2022 | |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2023 | bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2024 | const Instruction *I, CallingConv::ID CC, |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2025 | unsigned &NumBytes, bool isVarArg) { |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2026 | // Issue CALLSEQ_END |
Evan Cheng | 194c3dc | 2011-06-28 21:14:33 +0000 | [diff] [blame] | 2027 | unsigned AdjStackUp = TII.getCallFrameDestroyOpcode(); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2028 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | 71ef1af | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 2029 | TII.get(AdjStackUp)) |
| 2030 | .addImm(NumBytes).addImm(0)); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Now the return value. |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2033 | if (RetVT != MVT::isVoid) { |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2034 | SmallVector<CCValAssign, 16> RVLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 2035 | CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context); |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2036 | CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg)); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2037 | |
| 2038 | // Copy all of the result registers out of their specified physreg. |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2039 | if (RVLocs.size() == 2 && RetVT == MVT::f64) { |
Eric Christopher | c1e209d | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 2040 | // For this move we copy into two registers and then move into the |
| 2041 | // double fp reg we want. |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 2042 | MVT DestVT = RVLocs[0].getValVT(); |
Craig Topper | 760b134 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 2043 | const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT); |
Eric Christopher | c1e209d | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 2044 | unsigned ResultReg = createResultReg(DstRC); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2045 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Eric Christopher | c1e209d | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 2046 | TII.get(ARM::VMOVDRR), ResultReg) |
Eric Christopher | af719ef | 2010-10-20 08:02:24 +0000 | [diff] [blame] | 2047 | .addReg(RVLocs[0].getLocReg()) |
| 2048 | .addReg(RVLocs[1].getLocReg())); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2049 | |
Eric Christopher | af719ef | 2010-10-20 08:02:24 +0000 | [diff] [blame] | 2050 | UsedRegs.push_back(RVLocs[0].getLocReg()); |
| 2051 | UsedRegs.push_back(RVLocs[1].getLocReg()); |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 2052 | |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2053 | // Finally update the result. |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 2054 | updateValueMap(I, ResultReg); |
Chad Rosier | 90f9afe | 2012-05-11 18:51:55 +0000 | [diff] [blame] | 2055 | } else { |
| 2056 | assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!"); |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 2057 | MVT CopyVT = RVLocs[0].getValVT(); |
Chad Rosier | 5de1bea | 2011-11-08 00:03:32 +0000 | [diff] [blame] | 2058 | |
| 2059 | // Special handling for extended integers. |
| 2060 | if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16) |
| 2061 | CopyVT = MVT::i32; |
| 2062 | |
Craig Topper | 760b134 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 2063 | const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2064 | |
Eric Christopher | c1e209d | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 2065 | unsigned ResultReg = createResultReg(DstRC); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2066 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2067 | TII.get(TargetOpcode::COPY), |
Eric Christopher | c1e209d | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 2068 | ResultReg).addReg(RVLocs[0].getLocReg()); |
| 2069 | UsedRegs.push_back(RVLocs[0].getLocReg()); |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2070 | |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2071 | // Finally update the result. |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 2072 | updateValueMap(I, ResultReg); |
Eric Christopher | c1e209d | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 2073 | } |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2076 | return true; |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2079 | bool ARMFastISel::SelectRet(const Instruction *I) { |
| 2080 | const ReturnInst *Ret = cast<ReturnInst>(I); |
| 2081 | const Function &F = *I->getParent()->getParent(); |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 2082 | |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2083 | if (!FuncInfo.CanLowerReturn) |
| 2084 | return false; |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 2085 | |
Jakob Stoklund Olesen | f90fb6e | 2013-02-05 18:08:40 +0000 | [diff] [blame] | 2086 | // Build a list of return value registers. |
| 2087 | SmallVector<unsigned, 4> RetRegs; |
| 2088 | |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2089 | CallingConv::ID CC = F.getCallingConv(); |
| 2090 | if (Ret->getNumOperands() > 0) { |
| 2091 | SmallVector<ISD::OutputArg, 4> Outs; |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 2092 | GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL); |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2093 | |
| 2094 | // Analyze operands of the call, assigning locations to each operand. |
| 2095 | SmallVector<CCValAssign, 16> ValLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 2096 | CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext()); |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2097 | CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */, |
| 2098 | F.isVarArg())); |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2099 | |
| 2100 | const Value *RV = Ret->getOperand(0); |
| 2101 | unsigned Reg = getRegForValue(RV); |
| 2102 | if (Reg == 0) |
| 2103 | return false; |
| 2104 | |
| 2105 | // Only handle a single return value for now. |
| 2106 | if (ValLocs.size() != 1) |
| 2107 | return false; |
| 2108 | |
| 2109 | CCValAssign &VA = ValLocs[0]; |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 2110 | |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2111 | // Don't bother handling odd stuff for now. |
| 2112 | if (VA.getLocInfo() != CCValAssign::Full) |
| 2113 | return false; |
| 2114 | // Only handle register returns for now. |
| 2115 | if (!VA.isRegLoc()) |
| 2116 | return false; |
Chad Rosier | f3e73ad | 2011-11-04 00:50:21 +0000 | [diff] [blame] | 2117 | |
| 2118 | unsigned SrcReg = Reg + VA.getValNo(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2119 | EVT RVEVT = TLI.getValueType(DL, RV->getType()); |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 2120 | if (!RVEVT.isSimple()) return false; |
| 2121 | MVT RVVT = RVEVT.getSimpleVT(); |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 2122 | MVT DestVT = VA.getValVT(); |
Chad Rosier | f3e73ad | 2011-11-04 00:50:21 +0000 | [diff] [blame] | 2123 | // Special handling for extended integers. |
| 2124 | if (RVVT != DestVT) { |
| 2125 | if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16) |
| 2126 | return false; |
| 2127 | |
Chad Rosier | f3e73ad | 2011-11-04 00:50:21 +0000 | [diff] [blame] | 2128 | assert(DestVT == MVT::i32 && "ARM should always ext to i32"); |
| 2129 | |
Chad Rosier | fcd29ae | 2012-02-17 01:21:28 +0000 | [diff] [blame] | 2130 | // Perform extension if flagged as either zext or sext. Otherwise, do |
| 2131 | // nothing. |
| 2132 | if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) { |
| 2133 | SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt()); |
| 2134 | if (SrcReg == 0) return false; |
| 2135 | } |
Chad Rosier | f3e73ad | 2011-11-04 00:50:21 +0000 | [diff] [blame] | 2136 | } |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 2137 | |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2138 | // Make the copy. |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2139 | unsigned DstReg = VA.getLocReg(); |
| 2140 | const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg); |
| 2141 | // Avoid a cross-class copy. This is very unlikely. |
| 2142 | if (!SrcRC->contains(DstReg)) |
| 2143 | return false; |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2144 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2145 | TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg); |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2146 | |
Jakob Stoklund Olesen | f90fb6e | 2013-02-05 18:08:40 +0000 | [diff] [blame] | 2147 | // Add register to return instruction. |
| 2148 | RetRegs.push_back(VA.getLocReg()); |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2149 | } |
Jim Grosbach | 055de2c | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 2150 | |
Chad Rosier | 0439cfc | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 2151 | unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET; |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2152 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Jakob Stoklund Olesen | f90fb6e | 2013-02-05 18:08:40 +0000 | [diff] [blame] | 2153 | TII.get(RetOpc)); |
| 2154 | AddOptionalDefs(MIB); |
| 2155 | for (unsigned i = 0, e = RetRegs.size(); i != e; ++i) |
| 2156 | MIB.addReg(RetRegs[i], RegState::Implicit); |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2157 | return true; |
| 2158 | } |
| 2159 | |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2160 | unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) { |
| 2161 | if (UseReg) |
| 2162 | return isThumb2 ? ARM::tBLXr : ARM::BLX; |
| 2163 | else |
| 2164 | return isThumb2 ? ARM::tBL : ARM::BL; |
| 2165 | } |
| 2166 | |
| 2167 | unsigned ARMFastISel::getLibcallReg(const Twine &Name) { |
Chandler Carruth | 1c82d33 | 2013-07-27 11:23:08 +0000 | [diff] [blame] | 2168 | // Manually compute the global's type to avoid building it when unnecessary. |
| 2169 | Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2170 | EVT LCREVT = TLI.getValueType(DL, GVTy); |
Chandler Carruth | 1c82d33 | 2013-07-27 11:23:08 +0000 | [diff] [blame] | 2171 | if (!LCREVT.isSimple()) return 0; |
| 2172 | |
Bill Wendling | 76cce19 | 2013-12-29 08:00:04 +0000 | [diff] [blame] | 2173 | GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false, |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2174 | GlobalValue::ExternalLinkage, nullptr, |
| 2175 | Name); |
Chandler Carruth | 1c82d33 | 2013-07-27 11:23:08 +0000 | [diff] [blame] | 2176 | assert(GV->getType() == GVTy && "We miscomputed the type for the global!"); |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 2177 | return ARMMaterializeGV(GV, LCREVT.getSimpleVT()); |
Eric Christopher | 919772f | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2180 | // A quick function that will emit a call for a named libcall in F with the |
| 2181 | // vector of passed arguments for the Instruction in I. We can assume that we |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2182 | // can emit a call for any libcall we can produce. This is an abridged version |
| 2183 | // of the full call infrastructure since we won't need to worry about things |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2184 | // like computed function pointers or strange arguments at call sites. |
| 2185 | // TODO: Try to unify this and the normal call bits for ARM, then try to unify |
| 2186 | // with X86. |
Eric Christopher | 7990df1 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 2187 | bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) { |
| 2188 | CallingConv::ID CC = TLI.getLibcallCallingConv(Call); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2189 | |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2190 | // Handle *simple* calls for now. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2191 | Type *RetTy = I->getType(); |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2192 | MVT RetVT; |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2193 | if (RetTy->isVoidTy()) |
| 2194 | RetVT = MVT::isVoid; |
| 2195 | else if (!isTypeLegal(RetTy, RetVT)) |
| 2196 | return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2197 | |
Chad Rosier | 90f9afe | 2012-05-11 18:51:55 +0000 | [diff] [blame] | 2198 | // Can't handle non-double multi-reg retvals. |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 2199 | if (RetVT != MVT::isVoid && RetVT != MVT::i32) { |
Chad Rosier | 90f9afe | 2012-05-11 18:51:55 +0000 | [diff] [blame] | 2200 | SmallVector<CCValAssign, 16> RVLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 2201 | CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context); |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2202 | CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false)); |
Chad Rosier | 90f9afe | 2012-05-11 18:51:55 +0000 | [diff] [blame] | 2203 | if (RVLocs.size() >= 2 && RetVT != MVT::f64) |
| 2204 | return false; |
| 2205 | } |
| 2206 | |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2207 | // Set up the argument vectors. |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2208 | SmallVector<Value*, 8> Args; |
| 2209 | SmallVector<unsigned, 8> ArgRegs; |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2210 | SmallVector<MVT, 8> ArgVTs; |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2211 | SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; |
| 2212 | Args.reserve(I->getNumOperands()); |
| 2213 | ArgRegs.reserve(I->getNumOperands()); |
| 2214 | ArgVTs.reserve(I->getNumOperands()); |
| 2215 | ArgFlags.reserve(I->getNumOperands()); |
Eric Christopher | 7990df1 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 2216 | for (unsigned i = 0; i < I->getNumOperands(); ++i) { |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2217 | Value *Op = I->getOperand(i); |
| 2218 | unsigned Arg = getRegForValue(Op); |
| 2219 | if (Arg == 0) return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2220 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2221 | Type *ArgTy = Op->getType(); |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2222 | MVT ArgVT; |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2223 | if (!isTypeLegal(ArgTy, ArgVT)) return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2224 | |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2225 | ISD::ArgFlagsTy Flags; |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2226 | unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy); |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2227 | Flags.setOrigAlign(OriginalAlignment); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2228 | |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2229 | Args.push_back(Op); |
| 2230 | ArgRegs.push_back(Arg); |
| 2231 | ArgVTs.push_back(ArgVT); |
| 2232 | ArgFlags.push_back(Flags); |
| 2233 | } |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2234 | |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2235 | // Handle the arguments now that we've gotten them. |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2236 | SmallVector<unsigned, 4> RegArgs; |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2237 | unsigned NumBytes; |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2238 | if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, |
| 2239 | RegArgs, CC, NumBytes, false)) |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2240 | return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2241 | |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2242 | unsigned CalleeReg = 0; |
Akira Hatanaka | 1bc8af7 | 2015-07-07 06:54:42 +0000 | [diff] [blame] | 2243 | if (Subtarget->genLongCalls()) { |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2244 | CalleeReg = getLibcallReg(TLI.getLibcallName(Call)); |
| 2245 | if (CalleeReg == 0) return false; |
| 2246 | } |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2247 | |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2248 | // Issue the call. |
Akira Hatanaka | 1bc8af7 | 2015-07-07 06:54:42 +0000 | [diff] [blame] | 2249 | unsigned CallOpc = ARMSelectCallOp(Subtarget->genLongCalls()); |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2250 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2251 | DbgLoc, TII.get(CallOpc)); |
Jakob Stoklund Olesen | e6afde5 | 2012-08-24 20:52:46 +0000 | [diff] [blame] | 2252 | // BL / BLX don't take a predicate, but tBL / tBLX do. |
| 2253 | if (isThumb2) |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2254 | AddDefaultPred(MIB); |
Akira Hatanaka | 1bc8af7 | 2015-07-07 06:54:42 +0000 | [diff] [blame] | 2255 | if (Subtarget->genLongCalls()) |
Jakob Stoklund Olesen | e6afde5 | 2012-08-24 20:52:46 +0000 | [diff] [blame] | 2256 | MIB.addReg(CalleeReg); |
| 2257 | else |
| 2258 | MIB.addExternalSymbol(TLI.getLibcallName(Call)); |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2259 | |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2260 | // Add implicit physical register uses to the call. |
| 2261 | for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) |
Jakob Stoklund Olesen | e6afde5 | 2012-08-24 20:52:46 +0000 | [diff] [blame] | 2262 | MIB.addReg(RegArgs[i], RegState::Implicit); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2263 | |
Jakob Stoklund Olesen | fa7a537 | 2012-02-24 01:19:29 +0000 | [diff] [blame] | 2264 | // Add a register mask with the call-preserved registers. |
| 2265 | // Proper defs for return values will be added by setPhysRegsDeadExcept(). |
Eric Christopher | 9deb75d | 2015-03-11 22:42:13 +0000 | [diff] [blame] | 2266 | MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC)); |
Jakob Stoklund Olesen | fa7a537 | 2012-02-24 01:19:29 +0000 | [diff] [blame] | 2267 | |
Eric Christopher | 7939806 | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2268 | // Finish off the call including any return values. |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2269 | SmallVector<unsigned, 4> UsedRegs; |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2270 | if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2271 | |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2272 | // Set all unused physreg defs as dead. |
| 2273 | static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2274 | |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2275 | return true; |
| 2276 | } |
| 2277 | |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2278 | bool ARMFastISel::SelectCall(const Instruction *I, |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2279 | const char *IntrMemName = nullptr) { |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2280 | const CallInst *CI = cast<CallInst>(I); |
| 2281 | const Value *Callee = CI->getCalledValue(); |
| 2282 | |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2283 | // Can't handle inline asm. |
| 2284 | if (isa<InlineAsm>(Callee)) return false; |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2285 | |
Chad Rosier | df42cf3 | 2012-12-11 00:18:02 +0000 | [diff] [blame] | 2286 | // Allow SelectionDAG isel to handle tail calls. |
| 2287 | if (CI->isTailCall()) return false; |
| 2288 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2289 | // Check the calling convention. |
| 2290 | ImmutableCallSite CS(CI); |
| 2291 | CallingConv::ID CC = CS.getCallingConv(); |
Eric Christopher | 167a7002 | 2010-10-18 06:49:12 +0000 | [diff] [blame] | 2292 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2293 | // TODO: Avoid some calling conventions? |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2294 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2295 | PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 2296 | FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2297 | bool isVarArg = FTy->isVarArg(); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2298 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2299 | // Handle *simple* calls for now. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2300 | Type *RetTy = I->getType(); |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2301 | MVT RetVT; |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2302 | if (RetTy->isVoidTy()) |
| 2303 | RetVT = MVT::isVoid; |
Chad Rosier | 5de1bea | 2011-11-08 00:03:32 +0000 | [diff] [blame] | 2304 | else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 && |
| 2305 | RetVT != MVT::i8 && RetVT != MVT::i1) |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2306 | return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2307 | |
Chad Rosier | 90f9afe | 2012-05-11 18:51:55 +0000 | [diff] [blame] | 2308 | // Can't handle non-double multi-reg retvals. |
| 2309 | if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 && |
| 2310 | RetVT != MVT::i16 && RetVT != MVT::i32) { |
| 2311 | SmallVector<CCValAssign, 16> RVLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 2312 | CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context); |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2313 | CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg)); |
Chad Rosier | 90f9afe | 2012-05-11 18:51:55 +0000 | [diff] [blame] | 2314 | if (RVLocs.size() >= 2 && RetVT != MVT::f64) |
| 2315 | return false; |
| 2316 | } |
| 2317 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2318 | // Set up the argument vectors. |
| 2319 | SmallVector<Value*, 8> Args; |
| 2320 | SmallVector<unsigned, 8> ArgRegs; |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2321 | SmallVector<MVT, 8> ArgVTs; |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2322 | SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; |
Chad Rosier | dccc479 | 2012-02-15 00:23:55 +0000 | [diff] [blame] | 2323 | unsigned arg_size = CS.arg_size(); |
| 2324 | Args.reserve(arg_size); |
| 2325 | ArgRegs.reserve(arg_size); |
| 2326 | ArgVTs.reserve(arg_size); |
| 2327 | ArgFlags.reserve(arg_size); |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2328 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 2329 | i != e; ++i) { |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2330 | // If we're lowering a memory intrinsic instead of a regular call, skip the |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2331 | // last two arguments, which shouldn't be passed to the underlying function. |
| 2332 | if (IntrMemName && e-i <= 2) |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2333 | break; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2334 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2335 | ISD::ArgFlagsTy Flags; |
| 2336 | unsigned AttrInd = i - CS.arg_begin() + 1; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 2337 | if (CS.paramHasAttr(AttrInd, Attribute::SExt)) |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2338 | Flags.setSExt(); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 2339 | if (CS.paramHasAttr(AttrInd, Attribute::ZExt)) |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2340 | Flags.setZExt(); |
| 2341 | |
Chad Rosier | 8a98ec4 | 2011-11-04 00:58:10 +0000 | [diff] [blame] | 2342 | // FIXME: Only handle *easy* calls for now. |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 2343 | if (CS.paramHasAttr(AttrInd, Attribute::InReg) || |
| 2344 | CS.paramHasAttr(AttrInd, Attribute::StructRet) || |
| 2345 | CS.paramHasAttr(AttrInd, Attribute::Nest) || |
| 2346 | CS.paramHasAttr(AttrInd, Attribute::ByVal)) |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2347 | return false; |
| 2348 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2349 | Type *ArgTy = (*i)->getType(); |
Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2350 | MVT ArgVT; |
Chad Rosier | d0191a5 | 2011-11-05 20:16:15 +0000 | [diff] [blame] | 2351 | if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 && |
| 2352 | ArgVT != MVT::i1) |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2353 | return false; |
Chad Rosier | ee93ff7 | 2011-11-18 01:17:34 +0000 | [diff] [blame] | 2354 | |
| 2355 | unsigned Arg = getRegForValue(*i); |
| 2356 | if (Arg == 0) |
| 2357 | return false; |
| 2358 | |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2359 | unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy); |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2360 | Flags.setOrigAlign(OriginalAlignment); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2361 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2362 | Args.push_back(*i); |
| 2363 | ArgRegs.push_back(Arg); |
| 2364 | ArgVTs.push_back(ArgVT); |
| 2365 | ArgFlags.push_back(Flags); |
| 2366 | } |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2367 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2368 | // Handle the arguments now that we've gotten them. |
| 2369 | SmallVector<unsigned, 4> RegArgs; |
| 2370 | unsigned NumBytes; |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2371 | if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, |
| 2372 | RegArgs, CC, NumBytes, isVarArg)) |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2373 | return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2374 | |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2375 | bool UseReg = false; |
Chad Rosier | 223faf7 | 2012-05-23 18:38:57 +0000 | [diff] [blame] | 2376 | const GlobalValue *GV = dyn_cast<GlobalValue>(Callee); |
Akira Hatanaka | 1bc8af7 | 2015-07-07 06:54:42 +0000 | [diff] [blame] | 2377 | if (!GV || Subtarget->genLongCalls()) UseReg = true; |
Chad Rosier | 223faf7 | 2012-05-23 18:38:57 +0000 | [diff] [blame] | 2378 | |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2379 | unsigned CalleeReg = 0; |
| 2380 | if (UseReg) { |
| 2381 | if (IntrMemName) |
| 2382 | CalleeReg = getLibcallReg(IntrMemName); |
| 2383 | else |
| 2384 | CalleeReg = getRegForValue(Callee); |
| 2385 | |
Chad Rosier | 223faf7 | 2012-05-23 18:38:57 +0000 | [diff] [blame] | 2386 | if (CalleeReg == 0) return false; |
| 2387 | } |
| 2388 | |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2389 | // Issue the call. |
| 2390 | unsigned CallOpc = ARMSelectCallOp(UseReg); |
| 2391 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2392 | DbgLoc, TII.get(CallOpc)); |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2393 | |
Logan Chien | 2361f51 | 2013-08-22 12:08:04 +0000 | [diff] [blame] | 2394 | unsigned char OpFlags = 0; |
| 2395 | |
| 2396 | // Add MO_PLT for global address or external symbol in the PIC relocation |
| 2397 | // model. |
| 2398 | if (Subtarget->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_) |
| 2399 | OpFlags = ARMII::MO_PLT; |
| 2400 | |
Jakob Stoklund Olesen | e6afde5 | 2012-08-24 20:52:46 +0000 | [diff] [blame] | 2401 | // ARM calls don't take a predicate, but tBL / tBLX do. |
| 2402 | if(isThumb2) |
Chad Rosier | c6916f8 | 2012-06-12 19:25:13 +0000 | [diff] [blame] | 2403 | AddDefaultPred(MIB); |
Jakob Stoklund Olesen | e6afde5 | 2012-08-24 20:52:46 +0000 | [diff] [blame] | 2404 | if (UseReg) |
| 2405 | MIB.addReg(CalleeReg); |
| 2406 | else if (!IntrMemName) |
Logan Chien | 2361f51 | 2013-08-22 12:08:04 +0000 | [diff] [blame] | 2407 | MIB.addGlobalAddress(GV, 0, OpFlags); |
Jakob Stoklund Olesen | e6afde5 | 2012-08-24 20:52:46 +0000 | [diff] [blame] | 2408 | else |
Logan Chien | 2361f51 | 2013-08-22 12:08:04 +0000 | [diff] [blame] | 2409 | MIB.addExternalSymbol(IntrMemName, OpFlags); |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 2410 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2411 | // Add implicit physical register uses to the call. |
| 2412 | for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) |
Jakob Stoklund Olesen | e6afde5 | 2012-08-24 20:52:46 +0000 | [diff] [blame] | 2413 | MIB.addReg(RegArgs[i], RegState::Implicit); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2414 | |
Jakob Stoklund Olesen | fa7a537 | 2012-02-24 01:19:29 +0000 | [diff] [blame] | 2415 | // Add a register mask with the call-preserved registers. |
| 2416 | // Proper defs for return values will be added by setPhysRegsDeadExcept(). |
Eric Christopher | 9deb75d | 2015-03-11 22:42:13 +0000 | [diff] [blame] | 2417 | MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC)); |
Jakob Stoklund Olesen | fa7a537 | 2012-02-24 01:19:29 +0000 | [diff] [blame] | 2418 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2419 | // Finish off the call including any return values. |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2420 | SmallVector<unsigned, 4> UsedRegs; |
Jush Lu | e67e07b | 2012-07-19 09:49:00 +0000 | [diff] [blame] | 2421 | if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg)) |
| 2422 | return false; |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2423 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2424 | // Set all unused physreg defs as dead. |
| 2425 | static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); |
Eric Christopher | 7ac602b | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2426 | |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2427 | return true; |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Chad Rosier | 057b6d3 | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2430 | bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) { |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2431 | return Len <= 16; |
| 2432 | } |
| 2433 | |
Jim Grosbach | 0c509fa | 2012-04-06 23:43:50 +0000 | [diff] [blame] | 2434 | bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, |
Chad Rosier | 9f5c68a | 2012-12-06 01:34:31 +0000 | [diff] [blame] | 2435 | uint64_t Len, unsigned Alignment) { |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2436 | // Make sure we don't bloat code by inlining very large memcpy's. |
Chad Rosier | 057b6d3 | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2437 | if (!ARMIsMemCpySmall(Len)) |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2438 | return false; |
| 2439 | |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2440 | while (Len) { |
| 2441 | MVT VT; |
Chad Rosier | 9f5c68a | 2012-12-06 01:34:31 +0000 | [diff] [blame] | 2442 | if (!Alignment || Alignment >= 4) { |
| 2443 | if (Len >= 4) |
| 2444 | VT = MVT::i32; |
| 2445 | else if (Len >= 2) |
| 2446 | VT = MVT::i16; |
| 2447 | else { |
| 2448 | assert (Len == 1 && "Expected a length of 1!"); |
| 2449 | VT = MVT::i8; |
| 2450 | } |
| 2451 | } else { |
| 2452 | // Bound based on alignment. |
| 2453 | if (Len >= 2 && Alignment == 2) |
| 2454 | VT = MVT::i16; |
| 2455 | else { |
Chad Rosier | 9f5c68a | 2012-12-06 01:34:31 +0000 | [diff] [blame] | 2456 | VT = MVT::i8; |
| 2457 | } |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | bool RV; |
| 2461 | unsigned ResultReg; |
| 2462 | RV = ARMEmitLoad(VT, ResultReg, Src); |
Eric Christopher | d284c1d | 2012-01-11 20:55:27 +0000 | [diff] [blame] | 2463 | assert (RV == true && "Should be able to handle this load."); |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2464 | RV = ARMEmitStore(VT, ResultReg, Dest); |
Eric Christopher | d284c1d | 2012-01-11 20:55:27 +0000 | [diff] [blame] | 2465 | assert (RV == true && "Should be able to handle this store."); |
Duncan Sands | ae22c60 | 2012-02-05 14:20:11 +0000 | [diff] [blame] | 2466 | (void)RV; |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2467 | |
| 2468 | unsigned Size = VT.getSizeInBits()/8; |
| 2469 | Len -= Size; |
| 2470 | Dest.Offset += Size; |
| 2471 | Src.Offset += Size; |
| 2472 | } |
| 2473 | |
| 2474 | return true; |
| 2475 | } |
| 2476 | |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2477 | bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) { |
| 2478 | // FIXME: Handle more intrinsics. |
| 2479 | switch (I.getIntrinsicID()) { |
| 2480 | default: return false; |
Chad Rosier | 820d248c | 2012-05-30 17:23:22 +0000 | [diff] [blame] | 2481 | case Intrinsic::frameaddress: { |
| 2482 | MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo(); |
| 2483 | MFI->setFrameAddressIsTaken(true); |
| 2484 | |
Craig Topper | 61e88f4 | 2014-11-21 05:58:21 +0000 | [diff] [blame] | 2485 | unsigned LdrOpc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12; |
| 2486 | const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass |
| 2487 | : &ARM::GPRRegClass; |
Chad Rosier | 820d248c | 2012-05-30 17:23:22 +0000 | [diff] [blame] | 2488 | |
| 2489 | const ARMBaseRegisterInfo *RegInfo = |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame] | 2490 | static_cast<const ARMBaseRegisterInfo *>(Subtarget->getRegisterInfo()); |
Chad Rosier | 820d248c | 2012-05-30 17:23:22 +0000 | [diff] [blame] | 2491 | unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF)); |
| 2492 | unsigned SrcReg = FramePtr; |
| 2493 | |
| 2494 | // Recursively load frame address |
| 2495 | // ldr r0 [fp] |
| 2496 | // ldr r0 [r0] |
| 2497 | // ldr r0 [r0] |
| 2498 | // ... |
| 2499 | unsigned DestReg; |
| 2500 | unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue(); |
| 2501 | while (Depth--) { |
| 2502 | DestReg = createResultReg(RC); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2503 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Chad Rosier | 820d248c | 2012-05-30 17:23:22 +0000 | [diff] [blame] | 2504 | TII.get(LdrOpc), DestReg) |
| 2505 | .addReg(SrcReg).addImm(0)); |
| 2506 | SrcReg = DestReg; |
| 2507 | } |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 2508 | updateValueMap(&I, SrcReg); |
Chad Rosier | 820d248c | 2012-05-30 17:23:22 +0000 | [diff] [blame] | 2509 | return true; |
| 2510 | } |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2511 | case Intrinsic::memcpy: |
| 2512 | case Intrinsic::memmove: { |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2513 | const MemTransferInst &MTI = cast<MemTransferInst>(I); |
| 2514 | // Don't handle volatile. |
| 2515 | if (MTI.isVolatile()) |
| 2516 | return false; |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2517 | |
| 2518 | // Disable inlining for memmove before calls to ComputeAddress. Otherwise, |
| 2519 | // we would emit dead code because we don't currently handle memmoves. |
| 2520 | bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy); |
| 2521 | if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) { |
Chad Rosier | 057b6d3 | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2522 | // Small memcpy's are common enough that we want to do them without a call |
| 2523 | // if possible. |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2524 | uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue(); |
Chad Rosier | 057b6d3 | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2525 | if (ARMIsMemCpySmall(Len)) { |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2526 | Address Dest, Src; |
| 2527 | if (!ARMComputeAddress(MTI.getRawDest(), Dest) || |
| 2528 | !ARMComputeAddress(MTI.getRawSource(), Src)) |
| 2529 | return false; |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2530 | unsigned Alignment = MTI.getAlignment(); |
Chad Rosier | 9f5c68a | 2012-12-06 01:34:31 +0000 | [diff] [blame] | 2531 | if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment)) |
Chad Rosier | ab7223e | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2532 | return true; |
| 2533 | } |
| 2534 | } |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 2535 | |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2536 | if (!MTI.getLength()->getType()->isIntegerTy(32)) |
| 2537 | return false; |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 2538 | |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2539 | if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255) |
| 2540 | return false; |
| 2541 | |
| 2542 | const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove"; |
| 2543 | return SelectCall(&I, IntrMemName); |
| 2544 | } |
| 2545 | case Intrinsic::memset: { |
| 2546 | const MemSetInst &MSI = cast<MemSetInst>(I); |
| 2547 | // Don't handle volatile. |
| 2548 | if (MSI.isVolatile()) |
| 2549 | return false; |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 2550 | |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2551 | if (!MSI.getLength()->getType()->isIntegerTy(32)) |
| 2552 | return false; |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 2553 | |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2554 | if (MSI.getDestAddressSpace() > 255) |
| 2555 | return false; |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 2556 | |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2557 | return SelectCall(&I, "memset"); |
| 2558 | } |
Chad Rosier | aa9cb9d | 2012-05-11 21:33:49 +0000 | [diff] [blame] | 2559 | case Intrinsic::trap: { |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2560 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get( |
Eli Bendersky | 2e2ce49 | 2013-01-30 16:30:19 +0000 | [diff] [blame] | 2561 | Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP)); |
Chad Rosier | aa9cb9d | 2012-05-11 21:33:49 +0000 | [diff] [blame] | 2562 | return true; |
| 2563 | } |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2564 | } |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Chad Rosier | ee7e452 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 2567 | bool ARMFastISel::SelectTrunc(const Instruction *I) { |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 2568 | // The high bits for a type smaller than the register size are assumed to be |
Chad Rosier | ee7e452 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 2569 | // undefined. |
| 2570 | Value *Op = I->getOperand(0); |
| 2571 | |
| 2572 | EVT SrcVT, DestVT; |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2573 | SrcVT = TLI.getValueType(DL, Op->getType(), true); |
| 2574 | DestVT = TLI.getValueType(DL, I->getType(), true); |
Chad Rosier | ee7e452 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 2575 | |
| 2576 | if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) |
| 2577 | return false; |
| 2578 | if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1) |
| 2579 | return false; |
| 2580 | |
| 2581 | unsigned SrcReg = getRegForValue(Op); |
| 2582 | if (!SrcReg) return false; |
| 2583 | |
| 2584 | // Because the high bits are undefined, a truncate doesn't generate |
| 2585 | // any code. |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 2586 | updateValueMap(I, SrcReg); |
Chad Rosier | ee7e452 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 2587 | return true; |
| 2588 | } |
| 2589 | |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 2590 | unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, |
Chad Rosier | 4489f94 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2591 | bool isZExt) { |
Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2592 | if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8) |
Chad Rosier | 4489f94 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2593 | return 0; |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2594 | if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1) |
Chad Rosier | 4489f94 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2595 | return 0; |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2596 | |
| 2597 | // Table of which combinations can be emitted as a single instruction, |
| 2598 | // and which will require two. |
| 2599 | static const uint8_t isSingleInstrTbl[3][2][2][2] = { |
| 2600 | // ARM Thumb |
| 2601 | // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops |
| 2602 | // ext: s z s z s z s z |
| 2603 | /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } }, |
| 2604 | /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }, |
| 2605 | /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } } |
| 2606 | }; |
| 2607 | |
| 2608 | // Target registers for: |
| 2609 | // - For ARM can never be PC. |
| 2610 | // - For 16-bit Thumb are restricted to lower 8 registers. |
| 2611 | // - For 32-bit Thumb are restricted to non-SP and non-PC. |
| 2612 | static const TargetRegisterClass *RCTbl[2][2] = { |
| 2613 | // Instructions: Two Single |
| 2614 | /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass }, |
| 2615 | /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass } |
| 2616 | }; |
| 2617 | |
| 2618 | // Table governing the instruction(s) to be emitted. |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2619 | static const struct InstructionTable { |
| 2620 | uint32_t Opc : 16; |
| 2621 | uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0. |
| 2622 | uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi. |
| 2623 | uint32_t Imm : 8; // All instructions have either a shift or a mask. |
| 2624 | } IT[2][2][3][2] = { |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2625 | { // Two instructions (first is left shift, second is in this table). |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2626 | { // ARM Opc S Shift Imm |
| 2627 | /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 }, |
| 2628 | /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } }, |
| 2629 | /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 }, |
| 2630 | /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } }, |
| 2631 | /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 }, |
| 2632 | /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } } |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2633 | }, |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2634 | { // Thumb Opc S Shift Imm |
| 2635 | /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 }, |
| 2636 | /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } }, |
| 2637 | /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 }, |
| 2638 | /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } }, |
| 2639 | /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 }, |
| 2640 | /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } } |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2641 | } |
| 2642 | }, |
| 2643 | { // Single instruction. |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2644 | { // ARM Opc S Shift Imm |
| 2645 | /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 }, |
| 2646 | /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } }, |
| 2647 | /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 }, |
| 2648 | /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } }, |
| 2649 | /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 }, |
| 2650 | /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } } |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2651 | }, |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2652 | { // Thumb Opc S Shift Imm |
| 2653 | /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 }, |
| 2654 | /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } }, |
| 2655 | /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 }, |
| 2656 | /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } }, |
| 2657 | /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 }, |
| 2658 | /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } } |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | }; |
| 2662 | |
| 2663 | unsigned SrcBits = SrcVT.getSizeInBits(); |
| 2664 | unsigned DestBits = DestVT.getSizeInBits(); |
JF Bastien | 60a2442 | 2013-06-08 00:51:51 +0000 | [diff] [blame] | 2665 | (void) DestBits; |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2666 | assert((SrcBits < DestBits) && "can only extend to larger types"); |
| 2667 | assert((DestBits == 32 || DestBits == 16 || DestBits == 8) && |
| 2668 | "other sizes unimplemented"); |
| 2669 | assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) && |
| 2670 | "other sizes unimplemented"); |
| 2671 | |
| 2672 | bool hasV6Ops = Subtarget->hasV6Ops(); |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2673 | unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2} |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2674 | assert((Bitness < 3) && "sanity-check table bounds"); |
| 2675 | |
| 2676 | bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt]; |
| 2677 | const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr]; |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2678 | const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt]; |
| 2679 | unsigned Opc = ITP->Opc; |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2680 | assert(ARM::KILL != Opc && "Invalid table entry"); |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2681 | unsigned hasS = ITP->hasS; |
| 2682 | ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift; |
| 2683 | assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) && |
| 2684 | "only MOVsi has shift operand addressing mode"); |
| 2685 | unsigned Imm = ITP->Imm; |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2686 | |
| 2687 | // 16-bit Thumb instructions always set CPSR (unless they're in an IT block). |
| 2688 | bool setsCPSR = &ARM::tGPRRegClass == RC; |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2689 | unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi; |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2690 | unsigned ResultReg; |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2691 | // MOVsi encodes shift and immediate in shift operand addressing mode. |
| 2692 | // The following condition has the same value when emitting two |
| 2693 | // instruction sequences: both are shifts. |
| 2694 | bool ImmIsSO = (Shift != ARM_AM::no_shift); |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2695 | |
| 2696 | // Either one or two instructions are emitted. |
| 2697 | // They're always of the form: |
| 2698 | // dst = in OP imm |
| 2699 | // CPSR is set only by 16-bit Thumb instructions. |
| 2700 | // Predicate, if any, is AL. |
| 2701 | // S bit, if available, is always 0. |
| 2702 | // When two are emitted the first's result will feed as the second's input, |
| 2703 | // that value is then dead. |
| 2704 | unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2; |
| 2705 | for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) { |
| 2706 | ResultReg = createResultReg(RC); |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2707 | bool isLsl = (0 == Instr) && !isSingleInstr; |
| 2708 | unsigned Opcode = isLsl ? LSLOpc : Opc; |
| 2709 | ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift; |
| 2710 | unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm; |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2711 | bool isKill = 1 == Instr; |
| 2712 | MachineInstrBuilder MIB = BuildMI( |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2713 | *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg); |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2714 | if (setsCPSR) |
| 2715 | MIB.addReg(ARM::CPSR, RegState::Define); |
Jim Grosbach | 3fa7491 | 2013-08-16 23:37:36 +0000 | [diff] [blame] | 2716 | SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR); |
JF Bastien | cd4c64d | 2013-07-17 05:46:46 +0000 | [diff] [blame] | 2717 | AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc)); |
JF Bastien | 06ce03d | 2013-06-07 20:10:37 +0000 | [diff] [blame] | 2718 | if (hasS) |
| 2719 | AddDefaultCC(MIB); |
| 2720 | // Second instruction consumes the first's result. |
| 2721 | SrcReg = ResultReg; |
Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Chad Rosier | 4489f94 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2724 | return ResultReg; |
| 2725 | } |
| 2726 | |
| 2727 | bool ARMFastISel::SelectIntExt(const Instruction *I) { |
| 2728 | // On ARM, in general, integer casts don't involve legal types; this code |
| 2729 | // handles promotable integers. |
Chad Rosier | 4489f94 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2730 | Type *DestTy = I->getType(); |
| 2731 | Value *Src = I->getOperand(0); |
| 2732 | Type *SrcTy = Src->getType(); |
| 2733 | |
Chad Rosier | 4489f94 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2734 | bool isZExt = isa<ZExtInst>(I); |
| 2735 | unsigned SrcReg = getRegForValue(Src); |
| 2736 | if (!SrcReg) return false; |
| 2737 | |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 2738 | EVT SrcEVT, DestEVT; |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2739 | SrcEVT = TLI.getValueType(DL, SrcTy, true); |
| 2740 | DestEVT = TLI.getValueType(DL, DestTy, true); |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 2741 | if (!SrcEVT.isSimple()) return false; |
| 2742 | if (!DestEVT.isSimple()) return false; |
Patrik Hagglund | c494d24 | 2012-12-17 14:30:06 +0000 | [diff] [blame] | 2743 | |
Chad Rosier | 62a144f | 2012-12-17 19:59:43 +0000 | [diff] [blame] | 2744 | MVT SrcVT = SrcEVT.getSimpleVT(); |
| 2745 | MVT DestVT = DestEVT.getSimpleVT(); |
Chad Rosier | 4489f94 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2746 | unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt); |
| 2747 | if (ResultReg == 0) return false; |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 2748 | updateValueMap(I, ResultReg); |
Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2749 | return true; |
| 2750 | } |
| 2751 | |
Jush Lu | 4705da9 | 2012-08-03 02:37:48 +0000 | [diff] [blame] | 2752 | bool ARMFastISel::SelectShift(const Instruction *I, |
| 2753 | ARM_AM::ShiftOpc ShiftTy) { |
| 2754 | // We handle thumb2 mode by target independent selector |
| 2755 | // or SelectionDAG ISel. |
| 2756 | if (isThumb2) |
| 2757 | return false; |
| 2758 | |
| 2759 | // Only handle i32 now. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2760 | EVT DestVT = TLI.getValueType(DL, I->getType(), true); |
Jush Lu | 4705da9 | 2012-08-03 02:37:48 +0000 | [diff] [blame] | 2761 | if (DestVT != MVT::i32) |
| 2762 | return false; |
| 2763 | |
| 2764 | unsigned Opc = ARM::MOVsr; |
| 2765 | unsigned ShiftImm; |
| 2766 | Value *Src2Value = I->getOperand(1); |
| 2767 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) { |
| 2768 | ShiftImm = CI->getZExtValue(); |
| 2769 | |
| 2770 | // Fall back to selection DAG isel if the shift amount |
| 2771 | // is zero or greater than the width of the value type. |
| 2772 | if (ShiftImm == 0 || ShiftImm >=32) |
| 2773 | return false; |
| 2774 | |
| 2775 | Opc = ARM::MOVsi; |
| 2776 | } |
| 2777 | |
| 2778 | Value *Src1Value = I->getOperand(0); |
| 2779 | unsigned Reg1 = getRegForValue(Src1Value); |
| 2780 | if (Reg1 == 0) return false; |
| 2781 | |
Nadav Rotem | a8e15b0 | 2012-09-06 11:13:55 +0000 | [diff] [blame] | 2782 | unsigned Reg2 = 0; |
Jush Lu | 4705da9 | 2012-08-03 02:37:48 +0000 | [diff] [blame] | 2783 | if (Opc == ARM::MOVsr) { |
| 2784 | Reg2 = getRegForValue(Src2Value); |
| 2785 | if (Reg2 == 0) return false; |
| 2786 | } |
| 2787 | |
JF Bastien | 13969d0 | 2013-05-29 15:45:47 +0000 | [diff] [blame] | 2788 | unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass); |
Jush Lu | 4705da9 | 2012-08-03 02:37:48 +0000 | [diff] [blame] | 2789 | if(ResultReg == 0) return false; |
| 2790 | |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2791 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
Jush Lu | 4705da9 | 2012-08-03 02:37:48 +0000 | [diff] [blame] | 2792 | TII.get(Opc), ResultReg) |
| 2793 | .addReg(Reg1); |
| 2794 | |
| 2795 | if (Opc == ARM::MOVsi) |
| 2796 | MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm)); |
| 2797 | else if (Opc == ARM::MOVsr) { |
| 2798 | MIB.addReg(Reg2); |
| 2799 | MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0)); |
| 2800 | } |
| 2801 | |
| 2802 | AddOptionalDefs(MIB); |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 2803 | updateValueMap(I, ResultReg); |
Jush Lu | 4705da9 | 2012-08-03 02:37:48 +0000 | [diff] [blame] | 2804 | return true; |
| 2805 | } |
| 2806 | |
Eric Christopher | c3e118e | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 2807 | // TODO: SoftFP support. |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 2808 | bool ARMFastISel::fastSelectInstruction(const Instruction *I) { |
Eric Christopher | 2ff757d | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 2809 | |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 2810 | switch (I->getOpcode()) { |
Eric Christopher | 00202ee | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 2811 | case Instruction::Load: |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2812 | return SelectLoad(I); |
Eric Christopher | fde5a3d | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 2813 | case Instruction::Store: |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2814 | return SelectStore(I); |
Eric Christopher | 6aaed72 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 2815 | case Instruction::Br: |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2816 | return SelectBranch(I); |
Chad Rosier | ded4c99 | 2012-02-07 23:56:08 +0000 | [diff] [blame] | 2817 | case Instruction::IndirectBr: |
| 2818 | return SelectIndirectBr(I); |
Eric Christopher | c3e9c40 | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 2819 | case Instruction::ICmp: |
| 2820 | case Instruction::FCmp: |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2821 | return SelectCmp(I); |
Eric Christopher | f14b9bf | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 2822 | case Instruction::FPExt: |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2823 | return SelectFPExt(I); |
Eric Christopher | 5903c0b | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 2824 | case Instruction::FPTrunc: |
Eric Christopher | 29ab6d1 | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2825 | return SelectFPTrunc(I); |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 2826 | case Instruction::SIToFP: |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 2827 | return SelectIToFP(I, /*isSigned*/ true); |
Chad Rosier | a8a8ac5 | 2012-02-03 19:42:52 +0000 | [diff] [blame] | 2828 | case Instruction::UIToFP: |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 2829 | return SelectIToFP(I, /*isSigned*/ false); |
Eric Christopher | 6e3eeba | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 2830 | case Instruction::FPToSI: |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 2831 | return SelectFPToI(I, /*isSigned*/ true); |
Chad Rosier | 41f0e78 | 2012-02-03 20:27:51 +0000 | [diff] [blame] | 2832 | case Instruction::FPToUI: |
Chad Rosier | e023d5d | 2012-02-03 21:14:11 +0000 | [diff] [blame] | 2833 | return SelectFPToI(I, /*isSigned*/ false); |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 2834 | case Instruction::Add: |
| 2835 | return SelectBinaryIntOp(I, ISD::ADD); |
Chad Rosier | bd47125 | 2012-02-08 02:29:21 +0000 | [diff] [blame] | 2836 | case Instruction::Or: |
| 2837 | return SelectBinaryIntOp(I, ISD::OR); |
Chad Rosier | 0ee8c51 | 2012-02-08 02:45:44 +0000 | [diff] [blame] | 2838 | case Instruction::Sub: |
| 2839 | return SelectBinaryIntOp(I, ISD::SUB); |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 2840 | case Instruction::FAdd: |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 2841 | return SelectBinaryFPOp(I, ISD::FADD); |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 2842 | case Instruction::FSub: |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 2843 | return SelectBinaryFPOp(I, ISD::FSUB); |
Eric Christopher | 24dc27f | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 2844 | case Instruction::FMul: |
Chad Rosier | 685b20c | 2012-02-06 23:50:07 +0000 | [diff] [blame] | 2845 | return SelectBinaryFPOp(I, ISD::FMUL); |
Eric Christopher | 8b91266 | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2846 | case Instruction::SDiv: |
Chad Rosier | aaa55a8 | 2012-02-03 21:07:27 +0000 | [diff] [blame] | 2847 | return SelectDiv(I, /*isSigned*/ true); |
| 2848 | case Instruction::UDiv: |
| 2849 | return SelectDiv(I, /*isSigned*/ false); |
Eric Christopher | eae1b38 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 2850 | case Instruction::SRem: |
Chad Rosier | b84a4b4 | 2012-02-03 21:23:45 +0000 | [diff] [blame] | 2851 | return SelectRem(I, /*isSigned*/ true); |
| 2852 | case Instruction::URem: |
| 2853 | return SelectRem(I, /*isSigned*/ false); |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2854 | case Instruction::Call: |
Chad Rosier | a7ebc56 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2855 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2856 | return SelectIntrinsicCall(*II); |
Eric Christopher | 78f8d4e | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2857 | return SelectCall(I); |
Eric Christopher | 511aa31 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 2858 | case Instruction::Select: |
| 2859 | return SelectSelect(I); |
Eric Christopher | 93bbe65 | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2860 | case Instruction::Ret: |
| 2861 | return SelectRet(I); |
Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2862 | case Instruction::Trunc: |
Chad Rosier | ee7e452 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 2863 | return SelectTrunc(I); |
Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2864 | case Instruction::ZExt: |
| 2865 | case Instruction::SExt: |
Chad Rosier | ee7e452 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 2866 | return SelectIntExt(I); |
Jush Lu | 4705da9 | 2012-08-03 02:37:48 +0000 | [diff] [blame] | 2867 | case Instruction::Shl: |
| 2868 | return SelectShift(I, ARM_AM::lsl); |
| 2869 | case Instruction::LShr: |
| 2870 | return SelectShift(I, ARM_AM::lsr); |
| 2871 | case Instruction::AShr: |
| 2872 | return SelectShift(I, ARM_AM::asr); |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 2873 | default: break; |
| 2874 | } |
| 2875 | return false; |
| 2876 | } |
| 2877 | |
JF Bastien | 3c6bb8e | 2013-06-11 22:13:46 +0000 | [diff] [blame] | 2878 | namespace { |
| 2879 | // This table describes sign- and zero-extend instructions which can be |
| 2880 | // folded into a preceding load. All of these extends have an immediate |
| 2881 | // (sometimes a mask and sometimes a shift) that's applied after |
| 2882 | // extension. |
| 2883 | const struct FoldableLoadExtendsStruct { |
| 2884 | uint16_t Opc[2]; // ARM, Thumb. |
| 2885 | uint8_t ExpectedImm; |
| 2886 | uint8_t isZExt : 1; |
| 2887 | uint8_t ExpectedVT : 7; |
| 2888 | } FoldableLoadExtends[] = { |
| 2889 | { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 }, |
| 2890 | { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 }, |
| 2891 | { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 }, |
| 2892 | { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 }, |
| 2893 | { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 } |
| 2894 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 2895 | } |
JF Bastien | 3c6bb8e | 2013-06-11 22:13:46 +0000 | [diff] [blame] | 2896 | |
Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2897 | /// \brief The specified machine instr operand is a vreg, and that |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 2898 | /// vreg is being provided by the specified load instruction. If possible, |
| 2899 | /// try to fold the load as an operand to the instruction, returning true if |
| 2900 | /// successful. |
Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2901 | bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, |
| 2902 | const LoadInst *LI) { |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 2903 | // Verify we have a legal type before going any further. |
| 2904 | MVT VT; |
| 2905 | if (!isLoadTypeLegal(LI->getType(), VT)) |
| 2906 | return false; |
| 2907 | |
| 2908 | // Combine load followed by zero- or sign-extend. |
| 2909 | // ldrb r1, [r0] ldrb r1, [r0] |
| 2910 | // uxtb r2, r1 => |
| 2911 | // mov r3, r2 mov r3, r1 |
JF Bastien | 3c6bb8e | 2013-06-11 22:13:46 +0000 | [diff] [blame] | 2912 | if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm()) |
| 2913 | return false; |
| 2914 | const uint64_t Imm = MI->getOperand(2).getImm(); |
| 2915 | |
| 2916 | bool Found = false; |
| 2917 | bool isZExt; |
| 2918 | for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends); |
| 2919 | i != e; ++i) { |
| 2920 | if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() && |
| 2921 | (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm && |
| 2922 | MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) { |
| 2923 | Found = true; |
| 2924 | isZExt = FoldableLoadExtends[i].isZExt; |
| 2925 | } |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 2926 | } |
JF Bastien | 3c6bb8e | 2013-06-11 22:13:46 +0000 | [diff] [blame] | 2927 | if (!Found) return false; |
| 2928 | |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 2929 | // See if we can handle this address. |
| 2930 | Address Addr; |
| 2931 | if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false; |
Jush Lu | ac96b76 | 2012-06-14 06:08:19 +0000 | [diff] [blame] | 2932 | |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 2933 | unsigned ResultReg = MI->getOperand(0).getReg(); |
Chad Rosier | 563de60 | 2011-12-13 19:22:14 +0000 | [diff] [blame] | 2934 | if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false)) |
Chad Rosier | c8cfd3a | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 2935 | return false; |
| 2936 | MI->eraseFromParent(); |
| 2937 | return true; |
| 2938 | } |
| 2939 | |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 2940 | unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV, |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 2941 | unsigned Align, MVT VT) { |
Peter Collingbourne | 97aae40 | 2015-10-26 18:23:16 +0000 | [diff] [blame] | 2942 | bool UseGOT_PREL = |
Peter Collingbourne | 99fac80 | 2015-10-26 20:46:44 +0000 | [diff] [blame] | 2943 | !(GV->hasHiddenVisibility() || GV->hasLocalLinkage()); |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 2944 | |
Peter Collingbourne | 97aae40 | 2015-10-26 18:23:16 +0000 | [diff] [blame] | 2945 | LLVMContext *Context = &MF->getFunction()->getContext(); |
| 2946 | unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); |
| 2947 | unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; |
| 2948 | ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create( |
| 2949 | GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj, |
| 2950 | UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier, |
| 2951 | /*AddCurrentAddress=*/UseGOT_PREL); |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 2952 | |
Peter Collingbourne | 97aae40 | 2015-10-26 18:23:16 +0000 | [diff] [blame] | 2953 | unsigned ConstAlign = |
| 2954 | MF->getDataLayout().getPrefTypeAlignment(Type::getInt32PtrTy(*Context)); |
| 2955 | unsigned Idx = MF->getConstantPool()->getConstantPoolIndex(CPV, ConstAlign); |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 2956 | |
Peter Collingbourne | 97aae40 | 2015-10-26 18:23:16 +0000 | [diff] [blame] | 2957 | unsigned TempReg = MF->getRegInfo().createVirtualRegister(&ARM::rGPRRegClass); |
| 2958 | unsigned Opc = isThumb2 ? ARM::t2LDRpci : ARM::LDRcp; |
| 2959 | MachineInstrBuilder MIB = |
| 2960 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), TempReg) |
| 2961 | .addConstantPoolIndex(Idx); |
| 2962 | if (Opc == ARM::LDRcp) |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 2963 | MIB.addImm(0); |
Peter Collingbourne | 97aae40 | 2015-10-26 18:23:16 +0000 | [diff] [blame] | 2964 | AddDefaultPred(MIB); |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 2965 | |
Peter Collingbourne | 97aae40 | 2015-10-26 18:23:16 +0000 | [diff] [blame] | 2966 | // Fix the address by adding pc. |
| 2967 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 2968 | Opc = Subtarget->isThumb() ? ARM::tPICADD : UseGOT_PREL ? ARM::PICLDR |
| 2969 | : ARM::PICADD; |
| 2970 | DestReg = constrainOperandRegClass(TII.get(Opc), DestReg, 0); |
| 2971 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) |
| 2972 | .addReg(TempReg) |
| 2973 | .addImm(ARMPCLabelIndex); |
| 2974 | if (!Subtarget->isThumb()) |
| 2975 | AddDefaultPred(MIB); |
| 2976 | |
| 2977 | if (UseGOT_PREL && Subtarget->isThumb()) { |
| 2978 | unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 2979 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2980 | TII.get(ARM::t2LDRi12), NewDestReg) |
| 2981 | .addReg(DestReg) |
| 2982 | .addImm(0); |
| 2983 | DestReg = NewDestReg; |
| 2984 | AddOptionalDefs(MIB); |
| 2985 | } |
| 2986 | return DestReg; |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 2989 | bool ARMFastISel::fastLowerArguments() { |
Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 2990 | if (!FuncInfo.CanLowerReturn) |
| 2991 | return false; |
| 2992 | |
| 2993 | const Function *F = FuncInfo.Fn; |
| 2994 | if (F->isVarArg()) |
| 2995 | return false; |
| 2996 | |
| 2997 | CallingConv::ID CC = F->getCallingConv(); |
| 2998 | switch (CC) { |
| 2999 | default: |
| 3000 | return false; |
| 3001 | case CallingConv::Fast: |
| 3002 | case CallingConv::C: |
| 3003 | case CallingConv::ARM_AAPCS_VFP: |
| 3004 | case CallingConv::ARM_AAPCS: |
| 3005 | case CallingConv::ARM_APCS: |
| 3006 | break; |
| 3007 | } |
| 3008 | |
| 3009 | // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments |
| 3010 | // which are passed in r0 - r3. |
| 3011 | unsigned Idx = 1; |
| 3012 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 3013 | I != E; ++I, ++Idx) { |
| 3014 | if (Idx > 4) |
| 3015 | return false; |
| 3016 | |
| 3017 | if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) || |
| 3018 | F->getAttributes().hasAttribute(Idx, Attribute::StructRet) || |
| 3019 | F->getAttributes().hasAttribute(Idx, Attribute::ByVal)) |
| 3020 | return false; |
| 3021 | |
| 3022 | Type *ArgTy = I->getType(); |
| 3023 | if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) |
| 3024 | return false; |
| 3025 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 3026 | EVT ArgVT = TLI.getValueType(DL, ArgTy); |
Chad Rosier | 1b33e8d | 2013-02-26 01:05:31 +0000 | [diff] [blame] | 3027 | if (!ArgVT.isSimple()) return false; |
Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 3028 | switch (ArgVT.getSimpleVT().SimpleTy) { |
| 3029 | case MVT::i8: |
| 3030 | case MVT::i16: |
| 3031 | case MVT::i32: |
| 3032 | break; |
| 3033 | default: |
| 3034 | return false; |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 3039 | static const MCPhysReg GPRArgRegs[] = { |
Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 3040 | ARM::R0, ARM::R1, ARM::R2, ARM::R3 |
| 3041 | }; |
| 3042 | |
Jim Grosbach | d69f3ed | 2013-08-16 23:37:23 +0000 | [diff] [blame] | 3043 | const TargetRegisterClass *RC = &ARM::rGPRRegClass; |
Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 3044 | Idx = 0; |
| 3045 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 3046 | I != E; ++I, ++Idx) { |
Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 3047 | unsigned SrcReg = GPRArgRegs[Idx]; |
| 3048 | unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC); |
| 3049 | // FIXME: Unfortunately it's necessary to emit a copy from the livein copy. |
| 3050 | // Without this, EmitLiveInCopies may eliminate the livein if its only |
| 3051 | // use is a bitcast (which isn't turned into an instruction). |
| 3052 | unsigned ResultReg = createResultReg(RC); |
Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 3053 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3054 | TII.get(TargetOpcode::COPY), |
Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 3055 | ResultReg).addReg(DstReg, getKillRegState(true)); |
Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 3056 | updateValueMap(&*I, ResultReg); |
Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | return true; |
| 3060 | } |
| 3061 | |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 3062 | namespace llvm { |
Bob Wilson | 3e6fa46 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 3063 | FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo, |
| 3064 | const TargetLibraryInfo *libInfo) { |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 3065 | if (funcInfo.MF->getSubtarget<ARMSubtarget>().useFastISel()) |
Bob Wilson | 3e6fa46 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 3066 | return new ARMFastISel(funcInfo, libInfo); |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 3067 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 3068 | return nullptr; |
Eric Christopher | 84bdfd8 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 3069 | } |
| 3070 | } |