blob: 015306db6b46914971dbd2864032d0130260208b [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
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00002152 const TargetRegisterInfo &TRI = *Subtarget->getRegisterInfo();
2153 unsigned Opc = X86::getCMovFromCond(CC, TRI.getRegSizeInBits(*RC)/8);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002154 unsigned ResultReg = fastEmitInst_rr(Opc, RC, RHSReg, RHSIsKill,
2155 LHSReg, LHSIsKill);
2156 updateValueMap(I, ResultReg);
2157 return true;
2158}
2159
Sanjay Patel302404b2015-03-05 21:46:54 +00002160/// \brief Emit SSE or AVX instructions to lower the select.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002161///
2162/// Try to use SSE1/SSE2 instructions to simulate a select without branches.
2163/// This lowers fp selects into a CMP/AND/ANDN/OR sequence when the necessary
Sanjay Patel302404b2015-03-05 21:46:54 +00002164/// SSE instructions are available. If AVX is available, try to use a VBLENDV.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002165bool X86FastISel::X86FastEmitSSESelect(MVT RetVT, const Instruction *I) {
2166 // Optimize conditions coming from a compare if both instructions are in the
2167 // same basic block (values defined in other basic blocks may not have
2168 // initialized registers).
2169 const auto *CI = dyn_cast<FCmpInst>(I->getOperand(0));
2170 if (!CI || (CI->getParent() != I->getParent()))
2171 return false;
2172
2173 if (I->getType() != CI->getOperand(0)->getType() ||
2174 !((Subtarget->hasSSE1() && RetVT == MVT::f32) ||
2175 (Subtarget->hasSSE2() && RetVT == MVT::f64)))
2176 return false;
2177
2178 const Value *CmpLHS = CI->getOperand(0);
2179 const Value *CmpRHS = CI->getOperand(1);
2180 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2181
2182 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
2183 // We don't have to materialize a zero constant for this case and can just use
2184 // %x again on the RHS.
2185 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
2186 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
2187 if (CmpRHSC && CmpRHSC->isNullValue())
2188 CmpRHS = CmpLHS;
2189 }
2190
2191 unsigned CC;
2192 bool NeedSwap;
2193 std::tie(CC, NeedSwap) = getX86SSEConditionCode(Predicate);
2194 if (CC > 7)
2195 return false;
2196
2197 if (NeedSwap)
2198 std::swap(CmpLHS, CmpRHS);
2199
Sanjay Patel302404b2015-03-05 21:46:54 +00002200 // Choose the SSE instruction sequence based on data type (float or double).
Craig Topper428169a2016-09-05 07:14:21 +00002201 static const uint16_t OpcTable[2][4] = {
Craig Topper6413f8a2016-12-06 04:58:39 +00002202 { X86::CMPSSrr, X86::ANDPSrr, X86::ANDNPSrr, X86::ORPSrr },
2203 { X86::CMPSDrr, X86::ANDPDrr, X86::ANDNPDrr, X86::ORPDrr }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002204 };
2205
Craig Topper428169a2016-09-05 07:14:21 +00002206 const uint16_t *Opc = nullptr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002207 switch (RetVT.SimpleTy) {
2208 default: return false;
Sanjay Patel302404b2015-03-05 21:46:54 +00002209 case MVT::f32: Opc = &OpcTable[0][0]; break;
2210 case MVT::f64: Opc = &OpcTable[1][0]; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002211 }
2212
2213 const Value *LHS = I->getOperand(1);
2214 const Value *RHS = I->getOperand(2);
2215
2216 unsigned LHSReg = getRegForValue(LHS);
2217 bool LHSIsKill = hasTrivialKill(LHS);
2218
2219 unsigned RHSReg = getRegForValue(RHS);
2220 bool RHSIsKill = hasTrivialKill(RHS);
2221
2222 unsigned CmpLHSReg = getRegForValue(CmpLHS);
2223 bool CmpLHSIsKill = hasTrivialKill(CmpLHS);
2224
2225 unsigned CmpRHSReg = getRegForValue(CmpRHS);
2226 bool CmpRHSIsKill = hasTrivialKill(CmpRHS);
2227
2228 if (!LHSReg || !RHSReg || !CmpLHS || !CmpRHS)
2229 return false;
2230
2231 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
Sanjay Patel302404b2015-03-05 21:46:54 +00002232 unsigned ResultReg;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002233
2234 if (Subtarget->hasAVX512()) {
2235 // If we have AVX512 we can use a mask compare and masked movss/sd.
2236 const TargetRegisterClass *VR128X = &X86::VR128XRegClass;
2237 const TargetRegisterClass *VK1 = &X86::VK1RegClass;
2238
2239 unsigned CmpOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002240 (RetVT == MVT::f32) ? X86::VCMPSSZrr : X86::VCMPSDZrr;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002241 unsigned CmpReg = fastEmitInst_rri(CmpOpcode, VK1, CmpLHSReg, CmpLHSIsKill,
2242 CmpRHSReg, CmpRHSIsKill, CC);
2243
2244 // Need an IMPLICIT_DEF for the input that is used to generate the upper
2245 // bits of the result register since its not based on any of the inputs.
2246 unsigned ImplicitDefReg = createResultReg(VR128X);
2247 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2248 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2249
2250 // Place RHSReg is the passthru of the masked movss/sd operation and put
2251 // LHS in the input. The mask input comes from the compare.
2252 unsigned MovOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002253 (RetVT == MVT::f32) ? X86::VMOVSSZrrk : X86::VMOVSDZrrk;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002254 unsigned MovReg = fastEmitInst_rrrr(MovOpcode, VR128X, RHSReg, RHSIsKill,
2255 CmpReg, true, ImplicitDefReg, true,
2256 LHSReg, LHSIsKill);
2257
2258 ResultReg = createResultReg(RC);
2259 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2260 TII.get(TargetOpcode::COPY), ResultReg).addReg(MovReg);
2261
2262 } else if (Subtarget->hasAVX()) {
Matthias Braun818c78d2015-08-31 18:25:11 +00002263 const TargetRegisterClass *VR128 = &X86::VR128RegClass;
2264
Sanjay Patel302404b2015-03-05 21:46:54 +00002265 // If we have AVX, create 1 blendv instead of 3 logic instructions.
2266 // Blendv was introduced with SSE 4.1, but the 2 register form implicitly
2267 // uses XMM0 as the selection register. That may need just as many
2268 // instructions as the AND/ANDN/OR sequence due to register moves, so
2269 // don't bother.
2270 unsigned CmpOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002271 (RetVT == MVT::f32) ? X86::VCMPSSrr : X86::VCMPSDrr;
Sanjay Patel302404b2015-03-05 21:46:54 +00002272 unsigned BlendOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002273 (RetVT == MVT::f32) ? X86::VBLENDVPSrr : X86::VBLENDVPDrr;
2274
Craig Topper7ef6ea32016-12-05 04:51:31 +00002275 unsigned CmpReg = fastEmitInst_rri(CmpOpcode, RC, CmpLHSReg, CmpLHSIsKill,
Sanjay Patel302404b2015-03-05 21:46:54 +00002276 CmpRHSReg, CmpRHSIsKill, CC);
Matthias Braun818c78d2015-08-31 18:25:11 +00002277 unsigned VBlendReg = fastEmitInst_rrr(BlendOpcode, VR128, RHSReg, RHSIsKill,
2278 LHSReg, LHSIsKill, CmpReg, true);
2279 ResultReg = createResultReg(RC);
2280 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2281 TII.get(TargetOpcode::COPY), ResultReg).addReg(VBlendReg);
Sanjay Patel302404b2015-03-05 21:46:54 +00002282 } else {
Craig Topper6413f8a2016-12-06 04:58:39 +00002283 const TargetRegisterClass *VR128 = &X86::VR128RegClass;
Sanjay Patel302404b2015-03-05 21:46:54 +00002284 unsigned CmpReg = fastEmitInst_rri(Opc[0], RC, CmpLHSReg, CmpLHSIsKill,
2285 CmpRHSReg, CmpRHSIsKill, CC);
Craig Topper6413f8a2016-12-06 04:58:39 +00002286 unsigned AndReg = fastEmitInst_rr(Opc[1], VR128, CmpReg, /*IsKill=*/false,
Sanjay Patel302404b2015-03-05 21:46:54 +00002287 LHSReg, LHSIsKill);
Craig Topper6413f8a2016-12-06 04:58:39 +00002288 unsigned AndNReg = fastEmitInst_rr(Opc[2], VR128, CmpReg, /*IsKill=*/true,
Sanjay Patel302404b2015-03-05 21:46:54 +00002289 RHSReg, RHSIsKill);
Craig Topper6413f8a2016-12-06 04:58:39 +00002290 unsigned OrReg = fastEmitInst_rr(Opc[3], VR128, AndNReg, /*IsKill=*/true,
2291 AndReg, /*IsKill=*/true);
2292 ResultReg = createResultReg(RC);
2293 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2294 TII.get(TargetOpcode::COPY), ResultReg).addReg(OrReg);
Sanjay Patel302404b2015-03-05 21:46:54 +00002295 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002296 updateValueMap(I, ResultReg);
2297 return true;
2298}
2299
2300bool X86FastISel::X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I) {
2301 // These are pseudo CMOV instructions and will be later expanded into control-
2302 // flow.
2303 unsigned Opc;
2304 switch (RetVT.SimpleTy) {
2305 default: return false;
2306 case MVT::i8: Opc = X86::CMOV_GR8; break;
2307 case MVT::i16: Opc = X86::CMOV_GR16; break;
2308 case MVT::i32: Opc = X86::CMOV_GR32; break;
2309 case MVT::f32: Opc = X86::CMOV_FR32; break;
2310 case MVT::f64: Opc = X86::CMOV_FR64; break;
2311 }
2312
2313 const Value *Cond = I->getOperand(0);
2314 X86::CondCode CC = X86::COND_NE;
2315
2316 // Optimize conditions coming from a compare if both instructions are in the
2317 // same basic block (values defined in other basic blocks may not have
2318 // initialized registers).
2319 const auto *CI = dyn_cast<CmpInst>(Cond);
2320 if (CI && (CI->getParent() == I->getParent())) {
2321 bool NeedSwap;
2322 std::tie(CC, NeedSwap) = getX86ConditionCode(CI->getPredicate());
2323 if (CC > X86::LAST_VALID_COND)
2324 return false;
2325
2326 const Value *CmpLHS = CI->getOperand(0);
2327 const Value *CmpRHS = CI->getOperand(1);
2328
2329 if (NeedSwap)
2330 std::swap(CmpLHS, CmpRHS);
2331
Mehdi Amini44ede332015-07-09 02:09:04 +00002332 EVT CmpVT = TLI.getValueType(DL, CmpLHS->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002333 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT, CI->getDebugLoc()))
2334 return false;
2335 } else {
2336 unsigned CondReg = getRegForValue(Cond);
2337 if (CondReg == 0)
2338 return false;
2339 bool CondIsKill = hasTrivialKill(Cond);
Guy Blank9ae797a2016-08-21 08:02:27 +00002340
Guy Blank2bdc74a2016-09-28 11:22:17 +00002341 // In case OpReg is a K register, COPY to a GPR
2342 if (MRI.getRegClass(CondReg) == &X86::VK1RegClass) {
2343 unsigned KCondReg = CondReg;
Craig Topper058f2f62017-03-28 16:35:29 +00002344 CondReg = createResultReg(&X86::GR32RegClass);
Guy Blank9ae797a2016-08-21 08:02:27 +00002345 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Guy Blank2bdc74a2016-09-28 11:22:17 +00002346 TII.get(TargetOpcode::COPY), CondReg)
2347 .addReg(KCondReg, getKillRegState(CondIsKill));
Craig Topper058f2f62017-03-28 16:35:29 +00002348 CondReg = fastEmitInst_extractsubreg(MVT::i8, CondReg, /*Kill=*/true,
2349 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00002350 }
2351 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
2352 .addReg(CondReg, getKillRegState(CondIsKill))
2353 .addImm(1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002354 }
2355
2356 const Value *LHS = I->getOperand(1);
2357 const Value *RHS = I->getOperand(2);
2358
2359 unsigned LHSReg = getRegForValue(LHS);
2360 bool LHSIsKill = hasTrivialKill(LHS);
2361
2362 unsigned RHSReg = getRegForValue(RHS);
2363 bool RHSIsKill = hasTrivialKill(RHS);
2364
2365 if (!LHSReg || !RHSReg)
2366 return false;
2367
2368 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2369
2370 unsigned ResultReg =
2371 fastEmitInst_rri(Opc, RC, RHSReg, RHSIsKill, LHSReg, LHSIsKill, CC);
2372 updateValueMap(I, ResultReg);
2373 return true;
2374}
2375
2376bool X86FastISel::X86SelectSelect(const Instruction *I) {
2377 MVT RetVT;
2378 if (!isTypeLegal(I->getType(), RetVT))
2379 return false;
2380
2381 // Check if we can fold the select.
2382 if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) {
2383 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2384 const Value *Opnd = nullptr;
2385 switch (Predicate) {
2386 default: break;
2387 case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break;
2388 case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break;
2389 }
2390 // No need for a select anymore - this is an unconditional move.
2391 if (Opnd) {
2392 unsigned OpReg = getRegForValue(Opnd);
2393 if (OpReg == 0)
2394 return false;
2395 bool OpIsKill = hasTrivialKill(Opnd);
2396 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2397 unsigned ResultReg = createResultReg(RC);
2398 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2399 TII.get(TargetOpcode::COPY), ResultReg)
2400 .addReg(OpReg, getKillRegState(OpIsKill));
2401 updateValueMap(I, ResultReg);
2402 return true;
2403 }
2404 }
2405
2406 // First try to use real conditional move instructions.
2407 if (X86FastEmitCMoveSelect(RetVT, I))
2408 return true;
2409
2410 // Try to use a sequence of SSE instructions to simulate a conditional move.
2411 if (X86FastEmitSSESelect(RetVT, I))
2412 return true;
2413
2414 // Fall-back to pseudo conditional move instructions, which will be later
2415 // converted to control-flow.
2416 if (X86FastEmitPseudoSelect(RetVT, I))
2417 return true;
2418
2419 return false;
2420}
2421
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002422bool X86FastISel::X86SelectSIToFP(const Instruction *I) {
Andrea Di Biagio98c36702015-04-20 11:56:59 +00002423 // The target-independent selection algorithm in FastISel already knows how
2424 // to select a SINT_TO_FP if the target is SSE but not AVX.
2425 // Early exit if the subtarget doesn't have AVX.
2426 if (!Subtarget->hasAVX())
2427 return false;
2428
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002429 if (!I->getOperand(0)->getType()->isIntegerTy(32))
2430 return false;
2431
2432 // Select integer to float/double conversion.
2433 unsigned OpReg = getRegForValue(I->getOperand(0));
2434 if (OpReg == 0)
2435 return false;
2436
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002437 const TargetRegisterClass *RC = nullptr;
2438 unsigned Opcode;
2439
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002440 if (I->getType()->isDoubleTy()) {
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002441 // sitofp int -> double
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002442 Opcode = X86::VCVTSI2SDrr;
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002443 RC = &X86::FR64RegClass;
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002444 } else if (I->getType()->isFloatTy()) {
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002445 // sitofp int -> float
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002446 Opcode = X86::VCVTSI2SSrr;
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002447 RC = &X86::FR32RegClass;
2448 } else
2449 return false;
2450
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002451 unsigned ImplicitDefReg = createResultReg(RC);
2452 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2453 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2454 unsigned ResultReg =
2455 fastEmitInst_rr(Opcode, RC, ImplicitDefReg, true, OpReg, false);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002456 updateValueMap(I, ResultReg);
2457 return true;
2458}
2459
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002460// Helper method used by X86SelectFPExt and X86SelectFPTrunc.
2461bool X86FastISel::X86SelectFPExtOrFPTrunc(const Instruction *I,
2462 unsigned TargetOpc,
2463 const TargetRegisterClass *RC) {
2464 assert((I->getOpcode() == Instruction::FPExt ||
2465 I->getOpcode() == Instruction::FPTrunc) &&
2466 "Instruction must be an FPExt or FPTrunc!");
2467
2468 unsigned OpReg = getRegForValue(I->getOperand(0));
2469 if (OpReg == 0)
2470 return false;
2471
Ayman Musa9b802e42017-03-01 10:20:48 +00002472 unsigned ImplicitDefReg;
2473 if (Subtarget->hasAVX()) {
2474 ImplicitDefReg = createResultReg(RC);
2475 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2476 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2477
2478 }
2479
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002480 unsigned ResultReg = createResultReg(RC);
2481 MachineInstrBuilder MIB;
2482 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpc),
2483 ResultReg);
Ayman Musa4b2c9682017-02-23 13:15:44 +00002484
Ayman Musa9b802e42017-03-01 10:20:48 +00002485 if (Subtarget->hasAVX())
Ayman Musa4b2c9682017-02-23 13:15:44 +00002486 MIB.addReg(ImplicitDefReg);
Ayman Musa9b802e42017-03-01 10:20:48 +00002487
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002488 MIB.addReg(OpReg);
2489 updateValueMap(I, ResultReg);
2490 return true;
2491}
2492
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002493bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002494 if (X86ScalarSSEf64 && I->getType()->isDoubleTy() &&
2495 I->getOperand(0)->getType()->isFloatTy()) {
2496 // fpext from float to double.
2497 unsigned Opc = Subtarget->hasAVX() ? X86::VCVTSS2SDrr : X86::CVTSS2SDrr;
2498 return X86SelectFPExtOrFPTrunc(I, Opc, &X86::FR64RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002499 }
2500
2501 return false;
2502}
2503
2504bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002505 if (X86ScalarSSEf64 && I->getType()->isFloatTy() &&
2506 I->getOperand(0)->getType()->isDoubleTy()) {
2507 // fptrunc from double to float.
2508 unsigned Opc = Subtarget->hasAVX() ? X86::VCVTSD2SSrr : X86::CVTSD2SSrr;
2509 return X86SelectFPExtOrFPTrunc(I, Opc, &X86::FR32RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002510 }
2511
2512 return false;
2513}
2514
2515bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002516 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
2517 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002518
2519 // This code only handles truncation to byte.
Craig Topper331297c2017-03-28 23:20:37 +00002520 // TODO: Support truncate to i1 with AVX512.
2521 if (DstVT != MVT::i8 && (DstVT != MVT::i1 || Subtarget->hasAVX512()))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002522 return false;
2523 if (!TLI.isTypeLegal(SrcVT))
2524 return false;
2525
2526 unsigned InputReg = getRegForValue(I->getOperand(0));
2527 if (!InputReg)
2528 // Unhandled operand. Halt "fast" selection and bail.
2529 return false;
2530
2531 if (SrcVT == MVT::i8) {
2532 // Truncate from i8 to i1; no code needed.
2533 updateValueMap(I, InputReg);
2534 return true;
2535 }
2536
Pete Cooper7f7c9f12015-05-08 18:29:42 +00002537 bool KillInputReg = false;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002538 if (!Subtarget->is64Bit()) {
2539 // If we're on x86-32; we can't extract an i8 from a general register.
2540 // First issue a copy to GR16_ABCD or GR32_ABCD.
2541 const TargetRegisterClass *CopyRC =
2542 (SrcVT == MVT::i16) ? &X86::GR16_ABCDRegClass : &X86::GR32_ABCDRegClass;
2543 unsigned CopyReg = createResultReg(CopyRC);
2544 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2545 TII.get(TargetOpcode::COPY), CopyReg).addReg(InputReg);
2546 InputReg = CopyReg;
Pete Cooper7f7c9f12015-05-08 18:29:42 +00002547 KillInputReg = true;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002548 }
2549
2550 // Issue an extract_subreg.
2551 unsigned ResultReg = fastEmitInst_extractsubreg(MVT::i8,
Pete Cooper7f7c9f12015-05-08 18:29:42 +00002552 InputReg, KillInputReg,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002553 X86::sub_8bit);
2554 if (!ResultReg)
2555 return false;
2556
2557 updateValueMap(I, ResultReg);
2558 return true;
2559}
2560
2561bool X86FastISel::IsMemcpySmall(uint64_t Len) {
2562 return Len <= (Subtarget->is64Bit() ? 32 : 16);
2563}
2564
2565bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
2566 X86AddressMode SrcAM, uint64_t Len) {
2567
2568 // Make sure we don't bloat code by inlining very large memcpy's.
2569 if (!IsMemcpySmall(Len))
2570 return false;
2571
2572 bool i64Legal = Subtarget->is64Bit();
2573
2574 // We don't care about alignment here since we just emit integer accesses.
2575 while (Len) {
2576 MVT VT;
2577 if (Len >= 8 && i64Legal)
2578 VT = MVT::i64;
2579 else if (Len >= 4)
2580 VT = MVT::i32;
2581 else if (Len >= 2)
2582 VT = MVT::i16;
2583 else
2584 VT = MVT::i8;
2585
2586 unsigned Reg;
2587 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
2588 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
2589 assert(RV && "Failed to emit load or store??");
2590
2591 unsigned Size = VT.getSizeInBits()/8;
2592 Len -= Size;
2593 DestAM.Disp += Size;
2594 SrcAM.Disp += Size;
2595 }
2596
2597 return true;
2598}
2599
2600bool X86FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
2601 // FIXME: Handle more intrinsics.
2602 switch (II->getIntrinsicID()) {
2603 default: return false;
Andrea Di Biagio70351782015-02-20 19:37:14 +00002604 case Intrinsic::convert_from_fp16:
2605 case Intrinsic::convert_to_fp16: {
Eric Christopher824f42f2015-05-12 01:26:05 +00002606 if (Subtarget->useSoftFloat() || !Subtarget->hasF16C())
Andrea Di Biagio70351782015-02-20 19:37:14 +00002607 return false;
2608
2609 const Value *Op = II->getArgOperand(0);
2610 unsigned InputReg = getRegForValue(Op);
2611 if (InputReg == 0)
2612 return false;
2613
2614 // F16C only allows converting from float to half and from half to float.
2615 bool IsFloatToHalf = II->getIntrinsicID() == Intrinsic::convert_to_fp16;
2616 if (IsFloatToHalf) {
2617 if (!Op->getType()->isFloatTy())
2618 return false;
2619 } else {
2620 if (!II->getType()->isFloatTy())
2621 return false;
2622 }
2623
2624 unsigned ResultReg = 0;
2625 const TargetRegisterClass *RC = TLI.getRegClassFor(MVT::v8i16);
2626 if (IsFloatToHalf) {
2627 // 'InputReg' is implicitly promoted from register class FR32 to
2628 // register class VR128 by method 'constrainOperandRegClass' which is
2629 // directly called by 'fastEmitInst_ri'.
2630 // Instruction VCVTPS2PHrr takes an extra immediate operand which is
Ahmed Bougacha68a8efa2016-02-02 01:44:03 +00002631 // used to provide rounding control: use MXCSR.RC, encoded as 0b100.
2632 // It's consistent with the other FP instructions, which are usually
2633 // controlled by MXCSR.
2634 InputReg = fastEmitInst_ri(X86::VCVTPS2PHrr, RC, InputReg, false, 4);
Andrea Di Biagio70351782015-02-20 19:37:14 +00002635
2636 // Move the lower 32-bits of ResultReg to another register of class GR32.
2637 ResultReg = createResultReg(&X86::GR32RegClass);
2638 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2639 TII.get(X86::VMOVPDI2DIrr), ResultReg)
2640 .addReg(InputReg, RegState::Kill);
2641
2642 // The result value is in the lower 16-bits of ResultReg.
2643 unsigned RegIdx = X86::sub_16bit;
2644 ResultReg = fastEmitInst_extractsubreg(MVT::i16, ResultReg, true, RegIdx);
2645 } else {
2646 assert(Op->getType()->isIntegerTy(16) && "Expected a 16-bit integer!");
2647 // Explicitly sign-extend the input to 32-bit.
2648 InputReg = fastEmit_r(MVT::i16, MVT::i32, ISD::SIGN_EXTEND, InputReg,
2649 /*Kill=*/false);
2650
2651 // The following SCALAR_TO_VECTOR will be expanded into a VMOVDI2PDIrr.
2652 InputReg = fastEmit_r(MVT::i32, MVT::v4i32, ISD::SCALAR_TO_VECTOR,
2653 InputReg, /*Kill=*/true);
2654
2655 InputReg = fastEmitInst_r(X86::VCVTPH2PSrr, RC, InputReg, /*Kill=*/true);
2656
2657 // The result value is in the lower 32-bits of ResultReg.
2658 // Emit an explicit copy from register class VR128 to register class FR32.
2659 ResultReg = createResultReg(&X86::FR32RegClass);
2660 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2661 TII.get(TargetOpcode::COPY), ResultReg)
2662 .addReg(InputReg, RegState::Kill);
2663 }
2664
2665 updateValueMap(II, ResultReg);
2666 return true;
2667 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002668 case Intrinsic::frameaddress: {
David Majnemerca194852015-02-10 22:00:34 +00002669 MachineFunction *MF = FuncInfo.MF;
2670 if (MF->getTarget().getMCAsmInfo()->usesWindowsCFI())
2671 return false;
2672
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002673 Type *RetTy = II->getCalledFunction()->getReturnType();
2674
2675 MVT VT;
2676 if (!isTypeLegal(RetTy, VT))
2677 return false;
2678
2679 unsigned Opc;
2680 const TargetRegisterClass *RC = nullptr;
2681
2682 switch (VT.SimpleTy) {
2683 default: llvm_unreachable("Invalid result type for frameaddress.");
2684 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
2685 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
2686 }
2687
2688 // This needs to be set before we call getPtrSizedFrameRegister, otherwise
2689 // we get the wrong frame register.
Matthias Braun941a7052016-07-28 18:40:00 +00002690 MachineFrameInfo &MFI = MF->getFrameInfo();
2691 MFI.setFrameAddressIsTaken(true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002692
Eric Christophera1c535b2015-02-02 23:03:45 +00002693 const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
David Majnemerca194852015-02-10 22:00:34 +00002694 unsigned FrameReg = RegInfo->getPtrSizedFrameRegister(*MF);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002695 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
2696 (FrameReg == X86::EBP && VT == MVT::i32)) &&
2697 "Invalid Frame Register!");
2698
2699 // Always make a copy of the frame register to to a vreg first, so that we
2700 // never directly reference the frame register (the TwoAddressInstruction-
2701 // Pass doesn't like that).
2702 unsigned SrcReg = createResultReg(RC);
2703 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2704 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
2705
2706 // Now recursively load from the frame address.
2707 // movq (%rbp), %rax
2708 // movq (%rax), %rax
2709 // movq (%rax), %rax
2710 // ...
2711 unsigned DestReg;
2712 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
2713 while (Depth--) {
2714 DestReg = createResultReg(RC);
2715 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2716 TII.get(Opc), DestReg), SrcReg);
2717 SrcReg = DestReg;
2718 }
2719
2720 updateValueMap(II, SrcReg);
2721 return true;
2722 }
2723 case Intrinsic::memcpy: {
2724 const MemCpyInst *MCI = cast<MemCpyInst>(II);
2725 // Don't handle volatile or variable length memcpys.
2726 if (MCI->isVolatile())
2727 return false;
2728
2729 if (isa<ConstantInt>(MCI->getLength())) {
2730 // Small memcpy's are common enough that we want to do them
2731 // without a call if possible.
2732 uint64_t Len = cast<ConstantInt>(MCI->getLength())->getZExtValue();
2733 if (IsMemcpySmall(Len)) {
2734 X86AddressMode DestAM, SrcAM;
2735 if (!X86SelectAddress(MCI->getRawDest(), DestAM) ||
2736 !X86SelectAddress(MCI->getRawSource(), SrcAM))
2737 return false;
2738 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
2739 return true;
2740 }
2741 }
2742
2743 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2744 if (!MCI->getLength()->getType()->isIntegerTy(SizeWidth))
2745 return false;
2746
2747 if (MCI->getSourceAddressSpace() > 255 || MCI->getDestAddressSpace() > 255)
2748 return false;
2749
Pete Cooper67cf9a72015-11-19 05:56:52 +00002750 return lowerCallTo(II, "memcpy", II->getNumArgOperands() - 2);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002751 }
2752 case Intrinsic::memset: {
2753 const MemSetInst *MSI = cast<MemSetInst>(II);
2754
2755 if (MSI->isVolatile())
2756 return false;
2757
2758 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2759 if (!MSI->getLength()->getType()->isIntegerTy(SizeWidth))
2760 return false;
2761
2762 if (MSI->getDestAddressSpace() > 255)
2763 return false;
2764
Pete Cooper67cf9a72015-11-19 05:56:52 +00002765 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002766 }
2767 case Intrinsic::stackprotector: {
2768 // Emit code to store the stack guard onto the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002769 EVT PtrTy = TLI.getPointerTy(DL);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002770
2771 const Value *Op1 = II->getArgOperand(0); // The guard's value.
2772 const AllocaInst *Slot = cast<AllocaInst>(II->getArgOperand(1));
2773
2774 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
2775
2776 // Grab the frame index.
2777 X86AddressMode AM;
2778 if (!X86SelectAddress(Slot, AM)) return false;
2779 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
2780 return true;
2781 }
2782 case Intrinsic::dbg_declare: {
2783 const DbgDeclareInst *DI = cast<DbgDeclareInst>(II);
2784 X86AddressMode AM;
2785 assert(DI->getAddress() && "Null address should be checked earlier!");
2786 if (!X86SelectAddress(DI->getAddress(), AM))
2787 return false;
2788 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
2789 // FIXME may need to add RegState::Debug to any registers produced,
2790 // although ESP/EBP should be the only ones at the moment.
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00002791 assert(DI->getVariable()->isValidLocationForIntrinsic(DbgLoc) &&
2792 "Expected inlined-at fields to agree");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002793 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM)
2794 .addImm(0)
2795 .addMetadata(DI->getVariable())
2796 .addMetadata(DI->getExpression());
2797 return true;
2798 }
2799 case Intrinsic::trap: {
2800 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
2801 return true;
2802 }
2803 case Intrinsic::sqrt: {
2804 if (!Subtarget->hasSSE1())
2805 return false;
2806
2807 Type *RetTy = II->getCalledFunction()->getReturnType();
2808
2809 MVT VT;
2810 if (!isTypeLegal(RetTy, VT))
2811 return false;
2812
2813 // Unfortunately we can't use fastEmit_r, because the AVX version of FSQRT
2814 // is not generated by FastISel yet.
2815 // FIXME: Update this code once tablegen can handle it.
Craig Toppercf65c622016-03-02 04:42:31 +00002816 static const uint16_t SqrtOpc[2][2] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002817 {X86::SQRTSSr, X86::VSQRTSSr},
2818 {X86::SQRTSDr, X86::VSQRTSDr}
2819 };
2820 bool HasAVX = Subtarget->hasAVX();
2821 unsigned Opc;
2822 const TargetRegisterClass *RC;
2823 switch (VT.SimpleTy) {
2824 default: return false;
2825 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
2826 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
2827 }
2828
2829 const Value *SrcVal = II->getArgOperand(0);
2830 unsigned SrcReg = getRegForValue(SrcVal);
2831
2832 if (SrcReg == 0)
2833 return false;
2834
2835 unsigned ImplicitDefReg = 0;
2836 if (HasAVX) {
2837 ImplicitDefReg = createResultReg(RC);
2838 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2839 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2840 }
2841
2842 unsigned ResultReg = createResultReg(RC);
2843 MachineInstrBuilder MIB;
2844 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
2845 ResultReg);
2846
2847 if (ImplicitDefReg)
2848 MIB.addReg(ImplicitDefReg);
2849
2850 MIB.addReg(SrcReg);
2851
2852 updateValueMap(II, ResultReg);
2853 return true;
2854 }
2855 case Intrinsic::sadd_with_overflow:
2856 case Intrinsic::uadd_with_overflow:
2857 case Intrinsic::ssub_with_overflow:
2858 case Intrinsic::usub_with_overflow:
2859 case Intrinsic::smul_with_overflow:
2860 case Intrinsic::umul_with_overflow: {
2861 // This implements the basic lowering of the xalu with overflow intrinsics
2862 // into add/sub/mul followed by either seto or setb.
2863 const Function *Callee = II->getCalledFunction();
2864 auto *Ty = cast<StructType>(Callee->getReturnType());
2865 Type *RetTy = Ty->getTypeAtIndex(0U);
Zvi Rackover6f76f462016-11-15 13:50:35 +00002866 assert(Ty->getTypeAtIndex(1)->isIntegerTy() &&
2867 Ty->getTypeAtIndex(1)->getScalarSizeInBits() == 1 &&
2868 "Overflow value expected to be an i1");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002869
2870 MVT VT;
2871 if (!isTypeLegal(RetTy, VT))
2872 return false;
2873
2874 if (VT < MVT::i8 || VT > MVT::i64)
2875 return false;
2876
2877 const Value *LHS = II->getArgOperand(0);
2878 const Value *RHS = II->getArgOperand(1);
2879
2880 // Canonicalize immediate to the RHS.
2881 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2882 isCommutativeIntrinsic(II))
2883 std::swap(LHS, RHS);
2884
2885 bool UseIncDec = false;
2886 if (isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isOne())
2887 UseIncDec = true;
2888
2889 unsigned BaseOpc, CondOpc;
2890 switch (II->getIntrinsicID()) {
2891 default: llvm_unreachable("Unexpected intrinsic!");
2892 case Intrinsic::sadd_with_overflow:
2893 BaseOpc = UseIncDec ? unsigned(X86ISD::INC) : unsigned(ISD::ADD);
2894 CondOpc = X86::SETOr;
2895 break;
2896 case Intrinsic::uadd_with_overflow:
2897 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
2898 case Intrinsic::ssub_with_overflow:
2899 BaseOpc = UseIncDec ? unsigned(X86ISD::DEC) : unsigned(ISD::SUB);
2900 CondOpc = X86::SETOr;
2901 break;
2902 case Intrinsic::usub_with_overflow:
2903 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
2904 case Intrinsic::smul_with_overflow:
2905 BaseOpc = X86ISD::SMUL; CondOpc = X86::SETOr; break;
2906 case Intrinsic::umul_with_overflow:
2907 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
2908 }
2909
2910 unsigned LHSReg = getRegForValue(LHS);
2911 if (LHSReg == 0)
2912 return false;
2913 bool LHSIsKill = hasTrivialKill(LHS);
2914
2915 unsigned ResultReg = 0;
2916 // Check if we have an immediate version.
2917 if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
Craig Topper66111882016-06-02 04:19:42 +00002918 static const uint16_t Opc[2][4] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002919 { X86::INC8r, X86::INC16r, X86::INC32r, X86::INC64r },
2920 { X86::DEC8r, X86::DEC16r, X86::DEC32r, X86::DEC64r }
2921 };
2922
2923 if (BaseOpc == X86ISD::INC || BaseOpc == X86ISD::DEC) {
2924 ResultReg = createResultReg(TLI.getRegClassFor(VT));
2925 bool IsDec = BaseOpc == X86ISD::DEC;
2926 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2927 TII.get(Opc[IsDec][VT.SimpleTy-MVT::i8]), ResultReg)
2928 .addReg(LHSReg, getKillRegState(LHSIsKill));
2929 } else
2930 ResultReg = fastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2931 CI->getZExtValue());
2932 }
2933
2934 unsigned RHSReg;
2935 bool RHSIsKill;
2936 if (!ResultReg) {
2937 RHSReg = getRegForValue(RHS);
2938 if (RHSReg == 0)
2939 return false;
2940 RHSIsKill = hasTrivialKill(RHS);
2941 ResultReg = fastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2942 RHSIsKill);
2943 }
2944
2945 // FastISel doesn't have a pattern for all X86::MUL*r and X86::IMUL*r. Emit
2946 // it manually.
2947 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
Craig Toppercf65c622016-03-02 04:42:31 +00002948 static const uint16_t MULOpc[] =
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002949 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
Craig Toppercf65c622016-03-02 04:42:31 +00002950 static const MCPhysReg Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002951 // First copy the first operand into RAX, which is an implicit input to
2952 // the X86::MUL*r instruction.
2953 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2954 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2955 .addReg(LHSReg, getKillRegState(LHSIsKill));
2956 ResultReg = fastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2957 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
2958 } else if (BaseOpc == X86ISD::SMUL && !ResultReg) {
Craig Toppercf65c622016-03-02 04:42:31 +00002959 static const uint16_t MULOpc[] =
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002960 { X86::IMUL8r, X86::IMUL16rr, X86::IMUL32rr, X86::IMUL64rr };
2961 if (VT == MVT::i8) {
2962 // Copy the first operand into AL, which is an implicit input to the
2963 // X86::IMUL8r instruction.
2964 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2965 TII.get(TargetOpcode::COPY), X86::AL)
2966 .addReg(LHSReg, getKillRegState(LHSIsKill));
2967 ResultReg = fastEmitInst_r(MULOpc[0], TLI.getRegClassFor(VT), RHSReg,
2968 RHSIsKill);
2969 } else
2970 ResultReg = fastEmitInst_rr(MULOpc[VT.SimpleTy-MVT::i8],
2971 TLI.getRegClassFor(VT), LHSReg, LHSIsKill,
2972 RHSReg, RHSIsKill);
2973 }
2974
2975 if (!ResultReg)
2976 return false;
2977
Zvi Rackoverf0b9b57b2016-11-15 13:29:23 +00002978 // Assign to a GPR since the overflow return value is lowered to a SETcc.
2979 unsigned ResultReg2 = createResultReg(&X86::GR8RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002980 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
2981 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
2982 ResultReg2);
2983
2984 updateValueMap(II, ResultReg, 2);
2985 return true;
2986 }
2987 case Intrinsic::x86_sse_cvttss2si:
2988 case Intrinsic::x86_sse_cvttss2si64:
2989 case Intrinsic::x86_sse2_cvttsd2si:
2990 case Intrinsic::x86_sse2_cvttsd2si64: {
2991 bool IsInputDouble;
2992 switch (II->getIntrinsicID()) {
2993 default: llvm_unreachable("Unexpected intrinsic.");
2994 case Intrinsic::x86_sse_cvttss2si:
2995 case Intrinsic::x86_sse_cvttss2si64:
2996 if (!Subtarget->hasSSE1())
2997 return false;
2998 IsInputDouble = false;
2999 break;
3000 case Intrinsic::x86_sse2_cvttsd2si:
3001 case Intrinsic::x86_sse2_cvttsd2si64:
3002 if (!Subtarget->hasSSE2())
3003 return false;
3004 IsInputDouble = true;
3005 break;
3006 }
3007
3008 Type *RetTy = II->getCalledFunction()->getReturnType();
3009 MVT VT;
3010 if (!isTypeLegal(RetTy, VT))
3011 return false;
3012
Craig Topper66111882016-06-02 04:19:42 +00003013 static const uint16_t CvtOpc[2][2][2] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003014 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
3015 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
3016 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
3017 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
3018 };
3019 bool HasAVX = Subtarget->hasAVX();
3020 unsigned Opc;
3021 switch (VT.SimpleTy) {
3022 default: llvm_unreachable("Unexpected result type.");
3023 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
3024 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
3025 }
3026
3027 // Check if we can fold insertelement instructions into the convert.
3028 const Value *Op = II->getArgOperand(0);
3029 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
3030 const Value *Index = IE->getOperand(2);
3031 if (!isa<ConstantInt>(Index))
3032 break;
3033 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
3034
3035 if (Idx == 0) {
3036 Op = IE->getOperand(1);
3037 break;
3038 }
3039 Op = IE->getOperand(0);
3040 }
3041
3042 unsigned Reg = getRegForValue(Op);
3043 if (Reg == 0)
3044 return false;
3045
3046 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
3048 .addReg(Reg);
3049
3050 updateValueMap(II, ResultReg);
3051 return true;
3052 }
3053 }
3054}
3055
3056bool X86FastISel::fastLowerArguments() {
3057 if (!FuncInfo.CanLowerReturn)
3058 return false;
3059
3060 const Function *F = FuncInfo.Fn;
3061 if (F->isVarArg())
3062 return false;
3063
3064 CallingConv::ID CC = F->getCallingConv();
3065 if (CC != CallingConv::C)
3066 return false;
3067
3068 if (Subtarget->isCallingConvWin64(CC))
3069 return false;
3070
3071 if (!Subtarget->is64Bit())
3072 return false;
3073
3074 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
3075 unsigned GPRCnt = 0;
3076 unsigned FPRCnt = 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003077 for (auto const &Arg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00003078 if (Arg.hasAttribute(Attribute::ByVal) ||
3079 Arg.hasAttribute(Attribute::InReg) ||
3080 Arg.hasAttribute(Attribute::StructRet) ||
3081 Arg.hasAttribute(Attribute::SwiftSelf) ||
3082 Arg.hasAttribute(Attribute::SwiftError) ||
3083 Arg.hasAttribute(Attribute::Nest))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003084 return false;
3085
3086 Type *ArgTy = Arg.getType();
3087 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3088 return false;
3089
Mehdi Amini44ede332015-07-09 02:09:04 +00003090 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003091 if (!ArgVT.isSimple()) return false;
3092 switch (ArgVT.getSimpleVT().SimpleTy) {
3093 default: return false;
3094 case MVT::i32:
3095 case MVT::i64:
3096 ++GPRCnt;
3097 break;
3098 case MVT::f32:
3099 case MVT::f64:
3100 if (!Subtarget->hasSSE1())
3101 return false;
3102 ++FPRCnt;
3103 break;
3104 }
3105
3106 if (GPRCnt > 6)
3107 return false;
3108
3109 if (FPRCnt > 8)
3110 return false;
3111 }
3112
3113 static const MCPhysReg GPR32ArgRegs[] = {
3114 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
3115 };
3116 static const MCPhysReg GPR64ArgRegs[] = {
3117 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
3118 };
3119 static const MCPhysReg XMMArgRegs[] = {
3120 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3121 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3122 };
3123
3124 unsigned GPRIdx = 0;
3125 unsigned FPRIdx = 0;
3126 for (auto const &Arg : F->args()) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003127 MVT VT = TLI.getSimpleValueType(DL, Arg.getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003128 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
3129 unsigned SrcReg;
3130 switch (VT.SimpleTy) {
3131 default: llvm_unreachable("Unexpected value type.");
3132 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
3133 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00003134 case MVT::f32: LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003135 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
3136 }
3137 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3138 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3139 // Without this, EmitLiveInCopies may eliminate the livein if its only
3140 // use is a bitcast (which isn't turned into an instruction).
3141 unsigned ResultReg = createResultReg(RC);
3142 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3143 TII.get(TargetOpcode::COPY), ResultReg)
3144 .addReg(DstReg, getKillRegState(true));
3145 updateValueMap(&Arg, ResultReg);
3146 }
3147 return true;
3148}
3149
Nico Weberaf7e8462016-07-14 01:52:51 +00003150static unsigned computeBytesPoppedByCalleeForSRet(const X86Subtarget *Subtarget,
3151 CallingConv::ID CC,
3152 ImmutableCallSite *CS) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003153 if (Subtarget->is64Bit())
3154 return 0;
3155 if (Subtarget->getTargetTriple().isOSMSVCRT())
3156 return 0;
3157 if (CC == CallingConv::Fast || CC == CallingConv::GHC ||
3158 CC == CallingConv::HiPE)
3159 return 0;
Sanjoy Dasb11b4402015-11-04 20:33:45 +00003160
3161 if (CS)
Reid Klecknerfb502d22017-04-14 20:19:02 +00003162 if (CS->arg_empty() || !CS->paramHasAttr(0, Attribute::StructRet) ||
3163 CS->paramHasAttr(0, Attribute::InReg) || Subtarget->isTargetMCU())
Sanjoy Dasb11b4402015-11-04 20:33:45 +00003164 return 0;
3165
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003166 return 4;
3167}
3168
3169bool X86FastISel::fastLowerCall(CallLoweringInfo &CLI) {
3170 auto &OutVals = CLI.OutVals;
3171 auto &OutFlags = CLI.OutFlags;
3172 auto &OutRegs = CLI.OutRegs;
3173 auto &Ins = CLI.Ins;
3174 auto &InRegs = CLI.InRegs;
3175 CallingConv::ID CC = CLI.CallConv;
3176 bool &IsTailCall = CLI.IsTailCall;
3177 bool IsVarArg = CLI.IsVarArg;
3178 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00003179 MCSymbol *Symbol = CLI.Symbol;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003180
3181 bool Is64Bit = Subtarget->is64Bit();
3182 bool IsWin64 = Subtarget->isCallingConvWin64(CC);
3183
Oren Ben Simhondbd4bba2017-05-03 13:07:19 +00003184 const CallInst *CI =
3185 CLI.CS ? dyn_cast<CallInst>(CLI.CS->getInstruction()) : nullptr;
3186 const Function *CalledFn = CI ? CI->getCalledFunction() : nullptr;
3187
3188 // Functions with no_caller_saved_registers that need special handling.
3189 if ((CI && CI->hasFnAttr("no_caller_saved_registers")) ||
3190 (CalledFn && CalledFn->hasFnAttribute("no_caller_saved_registers")))
3191 return false;
3192
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003193 // Handle only C, fastcc, and webkit_js calling conventions for now.
3194 switch (CC) {
3195 default: return false;
3196 case CallingConv::C:
3197 case CallingConv::Fast:
3198 case CallingConv::WebKit_JS:
Manman Renf8bdd882016-04-05 22:41:47 +00003199 case CallingConv::Swift:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003200 case CallingConv::X86_FastCall:
Nico Weberecdf45b2016-07-14 13:54:26 +00003201 case CallingConv::X86_StdCall:
Nico Weberaf7e8462016-07-14 01:52:51 +00003202 case CallingConv::X86_ThisCall:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003203 case CallingConv::X86_64_Win64:
3204 case CallingConv::X86_64_SysV:
3205 break;
3206 }
3207
3208 // Allow SelectionDAG isel to handle tail calls.
3209 if (IsTailCall)
3210 return false;
3211
3212 // fastcc with -tailcallopt is intended to provide a guaranteed
3213 // tail call optimization. Fastisel doesn't know how to do that.
3214 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
3215 return false;
3216
3217 // Don't know how to handle Win64 varargs yet. Nothing special needed for
3218 // x86-32. Special handling for x86-64 is implemented.
3219 if (IsVarArg && IsWin64)
3220 return false;
3221
3222 // Don't know about inalloca yet.
3223 if (CLI.CS && CLI.CS->hasInAllocaArgument())
3224 return false;
3225
Manman Ren57518142016-04-11 21:08:06 +00003226 for (auto Flag : CLI.OutFlags)
3227 if (Flag.isSwiftError())
3228 return false;
3229
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003230 SmallVector<MVT, 16> OutVTs;
3231 SmallVector<unsigned, 16> ArgRegs;
3232
3233 // If this is a constant i1/i8/i16 argument, promote to i32 to avoid an extra
3234 // instruction. This is safe because it is common to all FastISel supported
3235 // calling conventions on x86.
3236 for (int i = 0, e = OutVals.size(); i != e; ++i) {
3237 Value *&Val = OutVals[i];
3238 ISD::ArgFlagsTy Flags = OutFlags[i];
3239 if (auto *CI = dyn_cast<ConstantInt>(Val)) {
3240 if (CI->getBitWidth() < 32) {
3241 if (Flags.isSExt())
3242 Val = ConstantExpr::getSExt(CI, Type::getInt32Ty(CI->getContext()));
3243 else
3244 Val = ConstantExpr::getZExt(CI, Type::getInt32Ty(CI->getContext()));
3245 }
3246 }
3247
3248 // Passing bools around ends up doing a trunc to i1 and passing it.
3249 // Codegen this as an argument + "and 1".
3250 MVT VT;
3251 auto *TI = dyn_cast<TruncInst>(Val);
3252 unsigned ResultReg;
3253 if (TI && TI->getType()->isIntegerTy(1) && CLI.CS &&
3254 (TI->getParent() == CLI.CS->getInstruction()->getParent()) &&
3255 TI->hasOneUse()) {
3256 Value *PrevVal = TI->getOperand(0);
3257 ResultReg = getRegForValue(PrevVal);
3258
3259 if (!ResultReg)
3260 return false;
3261
3262 if (!isTypeLegal(PrevVal->getType(), VT))
3263 return false;
3264
3265 ResultReg =
3266 fastEmit_ri(VT, VT, ISD::AND, ResultReg, hasTrivialKill(PrevVal), 1);
3267 } else {
3268 if (!isTypeLegal(Val->getType(), VT))
3269 return false;
3270 ResultReg = getRegForValue(Val);
3271 }
3272
3273 if (!ResultReg)
3274 return false;
3275
3276 ArgRegs.push_back(ResultReg);
3277 OutVTs.push_back(VT);
3278 }
3279
3280 // Analyze operands of the call, assigning locations to each operand.
3281 SmallVector<CCValAssign, 16> ArgLocs;
3282 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, CLI.RetTy->getContext());
3283
3284 // Allocate shadow area for Win64
3285 if (IsWin64)
3286 CCInfo.AllocateStack(32, 8);
3287
3288 CCInfo.AnalyzeCallOperands(OutVTs, OutFlags, CC_X86);
3289
3290 // Get a count of how many bytes are to be pushed on the stack.
Jeroen Ketema740f9d72015-09-29 10:12:57 +00003291 unsigned NumBytes = CCInfo.getAlignedCallFrameSize();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003292
3293 // Issue CALLSEQ_START
3294 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
3295 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Serge Pavlovd526b132017-05-09 13:35:13 +00003296 .addImm(NumBytes).addImm(0).addImm(0);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003297
3298 // Walk the register/memloc assignments, inserting copies/loads.
Eric Christophera1c535b2015-02-02 23:03:45 +00003299 const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003300 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3301 CCValAssign const &VA = ArgLocs[i];
3302 const Value *ArgVal = OutVals[VA.getValNo()];
3303 MVT ArgVT = OutVTs[VA.getValNo()];
3304
3305 if (ArgVT == MVT::x86mmx)
3306 return false;
3307
3308 unsigned ArgReg = ArgRegs[VA.getValNo()];
3309
3310 // Promote the value if needed.
3311 switch (VA.getLocInfo()) {
3312 case CCValAssign::Full: break;
3313 case CCValAssign::SExt: {
3314 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3315 "Unexpected extend");
David Majnemer2c5aeab2016-05-04 00:22:23 +00003316
Craig Topper088ba172016-12-05 06:09:55 +00003317 if (ArgVT == MVT::i1)
David Majnemer2c5aeab2016-05-04 00:22:23 +00003318 return false;
3319
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003320 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
3321 ArgVT, ArgReg);
3322 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
3323 ArgVT = VA.getLocVT();
3324 break;
3325 }
3326 case CCValAssign::ZExt: {
3327 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3328 "Unexpected extend");
David Majnemer2c5aeab2016-05-04 00:22:23 +00003329
3330 // Handle zero-extension from i1 to i8, which is common.
Craig Topper088ba172016-12-05 06:09:55 +00003331 if (ArgVT == MVT::i1) {
Craig Topper058f2f62017-03-28 16:35:29 +00003332 // In case SrcReg is a K register, COPY to a GPR
3333 if (MRI.getRegClass(ArgReg) == &X86::VK1RegClass) {
3334 unsigned KArgReg = ArgReg;
3335 ArgReg = createResultReg(&X86::GR32RegClass);
3336 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3337 TII.get(TargetOpcode::COPY), ArgReg)
3338 .addReg(KArgReg);
3339 ArgReg = fastEmitInst_extractsubreg(MVT::i8, ArgReg, /*Kill=*/true,
3340 X86::sub_8bit);
3341 }
David Majnemer2c5aeab2016-05-04 00:22:23 +00003342 // Set the high bits to zero.
3343 ArgReg = fastEmitZExtFromI1(MVT::i8, ArgReg, /*TODO: Kill=*/false);
3344 ArgVT = MVT::i8;
3345
3346 if (ArgReg == 0)
3347 return false;
3348 }
3349
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003350 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
3351 ArgVT, ArgReg);
3352 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
3353 ArgVT = VA.getLocVT();
3354 break;
3355 }
3356 case CCValAssign::AExt: {
3357 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3358 "Unexpected extend");
3359 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), ArgReg,
3360 ArgVT, ArgReg);
3361 if (!Emitted)
3362 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
3363 ArgVT, ArgReg);
3364 if (!Emitted)
3365 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
3366 ArgVT, ArgReg);
3367
3368 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
3369 ArgVT = VA.getLocVT();
3370 break;
3371 }
3372 case CCValAssign::BCvt: {
3373 ArgReg = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, ArgReg,
3374 /*TODO: Kill=*/false);
3375 assert(ArgReg && "Failed to emit a bitcast!");
3376 ArgVT = VA.getLocVT();
3377 break;
3378 }
3379 case CCValAssign::VExt:
3380 // VExt has not been implemented, so this should be impossible to reach
3381 // for now. However, fallback to Selection DAG isel once implemented.
3382 return false;
3383 case CCValAssign::AExtUpper:
3384 case CCValAssign::SExtUpper:
3385 case CCValAssign::ZExtUpper:
3386 case CCValAssign::FPExt:
3387 llvm_unreachable("Unexpected loc info!");
3388 case CCValAssign::Indirect:
3389 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
3390 // support this.
3391 return false;
3392 }
3393
3394 if (VA.isRegLoc()) {
3395 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3396 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
3397 OutRegs.push_back(VA.getLocReg());
3398 } else {
3399 assert(VA.isMemLoc());
3400
3401 // Don't emit stores for undef values.
3402 if (isa<UndefValue>(ArgVal))
3403 continue;
3404
3405 unsigned LocMemOffset = VA.getLocMemOffset();
3406 X86AddressMode AM;
3407 AM.Base.Reg = RegInfo->getStackRegister();
3408 AM.Disp = LocMemOffset;
3409 ISD::ArgFlagsTy Flags = OutFlags[VA.getValNo()];
3410 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
3411 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00003412 MachinePointerInfo::getStack(*FuncInfo.MF, LocMemOffset),
3413 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003414 if (Flags.isByVal()) {
3415 X86AddressMode SrcAM;
3416 SrcAM.Base.Reg = ArgReg;
3417 if (!TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize()))
3418 return false;
3419 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
3420 // If this is a really simple value, emit this with the Value* version
3421 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
3422 // as it can cause us to reevaluate the argument.
3423 if (!X86FastEmitStore(ArgVT, ArgVal, AM, MMO))
3424 return false;
3425 } else {
3426 bool ValIsKill = hasTrivialKill(ArgVal);
3427 if (!X86FastEmitStore(ArgVT, ArgReg, ValIsKill, AM, MMO))
3428 return false;
3429 }
3430 }
3431 }
3432
3433 // ELF / PIC requires GOT in the EBX register before function calls via PLT
3434 // GOT pointer.
3435 if (Subtarget->isPICStyleGOT()) {
3436 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
3437 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3438 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
3439 }
3440
3441 if (Is64Bit && IsVarArg && !IsWin64) {
3442 // From AMD64 ABI document:
3443 // For calls that may call functions that use varargs or stdargs
3444 // (prototype-less calls or calls to functions containing ellipsis (...) in
3445 // the declaration) %al is used as hidden argument to specify the number
3446 // of SSE registers used. The contents of %al do not need to match exactly
3447 // the number of registers, but must be an ubound on the number of SSE
3448 // registers used and is in the range 0 - 8 inclusive.
3449
3450 // Count the number of XMM registers allocated.
3451 static const MCPhysReg XMMArgRegs[] = {
3452 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3453 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3454 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003455 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003456 assert((Subtarget->hasSSE1() || !NumXMMRegs)
3457 && "SSE registers cannot be used when SSE is disabled");
3458 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
3459 X86::AL).addImm(NumXMMRegs);
3460 }
3461
3462 // Materialize callee address in a register. FIXME: GV address can be
3463 // handled with a CALLpcrel32 instead.
3464 X86AddressMode CalleeAM;
3465 if (!X86SelectCallAddress(Callee, CalleeAM))
3466 return false;
3467
3468 unsigned CalleeOp = 0;
3469 const GlobalValue *GV = nullptr;
3470 if (CalleeAM.GV != nullptr) {
3471 GV = CalleeAM.GV;
3472 } else if (CalleeAM.Base.Reg != 0) {
3473 CalleeOp = CalleeAM.Base.Reg;
3474 } else
3475 return false;
3476
3477 // Issue the call.
3478 MachineInstrBuilder MIB;
3479 if (CalleeOp) {
3480 // Register-indirect call.
3481 unsigned CallOpc = Is64Bit ? X86::CALL64r : X86::CALL32r;
3482 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
3483 .addReg(CalleeOp);
3484 } else {
3485 // Direct call.
3486 assert(GV && "Not a direct call");
3487 unsigned CallOpc = Is64Bit ? X86::CALL64pcrel32 : X86::CALLpcrel32;
3488
3489 // See if we need any target-specific flags on the GV operand.
Rafael Espindola46107b92016-05-19 18:49:29 +00003490 unsigned char OpFlags = Subtarget->classifyGlobalFunctionReference(GV);
Asaf Badouh89406d12016-04-20 08:32:57 +00003491 // Ignore NonLazyBind attribute in FastISel
3492 if (OpFlags == X86II::MO_GOTPCREL)
3493 OpFlags = 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003494
3495 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00003496 if (Symbol)
3497 MIB.addSym(Symbol, OpFlags);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003498 else
3499 MIB.addGlobalAddress(GV, 0, OpFlags);
3500 }
3501
3502 // Add a register mask operand representing the call-preserved registers.
3503 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00003504 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003505
3506 // Add an implicit use GOT pointer in EBX.
3507 if (Subtarget->isPICStyleGOT())
3508 MIB.addReg(X86::EBX, RegState::Implicit);
3509
3510 if (Is64Bit && IsVarArg && !IsWin64)
3511 MIB.addReg(X86::AL, RegState::Implicit);
3512
3513 // Add implicit physical register uses to the call.
3514 for (auto Reg : OutRegs)
3515 MIB.addReg(Reg, RegState::Implicit);
3516
3517 // Issue CALLSEQ_END
3518 unsigned NumBytesForCalleeToPop =
Nico Weberaf7e8462016-07-14 01:52:51 +00003519 X86::isCalleePop(CC, Subtarget->is64Bit(), IsVarArg,
3520 TM.Options.GuaranteedTailCallOpt)
3521 ? NumBytes // Callee pops everything.
3522 : computeBytesPoppedByCalleeForSRet(Subtarget, CC, CLI.CS);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003523 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
3524 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
3525 .addImm(NumBytes).addImm(NumBytesForCalleeToPop);
3526
3527 // Now handle call return values.
3528 SmallVector<CCValAssign, 16> RVLocs;
3529 CCState CCRetInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs,
3530 CLI.RetTy->getContext());
3531 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
3532
3533 // Copy all of the result registers out of their specified physreg.
3534 unsigned ResultReg = FuncInfo.CreateRegs(CLI.RetTy);
3535 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3536 CCValAssign &VA = RVLocs[i];
3537 EVT CopyVT = VA.getValVT();
3538 unsigned CopyReg = ResultReg + i;
Craig Topper533b1bd2017-03-30 21:02:52 +00003539 unsigned SrcReg = VA.getLocReg();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003540
3541 // If this is x86-64, and we disabled SSE, we can't return FP values
3542 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
3543 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
3544 report_fatal_error("SSE register return with SSE disabled");
3545 }
3546
Craig Topper3001b352017-03-30 21:05:33 +00003547 // If the return value is an i1 and AVX-512 is enabled, we need
3548 // to do a fixup to make the copy legal.
Craig Topper533b1bd2017-03-30 21:02:52 +00003549 if (CopyVT == MVT::i1 && SrcReg == X86::AL && Subtarget->hasAVX512()) {
3550 // Need to copy to a GR32 first.
3551 // TODO: MOVZX isn't great here. We don't care about the upper bits.
3552 SrcReg = createResultReg(&X86::GR32RegClass);
3553 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3554 TII.get(X86::MOVZX32rr8), SrcReg).addReg(X86::AL);
3555 }
3556
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003557 // If we prefer to use the value in xmm registers, copy it out as f80 and
3558 // use a truncate to move it from fp stack reg to xmm reg.
Craig Topper533b1bd2017-03-30 21:02:52 +00003559 if ((SrcReg == X86::FP0 || SrcReg == X86::FP1) &&
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003560 isScalarFPTypeInSSEReg(VA.getValVT())) {
3561 CopyVT = MVT::f80;
3562 CopyReg = createResultReg(&X86::RFP80RegClass);
3563 }
3564
3565 // Copy out the result.
3566 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Craig Topper533b1bd2017-03-30 21:02:52 +00003567 TII.get(TargetOpcode::COPY), CopyReg).addReg(SrcReg);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003568 InRegs.push_back(VA.getLocReg());
3569
3570 // Round the f80 to the right size, which also moves it to the appropriate
3571 // xmm register. This is accomplished by storing the f80 value in memory
3572 // and then loading it back.
3573 if (CopyVT != VA.getValVT()) {
3574 EVT ResVT = VA.getValVT();
3575 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
3576 unsigned MemSize = ResVT.getSizeInBits()/8;
3577 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
3578 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3579 TII.get(Opc)), FI)
3580 .addReg(CopyReg);
3581 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
3582 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3583 TII.get(Opc), ResultReg + i), FI);
3584 }
3585 }
3586
3587 CLI.ResultReg = ResultReg;
3588 CLI.NumResultRegs = RVLocs.size();
3589 CLI.Call = MIB;
3590
3591 return true;
3592}
3593
3594bool
3595X86FastISel::fastSelectInstruction(const Instruction *I) {
3596 switch (I->getOpcode()) {
3597 default: break;
3598 case Instruction::Load:
3599 return X86SelectLoad(I);
3600 case Instruction::Store:
3601 return X86SelectStore(I);
3602 case Instruction::Ret:
3603 return X86SelectRet(I);
3604 case Instruction::ICmp:
3605 case Instruction::FCmp:
3606 return X86SelectCmp(I);
3607 case Instruction::ZExt:
3608 return X86SelectZExt(I);
3609 case Instruction::Br:
3610 return X86SelectBranch(I);
3611 case Instruction::LShr:
3612 case Instruction::AShr:
3613 case Instruction::Shl:
3614 return X86SelectShift(I);
3615 case Instruction::SDiv:
3616 case Instruction::UDiv:
3617 case Instruction::SRem:
3618 case Instruction::URem:
3619 return X86SelectDivRem(I);
3620 case Instruction::Select:
3621 return X86SelectSelect(I);
3622 case Instruction::Trunc:
3623 return X86SelectTrunc(I);
3624 case Instruction::FPExt:
3625 return X86SelectFPExt(I);
3626 case Instruction::FPTrunc:
3627 return X86SelectFPTrunc(I);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00003628 case Instruction::SIToFP:
3629 return X86SelectSIToFP(I);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003630 case Instruction::IntToPtr: // Deliberate fall-through.
3631 case Instruction::PtrToInt: {
Mehdi Amini44ede332015-07-09 02:09:04 +00003632 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
3633 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003634 if (DstVT.bitsGT(SrcVT))
3635 return X86SelectZExt(I);
3636 if (DstVT.bitsLT(SrcVT))
3637 return X86SelectTrunc(I);
3638 unsigned Reg = getRegForValue(I->getOperand(0));
3639 if (Reg == 0) return false;
3640 updateValueMap(I, Reg);
3641 return true;
3642 }
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003643 case Instruction::BitCast: {
3644 // Select SSE2/AVX bitcasts between 128/256 bit vector types.
3645 if (!Subtarget->hasSSE2())
3646 return false;
3647
3648 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
3649 EVT DstVT = TLI.getValueType(DL, I->getType());
3650
3651 if (!SrcVT.isSimple() || !DstVT.isSimple())
3652 return false;
3653
Craig Topperdb8467a2016-12-05 05:50:51 +00003654 MVT SVT = SrcVT.getSimpleVT();
3655 MVT DVT = DstVT.getSimpleVT();
3656
3657 if (!SVT.is128BitVector() &&
3658 !(Subtarget->hasAVX() && SVT.is256BitVector()) &&
3659 !(Subtarget->hasAVX512() && SVT.is512BitVector() &&
3660 (Subtarget->hasBWI() || (SVT.getScalarSizeInBits() >= 32 &&
3661 DVT.getScalarSizeInBits() >= 32))))
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003662 return false;
3663
3664 unsigned Reg = getRegForValue(I->getOperand(0));
3665 if (Reg == 0)
3666 return false;
3667
3668 // No instruction is needed for conversion. Reuse the register used by
3669 // the fist operand.
3670 updateValueMap(I, Reg);
3671 return true;
3672 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003673 }
3674
3675 return false;
3676}
3677
3678unsigned X86FastISel::X86MaterializeInt(const ConstantInt *CI, MVT VT) {
3679 if (VT > MVT::i64)
3680 return 0;
3681
3682 uint64_t Imm = CI->getZExtValue();
3683 if (Imm == 0) {
3684 unsigned SrcReg = fastEmitInst_(X86::MOV32r0, &X86::GR32RegClass);
3685 switch (VT.SimpleTy) {
3686 default: llvm_unreachable("Unexpected value type");
3687 case MVT::i1:
Craig Topper058f2f62017-03-28 16:35:29 +00003688 if (Subtarget->hasAVX512()) {
3689 // Need to copy to a VK1 register.
3690 unsigned ResultReg = createResultReg(&X86::VK1RegClass);
3691 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3692 TII.get(TargetOpcode::COPY), ResultReg).addReg(SrcReg);
3693 return ResultReg;
3694 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003695 case MVT::i8:
3696 return fastEmitInst_extractsubreg(MVT::i8, SrcReg, /*Kill=*/true,
3697 X86::sub_8bit);
3698 case MVT::i16:
3699 return fastEmitInst_extractsubreg(MVT::i16, SrcReg, /*Kill=*/true,
3700 X86::sub_16bit);
3701 case MVT::i32:
3702 return SrcReg;
3703 case MVT::i64: {
3704 unsigned ResultReg = createResultReg(&X86::GR64RegClass);
3705 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3706 TII.get(TargetOpcode::SUBREG_TO_REG), ResultReg)
3707 .addImm(0).addReg(SrcReg).addImm(X86::sub_32bit);
3708 return ResultReg;
3709 }
3710 }
3711 }
3712
3713 unsigned Opc = 0;
3714 switch (VT.SimpleTy) {
3715 default: llvm_unreachable("Unexpected value type");
Craig Topper058f2f62017-03-28 16:35:29 +00003716 case MVT::i1:
3717 // TODO: Support this properly.
3718 if (Subtarget->hasAVX512())
3719 return 0;
3720 VT = MVT::i8;
3721 LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003722 case MVT::i8: Opc = X86::MOV8ri; break;
3723 case MVT::i16: Opc = X86::MOV16ri; break;
3724 case MVT::i32: Opc = X86::MOV32ri; break;
3725 case MVT::i64: {
3726 if (isUInt<32>(Imm))
3727 Opc = X86::MOV32ri;
3728 else if (isInt<32>(Imm))
3729 Opc = X86::MOV64ri32;
3730 else
3731 Opc = X86::MOV64ri;
3732 break;
3733 }
3734 }
3735 if (VT == MVT::i64 && Opc == X86::MOV32ri) {
3736 unsigned SrcReg = fastEmitInst_i(Opc, &X86::GR32RegClass, Imm);
3737 unsigned ResultReg = createResultReg(&X86::GR64RegClass);
3738 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3739 TII.get(TargetOpcode::SUBREG_TO_REG), ResultReg)
3740 .addImm(0).addReg(SrcReg).addImm(X86::sub_32bit);
3741 return ResultReg;
3742 }
3743 return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
3744}
3745
3746unsigned X86FastISel::X86MaterializeFP(const ConstantFP *CFP, MVT VT) {
3747 if (CFP->isNullValue())
3748 return fastMaterializeFloatZero(CFP);
3749
3750 // Can't handle alternate code models yet.
3751 CodeModel::Model CM = TM.getCodeModel();
3752 if (CM != CodeModel::Small && CM != CodeModel::Large)
3753 return 0;
3754
3755 // Get opcode and regclass of the output for the given load instruction.
3756 unsigned Opc = 0;
3757 const TargetRegisterClass *RC = nullptr;
3758 switch (VT.SimpleTy) {
3759 default: return 0;
3760 case MVT::f32:
3761 if (X86ScalarSSEf32) {
3762 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
3763 RC = &X86::FR32RegClass;
3764 } else {
3765 Opc = X86::LD_Fp32m;
3766 RC = &X86::RFP32RegClass;
3767 }
3768 break;
3769 case MVT::f64:
3770 if (X86ScalarSSEf64) {
3771 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
3772 RC = &X86::FR64RegClass;
3773 } else {
3774 Opc = X86::LD_Fp64m;
3775 RC = &X86::RFP64RegClass;
3776 }
3777 break;
3778 case MVT::f80:
3779 // No f80 support yet.
3780 return 0;
3781 }
3782
3783 // MachineConstantPool wants an explicit alignment.
3784 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
3785 if (Align == 0) {
3786 // Alignment of vector types. FIXME!
3787 Align = DL.getTypeAllocSize(CFP->getType());
3788 }
3789
3790 // x86-32 PIC requires a PIC base register for constant pools.
3791 unsigned PICBase = 0;
Rafael Espindolac7e98132016-05-20 12:20:10 +00003792 unsigned char OpFlag = Subtarget->classifyLocalReference(nullptr);
3793 if (OpFlag == X86II::MO_PIC_BASE_OFFSET)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003794 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolac7e98132016-05-20 12:20:10 +00003795 else if (OpFlag == X86II::MO_GOTOFF)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003796 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolac7e98132016-05-20 12:20:10 +00003797 else if (Subtarget->is64Bit() && TM.getCodeModel() == CodeModel::Small)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003798 PICBase = X86::RIP;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003799
3800 // Create the load from the constant pool.
3801 unsigned CPI = MCP.getConstantPoolIndex(CFP, Align);
3802 unsigned ResultReg = createResultReg(RC);
3803
3804 if (CM == CodeModel::Large) {
3805 unsigned AddrReg = createResultReg(&X86::GR64RegClass);
3806 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri),
3807 AddrReg)
3808 .addConstantPoolIndex(CPI, 0, OpFlag);
3809 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3810 TII.get(Opc), ResultReg);
3811 addDirectMem(MIB, AddrReg);
3812 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00003813 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
3814 MachineMemOperand::MOLoad, DL.getPointerSize(), Align);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003815 MIB->addMemOperand(*FuncInfo.MF, MMO);
3816 return ResultReg;
3817 }
3818
3819 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3820 TII.get(Opc), ResultReg),
3821 CPI, PICBase, OpFlag);
3822 return ResultReg;
3823}
3824
3825unsigned X86FastISel::X86MaterializeGV(const GlobalValue *GV, MVT VT) {
3826 // Can't handle alternate code models yet.
3827 if (TM.getCodeModel() != CodeModel::Small)
3828 return 0;
3829
3830 // Materialize addresses with LEA/MOV instructions.
3831 X86AddressMode AM;
3832 if (X86SelectAddress(GV, AM)) {
3833 // If the expression is just a basereg, then we're done, otherwise we need
3834 // to emit an LEA.
3835 if (AM.BaseType == X86AddressMode::RegBase &&
3836 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
3837 return AM.Base.Reg;
3838
3839 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3840 if (TM.getRelocationModel() == Reloc::Static &&
Mehdi Amini44ede332015-07-09 02:09:04 +00003841 TLI.getPointerTy(DL) == MVT::i64) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003842 // The displacement code could be more than 32 bits away so we need to use
3843 // an instruction with a 64 bit immediate
3844 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri),
3845 ResultReg)
3846 .addGlobalAddress(GV);
3847 } else {
Mehdi Amini44ede332015-07-09 02:09:04 +00003848 unsigned Opc =
3849 TLI.getPointerTy(DL) == MVT::i32
3850 ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r)
3851 : X86::LEA64r;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003852 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3853 TII.get(Opc), ResultReg), AM);
3854 }
3855 return ResultReg;
3856 }
3857 return 0;
3858}
3859
3860unsigned X86FastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003861 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003862
3863 // Only handle simple types.
3864 if (!CEVT.isSimple())
3865 return 0;
3866 MVT VT = CEVT.getSimpleVT();
3867
3868 if (const auto *CI = dyn_cast<ConstantInt>(C))
3869 return X86MaterializeInt(CI, VT);
3870 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
3871 return X86MaterializeFP(CFP, VT);
3872 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
3873 return X86MaterializeGV(GV, VT);
3874
3875 return 0;
3876}
3877
3878unsigned X86FastISel::fastMaterializeAlloca(const AllocaInst *C) {
3879 // Fail on dynamic allocas. At this point, getRegForValue has already
3880 // checked its CSE maps, so if we're here trying to handle a dynamic
3881 // alloca, we're not going to succeed. X86SelectAddress has a
3882 // check for dynamic allocas, because it's called directly from
3883 // various places, but targetMaterializeAlloca also needs a check
3884 // in order to avoid recursion between getRegForValue,
3885 // X86SelectAddrss, and targetMaterializeAlloca.
3886 if (!FuncInfo.StaticAllocaMap.count(C))
3887 return 0;
3888 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
3889
3890 X86AddressMode AM;
3891 if (!X86SelectAddress(C, AM))
3892 return 0;
Mehdi Amini44ede332015-07-09 02:09:04 +00003893 unsigned Opc =
3894 TLI.getPointerTy(DL) == MVT::i32
3895 ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r)
3896 : X86::LEA64r;
3897 const TargetRegisterClass *RC = TLI.getRegClassFor(TLI.getPointerTy(DL));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003898 unsigned ResultReg = createResultReg(RC);
3899 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3900 TII.get(Opc), ResultReg), AM);
3901 return ResultReg;
3902}
3903
3904unsigned X86FastISel::fastMaterializeFloatZero(const ConstantFP *CF) {
3905 MVT VT;
3906 if (!isTypeLegal(CF->getType(), VT))
3907 return 0;
3908
3909 // Get opcode and regclass for the given zero.
3910 unsigned Opc = 0;
3911 const TargetRegisterClass *RC = nullptr;
3912 switch (VT.SimpleTy) {
3913 default: return 0;
3914 case MVT::f32:
3915 if (X86ScalarSSEf32) {
3916 Opc = X86::FsFLD0SS;
3917 RC = &X86::FR32RegClass;
3918 } else {
3919 Opc = X86::LD_Fp032;
3920 RC = &X86::RFP32RegClass;
3921 }
3922 break;
3923 case MVT::f64:
3924 if (X86ScalarSSEf64) {
3925 Opc = X86::FsFLD0SD;
3926 RC = &X86::FR64RegClass;
3927 } else {
3928 Opc = X86::LD_Fp064;
3929 RC = &X86::RFP64RegClass;
3930 }
3931 break;
3932 case MVT::f80:
3933 // No f80 support yet.
3934 return 0;
3935 }
3936
3937 unsigned ResultReg = createResultReg(RC);
3938 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
3939 return ResultReg;
3940}
3941
3942
3943bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3944 const LoadInst *LI) {
3945 const Value *Ptr = LI->getPointerOperand();
3946 X86AddressMode AM;
3947 if (!X86SelectAddress(Ptr, AM))
3948 return false;
3949
3950 const X86InstrInfo &XII = (const X86InstrInfo &)TII;
3951
3952 unsigned Size = DL.getTypeAllocSize(LI->getType());
3953 unsigned Alignment = LI->getAlignment();
3954
3955 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3956 Alignment = DL.getABITypeAlignment(LI->getType());
3957
3958 SmallVector<MachineOperand, 8> AddrOps;
3959 AM.getFullAddress(AddrOps);
3960
Keno Fischere70b31f2015-06-08 20:09:58 +00003961 MachineInstr *Result = XII.foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003962 *FuncInfo.MF, *MI, OpNo, AddrOps, FuncInfo.InsertPt, Size, Alignment,
Keno Fischere70b31f2015-06-08 20:09:58 +00003963 /*AllowCommute=*/true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003964 if (!Result)
3965 return false;
3966
Pete Cooperd31583d2015-05-06 21:37:19 +00003967 // The index register could be in the wrong register class. Unfortunately,
3968 // foldMemoryOperandImpl could have commuted the instruction so its not enough
3969 // to just look at OpNo + the offset to the index reg. We actually need to
3970 // scan the instruction to find the index reg and see if its the correct reg
3971 // class.
Matthias Braune41e1462015-05-29 02:56:46 +00003972 unsigned OperandNo = 0;
3973 for (MachineInstr::mop_iterator I = Result->operands_begin(),
3974 E = Result->operands_end(); I != E; ++I, ++OperandNo) {
3975 MachineOperand &MO = *I;
3976 if (!MO.isReg() || MO.isDef() || MO.getReg() != AM.IndexReg)
Pete Cooperd31583d2015-05-06 21:37:19 +00003977 continue;
3978 // Found the index reg, now try to rewrite it.
Pete Cooperd31583d2015-05-06 21:37:19 +00003979 unsigned IndexReg = constrainOperandRegClass(Result->getDesc(),
Matthias Braune41e1462015-05-29 02:56:46 +00003980 MO.getReg(), OperandNo);
3981 if (IndexReg == MO.getReg())
Pete Cooperd31583d2015-05-06 21:37:19 +00003982 continue;
Matthias Braune41e1462015-05-29 02:56:46 +00003983 MO.setReg(IndexReg);
Pete Cooperd31583d2015-05-06 21:37:19 +00003984 }
3985
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003986 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003987 MI->eraseFromParent();
3988 return true;
3989}
3990
Craig Topper7ef6ea32016-12-05 04:51:31 +00003991unsigned X86FastISel::fastEmitInst_rrrr(unsigned MachineInstOpcode,
3992 const TargetRegisterClass *RC,
3993 unsigned Op0, bool Op0IsKill,
3994 unsigned Op1, bool Op1IsKill,
3995 unsigned Op2, bool Op2IsKill,
3996 unsigned Op3, bool Op3IsKill) {
3997 const MCInstrDesc &II = TII.get(MachineInstOpcode);
3998
3999 unsigned ResultReg = createResultReg(RC);
4000 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
4001 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
4002 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
4003 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 3);
4004
4005 if (II.getNumDefs() >= 1)
4006 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
4007 .addReg(Op0, getKillRegState(Op0IsKill))
4008 .addReg(Op1, getKillRegState(Op1IsKill))
4009 .addReg(Op2, getKillRegState(Op2IsKill))
4010 .addReg(Op3, getKillRegState(Op3IsKill));
4011 else {
4012 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
4013 .addReg(Op0, getKillRegState(Op0IsKill))
4014 .addReg(Op1, getKillRegState(Op1IsKill))
4015 .addReg(Op2, getKillRegState(Op2IsKill))
4016 .addReg(Op3, getKillRegState(Op3IsKill));
4017 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4018 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
4019 }
4020 return ResultReg;
4021}
4022
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00004023
4024namespace llvm {
4025 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
4026 const TargetLibraryInfo *libInfo) {
4027 return new X86FastISel(funcInfo, libInfo);
4028 }
4029}