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