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