Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1 | //===-- X86FastISel.cpp - X86 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 X86-specific support for the FastISel class. Much |
| 11 | // of the target-specific code is generated by tablegen in the file |
| 12 | // X86GenFastISel.inc, which is #included here. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "X86.h" |
| 17 | #include "X86CallingConv.h" |
| 18 | #include "X86InstrBuilder.h" |
| 19 | #include "X86InstrInfo.h" |
| 20 | #include "X86MachineFunctionInfo.h" |
| 21 | #include "X86RegisterInfo.h" |
| 22 | #include "X86Subtarget.h" |
| 23 | #include "X86TargetMachine.h" |
| 24 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/FastISel.h" |
| 26 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
| 27 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 30 | #include "llvm/IR/CallSite.h" |
| 31 | #include "llvm/IR/CallingConv.h" |
Reid Kleckner | 2886580 | 2016-04-14 18:29:59 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DebugInfo.h" |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DerivedTypes.h" |
| 34 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 35 | #include "llvm/IR/GlobalAlias.h" |
| 36 | #include "llvm/IR/GlobalVariable.h" |
| 37 | #include "llvm/IR/Instructions.h" |
| 38 | #include "llvm/IR/IntrinsicInst.h" |
| 39 | #include "llvm/IR/Operator.h" |
David Majnemer | ca19485 | 2015-02-10 22:00:34 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MCAsmInfo.h" |
Rafael Espindola | ce4c2bc | 2015-06-23 12:21:54 +0000 | [diff] [blame] | 41 | #include "llvm/MC/MCSymbol.h" |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ErrorHandling.h" |
| 43 | #include "llvm/Target/TargetOptions.h" |
| 44 | using namespace llvm; |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | class X86FastISel final : public FastISel { |
| 49 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 50 | /// make the right decision when generating code for different targets. |
| 51 | const X86Subtarget *Subtarget; |
| 52 | |
| 53 | /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87 |
| 54 | /// floating point ops. |
| 55 | /// When SSE is available, use it for f32 operations. |
| 56 | /// When SSE2 is available, use it for f64 operations. |
| 57 | bool X86ScalarSSEf64; |
| 58 | bool X86ScalarSSEf32; |
| 59 | |
| 60 | public: |
| 61 | explicit X86FastISel(FunctionLoweringInfo &funcInfo, |
| 62 | const TargetLibraryInfo *libInfo) |
Eric Christopher | a1c535b | 2015-02-02 23:03:45 +0000 | [diff] [blame] | 63 | : FastISel(funcInfo, libInfo) { |
| 64 | Subtarget = &funcInfo.MF->getSubtarget<X86Subtarget>(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 65 | X86ScalarSSEf64 = Subtarget->hasSSE2(); |
| 66 | X86ScalarSSEf32 = Subtarget->hasSSE1(); |
| 67 | } |
| 68 | |
| 69 | bool fastSelectInstruction(const Instruction *I) override; |
| 70 | |
| 71 | /// \brief The specified machine instr operand is a vreg, and that |
| 72 | /// vreg is being provided by the specified load instruction. If possible, |
| 73 | /// try to fold the load as an operand to the instruction, returning true if |
| 74 | /// possible. |
| 75 | bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, |
| 76 | const LoadInst *LI) override; |
| 77 | |
| 78 | bool fastLowerArguments() override; |
| 79 | bool fastLowerCall(CallLoweringInfo &CLI) override; |
| 80 | bool fastLowerIntrinsicCall(const IntrinsicInst *II) override; |
| 81 | |
| 82 | #include "X86GenFastISel.inc" |
| 83 | |
| 84 | private: |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 85 | bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT, |
| 86 | const DebugLoc &DL); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 87 | |
Pete Cooper | d0dae3e | 2015-05-05 23:41:53 +0000 | [diff] [blame] | 88 | bool X86FastEmitLoad(EVT VT, X86AddressMode &AM, MachineMemOperand *MMO, |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 89 | unsigned &ResultReg, unsigned Alignment = 1); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 90 | |
Pete Cooper | d0dae3e | 2015-05-05 23:41:53 +0000 | [diff] [blame] | 91 | bool X86FastEmitStore(EVT VT, const Value *Val, X86AddressMode &AM, |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 92 | MachineMemOperand *MMO = nullptr, bool Aligned = false); |
| 93 | bool X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill, |
Pete Cooper | d0dae3e | 2015-05-05 23:41:53 +0000 | [diff] [blame] | 94 | X86AddressMode &AM, |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 95 | MachineMemOperand *MMO = nullptr, bool Aligned = false); |
| 96 | |
| 97 | bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT, |
| 98 | unsigned &ResultReg); |
| 99 | |
| 100 | bool X86SelectAddress(const Value *V, X86AddressMode &AM); |
| 101 | bool X86SelectCallAddress(const Value *V, X86AddressMode &AM); |
| 102 | |
| 103 | bool X86SelectLoad(const Instruction *I); |
| 104 | |
| 105 | bool X86SelectStore(const Instruction *I); |
| 106 | |
| 107 | bool X86SelectRet(const Instruction *I); |
| 108 | |
| 109 | bool X86SelectCmp(const Instruction *I); |
| 110 | |
| 111 | bool X86SelectZExt(const Instruction *I); |
| 112 | |
| 113 | bool X86SelectBranch(const Instruction *I); |
| 114 | |
| 115 | bool X86SelectShift(const Instruction *I); |
| 116 | |
| 117 | bool X86SelectDivRem(const Instruction *I); |
| 118 | |
| 119 | bool X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I); |
| 120 | |
| 121 | bool X86FastEmitSSESelect(MVT RetVT, const Instruction *I); |
| 122 | |
| 123 | bool X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I); |
| 124 | |
| 125 | bool X86SelectSelect(const Instruction *I); |
| 126 | |
| 127 | bool X86SelectTrunc(const Instruction *I); |
| 128 | |
Andrea Di Biagio | 62622d2 | 2015-02-10 12:04:41 +0000 | [diff] [blame] | 129 | bool X86SelectFPExtOrFPTrunc(const Instruction *I, unsigned Opc, |
| 130 | const TargetRegisterClass *RC); |
| 131 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 132 | bool X86SelectFPExt(const Instruction *I); |
| 133 | bool X86SelectFPTrunc(const Instruction *I); |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 134 | bool X86SelectSIToFP(const Instruction *I); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 135 | |
| 136 | const X86InstrInfo *getInstrInfo() const { |
Eric Christopher | a1c535b | 2015-02-02 23:03:45 +0000 | [diff] [blame] | 137 | return Subtarget->getInstrInfo(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 138 | } |
| 139 | const X86TargetMachine *getTargetMachine() const { |
| 140 | return static_cast<const X86TargetMachine *>(&TM); |
| 141 | } |
| 142 | |
| 143 | bool handleConstantAddresses(const Value *V, X86AddressMode &AM); |
| 144 | |
| 145 | unsigned X86MaterializeInt(const ConstantInt *CI, MVT VT); |
| 146 | unsigned X86MaterializeFP(const ConstantFP *CFP, MVT VT); |
| 147 | unsigned X86MaterializeGV(const GlobalValue *GV, MVT VT); |
| 148 | unsigned fastMaterializeConstant(const Constant *C) override; |
| 149 | |
| 150 | unsigned fastMaterializeAlloca(const AllocaInst *C) override; |
| 151 | |
| 152 | unsigned fastMaterializeFloatZero(const ConstantFP *CF) override; |
| 153 | |
| 154 | /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is |
| 155 | /// computed in an SSE register, not on the X87 floating point stack. |
| 156 | bool isScalarFPTypeInSSEReg(EVT VT) const { |
| 157 | return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2 |
| 158 | (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1 |
| 159 | } |
| 160 | |
| 161 | bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false); |
| 162 | |
| 163 | bool IsMemcpySmall(uint64_t Len); |
| 164 | |
| 165 | bool TryEmitSmallMemcpy(X86AddressMode DestAM, |
| 166 | X86AddressMode SrcAM, uint64_t Len); |
| 167 | |
| 168 | bool foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I, |
| 169 | const Value *Cond); |
Pete Cooper | d0dae3e | 2015-05-05 23:41:53 +0000 | [diff] [blame] | 170 | |
| 171 | const MachineInstrBuilder &addFullAddress(const MachineInstrBuilder &MIB, |
| 172 | X86AddressMode &AM); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } // end anonymous namespace. |
| 176 | |
| 177 | static std::pair<X86::CondCode, bool> |
| 178 | getX86ConditionCode(CmpInst::Predicate Predicate) { |
| 179 | X86::CondCode CC = X86::COND_INVALID; |
| 180 | bool NeedSwap = false; |
| 181 | switch (Predicate) { |
| 182 | default: break; |
| 183 | // Floating-point Predicates |
| 184 | case CmpInst::FCMP_UEQ: CC = X86::COND_E; break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 185 | case CmpInst::FCMP_OLT: NeedSwap = true; LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 186 | case CmpInst::FCMP_OGT: CC = X86::COND_A; break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 187 | case CmpInst::FCMP_OLE: NeedSwap = true; LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 188 | case CmpInst::FCMP_OGE: CC = X86::COND_AE; break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 189 | case CmpInst::FCMP_UGT: NeedSwap = true; LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 190 | case CmpInst::FCMP_ULT: CC = X86::COND_B; break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 191 | case CmpInst::FCMP_UGE: NeedSwap = true; LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 192 | case CmpInst::FCMP_ULE: CC = X86::COND_BE; break; |
| 193 | case CmpInst::FCMP_ONE: CC = X86::COND_NE; break; |
| 194 | case CmpInst::FCMP_UNO: CC = X86::COND_P; break; |
| 195 | case CmpInst::FCMP_ORD: CC = X86::COND_NP; break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 196 | case CmpInst::FCMP_OEQ: LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 197 | case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break; |
| 198 | |
| 199 | // Integer Predicates |
| 200 | case CmpInst::ICMP_EQ: CC = X86::COND_E; break; |
| 201 | case CmpInst::ICMP_NE: CC = X86::COND_NE; break; |
| 202 | case CmpInst::ICMP_UGT: CC = X86::COND_A; break; |
| 203 | case CmpInst::ICMP_UGE: CC = X86::COND_AE; break; |
| 204 | case CmpInst::ICMP_ULT: CC = X86::COND_B; break; |
| 205 | case CmpInst::ICMP_ULE: CC = X86::COND_BE; break; |
| 206 | case CmpInst::ICMP_SGT: CC = X86::COND_G; break; |
| 207 | case CmpInst::ICMP_SGE: CC = X86::COND_GE; break; |
| 208 | case CmpInst::ICMP_SLT: CC = X86::COND_L; break; |
| 209 | case CmpInst::ICMP_SLE: CC = X86::COND_LE; break; |
| 210 | } |
| 211 | |
| 212 | return std::make_pair(CC, NeedSwap); |
| 213 | } |
| 214 | |
| 215 | static std::pair<unsigned, bool> |
| 216 | getX86SSEConditionCode(CmpInst::Predicate Predicate) { |
| 217 | unsigned CC; |
| 218 | bool NeedSwap = false; |
| 219 | |
| 220 | // SSE Condition code mapping: |
| 221 | // 0 - EQ |
| 222 | // 1 - LT |
| 223 | // 2 - LE |
| 224 | // 3 - UNORD |
| 225 | // 4 - NEQ |
| 226 | // 5 - NLT |
| 227 | // 6 - NLE |
| 228 | // 7 - ORD |
| 229 | switch (Predicate) { |
| 230 | default: llvm_unreachable("Unexpected predicate"); |
| 231 | case CmpInst::FCMP_OEQ: CC = 0; break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 232 | case CmpInst::FCMP_OGT: NeedSwap = true; LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 233 | case CmpInst::FCMP_OLT: CC = 1; break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 234 | case CmpInst::FCMP_OGE: NeedSwap = true; LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 235 | case CmpInst::FCMP_OLE: CC = 2; break; |
| 236 | case CmpInst::FCMP_UNO: CC = 3; break; |
| 237 | case CmpInst::FCMP_UNE: CC = 4; break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 238 | case CmpInst::FCMP_ULE: NeedSwap = true; LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 239 | case CmpInst::FCMP_UGE: CC = 5; break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 240 | case CmpInst::FCMP_ULT: NeedSwap = true; LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 241 | case CmpInst::FCMP_UGT: CC = 6; break; |
| 242 | case CmpInst::FCMP_ORD: CC = 7; break; |
| 243 | case CmpInst::FCMP_UEQ: |
| 244 | case CmpInst::FCMP_ONE: CC = 8; break; |
| 245 | } |
| 246 | |
| 247 | return std::make_pair(CC, NeedSwap); |
| 248 | } |
| 249 | |
Pete Cooper | d0dae3e | 2015-05-05 23:41:53 +0000 | [diff] [blame] | 250 | /// \brief Adds a complex addressing mode to the given machine instr builder. |
| 251 | /// Note, this will constrain the index register. If its not possible to |
| 252 | /// constrain the given index register, then a new one will be created. The |
| 253 | /// IndexReg field of the addressing mode will be updated to match in this case. |
| 254 | const MachineInstrBuilder & |
| 255 | X86FastISel::addFullAddress(const MachineInstrBuilder &MIB, |
| 256 | X86AddressMode &AM) { |
| 257 | // First constrain the index register. It needs to be a GR64_NOSP. |
| 258 | AM.IndexReg = constrainOperandRegClass(MIB->getDesc(), AM.IndexReg, |
| 259 | MIB->getNumOperands() + |
| 260 | X86::AddrIndexReg); |
| 261 | return ::addFullAddress(MIB, AM); |
| 262 | } |
| 263 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 264 | /// \brief Check if it is possible to fold the condition from the XALU intrinsic |
| 265 | /// into the user. The condition code will only be updated on success. |
| 266 | bool X86FastISel::foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I, |
| 267 | const Value *Cond) { |
| 268 | if (!isa<ExtractValueInst>(Cond)) |
| 269 | return false; |
| 270 | |
| 271 | const auto *EV = cast<ExtractValueInst>(Cond); |
| 272 | if (!isa<IntrinsicInst>(EV->getAggregateOperand())) |
| 273 | return false; |
| 274 | |
| 275 | const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand()); |
| 276 | MVT RetVT; |
| 277 | const Function *Callee = II->getCalledFunction(); |
| 278 | Type *RetTy = |
| 279 | cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U); |
| 280 | if (!isTypeLegal(RetTy, RetVT)) |
| 281 | return false; |
| 282 | |
| 283 | if (RetVT != MVT::i32 && RetVT != MVT::i64) |
| 284 | return false; |
| 285 | |
| 286 | X86::CondCode TmpCC; |
| 287 | switch (II->getIntrinsicID()) { |
| 288 | default: return false; |
| 289 | case Intrinsic::sadd_with_overflow: |
| 290 | case Intrinsic::ssub_with_overflow: |
| 291 | case Intrinsic::smul_with_overflow: |
| 292 | case Intrinsic::umul_with_overflow: TmpCC = X86::COND_O; break; |
| 293 | case Intrinsic::uadd_with_overflow: |
| 294 | case Intrinsic::usub_with_overflow: TmpCC = X86::COND_B; break; |
| 295 | } |
| 296 | |
| 297 | // Check if both instructions are in the same basic block. |
| 298 | if (II->getParent() != I->getParent()) |
| 299 | return false; |
| 300 | |
| 301 | // Make sure nothing is in the way |
Duncan P. N. Exon Smith | d77de64 | 2015-10-19 21:48:29 +0000 | [diff] [blame] | 302 | BasicBlock::const_iterator Start(I); |
| 303 | BasicBlock::const_iterator End(II); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 304 | for (auto Itr = std::prev(Start); Itr != End; --Itr) { |
| 305 | // We only expect extractvalue instructions between the intrinsic and the |
| 306 | // instruction to be selected. |
| 307 | if (!isa<ExtractValueInst>(Itr)) |
| 308 | return false; |
| 309 | |
| 310 | // Check that the extractvalue operand comes from the intrinsic. |
| 311 | const auto *EVI = cast<ExtractValueInst>(Itr); |
| 312 | if (EVI->getAggregateOperand() != II) |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | CC = TmpCC; |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 321 | EVT evt = TLI.getValueType(DL, Ty, /*HandleUnknown=*/true); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 322 | if (evt == MVT::Other || !evt.isSimple()) |
| 323 | // Unhandled type. Halt "fast" selection and bail. |
| 324 | return false; |
| 325 | |
| 326 | VT = evt.getSimpleVT(); |
| 327 | // For now, require SSE/SSE2 for performing floating-point operations, |
| 328 | // since x87 requires additional work. |
| 329 | if (VT == MVT::f64 && !X86ScalarSSEf64) |
| 330 | return false; |
| 331 | if (VT == MVT::f32 && !X86ScalarSSEf32) |
| 332 | return false; |
| 333 | // Similarly, no f80 support yet. |
| 334 | if (VT == MVT::f80) |
| 335 | return false; |
| 336 | // We only handle legal types. For example, on x86-32 the instruction |
| 337 | // selector contains all of the 64-bit instructions from x86-64, |
| 338 | // under the assumption that i64 won't be used if the target doesn't |
| 339 | // support it. |
| 340 | return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT); |
| 341 | } |
| 342 | |
| 343 | #include "X86GenCallingConv.inc" |
| 344 | |
| 345 | /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT. |
| 346 | /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV. |
| 347 | /// Return true and the result register by reference if it is possible. |
Pete Cooper | d0dae3e | 2015-05-05 23:41:53 +0000 | [diff] [blame] | 348 | bool X86FastISel::X86FastEmitLoad(EVT VT, X86AddressMode &AM, |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 349 | MachineMemOperand *MMO, unsigned &ResultReg, |
| 350 | unsigned Alignment) { |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 351 | bool HasSSE41 = Subtarget->hasSSE41(); |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 352 | bool HasAVX = Subtarget->hasAVX(); |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 353 | bool HasAVX2 = Subtarget->hasAVX2(); |
| 354 | bool IsNonTemporal = MMO && MMO->isNonTemporal(); |
| 355 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 356 | // Get opcode and regclass of the output for the given load instruction. |
| 357 | unsigned Opc = 0; |
| 358 | const TargetRegisterClass *RC = nullptr; |
| 359 | switch (VT.getSimpleVT().SimpleTy) { |
| 360 | default: return false; |
| 361 | case MVT::i1: |
| 362 | case MVT::i8: |
| 363 | Opc = X86::MOV8rm; |
| 364 | RC = &X86::GR8RegClass; |
| 365 | break; |
| 366 | case MVT::i16: |
| 367 | Opc = X86::MOV16rm; |
| 368 | RC = &X86::GR16RegClass; |
| 369 | break; |
| 370 | case MVT::i32: |
| 371 | Opc = X86::MOV32rm; |
| 372 | RC = &X86::GR32RegClass; |
| 373 | break; |
| 374 | case MVT::i64: |
| 375 | // Must be in x86-64 mode. |
| 376 | Opc = X86::MOV64rm; |
| 377 | RC = &X86::GR64RegClass; |
| 378 | break; |
| 379 | case MVT::f32: |
| 380 | if (X86ScalarSSEf32) { |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 381 | Opc = HasAVX ? X86::VMOVSSrm : X86::MOVSSrm; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 382 | RC = &X86::FR32RegClass; |
| 383 | } else { |
| 384 | Opc = X86::LD_Fp32m; |
| 385 | RC = &X86::RFP32RegClass; |
| 386 | } |
| 387 | break; |
| 388 | case MVT::f64: |
| 389 | if (X86ScalarSSEf64) { |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 390 | Opc = HasAVX ? X86::VMOVSDrm : X86::MOVSDrm; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 391 | RC = &X86::FR64RegClass; |
| 392 | } else { |
| 393 | Opc = X86::LD_Fp64m; |
| 394 | RC = &X86::RFP64RegClass; |
| 395 | } |
| 396 | break; |
| 397 | case MVT::f80: |
| 398 | // No f80 support yet. |
| 399 | return false; |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 400 | case MVT::v4f32: |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 401 | if (IsNonTemporal && Alignment >= 16 && HasSSE41) |
| 402 | Opc = HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm; |
| 403 | else if (Alignment >= 16) |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 404 | Opc = HasAVX ? X86::VMOVAPSrm : X86::MOVAPSrm; |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 405 | else |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 406 | Opc = HasAVX ? X86::VMOVUPSrm : X86::MOVUPSrm; |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 407 | RC = &X86::VR128RegClass; |
| 408 | break; |
| 409 | case MVT::v2f64: |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 410 | if (IsNonTemporal && Alignment >= 16 && HasSSE41) |
| 411 | Opc = HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm; |
| 412 | else if (Alignment >= 16) |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 413 | Opc = HasAVX ? X86::VMOVAPDrm : X86::MOVAPDrm; |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 414 | else |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 415 | Opc = HasAVX ? X86::VMOVUPDrm : X86::MOVUPDrm; |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 416 | RC = &X86::VR128RegClass; |
| 417 | break; |
| 418 | case MVT::v4i32: |
| 419 | case MVT::v2i64: |
| 420 | case MVT::v8i16: |
| 421 | case MVT::v16i8: |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 422 | if (IsNonTemporal && Alignment >= 16) |
| 423 | Opc = HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm; |
| 424 | else if (Alignment >= 16) |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 425 | Opc = HasAVX ? X86::VMOVDQArm : X86::MOVDQArm; |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 426 | else |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 427 | Opc = HasAVX ? X86::VMOVDQUrm : X86::MOVDQUrm; |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 428 | RC = &X86::VR128RegClass; |
| 429 | break; |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 430 | case MVT::v8f32: |
| 431 | assert(HasAVX); |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 432 | if (IsNonTemporal && Alignment >= 32 && HasAVX2) |
| 433 | Opc = X86::VMOVNTDQAYrm; |
| 434 | else |
| 435 | Opc = (Alignment >= 32) ? X86::VMOVAPSYrm : X86::VMOVUPSYrm; |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 436 | RC = &X86::VR256RegClass; |
| 437 | break; |
| 438 | case MVT::v4f64: |
| 439 | assert(HasAVX); |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 440 | if (IsNonTemporal && Alignment >= 32 && HasAVX2) |
| 441 | Opc = X86::VMOVNTDQAYrm; |
| 442 | else |
| 443 | Opc = (Alignment >= 32) ? X86::VMOVAPDYrm : X86::VMOVUPDYrm; |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 444 | RC = &X86::VR256RegClass; |
| 445 | break; |
| 446 | case MVT::v8i32: |
| 447 | case MVT::v4i64: |
| 448 | case MVT::v16i16: |
| 449 | case MVT::v32i8: |
| 450 | assert(HasAVX); |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 451 | if (IsNonTemporal && Alignment >= 32 && HasAVX2) |
| 452 | Opc = X86::VMOVNTDQAYrm; |
| 453 | else |
| 454 | Opc = (Alignment >= 32) ? X86::VMOVDQAYrm : X86::VMOVDQUYrm; |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 455 | RC = &X86::VR256RegClass; |
| 456 | break; |
Craig Topper | 048a08a | 2016-06-02 04:51:37 +0000 | [diff] [blame] | 457 | case MVT::v16f32: |
| 458 | assert(Subtarget->hasAVX512()); |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 459 | if (IsNonTemporal && Alignment >= 64) |
| 460 | Opc = X86::VMOVNTDQAZrm; |
| 461 | else |
| 462 | Opc = (Alignment >= 64) ? X86::VMOVAPSZrm : X86::VMOVUPSZrm; |
Craig Topper | 048a08a | 2016-06-02 04:51:37 +0000 | [diff] [blame] | 463 | RC = &X86::VR512RegClass; |
| 464 | break; |
| 465 | case MVT::v8f64: |
| 466 | assert(Subtarget->hasAVX512()); |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 467 | if (IsNonTemporal && Alignment >= 64) |
| 468 | Opc = X86::VMOVNTDQAZrm; |
| 469 | else |
| 470 | Opc = (Alignment >= 64) ? X86::VMOVAPDZrm : X86::VMOVUPDZrm; |
Craig Topper | 048a08a | 2016-06-02 04:51:37 +0000 | [diff] [blame] | 471 | RC = &X86::VR512RegClass; |
| 472 | break; |
| 473 | case MVT::v8i64: |
| 474 | case MVT::v16i32: |
| 475 | case MVT::v32i16: |
| 476 | case MVT::v64i8: |
| 477 | assert(Subtarget->hasAVX512()); |
| 478 | // Note: There are a lot more choices based on type with AVX-512, but |
| 479 | // there's really no advantage when the load isn't masked. |
Simon Pilgrim | 35c06a0 | 2016-06-07 13:47:23 +0000 | [diff] [blame] | 480 | if (IsNonTemporal && Alignment >= 64) |
| 481 | Opc = X86::VMOVNTDQAZrm; |
| 482 | else |
| 483 | Opc = (Alignment >= 64) ? X86::VMOVDQA64Zrm : X86::VMOVDQU64Zrm; |
Craig Topper | 048a08a | 2016-06-02 04:51:37 +0000 | [diff] [blame] | 484 | RC = &X86::VR512RegClass; |
| 485 | break; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | ResultReg = createResultReg(RC); |
| 489 | MachineInstrBuilder MIB = |
| 490 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg); |
| 491 | addFullAddress(MIB, AM); |
| 492 | if (MMO) |
| 493 | MIB->addMemOperand(*FuncInfo.MF, MMO); |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | /// X86FastEmitStore - Emit a machine instruction to store a value Val of |
| 498 | /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr |
| 499 | /// and a displacement offset, or a GlobalAddress, |
| 500 | /// i.e. V. Return true if it is possible. |
| 501 | bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill, |
Pete Cooper | d0dae3e | 2015-05-05 23:41:53 +0000 | [diff] [blame] | 502 | X86AddressMode &AM, |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 503 | MachineMemOperand *MMO, bool Aligned) { |
Andrea Di Biagio | c47edbe | 2015-10-14 10:03:13 +0000 | [diff] [blame] | 504 | bool HasSSE2 = Subtarget->hasSSE2(); |
Simon Pilgrim | 5b65f28 | 2015-10-17 13:04:42 +0000 | [diff] [blame] | 505 | bool HasSSE4A = Subtarget->hasSSE4A(); |
Andrea Di Biagio | c47edbe | 2015-10-14 10:03:13 +0000 | [diff] [blame] | 506 | bool HasAVX = Subtarget->hasAVX(); |
| 507 | bool IsNonTemporal = MMO && MMO->isNonTemporal(); |
| 508 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 509 | // Get opcode and regclass of the output for the given store instruction. |
| 510 | unsigned Opc = 0; |
| 511 | switch (VT.getSimpleVT().SimpleTy) { |
| 512 | case MVT::f80: // No f80 support yet. |
| 513 | default: return false; |
| 514 | case MVT::i1: { |
| 515 | // Mask out all but lowest bit. |
| 516 | unsigned AndResult = createResultReg(&X86::GR8RegClass); |
| 517 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 518 | TII.get(X86::AND8ri), AndResult) |
| 519 | .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1); |
| 520 | ValReg = AndResult; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 521 | LLVM_FALLTHROUGH; // handle i1 as i8. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 522 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 523 | case MVT::i8: Opc = X86::MOV8mr; break; |
| 524 | case MVT::i16: Opc = X86::MOV16mr; break; |
Andrea Di Biagio | c47edbe | 2015-10-14 10:03:13 +0000 | [diff] [blame] | 525 | case MVT::i32: |
| 526 | Opc = (IsNonTemporal && HasSSE2) ? X86::MOVNTImr : X86::MOV32mr; |
| 527 | break; |
| 528 | case MVT::i64: |
| 529 | // Must be in x86-64 mode. |
| 530 | Opc = (IsNonTemporal && HasSSE2) ? X86::MOVNTI_64mr : X86::MOV64mr; |
| 531 | break; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 532 | case MVT::f32: |
Simon Pilgrim | 5b65f28 | 2015-10-17 13:04:42 +0000 | [diff] [blame] | 533 | if (X86ScalarSSEf32) { |
| 534 | if (IsNonTemporal && HasSSE4A) |
| 535 | Opc = X86::MOVNTSS; |
| 536 | else |
| 537 | Opc = HasAVX ? X86::VMOVSSmr : X86::MOVSSmr; |
| 538 | } else |
| 539 | Opc = X86::ST_Fp32m; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 540 | break; |
| 541 | case MVT::f64: |
Simon Pilgrim | 5b65f28 | 2015-10-17 13:04:42 +0000 | [diff] [blame] | 542 | if (X86ScalarSSEf32) { |
| 543 | if (IsNonTemporal && HasSSE4A) |
| 544 | Opc = X86::MOVNTSD; |
| 545 | else |
| 546 | Opc = HasAVX ? X86::VMOVSDmr : X86::MOVSDmr; |
| 547 | } else |
| 548 | Opc = X86::ST_Fp64m; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 549 | break; |
| 550 | case MVT::v4f32: |
Andrea Di Biagio | c47edbe | 2015-10-14 10:03:13 +0000 | [diff] [blame] | 551 | if (Aligned) { |
| 552 | if (IsNonTemporal) |
| 553 | Opc = HasAVX ? X86::VMOVNTPSmr : X86::MOVNTPSmr; |
| 554 | else |
| 555 | Opc = HasAVX ? X86::VMOVAPSmr : X86::MOVAPSmr; |
| 556 | } else |
| 557 | Opc = HasAVX ? X86::VMOVUPSmr : X86::MOVUPSmr; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 558 | break; |
| 559 | case MVT::v2f64: |
Andrea Di Biagio | c47edbe | 2015-10-14 10:03:13 +0000 | [diff] [blame] | 560 | if (Aligned) { |
| 561 | if (IsNonTemporal) |
| 562 | Opc = HasAVX ? X86::VMOVNTPDmr : X86::MOVNTPDmr; |
| 563 | else |
| 564 | Opc = HasAVX ? X86::VMOVAPDmr : X86::MOVAPDmr; |
| 565 | } else |
| 566 | Opc = HasAVX ? X86::VMOVUPDmr : X86::MOVUPDmr; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 567 | break; |
| 568 | case MVT::v4i32: |
| 569 | case MVT::v2i64: |
| 570 | case MVT::v8i16: |
| 571 | case MVT::v16i8: |
Andrea Di Biagio | c47edbe | 2015-10-14 10:03:13 +0000 | [diff] [blame] | 572 | if (Aligned) { |
| 573 | if (IsNonTemporal) |
| 574 | Opc = HasAVX ? X86::VMOVNTDQmr : X86::MOVNTDQmr; |
| 575 | else |
| 576 | Opc = HasAVX ? X86::VMOVDQAmr : X86::MOVDQAmr; |
| 577 | } else |
Craig Topper | ca9c080 | 2016-06-02 04:19:45 +0000 | [diff] [blame] | 578 | Opc = HasAVX ? X86::VMOVDQUmr : X86::MOVDQUmr; |
| 579 | break; |
| 580 | case MVT::v8f32: |
| 581 | assert(HasAVX); |
| 582 | if (Aligned) |
| 583 | Opc = IsNonTemporal ? X86::VMOVNTPSYmr : X86::VMOVAPSYmr; |
| 584 | else |
| 585 | Opc = X86::VMOVUPSYmr; |
| 586 | break; |
| 587 | case MVT::v4f64: |
| 588 | assert(HasAVX); |
| 589 | if (Aligned) { |
| 590 | Opc = IsNonTemporal ? X86::VMOVNTPDYmr : X86::VMOVAPDYmr; |
| 591 | } else |
| 592 | Opc = X86::VMOVUPDYmr; |
| 593 | break; |
| 594 | case MVT::v8i32: |
| 595 | case MVT::v4i64: |
| 596 | case MVT::v16i16: |
| 597 | case MVT::v32i8: |
| 598 | assert(HasAVX); |
| 599 | if (Aligned) |
| 600 | Opc = IsNonTemporal ? X86::VMOVNTDQYmr : X86::VMOVDQAYmr; |
| 601 | else |
| 602 | Opc = X86::VMOVDQUYmr; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 603 | break; |
Craig Topper | 048a08a | 2016-06-02 04:51:37 +0000 | [diff] [blame] | 604 | case MVT::v16f32: |
| 605 | assert(Subtarget->hasAVX512()); |
| 606 | if (Aligned) |
| 607 | Opc = IsNonTemporal ? X86::VMOVNTPSZmr : X86::VMOVAPSZmr; |
| 608 | else |
| 609 | Opc = X86::VMOVUPSZmr; |
| 610 | break; |
| 611 | case MVT::v8f64: |
| 612 | assert(Subtarget->hasAVX512()); |
| 613 | if (Aligned) { |
| 614 | Opc = IsNonTemporal ? X86::VMOVNTPDZmr : X86::VMOVAPDZmr; |
| 615 | } else |
| 616 | Opc = X86::VMOVUPDZmr; |
| 617 | break; |
| 618 | case MVT::v8i64: |
| 619 | case MVT::v16i32: |
| 620 | case MVT::v32i16: |
| 621 | case MVT::v64i8: |
| 622 | assert(Subtarget->hasAVX512()); |
| 623 | // Note: There are a lot more choices based on type with AVX-512, but |
| 624 | // there's really no advantage when the store isn't masked. |
| 625 | if (Aligned) |
| 626 | Opc = IsNonTemporal ? X86::VMOVNTDQZmr : X86::VMOVDQA64Zmr; |
| 627 | else |
| 628 | Opc = X86::VMOVDQU64Zmr; |
| 629 | break; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Quentin Colombet | bf20068 | 2016-04-27 22:33:42 +0000 | [diff] [blame] | 632 | const MCInstrDesc &Desc = TII.get(Opc); |
| 633 | // Some of the instructions in the previous switch use FR128 instead |
| 634 | // of FR32 for ValReg. Make sure the register we feed the instruction |
| 635 | // matches its register class constraints. |
| 636 | // Note: This is fine to do a copy from FR32 to FR128, this is the |
| 637 | // same registers behind the scene and actually why it did not trigger |
| 638 | // any bugs before. |
| 639 | ValReg = constrainOperandRegClass(Desc, ValReg, Desc.getNumOperands() - 1); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 640 | MachineInstrBuilder MIB = |
Quentin Colombet | bf20068 | 2016-04-27 22:33:42 +0000 | [diff] [blame] | 641 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, Desc); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 642 | addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill)); |
| 643 | if (MMO) |
| 644 | MIB->addMemOperand(*FuncInfo.MF, MMO); |
| 645 | |
| 646 | return true; |
| 647 | } |
| 648 | |
| 649 | bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val, |
Pete Cooper | d0dae3e | 2015-05-05 23:41:53 +0000 | [diff] [blame] | 650 | X86AddressMode &AM, |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 651 | MachineMemOperand *MMO, bool Aligned) { |
| 652 | // Handle 'null' like i32/i64 0. |
| 653 | if (isa<ConstantPointerNull>(Val)) |
| 654 | Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext())); |
| 655 | |
| 656 | // If this is a store of a simple constant, fold the constant into the store. |
| 657 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
| 658 | unsigned Opc = 0; |
| 659 | bool Signed = true; |
| 660 | switch (VT.getSimpleVT().SimpleTy) { |
| 661 | default: break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 662 | case MVT::i1: |
| 663 | Signed = false; |
| 664 | LLVM_FALLTHROUGH; // Handle as i8. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 665 | case MVT::i8: Opc = X86::MOV8mi; break; |
| 666 | case MVT::i16: Opc = X86::MOV16mi; break; |
| 667 | case MVT::i32: Opc = X86::MOV32mi; break; |
| 668 | case MVT::i64: |
| 669 | // Must be a 32-bit sign extended value. |
| 670 | if (isInt<32>(CI->getSExtValue())) |
| 671 | Opc = X86::MOV64mi32; |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | if (Opc) { |
| 676 | MachineInstrBuilder MIB = |
| 677 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)); |
| 678 | addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue() |
| 679 | : CI->getZExtValue()); |
| 680 | if (MMO) |
| 681 | MIB->addMemOperand(*FuncInfo.MF, MMO); |
| 682 | return true; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | unsigned ValReg = getRegForValue(Val); |
| 687 | if (ValReg == 0) |
| 688 | return false; |
| 689 | |
| 690 | bool ValKill = hasTrivialKill(Val); |
| 691 | return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned); |
| 692 | } |
| 693 | |
| 694 | /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of |
| 695 | /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g. |
| 696 | /// ISD::SIGN_EXTEND). |
| 697 | bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, |
| 698 | unsigned Src, EVT SrcVT, |
| 699 | unsigned &ResultReg) { |
| 700 | unsigned RR = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, |
| 701 | Src, /*TODO: Kill=*/false); |
| 702 | if (RR == 0) |
| 703 | return false; |
| 704 | |
| 705 | ResultReg = RR; |
| 706 | return true; |
| 707 | } |
| 708 | |
| 709 | bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) { |
| 710 | // Handle constant address. |
| 711 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 712 | // Can't handle alternate code models yet. |
| 713 | if (TM.getCodeModel() != CodeModel::Small) |
| 714 | return false; |
| 715 | |
| 716 | // Can't handle TLS yet. |
| 717 | if (GV->isThreadLocal()) |
| 718 | return false; |
| 719 | |
| 720 | // RIP-relative addresses can't have additional register operands, so if |
| 721 | // we've already folded stuff into the addressing mode, just force the |
| 722 | // global value into its own register, which we can use as the basereg. |
| 723 | if (!Subtarget->isPICStyleRIPRel() || |
| 724 | (AM.Base.Reg == 0 && AM.IndexReg == 0)) { |
| 725 | // Okay, we've committed to selecting this global. Set up the address. |
| 726 | AM.GV = GV; |
| 727 | |
| 728 | // Allow the subtarget to classify the global. |
Rafael Espindola | ab03eb0 | 2016-05-19 22:07:57 +0000 | [diff] [blame] | 729 | unsigned char GVFlags = Subtarget->classifyGlobalReference(GV); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 730 | |
| 731 | // If this reference is relative to the pic base, set it now. |
| 732 | if (isGlobalRelativeToPICBase(GVFlags)) { |
| 733 | // FIXME: How do we know Base.Reg is free?? |
| 734 | AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF); |
| 735 | } |
| 736 | |
| 737 | // Unless the ABI requires an extra load, return a direct reference to |
| 738 | // the global. |
| 739 | if (!isGlobalStubReference(GVFlags)) { |
| 740 | if (Subtarget->isPICStyleRIPRel()) { |
| 741 | // Use rip-relative addressing if we can. Above we verified that the |
| 742 | // base and index registers are unused. |
| 743 | assert(AM.Base.Reg == 0 && AM.IndexReg == 0); |
| 744 | AM.Base.Reg = X86::RIP; |
| 745 | } |
| 746 | AM.GVOpFlags = GVFlags; |
| 747 | return true; |
| 748 | } |
| 749 | |
| 750 | // Ok, we need to do a load from a stub. If we've already loaded from |
| 751 | // this stub, reuse the loaded pointer, otherwise emit the load now. |
| 752 | DenseMap<const Value *, unsigned>::iterator I = LocalValueMap.find(V); |
| 753 | unsigned LoadReg; |
| 754 | if (I != LocalValueMap.end() && I->second != 0) { |
| 755 | LoadReg = I->second; |
| 756 | } else { |
| 757 | // Issue load from stub. |
| 758 | unsigned Opc = 0; |
| 759 | const TargetRegisterClass *RC = nullptr; |
| 760 | X86AddressMode StubAM; |
| 761 | StubAM.Base.Reg = AM.Base.Reg; |
| 762 | StubAM.GV = GV; |
| 763 | StubAM.GVOpFlags = GVFlags; |
| 764 | |
| 765 | // Prepare for inserting code in the local-value area. |
| 766 | SavePoint SaveInsertPt = enterLocalValueArea(); |
| 767 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 768 | if (TLI.getPointerTy(DL) == MVT::i64) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 769 | Opc = X86::MOV64rm; |
| 770 | RC = &X86::GR64RegClass; |
| 771 | |
| 772 | if (Subtarget->isPICStyleRIPRel()) |
| 773 | StubAM.Base.Reg = X86::RIP; |
| 774 | } else { |
| 775 | Opc = X86::MOV32rm; |
| 776 | RC = &X86::GR32RegClass; |
| 777 | } |
| 778 | |
| 779 | LoadReg = createResultReg(RC); |
| 780 | MachineInstrBuilder LoadMI = |
| 781 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg); |
| 782 | addFullAddress(LoadMI, StubAM); |
| 783 | |
| 784 | // Ok, back to normal mode. |
| 785 | leaveLocalValueArea(SaveInsertPt); |
| 786 | |
| 787 | // Prevent loading GV stub multiple times in same MBB. |
| 788 | LocalValueMap[V] = LoadReg; |
| 789 | } |
| 790 | |
| 791 | // Now construct the final address. Note that the Disp, Scale, |
| 792 | // and Index values may already be set here. |
| 793 | AM.Base.Reg = LoadReg; |
| 794 | AM.GV = nullptr; |
| 795 | return true; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | // If all else fails, try to materialize the value in a register. |
| 800 | if (!AM.GV || !Subtarget->isPICStyleRIPRel()) { |
| 801 | if (AM.Base.Reg == 0) { |
| 802 | AM.Base.Reg = getRegForValue(V); |
| 803 | return AM.Base.Reg != 0; |
| 804 | } |
| 805 | if (AM.IndexReg == 0) { |
| 806 | assert(AM.Scale == 1 && "Scale with no index!"); |
| 807 | AM.IndexReg = getRegForValue(V); |
| 808 | return AM.IndexReg != 0; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | /// X86SelectAddress - Attempt to fill in an address from the given value. |
| 816 | /// |
| 817 | bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) { |
| 818 | SmallVector<const Value *, 32> GEPs; |
| 819 | redo_gep: |
| 820 | const User *U = nullptr; |
| 821 | unsigned Opcode = Instruction::UserOp1; |
| 822 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 823 | // Don't walk into other basic blocks; it's possible we haven't |
| 824 | // visited them yet, so the instructions may not yet be assigned |
| 825 | // virtual registers. |
| 826 | if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) || |
| 827 | FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { |
| 828 | Opcode = I->getOpcode(); |
| 829 | U = I; |
| 830 | } |
| 831 | } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) { |
| 832 | Opcode = C->getOpcode(); |
| 833 | U = C; |
| 834 | } |
| 835 | |
| 836 | if (PointerType *Ty = dyn_cast<PointerType>(V->getType())) |
| 837 | if (Ty->getAddressSpace() > 255) |
| 838 | // Fast instruction selection doesn't support the special |
| 839 | // address spaces. |
| 840 | return false; |
| 841 | |
| 842 | switch (Opcode) { |
| 843 | default: break; |
| 844 | case Instruction::BitCast: |
| 845 | // Look past bitcasts. |
| 846 | return X86SelectAddress(U->getOperand(0), AM); |
| 847 | |
| 848 | case Instruction::IntToPtr: |
| 849 | // Look past no-op inttoptrs. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 850 | if (TLI.getValueType(DL, U->getOperand(0)->getType()) == |
| 851 | TLI.getPointerTy(DL)) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 852 | return X86SelectAddress(U->getOperand(0), AM); |
| 853 | break; |
| 854 | |
| 855 | case Instruction::PtrToInt: |
| 856 | // Look past no-op ptrtoints. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 857 | if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL)) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 858 | return X86SelectAddress(U->getOperand(0), AM); |
| 859 | break; |
| 860 | |
| 861 | case Instruction::Alloca: { |
| 862 | // Do static allocas. |
| 863 | const AllocaInst *A = cast<AllocaInst>(V); |
| 864 | DenseMap<const AllocaInst *, int>::iterator SI = |
| 865 | FuncInfo.StaticAllocaMap.find(A); |
| 866 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 867 | AM.BaseType = X86AddressMode::FrameIndexBase; |
| 868 | AM.Base.FrameIndex = SI->second; |
| 869 | return true; |
| 870 | } |
| 871 | break; |
| 872 | } |
| 873 | |
| 874 | case Instruction::Add: { |
| 875 | // Adds of constants are common and easy enough. |
| 876 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 877 | uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue(); |
| 878 | // They have to fit in the 32-bit signed displacement field though. |
| 879 | if (isInt<32>(Disp)) { |
| 880 | AM.Disp = (uint32_t)Disp; |
| 881 | return X86SelectAddress(U->getOperand(0), AM); |
| 882 | } |
| 883 | } |
| 884 | break; |
| 885 | } |
| 886 | |
| 887 | case Instruction::GetElementPtr: { |
| 888 | X86AddressMode SavedAM = AM; |
| 889 | |
| 890 | // Pattern-match simple GEPs. |
| 891 | uint64_t Disp = (int32_t)AM.Disp; |
| 892 | unsigned IndexReg = AM.IndexReg; |
| 893 | unsigned Scale = AM.Scale; |
| 894 | gep_type_iterator GTI = gep_type_begin(U); |
| 895 | // Iterate through the indices, folding what we can. Constants can be |
| 896 | // folded, and one dynamic index can be handled, if the scale is supported. |
| 897 | for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); |
| 898 | i != e; ++i, ++GTI) { |
| 899 | const Value *Op = *i; |
| 900 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 901 | const StructLayout *SL = DL.getStructLayout(STy); |
| 902 | Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue()); |
| 903 | continue; |
| 904 | } |
| 905 | |
| 906 | // A array/variable index is always of the form i*S where S is the |
| 907 | // constant scale size. See if we can push the scale into immediates. |
| 908 | uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType()); |
| 909 | for (;;) { |
| 910 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { |
| 911 | // Constant-offset addressing. |
| 912 | Disp += CI->getSExtValue() * S; |
| 913 | break; |
| 914 | } |
| 915 | if (canFoldAddIntoGEP(U, Op)) { |
| 916 | // A compatible add with a constant operand. Fold the constant. |
| 917 | ConstantInt *CI = |
| 918 | cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); |
| 919 | Disp += CI->getSExtValue() * S; |
| 920 | // Iterate on the other operand. |
| 921 | Op = cast<AddOperator>(Op)->getOperand(0); |
| 922 | continue; |
| 923 | } |
| 924 | if (IndexReg == 0 && |
| 925 | (!AM.GV || !Subtarget->isPICStyleRIPRel()) && |
| 926 | (S == 1 || S == 2 || S == 4 || S == 8)) { |
| 927 | // Scaled-index addressing. |
| 928 | Scale = S; |
| 929 | IndexReg = getRegForGEPIndex(Op).first; |
| 930 | if (IndexReg == 0) |
| 931 | return false; |
| 932 | break; |
| 933 | } |
| 934 | // Unsupported. |
| 935 | goto unsupported_gep; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | // Check for displacement overflow. |
| 940 | if (!isInt<32>(Disp)) |
| 941 | break; |
| 942 | |
| 943 | AM.IndexReg = IndexReg; |
| 944 | AM.Scale = Scale; |
| 945 | AM.Disp = (uint32_t)Disp; |
| 946 | GEPs.push_back(V); |
| 947 | |
| 948 | if (const GetElementPtrInst *GEP = |
| 949 | dyn_cast<GetElementPtrInst>(U->getOperand(0))) { |
| 950 | // Ok, the GEP indices were covered by constant-offset and scaled-index |
| 951 | // addressing. Update the address state and move on to examining the base. |
| 952 | V = GEP; |
| 953 | goto redo_gep; |
| 954 | } else if (X86SelectAddress(U->getOperand(0), AM)) { |
| 955 | return true; |
| 956 | } |
| 957 | |
| 958 | // If we couldn't merge the gep value into this addr mode, revert back to |
| 959 | // our address and just match the value instead of completely failing. |
| 960 | AM = SavedAM; |
| 961 | |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 962 | for (const Value *I : reverse(GEPs)) |
| 963 | if (handleConstantAddresses(I, AM)) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 964 | return true; |
| 965 | |
| 966 | return false; |
| 967 | unsupported_gep: |
| 968 | // Ok, the GEP indices weren't all covered. |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | return handleConstantAddresses(V, AM); |
| 974 | } |
| 975 | |
| 976 | /// X86SelectCallAddress - Attempt to fill in an address from the given value. |
| 977 | /// |
| 978 | bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) { |
| 979 | const User *U = nullptr; |
| 980 | unsigned Opcode = Instruction::UserOp1; |
| 981 | const Instruction *I = dyn_cast<Instruction>(V); |
| 982 | // Record if the value is defined in the same basic block. |
| 983 | // |
| 984 | // This information is crucial to know whether or not folding an |
| 985 | // operand is valid. |
| 986 | // Indeed, FastISel generates or reuses a virtual register for all |
| 987 | // operands of all instructions it selects. Obviously, the definition and |
| 988 | // its uses must use the same virtual register otherwise the produced |
| 989 | // code is incorrect. |
| 990 | // Before instruction selection, FunctionLoweringInfo::set sets the virtual |
| 991 | // registers for values that are alive across basic blocks. This ensures |
| 992 | // that the values are consistently set between across basic block, even |
| 993 | // if different instruction selection mechanisms are used (e.g., a mix of |
| 994 | // SDISel and FastISel). |
| 995 | // For values local to a basic block, the instruction selection process |
| 996 | // generates these virtual registers with whatever method is appropriate |
| 997 | // for its needs. In particular, FastISel and SDISel do not share the way |
| 998 | // local virtual registers are set. |
| 999 | // Therefore, this is impossible (or at least unsafe) to share values |
| 1000 | // between basic blocks unless they use the same instruction selection |
| 1001 | // method, which is not guarantee for X86. |
| 1002 | // Moreover, things like hasOneUse could not be used accurately, if we |
| 1003 | // allow to reference values across basic blocks whereas they are not |
| 1004 | // alive across basic blocks initially. |
| 1005 | bool InMBB = true; |
| 1006 | if (I) { |
| 1007 | Opcode = I->getOpcode(); |
| 1008 | U = I; |
| 1009 | InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock(); |
| 1010 | } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) { |
| 1011 | Opcode = C->getOpcode(); |
| 1012 | U = C; |
| 1013 | } |
| 1014 | |
| 1015 | switch (Opcode) { |
| 1016 | default: break; |
| 1017 | case Instruction::BitCast: |
| 1018 | // Look past bitcasts if its operand is in the same BB. |
| 1019 | if (InMBB) |
| 1020 | return X86SelectCallAddress(U->getOperand(0), AM); |
| 1021 | break; |
| 1022 | |
| 1023 | case Instruction::IntToPtr: |
| 1024 | // Look past no-op inttoptrs if its operand is in the same BB. |
| 1025 | if (InMBB && |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1026 | TLI.getValueType(DL, U->getOperand(0)->getType()) == |
| 1027 | TLI.getPointerTy(DL)) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1028 | return X86SelectCallAddress(U->getOperand(0), AM); |
| 1029 | break; |
| 1030 | |
| 1031 | case Instruction::PtrToInt: |
| 1032 | // Look past no-op ptrtoints if its operand is in the same BB. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1033 | if (InMBB && TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL)) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1034 | return X86SelectCallAddress(U->getOperand(0), AM); |
| 1035 | break; |
| 1036 | } |
| 1037 | |
| 1038 | // Handle constant address. |
| 1039 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 1040 | // Can't handle alternate code models yet. |
| 1041 | if (TM.getCodeModel() != CodeModel::Small) |
| 1042 | return false; |
| 1043 | |
| 1044 | // RIP-relative addresses can't have additional register operands. |
| 1045 | if (Subtarget->isPICStyleRIPRel() && |
| 1046 | (AM.Base.Reg != 0 || AM.IndexReg != 0)) |
| 1047 | return false; |
| 1048 | |
| 1049 | // Can't handle DLL Import. |
| 1050 | if (GV->hasDLLImportStorageClass()) |
| 1051 | return false; |
| 1052 | |
| 1053 | // Can't handle TLS. |
| 1054 | if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) |
| 1055 | if (GVar->isThreadLocal()) |
| 1056 | return false; |
| 1057 | |
| 1058 | // Okay, we've committed to selecting this global. Set up the basic address. |
| 1059 | AM.GV = GV; |
| 1060 | |
| 1061 | // No ABI requires an extra load for anything other than DLLImport, which |
| 1062 | // we rejected above. Return a direct reference to the global. |
| 1063 | if (Subtarget->isPICStyleRIPRel()) { |
| 1064 | // Use rip-relative addressing if we can. Above we verified that the |
| 1065 | // base and index registers are unused. |
| 1066 | assert(AM.Base.Reg == 0 && AM.IndexReg == 0); |
| 1067 | AM.Base.Reg = X86::RIP; |
Rafael Espindola | c7e9813 | 2016-05-20 12:20:10 +0000 | [diff] [blame] | 1068 | } else { |
| 1069 | AM.GVOpFlags = Subtarget->classifyLocalReference(nullptr); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
| 1075 | // If all else fails, try to materialize the value in a register. |
| 1076 | if (!AM.GV || !Subtarget->isPICStyleRIPRel()) { |
| 1077 | if (AM.Base.Reg == 0) { |
| 1078 | AM.Base.Reg = getRegForValue(V); |
| 1079 | return AM.Base.Reg != 0; |
| 1080 | } |
| 1081 | if (AM.IndexReg == 0) { |
| 1082 | assert(AM.Scale == 1 && "Scale with no index!"); |
| 1083 | AM.IndexReg = getRegForValue(V); |
| 1084 | return AM.IndexReg != 0; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | /// X86SelectStore - Select and emit code to implement store instructions. |
| 1093 | bool X86FastISel::X86SelectStore(const Instruction *I) { |
| 1094 | // Atomic stores need special handling. |
| 1095 | const StoreInst *S = cast<StoreInst>(I); |
| 1096 | |
| 1097 | if (S->isAtomic()) |
| 1098 | return false; |
| 1099 | |
Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 1100 | const Value *PtrV = I->getOperand(1); |
| 1101 | if (TLI.supportSwiftError()) { |
| 1102 | // Swifterror values can come from either a function parameter with |
| 1103 | // swifterror attribute or an alloca with swifterror attribute. |
| 1104 | if (const Argument *Arg = dyn_cast<Argument>(PtrV)) { |
| 1105 | if (Arg->hasSwiftErrorAttr()) |
| 1106 | return false; |
| 1107 | } |
| 1108 | |
| 1109 | if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) { |
| 1110 | if (Alloca->isSwiftError()) |
| 1111 | return false; |
| 1112 | } |
| 1113 | } |
| 1114 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1115 | const Value *Val = S->getValueOperand(); |
| 1116 | const Value *Ptr = S->getPointerOperand(); |
| 1117 | |
| 1118 | MVT VT; |
| 1119 | if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true)) |
| 1120 | return false; |
| 1121 | |
| 1122 | unsigned Alignment = S->getAlignment(); |
| 1123 | unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType()); |
| 1124 | if (Alignment == 0) // Ensure that codegen never sees alignment 0 |
| 1125 | Alignment = ABIAlignment; |
| 1126 | bool Aligned = Alignment >= ABIAlignment; |
| 1127 | |
| 1128 | X86AddressMode AM; |
| 1129 | if (!X86SelectAddress(Ptr, AM)) |
| 1130 | return false; |
| 1131 | |
| 1132 | return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned); |
| 1133 | } |
| 1134 | |
| 1135 | /// X86SelectRet - Select and emit code to implement ret instructions. |
| 1136 | bool X86FastISel::X86SelectRet(const Instruction *I) { |
| 1137 | const ReturnInst *Ret = cast<ReturnInst>(I); |
| 1138 | const Function &F = *I->getParent()->getParent(); |
| 1139 | const X86MachineFunctionInfo *X86MFInfo = |
| 1140 | FuncInfo.MF->getInfo<X86MachineFunctionInfo>(); |
| 1141 | |
| 1142 | if (!FuncInfo.CanLowerReturn) |
| 1143 | return false; |
| 1144 | |
Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 1145 | if (TLI.supportSwiftError() && |
| 1146 | F.getAttributes().hasAttrSomewhere(Attribute::SwiftError)) |
| 1147 | return false; |
| 1148 | |
Manman Ren | ed967f3 | 2016-01-12 01:08:46 +0000 | [diff] [blame] | 1149 | if (TLI.supportSplitCSR(FuncInfo.MF)) |
| 1150 | return false; |
| 1151 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1152 | CallingConv::ID CC = F.getCallingConv(); |
| 1153 | if (CC != CallingConv::C && |
| 1154 | CC != CallingConv::Fast && |
| 1155 | CC != CallingConv::X86_FastCall && |
Nico Weber | ecdf45b | 2016-07-14 13:54:26 +0000 | [diff] [blame] | 1156 | CC != CallingConv::X86_StdCall && |
Nico Weber | c7bf646 | 2016-07-12 01:30:35 +0000 | [diff] [blame] | 1157 | CC != CallingConv::X86_ThisCall && |
Nico Weber | 8d66df1 | 2016-07-15 20:18:37 +0000 | [diff] [blame] | 1158 | CC != CallingConv::X86_64_SysV && |
| 1159 | CC != CallingConv::X86_64_Win64) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1160 | return false; |
| 1161 | |
Nico Weber | c7bf646 | 2016-07-12 01:30:35 +0000 | [diff] [blame] | 1162 | // Don't handle popping bytes if they don't fit the ret's immediate. |
| 1163 | if (!isUInt<16>(X86MFInfo->getBytesToPopOnReturn())) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1164 | return false; |
| 1165 | |
| 1166 | // fastcc with -tailcallopt is intended to provide a guaranteed |
| 1167 | // tail call optimization. Fastisel doesn't know how to do that. |
| 1168 | if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt) |
| 1169 | return false; |
| 1170 | |
| 1171 | // Let SDISel handle vararg functions. |
| 1172 | if (F.isVarArg()) |
| 1173 | return false; |
| 1174 | |
| 1175 | // Build a list of return value registers. |
| 1176 | SmallVector<unsigned, 4> RetRegs; |
| 1177 | |
| 1178 | if (Ret->getNumOperands() > 0) { |
| 1179 | SmallVector<ISD::OutputArg, 4> Outs; |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1180 | GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Analyze operands of the call, assigning locations to each operand. |
| 1183 | SmallVector<CCValAssign, 16> ValLocs; |
| 1184 | CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext()); |
| 1185 | CCInfo.AnalyzeReturn(Outs, RetCC_X86); |
| 1186 | |
| 1187 | const Value *RV = Ret->getOperand(0); |
| 1188 | unsigned Reg = getRegForValue(RV); |
| 1189 | if (Reg == 0) |
| 1190 | return false; |
| 1191 | |
| 1192 | // Only handle a single return value for now. |
| 1193 | if (ValLocs.size() != 1) |
| 1194 | return false; |
| 1195 | |
| 1196 | CCValAssign &VA = ValLocs[0]; |
| 1197 | |
| 1198 | // Don't bother handling odd stuff for now. |
| 1199 | if (VA.getLocInfo() != CCValAssign::Full) |
| 1200 | return false; |
| 1201 | // Only handle register returns for now. |
| 1202 | if (!VA.isRegLoc()) |
| 1203 | return false; |
| 1204 | |
| 1205 | // The calling-convention tables for x87 returns don't tell |
| 1206 | // the whole story. |
| 1207 | if (VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1) |
| 1208 | return false; |
| 1209 | |
| 1210 | unsigned SrcReg = Reg + VA.getValNo(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1211 | EVT SrcVT = TLI.getValueType(DL, RV->getType()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1212 | EVT DstVT = VA.getValVT(); |
| 1213 | // Special handling for extended integers. |
| 1214 | if (SrcVT != DstVT) { |
| 1215 | if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16) |
| 1216 | return false; |
| 1217 | |
| 1218 | if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt()) |
| 1219 | return false; |
| 1220 | |
| 1221 | assert(DstVT == MVT::i32 && "X86 should always ext to i32"); |
| 1222 | |
| 1223 | if (SrcVT == MVT::i1) { |
| 1224 | if (Outs[0].Flags.isSExt()) |
| 1225 | return false; |
| 1226 | SrcReg = fastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false); |
| 1227 | SrcVT = MVT::i8; |
| 1228 | } |
| 1229 | unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND : |
| 1230 | ISD::SIGN_EXTEND; |
| 1231 | SrcReg = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op, |
| 1232 | SrcReg, /*TODO: Kill=*/false); |
| 1233 | } |
| 1234 | |
| 1235 | // Make the copy. |
| 1236 | unsigned DstReg = VA.getLocReg(); |
| 1237 | const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg); |
| 1238 | // Avoid a cross-class copy. This is very unlikely. |
| 1239 | if (!SrcRC->contains(DstReg)) |
| 1240 | return false; |
| 1241 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1242 | TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg); |
| 1243 | |
| 1244 | // Add register to return instruction. |
| 1245 | RetRegs.push_back(VA.getLocReg()); |
| 1246 | } |
| 1247 | |
Manman Ren | 1c3f65a | 2016-04-26 18:08:06 +0000 | [diff] [blame] | 1248 | // Swift calling convention does not require we copy the sret argument |
| 1249 | // into %rax/%eax for the return, and SRetReturnReg is not set for Swift. |
| 1250 | |
Dimitry Andric | 227b928 | 2016-01-03 17:22:03 +0000 | [diff] [blame] | 1251 | // All x86 ABIs require that for returning structs by value we copy |
| 1252 | // the sret argument into %rax/%eax (depending on ABI) for the return. |
| 1253 | // We saved the argument into a virtual register in the entry block, |
Michael Kuperstein | 2ea81ba | 2015-12-28 14:39:21 +0000 | [diff] [blame] | 1254 | // so now we copy the value out and into %rax/%eax. |
Manman Ren | 1c3f65a | 2016-04-26 18:08:06 +0000 | [diff] [blame] | 1255 | if (F.hasStructRetAttr() && CC != CallingConv::Swift) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1256 | unsigned Reg = X86MFInfo->getSRetReturnReg(); |
| 1257 | assert(Reg && |
| 1258 | "SRetReturnReg should have been set in LowerFormalArguments()!"); |
| 1259 | unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX; |
| 1260 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1261 | TII.get(TargetOpcode::COPY), RetReg).addReg(Reg); |
| 1262 | RetRegs.push_back(RetReg); |
| 1263 | } |
| 1264 | |
| 1265 | // Now emit the RET. |
Nico Weber | c7bf646 | 2016-07-12 01:30:35 +0000 | [diff] [blame] | 1266 | MachineInstrBuilder MIB; |
| 1267 | if (X86MFInfo->getBytesToPopOnReturn()) { |
| 1268 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1269 | TII.get(Subtarget->is64Bit() ? X86::RETIQ : X86::RETIL)) |
| 1270 | .addImm(X86MFInfo->getBytesToPopOnReturn()); |
| 1271 | } else { |
| 1272 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1273 | TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL)); |
| 1274 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1275 | for (unsigned i = 0, e = RetRegs.size(); i != e; ++i) |
| 1276 | MIB.addReg(RetRegs[i], RegState::Implicit); |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
| 1280 | /// X86SelectLoad - Select and emit code to implement load instructions. |
| 1281 | /// |
| 1282 | bool X86FastISel::X86SelectLoad(const Instruction *I) { |
| 1283 | const LoadInst *LI = cast<LoadInst>(I); |
| 1284 | |
| 1285 | // Atomic loads need special handling. |
| 1286 | if (LI->isAtomic()) |
| 1287 | return false; |
| 1288 | |
Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 1289 | const Value *SV = I->getOperand(0); |
| 1290 | if (TLI.supportSwiftError()) { |
| 1291 | // Swifterror values can come from either a function parameter with |
| 1292 | // swifterror attribute or an alloca with swifterror attribute. |
| 1293 | if (const Argument *Arg = dyn_cast<Argument>(SV)) { |
| 1294 | if (Arg->hasSwiftErrorAttr()) |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
| 1298 | if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) { |
| 1299 | if (Alloca->isSwiftError()) |
| 1300 | return false; |
| 1301 | } |
| 1302 | } |
| 1303 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1304 | MVT VT; |
| 1305 | if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true)) |
| 1306 | return false; |
| 1307 | |
| 1308 | const Value *Ptr = LI->getPointerOperand(); |
| 1309 | |
| 1310 | X86AddressMode AM; |
| 1311 | if (!X86SelectAddress(Ptr, AM)) |
| 1312 | return false; |
| 1313 | |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 1314 | unsigned Alignment = LI->getAlignment(); |
| 1315 | unsigned ABIAlignment = DL.getABITypeAlignment(LI->getType()); |
| 1316 | if (Alignment == 0) // Ensure that codegen never sees alignment 0 |
| 1317 | Alignment = ABIAlignment; |
| 1318 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1319 | unsigned ResultReg = 0; |
Andrea Di Biagio | 8f7feec | 2015-03-26 11:29:02 +0000 | [diff] [blame] | 1320 | if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg, |
| 1321 | Alignment)) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1322 | return false; |
| 1323 | |
| 1324 | updateValueMap(I, ResultReg); |
| 1325 | return true; |
| 1326 | } |
| 1327 | |
| 1328 | static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) { |
| 1329 | bool HasAVX = Subtarget->hasAVX(); |
| 1330 | bool X86ScalarSSEf32 = Subtarget->hasSSE1(); |
| 1331 | bool X86ScalarSSEf64 = Subtarget->hasSSE2(); |
| 1332 | |
| 1333 | switch (VT.getSimpleVT().SimpleTy) { |
| 1334 | default: return 0; |
| 1335 | case MVT::i8: return X86::CMP8rr; |
| 1336 | case MVT::i16: return X86::CMP16rr; |
| 1337 | case MVT::i32: return X86::CMP32rr; |
| 1338 | case MVT::i64: return X86::CMP64rr; |
| 1339 | case MVT::f32: |
| 1340 | return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0; |
| 1341 | case MVT::f64: |
| 1342 | return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0; |
| 1343 | } |
| 1344 | } |
| 1345 | |
Rafael Espindola | 19141f2 | 2015-03-16 14:05:49 +0000 | [diff] [blame] | 1346 | /// If we have a comparison with RHS as the RHS of the comparison, return an |
| 1347 | /// opcode that works for the compare (e.g. CMP32ri) otherwise return 0. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1348 | static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) { |
Rafael Espindola | 933f51a | 2015-03-16 14:25:08 +0000 | [diff] [blame] | 1349 | int64_t Val = RHSC->getSExtValue(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1350 | switch (VT.getSimpleVT().SimpleTy) { |
| 1351 | // Otherwise, we can't fold the immediate into this comparison. |
Rafael Espindola | 19141f2 | 2015-03-16 14:05:49 +0000 | [diff] [blame] | 1352 | default: |
| 1353 | return 0; |
| 1354 | case MVT::i8: |
| 1355 | return X86::CMP8ri; |
| 1356 | case MVT::i16: |
Rafael Espindola | 933f51a | 2015-03-16 14:25:08 +0000 | [diff] [blame] | 1357 | if (isInt<8>(Val)) |
| 1358 | return X86::CMP16ri8; |
Rafael Espindola | 19141f2 | 2015-03-16 14:05:49 +0000 | [diff] [blame] | 1359 | return X86::CMP16ri; |
| 1360 | case MVT::i32: |
Rafael Espindola | 933f51a | 2015-03-16 14:25:08 +0000 | [diff] [blame] | 1361 | if (isInt<8>(Val)) |
| 1362 | return X86::CMP32ri8; |
Rafael Espindola | 19141f2 | 2015-03-16 14:05:49 +0000 | [diff] [blame] | 1363 | return X86::CMP32ri; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1364 | case MVT::i64: |
Rafael Espindola | 933f51a | 2015-03-16 14:25:08 +0000 | [diff] [blame] | 1365 | if (isInt<8>(Val)) |
| 1366 | return X86::CMP64ri8; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1367 | // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext |
| 1368 | // field. |
Rafael Espindola | 933f51a | 2015-03-16 14:25:08 +0000 | [diff] [blame] | 1369 | if (isInt<32>(Val)) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1370 | return X86::CMP64ri32; |
| 1371 | return 0; |
| 1372 | } |
| 1373 | } |
| 1374 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1375 | bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1, EVT VT, |
| 1376 | const DebugLoc &CurDbgLoc) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1377 | unsigned Op0Reg = getRegForValue(Op0); |
| 1378 | if (Op0Reg == 0) return false; |
| 1379 | |
| 1380 | // Handle 'null' like i32/i64 0. |
| 1381 | if (isa<ConstantPointerNull>(Op1)) |
| 1382 | Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext())); |
| 1383 | |
| 1384 | // We have two options: compare with register or immediate. If the RHS of |
| 1385 | // the compare is an immediate that we can fold into this compare, use |
| 1386 | // CMPri, otherwise use CMPrr. |
| 1387 | if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 1388 | if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) { |
| 1389 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, CurDbgLoc, TII.get(CompareImmOpc)) |
| 1390 | .addReg(Op0Reg) |
| 1391 | .addImm(Op1C->getSExtValue()); |
| 1392 | return true; |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget); |
| 1397 | if (CompareOpc == 0) return false; |
| 1398 | |
| 1399 | unsigned Op1Reg = getRegForValue(Op1); |
| 1400 | if (Op1Reg == 0) return false; |
| 1401 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, CurDbgLoc, TII.get(CompareOpc)) |
| 1402 | .addReg(Op0Reg) |
| 1403 | .addReg(Op1Reg); |
| 1404 | |
| 1405 | return true; |
| 1406 | } |
| 1407 | |
| 1408 | bool X86FastISel::X86SelectCmp(const Instruction *I) { |
| 1409 | const CmpInst *CI = cast<CmpInst>(I); |
| 1410 | |
| 1411 | MVT VT; |
| 1412 | if (!isTypeLegal(I->getOperand(0)->getType(), VT)) |
| 1413 | return false; |
| 1414 | |
Elena Demikhovsky | ad0a56f | 2016-07-06 14:15:43 +0000 | [diff] [blame] | 1415 | if (I->getType()->isIntegerTy(1) && Subtarget->hasAVX512()) |
| 1416 | return false; |
| 1417 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1418 | // Try to optimize or fold the cmp. |
| 1419 | CmpInst::Predicate Predicate = optimizeCmpPredicate(CI); |
| 1420 | unsigned ResultReg = 0; |
| 1421 | switch (Predicate) { |
| 1422 | default: break; |
| 1423 | case CmpInst::FCMP_FALSE: { |
| 1424 | ResultReg = createResultReg(&X86::GR32RegClass); |
| 1425 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0), |
| 1426 | ResultReg); |
| 1427 | ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true, |
| 1428 | X86::sub_8bit); |
| 1429 | if (!ResultReg) |
| 1430 | return false; |
| 1431 | break; |
| 1432 | } |
| 1433 | case CmpInst::FCMP_TRUE: { |
| 1434 | ResultReg = createResultReg(&X86::GR8RegClass); |
| 1435 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri), |
| 1436 | ResultReg).addImm(1); |
| 1437 | break; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | if (ResultReg) { |
| 1442 | updateValueMap(I, ResultReg); |
| 1443 | return true; |
| 1444 | } |
| 1445 | |
| 1446 | const Value *LHS = CI->getOperand(0); |
| 1447 | const Value *RHS = CI->getOperand(1); |
| 1448 | |
| 1449 | // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0. |
| 1450 | // We don't have to materialize a zero constant for this case and can just use |
| 1451 | // %x again on the RHS. |
| 1452 | if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) { |
| 1453 | const auto *RHSC = dyn_cast<ConstantFP>(RHS); |
| 1454 | if (RHSC && RHSC->isNullValue()) |
| 1455 | RHS = LHS; |
| 1456 | } |
| 1457 | |
| 1458 | // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction. |
Craig Topper | 428169a | 2016-09-05 07:14:21 +0000 | [diff] [blame^] | 1459 | static const uint16_t SETFOpcTable[2][3] = { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1460 | { X86::SETEr, X86::SETNPr, X86::AND8rr }, |
| 1461 | { X86::SETNEr, X86::SETPr, X86::OR8rr } |
| 1462 | }; |
Craig Topper | 428169a | 2016-09-05 07:14:21 +0000 | [diff] [blame^] | 1463 | const uint16_t *SETFOpc = nullptr; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1464 | switch (Predicate) { |
| 1465 | default: break; |
| 1466 | case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break; |
| 1467 | case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break; |
| 1468 | } |
| 1469 | |
| 1470 | ResultReg = createResultReg(&X86::GR8RegClass); |
| 1471 | if (SETFOpc) { |
| 1472 | if (!X86FastEmitCompare(LHS, RHS, VT, I->getDebugLoc())) |
| 1473 | return false; |
| 1474 | |
| 1475 | unsigned FlagReg1 = createResultReg(&X86::GR8RegClass); |
| 1476 | unsigned FlagReg2 = createResultReg(&X86::GR8RegClass); |
| 1477 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]), |
| 1478 | FlagReg1); |
| 1479 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]), |
| 1480 | FlagReg2); |
| 1481 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]), |
| 1482 | ResultReg).addReg(FlagReg1).addReg(FlagReg2); |
| 1483 | updateValueMap(I, ResultReg); |
| 1484 | return true; |
| 1485 | } |
| 1486 | |
| 1487 | X86::CondCode CC; |
| 1488 | bool SwapArgs; |
| 1489 | std::tie(CC, SwapArgs) = getX86ConditionCode(Predicate); |
| 1490 | assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code."); |
| 1491 | unsigned Opc = X86::getSETFromCond(CC); |
| 1492 | |
| 1493 | if (SwapArgs) |
| 1494 | std::swap(LHS, RHS); |
| 1495 | |
| 1496 | // Emit a compare of LHS/RHS. |
| 1497 | if (!X86FastEmitCompare(LHS, RHS, VT, I->getDebugLoc())) |
| 1498 | return false; |
| 1499 | |
| 1500 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg); |
| 1501 | updateValueMap(I, ResultReg); |
| 1502 | return true; |
| 1503 | } |
| 1504 | |
| 1505 | bool X86FastISel::X86SelectZExt(const Instruction *I) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1506 | EVT DstVT = TLI.getValueType(DL, I->getType()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1507 | if (!TLI.isTypeLegal(DstVT)) |
| 1508 | return false; |
| 1509 | |
| 1510 | unsigned ResultReg = getRegForValue(I->getOperand(0)); |
| 1511 | if (ResultReg == 0) |
| 1512 | return false; |
| 1513 | |
| 1514 | // Handle zero-extension from i1 to i8, which is common. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1515 | MVT SrcVT = TLI.getSimpleValueType(DL, I->getOperand(0)->getType()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1516 | if (SrcVT.SimpleTy == MVT::i1) { |
| 1517 | // Set the high bits to zero. |
| 1518 | ResultReg = fastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false); |
| 1519 | SrcVT = MVT::i8; |
| 1520 | |
| 1521 | if (ResultReg == 0) |
| 1522 | return false; |
| 1523 | } |
| 1524 | |
| 1525 | if (DstVT == MVT::i64) { |
| 1526 | // Handle extension to 64-bits via sub-register shenanigans. |
| 1527 | unsigned MovInst; |
| 1528 | |
| 1529 | switch (SrcVT.SimpleTy) { |
| 1530 | case MVT::i8: MovInst = X86::MOVZX32rr8; break; |
| 1531 | case MVT::i16: MovInst = X86::MOVZX32rr16; break; |
| 1532 | case MVT::i32: MovInst = X86::MOV32rr; break; |
| 1533 | default: llvm_unreachable("Unexpected zext to i64 source type"); |
| 1534 | } |
| 1535 | |
| 1536 | unsigned Result32 = createResultReg(&X86::GR32RegClass); |
| 1537 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32) |
| 1538 | .addReg(ResultReg); |
| 1539 | |
| 1540 | ResultReg = createResultReg(&X86::GR64RegClass); |
| 1541 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG), |
| 1542 | ResultReg) |
| 1543 | .addImm(0).addReg(Result32).addImm(X86::sub_32bit); |
| 1544 | } else if (DstVT != MVT::i8) { |
| 1545 | ResultReg = fastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND, |
| 1546 | ResultReg, /*Kill=*/true); |
| 1547 | if (ResultReg == 0) |
| 1548 | return false; |
| 1549 | } |
| 1550 | |
| 1551 | updateValueMap(I, ResultReg); |
| 1552 | return true; |
| 1553 | } |
| 1554 | |
| 1555 | bool X86FastISel::X86SelectBranch(const Instruction *I) { |
| 1556 | // Unconditional branches are selected by tablegen-generated code. |
| 1557 | // Handle a conditional branch. |
| 1558 | const BranchInst *BI = cast<BranchInst>(I); |
| 1559 | MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; |
| 1560 | MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; |
| 1561 | |
| 1562 | // Fold the common case of a conditional branch with a comparison |
| 1563 | // in the same block (values defined on other blocks may not have |
| 1564 | // initialized registers). |
| 1565 | X86::CondCode CC; |
| 1566 | if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { |
| 1567 | if (CI->hasOneUse() && CI->getParent() == I->getParent()) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1568 | EVT VT = TLI.getValueType(DL, CI->getOperand(0)->getType()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1569 | |
| 1570 | // Try to optimize or fold the cmp. |
| 1571 | CmpInst::Predicate Predicate = optimizeCmpPredicate(CI); |
| 1572 | switch (Predicate) { |
| 1573 | default: break; |
| 1574 | case CmpInst::FCMP_FALSE: fastEmitBranch(FalseMBB, DbgLoc); return true; |
| 1575 | case CmpInst::FCMP_TRUE: fastEmitBranch(TrueMBB, DbgLoc); return true; |
| 1576 | } |
| 1577 | |
| 1578 | const Value *CmpLHS = CI->getOperand(0); |
| 1579 | const Value *CmpRHS = CI->getOperand(1); |
| 1580 | |
| 1581 | // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, |
| 1582 | // 0.0. |
| 1583 | // We don't have to materialize a zero constant for this case and can just |
| 1584 | // use %x again on the RHS. |
| 1585 | if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) { |
| 1586 | const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS); |
| 1587 | if (CmpRHSC && CmpRHSC->isNullValue()) |
| 1588 | CmpRHS = CmpLHS; |
| 1589 | } |
| 1590 | |
| 1591 | // Try to take advantage of fallthrough opportunities. |
| 1592 | if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) { |
| 1593 | std::swap(TrueMBB, FalseMBB); |
| 1594 | Predicate = CmpInst::getInversePredicate(Predicate); |
| 1595 | } |
| 1596 | |
| 1597 | // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/condition |
| 1598 | // code check. Instead two branch instructions are required to check all |
| 1599 | // the flags. First we change the predicate to a supported condition code, |
| 1600 | // which will be the first branch. Later one we will emit the second |
| 1601 | // branch. |
| 1602 | bool NeedExtraBranch = false; |
| 1603 | switch (Predicate) { |
| 1604 | default: break; |
| 1605 | case CmpInst::FCMP_OEQ: |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 1606 | std::swap(TrueMBB, FalseMBB); |
| 1607 | LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1608 | case CmpInst::FCMP_UNE: |
| 1609 | NeedExtraBranch = true; |
| 1610 | Predicate = CmpInst::FCMP_ONE; |
| 1611 | break; |
| 1612 | } |
| 1613 | |
| 1614 | bool SwapArgs; |
| 1615 | unsigned BranchOpc; |
| 1616 | std::tie(CC, SwapArgs) = getX86ConditionCode(Predicate); |
| 1617 | assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code."); |
| 1618 | |
| 1619 | BranchOpc = X86::GetCondBranchFromCond(CC); |
| 1620 | if (SwapArgs) |
| 1621 | std::swap(CmpLHS, CmpRHS); |
| 1622 | |
| 1623 | // Emit a compare of the LHS and RHS, setting the flags. |
| 1624 | if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT, CI->getDebugLoc())) |
| 1625 | return false; |
| 1626 | |
| 1627 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc)) |
| 1628 | .addMBB(TrueMBB); |
| 1629 | |
| 1630 | // X86 requires a second branch to handle UNE (and OEQ, which is mapped |
| 1631 | // to UNE above). |
| 1632 | if (NeedExtraBranch) { |
| 1633 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_1)) |
| 1634 | .addMBB(TrueMBB); |
| 1635 | } |
| 1636 | |
Matthias Braun | 17af607 | 2015-08-26 01:38:00 +0000 | [diff] [blame] | 1637 | finishCondBranch(BI->getParent(), TrueMBB, FalseMBB); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1638 | return true; |
| 1639 | } |
| 1640 | } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) { |
| 1641 | // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which |
| 1642 | // typically happen for _Bool and C++ bools. |
| 1643 | MVT SourceVT; |
| 1644 | if (TI->hasOneUse() && TI->getParent() == I->getParent() && |
| 1645 | isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) { |
| 1646 | unsigned TestOpc = 0; |
| 1647 | switch (SourceVT.SimpleTy) { |
| 1648 | default: break; |
| 1649 | case MVT::i8: TestOpc = X86::TEST8ri; break; |
| 1650 | case MVT::i16: TestOpc = X86::TEST16ri; break; |
| 1651 | case MVT::i32: TestOpc = X86::TEST32ri; break; |
| 1652 | case MVT::i64: TestOpc = X86::TEST64ri32; break; |
| 1653 | } |
| 1654 | if (TestOpc) { |
| 1655 | unsigned OpReg = getRegForValue(TI->getOperand(0)); |
| 1656 | if (OpReg == 0) return false; |
Guy Blank | 9ae797a | 2016-08-21 08:02:27 +0000 | [diff] [blame] | 1657 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1658 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc)) |
| 1659 | .addReg(OpReg).addImm(1); |
| 1660 | |
| 1661 | unsigned JmpOpc = X86::JNE_1; |
| 1662 | if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) { |
| 1663 | std::swap(TrueMBB, FalseMBB); |
| 1664 | JmpOpc = X86::JE_1; |
| 1665 | } |
| 1666 | |
| 1667 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc)) |
| 1668 | .addMBB(TrueMBB); |
Matthias Braun | 17af607 | 2015-08-26 01:38:00 +0000 | [diff] [blame] | 1669 | |
| 1670 | finishCondBranch(BI->getParent(), TrueMBB, FalseMBB); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1671 | return true; |
| 1672 | } |
| 1673 | } |
| 1674 | } else if (foldX86XALUIntrinsic(CC, BI, BI->getCondition())) { |
| 1675 | // Fake request the condition, otherwise the intrinsic might be completely |
| 1676 | // optimized away. |
| 1677 | unsigned TmpReg = getRegForValue(BI->getCondition()); |
| 1678 | if (TmpReg == 0) |
| 1679 | return false; |
| 1680 | |
| 1681 | unsigned BranchOpc = X86::GetCondBranchFromCond(CC); |
| 1682 | |
| 1683 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc)) |
| 1684 | .addMBB(TrueMBB); |
Matthias Braun | 17af607 | 2015-08-26 01:38:00 +0000 | [diff] [blame] | 1685 | finishCondBranch(BI->getParent(), TrueMBB, FalseMBB); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1686 | return true; |
| 1687 | } |
| 1688 | |
| 1689 | // Otherwise do a clumsy setcc and re-test it. |
| 1690 | // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used |
| 1691 | // in an explicit cast, so make sure to handle that correctly. |
| 1692 | unsigned OpReg = getRegForValue(BI->getCondition()); |
| 1693 | if (OpReg == 0) return false; |
| 1694 | |
Guy Blank | 9ae797a | 2016-08-21 08:02:27 +0000 | [diff] [blame] | 1695 | // In case OpReg is a K register, kortest against itself. |
| 1696 | if (MRI.getRegClass(OpReg) == &X86::VK1RegClass) |
| 1697 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::KORTESTWrr)) |
| 1698 | .addReg(OpReg) |
| 1699 | .addReg(OpReg); |
| 1700 | else |
| 1701 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri)) |
| 1702 | .addReg(OpReg) |
| 1703 | .addImm(1); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1704 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_1)) |
| 1705 | .addMBB(TrueMBB); |
Matthias Braun | 17af607 | 2015-08-26 01:38:00 +0000 | [diff] [blame] | 1706 | finishCondBranch(BI->getParent(), TrueMBB, FalseMBB); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1707 | return true; |
| 1708 | } |
| 1709 | |
| 1710 | bool X86FastISel::X86SelectShift(const Instruction *I) { |
| 1711 | unsigned CReg = 0, OpReg = 0; |
| 1712 | const TargetRegisterClass *RC = nullptr; |
| 1713 | if (I->getType()->isIntegerTy(8)) { |
| 1714 | CReg = X86::CL; |
| 1715 | RC = &X86::GR8RegClass; |
| 1716 | switch (I->getOpcode()) { |
| 1717 | case Instruction::LShr: OpReg = X86::SHR8rCL; break; |
| 1718 | case Instruction::AShr: OpReg = X86::SAR8rCL; break; |
| 1719 | case Instruction::Shl: OpReg = X86::SHL8rCL; break; |
| 1720 | default: return false; |
| 1721 | } |
| 1722 | } else if (I->getType()->isIntegerTy(16)) { |
| 1723 | CReg = X86::CX; |
| 1724 | RC = &X86::GR16RegClass; |
| 1725 | switch (I->getOpcode()) { |
| 1726 | case Instruction::LShr: OpReg = X86::SHR16rCL; break; |
| 1727 | case Instruction::AShr: OpReg = X86::SAR16rCL; break; |
| 1728 | case Instruction::Shl: OpReg = X86::SHL16rCL; break; |
| 1729 | default: return false; |
| 1730 | } |
| 1731 | } else if (I->getType()->isIntegerTy(32)) { |
| 1732 | CReg = X86::ECX; |
| 1733 | RC = &X86::GR32RegClass; |
| 1734 | switch (I->getOpcode()) { |
| 1735 | case Instruction::LShr: OpReg = X86::SHR32rCL; break; |
| 1736 | case Instruction::AShr: OpReg = X86::SAR32rCL; break; |
| 1737 | case Instruction::Shl: OpReg = X86::SHL32rCL; break; |
| 1738 | default: return false; |
| 1739 | } |
| 1740 | } else if (I->getType()->isIntegerTy(64)) { |
| 1741 | CReg = X86::RCX; |
| 1742 | RC = &X86::GR64RegClass; |
| 1743 | switch (I->getOpcode()) { |
| 1744 | case Instruction::LShr: OpReg = X86::SHR64rCL; break; |
| 1745 | case Instruction::AShr: OpReg = X86::SAR64rCL; break; |
| 1746 | case Instruction::Shl: OpReg = X86::SHL64rCL; break; |
| 1747 | default: return false; |
| 1748 | } |
| 1749 | } else { |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | MVT VT; |
| 1754 | if (!isTypeLegal(I->getType(), VT)) |
| 1755 | return false; |
| 1756 | |
| 1757 | unsigned Op0Reg = getRegForValue(I->getOperand(0)); |
| 1758 | if (Op0Reg == 0) return false; |
| 1759 | |
| 1760 | unsigned Op1Reg = getRegForValue(I->getOperand(1)); |
| 1761 | if (Op1Reg == 0) return false; |
| 1762 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY), |
| 1763 | CReg).addReg(Op1Reg); |
| 1764 | |
| 1765 | // The shift instruction uses X86::CL. If we defined a super-register |
| 1766 | // of X86::CL, emit a subreg KILL to precisely describe what we're doing here. |
| 1767 | if (CReg != X86::CL) |
| 1768 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1769 | TII.get(TargetOpcode::KILL), X86::CL) |
| 1770 | .addReg(CReg, RegState::Kill); |
| 1771 | |
| 1772 | unsigned ResultReg = createResultReg(RC); |
| 1773 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg) |
| 1774 | .addReg(Op0Reg); |
| 1775 | updateValueMap(I, ResultReg); |
| 1776 | return true; |
| 1777 | } |
| 1778 | |
| 1779 | bool X86FastISel::X86SelectDivRem(const Instruction *I) { |
| 1780 | const static unsigned NumTypes = 4; // i8, i16, i32, i64 |
| 1781 | const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem |
| 1782 | const static bool S = true; // IsSigned |
| 1783 | const static bool U = false; // !IsSigned |
| 1784 | const static unsigned Copy = TargetOpcode::COPY; |
| 1785 | // For the X86 DIV/IDIV instruction, in most cases the dividend |
| 1786 | // (numerator) must be in a specific register pair highreg:lowreg, |
| 1787 | // producing the quotient in lowreg and the remainder in highreg. |
| 1788 | // For most data types, to set up the instruction, the dividend is |
| 1789 | // copied into lowreg, and lowreg is sign-extended or zero-extended |
| 1790 | // into highreg. The exception is i8, where the dividend is defined |
| 1791 | // as a single register rather than a register pair, and we |
| 1792 | // therefore directly sign-extend or zero-extend the dividend into |
| 1793 | // lowreg, instead of copying, and ignore the highreg. |
| 1794 | const static struct DivRemEntry { |
| 1795 | // The following portion depends only on the data type. |
| 1796 | const TargetRegisterClass *RC; |
| 1797 | unsigned LowInReg; // low part of the register pair |
| 1798 | unsigned HighInReg; // high part of the register pair |
| 1799 | // The following portion depends on both the data type and the operation. |
| 1800 | struct DivRemResult { |
| 1801 | unsigned OpDivRem; // The specific DIV/IDIV opcode to use. |
| 1802 | unsigned OpSignExtend; // Opcode for sign-extending lowreg into |
| 1803 | // highreg, or copying a zero into highreg. |
| 1804 | unsigned OpCopy; // Opcode for copying dividend into lowreg, or |
| 1805 | // zero/sign-extending into lowreg for i8. |
| 1806 | unsigned DivRemResultReg; // Register containing the desired result. |
| 1807 | bool IsOpSigned; // Whether to use signed or unsigned form. |
| 1808 | } ResultTable[NumOps]; |
| 1809 | } OpTable[NumTypes] = { |
| 1810 | { &X86::GR8RegClass, X86::AX, 0, { |
| 1811 | { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv |
| 1812 | { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem |
| 1813 | { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv |
| 1814 | { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem |
| 1815 | } |
| 1816 | }, // i8 |
| 1817 | { &X86::GR16RegClass, X86::AX, X86::DX, { |
| 1818 | { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv |
| 1819 | { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem |
| 1820 | { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv |
| 1821 | { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem |
| 1822 | } |
| 1823 | }, // i16 |
| 1824 | { &X86::GR32RegClass, X86::EAX, X86::EDX, { |
| 1825 | { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv |
| 1826 | { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem |
| 1827 | { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv |
| 1828 | { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem |
| 1829 | } |
| 1830 | }, // i32 |
| 1831 | { &X86::GR64RegClass, X86::RAX, X86::RDX, { |
| 1832 | { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv |
| 1833 | { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem |
| 1834 | { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv |
| 1835 | { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem |
| 1836 | } |
| 1837 | }, // i64 |
| 1838 | }; |
| 1839 | |
| 1840 | MVT VT; |
| 1841 | if (!isTypeLegal(I->getType(), VT)) |
| 1842 | return false; |
| 1843 | |
| 1844 | unsigned TypeIndex, OpIndex; |
| 1845 | switch (VT.SimpleTy) { |
| 1846 | default: return false; |
| 1847 | case MVT::i8: TypeIndex = 0; break; |
| 1848 | case MVT::i16: TypeIndex = 1; break; |
| 1849 | case MVT::i32: TypeIndex = 2; break; |
| 1850 | case MVT::i64: TypeIndex = 3; |
| 1851 | if (!Subtarget->is64Bit()) |
| 1852 | return false; |
| 1853 | break; |
| 1854 | } |
| 1855 | |
| 1856 | switch (I->getOpcode()) { |
| 1857 | default: llvm_unreachable("Unexpected div/rem opcode"); |
| 1858 | case Instruction::SDiv: OpIndex = 0; break; |
| 1859 | case Instruction::SRem: OpIndex = 1; break; |
| 1860 | case Instruction::UDiv: OpIndex = 2; break; |
| 1861 | case Instruction::URem: OpIndex = 3; break; |
| 1862 | } |
| 1863 | |
| 1864 | const DivRemEntry &TypeEntry = OpTable[TypeIndex]; |
| 1865 | const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex]; |
| 1866 | unsigned Op0Reg = getRegForValue(I->getOperand(0)); |
| 1867 | if (Op0Reg == 0) |
| 1868 | return false; |
| 1869 | unsigned Op1Reg = getRegForValue(I->getOperand(1)); |
| 1870 | if (Op1Reg == 0) |
| 1871 | return false; |
| 1872 | |
| 1873 | // Move op0 into low-order input register. |
| 1874 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1875 | TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg); |
| 1876 | // Zero-extend or sign-extend into high-order input register. |
| 1877 | if (OpEntry.OpSignExtend) { |
| 1878 | if (OpEntry.IsOpSigned) |
| 1879 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1880 | TII.get(OpEntry.OpSignExtend)); |
| 1881 | else { |
| 1882 | unsigned Zero32 = createResultReg(&X86::GR32RegClass); |
| 1883 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1884 | TII.get(X86::MOV32r0), Zero32); |
| 1885 | |
| 1886 | // Copy the zero into the appropriate sub/super/identical physical |
| 1887 | // register. Unfortunately the operations needed are not uniform enough |
| 1888 | // to fit neatly into the table above. |
| 1889 | if (VT.SimpleTy == MVT::i16) { |
| 1890 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1891 | TII.get(Copy), TypeEntry.HighInReg) |
| 1892 | .addReg(Zero32, 0, X86::sub_16bit); |
| 1893 | } else if (VT.SimpleTy == MVT::i32) { |
| 1894 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1895 | TII.get(Copy), TypeEntry.HighInReg) |
| 1896 | .addReg(Zero32); |
| 1897 | } else if (VT.SimpleTy == MVT::i64) { |
| 1898 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1899 | TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg) |
| 1900 | .addImm(0).addReg(Zero32).addImm(X86::sub_32bit); |
| 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | // Generate the DIV/IDIV instruction. |
| 1905 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1906 | TII.get(OpEntry.OpDivRem)).addReg(Op1Reg); |
| 1907 | // For i8 remainder, we can't reference AH directly, as we'll end |
| 1908 | // up with bogus copies like %R9B = COPY %AH. Reference AX |
| 1909 | // instead to prevent AH references in a REX instruction. |
| 1910 | // |
| 1911 | // The current assumption of the fast register allocator is that isel |
| 1912 | // won't generate explicit references to the GPR8_NOREX registers. If |
| 1913 | // the allocator and/or the backend get enhanced to be more robust in |
| 1914 | // that regard, this can be, and should be, removed. |
| 1915 | unsigned ResultReg = 0; |
| 1916 | if ((I->getOpcode() == Instruction::SRem || |
| 1917 | I->getOpcode() == Instruction::URem) && |
| 1918 | OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) { |
| 1919 | unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass); |
| 1920 | unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass); |
| 1921 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1922 | TII.get(Copy), SourceSuperReg).addReg(X86::AX); |
| 1923 | |
| 1924 | // Shift AX right by 8 bits instead of using AH. |
| 1925 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri), |
| 1926 | ResultSuperReg).addReg(SourceSuperReg).addImm(8); |
| 1927 | |
| 1928 | // Now reference the 8-bit subreg of the result. |
| 1929 | ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultSuperReg, |
| 1930 | /*Kill=*/true, X86::sub_8bit); |
| 1931 | } |
| 1932 | // Copy the result out of the physreg if we haven't already. |
| 1933 | if (!ResultReg) { |
| 1934 | ResultReg = createResultReg(TypeEntry.RC); |
| 1935 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg) |
| 1936 | .addReg(OpEntry.DivRemResultReg); |
| 1937 | } |
| 1938 | updateValueMap(I, ResultReg); |
| 1939 | |
| 1940 | return true; |
| 1941 | } |
| 1942 | |
| 1943 | /// \brief Emit a conditional move instruction (if the are supported) to lower |
| 1944 | /// the select. |
| 1945 | bool X86FastISel::X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I) { |
| 1946 | // Check if the subtarget supports these instructions. |
| 1947 | if (!Subtarget->hasCMov()) |
| 1948 | return false; |
| 1949 | |
| 1950 | // FIXME: Add support for i8. |
| 1951 | if (RetVT < MVT::i16 || RetVT > MVT::i64) |
| 1952 | return false; |
| 1953 | |
| 1954 | const Value *Cond = I->getOperand(0); |
| 1955 | const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT); |
| 1956 | bool NeedTest = true; |
| 1957 | X86::CondCode CC = X86::COND_NE; |
| 1958 | |
| 1959 | // Optimize conditions coming from a compare if both instructions are in the |
| 1960 | // same basic block (values defined in other basic blocks may not have |
| 1961 | // initialized registers). |
| 1962 | const auto *CI = dyn_cast<CmpInst>(Cond); |
| 1963 | if (CI && (CI->getParent() == I->getParent())) { |
| 1964 | CmpInst::Predicate Predicate = optimizeCmpPredicate(CI); |
| 1965 | |
| 1966 | // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction. |
Craig Topper | 428169a | 2016-09-05 07:14:21 +0000 | [diff] [blame^] | 1967 | static const uint16_t SETFOpcTable[2][3] = { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1968 | { X86::SETNPr, X86::SETEr , X86::TEST8rr }, |
| 1969 | { X86::SETPr, X86::SETNEr, X86::OR8rr } |
| 1970 | }; |
Craig Topper | 428169a | 2016-09-05 07:14:21 +0000 | [diff] [blame^] | 1971 | const uint16_t *SETFOpc = nullptr; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1972 | switch (Predicate) { |
| 1973 | default: break; |
| 1974 | case CmpInst::FCMP_OEQ: |
| 1975 | SETFOpc = &SETFOpcTable[0][0]; |
| 1976 | Predicate = CmpInst::ICMP_NE; |
| 1977 | break; |
| 1978 | case CmpInst::FCMP_UNE: |
| 1979 | SETFOpc = &SETFOpcTable[1][0]; |
| 1980 | Predicate = CmpInst::ICMP_NE; |
| 1981 | break; |
| 1982 | } |
| 1983 | |
| 1984 | bool NeedSwap; |
| 1985 | std::tie(CC, NeedSwap) = getX86ConditionCode(Predicate); |
| 1986 | assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code."); |
| 1987 | |
| 1988 | const Value *CmpLHS = CI->getOperand(0); |
| 1989 | const Value *CmpRHS = CI->getOperand(1); |
| 1990 | if (NeedSwap) |
| 1991 | std::swap(CmpLHS, CmpRHS); |
| 1992 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1993 | EVT CmpVT = TLI.getValueType(DL, CmpLHS->getType()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1994 | // Emit a compare of the LHS and RHS, setting the flags. |
| 1995 | if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT, CI->getDebugLoc())) |
| 1996 | return false; |
| 1997 | |
| 1998 | if (SETFOpc) { |
| 1999 | unsigned FlagReg1 = createResultReg(&X86::GR8RegClass); |
| 2000 | unsigned FlagReg2 = createResultReg(&X86::GR8RegClass); |
| 2001 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]), |
| 2002 | FlagReg1); |
| 2003 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]), |
| 2004 | FlagReg2); |
| 2005 | auto const &II = TII.get(SETFOpc[2]); |
| 2006 | if (II.getNumDefs()) { |
| 2007 | unsigned TmpReg = createResultReg(&X86::GR8RegClass); |
| 2008 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, TmpReg) |
| 2009 | .addReg(FlagReg2).addReg(FlagReg1); |
| 2010 | } else { |
| 2011 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| 2012 | .addReg(FlagReg2).addReg(FlagReg1); |
| 2013 | } |
| 2014 | } |
| 2015 | NeedTest = false; |
| 2016 | } else if (foldX86XALUIntrinsic(CC, I, Cond)) { |
| 2017 | // Fake request the condition, otherwise the intrinsic might be completely |
| 2018 | // optimized away. |
| 2019 | unsigned TmpReg = getRegForValue(Cond); |
| 2020 | if (TmpReg == 0) |
| 2021 | return false; |
| 2022 | |
| 2023 | NeedTest = false; |
| 2024 | } |
| 2025 | |
| 2026 | if (NeedTest) { |
| 2027 | // Selects operate on i1, however, CondReg is 8 bits width and may contain |
| 2028 | // garbage. Indeed, only the less significant bit is supposed to be |
| 2029 | // accurate. If we read more than the lsb, we may see non-zero values |
| 2030 | // whereas lsb is zero. Therefore, we have to truncate Op0Reg to i1 for |
| 2031 | // the select. This is achieved by performing TEST against 1. |
| 2032 | unsigned CondReg = getRegForValue(Cond); |
| 2033 | if (CondReg == 0) |
| 2034 | return false; |
| 2035 | bool CondIsKill = hasTrivialKill(Cond); |
| 2036 | |
Guy Blank | 9ae797a | 2016-08-21 08:02:27 +0000 | [diff] [blame] | 2037 | // In case OpReg is a K register, kortest against itself. |
| 2038 | if (MRI.getRegClass(CondReg) == &X86::VK1RegClass) |
| 2039 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2040 | TII.get(X86::KORTESTWrr)) |
| 2041 | .addReg(CondReg, getKillRegState(CondIsKill)) |
| 2042 | .addReg(CondReg); |
| 2043 | else |
| 2044 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri)) |
| 2045 | .addReg(CondReg, getKillRegState(CondIsKill)) |
| 2046 | .addImm(1); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | const Value *LHS = I->getOperand(1); |
| 2050 | const Value *RHS = I->getOperand(2); |
| 2051 | |
| 2052 | unsigned RHSReg = getRegForValue(RHS); |
| 2053 | bool RHSIsKill = hasTrivialKill(RHS); |
| 2054 | |
| 2055 | unsigned LHSReg = getRegForValue(LHS); |
| 2056 | bool LHSIsKill = hasTrivialKill(LHS); |
| 2057 | |
| 2058 | if (!LHSReg || !RHSReg) |
| 2059 | return false; |
| 2060 | |
| 2061 | unsigned Opc = X86::getCMovFromCond(CC, RC->getSize()); |
| 2062 | unsigned ResultReg = fastEmitInst_rr(Opc, RC, RHSReg, RHSIsKill, |
| 2063 | LHSReg, LHSIsKill); |
| 2064 | updateValueMap(I, ResultReg); |
| 2065 | return true; |
| 2066 | } |
| 2067 | |
Sanjay Patel | 302404b | 2015-03-05 21:46:54 +0000 | [diff] [blame] | 2068 | /// \brief Emit SSE or AVX instructions to lower the select. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2069 | /// |
| 2070 | /// Try to use SSE1/SSE2 instructions to simulate a select without branches. |
| 2071 | /// This lowers fp selects into a CMP/AND/ANDN/OR sequence when the necessary |
Sanjay Patel | 302404b | 2015-03-05 21:46:54 +0000 | [diff] [blame] | 2072 | /// SSE instructions are available. If AVX is available, try to use a VBLENDV. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2073 | bool X86FastISel::X86FastEmitSSESelect(MVT RetVT, const Instruction *I) { |
| 2074 | // Optimize conditions coming from a compare if both instructions are in the |
| 2075 | // same basic block (values defined in other basic blocks may not have |
| 2076 | // initialized registers). |
| 2077 | const auto *CI = dyn_cast<FCmpInst>(I->getOperand(0)); |
| 2078 | if (!CI || (CI->getParent() != I->getParent())) |
| 2079 | return false; |
| 2080 | |
| 2081 | if (I->getType() != CI->getOperand(0)->getType() || |
| 2082 | !((Subtarget->hasSSE1() && RetVT == MVT::f32) || |
| 2083 | (Subtarget->hasSSE2() && RetVT == MVT::f64))) |
| 2084 | return false; |
| 2085 | |
| 2086 | const Value *CmpLHS = CI->getOperand(0); |
| 2087 | const Value *CmpRHS = CI->getOperand(1); |
| 2088 | CmpInst::Predicate Predicate = optimizeCmpPredicate(CI); |
| 2089 | |
| 2090 | // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0. |
| 2091 | // We don't have to materialize a zero constant for this case and can just use |
| 2092 | // %x again on the RHS. |
| 2093 | if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) { |
| 2094 | const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS); |
| 2095 | if (CmpRHSC && CmpRHSC->isNullValue()) |
| 2096 | CmpRHS = CmpLHS; |
| 2097 | } |
| 2098 | |
| 2099 | unsigned CC; |
| 2100 | bool NeedSwap; |
| 2101 | std::tie(CC, NeedSwap) = getX86SSEConditionCode(Predicate); |
| 2102 | if (CC > 7) |
| 2103 | return false; |
| 2104 | |
| 2105 | if (NeedSwap) |
| 2106 | std::swap(CmpLHS, CmpRHS); |
| 2107 | |
Sanjay Patel | 302404b | 2015-03-05 21:46:54 +0000 | [diff] [blame] | 2108 | // Choose the SSE instruction sequence based on data type (float or double). |
Craig Topper | 428169a | 2016-09-05 07:14:21 +0000 | [diff] [blame^] | 2109 | static const uint16_t OpcTable[2][4] = { |
Sanjay Patel | 302404b | 2015-03-05 21:46:54 +0000 | [diff] [blame] | 2110 | { X86::CMPSSrr, X86::FsANDPSrr, X86::FsANDNPSrr, X86::FsORPSrr }, |
| 2111 | { X86::CMPSDrr, X86::FsANDPDrr, X86::FsANDNPDrr, X86::FsORPDrr } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2112 | }; |
| 2113 | |
Craig Topper | 428169a | 2016-09-05 07:14:21 +0000 | [diff] [blame^] | 2114 | const uint16_t *Opc = nullptr; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2115 | switch (RetVT.SimpleTy) { |
| 2116 | default: return false; |
Sanjay Patel | 302404b | 2015-03-05 21:46:54 +0000 | [diff] [blame] | 2117 | case MVT::f32: Opc = &OpcTable[0][0]; break; |
| 2118 | case MVT::f64: Opc = &OpcTable[1][0]; break; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | const Value *LHS = I->getOperand(1); |
| 2122 | const Value *RHS = I->getOperand(2); |
| 2123 | |
| 2124 | unsigned LHSReg = getRegForValue(LHS); |
| 2125 | bool LHSIsKill = hasTrivialKill(LHS); |
| 2126 | |
| 2127 | unsigned RHSReg = getRegForValue(RHS); |
| 2128 | bool RHSIsKill = hasTrivialKill(RHS); |
| 2129 | |
| 2130 | unsigned CmpLHSReg = getRegForValue(CmpLHS); |
| 2131 | bool CmpLHSIsKill = hasTrivialKill(CmpLHS); |
| 2132 | |
| 2133 | unsigned CmpRHSReg = getRegForValue(CmpRHS); |
| 2134 | bool CmpRHSIsKill = hasTrivialKill(CmpRHS); |
| 2135 | |
| 2136 | if (!LHSReg || !RHSReg || !CmpLHS || !CmpRHS) |
| 2137 | return false; |
| 2138 | |
| 2139 | const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT); |
Sanjay Patel | 302404b | 2015-03-05 21:46:54 +0000 | [diff] [blame] | 2140 | unsigned ResultReg; |
| 2141 | |
| 2142 | if (Subtarget->hasAVX()) { |
Matthias Braun | 818c78d | 2015-08-31 18:25:11 +0000 | [diff] [blame] | 2143 | const TargetRegisterClass *FR32 = &X86::FR32RegClass; |
| 2144 | const TargetRegisterClass *VR128 = &X86::VR128RegClass; |
| 2145 | |
Sanjay Patel | 302404b | 2015-03-05 21:46:54 +0000 | [diff] [blame] | 2146 | // If we have AVX, create 1 blendv instead of 3 logic instructions. |
| 2147 | // Blendv was introduced with SSE 4.1, but the 2 register form implicitly |
| 2148 | // uses XMM0 as the selection register. That may need just as many |
| 2149 | // instructions as the AND/ANDN/OR sequence due to register moves, so |
| 2150 | // don't bother. |
| 2151 | unsigned CmpOpcode = |
| 2152 | (RetVT.SimpleTy == MVT::f32) ? X86::VCMPSSrr : X86::VCMPSDrr; |
| 2153 | unsigned BlendOpcode = |
| 2154 | (RetVT.SimpleTy == MVT::f32) ? X86::VBLENDVPSrr : X86::VBLENDVPDrr; |
| 2155 | |
Matthias Braun | 818c78d | 2015-08-31 18:25:11 +0000 | [diff] [blame] | 2156 | unsigned CmpReg = fastEmitInst_rri(CmpOpcode, FR32, CmpLHSReg, CmpLHSIsKill, |
Sanjay Patel | 302404b | 2015-03-05 21:46:54 +0000 | [diff] [blame] | 2157 | CmpRHSReg, CmpRHSIsKill, CC); |
Matthias Braun | 818c78d | 2015-08-31 18:25:11 +0000 | [diff] [blame] | 2158 | unsigned VBlendReg = fastEmitInst_rrr(BlendOpcode, VR128, RHSReg, RHSIsKill, |
| 2159 | LHSReg, LHSIsKill, CmpReg, true); |
| 2160 | ResultReg = createResultReg(RC); |
| 2161 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2162 | TII.get(TargetOpcode::COPY), ResultReg).addReg(VBlendReg); |
Sanjay Patel | 302404b | 2015-03-05 21:46:54 +0000 | [diff] [blame] | 2163 | } else { |
| 2164 | unsigned CmpReg = fastEmitInst_rri(Opc[0], RC, CmpLHSReg, CmpLHSIsKill, |
| 2165 | CmpRHSReg, CmpRHSIsKill, CC); |
| 2166 | unsigned AndReg = fastEmitInst_rr(Opc[1], RC, CmpReg, /*IsKill=*/false, |
| 2167 | LHSReg, LHSIsKill); |
| 2168 | unsigned AndNReg = fastEmitInst_rr(Opc[2], RC, CmpReg, /*IsKill=*/true, |
| 2169 | RHSReg, RHSIsKill); |
| 2170 | ResultReg = fastEmitInst_rr(Opc[3], RC, AndNReg, /*IsKill=*/true, |
| 2171 | AndReg, /*IsKill=*/true); |
| 2172 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2173 | updateValueMap(I, ResultReg); |
| 2174 | return true; |
| 2175 | } |
| 2176 | |
| 2177 | bool X86FastISel::X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I) { |
| 2178 | // These are pseudo CMOV instructions and will be later expanded into control- |
| 2179 | // flow. |
| 2180 | unsigned Opc; |
| 2181 | switch (RetVT.SimpleTy) { |
| 2182 | default: return false; |
| 2183 | case MVT::i8: Opc = X86::CMOV_GR8; break; |
| 2184 | case MVT::i16: Opc = X86::CMOV_GR16; break; |
| 2185 | case MVT::i32: Opc = X86::CMOV_GR32; break; |
| 2186 | case MVT::f32: Opc = X86::CMOV_FR32; break; |
| 2187 | case MVT::f64: Opc = X86::CMOV_FR64; break; |
| 2188 | } |
| 2189 | |
| 2190 | const Value *Cond = I->getOperand(0); |
| 2191 | X86::CondCode CC = X86::COND_NE; |
| 2192 | |
| 2193 | // Optimize conditions coming from a compare if both instructions are in the |
| 2194 | // same basic block (values defined in other basic blocks may not have |
| 2195 | // initialized registers). |
| 2196 | const auto *CI = dyn_cast<CmpInst>(Cond); |
| 2197 | if (CI && (CI->getParent() == I->getParent())) { |
| 2198 | bool NeedSwap; |
| 2199 | std::tie(CC, NeedSwap) = getX86ConditionCode(CI->getPredicate()); |
| 2200 | if (CC > X86::LAST_VALID_COND) |
| 2201 | return false; |
| 2202 | |
| 2203 | const Value *CmpLHS = CI->getOperand(0); |
| 2204 | const Value *CmpRHS = CI->getOperand(1); |
| 2205 | |
| 2206 | if (NeedSwap) |
| 2207 | std::swap(CmpLHS, CmpRHS); |
| 2208 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2209 | EVT CmpVT = TLI.getValueType(DL, CmpLHS->getType()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2210 | if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT, CI->getDebugLoc())) |
| 2211 | return false; |
| 2212 | } else { |
| 2213 | unsigned CondReg = getRegForValue(Cond); |
| 2214 | if (CondReg == 0) |
| 2215 | return false; |
| 2216 | bool CondIsKill = hasTrivialKill(Cond); |
Guy Blank | 9ae797a | 2016-08-21 08:02:27 +0000 | [diff] [blame] | 2217 | |
| 2218 | // In case OpReg is a K register, kortest against itself. |
| 2219 | if (MRI.getRegClass(CondReg) == &X86::VK1RegClass) |
| 2220 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2221 | TII.get(X86::KORTESTWrr)) |
| 2222 | .addReg(CondReg, getKillRegState(CondIsKill)) |
| 2223 | .addReg(CondReg); |
| 2224 | else |
| 2225 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri)) |
| 2226 | .addReg(CondReg, getKillRegState(CondIsKill)) |
| 2227 | .addImm(1); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | const Value *LHS = I->getOperand(1); |
| 2231 | const Value *RHS = I->getOperand(2); |
| 2232 | |
| 2233 | unsigned LHSReg = getRegForValue(LHS); |
| 2234 | bool LHSIsKill = hasTrivialKill(LHS); |
| 2235 | |
| 2236 | unsigned RHSReg = getRegForValue(RHS); |
| 2237 | bool RHSIsKill = hasTrivialKill(RHS); |
| 2238 | |
| 2239 | if (!LHSReg || !RHSReg) |
| 2240 | return false; |
| 2241 | |
| 2242 | const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT); |
| 2243 | |
| 2244 | unsigned ResultReg = |
| 2245 | fastEmitInst_rri(Opc, RC, RHSReg, RHSIsKill, LHSReg, LHSIsKill, CC); |
| 2246 | updateValueMap(I, ResultReg); |
| 2247 | return true; |
| 2248 | } |
| 2249 | |
| 2250 | bool X86FastISel::X86SelectSelect(const Instruction *I) { |
| 2251 | MVT RetVT; |
| 2252 | if (!isTypeLegal(I->getType(), RetVT)) |
| 2253 | return false; |
| 2254 | |
| 2255 | // Check if we can fold the select. |
| 2256 | if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) { |
| 2257 | CmpInst::Predicate Predicate = optimizeCmpPredicate(CI); |
| 2258 | const Value *Opnd = nullptr; |
| 2259 | switch (Predicate) { |
| 2260 | default: break; |
| 2261 | case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break; |
| 2262 | case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break; |
| 2263 | } |
| 2264 | // No need for a select anymore - this is an unconditional move. |
| 2265 | if (Opnd) { |
| 2266 | unsigned OpReg = getRegForValue(Opnd); |
| 2267 | if (OpReg == 0) |
| 2268 | return false; |
| 2269 | bool OpIsKill = hasTrivialKill(Opnd); |
| 2270 | const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT); |
| 2271 | unsigned ResultReg = createResultReg(RC); |
| 2272 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2273 | TII.get(TargetOpcode::COPY), ResultReg) |
| 2274 | .addReg(OpReg, getKillRegState(OpIsKill)); |
| 2275 | updateValueMap(I, ResultReg); |
| 2276 | return true; |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | // First try to use real conditional move instructions. |
| 2281 | if (X86FastEmitCMoveSelect(RetVT, I)) |
| 2282 | return true; |
| 2283 | |
| 2284 | // Try to use a sequence of SSE instructions to simulate a conditional move. |
| 2285 | if (X86FastEmitSSESelect(RetVT, I)) |
| 2286 | return true; |
| 2287 | |
| 2288 | // Fall-back to pseudo conditional move instructions, which will be later |
| 2289 | // converted to control-flow. |
| 2290 | if (X86FastEmitPseudoSelect(RetVT, I)) |
| 2291 | return true; |
| 2292 | |
| 2293 | return false; |
| 2294 | } |
| 2295 | |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 2296 | bool X86FastISel::X86SelectSIToFP(const Instruction *I) { |
Andrea Di Biagio | 98c3670 | 2015-04-20 11:56:59 +0000 | [diff] [blame] | 2297 | // The target-independent selection algorithm in FastISel already knows how |
| 2298 | // to select a SINT_TO_FP if the target is SSE but not AVX. |
| 2299 | // Early exit if the subtarget doesn't have AVX. |
| 2300 | if (!Subtarget->hasAVX()) |
| 2301 | return false; |
| 2302 | |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 2303 | if (!I->getOperand(0)->getType()->isIntegerTy(32)) |
| 2304 | return false; |
| 2305 | |
| 2306 | // Select integer to float/double conversion. |
| 2307 | unsigned OpReg = getRegForValue(I->getOperand(0)); |
| 2308 | if (OpReg == 0) |
| 2309 | return false; |
| 2310 | |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 2311 | const TargetRegisterClass *RC = nullptr; |
| 2312 | unsigned Opcode; |
| 2313 | |
Andrea Di Biagio | df93ccf | 2015-03-04 14:23:25 +0000 | [diff] [blame] | 2314 | if (I->getType()->isDoubleTy()) { |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 2315 | // sitofp int -> double |
Andrea Di Biagio | df93ccf | 2015-03-04 14:23:25 +0000 | [diff] [blame] | 2316 | Opcode = X86::VCVTSI2SDrr; |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 2317 | RC = &X86::FR64RegClass; |
Andrea Di Biagio | df93ccf | 2015-03-04 14:23:25 +0000 | [diff] [blame] | 2318 | } else if (I->getType()->isFloatTy()) { |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 2319 | // sitofp int -> float |
Andrea Di Biagio | df93ccf | 2015-03-04 14:23:25 +0000 | [diff] [blame] | 2320 | Opcode = X86::VCVTSI2SSrr; |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 2321 | RC = &X86::FR32RegClass; |
| 2322 | } else |
| 2323 | return false; |
| 2324 | |
Andrea Di Biagio | df93ccf | 2015-03-04 14:23:25 +0000 | [diff] [blame] | 2325 | unsigned ImplicitDefReg = createResultReg(RC); |
| 2326 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2327 | TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg); |
| 2328 | unsigned ResultReg = |
| 2329 | fastEmitInst_rr(Opcode, RC, ImplicitDefReg, true, OpReg, false); |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 2330 | updateValueMap(I, ResultReg); |
| 2331 | return true; |
| 2332 | } |
| 2333 | |
Andrea Di Biagio | 62622d2 | 2015-02-10 12:04:41 +0000 | [diff] [blame] | 2334 | // Helper method used by X86SelectFPExt and X86SelectFPTrunc. |
| 2335 | bool X86FastISel::X86SelectFPExtOrFPTrunc(const Instruction *I, |
| 2336 | unsigned TargetOpc, |
| 2337 | const TargetRegisterClass *RC) { |
| 2338 | assert((I->getOpcode() == Instruction::FPExt || |
| 2339 | I->getOpcode() == Instruction::FPTrunc) && |
| 2340 | "Instruction must be an FPExt or FPTrunc!"); |
| 2341 | |
| 2342 | unsigned OpReg = getRegForValue(I->getOperand(0)); |
| 2343 | if (OpReg == 0) |
| 2344 | return false; |
| 2345 | |
| 2346 | unsigned ResultReg = createResultReg(RC); |
| 2347 | MachineInstrBuilder MIB; |
| 2348 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpc), |
| 2349 | ResultReg); |
| 2350 | if (Subtarget->hasAVX()) |
| 2351 | MIB.addReg(OpReg); |
| 2352 | MIB.addReg(OpReg); |
| 2353 | updateValueMap(I, ResultReg); |
| 2354 | return true; |
| 2355 | } |
| 2356 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2357 | bool X86FastISel::X86SelectFPExt(const Instruction *I) { |
Andrea Di Biagio | 62622d2 | 2015-02-10 12:04:41 +0000 | [diff] [blame] | 2358 | if (X86ScalarSSEf64 && I->getType()->isDoubleTy() && |
| 2359 | I->getOperand(0)->getType()->isFloatTy()) { |
| 2360 | // fpext from float to double. |
| 2361 | unsigned Opc = Subtarget->hasAVX() ? X86::VCVTSS2SDrr : X86::CVTSS2SDrr; |
| 2362 | return X86SelectFPExtOrFPTrunc(I, Opc, &X86::FR64RegClass); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | return false; |
| 2366 | } |
| 2367 | |
| 2368 | bool X86FastISel::X86SelectFPTrunc(const Instruction *I) { |
Andrea Di Biagio | 62622d2 | 2015-02-10 12:04:41 +0000 | [diff] [blame] | 2369 | if (X86ScalarSSEf64 && I->getType()->isFloatTy() && |
| 2370 | I->getOperand(0)->getType()->isDoubleTy()) { |
| 2371 | // fptrunc from double to float. |
| 2372 | unsigned Opc = Subtarget->hasAVX() ? X86::VCVTSD2SSrr : X86::CVTSD2SSrr; |
| 2373 | return X86SelectFPExtOrFPTrunc(I, Opc, &X86::FR32RegClass); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | return false; |
| 2377 | } |
| 2378 | |
| 2379 | bool X86FastISel::X86SelectTrunc(const Instruction *I) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2380 | EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType()); |
| 2381 | EVT DstVT = TLI.getValueType(DL, I->getType()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2382 | |
| 2383 | // This code only handles truncation to byte. |
| 2384 | if (DstVT != MVT::i8 && DstVT != MVT::i1) |
| 2385 | return false; |
| 2386 | if (!TLI.isTypeLegal(SrcVT)) |
| 2387 | return false; |
| 2388 | |
| 2389 | unsigned InputReg = getRegForValue(I->getOperand(0)); |
| 2390 | if (!InputReg) |
| 2391 | // Unhandled operand. Halt "fast" selection and bail. |
| 2392 | return false; |
| 2393 | |
| 2394 | if (SrcVT == MVT::i8) { |
| 2395 | // Truncate from i8 to i1; no code needed. |
| 2396 | updateValueMap(I, InputReg); |
| 2397 | return true; |
| 2398 | } |
| 2399 | |
Pete Cooper | 7f7c9f1 | 2015-05-08 18:29:42 +0000 | [diff] [blame] | 2400 | bool KillInputReg = false; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2401 | if (!Subtarget->is64Bit()) { |
| 2402 | // If we're on x86-32; we can't extract an i8 from a general register. |
| 2403 | // First issue a copy to GR16_ABCD or GR32_ABCD. |
| 2404 | const TargetRegisterClass *CopyRC = |
| 2405 | (SrcVT == MVT::i16) ? &X86::GR16_ABCDRegClass : &X86::GR32_ABCDRegClass; |
| 2406 | unsigned CopyReg = createResultReg(CopyRC); |
| 2407 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2408 | TII.get(TargetOpcode::COPY), CopyReg).addReg(InputReg); |
| 2409 | InputReg = CopyReg; |
Pete Cooper | 7f7c9f1 | 2015-05-08 18:29:42 +0000 | [diff] [blame] | 2410 | KillInputReg = true; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | // Issue an extract_subreg. |
| 2414 | unsigned ResultReg = fastEmitInst_extractsubreg(MVT::i8, |
Pete Cooper | 7f7c9f1 | 2015-05-08 18:29:42 +0000 | [diff] [blame] | 2415 | InputReg, KillInputReg, |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2416 | X86::sub_8bit); |
| 2417 | if (!ResultReg) |
| 2418 | return false; |
| 2419 | |
| 2420 | updateValueMap(I, ResultReg); |
| 2421 | return true; |
| 2422 | } |
| 2423 | |
| 2424 | bool X86FastISel::IsMemcpySmall(uint64_t Len) { |
| 2425 | return Len <= (Subtarget->is64Bit() ? 32 : 16); |
| 2426 | } |
| 2427 | |
| 2428 | bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM, |
| 2429 | X86AddressMode SrcAM, uint64_t Len) { |
| 2430 | |
| 2431 | // Make sure we don't bloat code by inlining very large memcpy's. |
| 2432 | if (!IsMemcpySmall(Len)) |
| 2433 | return false; |
| 2434 | |
| 2435 | bool i64Legal = Subtarget->is64Bit(); |
| 2436 | |
| 2437 | // We don't care about alignment here since we just emit integer accesses. |
| 2438 | while (Len) { |
| 2439 | MVT VT; |
| 2440 | if (Len >= 8 && i64Legal) |
| 2441 | VT = MVT::i64; |
| 2442 | else if (Len >= 4) |
| 2443 | VT = MVT::i32; |
| 2444 | else if (Len >= 2) |
| 2445 | VT = MVT::i16; |
| 2446 | else |
| 2447 | VT = MVT::i8; |
| 2448 | |
| 2449 | unsigned Reg; |
| 2450 | bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg); |
| 2451 | RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM); |
| 2452 | assert(RV && "Failed to emit load or store??"); |
| 2453 | |
| 2454 | unsigned Size = VT.getSizeInBits()/8; |
| 2455 | Len -= Size; |
| 2456 | DestAM.Disp += Size; |
| 2457 | SrcAM.Disp += Size; |
| 2458 | } |
| 2459 | |
| 2460 | return true; |
| 2461 | } |
| 2462 | |
| 2463 | bool X86FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) { |
| 2464 | // FIXME: Handle more intrinsics. |
| 2465 | switch (II->getIntrinsicID()) { |
| 2466 | default: return false; |
Andrea Di Biagio | 7035178 | 2015-02-20 19:37:14 +0000 | [diff] [blame] | 2467 | case Intrinsic::convert_from_fp16: |
| 2468 | case Intrinsic::convert_to_fp16: { |
Eric Christopher | 824f42f | 2015-05-12 01:26:05 +0000 | [diff] [blame] | 2469 | if (Subtarget->useSoftFloat() || !Subtarget->hasF16C()) |
Andrea Di Biagio | 7035178 | 2015-02-20 19:37:14 +0000 | [diff] [blame] | 2470 | return false; |
| 2471 | |
| 2472 | const Value *Op = II->getArgOperand(0); |
| 2473 | unsigned InputReg = getRegForValue(Op); |
| 2474 | if (InputReg == 0) |
| 2475 | return false; |
| 2476 | |
| 2477 | // F16C only allows converting from float to half and from half to float. |
| 2478 | bool IsFloatToHalf = II->getIntrinsicID() == Intrinsic::convert_to_fp16; |
| 2479 | if (IsFloatToHalf) { |
| 2480 | if (!Op->getType()->isFloatTy()) |
| 2481 | return false; |
| 2482 | } else { |
| 2483 | if (!II->getType()->isFloatTy()) |
| 2484 | return false; |
| 2485 | } |
| 2486 | |
| 2487 | unsigned ResultReg = 0; |
| 2488 | const TargetRegisterClass *RC = TLI.getRegClassFor(MVT::v8i16); |
| 2489 | if (IsFloatToHalf) { |
| 2490 | // 'InputReg' is implicitly promoted from register class FR32 to |
| 2491 | // register class VR128 by method 'constrainOperandRegClass' which is |
| 2492 | // directly called by 'fastEmitInst_ri'. |
| 2493 | // Instruction VCVTPS2PHrr takes an extra immediate operand which is |
Ahmed Bougacha | 68a8efa | 2016-02-02 01:44:03 +0000 | [diff] [blame] | 2494 | // used to provide rounding control: use MXCSR.RC, encoded as 0b100. |
| 2495 | // It's consistent with the other FP instructions, which are usually |
| 2496 | // controlled by MXCSR. |
| 2497 | InputReg = fastEmitInst_ri(X86::VCVTPS2PHrr, RC, InputReg, false, 4); |
Andrea Di Biagio | 7035178 | 2015-02-20 19:37:14 +0000 | [diff] [blame] | 2498 | |
| 2499 | // Move the lower 32-bits of ResultReg to another register of class GR32. |
| 2500 | ResultReg = createResultReg(&X86::GR32RegClass); |
| 2501 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2502 | TII.get(X86::VMOVPDI2DIrr), ResultReg) |
| 2503 | .addReg(InputReg, RegState::Kill); |
| 2504 | |
| 2505 | // The result value is in the lower 16-bits of ResultReg. |
| 2506 | unsigned RegIdx = X86::sub_16bit; |
| 2507 | ResultReg = fastEmitInst_extractsubreg(MVT::i16, ResultReg, true, RegIdx); |
| 2508 | } else { |
| 2509 | assert(Op->getType()->isIntegerTy(16) && "Expected a 16-bit integer!"); |
| 2510 | // Explicitly sign-extend the input to 32-bit. |
| 2511 | InputReg = fastEmit_r(MVT::i16, MVT::i32, ISD::SIGN_EXTEND, InputReg, |
| 2512 | /*Kill=*/false); |
| 2513 | |
| 2514 | // The following SCALAR_TO_VECTOR will be expanded into a VMOVDI2PDIrr. |
| 2515 | InputReg = fastEmit_r(MVT::i32, MVT::v4i32, ISD::SCALAR_TO_VECTOR, |
| 2516 | InputReg, /*Kill=*/true); |
| 2517 | |
| 2518 | InputReg = fastEmitInst_r(X86::VCVTPH2PSrr, RC, InputReg, /*Kill=*/true); |
| 2519 | |
| 2520 | // The result value is in the lower 32-bits of ResultReg. |
| 2521 | // Emit an explicit copy from register class VR128 to register class FR32. |
| 2522 | ResultReg = createResultReg(&X86::FR32RegClass); |
| 2523 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2524 | TII.get(TargetOpcode::COPY), ResultReg) |
| 2525 | .addReg(InputReg, RegState::Kill); |
| 2526 | } |
| 2527 | |
| 2528 | updateValueMap(II, ResultReg); |
| 2529 | return true; |
| 2530 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2531 | case Intrinsic::frameaddress: { |
David Majnemer | ca19485 | 2015-02-10 22:00:34 +0000 | [diff] [blame] | 2532 | MachineFunction *MF = FuncInfo.MF; |
| 2533 | if (MF->getTarget().getMCAsmInfo()->usesWindowsCFI()) |
| 2534 | return false; |
| 2535 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2536 | Type *RetTy = II->getCalledFunction()->getReturnType(); |
| 2537 | |
| 2538 | MVT VT; |
| 2539 | if (!isTypeLegal(RetTy, VT)) |
| 2540 | return false; |
| 2541 | |
| 2542 | unsigned Opc; |
| 2543 | const TargetRegisterClass *RC = nullptr; |
| 2544 | |
| 2545 | switch (VT.SimpleTy) { |
| 2546 | default: llvm_unreachable("Invalid result type for frameaddress."); |
| 2547 | case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break; |
| 2548 | case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break; |
| 2549 | } |
| 2550 | |
| 2551 | // This needs to be set before we call getPtrSizedFrameRegister, otherwise |
| 2552 | // we get the wrong frame register. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 2553 | MachineFrameInfo &MFI = MF->getFrameInfo(); |
| 2554 | MFI.setFrameAddressIsTaken(true); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2555 | |
Eric Christopher | a1c535b | 2015-02-02 23:03:45 +0000 | [diff] [blame] | 2556 | const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo(); |
David Majnemer | ca19485 | 2015-02-10 22:00:34 +0000 | [diff] [blame] | 2557 | unsigned FrameReg = RegInfo->getPtrSizedFrameRegister(*MF); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2558 | assert(((FrameReg == X86::RBP && VT == MVT::i64) || |
| 2559 | (FrameReg == X86::EBP && VT == MVT::i32)) && |
| 2560 | "Invalid Frame Register!"); |
| 2561 | |
| 2562 | // Always make a copy of the frame register to to a vreg first, so that we |
| 2563 | // never directly reference the frame register (the TwoAddressInstruction- |
| 2564 | // Pass doesn't like that). |
| 2565 | unsigned SrcReg = createResultReg(RC); |
| 2566 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2567 | TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg); |
| 2568 | |
| 2569 | // Now recursively load from the frame address. |
| 2570 | // movq (%rbp), %rax |
| 2571 | // movq (%rax), %rax |
| 2572 | // movq (%rax), %rax |
| 2573 | // ... |
| 2574 | unsigned DestReg; |
| 2575 | unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue(); |
| 2576 | while (Depth--) { |
| 2577 | DestReg = createResultReg(RC); |
| 2578 | addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2579 | TII.get(Opc), DestReg), SrcReg); |
| 2580 | SrcReg = DestReg; |
| 2581 | } |
| 2582 | |
| 2583 | updateValueMap(II, SrcReg); |
| 2584 | return true; |
| 2585 | } |
| 2586 | case Intrinsic::memcpy: { |
| 2587 | const MemCpyInst *MCI = cast<MemCpyInst>(II); |
| 2588 | // Don't handle volatile or variable length memcpys. |
| 2589 | if (MCI->isVolatile()) |
| 2590 | return false; |
| 2591 | |
| 2592 | if (isa<ConstantInt>(MCI->getLength())) { |
| 2593 | // Small memcpy's are common enough that we want to do them |
| 2594 | // without a call if possible. |
| 2595 | uint64_t Len = cast<ConstantInt>(MCI->getLength())->getZExtValue(); |
| 2596 | if (IsMemcpySmall(Len)) { |
| 2597 | X86AddressMode DestAM, SrcAM; |
| 2598 | if (!X86SelectAddress(MCI->getRawDest(), DestAM) || |
| 2599 | !X86SelectAddress(MCI->getRawSource(), SrcAM)) |
| 2600 | return false; |
| 2601 | TryEmitSmallMemcpy(DestAM, SrcAM, Len); |
| 2602 | return true; |
| 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32; |
| 2607 | if (!MCI->getLength()->getType()->isIntegerTy(SizeWidth)) |
| 2608 | return false; |
| 2609 | |
| 2610 | if (MCI->getSourceAddressSpace() > 255 || MCI->getDestAddressSpace() > 255) |
| 2611 | return false; |
| 2612 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2613 | return lowerCallTo(II, "memcpy", II->getNumArgOperands() - 2); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2614 | } |
| 2615 | case Intrinsic::memset: { |
| 2616 | const MemSetInst *MSI = cast<MemSetInst>(II); |
| 2617 | |
| 2618 | if (MSI->isVolatile()) |
| 2619 | return false; |
| 2620 | |
| 2621 | unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32; |
| 2622 | if (!MSI->getLength()->getType()->isIntegerTy(SizeWidth)) |
| 2623 | return false; |
| 2624 | |
| 2625 | if (MSI->getDestAddressSpace() > 255) |
| 2626 | return false; |
| 2627 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2628 | return lowerCallTo(II, "memset", II->getNumArgOperands() - 2); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2629 | } |
| 2630 | case Intrinsic::stackprotector: { |
| 2631 | // Emit code to store the stack guard onto the stack. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2632 | EVT PtrTy = TLI.getPointerTy(DL); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2633 | |
| 2634 | const Value *Op1 = II->getArgOperand(0); // The guard's value. |
| 2635 | const AllocaInst *Slot = cast<AllocaInst>(II->getArgOperand(1)); |
| 2636 | |
| 2637 | MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]); |
| 2638 | |
| 2639 | // Grab the frame index. |
| 2640 | X86AddressMode AM; |
| 2641 | if (!X86SelectAddress(Slot, AM)) return false; |
| 2642 | if (!X86FastEmitStore(PtrTy, Op1, AM)) return false; |
| 2643 | return true; |
| 2644 | } |
| 2645 | case Intrinsic::dbg_declare: { |
| 2646 | const DbgDeclareInst *DI = cast<DbgDeclareInst>(II); |
| 2647 | X86AddressMode AM; |
| 2648 | assert(DI->getAddress() && "Null address should be checked earlier!"); |
| 2649 | if (!X86SelectAddress(DI->getAddress(), AM)) |
| 2650 | return false; |
| 2651 | const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE); |
| 2652 | // FIXME may need to add RegState::Debug to any registers produced, |
| 2653 | // although ESP/EBP should be the only ones at the moment. |
Duncan P. N. Exon Smith | 3bef6a3 | 2015-04-03 19:20:26 +0000 | [diff] [blame] | 2654 | assert(DI->getVariable()->isValidLocationForIntrinsic(DbgLoc) && |
| 2655 | "Expected inlined-at fields to agree"); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2656 | addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM) |
| 2657 | .addImm(0) |
| 2658 | .addMetadata(DI->getVariable()) |
| 2659 | .addMetadata(DI->getExpression()); |
| 2660 | return true; |
| 2661 | } |
| 2662 | case Intrinsic::trap: { |
| 2663 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP)); |
| 2664 | return true; |
| 2665 | } |
| 2666 | case Intrinsic::sqrt: { |
| 2667 | if (!Subtarget->hasSSE1()) |
| 2668 | return false; |
| 2669 | |
| 2670 | Type *RetTy = II->getCalledFunction()->getReturnType(); |
| 2671 | |
| 2672 | MVT VT; |
| 2673 | if (!isTypeLegal(RetTy, VT)) |
| 2674 | return false; |
| 2675 | |
| 2676 | // Unfortunately we can't use fastEmit_r, because the AVX version of FSQRT |
| 2677 | // is not generated by FastISel yet. |
| 2678 | // FIXME: Update this code once tablegen can handle it. |
Craig Topper | cf65c62 | 2016-03-02 04:42:31 +0000 | [diff] [blame] | 2679 | static const uint16_t SqrtOpc[2][2] = { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2680 | {X86::SQRTSSr, X86::VSQRTSSr}, |
| 2681 | {X86::SQRTSDr, X86::VSQRTSDr} |
| 2682 | }; |
| 2683 | bool HasAVX = Subtarget->hasAVX(); |
| 2684 | unsigned Opc; |
| 2685 | const TargetRegisterClass *RC; |
| 2686 | switch (VT.SimpleTy) { |
| 2687 | default: return false; |
| 2688 | case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break; |
| 2689 | case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break; |
| 2690 | } |
| 2691 | |
| 2692 | const Value *SrcVal = II->getArgOperand(0); |
| 2693 | unsigned SrcReg = getRegForValue(SrcVal); |
| 2694 | |
| 2695 | if (SrcReg == 0) |
| 2696 | return false; |
| 2697 | |
| 2698 | unsigned ImplicitDefReg = 0; |
| 2699 | if (HasAVX) { |
| 2700 | ImplicitDefReg = createResultReg(RC); |
| 2701 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2702 | TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg); |
| 2703 | } |
| 2704 | |
| 2705 | unsigned ResultReg = createResultReg(RC); |
| 2706 | MachineInstrBuilder MIB; |
| 2707 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), |
| 2708 | ResultReg); |
| 2709 | |
| 2710 | if (ImplicitDefReg) |
| 2711 | MIB.addReg(ImplicitDefReg); |
| 2712 | |
| 2713 | MIB.addReg(SrcReg); |
| 2714 | |
| 2715 | updateValueMap(II, ResultReg); |
| 2716 | return true; |
| 2717 | } |
| 2718 | case Intrinsic::sadd_with_overflow: |
| 2719 | case Intrinsic::uadd_with_overflow: |
| 2720 | case Intrinsic::ssub_with_overflow: |
| 2721 | case Intrinsic::usub_with_overflow: |
| 2722 | case Intrinsic::smul_with_overflow: |
| 2723 | case Intrinsic::umul_with_overflow: { |
| 2724 | // This implements the basic lowering of the xalu with overflow intrinsics |
| 2725 | // into add/sub/mul followed by either seto or setb. |
| 2726 | const Function *Callee = II->getCalledFunction(); |
| 2727 | auto *Ty = cast<StructType>(Callee->getReturnType()); |
| 2728 | Type *RetTy = Ty->getTypeAtIndex(0U); |
| 2729 | Type *CondTy = Ty->getTypeAtIndex(1); |
| 2730 | |
| 2731 | MVT VT; |
| 2732 | if (!isTypeLegal(RetTy, VT)) |
| 2733 | return false; |
| 2734 | |
| 2735 | if (VT < MVT::i8 || VT > MVT::i64) |
| 2736 | return false; |
| 2737 | |
| 2738 | const Value *LHS = II->getArgOperand(0); |
| 2739 | const Value *RHS = II->getArgOperand(1); |
| 2740 | |
| 2741 | // Canonicalize immediate to the RHS. |
| 2742 | if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) && |
| 2743 | isCommutativeIntrinsic(II)) |
| 2744 | std::swap(LHS, RHS); |
| 2745 | |
| 2746 | bool UseIncDec = false; |
| 2747 | if (isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isOne()) |
| 2748 | UseIncDec = true; |
| 2749 | |
| 2750 | unsigned BaseOpc, CondOpc; |
| 2751 | switch (II->getIntrinsicID()) { |
| 2752 | default: llvm_unreachable("Unexpected intrinsic!"); |
| 2753 | case Intrinsic::sadd_with_overflow: |
| 2754 | BaseOpc = UseIncDec ? unsigned(X86ISD::INC) : unsigned(ISD::ADD); |
| 2755 | CondOpc = X86::SETOr; |
| 2756 | break; |
| 2757 | case Intrinsic::uadd_with_overflow: |
| 2758 | BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break; |
| 2759 | case Intrinsic::ssub_with_overflow: |
| 2760 | BaseOpc = UseIncDec ? unsigned(X86ISD::DEC) : unsigned(ISD::SUB); |
| 2761 | CondOpc = X86::SETOr; |
| 2762 | break; |
| 2763 | case Intrinsic::usub_with_overflow: |
| 2764 | BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break; |
| 2765 | case Intrinsic::smul_with_overflow: |
| 2766 | BaseOpc = X86ISD::SMUL; CondOpc = X86::SETOr; break; |
| 2767 | case Intrinsic::umul_with_overflow: |
| 2768 | BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break; |
| 2769 | } |
| 2770 | |
| 2771 | unsigned LHSReg = getRegForValue(LHS); |
| 2772 | if (LHSReg == 0) |
| 2773 | return false; |
| 2774 | bool LHSIsKill = hasTrivialKill(LHS); |
| 2775 | |
| 2776 | unsigned ResultReg = 0; |
| 2777 | // Check if we have an immediate version. |
| 2778 | if (const auto *CI = dyn_cast<ConstantInt>(RHS)) { |
Craig Topper | 6611188 | 2016-06-02 04:19:42 +0000 | [diff] [blame] | 2779 | static const uint16_t Opc[2][4] = { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2780 | { X86::INC8r, X86::INC16r, X86::INC32r, X86::INC64r }, |
| 2781 | { X86::DEC8r, X86::DEC16r, X86::DEC32r, X86::DEC64r } |
| 2782 | }; |
| 2783 | |
| 2784 | if (BaseOpc == X86ISD::INC || BaseOpc == X86ISD::DEC) { |
| 2785 | ResultReg = createResultReg(TLI.getRegClassFor(VT)); |
| 2786 | bool IsDec = BaseOpc == X86ISD::DEC; |
| 2787 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2788 | TII.get(Opc[IsDec][VT.SimpleTy-MVT::i8]), ResultReg) |
| 2789 | .addReg(LHSReg, getKillRegState(LHSIsKill)); |
| 2790 | } else |
| 2791 | ResultReg = fastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill, |
| 2792 | CI->getZExtValue()); |
| 2793 | } |
| 2794 | |
| 2795 | unsigned RHSReg; |
| 2796 | bool RHSIsKill; |
| 2797 | if (!ResultReg) { |
| 2798 | RHSReg = getRegForValue(RHS); |
| 2799 | if (RHSReg == 0) |
| 2800 | return false; |
| 2801 | RHSIsKill = hasTrivialKill(RHS); |
| 2802 | ResultReg = fastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg, |
| 2803 | RHSIsKill); |
| 2804 | } |
| 2805 | |
| 2806 | // FastISel doesn't have a pattern for all X86::MUL*r and X86::IMUL*r. Emit |
| 2807 | // it manually. |
| 2808 | if (BaseOpc == X86ISD::UMUL && !ResultReg) { |
Craig Topper | cf65c62 | 2016-03-02 04:42:31 +0000 | [diff] [blame] | 2809 | static const uint16_t MULOpc[] = |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2810 | { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r }; |
Craig Topper | cf65c62 | 2016-03-02 04:42:31 +0000 | [diff] [blame] | 2811 | static const MCPhysReg Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX }; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2812 | // First copy the first operand into RAX, which is an implicit input to |
| 2813 | // the X86::MUL*r instruction. |
| 2814 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2815 | TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8]) |
| 2816 | .addReg(LHSReg, getKillRegState(LHSIsKill)); |
| 2817 | ResultReg = fastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8], |
| 2818 | TLI.getRegClassFor(VT), RHSReg, RHSIsKill); |
| 2819 | } else if (BaseOpc == X86ISD::SMUL && !ResultReg) { |
Craig Topper | cf65c62 | 2016-03-02 04:42:31 +0000 | [diff] [blame] | 2820 | static const uint16_t MULOpc[] = |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2821 | { X86::IMUL8r, X86::IMUL16rr, X86::IMUL32rr, X86::IMUL64rr }; |
| 2822 | if (VT == MVT::i8) { |
| 2823 | // Copy the first operand into AL, which is an implicit input to the |
| 2824 | // X86::IMUL8r instruction. |
| 2825 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2826 | TII.get(TargetOpcode::COPY), X86::AL) |
| 2827 | .addReg(LHSReg, getKillRegState(LHSIsKill)); |
| 2828 | ResultReg = fastEmitInst_r(MULOpc[0], TLI.getRegClassFor(VT), RHSReg, |
| 2829 | RHSIsKill); |
| 2830 | } else |
| 2831 | ResultReg = fastEmitInst_rr(MULOpc[VT.SimpleTy-MVT::i8], |
| 2832 | TLI.getRegClassFor(VT), LHSReg, LHSIsKill, |
| 2833 | RHSReg, RHSIsKill); |
| 2834 | } |
| 2835 | |
| 2836 | if (!ResultReg) |
| 2837 | return false; |
| 2838 | |
| 2839 | unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy); |
| 2840 | assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers."); |
| 2841 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc), |
| 2842 | ResultReg2); |
| 2843 | |
| 2844 | updateValueMap(II, ResultReg, 2); |
| 2845 | return true; |
| 2846 | } |
| 2847 | case Intrinsic::x86_sse_cvttss2si: |
| 2848 | case Intrinsic::x86_sse_cvttss2si64: |
| 2849 | case Intrinsic::x86_sse2_cvttsd2si: |
| 2850 | case Intrinsic::x86_sse2_cvttsd2si64: { |
| 2851 | bool IsInputDouble; |
| 2852 | switch (II->getIntrinsicID()) { |
| 2853 | default: llvm_unreachable("Unexpected intrinsic."); |
| 2854 | case Intrinsic::x86_sse_cvttss2si: |
| 2855 | case Intrinsic::x86_sse_cvttss2si64: |
| 2856 | if (!Subtarget->hasSSE1()) |
| 2857 | return false; |
| 2858 | IsInputDouble = false; |
| 2859 | break; |
| 2860 | case Intrinsic::x86_sse2_cvttsd2si: |
| 2861 | case Intrinsic::x86_sse2_cvttsd2si64: |
| 2862 | if (!Subtarget->hasSSE2()) |
| 2863 | return false; |
| 2864 | IsInputDouble = true; |
| 2865 | break; |
| 2866 | } |
| 2867 | |
| 2868 | Type *RetTy = II->getCalledFunction()->getReturnType(); |
| 2869 | MVT VT; |
| 2870 | if (!isTypeLegal(RetTy, VT)) |
| 2871 | return false; |
| 2872 | |
Craig Topper | 6611188 | 2016-06-02 04:19:42 +0000 | [diff] [blame] | 2873 | static const uint16_t CvtOpc[2][2][2] = { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2874 | { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr }, |
| 2875 | { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } }, |
| 2876 | { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr }, |
| 2877 | { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } } |
| 2878 | }; |
| 2879 | bool HasAVX = Subtarget->hasAVX(); |
| 2880 | unsigned Opc; |
| 2881 | switch (VT.SimpleTy) { |
| 2882 | default: llvm_unreachable("Unexpected result type."); |
| 2883 | case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break; |
| 2884 | case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break; |
| 2885 | } |
| 2886 | |
| 2887 | // Check if we can fold insertelement instructions into the convert. |
| 2888 | const Value *Op = II->getArgOperand(0); |
| 2889 | while (auto *IE = dyn_cast<InsertElementInst>(Op)) { |
| 2890 | const Value *Index = IE->getOperand(2); |
| 2891 | if (!isa<ConstantInt>(Index)) |
| 2892 | break; |
| 2893 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); |
| 2894 | |
| 2895 | if (Idx == 0) { |
| 2896 | Op = IE->getOperand(1); |
| 2897 | break; |
| 2898 | } |
| 2899 | Op = IE->getOperand(0); |
| 2900 | } |
| 2901 | |
| 2902 | unsigned Reg = getRegForValue(Op); |
| 2903 | if (Reg == 0) |
| 2904 | return false; |
| 2905 | |
| 2906 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); |
| 2907 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) |
| 2908 | .addReg(Reg); |
| 2909 | |
| 2910 | updateValueMap(II, ResultReg); |
| 2911 | return true; |
| 2912 | } |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | bool X86FastISel::fastLowerArguments() { |
| 2917 | if (!FuncInfo.CanLowerReturn) |
| 2918 | return false; |
| 2919 | |
| 2920 | const Function *F = FuncInfo.Fn; |
| 2921 | if (F->isVarArg()) |
| 2922 | return false; |
| 2923 | |
| 2924 | CallingConv::ID CC = F->getCallingConv(); |
| 2925 | if (CC != CallingConv::C) |
| 2926 | return false; |
| 2927 | |
| 2928 | if (Subtarget->isCallingConvWin64(CC)) |
| 2929 | return false; |
| 2930 | |
| 2931 | if (!Subtarget->is64Bit()) |
| 2932 | return false; |
| 2933 | |
| 2934 | // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments. |
| 2935 | unsigned GPRCnt = 0; |
| 2936 | unsigned FPRCnt = 0; |
| 2937 | unsigned Idx = 0; |
| 2938 | for (auto const &Arg : F->args()) { |
| 2939 | // The first argument is at index 1. |
| 2940 | ++Idx; |
| 2941 | if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) || |
| 2942 | F->getAttributes().hasAttribute(Idx, Attribute::InReg) || |
| 2943 | F->getAttributes().hasAttribute(Idx, Attribute::StructRet) || |
Manman Ren | f46262e | 2016-03-29 17:37:21 +0000 | [diff] [blame] | 2944 | F->getAttributes().hasAttribute(Idx, Attribute::SwiftSelf) || |
Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 2945 | F->getAttributes().hasAttribute(Idx, Attribute::SwiftError) || |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2946 | F->getAttributes().hasAttribute(Idx, Attribute::Nest)) |
| 2947 | return false; |
| 2948 | |
| 2949 | Type *ArgTy = Arg.getType(); |
| 2950 | if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) |
| 2951 | return false; |
| 2952 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2953 | EVT ArgVT = TLI.getValueType(DL, ArgTy); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2954 | if (!ArgVT.isSimple()) return false; |
| 2955 | switch (ArgVT.getSimpleVT().SimpleTy) { |
| 2956 | default: return false; |
| 2957 | case MVT::i32: |
| 2958 | case MVT::i64: |
| 2959 | ++GPRCnt; |
| 2960 | break; |
| 2961 | case MVT::f32: |
| 2962 | case MVT::f64: |
| 2963 | if (!Subtarget->hasSSE1()) |
| 2964 | return false; |
| 2965 | ++FPRCnt; |
| 2966 | break; |
| 2967 | } |
| 2968 | |
| 2969 | if (GPRCnt > 6) |
| 2970 | return false; |
| 2971 | |
| 2972 | if (FPRCnt > 8) |
| 2973 | return false; |
| 2974 | } |
| 2975 | |
| 2976 | static const MCPhysReg GPR32ArgRegs[] = { |
| 2977 | X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D |
| 2978 | }; |
| 2979 | static const MCPhysReg GPR64ArgRegs[] = { |
| 2980 | X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9 |
| 2981 | }; |
| 2982 | static const MCPhysReg XMMArgRegs[] = { |
| 2983 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 2984 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 2985 | }; |
| 2986 | |
| 2987 | unsigned GPRIdx = 0; |
| 2988 | unsigned FPRIdx = 0; |
| 2989 | for (auto const &Arg : F->args()) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2990 | MVT VT = TLI.getSimpleValueType(DL, Arg.getType()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2991 | const TargetRegisterClass *RC = TLI.getRegClassFor(VT); |
| 2992 | unsigned SrcReg; |
| 2993 | switch (VT.SimpleTy) { |
| 2994 | default: llvm_unreachable("Unexpected value type."); |
| 2995 | case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break; |
| 2996 | case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break; |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 2997 | case MVT::f32: LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2998 | case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break; |
| 2999 | } |
| 3000 | unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC); |
| 3001 | // FIXME: Unfortunately it's necessary to emit a copy from the livein copy. |
| 3002 | // Without this, EmitLiveInCopies may eliminate the livein if its only |
| 3003 | // use is a bitcast (which isn't turned into an instruction). |
| 3004 | unsigned ResultReg = createResultReg(RC); |
| 3005 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3006 | TII.get(TargetOpcode::COPY), ResultReg) |
| 3007 | .addReg(DstReg, getKillRegState(true)); |
| 3008 | updateValueMap(&Arg, ResultReg); |
| 3009 | } |
| 3010 | return true; |
| 3011 | } |
| 3012 | |
Nico Weber | af7e846 | 2016-07-14 01:52:51 +0000 | [diff] [blame] | 3013 | static unsigned computeBytesPoppedByCalleeForSRet(const X86Subtarget *Subtarget, |
| 3014 | CallingConv::ID CC, |
| 3015 | ImmutableCallSite *CS) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3016 | if (Subtarget->is64Bit()) |
| 3017 | return 0; |
| 3018 | if (Subtarget->getTargetTriple().isOSMSVCRT()) |
| 3019 | return 0; |
| 3020 | if (CC == CallingConv::Fast || CC == CallingConv::GHC || |
| 3021 | CC == CallingConv::HiPE) |
| 3022 | return 0; |
Sanjoy Das | b11b440 | 2015-11-04 20:33:45 +0000 | [diff] [blame] | 3023 | |
| 3024 | if (CS) |
| 3025 | if (CS->arg_empty() || !CS->paramHasAttr(1, Attribute::StructRet) || |
Michael Kuperstein | 2ea81ba | 2015-12-28 14:39:21 +0000 | [diff] [blame] | 3026 | CS->paramHasAttr(1, Attribute::InReg) || Subtarget->isTargetMCU()) |
Sanjoy Das | b11b440 | 2015-11-04 20:33:45 +0000 | [diff] [blame] | 3027 | return 0; |
| 3028 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3029 | return 4; |
| 3030 | } |
| 3031 | |
| 3032 | bool X86FastISel::fastLowerCall(CallLoweringInfo &CLI) { |
| 3033 | auto &OutVals = CLI.OutVals; |
| 3034 | auto &OutFlags = CLI.OutFlags; |
| 3035 | auto &OutRegs = CLI.OutRegs; |
| 3036 | auto &Ins = CLI.Ins; |
| 3037 | auto &InRegs = CLI.InRegs; |
| 3038 | CallingConv::ID CC = CLI.CallConv; |
| 3039 | bool &IsTailCall = CLI.IsTailCall; |
| 3040 | bool IsVarArg = CLI.IsVarArg; |
| 3041 | const Value *Callee = CLI.Callee; |
Rafael Espindola | ce4c2bc | 2015-06-23 12:21:54 +0000 | [diff] [blame] | 3042 | MCSymbol *Symbol = CLI.Symbol; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3043 | |
| 3044 | bool Is64Bit = Subtarget->is64Bit(); |
| 3045 | bool IsWin64 = Subtarget->isCallingConvWin64(CC); |
| 3046 | |
| 3047 | // Handle only C, fastcc, and webkit_js calling conventions for now. |
| 3048 | switch (CC) { |
| 3049 | default: return false; |
| 3050 | case CallingConv::C: |
| 3051 | case CallingConv::Fast: |
| 3052 | case CallingConv::WebKit_JS: |
Manman Ren | f8bdd88 | 2016-04-05 22:41:47 +0000 | [diff] [blame] | 3053 | case CallingConv::Swift: |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3054 | case CallingConv::X86_FastCall: |
Nico Weber | ecdf45b | 2016-07-14 13:54:26 +0000 | [diff] [blame] | 3055 | case CallingConv::X86_StdCall: |
Nico Weber | af7e846 | 2016-07-14 01:52:51 +0000 | [diff] [blame] | 3056 | case CallingConv::X86_ThisCall: |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3057 | case CallingConv::X86_64_Win64: |
| 3058 | case CallingConv::X86_64_SysV: |
| 3059 | break; |
| 3060 | } |
| 3061 | |
| 3062 | // Allow SelectionDAG isel to handle tail calls. |
| 3063 | if (IsTailCall) |
| 3064 | return false; |
| 3065 | |
| 3066 | // fastcc with -tailcallopt is intended to provide a guaranteed |
| 3067 | // tail call optimization. Fastisel doesn't know how to do that. |
| 3068 | if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt) |
| 3069 | return false; |
| 3070 | |
| 3071 | // Don't know how to handle Win64 varargs yet. Nothing special needed for |
| 3072 | // x86-32. Special handling for x86-64 is implemented. |
| 3073 | if (IsVarArg && IsWin64) |
| 3074 | return false; |
| 3075 | |
| 3076 | // Don't know about inalloca yet. |
| 3077 | if (CLI.CS && CLI.CS->hasInAllocaArgument()) |
| 3078 | return false; |
| 3079 | |
Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 3080 | for (auto Flag : CLI.OutFlags) |
| 3081 | if (Flag.isSwiftError()) |
| 3082 | return false; |
| 3083 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3084 | SmallVector<MVT, 16> OutVTs; |
| 3085 | SmallVector<unsigned, 16> ArgRegs; |
| 3086 | |
| 3087 | // If this is a constant i1/i8/i16 argument, promote to i32 to avoid an extra |
| 3088 | // instruction. This is safe because it is common to all FastISel supported |
| 3089 | // calling conventions on x86. |
| 3090 | for (int i = 0, e = OutVals.size(); i != e; ++i) { |
| 3091 | Value *&Val = OutVals[i]; |
| 3092 | ISD::ArgFlagsTy Flags = OutFlags[i]; |
| 3093 | if (auto *CI = dyn_cast<ConstantInt>(Val)) { |
| 3094 | if (CI->getBitWidth() < 32) { |
| 3095 | if (Flags.isSExt()) |
| 3096 | Val = ConstantExpr::getSExt(CI, Type::getInt32Ty(CI->getContext())); |
| 3097 | else |
| 3098 | Val = ConstantExpr::getZExt(CI, Type::getInt32Ty(CI->getContext())); |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | // Passing bools around ends up doing a trunc to i1 and passing it. |
| 3103 | // Codegen this as an argument + "and 1". |
| 3104 | MVT VT; |
| 3105 | auto *TI = dyn_cast<TruncInst>(Val); |
| 3106 | unsigned ResultReg; |
| 3107 | if (TI && TI->getType()->isIntegerTy(1) && CLI.CS && |
| 3108 | (TI->getParent() == CLI.CS->getInstruction()->getParent()) && |
| 3109 | TI->hasOneUse()) { |
| 3110 | Value *PrevVal = TI->getOperand(0); |
| 3111 | ResultReg = getRegForValue(PrevVal); |
| 3112 | |
| 3113 | if (!ResultReg) |
| 3114 | return false; |
| 3115 | |
| 3116 | if (!isTypeLegal(PrevVal->getType(), VT)) |
| 3117 | return false; |
| 3118 | |
| 3119 | ResultReg = |
| 3120 | fastEmit_ri(VT, VT, ISD::AND, ResultReg, hasTrivialKill(PrevVal), 1); |
| 3121 | } else { |
| 3122 | if (!isTypeLegal(Val->getType(), VT)) |
| 3123 | return false; |
| 3124 | ResultReg = getRegForValue(Val); |
| 3125 | } |
| 3126 | |
| 3127 | if (!ResultReg) |
| 3128 | return false; |
| 3129 | |
| 3130 | ArgRegs.push_back(ResultReg); |
| 3131 | OutVTs.push_back(VT); |
| 3132 | } |
| 3133 | |
| 3134 | // Analyze operands of the call, assigning locations to each operand. |
| 3135 | SmallVector<CCValAssign, 16> ArgLocs; |
| 3136 | CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, CLI.RetTy->getContext()); |
| 3137 | |
| 3138 | // Allocate shadow area for Win64 |
| 3139 | if (IsWin64) |
| 3140 | CCInfo.AllocateStack(32, 8); |
| 3141 | |
| 3142 | CCInfo.AnalyzeCallOperands(OutVTs, OutFlags, CC_X86); |
| 3143 | |
| 3144 | // Get a count of how many bytes are to be pushed on the stack. |
Jeroen Ketema | 740f9d7 | 2015-09-29 10:12:57 +0000 | [diff] [blame] | 3145 | unsigned NumBytes = CCInfo.getAlignedCallFrameSize(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3146 | |
| 3147 | // Issue CALLSEQ_START |
| 3148 | unsigned AdjStackDown = TII.getCallFrameSetupOpcode(); |
| 3149 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown)) |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 3150 | .addImm(NumBytes).addImm(0); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3151 | |
| 3152 | // Walk the register/memloc assignments, inserting copies/loads. |
Eric Christopher | a1c535b | 2015-02-02 23:03:45 +0000 | [diff] [blame] | 3153 | const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3154 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 3155 | CCValAssign const &VA = ArgLocs[i]; |
| 3156 | const Value *ArgVal = OutVals[VA.getValNo()]; |
| 3157 | MVT ArgVT = OutVTs[VA.getValNo()]; |
| 3158 | |
| 3159 | if (ArgVT == MVT::x86mmx) |
| 3160 | return false; |
| 3161 | |
| 3162 | unsigned ArgReg = ArgRegs[VA.getValNo()]; |
| 3163 | |
| 3164 | // Promote the value if needed. |
| 3165 | switch (VA.getLocInfo()) { |
| 3166 | case CCValAssign::Full: break; |
| 3167 | case CCValAssign::SExt: { |
| 3168 | assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() && |
| 3169 | "Unexpected extend"); |
David Majnemer | 2c5aeab | 2016-05-04 00:22:23 +0000 | [diff] [blame] | 3170 | |
| 3171 | if (ArgVT.SimpleTy == MVT::i1) |
| 3172 | return false; |
| 3173 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3174 | bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg, |
| 3175 | ArgVT, ArgReg); |
| 3176 | assert(Emitted && "Failed to emit a sext!"); (void)Emitted; |
| 3177 | ArgVT = VA.getLocVT(); |
| 3178 | break; |
| 3179 | } |
| 3180 | case CCValAssign::ZExt: { |
| 3181 | assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() && |
| 3182 | "Unexpected extend"); |
David Majnemer | 2c5aeab | 2016-05-04 00:22:23 +0000 | [diff] [blame] | 3183 | |
| 3184 | // Handle zero-extension from i1 to i8, which is common. |
| 3185 | if (ArgVT.SimpleTy == MVT::i1) { |
| 3186 | // Set the high bits to zero. |
| 3187 | ArgReg = fastEmitZExtFromI1(MVT::i8, ArgReg, /*TODO: Kill=*/false); |
| 3188 | ArgVT = MVT::i8; |
| 3189 | |
| 3190 | if (ArgReg == 0) |
| 3191 | return false; |
| 3192 | } |
| 3193 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3194 | bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg, |
| 3195 | ArgVT, ArgReg); |
| 3196 | assert(Emitted && "Failed to emit a zext!"); (void)Emitted; |
| 3197 | ArgVT = VA.getLocVT(); |
| 3198 | break; |
| 3199 | } |
| 3200 | case CCValAssign::AExt: { |
| 3201 | assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() && |
| 3202 | "Unexpected extend"); |
| 3203 | bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), ArgReg, |
| 3204 | ArgVT, ArgReg); |
| 3205 | if (!Emitted) |
| 3206 | Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg, |
| 3207 | ArgVT, ArgReg); |
| 3208 | if (!Emitted) |
| 3209 | Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg, |
| 3210 | ArgVT, ArgReg); |
| 3211 | |
| 3212 | assert(Emitted && "Failed to emit a aext!"); (void)Emitted; |
| 3213 | ArgVT = VA.getLocVT(); |
| 3214 | break; |
| 3215 | } |
| 3216 | case CCValAssign::BCvt: { |
| 3217 | ArgReg = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, ArgReg, |
| 3218 | /*TODO: Kill=*/false); |
| 3219 | assert(ArgReg && "Failed to emit a bitcast!"); |
| 3220 | ArgVT = VA.getLocVT(); |
| 3221 | break; |
| 3222 | } |
| 3223 | case CCValAssign::VExt: |
| 3224 | // VExt has not been implemented, so this should be impossible to reach |
| 3225 | // for now. However, fallback to Selection DAG isel once implemented. |
| 3226 | return false; |
| 3227 | case CCValAssign::AExtUpper: |
| 3228 | case CCValAssign::SExtUpper: |
| 3229 | case CCValAssign::ZExtUpper: |
| 3230 | case CCValAssign::FPExt: |
| 3231 | llvm_unreachable("Unexpected loc info!"); |
| 3232 | case CCValAssign::Indirect: |
| 3233 | // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully |
| 3234 | // support this. |
| 3235 | return false; |
| 3236 | } |
| 3237 | |
| 3238 | if (VA.isRegLoc()) { |
| 3239 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3240 | TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg); |
| 3241 | OutRegs.push_back(VA.getLocReg()); |
| 3242 | } else { |
| 3243 | assert(VA.isMemLoc()); |
| 3244 | |
| 3245 | // Don't emit stores for undef values. |
| 3246 | if (isa<UndefValue>(ArgVal)) |
| 3247 | continue; |
| 3248 | |
| 3249 | unsigned LocMemOffset = VA.getLocMemOffset(); |
| 3250 | X86AddressMode AM; |
| 3251 | AM.Base.Reg = RegInfo->getStackRegister(); |
| 3252 | AM.Disp = LocMemOffset; |
| 3253 | ISD::ArgFlagsTy Flags = OutFlags[VA.getValNo()]; |
| 3254 | unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType()); |
| 3255 | MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand( |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 3256 | MachinePointerInfo::getStack(*FuncInfo.MF, LocMemOffset), |
| 3257 | MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3258 | if (Flags.isByVal()) { |
| 3259 | X86AddressMode SrcAM; |
| 3260 | SrcAM.Base.Reg = ArgReg; |
| 3261 | if (!TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize())) |
| 3262 | return false; |
| 3263 | } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) { |
| 3264 | // If this is a really simple value, emit this with the Value* version |
| 3265 | // of X86FastEmitStore. If it isn't simple, we don't want to do this, |
| 3266 | // as it can cause us to reevaluate the argument. |
| 3267 | if (!X86FastEmitStore(ArgVT, ArgVal, AM, MMO)) |
| 3268 | return false; |
| 3269 | } else { |
| 3270 | bool ValIsKill = hasTrivialKill(ArgVal); |
| 3271 | if (!X86FastEmitStore(ArgVT, ArgReg, ValIsKill, AM, MMO)) |
| 3272 | return false; |
| 3273 | } |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
| 3278 | // GOT pointer. |
| 3279 | if (Subtarget->isPICStyleGOT()) { |
| 3280 | unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF); |
| 3281 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3282 | TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base); |
| 3283 | } |
| 3284 | |
| 3285 | if (Is64Bit && IsVarArg && !IsWin64) { |
| 3286 | // From AMD64 ABI document: |
| 3287 | // For calls that may call functions that use varargs or stdargs |
| 3288 | // (prototype-less calls or calls to functions containing ellipsis (...) in |
| 3289 | // the declaration) %al is used as hidden argument to specify the number |
| 3290 | // of SSE registers used. The contents of %al do not need to match exactly |
| 3291 | // the number of registers, but must be an ubound on the number of SSE |
| 3292 | // registers used and is in the range 0 - 8 inclusive. |
| 3293 | |
| 3294 | // Count the number of XMM registers allocated. |
| 3295 | static const MCPhysReg XMMArgRegs[] = { |
| 3296 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 3297 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 3298 | }; |
Tim Northover | 3b6b7ca | 2015-02-21 02:11:17 +0000 | [diff] [blame] | 3299 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3300 | assert((Subtarget->hasSSE1() || !NumXMMRegs) |
| 3301 | && "SSE registers cannot be used when SSE is disabled"); |
| 3302 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri), |
| 3303 | X86::AL).addImm(NumXMMRegs); |
| 3304 | } |
| 3305 | |
| 3306 | // Materialize callee address in a register. FIXME: GV address can be |
| 3307 | // handled with a CALLpcrel32 instead. |
| 3308 | X86AddressMode CalleeAM; |
| 3309 | if (!X86SelectCallAddress(Callee, CalleeAM)) |
| 3310 | return false; |
| 3311 | |
| 3312 | unsigned CalleeOp = 0; |
| 3313 | const GlobalValue *GV = nullptr; |
| 3314 | if (CalleeAM.GV != nullptr) { |
| 3315 | GV = CalleeAM.GV; |
| 3316 | } else if (CalleeAM.Base.Reg != 0) { |
| 3317 | CalleeOp = CalleeAM.Base.Reg; |
| 3318 | } else |
| 3319 | return false; |
| 3320 | |
| 3321 | // Issue the call. |
| 3322 | MachineInstrBuilder MIB; |
| 3323 | if (CalleeOp) { |
| 3324 | // Register-indirect call. |
| 3325 | unsigned CallOpc = Is64Bit ? X86::CALL64r : X86::CALL32r; |
| 3326 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc)) |
| 3327 | .addReg(CalleeOp); |
| 3328 | } else { |
| 3329 | // Direct call. |
| 3330 | assert(GV && "Not a direct call"); |
| 3331 | unsigned CallOpc = Is64Bit ? X86::CALL64pcrel32 : X86::CALLpcrel32; |
| 3332 | |
| 3333 | // See if we need any target-specific flags on the GV operand. |
Rafael Espindola | 46107b9 | 2016-05-19 18:49:29 +0000 | [diff] [blame] | 3334 | unsigned char OpFlags = Subtarget->classifyGlobalFunctionReference(GV); |
Asaf Badouh | 89406d1 | 2016-04-20 08:32:57 +0000 | [diff] [blame] | 3335 | // Ignore NonLazyBind attribute in FastISel |
| 3336 | if (OpFlags == X86II::MO_GOTPCREL) |
| 3337 | OpFlags = 0; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3338 | |
| 3339 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc)); |
Rafael Espindola | ce4c2bc | 2015-06-23 12:21:54 +0000 | [diff] [blame] | 3340 | if (Symbol) |
| 3341 | MIB.addSym(Symbol, OpFlags); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3342 | else |
| 3343 | MIB.addGlobalAddress(GV, 0, OpFlags); |
| 3344 | } |
| 3345 | |
| 3346 | // Add a register mask operand representing the call-preserved registers. |
| 3347 | // Proper defs for return values will be added by setPhysRegsDeadExcept(). |
Eric Christopher | 9deb75d | 2015-03-11 22:42:13 +0000 | [diff] [blame] | 3348 | MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC)); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3349 | |
| 3350 | // Add an implicit use GOT pointer in EBX. |
| 3351 | if (Subtarget->isPICStyleGOT()) |
| 3352 | MIB.addReg(X86::EBX, RegState::Implicit); |
| 3353 | |
| 3354 | if (Is64Bit && IsVarArg && !IsWin64) |
| 3355 | MIB.addReg(X86::AL, RegState::Implicit); |
| 3356 | |
| 3357 | // Add implicit physical register uses to the call. |
| 3358 | for (auto Reg : OutRegs) |
| 3359 | MIB.addReg(Reg, RegState::Implicit); |
| 3360 | |
| 3361 | // Issue CALLSEQ_END |
| 3362 | unsigned NumBytesForCalleeToPop = |
Nico Weber | af7e846 | 2016-07-14 01:52:51 +0000 | [diff] [blame] | 3363 | X86::isCalleePop(CC, Subtarget->is64Bit(), IsVarArg, |
| 3364 | TM.Options.GuaranteedTailCallOpt) |
| 3365 | ? NumBytes // Callee pops everything. |
| 3366 | : computeBytesPoppedByCalleeForSRet(Subtarget, CC, CLI.CS); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3367 | unsigned AdjStackUp = TII.getCallFrameDestroyOpcode(); |
| 3368 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp)) |
| 3369 | .addImm(NumBytes).addImm(NumBytesForCalleeToPop); |
| 3370 | |
| 3371 | // Now handle call return values. |
| 3372 | SmallVector<CCValAssign, 16> RVLocs; |
| 3373 | CCState CCRetInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, |
| 3374 | CLI.RetTy->getContext()); |
| 3375 | CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86); |
| 3376 | |
| 3377 | // Copy all of the result registers out of their specified physreg. |
| 3378 | unsigned ResultReg = FuncInfo.CreateRegs(CLI.RetTy); |
| 3379 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 3380 | CCValAssign &VA = RVLocs[i]; |
| 3381 | EVT CopyVT = VA.getValVT(); |
| 3382 | unsigned CopyReg = ResultReg + i; |
| 3383 | |
| 3384 | // If this is x86-64, and we disabled SSE, we can't return FP values |
| 3385 | if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) && |
| 3386 | ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) { |
| 3387 | report_fatal_error("SSE register return with SSE disabled"); |
| 3388 | } |
| 3389 | |
| 3390 | // If we prefer to use the value in xmm registers, copy it out as f80 and |
| 3391 | // use a truncate to move it from fp stack reg to xmm reg. |
| 3392 | if ((VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1) && |
| 3393 | isScalarFPTypeInSSEReg(VA.getValVT())) { |
| 3394 | CopyVT = MVT::f80; |
| 3395 | CopyReg = createResultReg(&X86::RFP80RegClass); |
| 3396 | } |
| 3397 | |
| 3398 | // Copy out the result. |
| 3399 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3400 | TII.get(TargetOpcode::COPY), CopyReg).addReg(VA.getLocReg()); |
| 3401 | InRegs.push_back(VA.getLocReg()); |
| 3402 | |
| 3403 | // Round the f80 to the right size, which also moves it to the appropriate |
| 3404 | // xmm register. This is accomplished by storing the f80 value in memory |
| 3405 | // and then loading it back. |
| 3406 | if (CopyVT != VA.getValVT()) { |
| 3407 | EVT ResVT = VA.getValVT(); |
| 3408 | unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64; |
| 3409 | unsigned MemSize = ResVT.getSizeInBits()/8; |
| 3410 | int FI = MFI.CreateStackObject(MemSize, MemSize, false); |
| 3411 | addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3412 | TII.get(Opc)), FI) |
| 3413 | .addReg(CopyReg); |
| 3414 | Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm; |
| 3415 | addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3416 | TII.get(Opc), ResultReg + i), FI); |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | CLI.ResultReg = ResultReg; |
| 3421 | CLI.NumResultRegs = RVLocs.size(); |
| 3422 | CLI.Call = MIB; |
| 3423 | |
| 3424 | return true; |
| 3425 | } |
| 3426 | |
| 3427 | bool |
| 3428 | X86FastISel::fastSelectInstruction(const Instruction *I) { |
| 3429 | switch (I->getOpcode()) { |
| 3430 | default: break; |
| 3431 | case Instruction::Load: |
| 3432 | return X86SelectLoad(I); |
| 3433 | case Instruction::Store: |
| 3434 | return X86SelectStore(I); |
| 3435 | case Instruction::Ret: |
| 3436 | return X86SelectRet(I); |
| 3437 | case Instruction::ICmp: |
| 3438 | case Instruction::FCmp: |
| 3439 | return X86SelectCmp(I); |
| 3440 | case Instruction::ZExt: |
| 3441 | return X86SelectZExt(I); |
| 3442 | case Instruction::Br: |
| 3443 | return X86SelectBranch(I); |
| 3444 | case Instruction::LShr: |
| 3445 | case Instruction::AShr: |
| 3446 | case Instruction::Shl: |
| 3447 | return X86SelectShift(I); |
| 3448 | case Instruction::SDiv: |
| 3449 | case Instruction::UDiv: |
| 3450 | case Instruction::SRem: |
| 3451 | case Instruction::URem: |
| 3452 | return X86SelectDivRem(I); |
| 3453 | case Instruction::Select: |
| 3454 | return X86SelectSelect(I); |
| 3455 | case Instruction::Trunc: |
| 3456 | return X86SelectTrunc(I); |
| 3457 | case Instruction::FPExt: |
| 3458 | return X86SelectFPExt(I); |
| 3459 | case Instruction::FPTrunc: |
| 3460 | return X86SelectFPTrunc(I); |
Andrea Di Biagio | e7b58ee | 2015-02-17 23:40:58 +0000 | [diff] [blame] | 3461 | case Instruction::SIToFP: |
| 3462 | return X86SelectSIToFP(I); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3463 | case Instruction::IntToPtr: // Deliberate fall-through. |
| 3464 | case Instruction::PtrToInt: { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 3465 | EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType()); |
| 3466 | EVT DstVT = TLI.getValueType(DL, I->getType()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3467 | if (DstVT.bitsGT(SrcVT)) |
| 3468 | return X86SelectZExt(I); |
| 3469 | if (DstVT.bitsLT(SrcVT)) |
| 3470 | return X86SelectTrunc(I); |
| 3471 | unsigned Reg = getRegForValue(I->getOperand(0)); |
| 3472 | if (Reg == 0) return false; |
| 3473 | updateValueMap(I, Reg); |
| 3474 | return true; |
| 3475 | } |
Andrea Di Biagio | 77f6265 | 2015-10-02 16:08:05 +0000 | [diff] [blame] | 3476 | case Instruction::BitCast: { |
| 3477 | // Select SSE2/AVX bitcasts between 128/256 bit vector types. |
| 3478 | if (!Subtarget->hasSSE2()) |
| 3479 | return false; |
| 3480 | |
| 3481 | EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType()); |
| 3482 | EVT DstVT = TLI.getValueType(DL, I->getType()); |
| 3483 | |
| 3484 | if (!SrcVT.isSimple() || !DstVT.isSimple()) |
| 3485 | return false; |
| 3486 | |
| 3487 | if (!SrcVT.is128BitVector() && |
| 3488 | !(Subtarget->hasAVX() && SrcVT.is256BitVector())) |
| 3489 | return false; |
| 3490 | |
| 3491 | unsigned Reg = getRegForValue(I->getOperand(0)); |
| 3492 | if (Reg == 0) |
| 3493 | return false; |
| 3494 | |
| 3495 | // No instruction is needed for conversion. Reuse the register used by |
| 3496 | // the fist operand. |
| 3497 | updateValueMap(I, Reg); |
| 3498 | return true; |
| 3499 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | return false; |
| 3503 | } |
| 3504 | |
| 3505 | unsigned X86FastISel::X86MaterializeInt(const ConstantInt *CI, MVT VT) { |
| 3506 | if (VT > MVT::i64) |
| 3507 | return 0; |
| 3508 | |
| 3509 | uint64_t Imm = CI->getZExtValue(); |
| 3510 | if (Imm == 0) { |
| 3511 | unsigned SrcReg = fastEmitInst_(X86::MOV32r0, &X86::GR32RegClass); |
| 3512 | switch (VT.SimpleTy) { |
| 3513 | default: llvm_unreachable("Unexpected value type"); |
| 3514 | case MVT::i1: |
| 3515 | case MVT::i8: |
| 3516 | return fastEmitInst_extractsubreg(MVT::i8, SrcReg, /*Kill=*/true, |
| 3517 | X86::sub_8bit); |
| 3518 | case MVT::i16: |
| 3519 | return fastEmitInst_extractsubreg(MVT::i16, SrcReg, /*Kill=*/true, |
| 3520 | X86::sub_16bit); |
| 3521 | case MVT::i32: |
| 3522 | return SrcReg; |
| 3523 | case MVT::i64: { |
| 3524 | unsigned ResultReg = createResultReg(&X86::GR64RegClass); |
| 3525 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3526 | TII.get(TargetOpcode::SUBREG_TO_REG), ResultReg) |
| 3527 | .addImm(0).addReg(SrcReg).addImm(X86::sub_32bit); |
| 3528 | return ResultReg; |
| 3529 | } |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | unsigned Opc = 0; |
| 3534 | switch (VT.SimpleTy) { |
| 3535 | default: llvm_unreachable("Unexpected value type"); |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 3536 | case MVT::i1: VT = MVT::i8; LLVM_FALLTHROUGH; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3537 | case MVT::i8: Opc = X86::MOV8ri; break; |
| 3538 | case MVT::i16: Opc = X86::MOV16ri; break; |
| 3539 | case MVT::i32: Opc = X86::MOV32ri; break; |
| 3540 | case MVT::i64: { |
| 3541 | if (isUInt<32>(Imm)) |
| 3542 | Opc = X86::MOV32ri; |
| 3543 | else if (isInt<32>(Imm)) |
| 3544 | Opc = X86::MOV64ri32; |
| 3545 | else |
| 3546 | Opc = X86::MOV64ri; |
| 3547 | break; |
| 3548 | } |
| 3549 | } |
| 3550 | if (VT == MVT::i64 && Opc == X86::MOV32ri) { |
| 3551 | unsigned SrcReg = fastEmitInst_i(Opc, &X86::GR32RegClass, Imm); |
| 3552 | unsigned ResultReg = createResultReg(&X86::GR64RegClass); |
| 3553 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3554 | TII.get(TargetOpcode::SUBREG_TO_REG), ResultReg) |
| 3555 | .addImm(0).addReg(SrcReg).addImm(X86::sub_32bit); |
| 3556 | return ResultReg; |
| 3557 | } |
| 3558 | return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm); |
| 3559 | } |
| 3560 | |
| 3561 | unsigned X86FastISel::X86MaterializeFP(const ConstantFP *CFP, MVT VT) { |
| 3562 | if (CFP->isNullValue()) |
| 3563 | return fastMaterializeFloatZero(CFP); |
| 3564 | |
| 3565 | // Can't handle alternate code models yet. |
| 3566 | CodeModel::Model CM = TM.getCodeModel(); |
| 3567 | if (CM != CodeModel::Small && CM != CodeModel::Large) |
| 3568 | return 0; |
| 3569 | |
| 3570 | // Get opcode and regclass of the output for the given load instruction. |
| 3571 | unsigned Opc = 0; |
| 3572 | const TargetRegisterClass *RC = nullptr; |
| 3573 | switch (VT.SimpleTy) { |
| 3574 | default: return 0; |
| 3575 | case MVT::f32: |
| 3576 | if (X86ScalarSSEf32) { |
| 3577 | Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm; |
| 3578 | RC = &X86::FR32RegClass; |
| 3579 | } else { |
| 3580 | Opc = X86::LD_Fp32m; |
| 3581 | RC = &X86::RFP32RegClass; |
| 3582 | } |
| 3583 | break; |
| 3584 | case MVT::f64: |
| 3585 | if (X86ScalarSSEf64) { |
| 3586 | Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm; |
| 3587 | RC = &X86::FR64RegClass; |
| 3588 | } else { |
| 3589 | Opc = X86::LD_Fp64m; |
| 3590 | RC = &X86::RFP64RegClass; |
| 3591 | } |
| 3592 | break; |
| 3593 | case MVT::f80: |
| 3594 | // No f80 support yet. |
| 3595 | return 0; |
| 3596 | } |
| 3597 | |
| 3598 | // MachineConstantPool wants an explicit alignment. |
| 3599 | unsigned Align = DL.getPrefTypeAlignment(CFP->getType()); |
| 3600 | if (Align == 0) { |
| 3601 | // Alignment of vector types. FIXME! |
| 3602 | Align = DL.getTypeAllocSize(CFP->getType()); |
| 3603 | } |
| 3604 | |
| 3605 | // x86-32 PIC requires a PIC base register for constant pools. |
| 3606 | unsigned PICBase = 0; |
Rafael Espindola | c7e9813 | 2016-05-20 12:20:10 +0000 | [diff] [blame] | 3607 | unsigned char OpFlag = Subtarget->classifyLocalReference(nullptr); |
| 3608 | if (OpFlag == X86II::MO_PIC_BASE_OFFSET) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3609 | PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF); |
Rafael Espindola | c7e9813 | 2016-05-20 12:20:10 +0000 | [diff] [blame] | 3610 | else if (OpFlag == X86II::MO_GOTOFF) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3611 | PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF); |
Rafael Espindola | c7e9813 | 2016-05-20 12:20:10 +0000 | [diff] [blame] | 3612 | else if (Subtarget->is64Bit() && TM.getCodeModel() == CodeModel::Small) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3613 | PICBase = X86::RIP; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3614 | |
| 3615 | // Create the load from the constant pool. |
| 3616 | unsigned CPI = MCP.getConstantPoolIndex(CFP, Align); |
| 3617 | unsigned ResultReg = createResultReg(RC); |
| 3618 | |
| 3619 | if (CM == CodeModel::Large) { |
| 3620 | unsigned AddrReg = createResultReg(&X86::GR64RegClass); |
| 3621 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri), |
| 3622 | AddrReg) |
| 3623 | .addConstantPoolIndex(CPI, 0, OpFlag); |
| 3624 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3625 | TII.get(Opc), ResultReg); |
| 3626 | addDirectMem(MIB, AddrReg); |
| 3627 | MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand( |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 3628 | MachinePointerInfo::getConstantPool(*FuncInfo.MF), |
| 3629 | MachineMemOperand::MOLoad, DL.getPointerSize(), Align); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3630 | MIB->addMemOperand(*FuncInfo.MF, MMO); |
| 3631 | return ResultReg; |
| 3632 | } |
| 3633 | |
| 3634 | addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3635 | TII.get(Opc), ResultReg), |
| 3636 | CPI, PICBase, OpFlag); |
| 3637 | return ResultReg; |
| 3638 | } |
| 3639 | |
| 3640 | unsigned X86FastISel::X86MaterializeGV(const GlobalValue *GV, MVT VT) { |
| 3641 | // Can't handle alternate code models yet. |
| 3642 | if (TM.getCodeModel() != CodeModel::Small) |
| 3643 | return 0; |
| 3644 | |
| 3645 | // Materialize addresses with LEA/MOV instructions. |
| 3646 | X86AddressMode AM; |
| 3647 | if (X86SelectAddress(GV, AM)) { |
| 3648 | // If the expression is just a basereg, then we're done, otherwise we need |
| 3649 | // to emit an LEA. |
| 3650 | if (AM.BaseType == X86AddressMode::RegBase && |
| 3651 | AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr) |
| 3652 | return AM.Base.Reg; |
| 3653 | |
| 3654 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); |
| 3655 | if (TM.getRelocationModel() == Reloc::Static && |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 3656 | TLI.getPointerTy(DL) == MVT::i64) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3657 | // The displacement code could be more than 32 bits away so we need to use |
| 3658 | // an instruction with a 64 bit immediate |
| 3659 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri), |
| 3660 | ResultReg) |
| 3661 | .addGlobalAddress(GV); |
| 3662 | } else { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 3663 | unsigned Opc = |
| 3664 | TLI.getPointerTy(DL) == MVT::i32 |
| 3665 | ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r) |
| 3666 | : X86::LEA64r; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3667 | addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3668 | TII.get(Opc), ResultReg), AM); |
| 3669 | } |
| 3670 | return ResultReg; |
| 3671 | } |
| 3672 | return 0; |
| 3673 | } |
| 3674 | |
| 3675 | unsigned X86FastISel::fastMaterializeConstant(const Constant *C) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 3676 | EVT CEVT = TLI.getValueType(DL, C->getType(), true); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3677 | |
| 3678 | // Only handle simple types. |
| 3679 | if (!CEVT.isSimple()) |
| 3680 | return 0; |
| 3681 | MVT VT = CEVT.getSimpleVT(); |
| 3682 | |
| 3683 | if (const auto *CI = dyn_cast<ConstantInt>(C)) |
| 3684 | return X86MaterializeInt(CI, VT); |
| 3685 | else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
| 3686 | return X86MaterializeFP(CFP, VT); |
| 3687 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 3688 | return X86MaterializeGV(GV, VT); |
| 3689 | |
| 3690 | return 0; |
| 3691 | } |
| 3692 | |
| 3693 | unsigned X86FastISel::fastMaterializeAlloca(const AllocaInst *C) { |
| 3694 | // Fail on dynamic allocas. At this point, getRegForValue has already |
| 3695 | // checked its CSE maps, so if we're here trying to handle a dynamic |
| 3696 | // alloca, we're not going to succeed. X86SelectAddress has a |
| 3697 | // check for dynamic allocas, because it's called directly from |
| 3698 | // various places, but targetMaterializeAlloca also needs a check |
| 3699 | // in order to avoid recursion between getRegForValue, |
| 3700 | // X86SelectAddrss, and targetMaterializeAlloca. |
| 3701 | if (!FuncInfo.StaticAllocaMap.count(C)) |
| 3702 | return 0; |
| 3703 | assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?"); |
| 3704 | |
| 3705 | X86AddressMode AM; |
| 3706 | if (!X86SelectAddress(C, AM)) |
| 3707 | return 0; |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 3708 | unsigned Opc = |
| 3709 | TLI.getPointerTy(DL) == MVT::i32 |
| 3710 | ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r) |
| 3711 | : X86::LEA64r; |
| 3712 | const TargetRegisterClass *RC = TLI.getRegClassFor(TLI.getPointerTy(DL)); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3713 | unsigned ResultReg = createResultReg(RC); |
| 3714 | addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 3715 | TII.get(Opc), ResultReg), AM); |
| 3716 | return ResultReg; |
| 3717 | } |
| 3718 | |
| 3719 | unsigned X86FastISel::fastMaterializeFloatZero(const ConstantFP *CF) { |
| 3720 | MVT VT; |
| 3721 | if (!isTypeLegal(CF->getType(), VT)) |
| 3722 | return 0; |
| 3723 | |
| 3724 | // Get opcode and regclass for the given zero. |
| 3725 | unsigned Opc = 0; |
| 3726 | const TargetRegisterClass *RC = nullptr; |
| 3727 | switch (VT.SimpleTy) { |
| 3728 | default: return 0; |
| 3729 | case MVT::f32: |
| 3730 | if (X86ScalarSSEf32) { |
| 3731 | Opc = X86::FsFLD0SS; |
| 3732 | RC = &X86::FR32RegClass; |
| 3733 | } else { |
| 3734 | Opc = X86::LD_Fp032; |
| 3735 | RC = &X86::RFP32RegClass; |
| 3736 | } |
| 3737 | break; |
| 3738 | case MVT::f64: |
| 3739 | if (X86ScalarSSEf64) { |
| 3740 | Opc = X86::FsFLD0SD; |
| 3741 | RC = &X86::FR64RegClass; |
| 3742 | } else { |
| 3743 | Opc = X86::LD_Fp064; |
| 3744 | RC = &X86::RFP64RegClass; |
| 3745 | } |
| 3746 | break; |
| 3747 | case MVT::f80: |
| 3748 | // No f80 support yet. |
| 3749 | return 0; |
| 3750 | } |
| 3751 | |
| 3752 | unsigned ResultReg = createResultReg(RC); |
| 3753 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg); |
| 3754 | return ResultReg; |
| 3755 | } |
| 3756 | |
| 3757 | |
| 3758 | bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, |
| 3759 | const LoadInst *LI) { |
| 3760 | const Value *Ptr = LI->getPointerOperand(); |
| 3761 | X86AddressMode AM; |
| 3762 | if (!X86SelectAddress(Ptr, AM)) |
| 3763 | return false; |
| 3764 | |
| 3765 | const X86InstrInfo &XII = (const X86InstrInfo &)TII; |
| 3766 | |
| 3767 | unsigned Size = DL.getTypeAllocSize(LI->getType()); |
| 3768 | unsigned Alignment = LI->getAlignment(); |
| 3769 | |
| 3770 | if (Alignment == 0) // Ensure that codegen never sees alignment 0 |
| 3771 | Alignment = DL.getABITypeAlignment(LI->getType()); |
| 3772 | |
| 3773 | SmallVector<MachineOperand, 8> AddrOps; |
| 3774 | AM.getFullAddress(AddrOps); |
| 3775 | |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 3776 | MachineInstr *Result = XII.foldMemoryOperandImpl( |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 3777 | *FuncInfo.MF, *MI, OpNo, AddrOps, FuncInfo.InsertPt, Size, Alignment, |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 3778 | /*AllowCommute=*/true); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3779 | if (!Result) |
| 3780 | return false; |
| 3781 | |
Pete Cooper | d31583d | 2015-05-06 21:37:19 +0000 | [diff] [blame] | 3782 | // The index register could be in the wrong register class. Unfortunately, |
| 3783 | // foldMemoryOperandImpl could have commuted the instruction so its not enough |
| 3784 | // to just look at OpNo + the offset to the index reg. We actually need to |
| 3785 | // scan the instruction to find the index reg and see if its the correct reg |
| 3786 | // class. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 3787 | unsigned OperandNo = 0; |
| 3788 | for (MachineInstr::mop_iterator I = Result->operands_begin(), |
| 3789 | E = Result->operands_end(); I != E; ++I, ++OperandNo) { |
| 3790 | MachineOperand &MO = *I; |
| 3791 | if (!MO.isReg() || MO.isDef() || MO.getReg() != AM.IndexReg) |
Pete Cooper | d31583d | 2015-05-06 21:37:19 +0000 | [diff] [blame] | 3792 | continue; |
| 3793 | // Found the index reg, now try to rewrite it. |
Pete Cooper | d31583d | 2015-05-06 21:37:19 +0000 | [diff] [blame] | 3794 | unsigned IndexReg = constrainOperandRegClass(Result->getDesc(), |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 3795 | MO.getReg(), OperandNo); |
| 3796 | if (IndexReg == MO.getReg()) |
Pete Cooper | d31583d | 2015-05-06 21:37:19 +0000 | [diff] [blame] | 3797 | continue; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 3798 | MO.setReg(IndexReg); |
Pete Cooper | d31583d | 2015-05-06 21:37:19 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3801 | Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI)); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 3802 | MI->eraseFromParent(); |
| 3803 | return true; |
| 3804 | } |
| 3805 | |
| 3806 | |
| 3807 | namespace llvm { |
| 3808 | FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo, |
| 3809 | const TargetLibraryInfo *libInfo) { |
| 3810 | return new X86FastISel(funcInfo, libInfo); |
| 3811 | } |
| 3812 | } |