blob: 7d38abbc2cef741cff41a0aa246d98d4c8a9a328 [file] [log] [blame]
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
17#include "X86CallingConv.h"
18#include "X86InstrBuilder.h"
19#include "X86InstrInfo.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86RegisterInfo.h"
22#include "X86Subtarget.h"
23#include "X86TargetMachine.h"
24#include "llvm/Analysis/BranchProbabilityInfo.h"
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000025#include "llvm/CodeGen/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/IR/CallSite.h"
31#include "llvm/IR/CallingConv.h"
Reid Kleckner28865802016-04-14 18:29:59 +000032#include "llvm/IR/DebugInfo.h"
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000033#include "llvm/IR/DerivedTypes.h"
34#include "llvm/IR/GetElementPtrTypeIterator.h"
35#include "llvm/IR/GlobalAlias.h"
36#include "llvm/IR/GlobalVariable.h"
37#include "llvm/IR/Instructions.h"
38#include "llvm/IR/IntrinsicInst.h"
39#include "llvm/IR/Operator.h"
David Majnemerca194852015-02-10 22:00:34 +000040#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000041#include "llvm/MC/MCSymbol.h"
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000042#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Target/TargetOptions.h"
44using namespace llvm;
45
46namespace {
47
48class 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
60public:
61 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
62 const TargetLibraryInfo *libInfo)
Eric Christophera1c535b2015-02-02 23:03:45 +000063 : FastISel(funcInfo, libInfo) {
64 Subtarget = &funcInfo.MF->getSubtarget<X86Subtarget>();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000065 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
84private:
Benjamin Kramerbdc49562016-06-12 15:39:02 +000085 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT,
86 const DebugLoc &DL);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000087
Pete Cooperd0dae3e2015-05-05 23:41:53 +000088 bool X86FastEmitLoad(EVT VT, X86AddressMode &AM, MachineMemOperand *MMO,
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +000089 unsigned &ResultReg, unsigned Alignment = 1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000090
Pete Cooperd0dae3e2015-05-05 23:41:53 +000091 bool X86FastEmitStore(EVT VT, const Value *Val, X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000092 MachineMemOperand *MMO = nullptr, bool Aligned = false);
93 bool X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
Pete Cooperd0dae3e2015-05-05 23:41:53 +000094 X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000095 MachineMemOperand *MMO = nullptr, bool Aligned = false);
96
97 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
98 unsigned &ResultReg);
99
100 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
101 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
102
103 bool X86SelectLoad(const Instruction *I);
104
105 bool X86SelectStore(const Instruction *I);
106
107 bool X86SelectRet(const Instruction *I);
108
109 bool X86SelectCmp(const Instruction *I);
110
111 bool X86SelectZExt(const Instruction *I);
112
113 bool X86SelectBranch(const Instruction *I);
114
115 bool X86SelectShift(const Instruction *I);
116
117 bool X86SelectDivRem(const Instruction *I);
118
119 bool X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I);
120
121 bool X86FastEmitSSESelect(MVT RetVT, const Instruction *I);
122
123 bool X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I);
124
125 bool X86SelectSelect(const Instruction *I);
126
127 bool X86SelectTrunc(const Instruction *I);
128
Andrea Di Biagio62622d22015-02-10 12:04:41 +0000129 bool X86SelectFPExtOrFPTrunc(const Instruction *I, unsigned Opc,
130 const TargetRegisterClass *RC);
131
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000132 bool X86SelectFPExt(const Instruction *I);
133 bool X86SelectFPTrunc(const Instruction *I);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +0000134 bool X86SelectSIToFP(const Instruction *I);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000135
136 const X86InstrInfo *getInstrInfo() const {
Eric Christophera1c535b2015-02-02 23:03:45 +0000137 return Subtarget->getInstrInfo();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000138 }
139 const X86TargetMachine *getTargetMachine() const {
140 return static_cast<const X86TargetMachine *>(&TM);
141 }
142
143 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
144
145 unsigned X86MaterializeInt(const ConstantInt *CI, MVT VT);
146 unsigned X86MaterializeFP(const ConstantFP *CFP, MVT VT);
147 unsigned X86MaterializeGV(const GlobalValue *GV, MVT VT);
148 unsigned fastMaterializeConstant(const Constant *C) override;
149
150 unsigned fastMaterializeAlloca(const AllocaInst *C) override;
151
152 unsigned fastMaterializeFloatZero(const ConstantFP *CF) override;
153
154 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
155 /// computed in an SSE register, not on the X87 floating point stack.
156 bool isScalarFPTypeInSSEReg(EVT VT) const {
157 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
158 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
159 }
160
161 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
162
163 bool IsMemcpySmall(uint64_t Len);
164
165 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
166 X86AddressMode SrcAM, uint64_t Len);
167
168 bool foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
169 const Value *Cond);
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000170
171 const MachineInstrBuilder &addFullAddress(const MachineInstrBuilder &MIB,
172 X86AddressMode &AM);
Craig Topper7ef6ea32016-12-05 04:51:31 +0000173
174 unsigned fastEmitInst_rrrr(unsigned MachineInstOpcode,
175 const TargetRegisterClass *RC, unsigned Op0,
176 bool Op0IsKill, unsigned Op1, bool Op1IsKill,
177 unsigned Op2, bool Op2IsKill, unsigned Op3,
178 bool Op3IsKill);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000179};
180
181} // end anonymous namespace.
182
183static std::pair<X86::CondCode, bool>
184getX86ConditionCode(CmpInst::Predicate Predicate) {
185 X86::CondCode CC = X86::COND_INVALID;
186 bool NeedSwap = false;
187 switch (Predicate) {
188 default: break;
189 // Floating-point Predicates
190 case CmpInst::FCMP_UEQ: CC = X86::COND_E; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000191 case CmpInst::FCMP_OLT: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000192 case CmpInst::FCMP_OGT: CC = X86::COND_A; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000193 case CmpInst::FCMP_OLE: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000194 case CmpInst::FCMP_OGE: CC = X86::COND_AE; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000195 case CmpInst::FCMP_UGT: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000196 case CmpInst::FCMP_ULT: CC = X86::COND_B; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000197 case CmpInst::FCMP_UGE: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000198 case CmpInst::FCMP_ULE: CC = X86::COND_BE; break;
199 case CmpInst::FCMP_ONE: CC = X86::COND_NE; break;
200 case CmpInst::FCMP_UNO: CC = X86::COND_P; break;
201 case CmpInst::FCMP_ORD: CC = X86::COND_NP; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000202 case CmpInst::FCMP_OEQ: LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000203 case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break;
204
205 // Integer Predicates
206 case CmpInst::ICMP_EQ: CC = X86::COND_E; break;
207 case CmpInst::ICMP_NE: CC = X86::COND_NE; break;
208 case CmpInst::ICMP_UGT: CC = X86::COND_A; break;
209 case CmpInst::ICMP_UGE: CC = X86::COND_AE; break;
210 case CmpInst::ICMP_ULT: CC = X86::COND_B; break;
211 case CmpInst::ICMP_ULE: CC = X86::COND_BE; break;
212 case CmpInst::ICMP_SGT: CC = X86::COND_G; break;
213 case CmpInst::ICMP_SGE: CC = X86::COND_GE; break;
214 case CmpInst::ICMP_SLT: CC = X86::COND_L; break;
215 case CmpInst::ICMP_SLE: CC = X86::COND_LE; break;
216 }
217
218 return std::make_pair(CC, NeedSwap);
219}
220
221static std::pair<unsigned, bool>
222getX86SSEConditionCode(CmpInst::Predicate Predicate) {
223 unsigned CC;
224 bool NeedSwap = false;
225
226 // SSE Condition code mapping:
227 // 0 - EQ
228 // 1 - LT
229 // 2 - LE
230 // 3 - UNORD
231 // 4 - NEQ
232 // 5 - NLT
233 // 6 - NLE
234 // 7 - ORD
235 switch (Predicate) {
236 default: llvm_unreachable("Unexpected predicate");
237 case CmpInst::FCMP_OEQ: CC = 0; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000238 case CmpInst::FCMP_OGT: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000239 case CmpInst::FCMP_OLT: CC = 1; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000240 case CmpInst::FCMP_OGE: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000241 case CmpInst::FCMP_OLE: CC = 2; break;
242 case CmpInst::FCMP_UNO: CC = 3; break;
243 case CmpInst::FCMP_UNE: CC = 4; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000244 case CmpInst::FCMP_ULE: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000245 case CmpInst::FCMP_UGE: CC = 5; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000246 case CmpInst::FCMP_ULT: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000247 case CmpInst::FCMP_UGT: CC = 6; break;
248 case CmpInst::FCMP_ORD: CC = 7; break;
249 case CmpInst::FCMP_UEQ:
250 case CmpInst::FCMP_ONE: CC = 8; break;
251 }
252
253 return std::make_pair(CC, NeedSwap);
254}
255
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000256/// \brief Adds a complex addressing mode to the given machine instr builder.
257/// Note, this will constrain the index register. If its not possible to
258/// constrain the given index register, then a new one will be created. The
259/// IndexReg field of the addressing mode will be updated to match in this case.
260const MachineInstrBuilder &
261X86FastISel::addFullAddress(const MachineInstrBuilder &MIB,
262 X86AddressMode &AM) {
263 // First constrain the index register. It needs to be a GR64_NOSP.
264 AM.IndexReg = constrainOperandRegClass(MIB->getDesc(), AM.IndexReg,
265 MIB->getNumOperands() +
266 X86::AddrIndexReg);
267 return ::addFullAddress(MIB, AM);
268}
269
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000270/// \brief Check if it is possible to fold the condition from the XALU intrinsic
271/// into the user. The condition code will only be updated on success.
272bool X86FastISel::foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
273 const Value *Cond) {
274 if (!isa<ExtractValueInst>(Cond))
275 return false;
276
277 const auto *EV = cast<ExtractValueInst>(Cond);
278 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
279 return false;
280
281 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
282 MVT RetVT;
283 const Function *Callee = II->getCalledFunction();
284 Type *RetTy =
285 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
286 if (!isTypeLegal(RetTy, RetVT))
287 return false;
288
289 if (RetVT != MVT::i32 && RetVT != MVT::i64)
290 return false;
291
292 X86::CondCode TmpCC;
293 switch (II->getIntrinsicID()) {
294 default: return false;
295 case Intrinsic::sadd_with_overflow:
296 case Intrinsic::ssub_with_overflow:
297 case Intrinsic::smul_with_overflow:
298 case Intrinsic::umul_with_overflow: TmpCC = X86::COND_O; break;
299 case Intrinsic::uadd_with_overflow:
300 case Intrinsic::usub_with_overflow: TmpCC = X86::COND_B; break;
301 }
302
303 // Check if both instructions are in the same basic block.
304 if (II->getParent() != I->getParent())
305 return false;
306
307 // Make sure nothing is in the way
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000308 BasicBlock::const_iterator Start(I);
309 BasicBlock::const_iterator End(II);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000310 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
311 // We only expect extractvalue instructions between the intrinsic and the
312 // instruction to be selected.
313 if (!isa<ExtractValueInst>(Itr))
314 return false;
315
316 // Check that the extractvalue operand comes from the intrinsic.
317 const auto *EVI = cast<ExtractValueInst>(Itr);
318 if (EVI->getAggregateOperand() != II)
319 return false;
320 }
321
322 CC = TmpCC;
323 return true;
324}
325
326bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000327 EVT evt = TLI.getValueType(DL, Ty, /*HandleUnknown=*/true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000328 if (evt == MVT::Other || !evt.isSimple())
329 // Unhandled type. Halt "fast" selection and bail.
330 return false;
331
332 VT = evt.getSimpleVT();
333 // For now, require SSE/SSE2 for performing floating-point operations,
334 // since x87 requires additional work.
335 if (VT == MVT::f64 && !X86ScalarSSEf64)
336 return false;
337 if (VT == MVT::f32 && !X86ScalarSSEf32)
338 return false;
339 // Similarly, no f80 support yet.
340 if (VT == MVT::f80)
341 return false;
342 // We only handle legal types. For example, on x86-32 the instruction
343 // selector contains all of the 64-bit instructions from x86-64,
344 // under the assumption that i64 won't be used if the target doesn't
345 // support it.
346 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
347}
348
349#include "X86GenCallingConv.inc"
350
351/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
352/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
353/// Return true and the result register by reference if it is possible.
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000354bool X86FastISel::X86FastEmitLoad(EVT VT, X86AddressMode &AM,
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000355 MachineMemOperand *MMO, unsigned &ResultReg,
356 unsigned Alignment) {
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000357 bool HasSSE41 = Subtarget->hasSSE41();
Craig Topperca9c0802016-06-02 04:19:45 +0000358 bool HasAVX = Subtarget->hasAVX();
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000359 bool HasAVX2 = Subtarget->hasAVX2();
Craig Topperdfc4fc92016-09-05 23:58:40 +0000360 bool HasAVX512 = Subtarget->hasAVX512();
361 bool HasVLX = Subtarget->hasVLX();
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000362 bool IsNonTemporal = MMO && MMO->isNonTemporal();
363
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000364 // Get opcode and regclass of the output for the given load instruction.
365 unsigned Opc = 0;
366 const TargetRegisterClass *RC = nullptr;
367 switch (VT.getSimpleVT().SimpleTy) {
368 default: return false;
369 case MVT::i1:
Craig Topper058f2f62017-03-28 16:35:29 +0000370 // TODO: Support this properly.
371 if (Subtarget->hasAVX512())
372 return false;
373 LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000374 case MVT::i8:
375 Opc = X86::MOV8rm;
376 RC = &X86::GR8RegClass;
377 break;
378 case MVT::i16:
379 Opc = X86::MOV16rm;
380 RC = &X86::GR16RegClass;
381 break;
382 case MVT::i32:
383 Opc = X86::MOV32rm;
384 RC = &X86::GR32RegClass;
385 break;
386 case MVT::i64:
387 // Must be in x86-64 mode.
388 Opc = X86::MOV64rm;
389 RC = &X86::GR64RegClass;
390 break;
391 case MVT::f32:
392 if (X86ScalarSSEf32) {
Craig Topperdfc4fc92016-09-05 23:58:40 +0000393 Opc = HasAVX512 ? X86::VMOVSSZrm : HasAVX ? X86::VMOVSSrm : X86::MOVSSrm;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000394 RC = &X86::FR32RegClass;
395 } else {
396 Opc = X86::LD_Fp32m;
397 RC = &X86::RFP32RegClass;
398 }
399 break;
400 case MVT::f64:
401 if (X86ScalarSSEf64) {
Craig Topperdfc4fc92016-09-05 23:58:40 +0000402 Opc = HasAVX512 ? X86::VMOVSDZrm : HasAVX ? X86::VMOVSDrm : X86::MOVSDrm;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000403 RC = &X86::FR64RegClass;
404 } else {
405 Opc = X86::LD_Fp64m;
406 RC = &X86::RFP64RegClass;
407 }
408 break;
409 case MVT::f80:
410 // No f80 support yet.
411 return false;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000412 case MVT::v4f32:
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000413 if (IsNonTemporal && Alignment >= 16 && HasSSE41)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000414 Opc = HasVLX ? X86::VMOVNTDQAZ128rm :
415 HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000416 else if (Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000417 Opc = HasVLX ? X86::VMOVAPSZ128rm :
418 HasAVX ? X86::VMOVAPSrm : X86::MOVAPSrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000419 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000420 Opc = HasVLX ? X86::VMOVUPSZ128rm :
421 HasAVX ? X86::VMOVUPSrm : X86::MOVUPSrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000422 RC = &X86::VR128RegClass;
423 break;
424 case MVT::v2f64:
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000425 if (IsNonTemporal && Alignment >= 16 && HasSSE41)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000426 Opc = HasVLX ? X86::VMOVNTDQAZ128rm :
427 HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000428 else if (Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000429 Opc = HasVLX ? X86::VMOVAPDZ128rm :
430 HasAVX ? X86::VMOVAPDrm : X86::MOVAPDrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000431 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000432 Opc = HasVLX ? X86::VMOVUPDZ128rm :
433 HasAVX ? X86::VMOVUPDrm : X86::MOVUPDrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000434 RC = &X86::VR128RegClass;
435 break;
436 case MVT::v4i32:
437 case MVT::v2i64:
438 case MVT::v8i16:
439 case MVT::v16i8:
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000440 if (IsNonTemporal && Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000441 Opc = HasVLX ? X86::VMOVNTDQAZ128rm :
442 HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000443 else if (Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000444 Opc = HasVLX ? X86::VMOVDQA64Z128rm :
445 HasAVX ? X86::VMOVDQArm : X86::MOVDQArm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000446 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000447 Opc = HasVLX ? X86::VMOVDQU64Z128rm :
448 HasAVX ? X86::VMOVDQUrm : X86::MOVDQUrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000449 RC = &X86::VR128RegClass;
450 break;
Craig Topperca9c0802016-06-02 04:19:45 +0000451 case MVT::v8f32:
452 assert(HasAVX);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000453 if (IsNonTemporal && Alignment >= 32 && HasAVX2)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000454 Opc = HasVLX ? X86::VMOVNTDQAZ256rm : X86::VMOVNTDQAYrm;
455 else if (Alignment >= 32)
456 Opc = HasVLX ? X86::VMOVAPSZ256rm : X86::VMOVAPSYrm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000457 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000458 Opc = HasVLX ? X86::VMOVUPSZ256rm : X86::VMOVUPSYrm;
Craig Topperca9c0802016-06-02 04:19:45 +0000459 RC = &X86::VR256RegClass;
460 break;
461 case MVT::v4f64:
462 assert(HasAVX);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000463 if (IsNonTemporal && Alignment >= 32 && HasAVX2)
464 Opc = X86::VMOVNTDQAYrm;
Craig Topperdfc4fc92016-09-05 23:58:40 +0000465 else if (Alignment >= 32)
466 Opc = HasVLX ? X86::VMOVAPDZ256rm : X86::VMOVAPDYrm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000467 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000468 Opc = HasVLX ? X86::VMOVUPDZ256rm : X86::VMOVUPDYrm;
Craig Topperca9c0802016-06-02 04:19:45 +0000469 RC = &X86::VR256RegClass;
470 break;
471 case MVT::v8i32:
472 case MVT::v4i64:
473 case MVT::v16i16:
474 case MVT::v32i8:
475 assert(HasAVX);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000476 if (IsNonTemporal && Alignment >= 32 && HasAVX2)
477 Opc = X86::VMOVNTDQAYrm;
Craig Topperdfc4fc92016-09-05 23:58:40 +0000478 else if (Alignment >= 32)
479 Opc = HasVLX ? X86::VMOVDQA64Z256rm : X86::VMOVDQAYrm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000480 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000481 Opc = HasVLX ? X86::VMOVDQU64Z256rm : X86::VMOVDQUYrm;
Craig Topperca9c0802016-06-02 04:19:45 +0000482 RC = &X86::VR256RegClass;
483 break;
Craig Topper048a08a2016-06-02 04:51:37 +0000484 case MVT::v16f32:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000485 assert(HasAVX512);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000486 if (IsNonTemporal && Alignment >= 64)
487 Opc = X86::VMOVNTDQAZrm;
488 else
489 Opc = (Alignment >= 64) ? X86::VMOVAPSZrm : X86::VMOVUPSZrm;
Craig Topper048a08a2016-06-02 04:51:37 +0000490 RC = &X86::VR512RegClass;
491 break;
492 case MVT::v8f64:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000493 assert(HasAVX512);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000494 if (IsNonTemporal && Alignment >= 64)
495 Opc = X86::VMOVNTDQAZrm;
496 else
497 Opc = (Alignment >= 64) ? X86::VMOVAPDZrm : X86::VMOVUPDZrm;
Craig Topper048a08a2016-06-02 04:51:37 +0000498 RC = &X86::VR512RegClass;
499 break;
500 case MVT::v8i64:
501 case MVT::v16i32:
502 case MVT::v32i16:
503 case MVT::v64i8:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000504 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000505 // Note: There are a lot more choices based on type with AVX-512, but
506 // there's really no advantage when the load isn't masked.
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000507 if (IsNonTemporal && Alignment >= 64)
508 Opc = X86::VMOVNTDQAZrm;
509 else
510 Opc = (Alignment >= 64) ? X86::VMOVDQA64Zrm : X86::VMOVDQU64Zrm;
Craig Topper048a08a2016-06-02 04:51:37 +0000511 RC = &X86::VR512RegClass;
512 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000513 }
514
515 ResultReg = createResultReg(RC);
516 MachineInstrBuilder MIB =
517 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
518 addFullAddress(MIB, AM);
519 if (MMO)
520 MIB->addMemOperand(*FuncInfo.MF, MMO);
521 return true;
522}
523
524/// X86FastEmitStore - Emit a machine instruction to store a value Val of
525/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
526/// and a displacement offset, or a GlobalAddress,
527/// i.e. V. Return true if it is possible.
528bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000529 X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000530 MachineMemOperand *MMO, bool Aligned) {
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000531 bool HasSSE2 = Subtarget->hasSSE2();
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000532 bool HasSSE4A = Subtarget->hasSSE4A();
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000533 bool HasAVX = Subtarget->hasAVX();
Craig Topperdfc4fc92016-09-05 23:58:40 +0000534 bool HasAVX512 = Subtarget->hasAVX512();
535 bool HasVLX = Subtarget->hasVLX();
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000536 bool IsNonTemporal = MMO && MMO->isNonTemporal();
537
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000538 // Get opcode and regclass of the output for the given store instruction.
539 unsigned Opc = 0;
540 switch (VT.getSimpleVT().SimpleTy) {
541 case MVT::f80: // No f80 support yet.
542 default: return false;
543 case MVT::i1: {
Craig Topper9d50e182017-03-14 04:18:25 +0000544 // In case ValReg is a K register, COPY to a GPR
545 if (MRI.getRegClass(ValReg) == &X86::VK1RegClass) {
546 unsigned KValReg = ValReg;
Craig Topper058f2f62017-03-28 16:35:29 +0000547 ValReg = createResultReg(&X86::GR32RegClass);
Craig Topper9d50e182017-03-14 04:18:25 +0000548 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
549 TII.get(TargetOpcode::COPY), ValReg)
550 .addReg(KValReg);
Craig Topper058f2f62017-03-28 16:35:29 +0000551 ValReg = fastEmitInst_extractsubreg(MVT::i8, ValReg, /*Kill=*/true,
552 X86::sub_8bit);
Craig Topper9d50e182017-03-14 04:18:25 +0000553 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000554 // Mask out all but lowest bit.
555 unsigned AndResult = createResultReg(&X86::GR8RegClass);
556 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
557 TII.get(X86::AND8ri), AndResult)
558 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
559 ValReg = AndResult;
Justin Bognerb03fd122016-08-17 05:10:15 +0000560 LLVM_FALLTHROUGH; // handle i1 as i8.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000561 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000562 case MVT::i8: Opc = X86::MOV8mr; break;
563 case MVT::i16: Opc = X86::MOV16mr; break;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000564 case MVT::i32:
565 Opc = (IsNonTemporal && HasSSE2) ? X86::MOVNTImr : X86::MOV32mr;
566 break;
567 case MVT::i64:
568 // Must be in x86-64 mode.
569 Opc = (IsNonTemporal && HasSSE2) ? X86::MOVNTI_64mr : X86::MOV64mr;
570 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000571 case MVT::f32:
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000572 if (X86ScalarSSEf32) {
573 if (IsNonTemporal && HasSSE4A)
574 Opc = X86::MOVNTSS;
575 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000576 Opc = HasAVX512 ? X86::VMOVSSZmr :
577 HasAVX ? X86::VMOVSSmr : X86::MOVSSmr;
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000578 } else
579 Opc = X86::ST_Fp32m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000580 break;
581 case MVT::f64:
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000582 if (X86ScalarSSEf32) {
583 if (IsNonTemporal && HasSSE4A)
584 Opc = X86::MOVNTSD;
585 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000586 Opc = HasAVX512 ? X86::VMOVSDZmr :
587 HasAVX ? X86::VMOVSDmr : X86::MOVSDmr;
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000588 } else
589 Opc = X86::ST_Fp64m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000590 break;
591 case MVT::v4f32:
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000592 if (Aligned) {
593 if (IsNonTemporal)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000594 Opc = HasVLX ? X86::VMOVNTPSZ128mr :
595 HasAVX ? X86::VMOVNTPSmr : X86::MOVNTPSmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000596 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000597 Opc = HasVLX ? X86::VMOVAPSZ128mr :
598 HasAVX ? X86::VMOVAPSmr : X86::MOVAPSmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000599 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000600 Opc = HasVLX ? X86::VMOVUPSZ128mr :
601 HasAVX ? X86::VMOVUPSmr : X86::MOVUPSmr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000602 break;
603 case MVT::v2f64:
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000604 if (Aligned) {
605 if (IsNonTemporal)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000606 Opc = HasVLX ? X86::VMOVNTPDZ128mr :
607 HasAVX ? X86::VMOVNTPDmr : X86::MOVNTPDmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000608 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000609 Opc = HasVLX ? X86::VMOVAPDZ128mr :
610 HasAVX ? X86::VMOVAPDmr : X86::MOVAPDmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000611 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000612 Opc = HasVLX ? X86::VMOVUPDZ128mr :
613 HasAVX ? X86::VMOVUPDmr : X86::MOVUPDmr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000614 break;
615 case MVT::v4i32:
616 case MVT::v2i64:
617 case MVT::v8i16:
618 case MVT::v16i8:
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000619 if (Aligned) {
620 if (IsNonTemporal)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000621 Opc = HasVLX ? X86::VMOVNTDQZ128mr :
622 HasAVX ? X86::VMOVNTDQmr : X86::MOVNTDQmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000623 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000624 Opc = HasVLX ? X86::VMOVDQA64Z128mr :
625 HasAVX ? X86::VMOVDQAmr : X86::MOVDQAmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000626 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000627 Opc = HasVLX ? X86::VMOVDQU64Z128mr :
628 HasAVX ? X86::VMOVDQUmr : X86::MOVDQUmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000629 break;
630 case MVT::v8f32:
631 assert(HasAVX);
Craig Topperdfc4fc92016-09-05 23:58:40 +0000632 if (Aligned) {
633 if (IsNonTemporal)
634 Opc = HasVLX ? X86::VMOVNTPSZ256mr : X86::VMOVNTPSYmr;
635 else
636 Opc = HasVLX ? X86::VMOVAPSZ256mr : X86::VMOVAPSYmr;
637 } else
638 Opc = HasVLX ? X86::VMOVUPSZ256mr : X86::VMOVUPSYmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000639 break;
640 case MVT::v4f64:
641 assert(HasAVX);
642 if (Aligned) {
Craig Topperdfc4fc92016-09-05 23:58:40 +0000643 if (IsNonTemporal)
644 Opc = HasVLX ? X86::VMOVNTPDZ256mr : X86::VMOVNTPDYmr;
645 else
646 Opc = HasVLX ? X86::VMOVAPDZ256mr : X86::VMOVAPDYmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000647 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000648 Opc = HasVLX ? X86::VMOVUPDZ256mr : X86::VMOVUPDYmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000649 break;
650 case MVT::v8i32:
651 case MVT::v4i64:
652 case MVT::v16i16:
653 case MVT::v32i8:
654 assert(HasAVX);
Craig Topperdfc4fc92016-09-05 23:58:40 +0000655 if (Aligned) {
656 if (IsNonTemporal)
657 Opc = HasVLX ? X86::VMOVNTDQZ256mr : X86::VMOVNTDQYmr;
658 else
659 Opc = HasVLX ? X86::VMOVDQA64Z256mr : X86::VMOVDQAYmr;
660 } else
661 Opc = HasVLX ? X86::VMOVDQU64Z256mr : X86::VMOVDQUYmr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000662 break;
Craig Topper048a08a2016-06-02 04:51:37 +0000663 case MVT::v16f32:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000664 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000665 if (Aligned)
666 Opc = IsNonTemporal ? X86::VMOVNTPSZmr : X86::VMOVAPSZmr;
667 else
668 Opc = X86::VMOVUPSZmr;
669 break;
670 case MVT::v8f64:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000671 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000672 if (Aligned) {
673 Opc = IsNonTemporal ? X86::VMOVNTPDZmr : X86::VMOVAPDZmr;
674 } else
675 Opc = X86::VMOVUPDZmr;
676 break;
677 case MVT::v8i64:
678 case MVT::v16i32:
679 case MVT::v32i16:
680 case MVT::v64i8:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000681 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000682 // Note: There are a lot more choices based on type with AVX-512, but
683 // there's really no advantage when the store isn't masked.
684 if (Aligned)
685 Opc = IsNonTemporal ? X86::VMOVNTDQZmr : X86::VMOVDQA64Zmr;
686 else
687 Opc = X86::VMOVDQU64Zmr;
688 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000689 }
690
Quentin Colombetbf200682016-04-27 22:33:42 +0000691 const MCInstrDesc &Desc = TII.get(Opc);
692 // Some of the instructions in the previous switch use FR128 instead
693 // of FR32 for ValReg. Make sure the register we feed the instruction
694 // matches its register class constraints.
695 // Note: This is fine to do a copy from FR32 to FR128, this is the
696 // same registers behind the scene and actually why it did not trigger
697 // any bugs before.
698 ValReg = constrainOperandRegClass(Desc, ValReg, Desc.getNumOperands() - 1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000699 MachineInstrBuilder MIB =
Quentin Colombetbf200682016-04-27 22:33:42 +0000700 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, Desc);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000701 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
702 if (MMO)
703 MIB->addMemOperand(*FuncInfo.MF, MMO);
704
705 return true;
706}
707
708bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000709 X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000710 MachineMemOperand *MMO, bool Aligned) {
711 // Handle 'null' like i32/i64 0.
712 if (isa<ConstantPointerNull>(Val))
713 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
714
715 // If this is a store of a simple constant, fold the constant into the store.
716 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
717 unsigned Opc = 0;
718 bool Signed = true;
719 switch (VT.getSimpleVT().SimpleTy) {
720 default: break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000721 case MVT::i1:
722 Signed = false;
723 LLVM_FALLTHROUGH; // Handle as i8.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000724 case MVT::i8: Opc = X86::MOV8mi; break;
725 case MVT::i16: Opc = X86::MOV16mi; break;
726 case MVT::i32: Opc = X86::MOV32mi; break;
727 case MVT::i64:
728 // Must be a 32-bit sign extended value.
729 if (isInt<32>(CI->getSExtValue()))
730 Opc = X86::MOV64mi32;
731 break;
732 }
733
734 if (Opc) {
735 MachineInstrBuilder MIB =
736 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
737 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
738 : CI->getZExtValue());
739 if (MMO)
740 MIB->addMemOperand(*FuncInfo.MF, MMO);
741 return true;
742 }
743 }
744
745 unsigned ValReg = getRegForValue(Val);
746 if (ValReg == 0)
747 return false;
748
749 bool ValKill = hasTrivialKill(Val);
750 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
751}
752
753/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
754/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
755/// ISD::SIGN_EXTEND).
756bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
757 unsigned Src, EVT SrcVT,
758 unsigned &ResultReg) {
759 unsigned RR = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
760 Src, /*TODO: Kill=*/false);
761 if (RR == 0)
762 return false;
763
764 ResultReg = RR;
765 return true;
766}
767
768bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
769 // Handle constant address.
770 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
771 // Can't handle alternate code models yet.
772 if (TM.getCodeModel() != CodeModel::Small)
773 return false;
774
775 // Can't handle TLS yet.
776 if (GV->isThreadLocal())
777 return false;
778
779 // RIP-relative addresses can't have additional register operands, so if
780 // we've already folded stuff into the addressing mode, just force the
781 // global value into its own register, which we can use as the basereg.
782 if (!Subtarget->isPICStyleRIPRel() ||
783 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
784 // Okay, we've committed to selecting this global. Set up the address.
785 AM.GV = GV;
786
787 // Allow the subtarget to classify the global.
Rafael Espindolaab03eb02016-05-19 22:07:57 +0000788 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000789
790 // If this reference is relative to the pic base, set it now.
791 if (isGlobalRelativeToPICBase(GVFlags)) {
792 // FIXME: How do we know Base.Reg is free??
793 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
794 }
795
796 // Unless the ABI requires an extra load, return a direct reference to
797 // the global.
798 if (!isGlobalStubReference(GVFlags)) {
799 if (Subtarget->isPICStyleRIPRel()) {
800 // Use rip-relative addressing if we can. Above we verified that the
801 // base and index registers are unused.
802 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
803 AM.Base.Reg = X86::RIP;
804 }
805 AM.GVOpFlags = GVFlags;
806 return true;
807 }
808
809 // Ok, we need to do a load from a stub. If we've already loaded from
810 // this stub, reuse the loaded pointer, otherwise emit the load now.
811 DenseMap<const Value *, unsigned>::iterator I = LocalValueMap.find(V);
812 unsigned LoadReg;
813 if (I != LocalValueMap.end() && I->second != 0) {
814 LoadReg = I->second;
815 } else {
816 // Issue load from stub.
817 unsigned Opc = 0;
818 const TargetRegisterClass *RC = nullptr;
819 X86AddressMode StubAM;
820 StubAM.Base.Reg = AM.Base.Reg;
821 StubAM.GV = GV;
822 StubAM.GVOpFlags = GVFlags;
823
824 // Prepare for inserting code in the local-value area.
825 SavePoint SaveInsertPt = enterLocalValueArea();
826
Mehdi Amini44ede332015-07-09 02:09:04 +0000827 if (TLI.getPointerTy(DL) == MVT::i64) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000828 Opc = X86::MOV64rm;
829 RC = &X86::GR64RegClass;
830
831 if (Subtarget->isPICStyleRIPRel())
832 StubAM.Base.Reg = X86::RIP;
833 } else {
834 Opc = X86::MOV32rm;
835 RC = &X86::GR32RegClass;
836 }
837
838 LoadReg = createResultReg(RC);
839 MachineInstrBuilder LoadMI =
840 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
841 addFullAddress(LoadMI, StubAM);
842
843 // Ok, back to normal mode.
844 leaveLocalValueArea(SaveInsertPt);
845
846 // Prevent loading GV stub multiple times in same MBB.
847 LocalValueMap[V] = LoadReg;
848 }
849
850 // Now construct the final address. Note that the Disp, Scale,
851 // and Index values may already be set here.
852 AM.Base.Reg = LoadReg;
853 AM.GV = nullptr;
854 return true;
855 }
856 }
857
858 // If all else fails, try to materialize the value in a register.
859 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
860 if (AM.Base.Reg == 0) {
861 AM.Base.Reg = getRegForValue(V);
862 return AM.Base.Reg != 0;
863 }
864 if (AM.IndexReg == 0) {
865 assert(AM.Scale == 1 && "Scale with no index!");
866 AM.IndexReg = getRegForValue(V);
867 return AM.IndexReg != 0;
868 }
869 }
870
871 return false;
872}
873
874/// X86SelectAddress - Attempt to fill in an address from the given value.
875///
876bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
877 SmallVector<const Value *, 32> GEPs;
878redo_gep:
879 const User *U = nullptr;
880 unsigned Opcode = Instruction::UserOp1;
881 if (const Instruction *I = dyn_cast<Instruction>(V)) {
882 // Don't walk into other basic blocks; it's possible we haven't
883 // visited them yet, so the instructions may not yet be assigned
884 // virtual registers.
885 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
886 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
887 Opcode = I->getOpcode();
888 U = I;
889 }
890 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
891 Opcode = C->getOpcode();
892 U = C;
893 }
894
895 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
896 if (Ty->getAddressSpace() > 255)
897 // Fast instruction selection doesn't support the special
898 // address spaces.
899 return false;
900
901 switch (Opcode) {
902 default: break;
903 case Instruction::BitCast:
904 // Look past bitcasts.
905 return X86SelectAddress(U->getOperand(0), AM);
906
907 case Instruction::IntToPtr:
908 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000909 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
910 TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000911 return X86SelectAddress(U->getOperand(0), AM);
912 break;
913
914 case Instruction::PtrToInt:
915 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000916 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000917 return X86SelectAddress(U->getOperand(0), AM);
918 break;
919
920 case Instruction::Alloca: {
921 // Do static allocas.
922 const AllocaInst *A = cast<AllocaInst>(V);
923 DenseMap<const AllocaInst *, int>::iterator SI =
924 FuncInfo.StaticAllocaMap.find(A);
925 if (SI != FuncInfo.StaticAllocaMap.end()) {
926 AM.BaseType = X86AddressMode::FrameIndexBase;
927 AM.Base.FrameIndex = SI->second;
928 return true;
929 }
930 break;
931 }
932
933 case Instruction::Add: {
934 // Adds of constants are common and easy enough.
935 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
936 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
937 // They have to fit in the 32-bit signed displacement field though.
938 if (isInt<32>(Disp)) {
939 AM.Disp = (uint32_t)Disp;
940 return X86SelectAddress(U->getOperand(0), AM);
941 }
942 }
943 break;
944 }
945
946 case Instruction::GetElementPtr: {
947 X86AddressMode SavedAM = AM;
948
949 // Pattern-match simple GEPs.
950 uint64_t Disp = (int32_t)AM.Disp;
951 unsigned IndexReg = AM.IndexReg;
952 unsigned Scale = AM.Scale;
953 gep_type_iterator GTI = gep_type_begin(U);
954 // Iterate through the indices, folding what we can. Constants can be
955 // folded, and one dynamic index can be handled, if the scale is supported.
956 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
957 i != e; ++i, ++GTI) {
958 const Value *Op = *i;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000959 if (StructType *STy = GTI.getStructTypeOrNull()) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000960 const StructLayout *SL = DL.getStructLayout(STy);
961 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
962 continue;
963 }
964
965 // A array/variable index is always of the form i*S where S is the
966 // constant scale size. See if we can push the scale into immediates.
967 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
968 for (;;) {
969 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
970 // Constant-offset addressing.
971 Disp += CI->getSExtValue() * S;
972 break;
973 }
974 if (canFoldAddIntoGEP(U, Op)) {
975 // A compatible add with a constant operand. Fold the constant.
976 ConstantInt *CI =
977 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
978 Disp += CI->getSExtValue() * S;
979 // Iterate on the other operand.
980 Op = cast<AddOperator>(Op)->getOperand(0);
981 continue;
982 }
983 if (IndexReg == 0 &&
984 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
985 (S == 1 || S == 2 || S == 4 || S == 8)) {
986 // Scaled-index addressing.
987 Scale = S;
988 IndexReg = getRegForGEPIndex(Op).first;
989 if (IndexReg == 0)
990 return false;
991 break;
992 }
993 // Unsupported.
994 goto unsupported_gep;
995 }
996 }
997
998 // Check for displacement overflow.
999 if (!isInt<32>(Disp))
1000 break;
1001
1002 AM.IndexReg = IndexReg;
1003 AM.Scale = Scale;
1004 AM.Disp = (uint32_t)Disp;
1005 GEPs.push_back(V);
1006
1007 if (const GetElementPtrInst *GEP =
1008 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
1009 // Ok, the GEP indices were covered by constant-offset and scaled-index
1010 // addressing. Update the address state and move on to examining the base.
1011 V = GEP;
1012 goto redo_gep;
1013 } else if (X86SelectAddress(U->getOperand(0), AM)) {
1014 return true;
1015 }
1016
1017 // If we couldn't merge the gep value into this addr mode, revert back to
1018 // our address and just match the value instead of completely failing.
1019 AM = SavedAM;
1020
David Majnemerd7708772016-06-24 04:05:21 +00001021 for (const Value *I : reverse(GEPs))
1022 if (handleConstantAddresses(I, AM))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001023 return true;
1024
1025 return false;
1026 unsupported_gep:
1027 // Ok, the GEP indices weren't all covered.
1028 break;
1029 }
1030 }
1031
1032 return handleConstantAddresses(V, AM);
1033}
1034
1035/// X86SelectCallAddress - Attempt to fill in an address from the given value.
1036///
1037bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
1038 const User *U = nullptr;
1039 unsigned Opcode = Instruction::UserOp1;
1040 const Instruction *I = dyn_cast<Instruction>(V);
1041 // Record if the value is defined in the same basic block.
1042 //
1043 // This information is crucial to know whether or not folding an
1044 // operand is valid.
1045 // Indeed, FastISel generates or reuses a virtual register for all
1046 // operands of all instructions it selects. Obviously, the definition and
1047 // its uses must use the same virtual register otherwise the produced
1048 // code is incorrect.
1049 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
1050 // registers for values that are alive across basic blocks. This ensures
1051 // that the values are consistently set between across basic block, even
1052 // if different instruction selection mechanisms are used (e.g., a mix of
1053 // SDISel and FastISel).
1054 // For values local to a basic block, the instruction selection process
1055 // generates these virtual registers with whatever method is appropriate
1056 // for its needs. In particular, FastISel and SDISel do not share the way
1057 // local virtual registers are set.
1058 // Therefore, this is impossible (or at least unsafe) to share values
1059 // between basic blocks unless they use the same instruction selection
1060 // method, which is not guarantee for X86.
1061 // Moreover, things like hasOneUse could not be used accurately, if we
1062 // allow to reference values across basic blocks whereas they are not
1063 // alive across basic blocks initially.
1064 bool InMBB = true;
1065 if (I) {
1066 Opcode = I->getOpcode();
1067 U = I;
1068 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
1069 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
1070 Opcode = C->getOpcode();
1071 U = C;
1072 }
1073
1074 switch (Opcode) {
1075 default: break;
1076 case Instruction::BitCast:
1077 // Look past bitcasts if its operand is in the same BB.
1078 if (InMBB)
1079 return X86SelectCallAddress(U->getOperand(0), AM);
1080 break;
1081
1082 case Instruction::IntToPtr:
1083 // Look past no-op inttoptrs if its operand is in the same BB.
1084 if (InMBB &&
Mehdi Amini44ede332015-07-09 02:09:04 +00001085 TLI.getValueType(DL, U->getOperand(0)->getType()) ==
1086 TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001087 return X86SelectCallAddress(U->getOperand(0), AM);
1088 break;
1089
1090 case Instruction::PtrToInt:
1091 // Look past no-op ptrtoints if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +00001092 if (InMBB && TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001093 return X86SelectCallAddress(U->getOperand(0), AM);
1094 break;
1095 }
1096
1097 // Handle constant address.
1098 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1099 // Can't handle alternate code models yet.
1100 if (TM.getCodeModel() != CodeModel::Small)
1101 return false;
1102
1103 // RIP-relative addresses can't have additional register operands.
1104 if (Subtarget->isPICStyleRIPRel() &&
1105 (AM.Base.Reg != 0 || AM.IndexReg != 0))
1106 return false;
1107
1108 // Can't handle DLL Import.
1109 if (GV->hasDLLImportStorageClass())
1110 return false;
1111
1112 // Can't handle TLS.
1113 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1114 if (GVar->isThreadLocal())
1115 return false;
1116
1117 // Okay, we've committed to selecting this global. Set up the basic address.
1118 AM.GV = GV;
1119
1120 // No ABI requires an extra load for anything other than DLLImport, which
1121 // we rejected above. Return a direct reference to the global.
1122 if (Subtarget->isPICStyleRIPRel()) {
1123 // Use rip-relative addressing if we can. Above we verified that the
1124 // base and index registers are unused.
1125 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
1126 AM.Base.Reg = X86::RIP;
Rafael Espindolac7e98132016-05-20 12:20:10 +00001127 } else {
1128 AM.GVOpFlags = Subtarget->classifyLocalReference(nullptr);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001129 }
1130
1131 return true;
1132 }
1133
1134 // If all else fails, try to materialize the value in a register.
1135 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
1136 if (AM.Base.Reg == 0) {
1137 AM.Base.Reg = getRegForValue(V);
1138 return AM.Base.Reg != 0;
1139 }
1140 if (AM.IndexReg == 0) {
1141 assert(AM.Scale == 1 && "Scale with no index!");
1142 AM.IndexReg = getRegForValue(V);
1143 return AM.IndexReg != 0;
1144 }
1145 }
1146
1147 return false;
1148}
1149
1150
1151/// X86SelectStore - Select and emit code to implement store instructions.
1152bool X86FastISel::X86SelectStore(const Instruction *I) {
1153 // Atomic stores need special handling.
1154 const StoreInst *S = cast<StoreInst>(I);
1155
1156 if (S->isAtomic())
1157 return false;
1158
Manman Ren57518142016-04-11 21:08:06 +00001159 const Value *PtrV = I->getOperand(1);
1160 if (TLI.supportSwiftError()) {
1161 // Swifterror values can come from either a function parameter with
1162 // swifterror attribute or an alloca with swifterror attribute.
1163 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
1164 if (Arg->hasSwiftErrorAttr())
1165 return false;
1166 }
1167
1168 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
1169 if (Alloca->isSwiftError())
1170 return false;
1171 }
1172 }
1173
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001174 const Value *Val = S->getValueOperand();
1175 const Value *Ptr = S->getPointerOperand();
1176
1177 MVT VT;
1178 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
1179 return false;
1180
1181 unsigned Alignment = S->getAlignment();
1182 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
1183 if (Alignment == 0) // Ensure that codegen never sees alignment 0
1184 Alignment = ABIAlignment;
1185 bool Aligned = Alignment >= ABIAlignment;
1186
1187 X86AddressMode AM;
1188 if (!X86SelectAddress(Ptr, AM))
1189 return false;
1190
1191 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
1192}
1193
1194/// X86SelectRet - Select and emit code to implement ret instructions.
1195bool X86FastISel::X86SelectRet(const Instruction *I) {
1196 const ReturnInst *Ret = cast<ReturnInst>(I);
1197 const Function &F = *I->getParent()->getParent();
1198 const X86MachineFunctionInfo *X86MFInfo =
1199 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
1200
1201 if (!FuncInfo.CanLowerReturn)
1202 return false;
1203
Manman Ren57518142016-04-11 21:08:06 +00001204 if (TLI.supportSwiftError() &&
1205 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError))
1206 return false;
1207
Manman Rened967f32016-01-12 01:08:46 +00001208 if (TLI.supportSplitCSR(FuncInfo.MF))
1209 return false;
1210
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001211 CallingConv::ID CC = F.getCallingConv();
1212 if (CC != CallingConv::C &&
1213 CC != CallingConv::Fast &&
1214 CC != CallingConv::X86_FastCall &&
Nico Weberecdf45b2016-07-14 13:54:26 +00001215 CC != CallingConv::X86_StdCall &&
Nico Weberc7bf6462016-07-12 01:30:35 +00001216 CC != CallingConv::X86_ThisCall &&
Nico Weber8d66df12016-07-15 20:18:37 +00001217 CC != CallingConv::X86_64_SysV &&
1218 CC != CallingConv::X86_64_Win64)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001219 return false;
1220
Nico Weberc7bf6462016-07-12 01:30:35 +00001221 // Don't handle popping bytes if they don't fit the ret's immediate.
1222 if (!isUInt<16>(X86MFInfo->getBytesToPopOnReturn()))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001223 return false;
1224
1225 // fastcc with -tailcallopt is intended to provide a guaranteed
1226 // tail call optimization. Fastisel doesn't know how to do that.
1227 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
1228 return false;
1229
1230 // Let SDISel handle vararg functions.
1231 if (F.isVarArg())
1232 return false;
1233
1234 // Build a list of return value registers.
1235 SmallVector<unsigned, 4> RetRegs;
1236
1237 if (Ret->getNumOperands() > 0) {
1238 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini44ede332015-07-09 02:09:04 +00001239 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001240
1241 // Analyze operands of the call, assigning locations to each operand.
1242 SmallVector<CCValAssign, 16> ValLocs;
1243 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
1244 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
1245
1246 const Value *RV = Ret->getOperand(0);
1247 unsigned Reg = getRegForValue(RV);
1248 if (Reg == 0)
1249 return false;
1250
1251 // Only handle a single return value for now.
1252 if (ValLocs.size() != 1)
1253 return false;
1254
1255 CCValAssign &VA = ValLocs[0];
1256
1257 // Don't bother handling odd stuff for now.
1258 if (VA.getLocInfo() != CCValAssign::Full)
1259 return false;
1260 // Only handle register returns for now.
1261 if (!VA.isRegLoc())
1262 return false;
1263
1264 // The calling-convention tables for x87 returns don't tell
1265 // the whole story.
1266 if (VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1)
1267 return false;
1268
1269 unsigned SrcReg = Reg + VA.getValNo();
Mehdi Amini44ede332015-07-09 02:09:04 +00001270 EVT SrcVT = TLI.getValueType(DL, RV->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001271 EVT DstVT = VA.getValVT();
1272 // Special handling for extended integers.
1273 if (SrcVT != DstVT) {
1274 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
1275 return false;
1276
1277 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1278 return false;
1279
1280 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
1281
1282 if (SrcVT == MVT::i1) {
1283 if (Outs[0].Flags.isSExt())
1284 return false;
Craig Topper9d50e182017-03-14 04:18:25 +00001285 // In case SrcReg is a K register, COPY to a GPR
1286 if (MRI.getRegClass(SrcReg) == &X86::VK1RegClass) {
1287 unsigned KSrcReg = SrcReg;
Craig Topper058f2f62017-03-28 16:35:29 +00001288 SrcReg = createResultReg(&X86::GR32RegClass);
Craig Topper9d50e182017-03-14 04:18:25 +00001289 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1290 TII.get(TargetOpcode::COPY), SrcReg)
1291 .addReg(KSrcReg);
Craig Topper058f2f62017-03-28 16:35:29 +00001292 SrcReg = fastEmitInst_extractsubreg(MVT::i8, SrcReg, /*Kill=*/true,
1293 X86::sub_8bit);
Craig Topper9d50e182017-03-14 04:18:25 +00001294 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001295 SrcReg = fastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
1296 SrcVT = MVT::i8;
1297 }
1298 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
1299 ISD::SIGN_EXTEND;
1300 SrcReg = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
1301 SrcReg, /*TODO: Kill=*/false);
1302 }
1303
1304 // Make the copy.
1305 unsigned DstReg = VA.getLocReg();
1306 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
1307 // Avoid a cross-class copy. This is very unlikely.
1308 if (!SrcRC->contains(DstReg))
1309 return false;
1310 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1311 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
1312
1313 // Add register to return instruction.
1314 RetRegs.push_back(VA.getLocReg());
1315 }
1316
Manman Ren1c3f65a2016-04-26 18:08:06 +00001317 // Swift calling convention does not require we copy the sret argument
1318 // into %rax/%eax for the return, and SRetReturnReg is not set for Swift.
1319
Dimitry Andric227b9282016-01-03 17:22:03 +00001320 // All x86 ABIs require that for returning structs by value we copy
1321 // the sret argument into %rax/%eax (depending on ABI) for the return.
1322 // We saved the argument into a virtual register in the entry block,
Michael Kuperstein2ea81ba2015-12-28 14:39:21 +00001323 // so now we copy the value out and into %rax/%eax.
Manman Ren1c3f65a2016-04-26 18:08:06 +00001324 if (F.hasStructRetAttr() && CC != CallingConv::Swift) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001325 unsigned Reg = X86MFInfo->getSRetReturnReg();
1326 assert(Reg &&
1327 "SRetReturnReg should have been set in LowerFormalArguments()!");
1328 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
1329 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1330 TII.get(TargetOpcode::COPY), RetReg).addReg(Reg);
1331 RetRegs.push_back(RetReg);
1332 }
1333
1334 // Now emit the RET.
Nico Weberc7bf6462016-07-12 01:30:35 +00001335 MachineInstrBuilder MIB;
1336 if (X86MFInfo->getBytesToPopOnReturn()) {
1337 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1338 TII.get(Subtarget->is64Bit() ? X86::RETIQ : X86::RETIL))
1339 .addImm(X86MFInfo->getBytesToPopOnReturn());
1340 } else {
1341 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1342 TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
1343 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001344 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1345 MIB.addReg(RetRegs[i], RegState::Implicit);
1346 return true;
1347}
1348
1349/// X86SelectLoad - Select and emit code to implement load instructions.
1350///
1351bool X86FastISel::X86SelectLoad(const Instruction *I) {
1352 const LoadInst *LI = cast<LoadInst>(I);
1353
1354 // Atomic loads need special handling.
1355 if (LI->isAtomic())
1356 return false;
1357
Manman Ren57518142016-04-11 21:08:06 +00001358 const Value *SV = I->getOperand(0);
1359 if (TLI.supportSwiftError()) {
1360 // Swifterror values can come from either a function parameter with
1361 // swifterror attribute or an alloca with swifterror attribute.
1362 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
1363 if (Arg->hasSwiftErrorAttr())
1364 return false;
1365 }
1366
1367 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
1368 if (Alloca->isSwiftError())
1369 return false;
1370 }
1371 }
1372
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001373 MVT VT;
1374 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
1375 return false;
1376
1377 const Value *Ptr = LI->getPointerOperand();
1378
1379 X86AddressMode AM;
1380 if (!X86SelectAddress(Ptr, AM))
1381 return false;
1382
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +00001383 unsigned Alignment = LI->getAlignment();
1384 unsigned ABIAlignment = DL.getABITypeAlignment(LI->getType());
1385 if (Alignment == 0) // Ensure that codegen never sees alignment 0
1386 Alignment = ABIAlignment;
1387
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001388 unsigned ResultReg = 0;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +00001389 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg,
1390 Alignment))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001391 return false;
1392
1393 updateValueMap(I, ResultReg);
1394 return true;
1395}
1396
1397static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
1398 bool HasAVX = Subtarget->hasAVX();
1399 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
1400 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
1401
1402 switch (VT.getSimpleVT().SimpleTy) {
1403 default: return 0;
1404 case MVT::i8: return X86::CMP8rr;
1405 case MVT::i16: return X86::CMP16rr;
1406 case MVT::i32: return X86::CMP32rr;
1407 case MVT::i64: return X86::CMP64rr;
1408 case MVT::f32:
1409 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
1410 case MVT::f64:
1411 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
1412 }
1413}
1414
Rafael Espindola19141f22015-03-16 14:05:49 +00001415/// If we have a comparison with RHS as the RHS of the comparison, return an
1416/// opcode that works for the compare (e.g. CMP32ri) otherwise return 0.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001417static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Rafael Espindola933f51a2015-03-16 14:25:08 +00001418 int64_t Val = RHSC->getSExtValue();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001419 switch (VT.getSimpleVT().SimpleTy) {
1420 // Otherwise, we can't fold the immediate into this comparison.
Rafael Espindola19141f22015-03-16 14:05:49 +00001421 default:
1422 return 0;
1423 case MVT::i8:
1424 return X86::CMP8ri;
1425 case MVT::i16:
Rafael Espindola933f51a2015-03-16 14:25:08 +00001426 if (isInt<8>(Val))
1427 return X86::CMP16ri8;
Rafael Espindola19141f22015-03-16 14:05:49 +00001428 return X86::CMP16ri;
1429 case MVT::i32:
Rafael Espindola933f51a2015-03-16 14:25:08 +00001430 if (isInt<8>(Val))
1431 return X86::CMP32ri8;
Rafael Espindola19141f22015-03-16 14:05:49 +00001432 return X86::CMP32ri;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001433 case MVT::i64:
Rafael Espindola933f51a2015-03-16 14:25:08 +00001434 if (isInt<8>(Val))
1435 return X86::CMP64ri8;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001436 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1437 // field.
Rafael Espindola933f51a2015-03-16 14:25:08 +00001438 if (isInt<32>(Val))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001439 return X86::CMP64ri32;
1440 return 0;
1441 }
1442}
1443
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001444bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1, EVT VT,
1445 const DebugLoc &CurDbgLoc) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001446 unsigned Op0Reg = getRegForValue(Op0);
1447 if (Op0Reg == 0) return false;
1448
1449 // Handle 'null' like i32/i64 0.
1450 if (isa<ConstantPointerNull>(Op1))
1451 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
1452
1453 // We have two options: compare with register or immediate. If the RHS of
1454 // the compare is an immediate that we can fold into this compare, use
1455 // CMPri, otherwise use CMPrr.
1456 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1457 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
1458 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, CurDbgLoc, TII.get(CompareImmOpc))
1459 .addReg(Op0Reg)
1460 .addImm(Op1C->getSExtValue());
1461 return true;
1462 }
1463 }
1464
1465 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
1466 if (CompareOpc == 0) return false;
1467
1468 unsigned Op1Reg = getRegForValue(Op1);
1469 if (Op1Reg == 0) return false;
1470 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, CurDbgLoc, TII.get(CompareOpc))
1471 .addReg(Op0Reg)
1472 .addReg(Op1Reg);
1473
1474 return true;
1475}
1476
1477bool X86FastISel::X86SelectCmp(const Instruction *I) {
1478 const CmpInst *CI = cast<CmpInst>(I);
1479
1480 MVT VT;
1481 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
1482 return false;
1483
Elena Demikhovskyad0a56f2016-07-06 14:15:43 +00001484 if (I->getType()->isIntegerTy(1) && Subtarget->hasAVX512())
1485 return false;
1486
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001487 // Try to optimize or fold the cmp.
1488 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1489 unsigned ResultReg = 0;
1490 switch (Predicate) {
1491 default: break;
1492 case CmpInst::FCMP_FALSE: {
1493 ResultReg = createResultReg(&X86::GR32RegClass);
1494 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0),
1495 ResultReg);
1496 ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1497 X86::sub_8bit);
1498 if (!ResultReg)
1499 return false;
1500 break;
1501 }
1502 case CmpInst::FCMP_TRUE: {
1503 ResultReg = createResultReg(&X86::GR8RegClass);
1504 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
1505 ResultReg).addImm(1);
1506 break;
1507 }
1508 }
1509
1510 if (ResultReg) {
1511 updateValueMap(I, ResultReg);
1512 return true;
1513 }
1514
1515 const Value *LHS = CI->getOperand(0);
1516 const Value *RHS = CI->getOperand(1);
1517
1518 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1519 // We don't have to materialize a zero constant for this case and can just use
1520 // %x again on the RHS.
1521 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1522 const auto *RHSC = dyn_cast<ConstantFP>(RHS);
1523 if (RHSC && RHSC->isNullValue())
1524 RHS = LHS;
1525 }
1526
1527 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Craig Topper428169a2016-09-05 07:14:21 +00001528 static const uint16_t SETFOpcTable[2][3] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001529 { X86::SETEr, X86::SETNPr, X86::AND8rr },
1530 { X86::SETNEr, X86::SETPr, X86::OR8rr }
1531 };
Craig Topper428169a2016-09-05 07:14:21 +00001532 const uint16_t *SETFOpc = nullptr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001533 switch (Predicate) {
1534 default: break;
1535 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1536 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1537 }
1538
1539 ResultReg = createResultReg(&X86::GR8RegClass);
1540 if (SETFOpc) {
1541 if (!X86FastEmitCompare(LHS, RHS, VT, I->getDebugLoc()))
1542 return false;
1543
1544 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1545 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1546 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1547 FlagReg1);
1548 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1549 FlagReg2);
1550 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
1551 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
1552 updateValueMap(I, ResultReg);
1553 return true;
1554 }
1555
1556 X86::CondCode CC;
1557 bool SwapArgs;
1558 std::tie(CC, SwapArgs) = getX86ConditionCode(Predicate);
1559 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
1560 unsigned Opc = X86::getSETFromCond(CC);
1561
1562 if (SwapArgs)
1563 std::swap(LHS, RHS);
1564
1565 // Emit a compare of LHS/RHS.
1566 if (!X86FastEmitCompare(LHS, RHS, VT, I->getDebugLoc()))
1567 return false;
1568
1569 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
1570 updateValueMap(I, ResultReg);
1571 return true;
1572}
1573
1574bool X86FastISel::X86SelectZExt(const Instruction *I) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001575 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001576 if (!TLI.isTypeLegal(DstVT))
1577 return false;
1578
1579 unsigned ResultReg = getRegForValue(I->getOperand(0));
1580 if (ResultReg == 0)
1581 return false;
1582
1583 // Handle zero-extension from i1 to i8, which is common.
Mehdi Amini44ede332015-07-09 02:09:04 +00001584 MVT SrcVT = TLI.getSimpleValueType(DL, I->getOperand(0)->getType());
Craig Topper088ba172016-12-05 06:09:55 +00001585 if (SrcVT == MVT::i1) {
Craig Topper9d50e182017-03-14 04:18:25 +00001586 // In case ResultReg is a K register, COPY to a GPR
1587 if (MRI.getRegClass(ResultReg) == &X86::VK1RegClass) {
1588 unsigned KResultReg = ResultReg;
Craig Topper058f2f62017-03-28 16:35:29 +00001589 ResultReg = createResultReg(&X86::GR32RegClass);
Craig Topper58647b12017-03-12 03:37:37 +00001590 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1591 TII.get(TargetOpcode::COPY), ResultReg)
Craig Topper9d50e182017-03-14 04:18:25 +00001592 .addReg(KResultReg);
Craig Topper058f2f62017-03-28 16:35:29 +00001593 ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1594 X86::sub_8bit);
Craig Topper58647b12017-03-12 03:37:37 +00001595 }
1596
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001597 // Set the high bits to zero.
1598 ResultReg = fastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1599 SrcVT = MVT::i8;
1600
1601 if (ResultReg == 0)
1602 return false;
1603 }
1604
1605 if (DstVT == MVT::i64) {
1606 // Handle extension to 64-bits via sub-register shenanigans.
1607 unsigned MovInst;
1608
1609 switch (SrcVT.SimpleTy) {
1610 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1611 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1612 case MVT::i32: MovInst = X86::MOV32rr; break;
1613 default: llvm_unreachable("Unexpected zext to i64 source type");
1614 }
1615
1616 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1617 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
1618 .addReg(ResultReg);
1619
1620 ResultReg = createResultReg(&X86::GR64RegClass);
1621 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
1622 ResultReg)
1623 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1624 } else if (DstVT != MVT::i8) {
1625 ResultReg = fastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1626 ResultReg, /*Kill=*/true);
1627 if (ResultReg == 0)
1628 return false;
1629 }
1630
1631 updateValueMap(I, ResultReg);
1632 return true;
1633}
1634
1635bool X86FastISel::X86SelectBranch(const Instruction *I) {
1636 // Unconditional branches are selected by tablegen-generated code.
1637 // Handle a conditional branch.
1638 const BranchInst *BI = cast<BranchInst>(I);
1639 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1640 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1641
1642 // Fold the common case of a conditional branch with a comparison
1643 // in the same block (values defined on other blocks may not have
1644 // initialized registers).
1645 X86::CondCode CC;
1646 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1647 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001648 EVT VT = TLI.getValueType(DL, CI->getOperand(0)->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001649
1650 // Try to optimize or fold the cmp.
1651 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1652 switch (Predicate) {
1653 default: break;
1654 case CmpInst::FCMP_FALSE: fastEmitBranch(FalseMBB, DbgLoc); return true;
1655 case CmpInst::FCMP_TRUE: fastEmitBranch(TrueMBB, DbgLoc); return true;
1656 }
1657
1658 const Value *CmpLHS = CI->getOperand(0);
1659 const Value *CmpRHS = CI->getOperand(1);
1660
1661 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x,
1662 // 0.0.
1663 // We don't have to materialize a zero constant for this case and can just
1664 // use %x again on the RHS.
1665 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1666 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1667 if (CmpRHSC && CmpRHSC->isNullValue())
1668 CmpRHS = CmpLHS;
1669 }
1670
1671 // Try to take advantage of fallthrough opportunities.
1672 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1673 std::swap(TrueMBB, FalseMBB);
1674 Predicate = CmpInst::getInversePredicate(Predicate);
1675 }
1676
1677 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/condition
1678 // code check. Instead two branch instructions are required to check all
1679 // the flags. First we change the predicate to a supported condition code,
1680 // which will be the first branch. Later one we will emit the second
1681 // branch.
1682 bool NeedExtraBranch = false;
1683 switch (Predicate) {
1684 default: break;
1685 case CmpInst::FCMP_OEQ:
Justin Bognerb03fd122016-08-17 05:10:15 +00001686 std::swap(TrueMBB, FalseMBB);
1687 LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001688 case CmpInst::FCMP_UNE:
1689 NeedExtraBranch = true;
1690 Predicate = CmpInst::FCMP_ONE;
1691 break;
1692 }
1693
1694 bool SwapArgs;
1695 unsigned BranchOpc;
1696 std::tie(CC, SwapArgs) = getX86ConditionCode(Predicate);
1697 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
1698
1699 BranchOpc = X86::GetCondBranchFromCond(CC);
1700 if (SwapArgs)
1701 std::swap(CmpLHS, CmpRHS);
1702
1703 // Emit a compare of the LHS and RHS, setting the flags.
1704 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT, CI->getDebugLoc()))
1705 return false;
1706
1707 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
1708 .addMBB(TrueMBB);
1709
1710 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1711 // to UNE above).
1712 if (NeedExtraBranch) {
1713 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_1))
1714 .addMBB(TrueMBB);
1715 }
1716
Matthias Braun17af6072015-08-26 01:38:00 +00001717 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001718 return true;
1719 }
1720 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1721 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1722 // typically happen for _Bool and C++ bools.
1723 MVT SourceVT;
1724 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1725 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1726 unsigned TestOpc = 0;
1727 switch (SourceVT.SimpleTy) {
1728 default: break;
1729 case MVT::i8: TestOpc = X86::TEST8ri; break;
1730 case MVT::i16: TestOpc = X86::TEST16ri; break;
1731 case MVT::i32: TestOpc = X86::TEST32ri; break;
1732 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1733 }
1734 if (TestOpc) {
1735 unsigned OpReg = getRegForValue(TI->getOperand(0));
1736 if (OpReg == 0) return false;
Guy Blank9ae797a2016-08-21 08:02:27 +00001737
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001738 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
1739 .addReg(OpReg).addImm(1);
1740
1741 unsigned JmpOpc = X86::JNE_1;
1742 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1743 std::swap(TrueMBB, FalseMBB);
1744 JmpOpc = X86::JE_1;
1745 }
1746
1747 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
1748 .addMBB(TrueMBB);
Matthias Braun17af6072015-08-26 01:38:00 +00001749
1750 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001751 return true;
1752 }
1753 }
1754 } else if (foldX86XALUIntrinsic(CC, BI, BI->getCondition())) {
1755 // Fake request the condition, otherwise the intrinsic might be completely
1756 // optimized away.
1757 unsigned TmpReg = getRegForValue(BI->getCondition());
1758 if (TmpReg == 0)
1759 return false;
1760
1761 unsigned BranchOpc = X86::GetCondBranchFromCond(CC);
1762
1763 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
1764 .addMBB(TrueMBB);
Matthias Braun17af6072015-08-26 01:38:00 +00001765 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001766 return true;
1767 }
1768
1769 // Otherwise do a clumsy setcc and re-test it.
1770 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1771 // in an explicit cast, so make sure to handle that correctly.
1772 unsigned OpReg = getRegForValue(BI->getCondition());
1773 if (OpReg == 0) return false;
1774
Guy Blank2bdc74a2016-09-28 11:22:17 +00001775 // In case OpReg is a K register, COPY to a GPR
1776 if (MRI.getRegClass(OpReg) == &X86::VK1RegClass) {
1777 unsigned KOpReg = OpReg;
Craig Topper058f2f62017-03-28 16:35:29 +00001778 OpReg = createResultReg(&X86::GR32RegClass);
Guy Blank2bdc74a2016-09-28 11:22:17 +00001779 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1780 TII.get(TargetOpcode::COPY), OpReg)
1781 .addReg(KOpReg);
Craig Topper058f2f62017-03-28 16:35:29 +00001782 OpReg = fastEmitInst_extractsubreg(MVT::i8, OpReg, /*Kill=*/true,
1783 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00001784 }
1785 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1786 .addReg(OpReg)
1787 .addImm(1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001788 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_1))
1789 .addMBB(TrueMBB);
Matthias Braun17af6072015-08-26 01:38:00 +00001790 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001791 return true;
1792}
1793
1794bool X86FastISel::X86SelectShift(const Instruction *I) {
1795 unsigned CReg = 0, OpReg = 0;
1796 const TargetRegisterClass *RC = nullptr;
1797 if (I->getType()->isIntegerTy(8)) {
1798 CReg = X86::CL;
1799 RC = &X86::GR8RegClass;
1800 switch (I->getOpcode()) {
1801 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1802 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1803 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
1804 default: return false;
1805 }
1806 } else if (I->getType()->isIntegerTy(16)) {
1807 CReg = X86::CX;
1808 RC = &X86::GR16RegClass;
1809 switch (I->getOpcode()) {
1810 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1811 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1812 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
1813 default: return false;
1814 }
1815 } else if (I->getType()->isIntegerTy(32)) {
1816 CReg = X86::ECX;
1817 RC = &X86::GR32RegClass;
1818 switch (I->getOpcode()) {
1819 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1820 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1821 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
1822 default: return false;
1823 }
1824 } else if (I->getType()->isIntegerTy(64)) {
1825 CReg = X86::RCX;
1826 RC = &X86::GR64RegClass;
1827 switch (I->getOpcode()) {
1828 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1829 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1830 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
1831 default: return false;
1832 }
1833 } else {
1834 return false;
1835 }
1836
1837 MVT VT;
1838 if (!isTypeLegal(I->getType(), VT))
1839 return false;
1840
1841 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1842 if (Op0Reg == 0) return false;
1843
1844 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1845 if (Op1Reg == 0) return false;
1846 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
1847 CReg).addReg(Op1Reg);
1848
1849 // The shift instruction uses X86::CL. If we defined a super-register
1850 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
1851 if (CReg != X86::CL)
1852 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1853 TII.get(TargetOpcode::KILL), X86::CL)
1854 .addReg(CReg, RegState::Kill);
1855
1856 unsigned ResultReg = createResultReg(RC);
1857 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
1858 .addReg(Op0Reg);
1859 updateValueMap(I, ResultReg);
1860 return true;
1861}
1862
1863bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1864 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1865 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1866 const static bool S = true; // IsSigned
1867 const static bool U = false; // !IsSigned
1868 const static unsigned Copy = TargetOpcode::COPY;
1869 // For the X86 DIV/IDIV instruction, in most cases the dividend
1870 // (numerator) must be in a specific register pair highreg:lowreg,
1871 // producing the quotient in lowreg and the remainder in highreg.
1872 // For most data types, to set up the instruction, the dividend is
1873 // copied into lowreg, and lowreg is sign-extended or zero-extended
1874 // into highreg. The exception is i8, where the dividend is defined
1875 // as a single register rather than a register pair, and we
1876 // therefore directly sign-extend or zero-extend the dividend into
1877 // lowreg, instead of copying, and ignore the highreg.
1878 const static struct DivRemEntry {
1879 // The following portion depends only on the data type.
1880 const TargetRegisterClass *RC;
1881 unsigned LowInReg; // low part of the register pair
1882 unsigned HighInReg; // high part of the register pair
1883 // The following portion depends on both the data type and the operation.
1884 struct DivRemResult {
1885 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1886 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1887 // highreg, or copying a zero into highreg.
1888 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1889 // zero/sign-extending into lowreg for i8.
1890 unsigned DivRemResultReg; // Register containing the desired result.
1891 bool IsOpSigned; // Whether to use signed or unsigned form.
1892 } ResultTable[NumOps];
1893 } OpTable[NumTypes] = {
1894 { &X86::GR8RegClass, X86::AX, 0, {
1895 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1896 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1897 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1898 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1899 }
1900 }, // i8
1901 { &X86::GR16RegClass, X86::AX, X86::DX, {
1902 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1903 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
1904 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1905 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
1906 }
1907 }, // i16
1908 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1909 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1910 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1911 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1912 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1913 }
1914 }, // i32
1915 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1916 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1917 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
1918 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1919 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
1920 }
1921 }, // i64
1922 };
1923
1924 MVT VT;
1925 if (!isTypeLegal(I->getType(), VT))
1926 return false;
1927
1928 unsigned TypeIndex, OpIndex;
1929 switch (VT.SimpleTy) {
1930 default: return false;
1931 case MVT::i8: TypeIndex = 0; break;
1932 case MVT::i16: TypeIndex = 1; break;
1933 case MVT::i32: TypeIndex = 2; break;
1934 case MVT::i64: TypeIndex = 3;
1935 if (!Subtarget->is64Bit())
1936 return false;
1937 break;
1938 }
1939
1940 switch (I->getOpcode()) {
1941 default: llvm_unreachable("Unexpected div/rem opcode");
1942 case Instruction::SDiv: OpIndex = 0; break;
1943 case Instruction::SRem: OpIndex = 1; break;
1944 case Instruction::UDiv: OpIndex = 2; break;
1945 case Instruction::URem: OpIndex = 3; break;
1946 }
1947
1948 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1949 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1950 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1951 if (Op0Reg == 0)
1952 return false;
1953 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1954 if (Op1Reg == 0)
1955 return false;
1956
1957 // Move op0 into low-order input register.
1958 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1959 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1960 // Zero-extend or sign-extend into high-order input register.
1961 if (OpEntry.OpSignExtend) {
1962 if (OpEntry.IsOpSigned)
1963 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1964 TII.get(OpEntry.OpSignExtend));
1965 else {
1966 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
1967 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1968 TII.get(X86::MOV32r0), Zero32);
1969
1970 // Copy the zero into the appropriate sub/super/identical physical
1971 // register. Unfortunately the operations needed are not uniform enough
1972 // to fit neatly into the table above.
Craig Topper088ba172016-12-05 06:09:55 +00001973 if (VT == MVT::i16) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001974 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1975 TII.get(Copy), TypeEntry.HighInReg)
1976 .addReg(Zero32, 0, X86::sub_16bit);
Craig Topper088ba172016-12-05 06:09:55 +00001977 } else if (VT == MVT::i32) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001978 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1979 TII.get(Copy), TypeEntry.HighInReg)
1980 .addReg(Zero32);
Craig Topper088ba172016-12-05 06:09:55 +00001981 } else if (VT == MVT::i64) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001982 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1983 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1984 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1985 }
1986 }
1987 }
1988 // Generate the DIV/IDIV instruction.
1989 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1990 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
1991 // For i8 remainder, we can't reference AH directly, as we'll end
1992 // up with bogus copies like %R9B = COPY %AH. Reference AX
1993 // instead to prevent AH references in a REX instruction.
1994 //
1995 // The current assumption of the fast register allocator is that isel
1996 // won't generate explicit references to the GPR8_NOREX registers. If
1997 // the allocator and/or the backend get enhanced to be more robust in
1998 // that regard, this can be, and should be, removed.
1999 unsigned ResultReg = 0;
2000 if ((I->getOpcode() == Instruction::SRem ||
2001 I->getOpcode() == Instruction::URem) &&
2002 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
2003 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
2004 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
2005 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2006 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
2007
2008 // Shift AX right by 8 bits instead of using AH.
2009 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
2010 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
2011
2012 // Now reference the 8-bit subreg of the result.
2013 ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
2014 /*Kill=*/true, X86::sub_8bit);
2015 }
2016 // Copy the result out of the physreg if we haven't already.
2017 if (!ResultReg) {
2018 ResultReg = createResultReg(TypeEntry.RC);
2019 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
2020 .addReg(OpEntry.DivRemResultReg);
2021 }
2022 updateValueMap(I, ResultReg);
2023
2024 return true;
2025}
2026
2027/// \brief Emit a conditional move instruction (if the are supported) to lower
2028/// the select.
2029bool X86FastISel::X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I) {
2030 // Check if the subtarget supports these instructions.
2031 if (!Subtarget->hasCMov())
2032 return false;
2033
2034 // FIXME: Add support for i8.
2035 if (RetVT < MVT::i16 || RetVT > MVT::i64)
2036 return false;
2037
2038 const Value *Cond = I->getOperand(0);
2039 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2040 bool NeedTest = true;
2041 X86::CondCode CC = X86::COND_NE;
2042
2043 // Optimize conditions coming from a compare if both instructions are in the
2044 // same basic block (values defined in other basic blocks may not have
2045 // initialized registers).
2046 const auto *CI = dyn_cast<CmpInst>(Cond);
2047 if (CI && (CI->getParent() == I->getParent())) {
2048 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2049
2050 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Craig Topper428169a2016-09-05 07:14:21 +00002051 static const uint16_t SETFOpcTable[2][3] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002052 { X86::SETNPr, X86::SETEr , X86::TEST8rr },
2053 { X86::SETPr, X86::SETNEr, X86::OR8rr }
2054 };
Craig Topper428169a2016-09-05 07:14:21 +00002055 const uint16_t *SETFOpc = nullptr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002056 switch (Predicate) {
2057 default: break;
2058 case CmpInst::FCMP_OEQ:
2059 SETFOpc = &SETFOpcTable[0][0];
2060 Predicate = CmpInst::ICMP_NE;
2061 break;
2062 case CmpInst::FCMP_UNE:
2063 SETFOpc = &SETFOpcTable[1][0];
2064 Predicate = CmpInst::ICMP_NE;
2065 break;
2066 }
2067
2068 bool NeedSwap;
2069 std::tie(CC, NeedSwap) = getX86ConditionCode(Predicate);
2070 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
2071
2072 const Value *CmpLHS = CI->getOperand(0);
2073 const Value *CmpRHS = CI->getOperand(1);
2074 if (NeedSwap)
2075 std::swap(CmpLHS, CmpRHS);
2076
Mehdi Amini44ede332015-07-09 02:09:04 +00002077 EVT CmpVT = TLI.getValueType(DL, CmpLHS->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002078 // Emit a compare of the LHS and RHS, setting the flags.
2079 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT, CI->getDebugLoc()))
2080 return false;
2081
2082 if (SETFOpc) {
2083 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
2084 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
2085 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
2086 FlagReg1);
2087 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
2088 FlagReg2);
2089 auto const &II = TII.get(SETFOpc[2]);
2090 if (II.getNumDefs()) {
2091 unsigned TmpReg = createResultReg(&X86::GR8RegClass);
2092 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, TmpReg)
2093 .addReg(FlagReg2).addReg(FlagReg1);
2094 } else {
2095 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
2096 .addReg(FlagReg2).addReg(FlagReg1);
2097 }
2098 }
2099 NeedTest = false;
2100 } else if (foldX86XALUIntrinsic(CC, I, Cond)) {
2101 // Fake request the condition, otherwise the intrinsic might be completely
2102 // optimized away.
2103 unsigned TmpReg = getRegForValue(Cond);
2104 if (TmpReg == 0)
2105 return false;
2106
2107 NeedTest = false;
2108 }
2109
2110 if (NeedTest) {
2111 // Selects operate on i1, however, CondReg is 8 bits width and may contain
2112 // garbage. Indeed, only the less significant bit is supposed to be
2113 // accurate. If we read more than the lsb, we may see non-zero values
2114 // whereas lsb is zero. Therefore, we have to truncate Op0Reg to i1 for
2115 // the select. This is achieved by performing TEST against 1.
2116 unsigned CondReg = getRegForValue(Cond);
2117 if (CondReg == 0)
2118 return false;
2119 bool CondIsKill = hasTrivialKill(Cond);
2120
Guy Blank2bdc74a2016-09-28 11:22:17 +00002121 // In case OpReg is a K register, COPY to a GPR
2122 if (MRI.getRegClass(CondReg) == &X86::VK1RegClass) {
2123 unsigned KCondReg = CondReg;
Craig Topper058f2f62017-03-28 16:35:29 +00002124 CondReg = createResultReg(&X86::GR32RegClass);
Guy Blank9ae797a2016-08-21 08:02:27 +00002125 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Guy Blank2bdc74a2016-09-28 11:22:17 +00002126 TII.get(TargetOpcode::COPY), CondReg)
2127 .addReg(KCondReg, getKillRegState(CondIsKill));
Craig Topper058f2f62017-03-28 16:35:29 +00002128 CondReg = fastEmitInst_extractsubreg(MVT::i8, CondReg, /*Kill=*/true,
2129 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00002130 }
2131 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
2132 .addReg(CondReg, getKillRegState(CondIsKill))
2133 .addImm(1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002134 }
2135
2136 const Value *LHS = I->getOperand(1);
2137 const Value *RHS = I->getOperand(2);
2138
2139 unsigned RHSReg = getRegForValue(RHS);
2140 bool RHSIsKill = hasTrivialKill(RHS);
2141
2142 unsigned LHSReg = getRegForValue(LHS);
2143 bool LHSIsKill = hasTrivialKill(LHS);
2144
2145 if (!LHSReg || !RHSReg)
2146 return false;
2147
2148 unsigned Opc = X86::getCMovFromCond(CC, RC->getSize());
2149 unsigned ResultReg = fastEmitInst_rr(Opc, RC, RHSReg, RHSIsKill,
2150 LHSReg, LHSIsKill);
2151 updateValueMap(I, ResultReg);
2152 return true;
2153}
2154
Sanjay Patel302404b2015-03-05 21:46:54 +00002155/// \brief Emit SSE or AVX instructions to lower the select.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002156///
2157/// Try to use SSE1/SSE2 instructions to simulate a select without branches.
2158/// This lowers fp selects into a CMP/AND/ANDN/OR sequence when the necessary
Sanjay Patel302404b2015-03-05 21:46:54 +00002159/// SSE instructions are available. If AVX is available, try to use a VBLENDV.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002160bool X86FastISel::X86FastEmitSSESelect(MVT RetVT, const Instruction *I) {
2161 // Optimize conditions coming from a compare if both instructions are in the
2162 // same basic block (values defined in other basic blocks may not have
2163 // initialized registers).
2164 const auto *CI = dyn_cast<FCmpInst>(I->getOperand(0));
2165 if (!CI || (CI->getParent() != I->getParent()))
2166 return false;
2167
2168 if (I->getType() != CI->getOperand(0)->getType() ||
2169 !((Subtarget->hasSSE1() && RetVT == MVT::f32) ||
2170 (Subtarget->hasSSE2() && RetVT == MVT::f64)))
2171 return false;
2172
2173 const Value *CmpLHS = CI->getOperand(0);
2174 const Value *CmpRHS = CI->getOperand(1);
2175 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2176
2177 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
2178 // We don't have to materialize a zero constant for this case and can just use
2179 // %x again on the RHS.
2180 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
2181 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
2182 if (CmpRHSC && CmpRHSC->isNullValue())
2183 CmpRHS = CmpLHS;
2184 }
2185
2186 unsigned CC;
2187 bool NeedSwap;
2188 std::tie(CC, NeedSwap) = getX86SSEConditionCode(Predicate);
2189 if (CC > 7)
2190 return false;
2191
2192 if (NeedSwap)
2193 std::swap(CmpLHS, CmpRHS);
2194
Sanjay Patel302404b2015-03-05 21:46:54 +00002195 // Choose the SSE instruction sequence based on data type (float or double).
Craig Topper428169a2016-09-05 07:14:21 +00002196 static const uint16_t OpcTable[2][4] = {
Craig Topper6413f8a2016-12-06 04:58:39 +00002197 { X86::CMPSSrr, X86::ANDPSrr, X86::ANDNPSrr, X86::ORPSrr },
2198 { X86::CMPSDrr, X86::ANDPDrr, X86::ANDNPDrr, X86::ORPDrr }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002199 };
2200
Craig Topper428169a2016-09-05 07:14:21 +00002201 const uint16_t *Opc = nullptr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002202 switch (RetVT.SimpleTy) {
2203 default: return false;
Sanjay Patel302404b2015-03-05 21:46:54 +00002204 case MVT::f32: Opc = &OpcTable[0][0]; break;
2205 case MVT::f64: Opc = &OpcTable[1][0]; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002206 }
2207
2208 const Value *LHS = I->getOperand(1);
2209 const Value *RHS = I->getOperand(2);
2210
2211 unsigned LHSReg = getRegForValue(LHS);
2212 bool LHSIsKill = hasTrivialKill(LHS);
2213
2214 unsigned RHSReg = getRegForValue(RHS);
2215 bool RHSIsKill = hasTrivialKill(RHS);
2216
2217 unsigned CmpLHSReg = getRegForValue(CmpLHS);
2218 bool CmpLHSIsKill = hasTrivialKill(CmpLHS);
2219
2220 unsigned CmpRHSReg = getRegForValue(CmpRHS);
2221 bool CmpRHSIsKill = hasTrivialKill(CmpRHS);
2222
2223 if (!LHSReg || !RHSReg || !CmpLHS || !CmpRHS)
2224 return false;
2225
2226 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
Sanjay Patel302404b2015-03-05 21:46:54 +00002227 unsigned ResultReg;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002228
2229 if (Subtarget->hasAVX512()) {
2230 // If we have AVX512 we can use a mask compare and masked movss/sd.
2231 const TargetRegisterClass *VR128X = &X86::VR128XRegClass;
2232 const TargetRegisterClass *VK1 = &X86::VK1RegClass;
2233
2234 unsigned CmpOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002235 (RetVT == MVT::f32) ? X86::VCMPSSZrr : X86::VCMPSDZrr;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002236 unsigned CmpReg = fastEmitInst_rri(CmpOpcode, VK1, CmpLHSReg, CmpLHSIsKill,
2237 CmpRHSReg, CmpRHSIsKill, CC);
2238
2239 // Need an IMPLICIT_DEF for the input that is used to generate the upper
2240 // bits of the result register since its not based on any of the inputs.
2241 unsigned ImplicitDefReg = createResultReg(VR128X);
2242 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2243 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2244
2245 // Place RHSReg is the passthru of the masked movss/sd operation and put
2246 // LHS in the input. The mask input comes from the compare.
2247 unsigned MovOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002248 (RetVT == MVT::f32) ? X86::VMOVSSZrrk : X86::VMOVSDZrrk;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002249 unsigned MovReg = fastEmitInst_rrrr(MovOpcode, VR128X, RHSReg, RHSIsKill,
2250 CmpReg, true, ImplicitDefReg, true,
2251 LHSReg, LHSIsKill);
2252
2253 ResultReg = createResultReg(RC);
2254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2255 TII.get(TargetOpcode::COPY), ResultReg).addReg(MovReg);
2256
2257 } else if (Subtarget->hasAVX()) {
Matthias Braun818c78d2015-08-31 18:25:11 +00002258 const TargetRegisterClass *VR128 = &X86::VR128RegClass;
2259
Sanjay Patel302404b2015-03-05 21:46:54 +00002260 // If we have AVX, create 1 blendv instead of 3 logic instructions.
2261 // Blendv was introduced with SSE 4.1, but the 2 register form implicitly
2262 // uses XMM0 as the selection register. That may need just as many
2263 // instructions as the AND/ANDN/OR sequence due to register moves, so
2264 // don't bother.
2265 unsigned CmpOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002266 (RetVT == MVT::f32) ? X86::VCMPSSrr : X86::VCMPSDrr;
Sanjay Patel302404b2015-03-05 21:46:54 +00002267 unsigned BlendOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002268 (RetVT == MVT::f32) ? X86::VBLENDVPSrr : X86::VBLENDVPDrr;
2269
Craig Topper7ef6ea32016-12-05 04:51:31 +00002270 unsigned CmpReg = fastEmitInst_rri(CmpOpcode, RC, CmpLHSReg, CmpLHSIsKill,
Sanjay Patel302404b2015-03-05 21:46:54 +00002271 CmpRHSReg, CmpRHSIsKill, CC);
Matthias Braun818c78d2015-08-31 18:25:11 +00002272 unsigned VBlendReg = fastEmitInst_rrr(BlendOpcode, VR128, RHSReg, RHSIsKill,
2273 LHSReg, LHSIsKill, CmpReg, true);
2274 ResultReg = createResultReg(RC);
2275 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2276 TII.get(TargetOpcode::COPY), ResultReg).addReg(VBlendReg);
Sanjay Patel302404b2015-03-05 21:46:54 +00002277 } else {
Craig Topper6413f8a2016-12-06 04:58:39 +00002278 const TargetRegisterClass *VR128 = &X86::VR128RegClass;
Sanjay Patel302404b2015-03-05 21:46:54 +00002279 unsigned CmpReg = fastEmitInst_rri(Opc[0], RC, CmpLHSReg, CmpLHSIsKill,
2280 CmpRHSReg, CmpRHSIsKill, CC);
Craig Topper6413f8a2016-12-06 04:58:39 +00002281 unsigned AndReg = fastEmitInst_rr(Opc[1], VR128, CmpReg, /*IsKill=*/false,
Sanjay Patel302404b2015-03-05 21:46:54 +00002282 LHSReg, LHSIsKill);
Craig Topper6413f8a2016-12-06 04:58:39 +00002283 unsigned AndNReg = fastEmitInst_rr(Opc[2], VR128, CmpReg, /*IsKill=*/true,
Sanjay Patel302404b2015-03-05 21:46:54 +00002284 RHSReg, RHSIsKill);
Craig Topper6413f8a2016-12-06 04:58:39 +00002285 unsigned OrReg = fastEmitInst_rr(Opc[3], VR128, AndNReg, /*IsKill=*/true,
2286 AndReg, /*IsKill=*/true);
2287 ResultReg = createResultReg(RC);
2288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2289 TII.get(TargetOpcode::COPY), ResultReg).addReg(OrReg);
Sanjay Patel302404b2015-03-05 21:46:54 +00002290 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002291 updateValueMap(I, ResultReg);
2292 return true;
2293}
2294
2295bool X86FastISel::X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I) {
2296 // These are pseudo CMOV instructions and will be later expanded into control-
2297 // flow.
2298 unsigned Opc;
2299 switch (RetVT.SimpleTy) {
2300 default: return false;
2301 case MVT::i8: Opc = X86::CMOV_GR8; break;
2302 case MVT::i16: Opc = X86::CMOV_GR16; break;
2303 case MVT::i32: Opc = X86::CMOV_GR32; break;
2304 case MVT::f32: Opc = X86::CMOV_FR32; break;
2305 case MVT::f64: Opc = X86::CMOV_FR64; break;
2306 }
2307
2308 const Value *Cond = I->getOperand(0);
2309 X86::CondCode CC = X86::COND_NE;
2310
2311 // Optimize conditions coming from a compare if both instructions are in the
2312 // same basic block (values defined in other basic blocks may not have
2313 // initialized registers).
2314 const auto *CI = dyn_cast<CmpInst>(Cond);
2315 if (CI && (CI->getParent() == I->getParent())) {
2316 bool NeedSwap;
2317 std::tie(CC, NeedSwap) = getX86ConditionCode(CI->getPredicate());
2318 if (CC > X86::LAST_VALID_COND)
2319 return false;
2320
2321 const Value *CmpLHS = CI->getOperand(0);
2322 const Value *CmpRHS = CI->getOperand(1);
2323
2324 if (NeedSwap)
2325 std::swap(CmpLHS, CmpRHS);
2326
Mehdi Amini44ede332015-07-09 02:09:04 +00002327 EVT CmpVT = TLI.getValueType(DL, CmpLHS->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002328 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT, CI->getDebugLoc()))
2329 return false;
2330 } else {
2331 unsigned CondReg = getRegForValue(Cond);
2332 if (CondReg == 0)
2333 return false;
2334 bool CondIsKill = hasTrivialKill(Cond);
Guy Blank9ae797a2016-08-21 08:02:27 +00002335
Guy Blank2bdc74a2016-09-28 11:22:17 +00002336 // In case OpReg is a K register, COPY to a GPR
2337 if (MRI.getRegClass(CondReg) == &X86::VK1RegClass) {
2338 unsigned KCondReg = CondReg;
Craig Topper058f2f62017-03-28 16:35:29 +00002339 CondReg = createResultReg(&X86::GR32RegClass);
Guy Blank9ae797a2016-08-21 08:02:27 +00002340 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Guy Blank2bdc74a2016-09-28 11:22:17 +00002341 TII.get(TargetOpcode::COPY), CondReg)
2342 .addReg(KCondReg, getKillRegState(CondIsKill));
Craig Topper058f2f62017-03-28 16:35:29 +00002343 CondReg = fastEmitInst_extractsubreg(MVT::i8, CondReg, /*Kill=*/true,
2344 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00002345 }
2346 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
2347 .addReg(CondReg, getKillRegState(CondIsKill))
2348 .addImm(1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002349 }
2350
2351 const Value *LHS = I->getOperand(1);
2352 const Value *RHS = I->getOperand(2);
2353
2354 unsigned LHSReg = getRegForValue(LHS);
2355 bool LHSIsKill = hasTrivialKill(LHS);
2356
2357 unsigned RHSReg = getRegForValue(RHS);
2358 bool RHSIsKill = hasTrivialKill(RHS);
2359
2360 if (!LHSReg || !RHSReg)
2361 return false;
2362
2363 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2364
2365 unsigned ResultReg =
2366 fastEmitInst_rri(Opc, RC, RHSReg, RHSIsKill, LHSReg, LHSIsKill, CC);
2367 updateValueMap(I, ResultReg);
2368 return true;
2369}
2370
2371bool X86FastISel::X86SelectSelect(const Instruction *I) {
2372 MVT RetVT;
2373 if (!isTypeLegal(I->getType(), RetVT))
2374 return false;
2375
2376 // Check if we can fold the select.
2377 if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) {
2378 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2379 const Value *Opnd = nullptr;
2380 switch (Predicate) {
2381 default: break;
2382 case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break;
2383 case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break;
2384 }
2385 // No need for a select anymore - this is an unconditional move.
2386 if (Opnd) {
2387 unsigned OpReg = getRegForValue(Opnd);
2388 if (OpReg == 0)
2389 return false;
2390 bool OpIsKill = hasTrivialKill(Opnd);
2391 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2392 unsigned ResultReg = createResultReg(RC);
2393 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2394 TII.get(TargetOpcode::COPY), ResultReg)
2395 .addReg(OpReg, getKillRegState(OpIsKill));
2396 updateValueMap(I, ResultReg);
2397 return true;
2398 }
2399 }
2400
2401 // First try to use real conditional move instructions.
2402 if (X86FastEmitCMoveSelect(RetVT, I))
2403 return true;
2404
2405 // Try to use a sequence of SSE instructions to simulate a conditional move.
2406 if (X86FastEmitSSESelect(RetVT, I))
2407 return true;
2408
2409 // Fall-back to pseudo conditional move instructions, which will be later
2410 // converted to control-flow.
2411 if (X86FastEmitPseudoSelect(RetVT, I))
2412 return true;
2413
2414 return false;
2415}
2416
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002417bool X86FastISel::X86SelectSIToFP(const Instruction *I) {
Andrea Di Biagio98c36702015-04-20 11:56:59 +00002418 // The target-independent selection algorithm in FastISel already knows how
2419 // to select a SINT_TO_FP if the target is SSE but not AVX.
2420 // Early exit if the subtarget doesn't have AVX.
2421 if (!Subtarget->hasAVX())
2422 return false;
2423
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002424 if (!I->getOperand(0)->getType()->isIntegerTy(32))
2425 return false;
2426
2427 // Select integer to float/double conversion.
2428 unsigned OpReg = getRegForValue(I->getOperand(0));
2429 if (OpReg == 0)
2430 return false;
2431
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002432 const TargetRegisterClass *RC = nullptr;
2433 unsigned Opcode;
2434
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002435 if (I->getType()->isDoubleTy()) {
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002436 // sitofp int -> double
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002437 Opcode = X86::VCVTSI2SDrr;
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002438 RC = &X86::FR64RegClass;
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002439 } else if (I->getType()->isFloatTy()) {
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002440 // sitofp int -> float
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002441 Opcode = X86::VCVTSI2SSrr;
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002442 RC = &X86::FR32RegClass;
2443 } else
2444 return false;
2445
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002446 unsigned ImplicitDefReg = createResultReg(RC);
2447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2448 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2449 unsigned ResultReg =
2450 fastEmitInst_rr(Opcode, RC, ImplicitDefReg, true, OpReg, false);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002451 updateValueMap(I, ResultReg);
2452 return true;
2453}
2454
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002455// Helper method used by X86SelectFPExt and X86SelectFPTrunc.
2456bool X86FastISel::X86SelectFPExtOrFPTrunc(const Instruction *I,
2457 unsigned TargetOpc,
2458 const TargetRegisterClass *RC) {
2459 assert((I->getOpcode() == Instruction::FPExt ||
2460 I->getOpcode() == Instruction::FPTrunc) &&
2461 "Instruction must be an FPExt or FPTrunc!");
2462
2463 unsigned OpReg = getRegForValue(I->getOperand(0));
2464 if (OpReg == 0)
2465 return false;
2466
Ayman Musa9b802e42017-03-01 10:20:48 +00002467 unsigned ImplicitDefReg;
2468 if (Subtarget->hasAVX()) {
2469 ImplicitDefReg = createResultReg(RC);
2470 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2471 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2472
2473 }
2474
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002475 unsigned ResultReg = createResultReg(RC);
2476 MachineInstrBuilder MIB;
2477 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpc),
2478 ResultReg);
Ayman Musa4b2c9682017-02-23 13:15:44 +00002479
Ayman Musa9b802e42017-03-01 10:20:48 +00002480 if (Subtarget->hasAVX())
Ayman Musa4b2c9682017-02-23 13:15:44 +00002481 MIB.addReg(ImplicitDefReg);
Ayman Musa9b802e42017-03-01 10:20:48 +00002482
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002483 MIB.addReg(OpReg);
2484 updateValueMap(I, ResultReg);
2485 return true;
2486}
2487
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002488bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002489 if (X86ScalarSSEf64 && I->getType()->isDoubleTy() &&
2490 I->getOperand(0)->getType()->isFloatTy()) {
2491 // fpext from float to double.
2492 unsigned Opc = Subtarget->hasAVX() ? X86::VCVTSS2SDrr : X86::CVTSS2SDrr;
2493 return X86SelectFPExtOrFPTrunc(I, Opc, &X86::FR64RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002494 }
2495
2496 return false;
2497}
2498
2499bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002500 if (X86ScalarSSEf64 && I->getType()->isFloatTy() &&
2501 I->getOperand(0)->getType()->isDoubleTy()) {
2502 // fptrunc from double to float.
2503 unsigned Opc = Subtarget->hasAVX() ? X86::VCVTSD2SSrr : X86::CVTSD2SSrr;
2504 return X86SelectFPExtOrFPTrunc(I, Opc, &X86::FR32RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002505 }
2506
2507 return false;
2508}
2509
2510bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002511 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
2512 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002513
2514 // This code only handles truncation to byte.
2515 if (DstVT != MVT::i8 && DstVT != MVT::i1)
2516 return false;
2517 if (!TLI.isTypeLegal(SrcVT))
2518 return false;
2519
2520 unsigned InputReg = getRegForValue(I->getOperand(0));
2521 if (!InputReg)
2522 // Unhandled operand. Halt "fast" selection and bail.
2523 return false;
2524
2525 if (SrcVT == MVT::i8) {
2526 // Truncate from i8 to i1; no code needed.
2527 updateValueMap(I, InputReg);
2528 return true;
2529 }
2530
Pete Cooper7f7c9f12015-05-08 18:29:42 +00002531 bool KillInputReg = false;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002532 if (!Subtarget->is64Bit()) {
2533 // If we're on x86-32; we can't extract an i8 from a general register.
2534 // First issue a copy to GR16_ABCD or GR32_ABCD.
2535 const TargetRegisterClass *CopyRC =
2536 (SrcVT == MVT::i16) ? &X86::GR16_ABCDRegClass : &X86::GR32_ABCDRegClass;
2537 unsigned CopyReg = createResultReg(CopyRC);
2538 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2539 TII.get(TargetOpcode::COPY), CopyReg).addReg(InputReg);
2540 InputReg = CopyReg;
Pete Cooper7f7c9f12015-05-08 18:29:42 +00002541 KillInputReg = true;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002542 }
2543
2544 // Issue an extract_subreg.
2545 unsigned ResultReg = fastEmitInst_extractsubreg(MVT::i8,
Pete Cooper7f7c9f12015-05-08 18:29:42 +00002546 InputReg, KillInputReg,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002547 X86::sub_8bit);
2548 if (!ResultReg)
2549 return false;
2550
2551 updateValueMap(I, ResultReg);
2552 return true;
2553}
2554
2555bool X86FastISel::IsMemcpySmall(uint64_t Len) {
2556 return Len <= (Subtarget->is64Bit() ? 32 : 16);
2557}
2558
2559bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
2560 X86AddressMode SrcAM, uint64_t Len) {
2561
2562 // Make sure we don't bloat code by inlining very large memcpy's.
2563 if (!IsMemcpySmall(Len))
2564 return false;
2565
2566 bool i64Legal = Subtarget->is64Bit();
2567
2568 // We don't care about alignment here since we just emit integer accesses.
2569 while (Len) {
2570 MVT VT;
2571 if (Len >= 8 && i64Legal)
2572 VT = MVT::i64;
2573 else if (Len >= 4)
2574 VT = MVT::i32;
2575 else if (Len >= 2)
2576 VT = MVT::i16;
2577 else
2578 VT = MVT::i8;
2579
2580 unsigned Reg;
2581 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
2582 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
2583 assert(RV && "Failed to emit load or store??");
2584
2585 unsigned Size = VT.getSizeInBits()/8;
2586 Len -= Size;
2587 DestAM.Disp += Size;
2588 SrcAM.Disp += Size;
2589 }
2590
2591 return true;
2592}
2593
2594bool X86FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
2595 // FIXME: Handle more intrinsics.
2596 switch (II->getIntrinsicID()) {
2597 default: return false;
Andrea Di Biagio70351782015-02-20 19:37:14 +00002598 case Intrinsic::convert_from_fp16:
2599 case Intrinsic::convert_to_fp16: {
Eric Christopher824f42f2015-05-12 01:26:05 +00002600 if (Subtarget->useSoftFloat() || !Subtarget->hasF16C())
Andrea Di Biagio70351782015-02-20 19:37:14 +00002601 return false;
2602
2603 const Value *Op = II->getArgOperand(0);
2604 unsigned InputReg = getRegForValue(Op);
2605 if (InputReg == 0)
2606 return false;
2607
2608 // F16C only allows converting from float to half and from half to float.
2609 bool IsFloatToHalf = II->getIntrinsicID() == Intrinsic::convert_to_fp16;
2610 if (IsFloatToHalf) {
2611 if (!Op->getType()->isFloatTy())
2612 return false;
2613 } else {
2614 if (!II->getType()->isFloatTy())
2615 return false;
2616 }
2617
2618 unsigned ResultReg = 0;
2619 const TargetRegisterClass *RC = TLI.getRegClassFor(MVT::v8i16);
2620 if (IsFloatToHalf) {
2621 // 'InputReg' is implicitly promoted from register class FR32 to
2622 // register class VR128 by method 'constrainOperandRegClass' which is
2623 // directly called by 'fastEmitInst_ri'.
2624 // Instruction VCVTPS2PHrr takes an extra immediate operand which is
Ahmed Bougacha68a8efa2016-02-02 01:44:03 +00002625 // used to provide rounding control: use MXCSR.RC, encoded as 0b100.
2626 // It's consistent with the other FP instructions, which are usually
2627 // controlled by MXCSR.
2628 InputReg = fastEmitInst_ri(X86::VCVTPS2PHrr, RC, InputReg, false, 4);
Andrea Di Biagio70351782015-02-20 19:37:14 +00002629
2630 // Move the lower 32-bits of ResultReg to another register of class GR32.
2631 ResultReg = createResultReg(&X86::GR32RegClass);
2632 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2633 TII.get(X86::VMOVPDI2DIrr), ResultReg)
2634 .addReg(InputReg, RegState::Kill);
2635
2636 // The result value is in the lower 16-bits of ResultReg.
2637 unsigned RegIdx = X86::sub_16bit;
2638 ResultReg = fastEmitInst_extractsubreg(MVT::i16, ResultReg, true, RegIdx);
2639 } else {
2640 assert(Op->getType()->isIntegerTy(16) && "Expected a 16-bit integer!");
2641 // Explicitly sign-extend the input to 32-bit.
2642 InputReg = fastEmit_r(MVT::i16, MVT::i32, ISD::SIGN_EXTEND, InputReg,
2643 /*Kill=*/false);
2644
2645 // The following SCALAR_TO_VECTOR will be expanded into a VMOVDI2PDIrr.
2646 InputReg = fastEmit_r(MVT::i32, MVT::v4i32, ISD::SCALAR_TO_VECTOR,
2647 InputReg, /*Kill=*/true);
2648
2649 InputReg = fastEmitInst_r(X86::VCVTPH2PSrr, RC, InputReg, /*Kill=*/true);
2650
2651 // The result value is in the lower 32-bits of ResultReg.
2652 // Emit an explicit copy from register class VR128 to register class FR32.
2653 ResultReg = createResultReg(&X86::FR32RegClass);
2654 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2655 TII.get(TargetOpcode::COPY), ResultReg)
2656 .addReg(InputReg, RegState::Kill);
2657 }
2658
2659 updateValueMap(II, ResultReg);
2660 return true;
2661 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002662 case Intrinsic::frameaddress: {
David Majnemerca194852015-02-10 22:00:34 +00002663 MachineFunction *MF = FuncInfo.MF;
2664 if (MF->getTarget().getMCAsmInfo()->usesWindowsCFI())
2665 return false;
2666
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002667 Type *RetTy = II->getCalledFunction()->getReturnType();
2668
2669 MVT VT;
2670 if (!isTypeLegal(RetTy, VT))
2671 return false;
2672
2673 unsigned Opc;
2674 const TargetRegisterClass *RC = nullptr;
2675
2676 switch (VT.SimpleTy) {
2677 default: llvm_unreachable("Invalid result type for frameaddress.");
2678 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
2679 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
2680 }
2681
2682 // This needs to be set before we call getPtrSizedFrameRegister, otherwise
2683 // we get the wrong frame register.
Matthias Braun941a7052016-07-28 18:40:00 +00002684 MachineFrameInfo &MFI = MF->getFrameInfo();
2685 MFI.setFrameAddressIsTaken(true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002686
Eric Christophera1c535b2015-02-02 23:03:45 +00002687 const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
David Majnemerca194852015-02-10 22:00:34 +00002688 unsigned FrameReg = RegInfo->getPtrSizedFrameRegister(*MF);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002689 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
2690 (FrameReg == X86::EBP && VT == MVT::i32)) &&
2691 "Invalid Frame Register!");
2692
2693 // Always make a copy of the frame register to to a vreg first, so that we
2694 // never directly reference the frame register (the TwoAddressInstruction-
2695 // Pass doesn't like that).
2696 unsigned SrcReg = createResultReg(RC);
2697 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2698 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
2699
2700 // Now recursively load from the frame address.
2701 // movq (%rbp), %rax
2702 // movq (%rax), %rax
2703 // movq (%rax), %rax
2704 // ...
2705 unsigned DestReg;
2706 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
2707 while (Depth--) {
2708 DestReg = createResultReg(RC);
2709 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2710 TII.get(Opc), DestReg), SrcReg);
2711 SrcReg = DestReg;
2712 }
2713
2714 updateValueMap(II, SrcReg);
2715 return true;
2716 }
2717 case Intrinsic::memcpy: {
2718 const MemCpyInst *MCI = cast<MemCpyInst>(II);
2719 // Don't handle volatile or variable length memcpys.
2720 if (MCI->isVolatile())
2721 return false;
2722
2723 if (isa<ConstantInt>(MCI->getLength())) {
2724 // Small memcpy's are common enough that we want to do them
2725 // without a call if possible.
2726 uint64_t Len = cast<ConstantInt>(MCI->getLength())->getZExtValue();
2727 if (IsMemcpySmall(Len)) {
2728 X86AddressMode DestAM, SrcAM;
2729 if (!X86SelectAddress(MCI->getRawDest(), DestAM) ||
2730 !X86SelectAddress(MCI->getRawSource(), SrcAM))
2731 return false;
2732 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
2733 return true;
2734 }
2735 }
2736
2737 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2738 if (!MCI->getLength()->getType()->isIntegerTy(SizeWidth))
2739 return false;
2740
2741 if (MCI->getSourceAddressSpace() > 255 || MCI->getDestAddressSpace() > 255)
2742 return false;
2743
Pete Cooper67cf9a72015-11-19 05:56:52 +00002744 return lowerCallTo(II, "memcpy", II->getNumArgOperands() - 2);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002745 }
2746 case Intrinsic::memset: {
2747 const MemSetInst *MSI = cast<MemSetInst>(II);
2748
2749 if (MSI->isVolatile())
2750 return false;
2751
2752 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2753 if (!MSI->getLength()->getType()->isIntegerTy(SizeWidth))
2754 return false;
2755
2756 if (MSI->getDestAddressSpace() > 255)
2757 return false;
2758
Pete Cooper67cf9a72015-11-19 05:56:52 +00002759 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002760 }
2761 case Intrinsic::stackprotector: {
2762 // Emit code to store the stack guard onto the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002763 EVT PtrTy = TLI.getPointerTy(DL);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002764
2765 const Value *Op1 = II->getArgOperand(0); // The guard's value.
2766 const AllocaInst *Slot = cast<AllocaInst>(II->getArgOperand(1));
2767
2768 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
2769
2770 // Grab the frame index.
2771 X86AddressMode AM;
2772 if (!X86SelectAddress(Slot, AM)) return false;
2773 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
2774 return true;
2775 }
2776 case Intrinsic::dbg_declare: {
2777 const DbgDeclareInst *DI = cast<DbgDeclareInst>(II);
2778 X86AddressMode AM;
2779 assert(DI->getAddress() && "Null address should be checked earlier!");
2780 if (!X86SelectAddress(DI->getAddress(), AM))
2781 return false;
2782 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
2783 // FIXME may need to add RegState::Debug to any registers produced,
2784 // although ESP/EBP should be the only ones at the moment.
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00002785 assert(DI->getVariable()->isValidLocationForIntrinsic(DbgLoc) &&
2786 "Expected inlined-at fields to agree");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002787 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM)
2788 .addImm(0)
2789 .addMetadata(DI->getVariable())
2790 .addMetadata(DI->getExpression());
2791 return true;
2792 }
2793 case Intrinsic::trap: {
2794 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
2795 return true;
2796 }
2797 case Intrinsic::sqrt: {
2798 if (!Subtarget->hasSSE1())
2799 return false;
2800
2801 Type *RetTy = II->getCalledFunction()->getReturnType();
2802
2803 MVT VT;
2804 if (!isTypeLegal(RetTy, VT))
2805 return false;
2806
2807 // Unfortunately we can't use fastEmit_r, because the AVX version of FSQRT
2808 // is not generated by FastISel yet.
2809 // FIXME: Update this code once tablegen can handle it.
Craig Toppercf65c622016-03-02 04:42:31 +00002810 static const uint16_t SqrtOpc[2][2] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002811 {X86::SQRTSSr, X86::VSQRTSSr},
2812 {X86::SQRTSDr, X86::VSQRTSDr}
2813 };
2814 bool HasAVX = Subtarget->hasAVX();
2815 unsigned Opc;
2816 const TargetRegisterClass *RC;
2817 switch (VT.SimpleTy) {
2818 default: return false;
2819 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
2820 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
2821 }
2822
2823 const Value *SrcVal = II->getArgOperand(0);
2824 unsigned SrcReg = getRegForValue(SrcVal);
2825
2826 if (SrcReg == 0)
2827 return false;
2828
2829 unsigned ImplicitDefReg = 0;
2830 if (HasAVX) {
2831 ImplicitDefReg = createResultReg(RC);
2832 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2833 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2834 }
2835
2836 unsigned ResultReg = createResultReg(RC);
2837 MachineInstrBuilder MIB;
2838 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
2839 ResultReg);
2840
2841 if (ImplicitDefReg)
2842 MIB.addReg(ImplicitDefReg);
2843
2844 MIB.addReg(SrcReg);
2845
2846 updateValueMap(II, ResultReg);
2847 return true;
2848 }
2849 case Intrinsic::sadd_with_overflow:
2850 case Intrinsic::uadd_with_overflow:
2851 case Intrinsic::ssub_with_overflow:
2852 case Intrinsic::usub_with_overflow:
2853 case Intrinsic::smul_with_overflow:
2854 case Intrinsic::umul_with_overflow: {
2855 // This implements the basic lowering of the xalu with overflow intrinsics
2856 // into add/sub/mul followed by either seto or setb.
2857 const Function *Callee = II->getCalledFunction();
2858 auto *Ty = cast<StructType>(Callee->getReturnType());
2859 Type *RetTy = Ty->getTypeAtIndex(0U);
Zvi Rackover6f76f462016-11-15 13:50:35 +00002860 assert(Ty->getTypeAtIndex(1)->isIntegerTy() &&
2861 Ty->getTypeAtIndex(1)->getScalarSizeInBits() == 1 &&
2862 "Overflow value expected to be an i1");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002863
2864 MVT VT;
2865 if (!isTypeLegal(RetTy, VT))
2866 return false;
2867
2868 if (VT < MVT::i8 || VT > MVT::i64)
2869 return false;
2870
2871 const Value *LHS = II->getArgOperand(0);
2872 const Value *RHS = II->getArgOperand(1);
2873
2874 // Canonicalize immediate to the RHS.
2875 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2876 isCommutativeIntrinsic(II))
2877 std::swap(LHS, RHS);
2878
2879 bool UseIncDec = false;
2880 if (isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isOne())
2881 UseIncDec = true;
2882
2883 unsigned BaseOpc, CondOpc;
2884 switch (II->getIntrinsicID()) {
2885 default: llvm_unreachable("Unexpected intrinsic!");
2886 case Intrinsic::sadd_with_overflow:
2887 BaseOpc = UseIncDec ? unsigned(X86ISD::INC) : unsigned(ISD::ADD);
2888 CondOpc = X86::SETOr;
2889 break;
2890 case Intrinsic::uadd_with_overflow:
2891 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
2892 case Intrinsic::ssub_with_overflow:
2893 BaseOpc = UseIncDec ? unsigned(X86ISD::DEC) : unsigned(ISD::SUB);
2894 CondOpc = X86::SETOr;
2895 break;
2896 case Intrinsic::usub_with_overflow:
2897 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
2898 case Intrinsic::smul_with_overflow:
2899 BaseOpc = X86ISD::SMUL; CondOpc = X86::SETOr; break;
2900 case Intrinsic::umul_with_overflow:
2901 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
2902 }
2903
2904 unsigned LHSReg = getRegForValue(LHS);
2905 if (LHSReg == 0)
2906 return false;
2907 bool LHSIsKill = hasTrivialKill(LHS);
2908
2909 unsigned ResultReg = 0;
2910 // Check if we have an immediate version.
2911 if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
Craig Topper66111882016-06-02 04:19:42 +00002912 static const uint16_t Opc[2][4] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002913 { X86::INC8r, X86::INC16r, X86::INC32r, X86::INC64r },
2914 { X86::DEC8r, X86::DEC16r, X86::DEC32r, X86::DEC64r }
2915 };
2916
2917 if (BaseOpc == X86ISD::INC || BaseOpc == X86ISD::DEC) {
2918 ResultReg = createResultReg(TLI.getRegClassFor(VT));
2919 bool IsDec = BaseOpc == X86ISD::DEC;
2920 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2921 TII.get(Opc[IsDec][VT.SimpleTy-MVT::i8]), ResultReg)
2922 .addReg(LHSReg, getKillRegState(LHSIsKill));
2923 } else
2924 ResultReg = fastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2925 CI->getZExtValue());
2926 }
2927
2928 unsigned RHSReg;
2929 bool RHSIsKill;
2930 if (!ResultReg) {
2931 RHSReg = getRegForValue(RHS);
2932 if (RHSReg == 0)
2933 return false;
2934 RHSIsKill = hasTrivialKill(RHS);
2935 ResultReg = fastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2936 RHSIsKill);
2937 }
2938
2939 // FastISel doesn't have a pattern for all X86::MUL*r and X86::IMUL*r. Emit
2940 // it manually.
2941 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
Craig Toppercf65c622016-03-02 04:42:31 +00002942 static const uint16_t MULOpc[] =
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002943 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
Craig Toppercf65c622016-03-02 04:42:31 +00002944 static const MCPhysReg Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002945 // First copy the first operand into RAX, which is an implicit input to
2946 // the X86::MUL*r instruction.
2947 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2948 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2949 .addReg(LHSReg, getKillRegState(LHSIsKill));
2950 ResultReg = fastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2951 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
2952 } else if (BaseOpc == X86ISD::SMUL && !ResultReg) {
Craig Toppercf65c622016-03-02 04:42:31 +00002953 static const uint16_t MULOpc[] =
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002954 { X86::IMUL8r, X86::IMUL16rr, X86::IMUL32rr, X86::IMUL64rr };
2955 if (VT == MVT::i8) {
2956 // Copy the first operand into AL, which is an implicit input to the
2957 // X86::IMUL8r instruction.
2958 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2959 TII.get(TargetOpcode::COPY), X86::AL)
2960 .addReg(LHSReg, getKillRegState(LHSIsKill));
2961 ResultReg = fastEmitInst_r(MULOpc[0], TLI.getRegClassFor(VT), RHSReg,
2962 RHSIsKill);
2963 } else
2964 ResultReg = fastEmitInst_rr(MULOpc[VT.SimpleTy-MVT::i8],
2965 TLI.getRegClassFor(VT), LHSReg, LHSIsKill,
2966 RHSReg, RHSIsKill);
2967 }
2968
2969 if (!ResultReg)
2970 return false;
2971
Zvi Rackoverf0b9b57b2016-11-15 13:29:23 +00002972 // Assign to a GPR since the overflow return value is lowered to a SETcc.
2973 unsigned ResultReg2 = createResultReg(&X86::GR8RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002974 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
2975 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
2976 ResultReg2);
2977
2978 updateValueMap(II, ResultReg, 2);
2979 return true;
2980 }
2981 case Intrinsic::x86_sse_cvttss2si:
2982 case Intrinsic::x86_sse_cvttss2si64:
2983 case Intrinsic::x86_sse2_cvttsd2si:
2984 case Intrinsic::x86_sse2_cvttsd2si64: {
2985 bool IsInputDouble;
2986 switch (II->getIntrinsicID()) {
2987 default: llvm_unreachable("Unexpected intrinsic.");
2988 case Intrinsic::x86_sse_cvttss2si:
2989 case Intrinsic::x86_sse_cvttss2si64:
2990 if (!Subtarget->hasSSE1())
2991 return false;
2992 IsInputDouble = false;
2993 break;
2994 case Intrinsic::x86_sse2_cvttsd2si:
2995 case Intrinsic::x86_sse2_cvttsd2si64:
2996 if (!Subtarget->hasSSE2())
2997 return false;
2998 IsInputDouble = true;
2999 break;
3000 }
3001
3002 Type *RetTy = II->getCalledFunction()->getReturnType();
3003 MVT VT;
3004 if (!isTypeLegal(RetTy, VT))
3005 return false;
3006
Craig Topper66111882016-06-02 04:19:42 +00003007 static const uint16_t CvtOpc[2][2][2] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003008 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
3009 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
3010 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
3011 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
3012 };
3013 bool HasAVX = Subtarget->hasAVX();
3014 unsigned Opc;
3015 switch (VT.SimpleTy) {
3016 default: llvm_unreachable("Unexpected result type.");
3017 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
3018 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
3019 }
3020
3021 // Check if we can fold insertelement instructions into the convert.
3022 const Value *Op = II->getArgOperand(0);
3023 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
3024 const Value *Index = IE->getOperand(2);
3025 if (!isa<ConstantInt>(Index))
3026 break;
3027 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
3028
3029 if (Idx == 0) {
3030 Op = IE->getOperand(1);
3031 break;
3032 }
3033 Op = IE->getOperand(0);
3034 }
3035
3036 unsigned Reg = getRegForValue(Op);
3037 if (Reg == 0)
3038 return false;
3039
3040 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3041 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
3042 .addReg(Reg);
3043
3044 updateValueMap(II, ResultReg);
3045 return true;
3046 }
3047 }
3048}
3049
3050bool X86FastISel::fastLowerArguments() {
3051 if (!FuncInfo.CanLowerReturn)
3052 return false;
3053
3054 const Function *F = FuncInfo.Fn;
3055 if (F->isVarArg())
3056 return false;
3057
3058 CallingConv::ID CC = F->getCallingConv();
3059 if (CC != CallingConv::C)
3060 return false;
3061
3062 if (Subtarget->isCallingConvWin64(CC))
3063 return false;
3064
3065 if (!Subtarget->is64Bit())
3066 return false;
3067
3068 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
3069 unsigned GPRCnt = 0;
3070 unsigned FPRCnt = 0;
3071 unsigned Idx = 0;
3072 for (auto const &Arg : F->args()) {
3073 // The first argument is at index 1.
3074 ++Idx;
3075 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
3076 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
3077 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
Manman Renf46262e2016-03-29 17:37:21 +00003078 F->getAttributes().hasAttribute(Idx, Attribute::SwiftSelf) ||
Manman Ren57518142016-04-11 21:08:06 +00003079 F->getAttributes().hasAttribute(Idx, Attribute::SwiftError) ||
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003080 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
3081 return false;
3082
3083 Type *ArgTy = Arg.getType();
3084 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3085 return false;
3086
Mehdi Amini44ede332015-07-09 02:09:04 +00003087 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003088 if (!ArgVT.isSimple()) return false;
3089 switch (ArgVT.getSimpleVT().SimpleTy) {
3090 default: return false;
3091 case MVT::i32:
3092 case MVT::i64:
3093 ++GPRCnt;
3094 break;
3095 case MVT::f32:
3096 case MVT::f64:
3097 if (!Subtarget->hasSSE1())
3098 return false;
3099 ++FPRCnt;
3100 break;
3101 }
3102
3103 if (GPRCnt > 6)
3104 return false;
3105
3106 if (FPRCnt > 8)
3107 return false;
3108 }
3109
3110 static const MCPhysReg GPR32ArgRegs[] = {
3111 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
3112 };
3113 static const MCPhysReg GPR64ArgRegs[] = {
3114 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
3115 };
3116 static const MCPhysReg XMMArgRegs[] = {
3117 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3118 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3119 };
3120
3121 unsigned GPRIdx = 0;
3122 unsigned FPRIdx = 0;
3123 for (auto const &Arg : F->args()) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003124 MVT VT = TLI.getSimpleValueType(DL, Arg.getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003125 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
3126 unsigned SrcReg;
3127 switch (VT.SimpleTy) {
3128 default: llvm_unreachable("Unexpected value type.");
3129 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
3130 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00003131 case MVT::f32: LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003132 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
3133 }
3134 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3135 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3136 // Without this, EmitLiveInCopies may eliminate the livein if its only
3137 // use is a bitcast (which isn't turned into an instruction).
3138 unsigned ResultReg = createResultReg(RC);
3139 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3140 TII.get(TargetOpcode::COPY), ResultReg)
3141 .addReg(DstReg, getKillRegState(true));
3142 updateValueMap(&Arg, ResultReg);
3143 }
3144 return true;
3145}
3146
Nico Weberaf7e8462016-07-14 01:52:51 +00003147static unsigned computeBytesPoppedByCalleeForSRet(const X86Subtarget *Subtarget,
3148 CallingConv::ID CC,
3149 ImmutableCallSite *CS) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003150 if (Subtarget->is64Bit())
3151 return 0;
3152 if (Subtarget->getTargetTriple().isOSMSVCRT())
3153 return 0;
3154 if (CC == CallingConv::Fast || CC == CallingConv::GHC ||
3155 CC == CallingConv::HiPE)
3156 return 0;
Sanjoy Dasb11b4402015-11-04 20:33:45 +00003157
3158 if (CS)
3159 if (CS->arg_empty() || !CS->paramHasAttr(1, Attribute::StructRet) ||
Michael Kuperstein2ea81ba2015-12-28 14:39:21 +00003160 CS->paramHasAttr(1, Attribute::InReg) || Subtarget->isTargetMCU())
Sanjoy Dasb11b4402015-11-04 20:33:45 +00003161 return 0;
3162
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003163 return 4;
3164}
3165
3166bool X86FastISel::fastLowerCall(CallLoweringInfo &CLI) {
3167 auto &OutVals = CLI.OutVals;
3168 auto &OutFlags = CLI.OutFlags;
3169 auto &OutRegs = CLI.OutRegs;
3170 auto &Ins = CLI.Ins;
3171 auto &InRegs = CLI.InRegs;
3172 CallingConv::ID CC = CLI.CallConv;
3173 bool &IsTailCall = CLI.IsTailCall;
3174 bool IsVarArg = CLI.IsVarArg;
3175 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00003176 MCSymbol *Symbol = CLI.Symbol;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003177
3178 bool Is64Bit = Subtarget->is64Bit();
3179 bool IsWin64 = Subtarget->isCallingConvWin64(CC);
3180
3181 // Handle only C, fastcc, and webkit_js calling conventions for now.
3182 switch (CC) {
3183 default: return false;
3184 case CallingConv::C:
3185 case CallingConv::Fast:
3186 case CallingConv::WebKit_JS:
Manman Renf8bdd882016-04-05 22:41:47 +00003187 case CallingConv::Swift:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003188 case CallingConv::X86_FastCall:
Nico Weberecdf45b2016-07-14 13:54:26 +00003189 case CallingConv::X86_StdCall:
Nico Weberaf7e8462016-07-14 01:52:51 +00003190 case CallingConv::X86_ThisCall:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003191 case CallingConv::X86_64_Win64:
3192 case CallingConv::X86_64_SysV:
3193 break;
3194 }
3195
3196 // Allow SelectionDAG isel to handle tail calls.
3197 if (IsTailCall)
3198 return false;
3199
3200 // fastcc with -tailcallopt is intended to provide a guaranteed
3201 // tail call optimization. Fastisel doesn't know how to do that.
3202 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
3203 return false;
3204
3205 // Don't know how to handle Win64 varargs yet. Nothing special needed for
3206 // x86-32. Special handling for x86-64 is implemented.
3207 if (IsVarArg && IsWin64)
3208 return false;
3209
3210 // Don't know about inalloca yet.
3211 if (CLI.CS && CLI.CS->hasInAllocaArgument())
3212 return false;
3213
Manman Ren57518142016-04-11 21:08:06 +00003214 for (auto Flag : CLI.OutFlags)
3215 if (Flag.isSwiftError())
3216 return false;
3217
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003218 SmallVector<MVT, 16> OutVTs;
3219 SmallVector<unsigned, 16> ArgRegs;
3220
3221 // If this is a constant i1/i8/i16 argument, promote to i32 to avoid an extra
3222 // instruction. This is safe because it is common to all FastISel supported
3223 // calling conventions on x86.
3224 for (int i = 0, e = OutVals.size(); i != e; ++i) {
3225 Value *&Val = OutVals[i];
3226 ISD::ArgFlagsTy Flags = OutFlags[i];
3227 if (auto *CI = dyn_cast<ConstantInt>(Val)) {
3228 if (CI->getBitWidth() < 32) {
3229 if (Flags.isSExt())
3230 Val = ConstantExpr::getSExt(CI, Type::getInt32Ty(CI->getContext()));
3231 else
3232 Val = ConstantExpr::getZExt(CI, Type::getInt32Ty(CI->getContext()));
3233 }
3234 }
3235
3236 // Passing bools around ends up doing a trunc to i1 and passing it.
3237 // Codegen this as an argument + "and 1".
3238 MVT VT;
3239 auto *TI = dyn_cast<TruncInst>(Val);
3240 unsigned ResultReg;
3241 if (TI && TI->getType()->isIntegerTy(1) && CLI.CS &&
3242 (TI->getParent() == CLI.CS->getInstruction()->getParent()) &&
3243 TI->hasOneUse()) {
3244 Value *PrevVal = TI->getOperand(0);
3245 ResultReg = getRegForValue(PrevVal);
3246
3247 if (!ResultReg)
3248 return false;
3249
3250 if (!isTypeLegal(PrevVal->getType(), VT))
3251 return false;
3252
3253 ResultReg =
3254 fastEmit_ri(VT, VT, ISD::AND, ResultReg, hasTrivialKill(PrevVal), 1);
3255 } else {
3256 if (!isTypeLegal(Val->getType(), VT))
3257 return false;
3258 ResultReg = getRegForValue(Val);
3259 }
3260
3261 if (!ResultReg)
3262 return false;
3263
3264 ArgRegs.push_back(ResultReg);
3265 OutVTs.push_back(VT);
3266 }
3267
3268 // Analyze operands of the call, assigning locations to each operand.
3269 SmallVector<CCValAssign, 16> ArgLocs;
3270 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, CLI.RetTy->getContext());
3271
3272 // Allocate shadow area for Win64
3273 if (IsWin64)
3274 CCInfo.AllocateStack(32, 8);
3275
3276 CCInfo.AnalyzeCallOperands(OutVTs, OutFlags, CC_X86);
3277
3278 // Get a count of how many bytes are to be pushed on the stack.
Jeroen Ketema740f9d72015-09-29 10:12:57 +00003279 unsigned NumBytes = CCInfo.getAlignedCallFrameSize();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003280
3281 // Issue CALLSEQ_START
3282 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
3283 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Michael Kuperstein13fbd452015-02-01 16:56:04 +00003284 .addImm(NumBytes).addImm(0);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003285
3286 // Walk the register/memloc assignments, inserting copies/loads.
Eric Christophera1c535b2015-02-02 23:03:45 +00003287 const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003288 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3289 CCValAssign const &VA = ArgLocs[i];
3290 const Value *ArgVal = OutVals[VA.getValNo()];
3291 MVT ArgVT = OutVTs[VA.getValNo()];
3292
3293 if (ArgVT == MVT::x86mmx)
3294 return false;
3295
3296 unsigned ArgReg = ArgRegs[VA.getValNo()];
3297
3298 // Promote the value if needed.
3299 switch (VA.getLocInfo()) {
3300 case CCValAssign::Full: break;
3301 case CCValAssign::SExt: {
3302 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3303 "Unexpected extend");
David Majnemer2c5aeab2016-05-04 00:22:23 +00003304
Craig Topper088ba172016-12-05 06:09:55 +00003305 if (ArgVT == MVT::i1)
David Majnemer2c5aeab2016-05-04 00:22:23 +00003306 return false;
3307
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003308 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
3309 ArgVT, ArgReg);
3310 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
3311 ArgVT = VA.getLocVT();
3312 break;
3313 }
3314 case CCValAssign::ZExt: {
3315 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3316 "Unexpected extend");
David Majnemer2c5aeab2016-05-04 00:22:23 +00003317
3318 // Handle zero-extension from i1 to i8, which is common.
Craig Topper088ba172016-12-05 06:09:55 +00003319 if (ArgVT == MVT::i1) {
Craig Topper058f2f62017-03-28 16:35:29 +00003320 // In case SrcReg is a K register, COPY to a GPR
3321 if (MRI.getRegClass(ArgReg) == &X86::VK1RegClass) {
3322 unsigned KArgReg = ArgReg;
3323 ArgReg = createResultReg(&X86::GR32RegClass);
3324 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3325 TII.get(TargetOpcode::COPY), ArgReg)
3326 .addReg(KArgReg);
3327 ArgReg = fastEmitInst_extractsubreg(MVT::i8, ArgReg, /*Kill=*/true,
3328 X86::sub_8bit);
3329 }
David Majnemer2c5aeab2016-05-04 00:22:23 +00003330 // Set the high bits to zero.
3331 ArgReg = fastEmitZExtFromI1(MVT::i8, ArgReg, /*TODO: Kill=*/false);
3332 ArgVT = MVT::i8;
3333
3334 if (ArgReg == 0)
3335 return false;
3336 }
3337
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003338 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
3339 ArgVT, ArgReg);
3340 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
3341 ArgVT = VA.getLocVT();
3342 break;
3343 }
3344 case CCValAssign::AExt: {
3345 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3346 "Unexpected extend");
3347 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), ArgReg,
3348 ArgVT, ArgReg);
3349 if (!Emitted)
3350 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
3351 ArgVT, ArgReg);
3352 if (!Emitted)
3353 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
3354 ArgVT, ArgReg);
3355
3356 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
3357 ArgVT = VA.getLocVT();
3358 break;
3359 }
3360 case CCValAssign::BCvt: {
3361 ArgReg = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, ArgReg,
3362 /*TODO: Kill=*/false);
3363 assert(ArgReg && "Failed to emit a bitcast!");
3364 ArgVT = VA.getLocVT();
3365 break;
3366 }
3367 case CCValAssign::VExt:
3368 // VExt has not been implemented, so this should be impossible to reach
3369 // for now. However, fallback to Selection DAG isel once implemented.
3370 return false;
3371 case CCValAssign::AExtUpper:
3372 case CCValAssign::SExtUpper:
3373 case CCValAssign::ZExtUpper:
3374 case CCValAssign::FPExt:
3375 llvm_unreachable("Unexpected loc info!");
3376 case CCValAssign::Indirect:
3377 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
3378 // support this.
3379 return false;
3380 }
3381
3382 if (VA.isRegLoc()) {
3383 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3384 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
3385 OutRegs.push_back(VA.getLocReg());
3386 } else {
3387 assert(VA.isMemLoc());
3388
3389 // Don't emit stores for undef values.
3390 if (isa<UndefValue>(ArgVal))
3391 continue;
3392
3393 unsigned LocMemOffset = VA.getLocMemOffset();
3394 X86AddressMode AM;
3395 AM.Base.Reg = RegInfo->getStackRegister();
3396 AM.Disp = LocMemOffset;
3397 ISD::ArgFlagsTy Flags = OutFlags[VA.getValNo()];
3398 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
3399 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00003400 MachinePointerInfo::getStack(*FuncInfo.MF, LocMemOffset),
3401 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003402 if (Flags.isByVal()) {
3403 X86AddressMode SrcAM;
3404 SrcAM.Base.Reg = ArgReg;
3405 if (!TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize()))
3406 return false;
3407 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
3408 // If this is a really simple value, emit this with the Value* version
3409 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
3410 // as it can cause us to reevaluate the argument.
3411 if (!X86FastEmitStore(ArgVT, ArgVal, AM, MMO))
3412 return false;
3413 } else {
3414 bool ValIsKill = hasTrivialKill(ArgVal);
3415 if (!X86FastEmitStore(ArgVT, ArgReg, ValIsKill, AM, MMO))
3416 return false;
3417 }
3418 }
3419 }
3420
3421 // ELF / PIC requires GOT in the EBX register before function calls via PLT
3422 // GOT pointer.
3423 if (Subtarget->isPICStyleGOT()) {
3424 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
3425 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3426 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
3427 }
3428
3429 if (Is64Bit && IsVarArg && !IsWin64) {
3430 // From AMD64 ABI document:
3431 // For calls that may call functions that use varargs or stdargs
3432 // (prototype-less calls or calls to functions containing ellipsis (...) in
3433 // the declaration) %al is used as hidden argument to specify the number
3434 // of SSE registers used. The contents of %al do not need to match exactly
3435 // the number of registers, but must be an ubound on the number of SSE
3436 // registers used and is in the range 0 - 8 inclusive.
3437
3438 // Count the number of XMM registers allocated.
3439 static const MCPhysReg XMMArgRegs[] = {
3440 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3441 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3442 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003443 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003444 assert((Subtarget->hasSSE1() || !NumXMMRegs)
3445 && "SSE registers cannot be used when SSE is disabled");
3446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
3447 X86::AL).addImm(NumXMMRegs);
3448 }
3449
3450 // Materialize callee address in a register. FIXME: GV address can be
3451 // handled with a CALLpcrel32 instead.
3452 X86AddressMode CalleeAM;
3453 if (!X86SelectCallAddress(Callee, CalleeAM))
3454 return false;
3455
3456 unsigned CalleeOp = 0;
3457 const GlobalValue *GV = nullptr;
3458 if (CalleeAM.GV != nullptr) {
3459 GV = CalleeAM.GV;
3460 } else if (CalleeAM.Base.Reg != 0) {
3461 CalleeOp = CalleeAM.Base.Reg;
3462 } else
3463 return false;
3464
3465 // Issue the call.
3466 MachineInstrBuilder MIB;
3467 if (CalleeOp) {
3468 // Register-indirect call.
3469 unsigned CallOpc = Is64Bit ? X86::CALL64r : X86::CALL32r;
3470 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
3471 .addReg(CalleeOp);
3472 } else {
3473 // Direct call.
3474 assert(GV && "Not a direct call");
3475 unsigned CallOpc = Is64Bit ? X86::CALL64pcrel32 : X86::CALLpcrel32;
3476
3477 // See if we need any target-specific flags on the GV operand.
Rafael Espindola46107b92016-05-19 18:49:29 +00003478 unsigned char OpFlags = Subtarget->classifyGlobalFunctionReference(GV);
Asaf Badouh89406d12016-04-20 08:32:57 +00003479 // Ignore NonLazyBind attribute in FastISel
3480 if (OpFlags == X86II::MO_GOTPCREL)
3481 OpFlags = 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003482
3483 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00003484 if (Symbol)
3485 MIB.addSym(Symbol, OpFlags);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003486 else
3487 MIB.addGlobalAddress(GV, 0, OpFlags);
3488 }
3489
3490 // Add a register mask operand representing the call-preserved registers.
3491 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00003492 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003493
3494 // Add an implicit use GOT pointer in EBX.
3495 if (Subtarget->isPICStyleGOT())
3496 MIB.addReg(X86::EBX, RegState::Implicit);
3497
3498 if (Is64Bit && IsVarArg && !IsWin64)
3499 MIB.addReg(X86::AL, RegState::Implicit);
3500
3501 // Add implicit physical register uses to the call.
3502 for (auto Reg : OutRegs)
3503 MIB.addReg(Reg, RegState::Implicit);
3504
3505 // Issue CALLSEQ_END
3506 unsigned NumBytesForCalleeToPop =
Nico Weberaf7e8462016-07-14 01:52:51 +00003507 X86::isCalleePop(CC, Subtarget->is64Bit(), IsVarArg,
3508 TM.Options.GuaranteedTailCallOpt)
3509 ? NumBytes // Callee pops everything.
3510 : computeBytesPoppedByCalleeForSRet(Subtarget, CC, CLI.CS);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003511 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
3512 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
3513 .addImm(NumBytes).addImm(NumBytesForCalleeToPop);
3514
3515 // Now handle call return values.
3516 SmallVector<CCValAssign, 16> RVLocs;
3517 CCState CCRetInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs,
3518 CLI.RetTy->getContext());
3519 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
3520
3521 // Copy all of the result registers out of their specified physreg.
3522 unsigned ResultReg = FuncInfo.CreateRegs(CLI.RetTy);
3523 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3524 CCValAssign &VA = RVLocs[i];
3525 EVT CopyVT = VA.getValVT();
3526 unsigned CopyReg = ResultReg + i;
3527
3528 // If this is x86-64, and we disabled SSE, we can't return FP values
3529 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
3530 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
3531 report_fatal_error("SSE register return with SSE disabled");
3532 }
3533
3534 // If we prefer to use the value in xmm registers, copy it out as f80 and
3535 // use a truncate to move it from fp stack reg to xmm reg.
3536 if ((VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1) &&
3537 isScalarFPTypeInSSEReg(VA.getValVT())) {
3538 CopyVT = MVT::f80;
3539 CopyReg = createResultReg(&X86::RFP80RegClass);
3540 }
3541
3542 // Copy out the result.
3543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3544 TII.get(TargetOpcode::COPY), CopyReg).addReg(VA.getLocReg());
3545 InRegs.push_back(VA.getLocReg());
3546
3547 // Round the f80 to the right size, which also moves it to the appropriate
3548 // xmm register. This is accomplished by storing the f80 value in memory
3549 // and then loading it back.
3550 if (CopyVT != VA.getValVT()) {
3551 EVT ResVT = VA.getValVT();
3552 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
3553 unsigned MemSize = ResVT.getSizeInBits()/8;
3554 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
3555 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3556 TII.get(Opc)), FI)
3557 .addReg(CopyReg);
3558 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
3559 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3560 TII.get(Opc), ResultReg + i), FI);
3561 }
3562 }
3563
3564 CLI.ResultReg = ResultReg;
3565 CLI.NumResultRegs = RVLocs.size();
3566 CLI.Call = MIB;
3567
3568 return true;
3569}
3570
3571bool
3572X86FastISel::fastSelectInstruction(const Instruction *I) {
3573 switch (I->getOpcode()) {
3574 default: break;
3575 case Instruction::Load:
3576 return X86SelectLoad(I);
3577 case Instruction::Store:
3578 return X86SelectStore(I);
3579 case Instruction::Ret:
3580 return X86SelectRet(I);
3581 case Instruction::ICmp:
3582 case Instruction::FCmp:
3583 return X86SelectCmp(I);
3584 case Instruction::ZExt:
3585 return X86SelectZExt(I);
3586 case Instruction::Br:
3587 return X86SelectBranch(I);
3588 case Instruction::LShr:
3589 case Instruction::AShr:
3590 case Instruction::Shl:
3591 return X86SelectShift(I);
3592 case Instruction::SDiv:
3593 case Instruction::UDiv:
3594 case Instruction::SRem:
3595 case Instruction::URem:
3596 return X86SelectDivRem(I);
3597 case Instruction::Select:
3598 return X86SelectSelect(I);
3599 case Instruction::Trunc:
3600 return X86SelectTrunc(I);
3601 case Instruction::FPExt:
3602 return X86SelectFPExt(I);
3603 case Instruction::FPTrunc:
3604 return X86SelectFPTrunc(I);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00003605 case Instruction::SIToFP:
3606 return X86SelectSIToFP(I);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003607 case Instruction::IntToPtr: // Deliberate fall-through.
3608 case Instruction::PtrToInt: {
Mehdi Amini44ede332015-07-09 02:09:04 +00003609 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
3610 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003611 if (DstVT.bitsGT(SrcVT))
3612 return X86SelectZExt(I);
3613 if (DstVT.bitsLT(SrcVT))
3614 return X86SelectTrunc(I);
3615 unsigned Reg = getRegForValue(I->getOperand(0));
3616 if (Reg == 0) return false;
3617 updateValueMap(I, Reg);
3618 return true;
3619 }
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003620 case Instruction::BitCast: {
3621 // Select SSE2/AVX bitcasts between 128/256 bit vector types.
3622 if (!Subtarget->hasSSE2())
3623 return false;
3624
3625 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
3626 EVT DstVT = TLI.getValueType(DL, I->getType());
3627
3628 if (!SrcVT.isSimple() || !DstVT.isSimple())
3629 return false;
3630
Craig Topperdb8467a2016-12-05 05:50:51 +00003631 MVT SVT = SrcVT.getSimpleVT();
3632 MVT DVT = DstVT.getSimpleVT();
3633
3634 if (!SVT.is128BitVector() &&
3635 !(Subtarget->hasAVX() && SVT.is256BitVector()) &&
3636 !(Subtarget->hasAVX512() && SVT.is512BitVector() &&
3637 (Subtarget->hasBWI() || (SVT.getScalarSizeInBits() >= 32 &&
3638 DVT.getScalarSizeInBits() >= 32))))
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003639 return false;
3640
3641 unsigned Reg = getRegForValue(I->getOperand(0));
3642 if (Reg == 0)
3643 return false;
3644
3645 // No instruction is needed for conversion. Reuse the register used by
3646 // the fist operand.
3647 updateValueMap(I, Reg);
3648 return true;
3649 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003650 }
3651
3652 return false;
3653}
3654
3655unsigned X86FastISel::X86MaterializeInt(const ConstantInt *CI, MVT VT) {
3656 if (VT > MVT::i64)
3657 return 0;
3658
3659 uint64_t Imm = CI->getZExtValue();
3660 if (Imm == 0) {
3661 unsigned SrcReg = fastEmitInst_(X86::MOV32r0, &X86::GR32RegClass);
3662 switch (VT.SimpleTy) {
3663 default: llvm_unreachable("Unexpected value type");
3664 case MVT::i1:
Craig Topper058f2f62017-03-28 16:35:29 +00003665 if (Subtarget->hasAVX512()) {
3666 // Need to copy to a VK1 register.
3667 unsigned ResultReg = createResultReg(&X86::VK1RegClass);
3668 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3669 TII.get(TargetOpcode::COPY), ResultReg).addReg(SrcReg);
3670 return ResultReg;
3671 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003672 case MVT::i8:
3673 return fastEmitInst_extractsubreg(MVT::i8, SrcReg, /*Kill=*/true,
3674 X86::sub_8bit);
3675 case MVT::i16:
3676 return fastEmitInst_extractsubreg(MVT::i16, SrcReg, /*Kill=*/true,
3677 X86::sub_16bit);
3678 case MVT::i32:
3679 return SrcReg;
3680 case MVT::i64: {
3681 unsigned ResultReg = createResultReg(&X86::GR64RegClass);
3682 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3683 TII.get(TargetOpcode::SUBREG_TO_REG), ResultReg)
3684 .addImm(0).addReg(SrcReg).addImm(X86::sub_32bit);
3685 return ResultReg;
3686 }
3687 }
3688 }
3689
3690 unsigned Opc = 0;
3691 switch (VT.SimpleTy) {
3692 default: llvm_unreachable("Unexpected value type");
Craig Topper058f2f62017-03-28 16:35:29 +00003693 case MVT::i1:
3694 // TODO: Support this properly.
3695 if (Subtarget->hasAVX512())
3696 return 0;
3697 VT = MVT::i8;
3698 LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003699 case MVT::i8: Opc = X86::MOV8ri; break;
3700 case MVT::i16: Opc = X86::MOV16ri; break;
3701 case MVT::i32: Opc = X86::MOV32ri; break;
3702 case MVT::i64: {
3703 if (isUInt<32>(Imm))
3704 Opc = X86::MOV32ri;
3705 else if (isInt<32>(Imm))
3706 Opc = X86::MOV64ri32;
3707 else
3708 Opc = X86::MOV64ri;
3709 break;
3710 }
3711 }
3712 if (VT == MVT::i64 && Opc == X86::MOV32ri) {
3713 unsigned SrcReg = fastEmitInst_i(Opc, &X86::GR32RegClass, Imm);
3714 unsigned ResultReg = createResultReg(&X86::GR64RegClass);
3715 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3716 TII.get(TargetOpcode::SUBREG_TO_REG), ResultReg)
3717 .addImm(0).addReg(SrcReg).addImm(X86::sub_32bit);
3718 return ResultReg;
3719 }
3720 return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
3721}
3722
3723unsigned X86FastISel::X86MaterializeFP(const ConstantFP *CFP, MVT VT) {
3724 if (CFP->isNullValue())
3725 return fastMaterializeFloatZero(CFP);
3726
3727 // Can't handle alternate code models yet.
3728 CodeModel::Model CM = TM.getCodeModel();
3729 if (CM != CodeModel::Small && CM != CodeModel::Large)
3730 return 0;
3731
3732 // Get opcode and regclass of the output for the given load instruction.
3733 unsigned Opc = 0;
3734 const TargetRegisterClass *RC = nullptr;
3735 switch (VT.SimpleTy) {
3736 default: return 0;
3737 case MVT::f32:
3738 if (X86ScalarSSEf32) {
3739 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
3740 RC = &X86::FR32RegClass;
3741 } else {
3742 Opc = X86::LD_Fp32m;
3743 RC = &X86::RFP32RegClass;
3744 }
3745 break;
3746 case MVT::f64:
3747 if (X86ScalarSSEf64) {
3748 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
3749 RC = &X86::FR64RegClass;
3750 } else {
3751 Opc = X86::LD_Fp64m;
3752 RC = &X86::RFP64RegClass;
3753 }
3754 break;
3755 case MVT::f80:
3756 // No f80 support yet.
3757 return 0;
3758 }
3759
3760 // MachineConstantPool wants an explicit alignment.
3761 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
3762 if (Align == 0) {
3763 // Alignment of vector types. FIXME!
3764 Align = DL.getTypeAllocSize(CFP->getType());
3765 }
3766
3767 // x86-32 PIC requires a PIC base register for constant pools.
3768 unsigned PICBase = 0;
Rafael Espindolac7e98132016-05-20 12:20:10 +00003769 unsigned char OpFlag = Subtarget->classifyLocalReference(nullptr);
3770 if (OpFlag == X86II::MO_PIC_BASE_OFFSET)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003771 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolac7e98132016-05-20 12:20:10 +00003772 else if (OpFlag == X86II::MO_GOTOFF)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003773 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolac7e98132016-05-20 12:20:10 +00003774 else if (Subtarget->is64Bit() && TM.getCodeModel() == CodeModel::Small)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003775 PICBase = X86::RIP;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003776
3777 // Create the load from the constant pool.
3778 unsigned CPI = MCP.getConstantPoolIndex(CFP, Align);
3779 unsigned ResultReg = createResultReg(RC);
3780
3781 if (CM == CodeModel::Large) {
3782 unsigned AddrReg = createResultReg(&X86::GR64RegClass);
3783 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri),
3784 AddrReg)
3785 .addConstantPoolIndex(CPI, 0, OpFlag);
3786 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3787 TII.get(Opc), ResultReg);
3788 addDirectMem(MIB, AddrReg);
3789 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00003790 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
3791 MachineMemOperand::MOLoad, DL.getPointerSize(), Align);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003792 MIB->addMemOperand(*FuncInfo.MF, MMO);
3793 return ResultReg;
3794 }
3795
3796 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3797 TII.get(Opc), ResultReg),
3798 CPI, PICBase, OpFlag);
3799 return ResultReg;
3800}
3801
3802unsigned X86FastISel::X86MaterializeGV(const GlobalValue *GV, MVT VT) {
3803 // Can't handle alternate code models yet.
3804 if (TM.getCodeModel() != CodeModel::Small)
3805 return 0;
3806
3807 // Materialize addresses with LEA/MOV instructions.
3808 X86AddressMode AM;
3809 if (X86SelectAddress(GV, AM)) {
3810 // If the expression is just a basereg, then we're done, otherwise we need
3811 // to emit an LEA.
3812 if (AM.BaseType == X86AddressMode::RegBase &&
3813 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
3814 return AM.Base.Reg;
3815
3816 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3817 if (TM.getRelocationModel() == Reloc::Static &&
Mehdi Amini44ede332015-07-09 02:09:04 +00003818 TLI.getPointerTy(DL) == MVT::i64) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003819 // The displacement code could be more than 32 bits away so we need to use
3820 // an instruction with a 64 bit immediate
3821 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri),
3822 ResultReg)
3823 .addGlobalAddress(GV);
3824 } else {
Mehdi Amini44ede332015-07-09 02:09:04 +00003825 unsigned Opc =
3826 TLI.getPointerTy(DL) == MVT::i32
3827 ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r)
3828 : X86::LEA64r;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003829 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3830 TII.get(Opc), ResultReg), AM);
3831 }
3832 return ResultReg;
3833 }
3834 return 0;
3835}
3836
3837unsigned X86FastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003838 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003839
3840 // Only handle simple types.
3841 if (!CEVT.isSimple())
3842 return 0;
3843 MVT VT = CEVT.getSimpleVT();
3844
3845 if (const auto *CI = dyn_cast<ConstantInt>(C))
3846 return X86MaterializeInt(CI, VT);
3847 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
3848 return X86MaterializeFP(CFP, VT);
3849 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
3850 return X86MaterializeGV(GV, VT);
3851
3852 return 0;
3853}
3854
3855unsigned X86FastISel::fastMaterializeAlloca(const AllocaInst *C) {
3856 // Fail on dynamic allocas. At this point, getRegForValue has already
3857 // checked its CSE maps, so if we're here trying to handle a dynamic
3858 // alloca, we're not going to succeed. X86SelectAddress has a
3859 // check for dynamic allocas, because it's called directly from
3860 // various places, but targetMaterializeAlloca also needs a check
3861 // in order to avoid recursion between getRegForValue,
3862 // X86SelectAddrss, and targetMaterializeAlloca.
3863 if (!FuncInfo.StaticAllocaMap.count(C))
3864 return 0;
3865 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
3866
3867 X86AddressMode AM;
3868 if (!X86SelectAddress(C, AM))
3869 return 0;
Mehdi Amini44ede332015-07-09 02:09:04 +00003870 unsigned Opc =
3871 TLI.getPointerTy(DL) == MVT::i32
3872 ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r)
3873 : X86::LEA64r;
3874 const TargetRegisterClass *RC = TLI.getRegClassFor(TLI.getPointerTy(DL));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003875 unsigned ResultReg = createResultReg(RC);
3876 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3877 TII.get(Opc), ResultReg), AM);
3878 return ResultReg;
3879}
3880
3881unsigned X86FastISel::fastMaterializeFloatZero(const ConstantFP *CF) {
3882 MVT VT;
3883 if (!isTypeLegal(CF->getType(), VT))
3884 return 0;
3885
3886 // Get opcode and regclass for the given zero.
3887 unsigned Opc = 0;
3888 const TargetRegisterClass *RC = nullptr;
3889 switch (VT.SimpleTy) {
3890 default: return 0;
3891 case MVT::f32:
3892 if (X86ScalarSSEf32) {
3893 Opc = X86::FsFLD0SS;
3894 RC = &X86::FR32RegClass;
3895 } else {
3896 Opc = X86::LD_Fp032;
3897 RC = &X86::RFP32RegClass;
3898 }
3899 break;
3900 case MVT::f64:
3901 if (X86ScalarSSEf64) {
3902 Opc = X86::FsFLD0SD;
3903 RC = &X86::FR64RegClass;
3904 } else {
3905 Opc = X86::LD_Fp064;
3906 RC = &X86::RFP64RegClass;
3907 }
3908 break;
3909 case MVT::f80:
3910 // No f80 support yet.
3911 return 0;
3912 }
3913
3914 unsigned ResultReg = createResultReg(RC);
3915 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
3916 return ResultReg;
3917}
3918
3919
3920bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3921 const LoadInst *LI) {
3922 const Value *Ptr = LI->getPointerOperand();
3923 X86AddressMode AM;
3924 if (!X86SelectAddress(Ptr, AM))
3925 return false;
3926
3927 const X86InstrInfo &XII = (const X86InstrInfo &)TII;
3928
3929 unsigned Size = DL.getTypeAllocSize(LI->getType());
3930 unsigned Alignment = LI->getAlignment();
3931
3932 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3933 Alignment = DL.getABITypeAlignment(LI->getType());
3934
3935 SmallVector<MachineOperand, 8> AddrOps;
3936 AM.getFullAddress(AddrOps);
3937
Keno Fischere70b31f2015-06-08 20:09:58 +00003938 MachineInstr *Result = XII.foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003939 *FuncInfo.MF, *MI, OpNo, AddrOps, FuncInfo.InsertPt, Size, Alignment,
Keno Fischere70b31f2015-06-08 20:09:58 +00003940 /*AllowCommute=*/true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003941 if (!Result)
3942 return false;
3943
Pete Cooperd31583d2015-05-06 21:37:19 +00003944 // The index register could be in the wrong register class. Unfortunately,
3945 // foldMemoryOperandImpl could have commuted the instruction so its not enough
3946 // to just look at OpNo + the offset to the index reg. We actually need to
3947 // scan the instruction to find the index reg and see if its the correct reg
3948 // class.
Matthias Braune41e1462015-05-29 02:56:46 +00003949 unsigned OperandNo = 0;
3950 for (MachineInstr::mop_iterator I = Result->operands_begin(),
3951 E = Result->operands_end(); I != E; ++I, ++OperandNo) {
3952 MachineOperand &MO = *I;
3953 if (!MO.isReg() || MO.isDef() || MO.getReg() != AM.IndexReg)
Pete Cooperd31583d2015-05-06 21:37:19 +00003954 continue;
3955 // Found the index reg, now try to rewrite it.
Pete Cooperd31583d2015-05-06 21:37:19 +00003956 unsigned IndexReg = constrainOperandRegClass(Result->getDesc(),
Matthias Braune41e1462015-05-29 02:56:46 +00003957 MO.getReg(), OperandNo);
3958 if (IndexReg == MO.getReg())
Pete Cooperd31583d2015-05-06 21:37:19 +00003959 continue;
Matthias Braune41e1462015-05-29 02:56:46 +00003960 MO.setReg(IndexReg);
Pete Cooperd31583d2015-05-06 21:37:19 +00003961 }
3962
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003963 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003964 MI->eraseFromParent();
3965 return true;
3966}
3967
Craig Topper7ef6ea32016-12-05 04:51:31 +00003968unsigned X86FastISel::fastEmitInst_rrrr(unsigned MachineInstOpcode,
3969 const TargetRegisterClass *RC,
3970 unsigned Op0, bool Op0IsKill,
3971 unsigned Op1, bool Op1IsKill,
3972 unsigned Op2, bool Op2IsKill,
3973 unsigned Op3, bool Op3IsKill) {
3974 const MCInstrDesc &II = TII.get(MachineInstOpcode);
3975
3976 unsigned ResultReg = createResultReg(RC);
3977 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
3978 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
3979 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
3980 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 3);
3981
3982 if (II.getNumDefs() >= 1)
3983 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
3984 .addReg(Op0, getKillRegState(Op0IsKill))
3985 .addReg(Op1, getKillRegState(Op1IsKill))
3986 .addReg(Op2, getKillRegState(Op2IsKill))
3987 .addReg(Op3, getKillRegState(Op3IsKill));
3988 else {
3989 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
3990 .addReg(Op0, getKillRegState(Op0IsKill))
3991 .addReg(Op1, getKillRegState(Op1IsKill))
3992 .addReg(Op2, getKillRegState(Op2IsKill))
3993 .addReg(Op3, getKillRegState(Op3IsKill));
3994 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3995 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
3996 }
3997 return ResultReg;
3998}
3999
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00004000
4001namespace llvm {
4002 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
4003 const TargetLibraryInfo *libInfo) {
4004 return new X86FastISel(funcInfo, libInfo);
4005 }
4006}