blob: 928a37a1408efc7f83eece22b80b2969bb314211 [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
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000183static std::pair<unsigned, bool>
184getX86SSEConditionCode(CmpInst::Predicate Predicate) {
185 unsigned CC;
186 bool NeedSwap = false;
187
188 // SSE Condition code mapping:
189 // 0 - EQ
190 // 1 - LT
191 // 2 - LE
192 // 3 - UNORD
193 // 4 - NEQ
194 // 5 - NLT
195 // 6 - NLE
196 // 7 - ORD
197 switch (Predicate) {
198 default: llvm_unreachable("Unexpected predicate");
199 case CmpInst::FCMP_OEQ: CC = 0; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000200 case CmpInst::FCMP_OGT: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000201 case CmpInst::FCMP_OLT: CC = 1; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000202 case CmpInst::FCMP_OGE: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000203 case CmpInst::FCMP_OLE: CC = 2; break;
204 case CmpInst::FCMP_UNO: CC = 3; break;
205 case CmpInst::FCMP_UNE: CC = 4; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000206 case CmpInst::FCMP_ULE: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000207 case CmpInst::FCMP_UGE: CC = 5; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000208 case CmpInst::FCMP_ULT: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000209 case CmpInst::FCMP_UGT: CC = 6; break;
210 case CmpInst::FCMP_ORD: CC = 7; break;
211 case CmpInst::FCMP_UEQ:
212 case CmpInst::FCMP_ONE: CC = 8; break;
213 }
214
215 return std::make_pair(CC, NeedSwap);
216}
217
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000218/// \brief Adds a complex addressing mode to the given machine instr builder.
219/// Note, this will constrain the index register. If its not possible to
220/// constrain the given index register, then a new one will be created. The
221/// IndexReg field of the addressing mode will be updated to match in this case.
222const MachineInstrBuilder &
223X86FastISel::addFullAddress(const MachineInstrBuilder &MIB,
224 X86AddressMode &AM) {
225 // First constrain the index register. It needs to be a GR64_NOSP.
226 AM.IndexReg = constrainOperandRegClass(MIB->getDesc(), AM.IndexReg,
227 MIB->getNumOperands() +
228 X86::AddrIndexReg);
229 return ::addFullAddress(MIB, AM);
230}
231
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000232/// \brief Check if it is possible to fold the condition from the XALU intrinsic
233/// into the user. The condition code will only be updated on success.
234bool X86FastISel::foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
235 const Value *Cond) {
236 if (!isa<ExtractValueInst>(Cond))
237 return false;
238
239 const auto *EV = cast<ExtractValueInst>(Cond);
240 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
241 return false;
242
243 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
244 MVT RetVT;
245 const Function *Callee = II->getCalledFunction();
246 Type *RetTy =
247 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
248 if (!isTypeLegal(RetTy, RetVT))
249 return false;
250
251 if (RetVT != MVT::i32 && RetVT != MVT::i64)
252 return false;
253
254 X86::CondCode TmpCC;
255 switch (II->getIntrinsicID()) {
256 default: return false;
257 case Intrinsic::sadd_with_overflow:
258 case Intrinsic::ssub_with_overflow:
259 case Intrinsic::smul_with_overflow:
260 case Intrinsic::umul_with_overflow: TmpCC = X86::COND_O; break;
261 case Intrinsic::uadd_with_overflow:
262 case Intrinsic::usub_with_overflow: TmpCC = X86::COND_B; break;
263 }
264
265 // Check if both instructions are in the same basic block.
266 if (II->getParent() != I->getParent())
267 return false;
268
269 // Make sure nothing is in the way
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000270 BasicBlock::const_iterator Start(I);
271 BasicBlock::const_iterator End(II);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000272 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
273 // We only expect extractvalue instructions between the intrinsic and the
274 // instruction to be selected.
275 if (!isa<ExtractValueInst>(Itr))
276 return false;
277
278 // Check that the extractvalue operand comes from the intrinsic.
279 const auto *EVI = cast<ExtractValueInst>(Itr);
280 if (EVI->getAggregateOperand() != II)
281 return false;
282 }
283
284 CC = TmpCC;
285 return true;
286}
287
288bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000289 EVT evt = TLI.getValueType(DL, Ty, /*HandleUnknown=*/true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000290 if (evt == MVT::Other || !evt.isSimple())
291 // Unhandled type. Halt "fast" selection and bail.
292 return false;
293
294 VT = evt.getSimpleVT();
295 // For now, require SSE/SSE2 for performing floating-point operations,
296 // since x87 requires additional work.
297 if (VT == MVT::f64 && !X86ScalarSSEf64)
298 return false;
299 if (VT == MVT::f32 && !X86ScalarSSEf32)
300 return false;
301 // Similarly, no f80 support yet.
302 if (VT == MVT::f80)
303 return false;
304 // We only handle legal types. For example, on x86-32 the instruction
305 // selector contains all of the 64-bit instructions from x86-64,
306 // under the assumption that i64 won't be used if the target doesn't
307 // support it.
308 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
309}
310
311#include "X86GenCallingConv.inc"
312
313/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
314/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
315/// Return true and the result register by reference if it is possible.
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000316bool X86FastISel::X86FastEmitLoad(EVT VT, X86AddressMode &AM,
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000317 MachineMemOperand *MMO, unsigned &ResultReg,
318 unsigned Alignment) {
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000319 bool HasSSE41 = Subtarget->hasSSE41();
Craig Topperca9c0802016-06-02 04:19:45 +0000320 bool HasAVX = Subtarget->hasAVX();
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000321 bool HasAVX2 = Subtarget->hasAVX2();
Craig Topperdfc4fc92016-09-05 23:58:40 +0000322 bool HasAVX512 = Subtarget->hasAVX512();
323 bool HasVLX = Subtarget->hasVLX();
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000324 bool IsNonTemporal = MMO && MMO->isNonTemporal();
325
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000326 // Get opcode and regclass of the output for the given load instruction.
327 unsigned Opc = 0;
328 const TargetRegisterClass *RC = nullptr;
329 switch (VT.getSimpleVT().SimpleTy) {
330 default: return false;
331 case MVT::i1:
332 case MVT::i8:
333 Opc = X86::MOV8rm;
334 RC = &X86::GR8RegClass;
335 break;
336 case MVT::i16:
337 Opc = X86::MOV16rm;
338 RC = &X86::GR16RegClass;
339 break;
340 case MVT::i32:
341 Opc = X86::MOV32rm;
342 RC = &X86::GR32RegClass;
343 break;
344 case MVT::i64:
345 // Must be in x86-64 mode.
346 Opc = X86::MOV64rm;
347 RC = &X86::GR64RegClass;
348 break;
349 case MVT::f32:
350 if (X86ScalarSSEf32) {
Craig Topperdfc4fc92016-09-05 23:58:40 +0000351 Opc = HasAVX512 ? X86::VMOVSSZrm : HasAVX ? X86::VMOVSSrm : X86::MOVSSrm;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000352 RC = &X86::FR32RegClass;
353 } else {
354 Opc = X86::LD_Fp32m;
355 RC = &X86::RFP32RegClass;
356 }
357 break;
358 case MVT::f64:
359 if (X86ScalarSSEf64) {
Craig Topperdfc4fc92016-09-05 23:58:40 +0000360 Opc = HasAVX512 ? X86::VMOVSDZrm : HasAVX ? X86::VMOVSDrm : X86::MOVSDrm;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000361 RC = &X86::FR64RegClass;
362 } else {
363 Opc = X86::LD_Fp64m;
364 RC = &X86::RFP64RegClass;
365 }
366 break;
367 case MVT::f80:
368 // No f80 support yet.
369 return false;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000370 case MVT::v4f32:
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000371 if (IsNonTemporal && Alignment >= 16 && HasSSE41)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000372 Opc = HasVLX ? X86::VMOVNTDQAZ128rm :
373 HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000374 else if (Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000375 Opc = HasVLX ? X86::VMOVAPSZ128rm :
376 HasAVX ? X86::VMOVAPSrm : X86::MOVAPSrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000377 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000378 Opc = HasVLX ? X86::VMOVUPSZ128rm :
379 HasAVX ? X86::VMOVUPSrm : X86::MOVUPSrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000380 RC = &X86::VR128RegClass;
381 break;
382 case MVT::v2f64:
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000383 if (IsNonTemporal && Alignment >= 16 && HasSSE41)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000384 Opc = HasVLX ? X86::VMOVNTDQAZ128rm :
385 HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000386 else if (Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000387 Opc = HasVLX ? X86::VMOVAPDZ128rm :
388 HasAVX ? X86::VMOVAPDrm : X86::MOVAPDrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000389 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000390 Opc = HasVLX ? X86::VMOVUPDZ128rm :
391 HasAVX ? X86::VMOVUPDrm : X86::MOVUPDrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000392 RC = &X86::VR128RegClass;
393 break;
394 case MVT::v4i32:
395 case MVT::v2i64:
396 case MVT::v8i16:
397 case MVT::v16i8:
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000398 if (IsNonTemporal && Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000399 Opc = HasVLX ? X86::VMOVNTDQAZ128rm :
400 HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000401 else if (Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000402 Opc = HasVLX ? X86::VMOVDQA64Z128rm :
403 HasAVX ? X86::VMOVDQArm : X86::MOVDQArm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000404 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000405 Opc = HasVLX ? X86::VMOVDQU64Z128rm :
406 HasAVX ? X86::VMOVDQUrm : X86::MOVDQUrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000407 RC = &X86::VR128RegClass;
408 break;
Craig Topperca9c0802016-06-02 04:19:45 +0000409 case MVT::v8f32:
410 assert(HasAVX);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000411 if (IsNonTemporal && Alignment >= 32 && HasAVX2)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000412 Opc = HasVLX ? X86::VMOVNTDQAZ256rm : X86::VMOVNTDQAYrm;
Simon Pilgrimf7113fd2017-06-06 14:18:39 +0000413 else if (IsNonTemporal && Alignment >= 16)
414 return false; // Force split for X86::VMOVNTDQArm
Craig Topperdfc4fc92016-09-05 23:58:40 +0000415 else if (Alignment >= 32)
416 Opc = HasVLX ? X86::VMOVAPSZ256rm : X86::VMOVAPSYrm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000417 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000418 Opc = HasVLX ? X86::VMOVUPSZ256rm : X86::VMOVUPSYrm;
Craig Topperca9c0802016-06-02 04:19:45 +0000419 RC = &X86::VR256RegClass;
420 break;
421 case MVT::v4f64:
422 assert(HasAVX);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000423 if (IsNonTemporal && Alignment >= 32 && HasAVX2)
424 Opc = X86::VMOVNTDQAYrm;
Simon Pilgrimf7113fd2017-06-06 14:18:39 +0000425 else if (IsNonTemporal && Alignment >= 16)
426 return false; // Force split for X86::VMOVNTDQArm
Craig Topperdfc4fc92016-09-05 23:58:40 +0000427 else if (Alignment >= 32)
428 Opc = HasVLX ? X86::VMOVAPDZ256rm : X86::VMOVAPDYrm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000429 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000430 Opc = HasVLX ? X86::VMOVUPDZ256rm : X86::VMOVUPDYrm;
Craig Topperca9c0802016-06-02 04:19:45 +0000431 RC = &X86::VR256RegClass;
432 break;
433 case MVT::v8i32:
434 case MVT::v4i64:
435 case MVT::v16i16:
436 case MVT::v32i8:
437 assert(HasAVX);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000438 if (IsNonTemporal && Alignment >= 32 && HasAVX2)
439 Opc = X86::VMOVNTDQAYrm;
Simon Pilgrimf7113fd2017-06-06 14:18:39 +0000440 else if (IsNonTemporal && Alignment >= 16)
441 return false; // Force split for X86::VMOVNTDQArm
Craig Topperdfc4fc92016-09-05 23:58:40 +0000442 else if (Alignment >= 32)
443 Opc = HasVLX ? X86::VMOVDQA64Z256rm : X86::VMOVDQAYrm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000444 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000445 Opc = HasVLX ? X86::VMOVDQU64Z256rm : X86::VMOVDQUYrm;
Craig Topperca9c0802016-06-02 04:19:45 +0000446 RC = &X86::VR256RegClass;
447 break;
Craig Topper048a08a2016-06-02 04:51:37 +0000448 case MVT::v16f32:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000449 assert(HasAVX512);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000450 if (IsNonTemporal && Alignment >= 64)
451 Opc = X86::VMOVNTDQAZrm;
452 else
453 Opc = (Alignment >= 64) ? X86::VMOVAPSZrm : X86::VMOVUPSZrm;
Craig Topper048a08a2016-06-02 04:51:37 +0000454 RC = &X86::VR512RegClass;
455 break;
456 case MVT::v8f64:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000457 assert(HasAVX512);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000458 if (IsNonTemporal && Alignment >= 64)
459 Opc = X86::VMOVNTDQAZrm;
460 else
461 Opc = (Alignment >= 64) ? X86::VMOVAPDZrm : X86::VMOVUPDZrm;
Craig Topper048a08a2016-06-02 04:51:37 +0000462 RC = &X86::VR512RegClass;
463 break;
464 case MVT::v8i64:
465 case MVT::v16i32:
466 case MVT::v32i16:
467 case MVT::v64i8:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000468 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000469 // Note: There are a lot more choices based on type with AVX-512, but
470 // there's really no advantage when the load isn't masked.
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000471 if (IsNonTemporal && Alignment >= 64)
472 Opc = X86::VMOVNTDQAZrm;
473 else
474 Opc = (Alignment >= 64) ? X86::VMOVDQA64Zrm : X86::VMOVDQU64Zrm;
Craig Topper048a08a2016-06-02 04:51:37 +0000475 RC = &X86::VR512RegClass;
476 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000477 }
478
479 ResultReg = createResultReg(RC);
480 MachineInstrBuilder MIB =
481 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
482 addFullAddress(MIB, AM);
483 if (MMO)
484 MIB->addMemOperand(*FuncInfo.MF, MMO);
485 return true;
486}
487
488/// X86FastEmitStore - Emit a machine instruction to store a value Val of
489/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
490/// and a displacement offset, or a GlobalAddress,
491/// i.e. V. Return true if it is possible.
492bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000493 X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000494 MachineMemOperand *MMO, bool Aligned) {
Simon Pilgrimb6702ea2017-04-10 16:58:07 +0000495 bool HasSSE1 = Subtarget->hasSSE1();
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000496 bool HasSSE2 = Subtarget->hasSSE2();
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000497 bool HasSSE4A = Subtarget->hasSSE4A();
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000498 bool HasAVX = Subtarget->hasAVX();
Craig Topperdfc4fc92016-09-05 23:58:40 +0000499 bool HasAVX512 = Subtarget->hasAVX512();
500 bool HasVLX = Subtarget->hasVLX();
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000501 bool IsNonTemporal = MMO && MMO->isNonTemporal();
502
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000503 // Get opcode and regclass of the output for the given store instruction.
504 unsigned Opc = 0;
505 switch (VT.getSimpleVT().SimpleTy) {
506 case MVT::f80: // No f80 support yet.
507 default: return false;
508 case MVT::i1: {
509 // Mask out all but lowest bit.
510 unsigned AndResult = createResultReg(&X86::GR8RegClass);
511 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
512 TII.get(X86::AND8ri), AndResult)
513 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
514 ValReg = AndResult;
Justin Bognerb03fd122016-08-17 05:10:15 +0000515 LLVM_FALLTHROUGH; // handle i1 as i8.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000516 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000517 case MVT::i8: Opc = X86::MOV8mr; break;
518 case MVT::i16: Opc = X86::MOV16mr; break;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000519 case MVT::i32:
520 Opc = (IsNonTemporal && HasSSE2) ? X86::MOVNTImr : X86::MOV32mr;
521 break;
522 case MVT::i64:
523 // Must be in x86-64 mode.
524 Opc = (IsNonTemporal && HasSSE2) ? X86::MOVNTI_64mr : X86::MOV64mr;
525 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000526 case MVT::f32:
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000527 if (X86ScalarSSEf32) {
528 if (IsNonTemporal && HasSSE4A)
529 Opc = X86::MOVNTSS;
530 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000531 Opc = HasAVX512 ? X86::VMOVSSZmr :
532 HasAVX ? X86::VMOVSSmr : X86::MOVSSmr;
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000533 } else
534 Opc = X86::ST_Fp32m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000535 break;
536 case MVT::f64:
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000537 if (X86ScalarSSEf32) {
538 if (IsNonTemporal && HasSSE4A)
539 Opc = X86::MOVNTSD;
540 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000541 Opc = HasAVX512 ? X86::VMOVSDZmr :
542 HasAVX ? X86::VMOVSDmr : X86::MOVSDmr;
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000543 } else
544 Opc = X86::ST_Fp64m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000545 break;
Simon Pilgrimb6702ea2017-04-10 16:58:07 +0000546 case MVT::x86mmx:
547 Opc = (IsNonTemporal && HasSSE1) ? X86::MMX_MOVNTQmr : X86::MMX_MOVQ64mr;
548 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000549 case MVT::v4f32:
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000550 if (Aligned) {
551 if (IsNonTemporal)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000552 Opc = HasVLX ? X86::VMOVNTPSZ128mr :
553 HasAVX ? X86::VMOVNTPSmr : X86::MOVNTPSmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000554 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000555 Opc = HasVLX ? X86::VMOVAPSZ128mr :
556 HasAVX ? X86::VMOVAPSmr : X86::MOVAPSmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000557 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000558 Opc = HasVLX ? X86::VMOVUPSZ128mr :
559 HasAVX ? X86::VMOVUPSmr : X86::MOVUPSmr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000560 break;
561 case MVT::v2f64:
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000562 if (Aligned) {
563 if (IsNonTemporal)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000564 Opc = HasVLX ? X86::VMOVNTPDZ128mr :
565 HasAVX ? X86::VMOVNTPDmr : X86::MOVNTPDmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000566 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000567 Opc = HasVLX ? X86::VMOVAPDZ128mr :
568 HasAVX ? X86::VMOVAPDmr : X86::MOVAPDmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000569 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000570 Opc = HasVLX ? X86::VMOVUPDZ128mr :
571 HasAVX ? X86::VMOVUPDmr : X86::MOVUPDmr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000572 break;
573 case MVT::v4i32:
574 case MVT::v2i64:
575 case MVT::v8i16:
576 case MVT::v16i8:
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000577 if (Aligned) {
578 if (IsNonTemporal)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000579 Opc = HasVLX ? X86::VMOVNTDQZ128mr :
580 HasAVX ? X86::VMOVNTDQmr : X86::MOVNTDQmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000581 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000582 Opc = HasVLX ? X86::VMOVDQA64Z128mr :
583 HasAVX ? X86::VMOVDQAmr : X86::MOVDQAmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000584 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000585 Opc = HasVLX ? X86::VMOVDQU64Z128mr :
586 HasAVX ? X86::VMOVDQUmr : X86::MOVDQUmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000587 break;
588 case MVT::v8f32:
589 assert(HasAVX);
Craig Topperdfc4fc92016-09-05 23:58:40 +0000590 if (Aligned) {
591 if (IsNonTemporal)
592 Opc = HasVLX ? X86::VMOVNTPSZ256mr : X86::VMOVNTPSYmr;
593 else
594 Opc = HasVLX ? X86::VMOVAPSZ256mr : X86::VMOVAPSYmr;
595 } else
596 Opc = HasVLX ? X86::VMOVUPSZ256mr : X86::VMOVUPSYmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000597 break;
598 case MVT::v4f64:
599 assert(HasAVX);
600 if (Aligned) {
Craig Topperdfc4fc92016-09-05 23:58:40 +0000601 if (IsNonTemporal)
602 Opc = HasVLX ? X86::VMOVNTPDZ256mr : X86::VMOVNTPDYmr;
603 else
604 Opc = HasVLX ? X86::VMOVAPDZ256mr : X86::VMOVAPDYmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000605 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000606 Opc = HasVLX ? X86::VMOVUPDZ256mr : X86::VMOVUPDYmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000607 break;
608 case MVT::v8i32:
609 case MVT::v4i64:
610 case MVT::v16i16:
611 case MVT::v32i8:
612 assert(HasAVX);
Craig Topperdfc4fc92016-09-05 23:58:40 +0000613 if (Aligned) {
614 if (IsNonTemporal)
615 Opc = HasVLX ? X86::VMOVNTDQZ256mr : X86::VMOVNTDQYmr;
616 else
617 Opc = HasVLX ? X86::VMOVDQA64Z256mr : X86::VMOVDQAYmr;
618 } else
619 Opc = HasVLX ? X86::VMOVDQU64Z256mr : X86::VMOVDQUYmr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000620 break;
Craig Topper048a08a2016-06-02 04:51:37 +0000621 case MVT::v16f32:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000622 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000623 if (Aligned)
624 Opc = IsNonTemporal ? X86::VMOVNTPSZmr : X86::VMOVAPSZmr;
625 else
626 Opc = X86::VMOVUPSZmr;
627 break;
628 case MVT::v8f64:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000629 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000630 if (Aligned) {
631 Opc = IsNonTemporal ? X86::VMOVNTPDZmr : X86::VMOVAPDZmr;
632 } else
633 Opc = X86::VMOVUPDZmr;
634 break;
635 case MVT::v8i64:
636 case MVT::v16i32:
637 case MVT::v32i16:
638 case MVT::v64i8:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000639 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000640 // Note: There are a lot more choices based on type with AVX-512, but
641 // there's really no advantage when the store isn't masked.
642 if (Aligned)
643 Opc = IsNonTemporal ? X86::VMOVNTDQZmr : X86::VMOVDQA64Zmr;
644 else
645 Opc = X86::VMOVDQU64Zmr;
646 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000647 }
648
Quentin Colombetbf200682016-04-27 22:33:42 +0000649 const MCInstrDesc &Desc = TII.get(Opc);
650 // Some of the instructions in the previous switch use FR128 instead
651 // of FR32 for ValReg. Make sure the register we feed the instruction
652 // matches its register class constraints.
653 // Note: This is fine to do a copy from FR32 to FR128, this is the
654 // same registers behind the scene and actually why it did not trigger
655 // any bugs before.
656 ValReg = constrainOperandRegClass(Desc, ValReg, Desc.getNumOperands() - 1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000657 MachineInstrBuilder MIB =
Quentin Colombetbf200682016-04-27 22:33:42 +0000658 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, Desc);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000659 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
660 if (MMO)
661 MIB->addMemOperand(*FuncInfo.MF, MMO);
662
663 return true;
664}
665
666bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000667 X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000668 MachineMemOperand *MMO, bool Aligned) {
669 // Handle 'null' like i32/i64 0.
670 if (isa<ConstantPointerNull>(Val))
671 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
672
673 // If this is a store of a simple constant, fold the constant into the store.
674 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
675 unsigned Opc = 0;
676 bool Signed = true;
677 switch (VT.getSimpleVT().SimpleTy) {
678 default: break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000679 case MVT::i1:
680 Signed = false;
681 LLVM_FALLTHROUGH; // Handle as i8.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000682 case MVT::i8: Opc = X86::MOV8mi; break;
683 case MVT::i16: Opc = X86::MOV16mi; break;
684 case MVT::i32: Opc = X86::MOV32mi; break;
685 case MVT::i64:
686 // Must be a 32-bit sign extended value.
687 if (isInt<32>(CI->getSExtValue()))
688 Opc = X86::MOV64mi32;
689 break;
690 }
691
692 if (Opc) {
693 MachineInstrBuilder MIB =
694 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
695 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
696 : CI->getZExtValue());
697 if (MMO)
698 MIB->addMemOperand(*FuncInfo.MF, MMO);
699 return true;
700 }
701 }
702
703 unsigned ValReg = getRegForValue(Val);
704 if (ValReg == 0)
705 return false;
706
707 bool ValKill = hasTrivialKill(Val);
708 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
709}
710
711/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
712/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
713/// ISD::SIGN_EXTEND).
714bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
715 unsigned Src, EVT SrcVT,
716 unsigned &ResultReg) {
717 unsigned RR = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
718 Src, /*TODO: Kill=*/false);
719 if (RR == 0)
720 return false;
721
722 ResultReg = RR;
723 return true;
724}
725
726bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
727 // Handle constant address.
728 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
729 // Can't handle alternate code models yet.
730 if (TM.getCodeModel() != CodeModel::Small)
731 return false;
732
733 // Can't handle TLS yet.
734 if (GV->isThreadLocal())
735 return false;
736
737 // RIP-relative addresses can't have additional register operands, so if
738 // we've already folded stuff into the addressing mode, just force the
739 // global value into its own register, which we can use as the basereg.
740 if (!Subtarget->isPICStyleRIPRel() ||
741 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
742 // Okay, we've committed to selecting this global. Set up the address.
743 AM.GV = GV;
744
745 // Allow the subtarget to classify the global.
Rafael Espindolaab03eb02016-05-19 22:07:57 +0000746 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000747
748 // If this reference is relative to the pic base, set it now.
749 if (isGlobalRelativeToPICBase(GVFlags)) {
750 // FIXME: How do we know Base.Reg is free??
751 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
752 }
753
754 // Unless the ABI requires an extra load, return a direct reference to
755 // the global.
756 if (!isGlobalStubReference(GVFlags)) {
757 if (Subtarget->isPICStyleRIPRel()) {
758 // Use rip-relative addressing if we can. Above we verified that the
759 // base and index registers are unused.
760 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
761 AM.Base.Reg = X86::RIP;
762 }
763 AM.GVOpFlags = GVFlags;
764 return true;
765 }
766
767 // Ok, we need to do a load from a stub. If we've already loaded from
768 // this stub, reuse the loaded pointer, otherwise emit the load now.
769 DenseMap<const Value *, unsigned>::iterator I = LocalValueMap.find(V);
770 unsigned LoadReg;
771 if (I != LocalValueMap.end() && I->second != 0) {
772 LoadReg = I->second;
773 } else {
774 // Issue load from stub.
775 unsigned Opc = 0;
776 const TargetRegisterClass *RC = nullptr;
777 X86AddressMode StubAM;
778 StubAM.Base.Reg = AM.Base.Reg;
779 StubAM.GV = GV;
780 StubAM.GVOpFlags = GVFlags;
781
782 // Prepare for inserting code in the local-value area.
783 SavePoint SaveInsertPt = enterLocalValueArea();
784
Mehdi Amini44ede332015-07-09 02:09:04 +0000785 if (TLI.getPointerTy(DL) == MVT::i64) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000786 Opc = X86::MOV64rm;
787 RC = &X86::GR64RegClass;
788
789 if (Subtarget->isPICStyleRIPRel())
790 StubAM.Base.Reg = X86::RIP;
791 } else {
792 Opc = X86::MOV32rm;
793 RC = &X86::GR32RegClass;
794 }
795
796 LoadReg = createResultReg(RC);
797 MachineInstrBuilder LoadMI =
798 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
799 addFullAddress(LoadMI, StubAM);
800
801 // Ok, back to normal mode.
802 leaveLocalValueArea(SaveInsertPt);
803
804 // Prevent loading GV stub multiple times in same MBB.
805 LocalValueMap[V] = LoadReg;
806 }
807
808 // Now construct the final address. Note that the Disp, Scale,
809 // and Index values may already be set here.
810 AM.Base.Reg = LoadReg;
811 AM.GV = nullptr;
812 return true;
813 }
814 }
815
816 // If all else fails, try to materialize the value in a register.
817 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
818 if (AM.Base.Reg == 0) {
819 AM.Base.Reg = getRegForValue(V);
820 return AM.Base.Reg != 0;
821 }
822 if (AM.IndexReg == 0) {
823 assert(AM.Scale == 1 && "Scale with no index!");
824 AM.IndexReg = getRegForValue(V);
825 return AM.IndexReg != 0;
826 }
827 }
828
829 return false;
830}
831
832/// X86SelectAddress - Attempt to fill in an address from the given value.
833///
834bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
835 SmallVector<const Value *, 32> GEPs;
836redo_gep:
837 const User *U = nullptr;
838 unsigned Opcode = Instruction::UserOp1;
839 if (const Instruction *I = dyn_cast<Instruction>(V)) {
840 // Don't walk into other basic blocks; it's possible we haven't
841 // visited them yet, so the instructions may not yet be assigned
842 // virtual registers.
843 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
844 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
845 Opcode = I->getOpcode();
846 U = I;
847 }
848 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
849 Opcode = C->getOpcode();
850 U = C;
851 }
852
853 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
854 if (Ty->getAddressSpace() > 255)
855 // Fast instruction selection doesn't support the special
856 // address spaces.
857 return false;
858
859 switch (Opcode) {
860 default: break;
861 case Instruction::BitCast:
862 // Look past bitcasts.
863 return X86SelectAddress(U->getOperand(0), AM);
864
865 case Instruction::IntToPtr:
866 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000867 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
868 TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000869 return X86SelectAddress(U->getOperand(0), AM);
870 break;
871
872 case Instruction::PtrToInt:
873 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000874 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000875 return X86SelectAddress(U->getOperand(0), AM);
876 break;
877
878 case Instruction::Alloca: {
879 // Do static allocas.
880 const AllocaInst *A = cast<AllocaInst>(V);
881 DenseMap<const AllocaInst *, int>::iterator SI =
882 FuncInfo.StaticAllocaMap.find(A);
883 if (SI != FuncInfo.StaticAllocaMap.end()) {
884 AM.BaseType = X86AddressMode::FrameIndexBase;
885 AM.Base.FrameIndex = SI->second;
886 return true;
887 }
888 break;
889 }
890
891 case Instruction::Add: {
892 // Adds of constants are common and easy enough.
893 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
894 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
895 // They have to fit in the 32-bit signed displacement field though.
896 if (isInt<32>(Disp)) {
897 AM.Disp = (uint32_t)Disp;
898 return X86SelectAddress(U->getOperand(0), AM);
899 }
900 }
901 break;
902 }
903
904 case Instruction::GetElementPtr: {
905 X86AddressMode SavedAM = AM;
906
907 // Pattern-match simple GEPs.
908 uint64_t Disp = (int32_t)AM.Disp;
909 unsigned IndexReg = AM.IndexReg;
910 unsigned Scale = AM.Scale;
911 gep_type_iterator GTI = gep_type_begin(U);
912 // Iterate through the indices, folding what we can. Constants can be
913 // folded, and one dynamic index can be handled, if the scale is supported.
914 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
915 i != e; ++i, ++GTI) {
916 const Value *Op = *i;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000917 if (StructType *STy = GTI.getStructTypeOrNull()) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000918 const StructLayout *SL = DL.getStructLayout(STy);
919 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
920 continue;
921 }
922
923 // A array/variable index is always of the form i*S where S is the
924 // constant scale size. See if we can push the scale into immediates.
925 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
926 for (;;) {
927 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
928 // Constant-offset addressing.
929 Disp += CI->getSExtValue() * S;
930 break;
931 }
932 if (canFoldAddIntoGEP(U, Op)) {
933 // A compatible add with a constant operand. Fold the constant.
934 ConstantInt *CI =
935 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
936 Disp += CI->getSExtValue() * S;
937 // Iterate on the other operand.
938 Op = cast<AddOperator>(Op)->getOperand(0);
939 continue;
940 }
941 if (IndexReg == 0 &&
942 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
943 (S == 1 || S == 2 || S == 4 || S == 8)) {
944 // Scaled-index addressing.
945 Scale = S;
946 IndexReg = getRegForGEPIndex(Op).first;
947 if (IndexReg == 0)
948 return false;
949 break;
950 }
951 // Unsupported.
952 goto unsupported_gep;
953 }
954 }
955
956 // Check for displacement overflow.
957 if (!isInt<32>(Disp))
958 break;
959
960 AM.IndexReg = IndexReg;
961 AM.Scale = Scale;
962 AM.Disp = (uint32_t)Disp;
963 GEPs.push_back(V);
964
965 if (const GetElementPtrInst *GEP =
966 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
967 // Ok, the GEP indices were covered by constant-offset and scaled-index
968 // addressing. Update the address state and move on to examining the base.
969 V = GEP;
970 goto redo_gep;
971 } else if (X86SelectAddress(U->getOperand(0), AM)) {
972 return true;
973 }
974
975 // If we couldn't merge the gep value into this addr mode, revert back to
976 // our address and just match the value instead of completely failing.
977 AM = SavedAM;
978
David Majnemerd7708772016-06-24 04:05:21 +0000979 for (const Value *I : reverse(GEPs))
980 if (handleConstantAddresses(I, AM))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000981 return true;
982
983 return false;
984 unsupported_gep:
985 // Ok, the GEP indices weren't all covered.
986 break;
987 }
988 }
989
990 return handleConstantAddresses(V, AM);
991}
992
993/// X86SelectCallAddress - Attempt to fill in an address from the given value.
994///
995bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
996 const User *U = nullptr;
997 unsigned Opcode = Instruction::UserOp1;
998 const Instruction *I = dyn_cast<Instruction>(V);
999 // Record if the value is defined in the same basic block.
1000 //
1001 // This information is crucial to know whether or not folding an
1002 // operand is valid.
1003 // Indeed, FastISel generates or reuses a virtual register for all
1004 // operands of all instructions it selects. Obviously, the definition and
1005 // its uses must use the same virtual register otherwise the produced
1006 // code is incorrect.
1007 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
1008 // registers for values that are alive across basic blocks. This ensures
1009 // that the values are consistently set between across basic block, even
1010 // if different instruction selection mechanisms are used (e.g., a mix of
1011 // SDISel and FastISel).
1012 // For values local to a basic block, the instruction selection process
1013 // generates these virtual registers with whatever method is appropriate
1014 // for its needs. In particular, FastISel and SDISel do not share the way
1015 // local virtual registers are set.
1016 // Therefore, this is impossible (or at least unsafe) to share values
1017 // between basic blocks unless they use the same instruction selection
1018 // method, which is not guarantee for X86.
1019 // Moreover, things like hasOneUse could not be used accurately, if we
1020 // allow to reference values across basic blocks whereas they are not
1021 // alive across basic blocks initially.
1022 bool InMBB = true;
1023 if (I) {
1024 Opcode = I->getOpcode();
1025 U = I;
1026 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
1027 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
1028 Opcode = C->getOpcode();
1029 U = C;
1030 }
1031
1032 switch (Opcode) {
1033 default: break;
1034 case Instruction::BitCast:
1035 // Look past bitcasts if its operand is in the same BB.
1036 if (InMBB)
1037 return X86SelectCallAddress(U->getOperand(0), AM);
1038 break;
1039
1040 case Instruction::IntToPtr:
1041 // Look past no-op inttoptrs if its operand is in the same BB.
1042 if (InMBB &&
Mehdi Amini44ede332015-07-09 02:09:04 +00001043 TLI.getValueType(DL, U->getOperand(0)->getType()) ==
1044 TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001045 return X86SelectCallAddress(U->getOperand(0), AM);
1046 break;
1047
1048 case Instruction::PtrToInt:
1049 // Look past no-op ptrtoints if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +00001050 if (InMBB && TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001051 return X86SelectCallAddress(U->getOperand(0), AM);
1052 break;
1053 }
1054
1055 // Handle constant address.
1056 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1057 // Can't handle alternate code models yet.
1058 if (TM.getCodeModel() != CodeModel::Small)
1059 return false;
1060
1061 // RIP-relative addresses can't have additional register operands.
1062 if (Subtarget->isPICStyleRIPRel() &&
1063 (AM.Base.Reg != 0 || AM.IndexReg != 0))
1064 return false;
1065
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001066 // Can't handle TLS.
1067 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1068 if (GVar->isThreadLocal())
1069 return false;
1070
1071 // Okay, we've committed to selecting this global. Set up the basic address.
1072 AM.GV = GV;
1073
Reid Kleckner7662d502017-08-05 00:10:43 +00001074 // Return a direct reference to the global. Fastisel can handle calls to
1075 // functions that require loads, such as dllimport and nonlazybind
1076 // functions.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001077 if (Subtarget->isPICStyleRIPRel()) {
1078 // Use rip-relative addressing if we can. Above we verified that the
1079 // base and index registers are unused.
1080 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
1081 AM.Base.Reg = X86::RIP;
Rafael Espindolac7e98132016-05-20 12:20:10 +00001082 } else {
1083 AM.GVOpFlags = Subtarget->classifyLocalReference(nullptr);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001084 }
1085
1086 return true;
1087 }
1088
1089 // If all else fails, try to materialize the value in a register.
1090 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
1091 if (AM.Base.Reg == 0) {
1092 AM.Base.Reg = getRegForValue(V);
1093 return AM.Base.Reg != 0;
1094 }
1095 if (AM.IndexReg == 0) {
1096 assert(AM.Scale == 1 && "Scale with no index!");
1097 AM.IndexReg = getRegForValue(V);
1098 return AM.IndexReg != 0;
1099 }
1100 }
1101
1102 return false;
1103}
1104
1105
1106/// X86SelectStore - Select and emit code to implement store instructions.
1107bool X86FastISel::X86SelectStore(const Instruction *I) {
1108 // Atomic stores need special handling.
1109 const StoreInst *S = cast<StoreInst>(I);
1110
1111 if (S->isAtomic())
1112 return false;
1113
Manman Ren57518142016-04-11 21:08:06 +00001114 const Value *PtrV = I->getOperand(1);
1115 if (TLI.supportSwiftError()) {
1116 // Swifterror values can come from either a function parameter with
1117 // swifterror attribute or an alloca with swifterror attribute.
1118 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
1119 if (Arg->hasSwiftErrorAttr())
1120 return false;
1121 }
1122
1123 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
1124 if (Alloca->isSwiftError())
1125 return false;
1126 }
1127 }
1128
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001129 const Value *Val = S->getValueOperand();
1130 const Value *Ptr = S->getPointerOperand();
1131
1132 MVT VT;
1133 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
1134 return false;
1135
1136 unsigned Alignment = S->getAlignment();
1137 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
1138 if (Alignment == 0) // Ensure that codegen never sees alignment 0
1139 Alignment = ABIAlignment;
1140 bool Aligned = Alignment >= ABIAlignment;
1141
1142 X86AddressMode AM;
1143 if (!X86SelectAddress(Ptr, AM))
1144 return false;
1145
1146 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
1147}
1148
1149/// X86SelectRet - Select and emit code to implement ret instructions.
1150bool X86FastISel::X86SelectRet(const Instruction *I) {
1151 const ReturnInst *Ret = cast<ReturnInst>(I);
1152 const Function &F = *I->getParent()->getParent();
1153 const X86MachineFunctionInfo *X86MFInfo =
1154 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
1155
1156 if (!FuncInfo.CanLowerReturn)
1157 return false;
1158
Manman Ren57518142016-04-11 21:08:06 +00001159 if (TLI.supportSwiftError() &&
1160 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError))
1161 return false;
1162
Manman Rened967f32016-01-12 01:08:46 +00001163 if (TLI.supportSplitCSR(FuncInfo.MF))
1164 return false;
1165
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001166 CallingConv::ID CC = F.getCallingConv();
1167 if (CC != CallingConv::C &&
1168 CC != CallingConv::Fast &&
1169 CC != CallingConv::X86_FastCall &&
Nico Weberecdf45b2016-07-14 13:54:26 +00001170 CC != CallingConv::X86_StdCall &&
Nico Weberc7bf6462016-07-12 01:30:35 +00001171 CC != CallingConv::X86_ThisCall &&
Nico Weber8d66df12016-07-15 20:18:37 +00001172 CC != CallingConv::X86_64_SysV &&
Martin Storsjo2f24e932017-07-17 20:05:19 +00001173 CC != CallingConv::Win64)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001174 return false;
1175
Nico Weberc7bf6462016-07-12 01:30:35 +00001176 // Don't handle popping bytes if they don't fit the ret's immediate.
1177 if (!isUInt<16>(X86MFInfo->getBytesToPopOnReturn()))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001178 return false;
1179
1180 // fastcc with -tailcallopt is intended to provide a guaranteed
1181 // tail call optimization. Fastisel doesn't know how to do that.
1182 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
1183 return false;
1184
1185 // Let SDISel handle vararg functions.
1186 if (F.isVarArg())
1187 return false;
1188
1189 // Build a list of return value registers.
1190 SmallVector<unsigned, 4> RetRegs;
1191
1192 if (Ret->getNumOperands() > 0) {
1193 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini44ede332015-07-09 02:09:04 +00001194 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001195
1196 // Analyze operands of the call, assigning locations to each operand.
1197 SmallVector<CCValAssign, 16> ValLocs;
1198 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
1199 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
1200
1201 const Value *RV = Ret->getOperand(0);
1202 unsigned Reg = getRegForValue(RV);
1203 if (Reg == 0)
1204 return false;
1205
1206 // Only handle a single return value for now.
1207 if (ValLocs.size() != 1)
1208 return false;
1209
1210 CCValAssign &VA = ValLocs[0];
1211
1212 // Don't bother handling odd stuff for now.
1213 if (VA.getLocInfo() != CCValAssign::Full)
1214 return false;
1215 // Only handle register returns for now.
1216 if (!VA.isRegLoc())
1217 return false;
1218
1219 // The calling-convention tables for x87 returns don't tell
1220 // the whole story.
1221 if (VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1)
1222 return false;
1223
1224 unsigned SrcReg = Reg + VA.getValNo();
Mehdi Amini44ede332015-07-09 02:09:04 +00001225 EVT SrcVT = TLI.getValueType(DL, RV->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001226 EVT DstVT = VA.getValVT();
1227 // Special handling for extended integers.
1228 if (SrcVT != DstVT) {
1229 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
1230 return false;
1231
1232 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1233 return false;
1234
1235 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
1236
1237 if (SrcVT == MVT::i1) {
1238 if (Outs[0].Flags.isSExt())
1239 return false;
Craig Topper9d50e182017-03-14 04:18:25 +00001240 // In case SrcReg is a K register, COPY to a GPR
1241 if (MRI.getRegClass(SrcReg) == &X86::VK1RegClass) {
1242 unsigned KSrcReg = SrcReg;
Craig Topper058f2f62017-03-28 16:35:29 +00001243 SrcReg = createResultReg(&X86::GR32RegClass);
Craig Topper9d50e182017-03-14 04:18:25 +00001244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1245 TII.get(TargetOpcode::COPY), SrcReg)
1246 .addReg(KSrcReg);
Craig Topper058f2f62017-03-28 16:35:29 +00001247 SrcReg = fastEmitInst_extractsubreg(MVT::i8, SrcReg, /*Kill=*/true,
1248 X86::sub_8bit);
Craig Topper9d50e182017-03-14 04:18:25 +00001249 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001250 SrcReg = fastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
1251 SrcVT = MVT::i8;
1252 }
1253 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
1254 ISD::SIGN_EXTEND;
1255 SrcReg = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
1256 SrcReg, /*TODO: Kill=*/false);
1257 }
1258
1259 // Make the copy.
1260 unsigned DstReg = VA.getLocReg();
1261 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
1262 // Avoid a cross-class copy. This is very unlikely.
1263 if (!SrcRC->contains(DstReg))
1264 return false;
1265 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1266 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
1267
1268 // Add register to return instruction.
1269 RetRegs.push_back(VA.getLocReg());
1270 }
1271
Manman Ren1c3f65a2016-04-26 18:08:06 +00001272 // Swift calling convention does not require we copy the sret argument
1273 // into %rax/%eax for the return, and SRetReturnReg is not set for Swift.
1274
Dimitry Andric227b9282016-01-03 17:22:03 +00001275 // All x86 ABIs require that for returning structs by value we copy
1276 // the sret argument into %rax/%eax (depending on ABI) for the return.
1277 // We saved the argument into a virtual register in the entry block,
Michael Kuperstein2ea81ba2015-12-28 14:39:21 +00001278 // so now we copy the value out and into %rax/%eax.
Manman Ren1c3f65a2016-04-26 18:08:06 +00001279 if (F.hasStructRetAttr() && CC != CallingConv::Swift) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001280 unsigned Reg = X86MFInfo->getSRetReturnReg();
1281 assert(Reg &&
1282 "SRetReturnReg should have been set in LowerFormalArguments()!");
1283 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
1284 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1285 TII.get(TargetOpcode::COPY), RetReg).addReg(Reg);
1286 RetRegs.push_back(RetReg);
1287 }
1288
1289 // Now emit the RET.
Nico Weberc7bf6462016-07-12 01:30:35 +00001290 MachineInstrBuilder MIB;
1291 if (X86MFInfo->getBytesToPopOnReturn()) {
1292 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1293 TII.get(Subtarget->is64Bit() ? X86::RETIQ : X86::RETIL))
1294 .addImm(X86MFInfo->getBytesToPopOnReturn());
1295 } else {
1296 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1297 TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
1298 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001299 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1300 MIB.addReg(RetRegs[i], RegState::Implicit);
1301 return true;
1302}
1303
1304/// X86SelectLoad - Select and emit code to implement load instructions.
1305///
1306bool X86FastISel::X86SelectLoad(const Instruction *I) {
1307 const LoadInst *LI = cast<LoadInst>(I);
1308
1309 // Atomic loads need special handling.
1310 if (LI->isAtomic())
1311 return false;
1312
Manman Ren57518142016-04-11 21:08:06 +00001313 const Value *SV = I->getOperand(0);
1314 if (TLI.supportSwiftError()) {
1315 // Swifterror values can come from either a function parameter with
1316 // swifterror attribute or an alloca with swifterror attribute.
1317 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
1318 if (Arg->hasSwiftErrorAttr())
1319 return false;
1320 }
1321
1322 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
1323 if (Alloca->isSwiftError())
1324 return false;
1325 }
1326 }
1327
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001328 MVT VT;
1329 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
1330 return false;
1331
1332 const Value *Ptr = LI->getPointerOperand();
1333
1334 X86AddressMode AM;
1335 if (!X86SelectAddress(Ptr, AM))
1336 return false;
1337
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +00001338 unsigned Alignment = LI->getAlignment();
1339 unsigned ABIAlignment = DL.getABITypeAlignment(LI->getType());
1340 if (Alignment == 0) // Ensure that codegen never sees alignment 0
1341 Alignment = ABIAlignment;
1342
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001343 unsigned ResultReg = 0;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +00001344 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg,
1345 Alignment))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001346 return false;
1347
1348 updateValueMap(I, ResultReg);
1349 return true;
1350}
1351
1352static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
1353 bool HasAVX = Subtarget->hasAVX();
1354 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
1355 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
1356
1357 switch (VT.getSimpleVT().SimpleTy) {
1358 default: return 0;
1359 case MVT::i8: return X86::CMP8rr;
1360 case MVT::i16: return X86::CMP16rr;
1361 case MVT::i32: return X86::CMP32rr;
1362 case MVT::i64: return X86::CMP64rr;
1363 case MVT::f32:
1364 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
1365 case MVT::f64:
1366 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
1367 }
1368}
1369
Rafael Espindola19141f22015-03-16 14:05:49 +00001370/// If we have a comparison with RHS as the RHS of the comparison, return an
1371/// opcode that works for the compare (e.g. CMP32ri) otherwise return 0.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001372static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Rafael Espindola933f51a2015-03-16 14:25:08 +00001373 int64_t Val = RHSC->getSExtValue();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001374 switch (VT.getSimpleVT().SimpleTy) {
1375 // Otherwise, we can't fold the immediate into this comparison.
Rafael Espindola19141f22015-03-16 14:05:49 +00001376 default:
1377 return 0;
1378 case MVT::i8:
1379 return X86::CMP8ri;
1380 case MVT::i16:
Rafael Espindola933f51a2015-03-16 14:25:08 +00001381 if (isInt<8>(Val))
1382 return X86::CMP16ri8;
Rafael Espindola19141f22015-03-16 14:05:49 +00001383 return X86::CMP16ri;
1384 case MVT::i32:
Rafael Espindola933f51a2015-03-16 14:25:08 +00001385 if (isInt<8>(Val))
1386 return X86::CMP32ri8;
Rafael Espindola19141f22015-03-16 14:05:49 +00001387 return X86::CMP32ri;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001388 case MVT::i64:
Rafael Espindola933f51a2015-03-16 14:25:08 +00001389 if (isInt<8>(Val))
1390 return X86::CMP64ri8;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001391 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1392 // field.
Rafael Espindola933f51a2015-03-16 14:25:08 +00001393 if (isInt<32>(Val))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001394 return X86::CMP64ri32;
1395 return 0;
1396 }
1397}
1398
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001399bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1, EVT VT,
1400 const DebugLoc &CurDbgLoc) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001401 unsigned Op0Reg = getRegForValue(Op0);
1402 if (Op0Reg == 0) return false;
1403
1404 // Handle 'null' like i32/i64 0.
1405 if (isa<ConstantPointerNull>(Op1))
1406 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
1407
1408 // We have two options: compare with register or immediate. If the RHS of
1409 // the compare is an immediate that we can fold into this compare, use
1410 // CMPri, otherwise use CMPrr.
1411 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1412 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
1413 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, CurDbgLoc, TII.get(CompareImmOpc))
1414 .addReg(Op0Reg)
1415 .addImm(Op1C->getSExtValue());
1416 return true;
1417 }
1418 }
1419
1420 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
1421 if (CompareOpc == 0) return false;
1422
1423 unsigned Op1Reg = getRegForValue(Op1);
1424 if (Op1Reg == 0) return false;
1425 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, CurDbgLoc, TII.get(CompareOpc))
1426 .addReg(Op0Reg)
1427 .addReg(Op1Reg);
1428
1429 return true;
1430}
1431
1432bool X86FastISel::X86SelectCmp(const Instruction *I) {
1433 const CmpInst *CI = cast<CmpInst>(I);
1434
1435 MVT VT;
1436 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
1437 return false;
1438
Elena Demikhovskyad0a56f2016-07-06 14:15:43 +00001439 if (I->getType()->isIntegerTy(1) && Subtarget->hasAVX512())
1440 return false;
1441
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001442 // Try to optimize or fold the cmp.
1443 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1444 unsigned ResultReg = 0;
1445 switch (Predicate) {
1446 default: break;
1447 case CmpInst::FCMP_FALSE: {
1448 ResultReg = createResultReg(&X86::GR32RegClass);
1449 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0),
1450 ResultReg);
1451 ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1452 X86::sub_8bit);
1453 if (!ResultReg)
1454 return false;
1455 break;
1456 }
1457 case CmpInst::FCMP_TRUE: {
1458 ResultReg = createResultReg(&X86::GR8RegClass);
1459 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
1460 ResultReg).addImm(1);
1461 break;
1462 }
1463 }
1464
1465 if (ResultReg) {
1466 updateValueMap(I, ResultReg);
1467 return true;
1468 }
1469
1470 const Value *LHS = CI->getOperand(0);
1471 const Value *RHS = CI->getOperand(1);
1472
1473 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1474 // We don't have to materialize a zero constant for this case and can just use
1475 // %x again on the RHS.
1476 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1477 const auto *RHSC = dyn_cast<ConstantFP>(RHS);
1478 if (RHSC && RHSC->isNullValue())
1479 RHS = LHS;
1480 }
1481
1482 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Craig Topper428169a2016-09-05 07:14:21 +00001483 static const uint16_t SETFOpcTable[2][3] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001484 { X86::SETEr, X86::SETNPr, X86::AND8rr },
1485 { X86::SETNEr, X86::SETPr, X86::OR8rr }
1486 };
Craig Topper428169a2016-09-05 07:14:21 +00001487 const uint16_t *SETFOpc = nullptr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001488 switch (Predicate) {
1489 default: break;
1490 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1491 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1492 }
1493
1494 ResultReg = createResultReg(&X86::GR8RegClass);
1495 if (SETFOpc) {
1496 if (!X86FastEmitCompare(LHS, RHS, VT, I->getDebugLoc()))
1497 return false;
1498
1499 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1500 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1501 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1502 FlagReg1);
1503 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1504 FlagReg2);
1505 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
1506 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
1507 updateValueMap(I, ResultReg);
1508 return true;
1509 }
1510
1511 X86::CondCode CC;
1512 bool SwapArgs;
Igor Bregerdb754552017-05-11 06:36:37 +00001513 std::tie(CC, SwapArgs) = X86::getX86ConditionCode(Predicate);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001514 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
1515 unsigned Opc = X86::getSETFromCond(CC);
1516
1517 if (SwapArgs)
1518 std::swap(LHS, RHS);
1519
1520 // Emit a compare of LHS/RHS.
1521 if (!X86FastEmitCompare(LHS, RHS, VT, I->getDebugLoc()))
1522 return false;
1523
1524 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
1525 updateValueMap(I, ResultReg);
1526 return true;
1527}
1528
1529bool X86FastISel::X86SelectZExt(const Instruction *I) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001530 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001531 if (!TLI.isTypeLegal(DstVT))
1532 return false;
1533
1534 unsigned ResultReg = getRegForValue(I->getOperand(0));
1535 if (ResultReg == 0)
1536 return false;
1537
1538 // Handle zero-extension from i1 to i8, which is common.
Mehdi Amini44ede332015-07-09 02:09:04 +00001539 MVT SrcVT = TLI.getSimpleValueType(DL, I->getOperand(0)->getType());
Craig Topper088ba172016-12-05 06:09:55 +00001540 if (SrcVT == MVT::i1) {
Craig Topper9d50e182017-03-14 04:18:25 +00001541 // In case ResultReg is a K register, COPY to a GPR
1542 if (MRI.getRegClass(ResultReg) == &X86::VK1RegClass) {
1543 unsigned KResultReg = ResultReg;
Craig Topper058f2f62017-03-28 16:35:29 +00001544 ResultReg = createResultReg(&X86::GR32RegClass);
Craig Topper58647b12017-03-12 03:37:37 +00001545 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1546 TII.get(TargetOpcode::COPY), ResultReg)
Craig Topper9d50e182017-03-14 04:18:25 +00001547 .addReg(KResultReg);
Craig Topper058f2f62017-03-28 16:35:29 +00001548 ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1549 X86::sub_8bit);
Craig Topper58647b12017-03-12 03:37:37 +00001550 }
1551
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001552 // Set the high bits to zero.
1553 ResultReg = fastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1554 SrcVT = MVT::i8;
1555
1556 if (ResultReg == 0)
1557 return false;
1558 }
1559
1560 if (DstVT == MVT::i64) {
1561 // Handle extension to 64-bits via sub-register shenanigans.
1562 unsigned MovInst;
1563
1564 switch (SrcVT.SimpleTy) {
1565 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1566 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1567 case MVT::i32: MovInst = X86::MOV32rr; break;
1568 default: llvm_unreachable("Unexpected zext to i64 source type");
1569 }
1570
1571 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1572 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
1573 .addReg(ResultReg);
1574
1575 ResultReg = createResultReg(&X86::GR64RegClass);
1576 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
1577 ResultReg)
1578 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1579 } else if (DstVT != MVT::i8) {
1580 ResultReg = fastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1581 ResultReg, /*Kill=*/true);
1582 if (ResultReg == 0)
1583 return false;
1584 }
1585
1586 updateValueMap(I, ResultReg);
1587 return true;
1588}
1589
1590bool X86FastISel::X86SelectBranch(const Instruction *I) {
1591 // Unconditional branches are selected by tablegen-generated code.
1592 // Handle a conditional branch.
1593 const BranchInst *BI = cast<BranchInst>(I);
1594 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1595 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1596
1597 // Fold the common case of a conditional branch with a comparison
1598 // in the same block (values defined on other blocks may not have
1599 // initialized registers).
1600 X86::CondCode CC;
1601 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1602 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001603 EVT VT = TLI.getValueType(DL, CI->getOperand(0)->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001604
1605 // Try to optimize or fold the cmp.
1606 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1607 switch (Predicate) {
1608 default: break;
1609 case CmpInst::FCMP_FALSE: fastEmitBranch(FalseMBB, DbgLoc); return true;
1610 case CmpInst::FCMP_TRUE: fastEmitBranch(TrueMBB, DbgLoc); return true;
1611 }
1612
1613 const Value *CmpLHS = CI->getOperand(0);
1614 const Value *CmpRHS = CI->getOperand(1);
1615
1616 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x,
1617 // 0.0.
1618 // We don't have to materialize a zero constant for this case and can just
1619 // use %x again on the RHS.
1620 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1621 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1622 if (CmpRHSC && CmpRHSC->isNullValue())
1623 CmpRHS = CmpLHS;
1624 }
1625
1626 // Try to take advantage of fallthrough opportunities.
1627 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1628 std::swap(TrueMBB, FalseMBB);
1629 Predicate = CmpInst::getInversePredicate(Predicate);
1630 }
1631
1632 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/condition
1633 // code check. Instead two branch instructions are required to check all
1634 // the flags. First we change the predicate to a supported condition code,
1635 // which will be the first branch. Later one we will emit the second
1636 // branch.
1637 bool NeedExtraBranch = false;
1638 switch (Predicate) {
1639 default: break;
1640 case CmpInst::FCMP_OEQ:
Justin Bognerb03fd122016-08-17 05:10:15 +00001641 std::swap(TrueMBB, FalseMBB);
1642 LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001643 case CmpInst::FCMP_UNE:
1644 NeedExtraBranch = true;
1645 Predicate = CmpInst::FCMP_ONE;
1646 break;
1647 }
1648
1649 bool SwapArgs;
1650 unsigned BranchOpc;
Igor Bregerdb754552017-05-11 06:36:37 +00001651 std::tie(CC, SwapArgs) = X86::getX86ConditionCode(Predicate);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001652 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
1653
1654 BranchOpc = X86::GetCondBranchFromCond(CC);
1655 if (SwapArgs)
1656 std::swap(CmpLHS, CmpRHS);
1657
1658 // Emit a compare of the LHS and RHS, setting the flags.
1659 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT, CI->getDebugLoc()))
1660 return false;
1661
1662 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
1663 .addMBB(TrueMBB);
1664
1665 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1666 // to UNE above).
1667 if (NeedExtraBranch) {
1668 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_1))
1669 .addMBB(TrueMBB);
1670 }
1671
Matthias Braun17af6072015-08-26 01:38:00 +00001672 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001673 return true;
1674 }
1675 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1676 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1677 // typically happen for _Bool and C++ bools.
1678 MVT SourceVT;
1679 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1680 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1681 unsigned TestOpc = 0;
1682 switch (SourceVT.SimpleTy) {
1683 default: break;
1684 case MVT::i8: TestOpc = X86::TEST8ri; break;
1685 case MVT::i16: TestOpc = X86::TEST16ri; break;
1686 case MVT::i32: TestOpc = X86::TEST32ri; break;
1687 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1688 }
1689 if (TestOpc) {
1690 unsigned OpReg = getRegForValue(TI->getOperand(0));
1691 if (OpReg == 0) return false;
Guy Blank9ae797a2016-08-21 08:02:27 +00001692
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001693 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
1694 .addReg(OpReg).addImm(1);
1695
1696 unsigned JmpOpc = X86::JNE_1;
1697 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1698 std::swap(TrueMBB, FalseMBB);
1699 JmpOpc = X86::JE_1;
1700 }
1701
1702 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
1703 .addMBB(TrueMBB);
Matthias Braun17af6072015-08-26 01:38:00 +00001704
1705 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001706 return true;
1707 }
1708 }
1709 } else if (foldX86XALUIntrinsic(CC, BI, BI->getCondition())) {
1710 // Fake request the condition, otherwise the intrinsic might be completely
1711 // optimized away.
1712 unsigned TmpReg = getRegForValue(BI->getCondition());
1713 if (TmpReg == 0)
1714 return false;
1715
1716 unsigned BranchOpc = X86::GetCondBranchFromCond(CC);
1717
1718 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
1719 .addMBB(TrueMBB);
Matthias Braun17af6072015-08-26 01:38:00 +00001720 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001721 return true;
1722 }
1723
1724 // Otherwise do a clumsy setcc and re-test it.
1725 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1726 // in an explicit cast, so make sure to handle that correctly.
1727 unsigned OpReg = getRegForValue(BI->getCondition());
1728 if (OpReg == 0) return false;
1729
Guy Blank2bdc74a2016-09-28 11:22:17 +00001730 // In case OpReg is a K register, COPY to a GPR
1731 if (MRI.getRegClass(OpReg) == &X86::VK1RegClass) {
1732 unsigned KOpReg = OpReg;
Craig Topper058f2f62017-03-28 16:35:29 +00001733 OpReg = createResultReg(&X86::GR32RegClass);
Guy Blank2bdc74a2016-09-28 11:22:17 +00001734 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1735 TII.get(TargetOpcode::COPY), OpReg)
1736 .addReg(KOpReg);
Craig Topper058f2f62017-03-28 16:35:29 +00001737 OpReg = fastEmitInst_extractsubreg(MVT::i8, OpReg, /*Kill=*/true,
1738 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00001739 }
1740 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1741 .addReg(OpReg)
1742 .addImm(1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001743 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_1))
1744 .addMBB(TrueMBB);
Matthias Braun17af6072015-08-26 01:38:00 +00001745 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001746 return true;
1747}
1748
1749bool X86FastISel::X86SelectShift(const Instruction *I) {
1750 unsigned CReg = 0, OpReg = 0;
1751 const TargetRegisterClass *RC = nullptr;
1752 if (I->getType()->isIntegerTy(8)) {
1753 CReg = X86::CL;
1754 RC = &X86::GR8RegClass;
1755 switch (I->getOpcode()) {
1756 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1757 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1758 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
1759 default: return false;
1760 }
1761 } else if (I->getType()->isIntegerTy(16)) {
1762 CReg = X86::CX;
1763 RC = &X86::GR16RegClass;
1764 switch (I->getOpcode()) {
1765 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1766 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1767 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
1768 default: return false;
1769 }
1770 } else if (I->getType()->isIntegerTy(32)) {
1771 CReg = X86::ECX;
1772 RC = &X86::GR32RegClass;
1773 switch (I->getOpcode()) {
1774 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1775 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1776 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
1777 default: return false;
1778 }
1779 } else if (I->getType()->isIntegerTy(64)) {
1780 CReg = X86::RCX;
1781 RC = &X86::GR64RegClass;
1782 switch (I->getOpcode()) {
1783 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1784 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1785 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
1786 default: return false;
1787 }
1788 } else {
1789 return false;
1790 }
1791
1792 MVT VT;
1793 if (!isTypeLegal(I->getType(), VT))
1794 return false;
1795
1796 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1797 if (Op0Reg == 0) return false;
1798
1799 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1800 if (Op1Reg == 0) return false;
1801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
1802 CReg).addReg(Op1Reg);
1803
1804 // The shift instruction uses X86::CL. If we defined a super-register
1805 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
1806 if (CReg != X86::CL)
1807 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1808 TII.get(TargetOpcode::KILL), X86::CL)
1809 .addReg(CReg, RegState::Kill);
1810
1811 unsigned ResultReg = createResultReg(RC);
1812 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
1813 .addReg(Op0Reg);
1814 updateValueMap(I, ResultReg);
1815 return true;
1816}
1817
1818bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1819 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1820 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1821 const static bool S = true; // IsSigned
1822 const static bool U = false; // !IsSigned
1823 const static unsigned Copy = TargetOpcode::COPY;
1824 // For the X86 DIV/IDIV instruction, in most cases the dividend
1825 // (numerator) must be in a specific register pair highreg:lowreg,
1826 // producing the quotient in lowreg and the remainder in highreg.
1827 // For most data types, to set up the instruction, the dividend is
1828 // copied into lowreg, and lowreg is sign-extended or zero-extended
1829 // into highreg. The exception is i8, where the dividend is defined
1830 // as a single register rather than a register pair, and we
1831 // therefore directly sign-extend or zero-extend the dividend into
1832 // lowreg, instead of copying, and ignore the highreg.
1833 const static struct DivRemEntry {
1834 // The following portion depends only on the data type.
1835 const TargetRegisterClass *RC;
1836 unsigned LowInReg; // low part of the register pair
1837 unsigned HighInReg; // high part of the register pair
1838 // The following portion depends on both the data type and the operation.
1839 struct DivRemResult {
1840 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1841 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1842 // highreg, or copying a zero into highreg.
1843 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1844 // zero/sign-extending into lowreg for i8.
1845 unsigned DivRemResultReg; // Register containing the desired result.
1846 bool IsOpSigned; // Whether to use signed or unsigned form.
1847 } ResultTable[NumOps];
1848 } OpTable[NumTypes] = {
1849 { &X86::GR8RegClass, X86::AX, 0, {
1850 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1851 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1852 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1853 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1854 }
1855 }, // i8
1856 { &X86::GR16RegClass, X86::AX, X86::DX, {
1857 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1858 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
1859 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1860 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
1861 }
1862 }, // i16
1863 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1864 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1865 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1866 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1867 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1868 }
1869 }, // i32
1870 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1871 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1872 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
1873 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1874 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
1875 }
1876 }, // i64
1877 };
1878
1879 MVT VT;
1880 if (!isTypeLegal(I->getType(), VT))
1881 return false;
1882
1883 unsigned TypeIndex, OpIndex;
1884 switch (VT.SimpleTy) {
1885 default: return false;
1886 case MVT::i8: TypeIndex = 0; break;
1887 case MVT::i16: TypeIndex = 1; break;
1888 case MVT::i32: TypeIndex = 2; break;
1889 case MVT::i64: TypeIndex = 3;
1890 if (!Subtarget->is64Bit())
1891 return false;
1892 break;
1893 }
1894
1895 switch (I->getOpcode()) {
1896 default: llvm_unreachable("Unexpected div/rem opcode");
1897 case Instruction::SDiv: OpIndex = 0; break;
1898 case Instruction::SRem: OpIndex = 1; break;
1899 case Instruction::UDiv: OpIndex = 2; break;
1900 case Instruction::URem: OpIndex = 3; break;
1901 }
1902
1903 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1904 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1905 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1906 if (Op0Reg == 0)
1907 return false;
1908 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1909 if (Op1Reg == 0)
1910 return false;
1911
1912 // Move op0 into low-order input register.
1913 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1914 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1915 // Zero-extend or sign-extend into high-order input register.
1916 if (OpEntry.OpSignExtend) {
1917 if (OpEntry.IsOpSigned)
1918 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1919 TII.get(OpEntry.OpSignExtend));
1920 else {
1921 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
1922 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1923 TII.get(X86::MOV32r0), Zero32);
1924
1925 // Copy the zero into the appropriate sub/super/identical physical
1926 // register. Unfortunately the operations needed are not uniform enough
1927 // to fit neatly into the table above.
Craig Topper088ba172016-12-05 06:09:55 +00001928 if (VT == MVT::i16) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001929 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1930 TII.get(Copy), TypeEntry.HighInReg)
1931 .addReg(Zero32, 0, X86::sub_16bit);
Craig Topper088ba172016-12-05 06:09:55 +00001932 } else if (VT == MVT::i32) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001933 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1934 TII.get(Copy), TypeEntry.HighInReg)
1935 .addReg(Zero32);
Craig Topper088ba172016-12-05 06:09:55 +00001936 } else if (VT == MVT::i64) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001937 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1938 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1939 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1940 }
1941 }
1942 }
1943 // Generate the DIV/IDIV instruction.
1944 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1945 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
1946 // For i8 remainder, we can't reference AH directly, as we'll end
1947 // up with bogus copies like %R9B = COPY %AH. Reference AX
1948 // instead to prevent AH references in a REX instruction.
1949 //
1950 // The current assumption of the fast register allocator is that isel
1951 // won't generate explicit references to the GPR8_NOREX registers. If
1952 // the allocator and/or the backend get enhanced to be more robust in
1953 // that regard, this can be, and should be, removed.
1954 unsigned ResultReg = 0;
1955 if ((I->getOpcode() == Instruction::SRem ||
1956 I->getOpcode() == Instruction::URem) &&
1957 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1958 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1959 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1960 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1961 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1962
1963 // Shift AX right by 8 bits instead of using AH.
1964 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
1965 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1966
1967 // Now reference the 8-bit subreg of the result.
1968 ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1969 /*Kill=*/true, X86::sub_8bit);
1970 }
1971 // Copy the result out of the physreg if we haven't already.
1972 if (!ResultReg) {
1973 ResultReg = createResultReg(TypeEntry.RC);
1974 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
1975 .addReg(OpEntry.DivRemResultReg);
1976 }
1977 updateValueMap(I, ResultReg);
1978
1979 return true;
1980}
1981
1982/// \brief Emit a conditional move instruction (if the are supported) to lower
1983/// the select.
1984bool X86FastISel::X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I) {
1985 // Check if the subtarget supports these instructions.
1986 if (!Subtarget->hasCMov())
1987 return false;
1988
1989 // FIXME: Add support for i8.
1990 if (RetVT < MVT::i16 || RetVT > MVT::i64)
1991 return false;
1992
1993 const Value *Cond = I->getOperand(0);
1994 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1995 bool NeedTest = true;
1996 X86::CondCode CC = X86::COND_NE;
1997
1998 // Optimize conditions coming from a compare if both instructions are in the
1999 // same basic block (values defined in other basic blocks may not have
2000 // initialized registers).
2001 const auto *CI = dyn_cast<CmpInst>(Cond);
2002 if (CI && (CI->getParent() == I->getParent())) {
2003 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2004
2005 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Craig Topper428169a2016-09-05 07:14:21 +00002006 static const uint16_t SETFOpcTable[2][3] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002007 { X86::SETNPr, X86::SETEr , X86::TEST8rr },
2008 { X86::SETPr, X86::SETNEr, X86::OR8rr }
2009 };
Craig Topper428169a2016-09-05 07:14:21 +00002010 const uint16_t *SETFOpc = nullptr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002011 switch (Predicate) {
2012 default: break;
2013 case CmpInst::FCMP_OEQ:
2014 SETFOpc = &SETFOpcTable[0][0];
2015 Predicate = CmpInst::ICMP_NE;
2016 break;
2017 case CmpInst::FCMP_UNE:
2018 SETFOpc = &SETFOpcTable[1][0];
2019 Predicate = CmpInst::ICMP_NE;
2020 break;
2021 }
2022
2023 bool NeedSwap;
Igor Bregerdb754552017-05-11 06:36:37 +00002024 std::tie(CC, NeedSwap) = X86::getX86ConditionCode(Predicate);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002025 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
2026
2027 const Value *CmpLHS = CI->getOperand(0);
2028 const Value *CmpRHS = CI->getOperand(1);
2029 if (NeedSwap)
2030 std::swap(CmpLHS, CmpRHS);
2031
Mehdi Amini44ede332015-07-09 02:09:04 +00002032 EVT CmpVT = TLI.getValueType(DL, CmpLHS->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002033 // Emit a compare of the LHS and RHS, setting the flags.
2034 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT, CI->getDebugLoc()))
2035 return false;
2036
2037 if (SETFOpc) {
2038 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
2039 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
2040 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
2041 FlagReg1);
2042 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
2043 FlagReg2);
2044 auto const &II = TII.get(SETFOpc[2]);
2045 if (II.getNumDefs()) {
2046 unsigned TmpReg = createResultReg(&X86::GR8RegClass);
2047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, TmpReg)
2048 .addReg(FlagReg2).addReg(FlagReg1);
2049 } else {
2050 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
2051 .addReg(FlagReg2).addReg(FlagReg1);
2052 }
2053 }
2054 NeedTest = false;
2055 } else if (foldX86XALUIntrinsic(CC, I, Cond)) {
2056 // Fake request the condition, otherwise the intrinsic might be completely
2057 // optimized away.
2058 unsigned TmpReg = getRegForValue(Cond);
2059 if (TmpReg == 0)
2060 return false;
2061
2062 NeedTest = false;
2063 }
2064
2065 if (NeedTest) {
2066 // Selects operate on i1, however, CondReg is 8 bits width and may contain
2067 // garbage. Indeed, only the less significant bit is supposed to be
2068 // accurate. If we read more than the lsb, we may see non-zero values
2069 // whereas lsb is zero. Therefore, we have to truncate Op0Reg to i1 for
2070 // the select. This is achieved by performing TEST against 1.
2071 unsigned CondReg = getRegForValue(Cond);
2072 if (CondReg == 0)
2073 return false;
2074 bool CondIsKill = hasTrivialKill(Cond);
2075
Guy Blank2bdc74a2016-09-28 11:22:17 +00002076 // In case OpReg is a K register, COPY to a GPR
2077 if (MRI.getRegClass(CondReg) == &X86::VK1RegClass) {
2078 unsigned KCondReg = CondReg;
Craig Topper058f2f62017-03-28 16:35:29 +00002079 CondReg = createResultReg(&X86::GR32RegClass);
Guy Blank9ae797a2016-08-21 08:02:27 +00002080 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Guy Blank2bdc74a2016-09-28 11:22:17 +00002081 TII.get(TargetOpcode::COPY), CondReg)
2082 .addReg(KCondReg, getKillRegState(CondIsKill));
Craig Topper058f2f62017-03-28 16:35:29 +00002083 CondReg = fastEmitInst_extractsubreg(MVT::i8, CondReg, /*Kill=*/true,
2084 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00002085 }
2086 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
2087 .addReg(CondReg, getKillRegState(CondIsKill))
2088 .addImm(1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002089 }
2090
2091 const Value *LHS = I->getOperand(1);
2092 const Value *RHS = I->getOperand(2);
2093
2094 unsigned RHSReg = getRegForValue(RHS);
2095 bool RHSIsKill = hasTrivialKill(RHS);
2096
2097 unsigned LHSReg = getRegForValue(LHS);
2098 bool LHSIsKill = hasTrivialKill(LHS);
2099
2100 if (!LHSReg || !RHSReg)
2101 return false;
2102
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00002103 const TargetRegisterInfo &TRI = *Subtarget->getRegisterInfo();
2104 unsigned Opc = X86::getCMovFromCond(CC, TRI.getRegSizeInBits(*RC)/8);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002105 unsigned ResultReg = fastEmitInst_rr(Opc, RC, RHSReg, RHSIsKill,
2106 LHSReg, LHSIsKill);
2107 updateValueMap(I, ResultReg);
2108 return true;
2109}
2110
Sanjay Patel302404b2015-03-05 21:46:54 +00002111/// \brief Emit SSE or AVX instructions to lower the select.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002112///
2113/// Try to use SSE1/SSE2 instructions to simulate a select without branches.
2114/// This lowers fp selects into a CMP/AND/ANDN/OR sequence when the necessary
Sanjay Patel302404b2015-03-05 21:46:54 +00002115/// SSE instructions are available. If AVX is available, try to use a VBLENDV.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002116bool X86FastISel::X86FastEmitSSESelect(MVT RetVT, const Instruction *I) {
2117 // Optimize conditions coming from a compare if both instructions are in the
2118 // same basic block (values defined in other basic blocks may not have
2119 // initialized registers).
2120 const auto *CI = dyn_cast<FCmpInst>(I->getOperand(0));
2121 if (!CI || (CI->getParent() != I->getParent()))
2122 return false;
2123
2124 if (I->getType() != CI->getOperand(0)->getType() ||
2125 !((Subtarget->hasSSE1() && RetVT == MVT::f32) ||
2126 (Subtarget->hasSSE2() && RetVT == MVT::f64)))
2127 return false;
2128
2129 const Value *CmpLHS = CI->getOperand(0);
2130 const Value *CmpRHS = CI->getOperand(1);
2131 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2132
2133 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
2134 // We don't have to materialize a zero constant for this case and can just use
2135 // %x again on the RHS.
2136 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
2137 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
2138 if (CmpRHSC && CmpRHSC->isNullValue())
2139 CmpRHS = CmpLHS;
2140 }
2141
2142 unsigned CC;
2143 bool NeedSwap;
2144 std::tie(CC, NeedSwap) = getX86SSEConditionCode(Predicate);
2145 if (CC > 7)
2146 return false;
2147
2148 if (NeedSwap)
2149 std::swap(CmpLHS, CmpRHS);
2150
Sanjay Patel302404b2015-03-05 21:46:54 +00002151 // Choose the SSE instruction sequence based on data type (float or double).
Craig Topper428169a2016-09-05 07:14:21 +00002152 static const uint16_t OpcTable[2][4] = {
Craig Topper6413f8a2016-12-06 04:58:39 +00002153 { X86::CMPSSrr, X86::ANDPSrr, X86::ANDNPSrr, X86::ORPSrr },
2154 { X86::CMPSDrr, X86::ANDPDrr, X86::ANDNPDrr, X86::ORPDrr }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002155 };
2156
Craig Topper428169a2016-09-05 07:14:21 +00002157 const uint16_t *Opc = nullptr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002158 switch (RetVT.SimpleTy) {
2159 default: return false;
Sanjay Patel302404b2015-03-05 21:46:54 +00002160 case MVT::f32: Opc = &OpcTable[0][0]; break;
2161 case MVT::f64: Opc = &OpcTable[1][0]; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002162 }
2163
2164 const Value *LHS = I->getOperand(1);
2165 const Value *RHS = I->getOperand(2);
2166
2167 unsigned LHSReg = getRegForValue(LHS);
2168 bool LHSIsKill = hasTrivialKill(LHS);
2169
2170 unsigned RHSReg = getRegForValue(RHS);
2171 bool RHSIsKill = hasTrivialKill(RHS);
2172
2173 unsigned CmpLHSReg = getRegForValue(CmpLHS);
2174 bool CmpLHSIsKill = hasTrivialKill(CmpLHS);
2175
2176 unsigned CmpRHSReg = getRegForValue(CmpRHS);
2177 bool CmpRHSIsKill = hasTrivialKill(CmpRHS);
2178
2179 if (!LHSReg || !RHSReg || !CmpLHS || !CmpRHS)
2180 return false;
2181
2182 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
Sanjay Patel302404b2015-03-05 21:46:54 +00002183 unsigned ResultReg;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002184
2185 if (Subtarget->hasAVX512()) {
2186 // If we have AVX512 we can use a mask compare and masked movss/sd.
2187 const TargetRegisterClass *VR128X = &X86::VR128XRegClass;
2188 const TargetRegisterClass *VK1 = &X86::VK1RegClass;
2189
2190 unsigned CmpOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002191 (RetVT == MVT::f32) ? X86::VCMPSSZrr : X86::VCMPSDZrr;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002192 unsigned CmpReg = fastEmitInst_rri(CmpOpcode, VK1, CmpLHSReg, CmpLHSIsKill,
2193 CmpRHSReg, CmpRHSIsKill, CC);
2194
2195 // Need an IMPLICIT_DEF for the input that is used to generate the upper
2196 // bits of the result register since its not based on any of the inputs.
2197 unsigned ImplicitDefReg = createResultReg(VR128X);
2198 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2199 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2200
2201 // Place RHSReg is the passthru of the masked movss/sd operation and put
2202 // LHS in the input. The mask input comes from the compare.
2203 unsigned MovOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002204 (RetVT == MVT::f32) ? X86::VMOVSSZrrk : X86::VMOVSDZrrk;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002205 unsigned MovReg = fastEmitInst_rrrr(MovOpcode, VR128X, RHSReg, RHSIsKill,
2206 CmpReg, true, ImplicitDefReg, true,
2207 LHSReg, LHSIsKill);
2208
2209 ResultReg = createResultReg(RC);
2210 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2211 TII.get(TargetOpcode::COPY), ResultReg).addReg(MovReg);
2212
2213 } else if (Subtarget->hasAVX()) {
Matthias Braun818c78d2015-08-31 18:25:11 +00002214 const TargetRegisterClass *VR128 = &X86::VR128RegClass;
2215
Sanjay Patel302404b2015-03-05 21:46:54 +00002216 // If we have AVX, create 1 blendv instead of 3 logic instructions.
2217 // Blendv was introduced with SSE 4.1, but the 2 register form implicitly
2218 // uses XMM0 as the selection register. That may need just as many
2219 // instructions as the AND/ANDN/OR sequence due to register moves, so
2220 // don't bother.
2221 unsigned CmpOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002222 (RetVT == MVT::f32) ? X86::VCMPSSrr : X86::VCMPSDrr;
Sanjay Patel302404b2015-03-05 21:46:54 +00002223 unsigned BlendOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002224 (RetVT == MVT::f32) ? X86::VBLENDVPSrr : X86::VBLENDVPDrr;
2225
Craig Topper7ef6ea32016-12-05 04:51:31 +00002226 unsigned CmpReg = fastEmitInst_rri(CmpOpcode, RC, CmpLHSReg, CmpLHSIsKill,
Sanjay Patel302404b2015-03-05 21:46:54 +00002227 CmpRHSReg, CmpRHSIsKill, CC);
Matthias Braun818c78d2015-08-31 18:25:11 +00002228 unsigned VBlendReg = fastEmitInst_rrr(BlendOpcode, VR128, RHSReg, RHSIsKill,
2229 LHSReg, LHSIsKill, CmpReg, true);
2230 ResultReg = createResultReg(RC);
2231 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2232 TII.get(TargetOpcode::COPY), ResultReg).addReg(VBlendReg);
Sanjay Patel302404b2015-03-05 21:46:54 +00002233 } else {
Craig Topper6413f8a2016-12-06 04:58:39 +00002234 const TargetRegisterClass *VR128 = &X86::VR128RegClass;
Sanjay Patel302404b2015-03-05 21:46:54 +00002235 unsigned CmpReg = fastEmitInst_rri(Opc[0], RC, CmpLHSReg, CmpLHSIsKill,
2236 CmpRHSReg, CmpRHSIsKill, CC);
Craig Topper6413f8a2016-12-06 04:58:39 +00002237 unsigned AndReg = fastEmitInst_rr(Opc[1], VR128, CmpReg, /*IsKill=*/false,
Sanjay Patel302404b2015-03-05 21:46:54 +00002238 LHSReg, LHSIsKill);
Craig Topper6413f8a2016-12-06 04:58:39 +00002239 unsigned AndNReg = fastEmitInst_rr(Opc[2], VR128, CmpReg, /*IsKill=*/true,
Sanjay Patel302404b2015-03-05 21:46:54 +00002240 RHSReg, RHSIsKill);
Craig Topper6413f8a2016-12-06 04:58:39 +00002241 unsigned OrReg = fastEmitInst_rr(Opc[3], VR128, AndNReg, /*IsKill=*/true,
2242 AndReg, /*IsKill=*/true);
2243 ResultReg = createResultReg(RC);
2244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2245 TII.get(TargetOpcode::COPY), ResultReg).addReg(OrReg);
Sanjay Patel302404b2015-03-05 21:46:54 +00002246 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002247 updateValueMap(I, ResultReg);
2248 return true;
2249}
2250
2251bool X86FastISel::X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I) {
2252 // These are pseudo CMOV instructions and will be later expanded into control-
2253 // flow.
2254 unsigned Opc;
2255 switch (RetVT.SimpleTy) {
2256 default: return false;
2257 case MVT::i8: Opc = X86::CMOV_GR8; break;
2258 case MVT::i16: Opc = X86::CMOV_GR16; break;
2259 case MVT::i32: Opc = X86::CMOV_GR32; break;
2260 case MVT::f32: Opc = X86::CMOV_FR32; break;
2261 case MVT::f64: Opc = X86::CMOV_FR64; break;
2262 }
2263
2264 const Value *Cond = I->getOperand(0);
2265 X86::CondCode CC = X86::COND_NE;
2266
2267 // Optimize conditions coming from a compare if both instructions are in the
2268 // same basic block (values defined in other basic blocks may not have
2269 // initialized registers).
2270 const auto *CI = dyn_cast<CmpInst>(Cond);
2271 if (CI && (CI->getParent() == I->getParent())) {
2272 bool NeedSwap;
Igor Bregerdb754552017-05-11 06:36:37 +00002273 std::tie(CC, NeedSwap) = X86::getX86ConditionCode(CI->getPredicate());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002274 if (CC > X86::LAST_VALID_COND)
2275 return false;
2276
2277 const Value *CmpLHS = CI->getOperand(0);
2278 const Value *CmpRHS = CI->getOperand(1);
2279
2280 if (NeedSwap)
2281 std::swap(CmpLHS, CmpRHS);
2282
Mehdi Amini44ede332015-07-09 02:09:04 +00002283 EVT CmpVT = TLI.getValueType(DL, CmpLHS->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002284 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT, CI->getDebugLoc()))
2285 return false;
2286 } else {
2287 unsigned CondReg = getRegForValue(Cond);
2288 if (CondReg == 0)
2289 return false;
2290 bool CondIsKill = hasTrivialKill(Cond);
Guy Blank9ae797a2016-08-21 08:02:27 +00002291
Guy Blank2bdc74a2016-09-28 11:22:17 +00002292 // In case OpReg is a K register, COPY to a GPR
2293 if (MRI.getRegClass(CondReg) == &X86::VK1RegClass) {
2294 unsigned KCondReg = CondReg;
Craig Topper058f2f62017-03-28 16:35:29 +00002295 CondReg = createResultReg(&X86::GR32RegClass);
Guy Blank9ae797a2016-08-21 08:02:27 +00002296 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Guy Blank2bdc74a2016-09-28 11:22:17 +00002297 TII.get(TargetOpcode::COPY), CondReg)
2298 .addReg(KCondReg, getKillRegState(CondIsKill));
Craig Topper058f2f62017-03-28 16:35:29 +00002299 CondReg = fastEmitInst_extractsubreg(MVT::i8, CondReg, /*Kill=*/true,
2300 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00002301 }
2302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
2303 .addReg(CondReg, getKillRegState(CondIsKill))
2304 .addImm(1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002305 }
2306
2307 const Value *LHS = I->getOperand(1);
2308 const Value *RHS = I->getOperand(2);
2309
2310 unsigned LHSReg = getRegForValue(LHS);
2311 bool LHSIsKill = hasTrivialKill(LHS);
2312
2313 unsigned RHSReg = getRegForValue(RHS);
2314 bool RHSIsKill = hasTrivialKill(RHS);
2315
2316 if (!LHSReg || !RHSReg)
2317 return false;
2318
2319 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2320
2321 unsigned ResultReg =
2322 fastEmitInst_rri(Opc, RC, RHSReg, RHSIsKill, LHSReg, LHSIsKill, CC);
2323 updateValueMap(I, ResultReg);
2324 return true;
2325}
2326
2327bool X86FastISel::X86SelectSelect(const Instruction *I) {
2328 MVT RetVT;
2329 if (!isTypeLegal(I->getType(), RetVT))
2330 return false;
2331
2332 // Check if we can fold the select.
2333 if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) {
2334 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2335 const Value *Opnd = nullptr;
2336 switch (Predicate) {
2337 default: break;
2338 case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break;
2339 case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break;
2340 }
2341 // No need for a select anymore - this is an unconditional move.
2342 if (Opnd) {
2343 unsigned OpReg = getRegForValue(Opnd);
2344 if (OpReg == 0)
2345 return false;
2346 bool OpIsKill = hasTrivialKill(Opnd);
2347 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2348 unsigned ResultReg = createResultReg(RC);
2349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2350 TII.get(TargetOpcode::COPY), ResultReg)
2351 .addReg(OpReg, getKillRegState(OpIsKill));
2352 updateValueMap(I, ResultReg);
2353 return true;
2354 }
2355 }
2356
2357 // First try to use real conditional move instructions.
2358 if (X86FastEmitCMoveSelect(RetVT, I))
2359 return true;
2360
2361 // Try to use a sequence of SSE instructions to simulate a conditional move.
2362 if (X86FastEmitSSESelect(RetVT, I))
2363 return true;
2364
2365 // Fall-back to pseudo conditional move instructions, which will be later
2366 // converted to control-flow.
2367 if (X86FastEmitPseudoSelect(RetVT, I))
2368 return true;
2369
2370 return false;
2371}
2372
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002373bool X86FastISel::X86SelectSIToFP(const Instruction *I) {
Andrea Di Biagio98c36702015-04-20 11:56:59 +00002374 // The target-independent selection algorithm in FastISel already knows how
2375 // to select a SINT_TO_FP if the target is SSE but not AVX.
2376 // Early exit if the subtarget doesn't have AVX.
2377 if (!Subtarget->hasAVX())
2378 return false;
2379
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002380 if (!I->getOperand(0)->getType()->isIntegerTy(32))
2381 return false;
2382
2383 // Select integer to float/double conversion.
2384 unsigned OpReg = getRegForValue(I->getOperand(0));
2385 if (OpReg == 0)
2386 return false;
2387
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002388 const TargetRegisterClass *RC = nullptr;
2389 unsigned Opcode;
2390
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002391 if (I->getType()->isDoubleTy()) {
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002392 // sitofp int -> double
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002393 Opcode = X86::VCVTSI2SDrr;
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002394 RC = &X86::FR64RegClass;
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002395 } else if (I->getType()->isFloatTy()) {
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002396 // sitofp int -> float
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002397 Opcode = X86::VCVTSI2SSrr;
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002398 RC = &X86::FR32RegClass;
2399 } else
2400 return false;
2401
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002402 unsigned ImplicitDefReg = createResultReg(RC);
2403 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2404 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2405 unsigned ResultReg =
2406 fastEmitInst_rr(Opcode, RC, ImplicitDefReg, true, OpReg, false);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002407 updateValueMap(I, ResultReg);
2408 return true;
2409}
2410
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002411// Helper method used by X86SelectFPExt and X86SelectFPTrunc.
2412bool X86FastISel::X86SelectFPExtOrFPTrunc(const Instruction *I,
2413 unsigned TargetOpc,
2414 const TargetRegisterClass *RC) {
2415 assert((I->getOpcode() == Instruction::FPExt ||
2416 I->getOpcode() == Instruction::FPTrunc) &&
2417 "Instruction must be an FPExt or FPTrunc!");
2418
2419 unsigned OpReg = getRegForValue(I->getOperand(0));
2420 if (OpReg == 0)
2421 return false;
2422
Ayman Musa9b802e42017-03-01 10:20:48 +00002423 unsigned ImplicitDefReg;
2424 if (Subtarget->hasAVX()) {
2425 ImplicitDefReg = createResultReg(RC);
2426 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2427 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2428
2429 }
2430
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002431 unsigned ResultReg = createResultReg(RC);
2432 MachineInstrBuilder MIB;
2433 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpc),
2434 ResultReg);
Ayman Musa4b2c9682017-02-23 13:15:44 +00002435
Ayman Musa9b802e42017-03-01 10:20:48 +00002436 if (Subtarget->hasAVX())
Ayman Musa4b2c9682017-02-23 13:15:44 +00002437 MIB.addReg(ImplicitDefReg);
Ayman Musa9b802e42017-03-01 10:20:48 +00002438
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002439 MIB.addReg(OpReg);
2440 updateValueMap(I, ResultReg);
2441 return true;
2442}
2443
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002444bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002445 if (X86ScalarSSEf64 && I->getType()->isDoubleTy() &&
2446 I->getOperand(0)->getType()->isFloatTy()) {
2447 // fpext from float to double.
2448 unsigned Opc = Subtarget->hasAVX() ? X86::VCVTSS2SDrr : X86::CVTSS2SDrr;
2449 return X86SelectFPExtOrFPTrunc(I, Opc, &X86::FR64RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002450 }
2451
2452 return false;
2453}
2454
2455bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002456 if (X86ScalarSSEf64 && I->getType()->isFloatTy() &&
2457 I->getOperand(0)->getType()->isDoubleTy()) {
2458 // fptrunc from double to float.
2459 unsigned Opc = Subtarget->hasAVX() ? X86::VCVTSD2SSrr : X86::CVTSD2SSrr;
2460 return X86SelectFPExtOrFPTrunc(I, Opc, &X86::FR32RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002461 }
2462
2463 return false;
2464}
2465
2466bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002467 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
2468 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002469
2470 // This code only handles truncation to byte.
Craig Topper331297c2017-03-28 23:20:37 +00002471 // TODO: Support truncate to i1 with AVX512.
2472 if (DstVT != MVT::i8 && (DstVT != MVT::i1 || Subtarget->hasAVX512()))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002473 return false;
2474 if (!TLI.isTypeLegal(SrcVT))
2475 return false;
2476
2477 unsigned InputReg = getRegForValue(I->getOperand(0));
2478 if (!InputReg)
2479 // Unhandled operand. Halt "fast" selection and bail.
2480 return false;
2481
2482 if (SrcVT == MVT::i8) {
2483 // Truncate from i8 to i1; no code needed.
2484 updateValueMap(I, InputReg);
2485 return true;
2486 }
2487
Pete Cooper7f7c9f12015-05-08 18:29:42 +00002488 bool KillInputReg = false;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002489 if (!Subtarget->is64Bit()) {
2490 // If we're on x86-32; we can't extract an i8 from a general register.
2491 // First issue a copy to GR16_ABCD or GR32_ABCD.
2492 const TargetRegisterClass *CopyRC =
2493 (SrcVT == MVT::i16) ? &X86::GR16_ABCDRegClass : &X86::GR32_ABCDRegClass;
2494 unsigned CopyReg = createResultReg(CopyRC);
2495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2496 TII.get(TargetOpcode::COPY), CopyReg).addReg(InputReg);
2497 InputReg = CopyReg;
Pete Cooper7f7c9f12015-05-08 18:29:42 +00002498 KillInputReg = true;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002499 }
2500
2501 // Issue an extract_subreg.
2502 unsigned ResultReg = fastEmitInst_extractsubreg(MVT::i8,
Pete Cooper7f7c9f12015-05-08 18:29:42 +00002503 InputReg, KillInputReg,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002504 X86::sub_8bit);
2505 if (!ResultReg)
2506 return false;
2507
2508 updateValueMap(I, ResultReg);
2509 return true;
2510}
2511
2512bool X86FastISel::IsMemcpySmall(uint64_t Len) {
2513 return Len <= (Subtarget->is64Bit() ? 32 : 16);
2514}
2515
2516bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
2517 X86AddressMode SrcAM, uint64_t Len) {
2518
2519 // Make sure we don't bloat code by inlining very large memcpy's.
2520 if (!IsMemcpySmall(Len))
2521 return false;
2522
2523 bool i64Legal = Subtarget->is64Bit();
2524
2525 // We don't care about alignment here since we just emit integer accesses.
2526 while (Len) {
2527 MVT VT;
2528 if (Len >= 8 && i64Legal)
2529 VT = MVT::i64;
2530 else if (Len >= 4)
2531 VT = MVT::i32;
2532 else if (Len >= 2)
2533 VT = MVT::i16;
2534 else
2535 VT = MVT::i8;
2536
2537 unsigned Reg;
2538 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
2539 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
2540 assert(RV && "Failed to emit load or store??");
2541
2542 unsigned Size = VT.getSizeInBits()/8;
2543 Len -= Size;
2544 DestAM.Disp += Size;
2545 SrcAM.Disp += Size;
2546 }
2547
2548 return true;
2549}
2550
2551bool X86FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
2552 // FIXME: Handle more intrinsics.
2553 switch (II->getIntrinsicID()) {
2554 default: return false;
Andrea Di Biagio70351782015-02-20 19:37:14 +00002555 case Intrinsic::convert_from_fp16:
2556 case Intrinsic::convert_to_fp16: {
Eric Christopher824f42f2015-05-12 01:26:05 +00002557 if (Subtarget->useSoftFloat() || !Subtarget->hasF16C())
Andrea Di Biagio70351782015-02-20 19:37:14 +00002558 return false;
2559
2560 const Value *Op = II->getArgOperand(0);
2561 unsigned InputReg = getRegForValue(Op);
2562 if (InputReg == 0)
2563 return false;
2564
2565 // F16C only allows converting from float to half and from half to float.
2566 bool IsFloatToHalf = II->getIntrinsicID() == Intrinsic::convert_to_fp16;
2567 if (IsFloatToHalf) {
2568 if (!Op->getType()->isFloatTy())
2569 return false;
2570 } else {
2571 if (!II->getType()->isFloatTy())
2572 return false;
2573 }
2574
2575 unsigned ResultReg = 0;
2576 const TargetRegisterClass *RC = TLI.getRegClassFor(MVT::v8i16);
2577 if (IsFloatToHalf) {
2578 // 'InputReg' is implicitly promoted from register class FR32 to
2579 // register class VR128 by method 'constrainOperandRegClass' which is
2580 // directly called by 'fastEmitInst_ri'.
2581 // Instruction VCVTPS2PHrr takes an extra immediate operand which is
Ahmed Bougacha68a8efa2016-02-02 01:44:03 +00002582 // used to provide rounding control: use MXCSR.RC, encoded as 0b100.
2583 // It's consistent with the other FP instructions, which are usually
2584 // controlled by MXCSR.
2585 InputReg = fastEmitInst_ri(X86::VCVTPS2PHrr, RC, InputReg, false, 4);
Andrea Di Biagio70351782015-02-20 19:37:14 +00002586
2587 // Move the lower 32-bits of ResultReg to another register of class GR32.
2588 ResultReg = createResultReg(&X86::GR32RegClass);
2589 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2590 TII.get(X86::VMOVPDI2DIrr), ResultReg)
2591 .addReg(InputReg, RegState::Kill);
2592
2593 // The result value is in the lower 16-bits of ResultReg.
2594 unsigned RegIdx = X86::sub_16bit;
2595 ResultReg = fastEmitInst_extractsubreg(MVT::i16, ResultReg, true, RegIdx);
2596 } else {
2597 assert(Op->getType()->isIntegerTy(16) && "Expected a 16-bit integer!");
2598 // Explicitly sign-extend the input to 32-bit.
2599 InputReg = fastEmit_r(MVT::i16, MVT::i32, ISD::SIGN_EXTEND, InputReg,
2600 /*Kill=*/false);
2601
2602 // The following SCALAR_TO_VECTOR will be expanded into a VMOVDI2PDIrr.
2603 InputReg = fastEmit_r(MVT::i32, MVT::v4i32, ISD::SCALAR_TO_VECTOR,
2604 InputReg, /*Kill=*/true);
2605
2606 InputReg = fastEmitInst_r(X86::VCVTPH2PSrr, RC, InputReg, /*Kill=*/true);
2607
2608 // The result value is in the lower 32-bits of ResultReg.
2609 // Emit an explicit copy from register class VR128 to register class FR32.
2610 ResultReg = createResultReg(&X86::FR32RegClass);
2611 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2612 TII.get(TargetOpcode::COPY), ResultReg)
2613 .addReg(InputReg, RegState::Kill);
2614 }
2615
2616 updateValueMap(II, ResultReg);
2617 return true;
2618 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002619 case Intrinsic::frameaddress: {
David Majnemerca194852015-02-10 22:00:34 +00002620 MachineFunction *MF = FuncInfo.MF;
2621 if (MF->getTarget().getMCAsmInfo()->usesWindowsCFI())
2622 return false;
2623
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002624 Type *RetTy = II->getCalledFunction()->getReturnType();
2625
2626 MVT VT;
2627 if (!isTypeLegal(RetTy, VT))
2628 return false;
2629
2630 unsigned Opc;
2631 const TargetRegisterClass *RC = nullptr;
2632
2633 switch (VT.SimpleTy) {
2634 default: llvm_unreachable("Invalid result type for frameaddress.");
2635 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
2636 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
2637 }
2638
2639 // This needs to be set before we call getPtrSizedFrameRegister, otherwise
2640 // we get the wrong frame register.
Matthias Braun941a7052016-07-28 18:40:00 +00002641 MachineFrameInfo &MFI = MF->getFrameInfo();
2642 MFI.setFrameAddressIsTaken(true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002643
Eric Christophera1c535b2015-02-02 23:03:45 +00002644 const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
David Majnemerca194852015-02-10 22:00:34 +00002645 unsigned FrameReg = RegInfo->getPtrSizedFrameRegister(*MF);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002646 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
2647 (FrameReg == X86::EBP && VT == MVT::i32)) &&
2648 "Invalid Frame Register!");
2649
2650 // Always make a copy of the frame register to to a vreg first, so that we
2651 // never directly reference the frame register (the TwoAddressInstruction-
2652 // Pass doesn't like that).
2653 unsigned SrcReg = createResultReg(RC);
2654 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2655 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
2656
2657 // Now recursively load from the frame address.
2658 // movq (%rbp), %rax
2659 // movq (%rax), %rax
2660 // movq (%rax), %rax
2661 // ...
2662 unsigned DestReg;
2663 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
2664 while (Depth--) {
2665 DestReg = createResultReg(RC);
2666 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2667 TII.get(Opc), DestReg), SrcReg);
2668 SrcReg = DestReg;
2669 }
2670
2671 updateValueMap(II, SrcReg);
2672 return true;
2673 }
2674 case Intrinsic::memcpy: {
2675 const MemCpyInst *MCI = cast<MemCpyInst>(II);
2676 // Don't handle volatile or variable length memcpys.
2677 if (MCI->isVolatile())
2678 return false;
2679
2680 if (isa<ConstantInt>(MCI->getLength())) {
2681 // Small memcpy's are common enough that we want to do them
2682 // without a call if possible.
2683 uint64_t Len = cast<ConstantInt>(MCI->getLength())->getZExtValue();
2684 if (IsMemcpySmall(Len)) {
2685 X86AddressMode DestAM, SrcAM;
2686 if (!X86SelectAddress(MCI->getRawDest(), DestAM) ||
2687 !X86SelectAddress(MCI->getRawSource(), SrcAM))
2688 return false;
2689 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
2690 return true;
2691 }
2692 }
2693
2694 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2695 if (!MCI->getLength()->getType()->isIntegerTy(SizeWidth))
2696 return false;
2697
2698 if (MCI->getSourceAddressSpace() > 255 || MCI->getDestAddressSpace() > 255)
2699 return false;
2700
Pete Cooper67cf9a72015-11-19 05:56:52 +00002701 return lowerCallTo(II, "memcpy", II->getNumArgOperands() - 2);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002702 }
2703 case Intrinsic::memset: {
2704 const MemSetInst *MSI = cast<MemSetInst>(II);
2705
2706 if (MSI->isVolatile())
2707 return false;
2708
2709 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2710 if (!MSI->getLength()->getType()->isIntegerTy(SizeWidth))
2711 return false;
2712
2713 if (MSI->getDestAddressSpace() > 255)
2714 return false;
2715
Pete Cooper67cf9a72015-11-19 05:56:52 +00002716 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002717 }
2718 case Intrinsic::stackprotector: {
2719 // Emit code to store the stack guard onto the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002720 EVT PtrTy = TLI.getPointerTy(DL);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002721
2722 const Value *Op1 = II->getArgOperand(0); // The guard's value.
2723 const AllocaInst *Slot = cast<AllocaInst>(II->getArgOperand(1));
2724
2725 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
2726
2727 // Grab the frame index.
2728 X86AddressMode AM;
2729 if (!X86SelectAddress(Slot, AM)) return false;
2730 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
2731 return true;
2732 }
2733 case Intrinsic::dbg_declare: {
2734 const DbgDeclareInst *DI = cast<DbgDeclareInst>(II);
2735 X86AddressMode AM;
2736 assert(DI->getAddress() && "Null address should be checked earlier!");
2737 if (!X86SelectAddress(DI->getAddress(), AM))
2738 return false;
2739 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
2740 // FIXME may need to add RegState::Debug to any registers produced,
2741 // although ESP/EBP should be the only ones at the moment.
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00002742 assert(DI->getVariable()->isValidLocationForIntrinsic(DbgLoc) &&
2743 "Expected inlined-at fields to agree");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002744 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM)
2745 .addImm(0)
2746 .addMetadata(DI->getVariable())
2747 .addMetadata(DI->getExpression());
2748 return true;
2749 }
2750 case Intrinsic::trap: {
2751 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
2752 return true;
2753 }
2754 case Intrinsic::sqrt: {
2755 if (!Subtarget->hasSSE1())
2756 return false;
2757
2758 Type *RetTy = II->getCalledFunction()->getReturnType();
2759
2760 MVT VT;
2761 if (!isTypeLegal(RetTy, VT))
2762 return false;
2763
2764 // Unfortunately we can't use fastEmit_r, because the AVX version of FSQRT
2765 // is not generated by FastISel yet.
2766 // FIXME: Update this code once tablegen can handle it.
Craig Toppercf65c622016-03-02 04:42:31 +00002767 static const uint16_t SqrtOpc[2][2] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002768 {X86::SQRTSSr, X86::VSQRTSSr},
2769 {X86::SQRTSDr, X86::VSQRTSDr}
2770 };
2771 bool HasAVX = Subtarget->hasAVX();
2772 unsigned Opc;
2773 const TargetRegisterClass *RC;
2774 switch (VT.SimpleTy) {
2775 default: return false;
2776 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
2777 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
2778 }
2779
2780 const Value *SrcVal = II->getArgOperand(0);
2781 unsigned SrcReg = getRegForValue(SrcVal);
2782
2783 if (SrcReg == 0)
2784 return false;
2785
2786 unsigned ImplicitDefReg = 0;
2787 if (HasAVX) {
2788 ImplicitDefReg = createResultReg(RC);
2789 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2790 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2791 }
2792
2793 unsigned ResultReg = createResultReg(RC);
2794 MachineInstrBuilder MIB;
2795 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
2796 ResultReg);
2797
2798 if (ImplicitDefReg)
2799 MIB.addReg(ImplicitDefReg);
2800
2801 MIB.addReg(SrcReg);
2802
2803 updateValueMap(II, ResultReg);
2804 return true;
2805 }
2806 case Intrinsic::sadd_with_overflow:
2807 case Intrinsic::uadd_with_overflow:
2808 case Intrinsic::ssub_with_overflow:
2809 case Intrinsic::usub_with_overflow:
2810 case Intrinsic::smul_with_overflow:
2811 case Intrinsic::umul_with_overflow: {
2812 // This implements the basic lowering of the xalu with overflow intrinsics
2813 // into add/sub/mul followed by either seto or setb.
2814 const Function *Callee = II->getCalledFunction();
2815 auto *Ty = cast<StructType>(Callee->getReturnType());
2816 Type *RetTy = Ty->getTypeAtIndex(0U);
Zvi Rackover6f76f462016-11-15 13:50:35 +00002817 assert(Ty->getTypeAtIndex(1)->isIntegerTy() &&
2818 Ty->getTypeAtIndex(1)->getScalarSizeInBits() == 1 &&
2819 "Overflow value expected to be an i1");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002820
2821 MVT VT;
2822 if (!isTypeLegal(RetTy, VT))
2823 return false;
2824
2825 if (VT < MVT::i8 || VT > MVT::i64)
2826 return false;
2827
2828 const Value *LHS = II->getArgOperand(0);
2829 const Value *RHS = II->getArgOperand(1);
2830
2831 // Canonicalize immediate to the RHS.
2832 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2833 isCommutativeIntrinsic(II))
2834 std::swap(LHS, RHS);
2835
2836 bool UseIncDec = false;
2837 if (isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isOne())
2838 UseIncDec = true;
2839
2840 unsigned BaseOpc, CondOpc;
2841 switch (II->getIntrinsicID()) {
2842 default: llvm_unreachable("Unexpected intrinsic!");
2843 case Intrinsic::sadd_with_overflow:
2844 BaseOpc = UseIncDec ? unsigned(X86ISD::INC) : unsigned(ISD::ADD);
2845 CondOpc = X86::SETOr;
2846 break;
2847 case Intrinsic::uadd_with_overflow:
2848 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
2849 case Intrinsic::ssub_with_overflow:
2850 BaseOpc = UseIncDec ? unsigned(X86ISD::DEC) : unsigned(ISD::SUB);
2851 CondOpc = X86::SETOr;
2852 break;
2853 case Intrinsic::usub_with_overflow:
2854 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
2855 case Intrinsic::smul_with_overflow:
2856 BaseOpc = X86ISD::SMUL; CondOpc = X86::SETOr; break;
2857 case Intrinsic::umul_with_overflow:
2858 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
2859 }
2860
2861 unsigned LHSReg = getRegForValue(LHS);
2862 if (LHSReg == 0)
2863 return false;
2864 bool LHSIsKill = hasTrivialKill(LHS);
2865
2866 unsigned ResultReg = 0;
2867 // Check if we have an immediate version.
2868 if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
Craig Topper66111882016-06-02 04:19:42 +00002869 static const uint16_t Opc[2][4] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002870 { X86::INC8r, X86::INC16r, X86::INC32r, X86::INC64r },
2871 { X86::DEC8r, X86::DEC16r, X86::DEC32r, X86::DEC64r }
2872 };
2873
2874 if (BaseOpc == X86ISD::INC || BaseOpc == X86ISD::DEC) {
2875 ResultReg = createResultReg(TLI.getRegClassFor(VT));
2876 bool IsDec = BaseOpc == X86ISD::DEC;
2877 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2878 TII.get(Opc[IsDec][VT.SimpleTy-MVT::i8]), ResultReg)
2879 .addReg(LHSReg, getKillRegState(LHSIsKill));
2880 } else
2881 ResultReg = fastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2882 CI->getZExtValue());
2883 }
2884
2885 unsigned RHSReg;
2886 bool RHSIsKill;
2887 if (!ResultReg) {
2888 RHSReg = getRegForValue(RHS);
2889 if (RHSReg == 0)
2890 return false;
2891 RHSIsKill = hasTrivialKill(RHS);
2892 ResultReg = fastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2893 RHSIsKill);
2894 }
2895
2896 // FastISel doesn't have a pattern for all X86::MUL*r and X86::IMUL*r. Emit
2897 // it manually.
2898 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
Craig Toppercf65c622016-03-02 04:42:31 +00002899 static const uint16_t MULOpc[] =
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002900 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
Craig Toppercf65c622016-03-02 04:42:31 +00002901 static const MCPhysReg Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002902 // First copy the first operand into RAX, which is an implicit input to
2903 // the X86::MUL*r instruction.
2904 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2905 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2906 .addReg(LHSReg, getKillRegState(LHSIsKill));
2907 ResultReg = fastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2908 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
2909 } else if (BaseOpc == X86ISD::SMUL && !ResultReg) {
Craig Toppercf65c622016-03-02 04:42:31 +00002910 static const uint16_t MULOpc[] =
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002911 { X86::IMUL8r, X86::IMUL16rr, X86::IMUL32rr, X86::IMUL64rr };
2912 if (VT == MVT::i8) {
2913 // Copy the first operand into AL, which is an implicit input to the
2914 // X86::IMUL8r instruction.
2915 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2916 TII.get(TargetOpcode::COPY), X86::AL)
2917 .addReg(LHSReg, getKillRegState(LHSIsKill));
2918 ResultReg = fastEmitInst_r(MULOpc[0], TLI.getRegClassFor(VT), RHSReg,
2919 RHSIsKill);
2920 } else
2921 ResultReg = fastEmitInst_rr(MULOpc[VT.SimpleTy-MVT::i8],
2922 TLI.getRegClassFor(VT), LHSReg, LHSIsKill,
2923 RHSReg, RHSIsKill);
2924 }
2925
2926 if (!ResultReg)
2927 return false;
2928
Zvi Rackoverf0b9b57b2016-11-15 13:29:23 +00002929 // Assign to a GPR since the overflow return value is lowered to a SETcc.
2930 unsigned ResultReg2 = createResultReg(&X86::GR8RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002931 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
2932 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
2933 ResultReg2);
2934
2935 updateValueMap(II, ResultReg, 2);
2936 return true;
2937 }
2938 case Intrinsic::x86_sse_cvttss2si:
2939 case Intrinsic::x86_sse_cvttss2si64:
2940 case Intrinsic::x86_sse2_cvttsd2si:
2941 case Intrinsic::x86_sse2_cvttsd2si64: {
2942 bool IsInputDouble;
2943 switch (II->getIntrinsicID()) {
2944 default: llvm_unreachable("Unexpected intrinsic.");
2945 case Intrinsic::x86_sse_cvttss2si:
2946 case Intrinsic::x86_sse_cvttss2si64:
2947 if (!Subtarget->hasSSE1())
2948 return false;
2949 IsInputDouble = false;
2950 break;
2951 case Intrinsic::x86_sse2_cvttsd2si:
2952 case Intrinsic::x86_sse2_cvttsd2si64:
2953 if (!Subtarget->hasSSE2())
2954 return false;
2955 IsInputDouble = true;
2956 break;
2957 }
2958
2959 Type *RetTy = II->getCalledFunction()->getReturnType();
2960 MVT VT;
2961 if (!isTypeLegal(RetTy, VT))
2962 return false;
2963
Craig Topper66111882016-06-02 04:19:42 +00002964 static const uint16_t CvtOpc[2][2][2] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002965 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
2966 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
2967 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
2968 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
2969 };
2970 bool HasAVX = Subtarget->hasAVX();
2971 unsigned Opc;
2972 switch (VT.SimpleTy) {
2973 default: llvm_unreachable("Unexpected result type.");
2974 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
2975 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
2976 }
2977
2978 // Check if we can fold insertelement instructions into the convert.
2979 const Value *Op = II->getArgOperand(0);
2980 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
2981 const Value *Index = IE->getOperand(2);
2982 if (!isa<ConstantInt>(Index))
2983 break;
2984 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
2985
2986 if (Idx == 0) {
2987 Op = IE->getOperand(1);
2988 break;
2989 }
2990 Op = IE->getOperand(0);
2991 }
2992
2993 unsigned Reg = getRegForValue(Op);
2994 if (Reg == 0)
2995 return false;
2996
2997 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
2998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2999 .addReg(Reg);
3000
3001 updateValueMap(II, ResultReg);
3002 return true;
3003 }
3004 }
3005}
3006
3007bool X86FastISel::fastLowerArguments() {
3008 if (!FuncInfo.CanLowerReturn)
3009 return false;
3010
3011 const Function *F = FuncInfo.Fn;
3012 if (F->isVarArg())
3013 return false;
3014
3015 CallingConv::ID CC = F->getCallingConv();
3016 if (CC != CallingConv::C)
3017 return false;
3018
3019 if (Subtarget->isCallingConvWin64(CC))
3020 return false;
3021
3022 if (!Subtarget->is64Bit())
3023 return false;
3024
Davide Italianoa63981a2017-07-12 15:26:06 +00003025 if (Subtarget->useSoftFloat())
3026 return false;
3027
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003028 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
3029 unsigned GPRCnt = 0;
3030 unsigned FPRCnt = 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003031 for (auto const &Arg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00003032 if (Arg.hasAttribute(Attribute::ByVal) ||
3033 Arg.hasAttribute(Attribute::InReg) ||
3034 Arg.hasAttribute(Attribute::StructRet) ||
3035 Arg.hasAttribute(Attribute::SwiftSelf) ||
3036 Arg.hasAttribute(Attribute::SwiftError) ||
3037 Arg.hasAttribute(Attribute::Nest))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003038 return false;
3039
3040 Type *ArgTy = Arg.getType();
3041 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3042 return false;
3043
Mehdi Amini44ede332015-07-09 02:09:04 +00003044 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003045 if (!ArgVT.isSimple()) return false;
3046 switch (ArgVT.getSimpleVT().SimpleTy) {
3047 default: return false;
3048 case MVT::i32:
3049 case MVT::i64:
3050 ++GPRCnt;
3051 break;
3052 case MVT::f32:
3053 case MVT::f64:
3054 if (!Subtarget->hasSSE1())
3055 return false;
3056 ++FPRCnt;
3057 break;
3058 }
3059
3060 if (GPRCnt > 6)
3061 return false;
3062
3063 if (FPRCnt > 8)
3064 return false;
3065 }
3066
3067 static const MCPhysReg GPR32ArgRegs[] = {
3068 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
3069 };
3070 static const MCPhysReg GPR64ArgRegs[] = {
3071 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
3072 };
3073 static const MCPhysReg XMMArgRegs[] = {
3074 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3075 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3076 };
3077
3078 unsigned GPRIdx = 0;
3079 unsigned FPRIdx = 0;
3080 for (auto const &Arg : F->args()) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003081 MVT VT = TLI.getSimpleValueType(DL, Arg.getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003082 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
3083 unsigned SrcReg;
3084 switch (VT.SimpleTy) {
3085 default: llvm_unreachable("Unexpected value type.");
3086 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
3087 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00003088 case MVT::f32: LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003089 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
3090 }
3091 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3092 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3093 // Without this, EmitLiveInCopies may eliminate the livein if its only
3094 // use is a bitcast (which isn't turned into an instruction).
3095 unsigned ResultReg = createResultReg(RC);
3096 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3097 TII.get(TargetOpcode::COPY), ResultReg)
3098 .addReg(DstReg, getKillRegState(true));
3099 updateValueMap(&Arg, ResultReg);
3100 }
3101 return true;
3102}
3103
Nico Weberaf7e8462016-07-14 01:52:51 +00003104static unsigned computeBytesPoppedByCalleeForSRet(const X86Subtarget *Subtarget,
3105 CallingConv::ID CC,
3106 ImmutableCallSite *CS) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003107 if (Subtarget->is64Bit())
3108 return 0;
3109 if (Subtarget->getTargetTriple().isOSMSVCRT())
3110 return 0;
3111 if (CC == CallingConv::Fast || CC == CallingConv::GHC ||
3112 CC == CallingConv::HiPE)
3113 return 0;
Sanjoy Dasb11b4402015-11-04 20:33:45 +00003114
3115 if (CS)
Reid Klecknerfb502d22017-04-14 20:19:02 +00003116 if (CS->arg_empty() || !CS->paramHasAttr(0, Attribute::StructRet) ||
3117 CS->paramHasAttr(0, Attribute::InReg) || Subtarget->isTargetMCU())
Sanjoy Dasb11b4402015-11-04 20:33:45 +00003118 return 0;
3119
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003120 return 4;
3121}
3122
3123bool X86FastISel::fastLowerCall(CallLoweringInfo &CLI) {
3124 auto &OutVals = CLI.OutVals;
3125 auto &OutFlags = CLI.OutFlags;
3126 auto &OutRegs = CLI.OutRegs;
3127 auto &Ins = CLI.Ins;
3128 auto &InRegs = CLI.InRegs;
3129 CallingConv::ID CC = CLI.CallConv;
3130 bool &IsTailCall = CLI.IsTailCall;
3131 bool IsVarArg = CLI.IsVarArg;
3132 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00003133 MCSymbol *Symbol = CLI.Symbol;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003134
3135 bool Is64Bit = Subtarget->is64Bit();
3136 bool IsWin64 = Subtarget->isCallingConvWin64(CC);
3137
Oren Ben Simhondbd4bba2017-05-03 13:07:19 +00003138 const CallInst *CI =
3139 CLI.CS ? dyn_cast<CallInst>(CLI.CS->getInstruction()) : nullptr;
3140 const Function *CalledFn = CI ? CI->getCalledFunction() : nullptr;
3141
3142 // Functions with no_caller_saved_registers that need special handling.
3143 if ((CI && CI->hasFnAttr("no_caller_saved_registers")) ||
3144 (CalledFn && CalledFn->hasFnAttribute("no_caller_saved_registers")))
3145 return false;
3146
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003147 // Handle only C, fastcc, and webkit_js calling conventions for now.
3148 switch (CC) {
3149 default: return false;
3150 case CallingConv::C:
3151 case CallingConv::Fast:
3152 case CallingConv::WebKit_JS:
Manman Renf8bdd882016-04-05 22:41:47 +00003153 case CallingConv::Swift:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003154 case CallingConv::X86_FastCall:
Nico Weberecdf45b2016-07-14 13:54:26 +00003155 case CallingConv::X86_StdCall:
Nico Weberaf7e8462016-07-14 01:52:51 +00003156 case CallingConv::X86_ThisCall:
Martin Storsjo2f24e932017-07-17 20:05:19 +00003157 case CallingConv::Win64:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003158 case CallingConv::X86_64_SysV:
3159 break;
3160 }
3161
3162 // Allow SelectionDAG isel to handle tail calls.
3163 if (IsTailCall)
3164 return false;
3165
3166 // fastcc with -tailcallopt is intended to provide a guaranteed
3167 // tail call optimization. Fastisel doesn't know how to do that.
3168 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
3169 return false;
3170
3171 // Don't know how to handle Win64 varargs yet. Nothing special needed for
3172 // x86-32. Special handling for x86-64 is implemented.
3173 if (IsVarArg && IsWin64)
3174 return false;
3175
3176 // Don't know about inalloca yet.
3177 if (CLI.CS && CLI.CS->hasInAllocaArgument())
3178 return false;
3179
Manman Ren57518142016-04-11 21:08:06 +00003180 for (auto Flag : CLI.OutFlags)
3181 if (Flag.isSwiftError())
3182 return false;
3183
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003184 SmallVector<MVT, 16> OutVTs;
3185 SmallVector<unsigned, 16> ArgRegs;
3186
3187 // If this is a constant i1/i8/i16 argument, promote to i32 to avoid an extra
3188 // instruction. This is safe because it is common to all FastISel supported
3189 // calling conventions on x86.
3190 for (int i = 0, e = OutVals.size(); i != e; ++i) {
3191 Value *&Val = OutVals[i];
3192 ISD::ArgFlagsTy Flags = OutFlags[i];
3193 if (auto *CI = dyn_cast<ConstantInt>(Val)) {
3194 if (CI->getBitWidth() < 32) {
3195 if (Flags.isSExt())
3196 Val = ConstantExpr::getSExt(CI, Type::getInt32Ty(CI->getContext()));
3197 else
3198 Val = ConstantExpr::getZExt(CI, Type::getInt32Ty(CI->getContext()));
3199 }
3200 }
3201
3202 // Passing bools around ends up doing a trunc to i1 and passing it.
3203 // Codegen this as an argument + "and 1".
3204 MVT VT;
3205 auto *TI = dyn_cast<TruncInst>(Val);
3206 unsigned ResultReg;
3207 if (TI && TI->getType()->isIntegerTy(1) && CLI.CS &&
3208 (TI->getParent() == CLI.CS->getInstruction()->getParent()) &&
3209 TI->hasOneUse()) {
3210 Value *PrevVal = TI->getOperand(0);
3211 ResultReg = getRegForValue(PrevVal);
3212
3213 if (!ResultReg)
3214 return false;
3215
3216 if (!isTypeLegal(PrevVal->getType(), VT))
3217 return false;
3218
3219 ResultReg =
3220 fastEmit_ri(VT, VT, ISD::AND, ResultReg, hasTrivialKill(PrevVal), 1);
3221 } else {
3222 if (!isTypeLegal(Val->getType(), VT))
3223 return false;
3224 ResultReg = getRegForValue(Val);
3225 }
3226
3227 if (!ResultReg)
3228 return false;
3229
3230 ArgRegs.push_back(ResultReg);
3231 OutVTs.push_back(VT);
3232 }
3233
3234 // Analyze operands of the call, assigning locations to each operand.
3235 SmallVector<CCValAssign, 16> ArgLocs;
3236 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, CLI.RetTy->getContext());
3237
3238 // Allocate shadow area for Win64
3239 if (IsWin64)
3240 CCInfo.AllocateStack(32, 8);
3241
3242 CCInfo.AnalyzeCallOperands(OutVTs, OutFlags, CC_X86);
3243
3244 // Get a count of how many bytes are to be pushed on the stack.
Jeroen Ketema740f9d72015-09-29 10:12:57 +00003245 unsigned NumBytes = CCInfo.getAlignedCallFrameSize();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003246
3247 // Issue CALLSEQ_START
3248 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
3249 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Serge Pavlovd526b132017-05-09 13:35:13 +00003250 .addImm(NumBytes).addImm(0).addImm(0);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003251
3252 // Walk the register/memloc assignments, inserting copies/loads.
Eric Christophera1c535b2015-02-02 23:03:45 +00003253 const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003254 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3255 CCValAssign const &VA = ArgLocs[i];
3256 const Value *ArgVal = OutVals[VA.getValNo()];
3257 MVT ArgVT = OutVTs[VA.getValNo()];
3258
3259 if (ArgVT == MVT::x86mmx)
3260 return false;
3261
3262 unsigned ArgReg = ArgRegs[VA.getValNo()];
3263
3264 // Promote the value if needed.
3265 switch (VA.getLocInfo()) {
3266 case CCValAssign::Full: break;
3267 case CCValAssign::SExt: {
3268 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3269 "Unexpected extend");
David Majnemer2c5aeab2016-05-04 00:22:23 +00003270
Craig Topper088ba172016-12-05 06:09:55 +00003271 if (ArgVT == MVT::i1)
David Majnemer2c5aeab2016-05-04 00:22:23 +00003272 return false;
3273
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003274 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
3275 ArgVT, ArgReg);
3276 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
3277 ArgVT = VA.getLocVT();
3278 break;
3279 }
3280 case CCValAssign::ZExt: {
3281 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3282 "Unexpected extend");
David Majnemer2c5aeab2016-05-04 00:22:23 +00003283
3284 // Handle zero-extension from i1 to i8, which is common.
Craig Topper088ba172016-12-05 06:09:55 +00003285 if (ArgVT == MVT::i1) {
Craig Topper058f2f62017-03-28 16:35:29 +00003286 // In case SrcReg is a K register, COPY to a GPR
3287 if (MRI.getRegClass(ArgReg) == &X86::VK1RegClass) {
3288 unsigned KArgReg = ArgReg;
3289 ArgReg = createResultReg(&X86::GR32RegClass);
3290 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3291 TII.get(TargetOpcode::COPY), ArgReg)
3292 .addReg(KArgReg);
3293 ArgReg = fastEmitInst_extractsubreg(MVT::i8, ArgReg, /*Kill=*/true,
3294 X86::sub_8bit);
3295 }
David Majnemer2c5aeab2016-05-04 00:22:23 +00003296 // Set the high bits to zero.
3297 ArgReg = fastEmitZExtFromI1(MVT::i8, ArgReg, /*TODO: Kill=*/false);
3298 ArgVT = MVT::i8;
3299
3300 if (ArgReg == 0)
3301 return false;
3302 }
3303
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003304 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
3305 ArgVT, ArgReg);
3306 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
3307 ArgVT = VA.getLocVT();
3308 break;
3309 }
3310 case CCValAssign::AExt: {
3311 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3312 "Unexpected extend");
3313 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), ArgReg,
3314 ArgVT, ArgReg);
3315 if (!Emitted)
3316 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
3317 ArgVT, ArgReg);
3318 if (!Emitted)
3319 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
3320 ArgVT, ArgReg);
3321
3322 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
3323 ArgVT = VA.getLocVT();
3324 break;
3325 }
3326 case CCValAssign::BCvt: {
3327 ArgReg = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, ArgReg,
3328 /*TODO: Kill=*/false);
3329 assert(ArgReg && "Failed to emit a bitcast!");
3330 ArgVT = VA.getLocVT();
3331 break;
3332 }
3333 case CCValAssign::VExt:
3334 // VExt has not been implemented, so this should be impossible to reach
3335 // for now. However, fallback to Selection DAG isel once implemented.
3336 return false;
3337 case CCValAssign::AExtUpper:
3338 case CCValAssign::SExtUpper:
3339 case CCValAssign::ZExtUpper:
3340 case CCValAssign::FPExt:
3341 llvm_unreachable("Unexpected loc info!");
3342 case CCValAssign::Indirect:
3343 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
3344 // support this.
3345 return false;
3346 }
3347
3348 if (VA.isRegLoc()) {
3349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3350 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
3351 OutRegs.push_back(VA.getLocReg());
3352 } else {
3353 assert(VA.isMemLoc());
3354
3355 // Don't emit stores for undef values.
3356 if (isa<UndefValue>(ArgVal))
3357 continue;
3358
3359 unsigned LocMemOffset = VA.getLocMemOffset();
3360 X86AddressMode AM;
3361 AM.Base.Reg = RegInfo->getStackRegister();
3362 AM.Disp = LocMemOffset;
3363 ISD::ArgFlagsTy Flags = OutFlags[VA.getValNo()];
3364 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
3365 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00003366 MachinePointerInfo::getStack(*FuncInfo.MF, LocMemOffset),
3367 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003368 if (Flags.isByVal()) {
3369 X86AddressMode SrcAM;
3370 SrcAM.Base.Reg = ArgReg;
3371 if (!TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize()))
3372 return false;
3373 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
3374 // If this is a really simple value, emit this with the Value* version
3375 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
3376 // as it can cause us to reevaluate the argument.
3377 if (!X86FastEmitStore(ArgVT, ArgVal, AM, MMO))
3378 return false;
3379 } else {
3380 bool ValIsKill = hasTrivialKill(ArgVal);
3381 if (!X86FastEmitStore(ArgVT, ArgReg, ValIsKill, AM, MMO))
3382 return false;
3383 }
3384 }
3385 }
3386
3387 // ELF / PIC requires GOT in the EBX register before function calls via PLT
3388 // GOT pointer.
3389 if (Subtarget->isPICStyleGOT()) {
3390 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
3391 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3392 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
3393 }
3394
3395 if (Is64Bit && IsVarArg && !IsWin64) {
3396 // From AMD64 ABI document:
3397 // For calls that may call functions that use varargs or stdargs
3398 // (prototype-less calls or calls to functions containing ellipsis (...) in
3399 // the declaration) %al is used as hidden argument to specify the number
3400 // of SSE registers used. The contents of %al do not need to match exactly
3401 // the number of registers, but must be an ubound on the number of SSE
3402 // registers used and is in the range 0 - 8 inclusive.
3403
3404 // Count the number of XMM registers allocated.
3405 static const MCPhysReg XMMArgRegs[] = {
3406 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3407 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3408 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003409 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003410 assert((Subtarget->hasSSE1() || !NumXMMRegs)
3411 && "SSE registers cannot be used when SSE is disabled");
3412 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
3413 X86::AL).addImm(NumXMMRegs);
3414 }
3415
3416 // Materialize callee address in a register. FIXME: GV address can be
3417 // handled with a CALLpcrel32 instead.
3418 X86AddressMode CalleeAM;
3419 if (!X86SelectCallAddress(Callee, CalleeAM))
3420 return false;
3421
3422 unsigned CalleeOp = 0;
3423 const GlobalValue *GV = nullptr;
3424 if (CalleeAM.GV != nullptr) {
3425 GV = CalleeAM.GV;
3426 } else if (CalleeAM.Base.Reg != 0) {
3427 CalleeOp = CalleeAM.Base.Reg;
3428 } else
3429 return false;
3430
3431 // Issue the call.
3432 MachineInstrBuilder MIB;
3433 if (CalleeOp) {
3434 // Register-indirect call.
3435 unsigned CallOpc = Is64Bit ? X86::CALL64r : X86::CALL32r;
3436 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
3437 .addReg(CalleeOp);
3438 } else {
3439 // Direct call.
3440 assert(GV && "Not a direct call");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003441 // See if we need any target-specific flags on the GV operand.
Rafael Espindola46107b92016-05-19 18:49:29 +00003442 unsigned char OpFlags = Subtarget->classifyGlobalFunctionReference(GV);
Asaf Badouh89406d12016-04-20 08:32:57 +00003443 // Ignore NonLazyBind attribute in FastISel
3444 if (OpFlags == X86II::MO_GOTPCREL)
3445 OpFlags = 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003446
Reid Kleckner7662d502017-08-05 00:10:43 +00003447 // This will be a direct call, or an indirect call through memory for
3448 // NonLazyBind calls or dllimport calls.
3449 bool NeedLoad = OpFlags == X86II::MO_DLLIMPORT;
3450 unsigned CallOpc = NeedLoad
3451 ? (Is64Bit ? X86::CALL64m : X86::CALL32m)
3452 : (Is64Bit ? X86::CALL64pcrel32 : X86::CALLpcrel32);
3453
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003454 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Reid Kleckner7662d502017-08-05 00:10:43 +00003455 if (NeedLoad)
3456 MIB.addReg(Is64Bit ? X86::RIP : 0).addImm(1).addReg(0);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00003457 if (Symbol)
3458 MIB.addSym(Symbol, OpFlags);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003459 else
3460 MIB.addGlobalAddress(GV, 0, OpFlags);
Reid Kleckner7662d502017-08-05 00:10:43 +00003461 if (NeedLoad)
3462 MIB.addReg(0);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003463 }
3464
3465 // Add a register mask operand representing the call-preserved registers.
3466 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00003467 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003468
3469 // Add an implicit use GOT pointer in EBX.
3470 if (Subtarget->isPICStyleGOT())
3471 MIB.addReg(X86::EBX, RegState::Implicit);
3472
3473 if (Is64Bit && IsVarArg && !IsWin64)
3474 MIB.addReg(X86::AL, RegState::Implicit);
3475
3476 // Add implicit physical register uses to the call.
3477 for (auto Reg : OutRegs)
3478 MIB.addReg(Reg, RegState::Implicit);
3479
3480 // Issue CALLSEQ_END
3481 unsigned NumBytesForCalleeToPop =
Nico Weberaf7e8462016-07-14 01:52:51 +00003482 X86::isCalleePop(CC, Subtarget->is64Bit(), IsVarArg,
3483 TM.Options.GuaranteedTailCallOpt)
3484 ? NumBytes // Callee pops everything.
3485 : computeBytesPoppedByCalleeForSRet(Subtarget, CC, CLI.CS);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003486 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
3487 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
3488 .addImm(NumBytes).addImm(NumBytesForCalleeToPop);
3489
3490 // Now handle call return values.
3491 SmallVector<CCValAssign, 16> RVLocs;
3492 CCState CCRetInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs,
3493 CLI.RetTy->getContext());
3494 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
3495
3496 // Copy all of the result registers out of their specified physreg.
3497 unsigned ResultReg = FuncInfo.CreateRegs(CLI.RetTy);
3498 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3499 CCValAssign &VA = RVLocs[i];
3500 EVT CopyVT = VA.getValVT();
3501 unsigned CopyReg = ResultReg + i;
Craig Topper533b1bd2017-03-30 21:02:52 +00003502 unsigned SrcReg = VA.getLocReg();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003503
3504 // If this is x86-64, and we disabled SSE, we can't return FP values
3505 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
3506 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
3507 report_fatal_error("SSE register return with SSE disabled");
3508 }
3509
Craig Topper3001b352017-03-30 21:05:33 +00003510 // If the return value is an i1 and AVX-512 is enabled, we need
3511 // to do a fixup to make the copy legal.
Craig Topper533b1bd2017-03-30 21:02:52 +00003512 if (CopyVT == MVT::i1 && SrcReg == X86::AL && Subtarget->hasAVX512()) {
3513 // Need to copy to a GR32 first.
3514 // TODO: MOVZX isn't great here. We don't care about the upper bits.
3515 SrcReg = createResultReg(&X86::GR32RegClass);
3516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3517 TII.get(X86::MOVZX32rr8), SrcReg).addReg(X86::AL);
3518 }
3519
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003520 // If we prefer to use the value in xmm registers, copy it out as f80 and
3521 // use a truncate to move it from fp stack reg to xmm reg.
Craig Topper533b1bd2017-03-30 21:02:52 +00003522 if ((SrcReg == X86::FP0 || SrcReg == X86::FP1) &&
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003523 isScalarFPTypeInSSEReg(VA.getValVT())) {
3524 CopyVT = MVT::f80;
3525 CopyReg = createResultReg(&X86::RFP80RegClass);
3526 }
3527
3528 // Copy out the result.
3529 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Craig Topper533b1bd2017-03-30 21:02:52 +00003530 TII.get(TargetOpcode::COPY), CopyReg).addReg(SrcReg);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003531 InRegs.push_back(VA.getLocReg());
3532
3533 // Round the f80 to the right size, which also moves it to the appropriate
3534 // xmm register. This is accomplished by storing the f80 value in memory
3535 // and then loading it back.
3536 if (CopyVT != VA.getValVT()) {
3537 EVT ResVT = VA.getValVT();
3538 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
3539 unsigned MemSize = ResVT.getSizeInBits()/8;
3540 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
3541 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3542 TII.get(Opc)), FI)
3543 .addReg(CopyReg);
3544 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
3545 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3546 TII.get(Opc), ResultReg + i), FI);
3547 }
3548 }
3549
3550 CLI.ResultReg = ResultReg;
3551 CLI.NumResultRegs = RVLocs.size();
3552 CLI.Call = MIB;
3553
3554 return true;
3555}
3556
3557bool
3558X86FastISel::fastSelectInstruction(const Instruction *I) {
3559 switch (I->getOpcode()) {
3560 default: break;
3561 case Instruction::Load:
3562 return X86SelectLoad(I);
3563 case Instruction::Store:
3564 return X86SelectStore(I);
3565 case Instruction::Ret:
3566 return X86SelectRet(I);
3567 case Instruction::ICmp:
3568 case Instruction::FCmp:
3569 return X86SelectCmp(I);
3570 case Instruction::ZExt:
3571 return X86SelectZExt(I);
3572 case Instruction::Br:
3573 return X86SelectBranch(I);
3574 case Instruction::LShr:
3575 case Instruction::AShr:
3576 case Instruction::Shl:
3577 return X86SelectShift(I);
3578 case Instruction::SDiv:
3579 case Instruction::UDiv:
3580 case Instruction::SRem:
3581 case Instruction::URem:
3582 return X86SelectDivRem(I);
3583 case Instruction::Select:
3584 return X86SelectSelect(I);
3585 case Instruction::Trunc:
3586 return X86SelectTrunc(I);
3587 case Instruction::FPExt:
3588 return X86SelectFPExt(I);
3589 case Instruction::FPTrunc:
3590 return X86SelectFPTrunc(I);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00003591 case Instruction::SIToFP:
3592 return X86SelectSIToFP(I);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003593 case Instruction::IntToPtr: // Deliberate fall-through.
3594 case Instruction::PtrToInt: {
Mehdi Amini44ede332015-07-09 02:09:04 +00003595 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
3596 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003597 if (DstVT.bitsGT(SrcVT))
3598 return X86SelectZExt(I);
3599 if (DstVT.bitsLT(SrcVT))
3600 return X86SelectTrunc(I);
3601 unsigned Reg = getRegForValue(I->getOperand(0));
3602 if (Reg == 0) return false;
3603 updateValueMap(I, Reg);
3604 return true;
3605 }
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003606 case Instruction::BitCast: {
3607 // Select SSE2/AVX bitcasts between 128/256 bit vector types.
3608 if (!Subtarget->hasSSE2())
3609 return false;
3610
3611 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
3612 EVT DstVT = TLI.getValueType(DL, I->getType());
3613
3614 if (!SrcVT.isSimple() || !DstVT.isSimple())
3615 return false;
3616
Craig Topperdb8467a2016-12-05 05:50:51 +00003617 MVT SVT = SrcVT.getSimpleVT();
3618 MVT DVT = DstVT.getSimpleVT();
3619
3620 if (!SVT.is128BitVector() &&
3621 !(Subtarget->hasAVX() && SVT.is256BitVector()) &&
3622 !(Subtarget->hasAVX512() && SVT.is512BitVector() &&
3623 (Subtarget->hasBWI() || (SVT.getScalarSizeInBits() >= 32 &&
3624 DVT.getScalarSizeInBits() >= 32))))
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003625 return false;
3626
3627 unsigned Reg = getRegForValue(I->getOperand(0));
3628 if (Reg == 0)
3629 return false;
3630
3631 // No instruction is needed for conversion. Reuse the register used by
3632 // the fist operand.
3633 updateValueMap(I, Reg);
3634 return true;
3635 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003636 }
3637
3638 return false;
3639}
3640
3641unsigned X86FastISel::X86MaterializeInt(const ConstantInt *CI, MVT VT) {
3642 if (VT > MVT::i64)
3643 return 0;
3644
3645 uint64_t Imm = CI->getZExtValue();
3646 if (Imm == 0) {
3647 unsigned SrcReg = fastEmitInst_(X86::MOV32r0, &X86::GR32RegClass);
3648 switch (VT.SimpleTy) {
3649 default: llvm_unreachable("Unexpected value type");
3650 case MVT::i1:
3651 case MVT::i8:
3652 return fastEmitInst_extractsubreg(MVT::i8, SrcReg, /*Kill=*/true,
3653 X86::sub_8bit);
3654 case MVT::i16:
3655 return fastEmitInst_extractsubreg(MVT::i16, SrcReg, /*Kill=*/true,
3656 X86::sub_16bit);
3657 case MVT::i32:
3658 return SrcReg;
3659 case MVT::i64: {
3660 unsigned ResultReg = createResultReg(&X86::GR64RegClass);
3661 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3662 TII.get(TargetOpcode::SUBREG_TO_REG), ResultReg)
3663 .addImm(0).addReg(SrcReg).addImm(X86::sub_32bit);
3664 return ResultReg;
3665 }
3666 }
3667 }
3668
3669 unsigned Opc = 0;
3670 switch (VT.SimpleTy) {
3671 default: llvm_unreachable("Unexpected value type");
Craig Topper058f2f62017-03-28 16:35:29 +00003672 case MVT::i1:
3673 // TODO: Support this properly.
3674 if (Subtarget->hasAVX512())
3675 return 0;
3676 VT = MVT::i8;
3677 LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003678 case MVT::i8: Opc = X86::MOV8ri; break;
3679 case MVT::i16: Opc = X86::MOV16ri; break;
3680 case MVT::i32: Opc = X86::MOV32ri; break;
3681 case MVT::i64: {
3682 if (isUInt<32>(Imm))
3683 Opc = X86::MOV32ri;
3684 else if (isInt<32>(Imm))
3685 Opc = X86::MOV64ri32;
3686 else
3687 Opc = X86::MOV64ri;
3688 break;
3689 }
3690 }
3691 if (VT == MVT::i64 && Opc == X86::MOV32ri) {
3692 unsigned SrcReg = fastEmitInst_i(Opc, &X86::GR32RegClass, Imm);
3693 unsigned ResultReg = createResultReg(&X86::GR64RegClass);
3694 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3695 TII.get(TargetOpcode::SUBREG_TO_REG), ResultReg)
3696 .addImm(0).addReg(SrcReg).addImm(X86::sub_32bit);
3697 return ResultReg;
3698 }
3699 return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
3700}
3701
3702unsigned X86FastISel::X86MaterializeFP(const ConstantFP *CFP, MVT VT) {
3703 if (CFP->isNullValue())
3704 return fastMaterializeFloatZero(CFP);
3705
3706 // Can't handle alternate code models yet.
3707 CodeModel::Model CM = TM.getCodeModel();
3708 if (CM != CodeModel::Small && CM != CodeModel::Large)
3709 return 0;
3710
3711 // Get opcode and regclass of the output for the given load instruction.
3712 unsigned Opc = 0;
3713 const TargetRegisterClass *RC = nullptr;
3714 switch (VT.SimpleTy) {
3715 default: return 0;
3716 case MVT::f32:
3717 if (X86ScalarSSEf32) {
3718 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
3719 RC = &X86::FR32RegClass;
3720 } else {
3721 Opc = X86::LD_Fp32m;
3722 RC = &X86::RFP32RegClass;
3723 }
3724 break;
3725 case MVT::f64:
3726 if (X86ScalarSSEf64) {
3727 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
3728 RC = &X86::FR64RegClass;
3729 } else {
3730 Opc = X86::LD_Fp64m;
3731 RC = &X86::RFP64RegClass;
3732 }
3733 break;
3734 case MVT::f80:
3735 // No f80 support yet.
3736 return 0;
3737 }
3738
3739 // MachineConstantPool wants an explicit alignment.
3740 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
3741 if (Align == 0) {
3742 // Alignment of vector types. FIXME!
3743 Align = DL.getTypeAllocSize(CFP->getType());
3744 }
3745
3746 // x86-32 PIC requires a PIC base register for constant pools.
3747 unsigned PICBase = 0;
Rafael Espindolac7e98132016-05-20 12:20:10 +00003748 unsigned char OpFlag = Subtarget->classifyLocalReference(nullptr);
3749 if (OpFlag == X86II::MO_PIC_BASE_OFFSET)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003750 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolac7e98132016-05-20 12:20:10 +00003751 else if (OpFlag == X86II::MO_GOTOFF)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003752 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolac7e98132016-05-20 12:20:10 +00003753 else if (Subtarget->is64Bit() && TM.getCodeModel() == CodeModel::Small)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003754 PICBase = X86::RIP;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003755
3756 // Create the load from the constant pool.
3757 unsigned CPI = MCP.getConstantPoolIndex(CFP, Align);
3758 unsigned ResultReg = createResultReg(RC);
3759
3760 if (CM == CodeModel::Large) {
3761 unsigned AddrReg = createResultReg(&X86::GR64RegClass);
3762 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri),
3763 AddrReg)
3764 .addConstantPoolIndex(CPI, 0, OpFlag);
3765 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3766 TII.get(Opc), ResultReg);
3767 addDirectMem(MIB, AddrReg);
3768 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00003769 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
3770 MachineMemOperand::MOLoad, DL.getPointerSize(), Align);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003771 MIB->addMemOperand(*FuncInfo.MF, MMO);
3772 return ResultReg;
3773 }
3774
3775 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3776 TII.get(Opc), ResultReg),
3777 CPI, PICBase, OpFlag);
3778 return ResultReg;
3779}
3780
3781unsigned X86FastISel::X86MaterializeGV(const GlobalValue *GV, MVT VT) {
3782 // Can't handle alternate code models yet.
3783 if (TM.getCodeModel() != CodeModel::Small)
3784 return 0;
3785
3786 // Materialize addresses with LEA/MOV instructions.
3787 X86AddressMode AM;
3788 if (X86SelectAddress(GV, AM)) {
3789 // If the expression is just a basereg, then we're done, otherwise we need
3790 // to emit an LEA.
3791 if (AM.BaseType == X86AddressMode::RegBase &&
3792 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
3793 return AM.Base.Reg;
3794
3795 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3796 if (TM.getRelocationModel() == Reloc::Static &&
Mehdi Amini44ede332015-07-09 02:09:04 +00003797 TLI.getPointerTy(DL) == MVT::i64) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003798 // The displacement code could be more than 32 bits away so we need to use
3799 // an instruction with a 64 bit immediate
3800 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri),
3801 ResultReg)
3802 .addGlobalAddress(GV);
3803 } else {
Mehdi Amini44ede332015-07-09 02:09:04 +00003804 unsigned Opc =
3805 TLI.getPointerTy(DL) == MVT::i32
3806 ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r)
3807 : X86::LEA64r;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003808 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3809 TII.get(Opc), ResultReg), AM);
3810 }
3811 return ResultReg;
3812 }
3813 return 0;
3814}
3815
3816unsigned X86FastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003817 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003818
3819 // Only handle simple types.
3820 if (!CEVT.isSimple())
3821 return 0;
3822 MVT VT = CEVT.getSimpleVT();
3823
3824 if (const auto *CI = dyn_cast<ConstantInt>(C))
3825 return X86MaterializeInt(CI, VT);
3826 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
3827 return X86MaterializeFP(CFP, VT);
3828 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
3829 return X86MaterializeGV(GV, VT);
3830
3831 return 0;
3832}
3833
3834unsigned X86FastISel::fastMaterializeAlloca(const AllocaInst *C) {
3835 // Fail on dynamic allocas. At this point, getRegForValue has already
3836 // checked its CSE maps, so if we're here trying to handle a dynamic
3837 // alloca, we're not going to succeed. X86SelectAddress has a
3838 // check for dynamic allocas, because it's called directly from
3839 // various places, but targetMaterializeAlloca also needs a check
3840 // in order to avoid recursion between getRegForValue,
3841 // X86SelectAddrss, and targetMaterializeAlloca.
3842 if (!FuncInfo.StaticAllocaMap.count(C))
3843 return 0;
3844 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
3845
3846 X86AddressMode AM;
3847 if (!X86SelectAddress(C, AM))
3848 return 0;
Mehdi Amini44ede332015-07-09 02:09:04 +00003849 unsigned Opc =
3850 TLI.getPointerTy(DL) == MVT::i32
3851 ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r)
3852 : X86::LEA64r;
3853 const TargetRegisterClass *RC = TLI.getRegClassFor(TLI.getPointerTy(DL));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003854 unsigned ResultReg = createResultReg(RC);
3855 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3856 TII.get(Opc), ResultReg), AM);
3857 return ResultReg;
3858}
3859
3860unsigned X86FastISel::fastMaterializeFloatZero(const ConstantFP *CF) {
3861 MVT VT;
3862 if (!isTypeLegal(CF->getType(), VT))
3863 return 0;
3864
3865 // Get opcode and regclass for the given zero.
3866 unsigned Opc = 0;
3867 const TargetRegisterClass *RC = nullptr;
3868 switch (VT.SimpleTy) {
3869 default: return 0;
3870 case MVT::f32:
3871 if (X86ScalarSSEf32) {
3872 Opc = X86::FsFLD0SS;
3873 RC = &X86::FR32RegClass;
3874 } else {
3875 Opc = X86::LD_Fp032;
3876 RC = &X86::RFP32RegClass;
3877 }
3878 break;
3879 case MVT::f64:
3880 if (X86ScalarSSEf64) {
3881 Opc = X86::FsFLD0SD;
3882 RC = &X86::FR64RegClass;
3883 } else {
3884 Opc = X86::LD_Fp064;
3885 RC = &X86::RFP64RegClass;
3886 }
3887 break;
3888 case MVT::f80:
3889 // No f80 support yet.
3890 return 0;
3891 }
3892
3893 unsigned ResultReg = createResultReg(RC);
3894 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
3895 return ResultReg;
3896}
3897
3898
3899bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3900 const LoadInst *LI) {
3901 const Value *Ptr = LI->getPointerOperand();
3902 X86AddressMode AM;
3903 if (!X86SelectAddress(Ptr, AM))
3904 return false;
3905
3906 const X86InstrInfo &XII = (const X86InstrInfo &)TII;
3907
3908 unsigned Size = DL.getTypeAllocSize(LI->getType());
3909 unsigned Alignment = LI->getAlignment();
3910
3911 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3912 Alignment = DL.getABITypeAlignment(LI->getType());
3913
3914 SmallVector<MachineOperand, 8> AddrOps;
3915 AM.getFullAddress(AddrOps);
3916
Keno Fischere70b31f2015-06-08 20:09:58 +00003917 MachineInstr *Result = XII.foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003918 *FuncInfo.MF, *MI, OpNo, AddrOps, FuncInfo.InsertPt, Size, Alignment,
Keno Fischere70b31f2015-06-08 20:09:58 +00003919 /*AllowCommute=*/true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003920 if (!Result)
3921 return false;
3922
Pete Cooperd31583d2015-05-06 21:37:19 +00003923 // The index register could be in the wrong register class. Unfortunately,
3924 // foldMemoryOperandImpl could have commuted the instruction so its not enough
3925 // to just look at OpNo + the offset to the index reg. We actually need to
3926 // scan the instruction to find the index reg and see if its the correct reg
3927 // class.
Matthias Braune41e1462015-05-29 02:56:46 +00003928 unsigned OperandNo = 0;
3929 for (MachineInstr::mop_iterator I = Result->operands_begin(),
3930 E = Result->operands_end(); I != E; ++I, ++OperandNo) {
3931 MachineOperand &MO = *I;
3932 if (!MO.isReg() || MO.isDef() || MO.getReg() != AM.IndexReg)
Pete Cooperd31583d2015-05-06 21:37:19 +00003933 continue;
3934 // Found the index reg, now try to rewrite it.
Pete Cooperd31583d2015-05-06 21:37:19 +00003935 unsigned IndexReg = constrainOperandRegClass(Result->getDesc(),
Matthias Braune41e1462015-05-29 02:56:46 +00003936 MO.getReg(), OperandNo);
3937 if (IndexReg == MO.getReg())
Pete Cooperd31583d2015-05-06 21:37:19 +00003938 continue;
Matthias Braune41e1462015-05-29 02:56:46 +00003939 MO.setReg(IndexReg);
Pete Cooperd31583d2015-05-06 21:37:19 +00003940 }
3941
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003942 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003943 MI->eraseFromParent();
3944 return true;
3945}
3946
Craig Topper7ef6ea32016-12-05 04:51:31 +00003947unsigned X86FastISel::fastEmitInst_rrrr(unsigned MachineInstOpcode,
3948 const TargetRegisterClass *RC,
3949 unsigned Op0, bool Op0IsKill,
3950 unsigned Op1, bool Op1IsKill,
3951 unsigned Op2, bool Op2IsKill,
3952 unsigned Op3, bool Op3IsKill) {
3953 const MCInstrDesc &II = TII.get(MachineInstOpcode);
3954
3955 unsigned ResultReg = createResultReg(RC);
3956 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
3957 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
3958 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
3959 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 3);
3960
3961 if (II.getNumDefs() >= 1)
3962 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
3963 .addReg(Op0, getKillRegState(Op0IsKill))
3964 .addReg(Op1, getKillRegState(Op1IsKill))
3965 .addReg(Op2, getKillRegState(Op2IsKill))
3966 .addReg(Op3, getKillRegState(Op3IsKill));
3967 else {
3968 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
3969 .addReg(Op0, getKillRegState(Op0IsKill))
3970 .addReg(Op1, getKillRegState(Op1IsKill))
3971 .addReg(Op2, getKillRegState(Op2IsKill))
3972 .addReg(Op3, getKillRegState(Op3IsKill));
3973 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3974 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
3975 }
3976 return ResultReg;
3977}
3978
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003979
3980namespace llvm {
3981 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
3982 const TargetLibraryInfo *libInfo) {
3983 return new X86FastISel(funcInfo, libInfo);
3984 }
3985}