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