blob: e5e089d07d55c8f24e4ca6fff99547a42983d86f [file] [log] [blame]
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the X86-specific support for the FastISel class. Much
10// of the target-specific code is generated by tablegen in the file
11// X86GenFastISel.inc, which is #included here.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86CallingConv.h"
17#include "X86InstrBuilder.h"
18#include "X86InstrInfo.h"
19#include "X86MachineFunctionInfo.h"
20#include "X86RegisterInfo.h"
21#include "X86Subtarget.h"
22#include "X86TargetMachine.h"
23#include "llvm/Analysis/BranchProbabilityInfo.h"
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000024#include "llvm/CodeGen/FastISel.h"
25#include "llvm/CodeGen/FunctionLoweringInfo.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/IR/CallSite.h"
30#include "llvm/IR/CallingConv.h"
Reid Kleckner28865802016-04-14 18:29:59 +000031#include "llvm/IR/DebugInfo.h"
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000032#include "llvm/IR/DerivedTypes.h"
33#include "llvm/IR/GetElementPtrTypeIterator.h"
34#include "llvm/IR/GlobalAlias.h"
35#include "llvm/IR/GlobalVariable.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/Operator.h"
David Majnemerca194852015-02-10 22:00:34 +000039#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000040#include "llvm/MC/MCSymbol.h"
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000041#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Target/TargetOptions.h"
43using namespace llvm;
44
45namespace {
46
47class X86FastISel final : public FastISel {
48 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
49 /// make the right decision when generating code for different targets.
50 const X86Subtarget *Subtarget;
51
52 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
53 /// floating point ops.
54 /// When SSE is available, use it for f32 operations.
55 /// When SSE2 is available, use it for f64 operations.
56 bool X86ScalarSSEf64;
57 bool X86ScalarSSEf32;
58
59public:
60 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
61 const TargetLibraryInfo *libInfo)
Eric Christophera1c535b2015-02-02 23:03:45 +000062 : FastISel(funcInfo, libInfo) {
63 Subtarget = &funcInfo.MF->getSubtarget<X86Subtarget>();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000064 X86ScalarSSEf64 = Subtarget->hasSSE2();
65 X86ScalarSSEf32 = Subtarget->hasSSE1();
66 }
67
68 bool fastSelectInstruction(const Instruction *I) override;
69
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000070 /// The specified machine instr operand is a vreg, and that
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000071 /// vreg is being provided by the specified load instruction. If possible,
72 /// try to fold the load as an operand to the instruction, returning true if
73 /// possible.
74 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
75 const LoadInst *LI) override;
76
77 bool fastLowerArguments() override;
78 bool fastLowerCall(CallLoweringInfo &CLI) override;
79 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
80
81#include "X86GenFastISel.inc"
82
83private:
Benjamin Kramerbdc49562016-06-12 15:39:02 +000084 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT,
85 const DebugLoc &DL);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000086
Craig Topper682cc092019-05-11 05:18:58 +000087 bool X86FastEmitLoad(MVT VT, X86AddressMode &AM, MachineMemOperand *MMO,
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +000088 unsigned &ResultReg, unsigned Alignment = 1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000089
Pete Cooperd0dae3e2015-05-05 23:41:53 +000090 bool X86FastEmitStore(EVT VT, const Value *Val, X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000091 MachineMemOperand *MMO = nullptr, bool Aligned = false);
92 bool X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
Pete Cooperd0dae3e2015-05-05 23:41:53 +000093 X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000094 MachineMemOperand *MMO = nullptr, bool Aligned = false);
95
96 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
97 unsigned &ResultReg);
98
99 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
100 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
101
102 bool X86SelectLoad(const Instruction *I);
103
104 bool X86SelectStore(const Instruction *I);
105
106 bool X86SelectRet(const Instruction *I);
107
108 bool X86SelectCmp(const Instruction *I);
109
110 bool X86SelectZExt(const Instruction *I);
111
Craig Topper619b7592017-09-02 18:53:46 +0000112 bool X86SelectSExt(const Instruction *I);
113
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000114 bool X86SelectBranch(const Instruction *I);
115
116 bool X86SelectShift(const Instruction *I);
117
118 bool X86SelectDivRem(const Instruction *I);
119
120 bool X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I);
121
122 bool X86FastEmitSSESelect(MVT RetVT, const Instruction *I);
123
124 bool X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I);
125
126 bool X86SelectSelect(const Instruction *I);
127
128 bool X86SelectTrunc(const Instruction *I);
129
Andrea Di Biagio62622d22015-02-10 12:04:41 +0000130 bool X86SelectFPExtOrFPTrunc(const Instruction *I, unsigned Opc,
131 const TargetRegisterClass *RC);
132
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000133 bool X86SelectFPExt(const Instruction *I);
134 bool X86SelectFPTrunc(const Instruction *I);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +0000135 bool X86SelectSIToFP(const Instruction *I);
Craig Topperda424ba2018-07-13 22:09:30 +0000136 bool X86SelectUIToFP(const Instruction *I);
137 bool X86SelectIntToFP(const Instruction *I, bool IsSigned);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000138
139 const X86InstrInfo *getInstrInfo() const {
Eric Christophera1c535b2015-02-02 23:03:45 +0000140 return Subtarget->getInstrInfo();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000141 }
142 const X86TargetMachine *getTargetMachine() const {
143 return static_cast<const X86TargetMachine *>(&TM);
144 }
145
146 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
147
148 unsigned X86MaterializeInt(const ConstantInt *CI, MVT VT);
149 unsigned X86MaterializeFP(const ConstantFP *CFP, MVT VT);
150 unsigned X86MaterializeGV(const GlobalValue *GV, MVT VT);
151 unsigned fastMaterializeConstant(const Constant *C) override;
152
153 unsigned fastMaterializeAlloca(const AllocaInst *C) override;
154
155 unsigned fastMaterializeFloatZero(const ConstantFP *CF) override;
156
157 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
158 /// computed in an SSE register, not on the X87 floating point stack.
159 bool isScalarFPTypeInSSEReg(EVT VT) const {
160 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
161 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
162 }
163
164 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
165
166 bool IsMemcpySmall(uint64_t Len);
167
168 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
169 X86AddressMode SrcAM, uint64_t Len);
170
171 bool foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
172 const Value *Cond);
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000173
174 const MachineInstrBuilder &addFullAddress(const MachineInstrBuilder &MIB,
175 X86AddressMode &AM);
Craig Topper7ef6ea32016-12-05 04:51:31 +0000176
177 unsigned fastEmitInst_rrrr(unsigned MachineInstOpcode,
178 const TargetRegisterClass *RC, unsigned Op0,
179 bool Op0IsKill, unsigned Op1, bool Op1IsKill,
180 unsigned Op2, bool Op2IsKill, unsigned Op3,
181 bool Op3IsKill);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000182};
183
184} // end anonymous namespace.
185
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000186static std::pair<unsigned, bool>
187getX86SSEConditionCode(CmpInst::Predicate Predicate) {
188 unsigned CC;
189 bool NeedSwap = false;
190
191 // SSE Condition code mapping:
192 // 0 - EQ
193 // 1 - LT
194 // 2 - LE
195 // 3 - UNORD
196 // 4 - NEQ
197 // 5 - NLT
198 // 6 - NLE
199 // 7 - ORD
200 switch (Predicate) {
201 default: llvm_unreachable("Unexpected predicate");
202 case CmpInst::FCMP_OEQ: CC = 0; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000203 case CmpInst::FCMP_OGT: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000204 case CmpInst::FCMP_OLT: CC = 1; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000205 case CmpInst::FCMP_OGE: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000206 case CmpInst::FCMP_OLE: CC = 2; break;
207 case CmpInst::FCMP_UNO: CC = 3; break;
208 case CmpInst::FCMP_UNE: CC = 4; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000209 case CmpInst::FCMP_ULE: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000210 case CmpInst::FCMP_UGE: CC = 5; break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000211 case CmpInst::FCMP_ULT: NeedSwap = true; LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000212 case CmpInst::FCMP_UGT: CC = 6; break;
213 case CmpInst::FCMP_ORD: CC = 7; break;
Craig Topper4f8656a2017-10-09 01:05:15 +0000214 case CmpInst::FCMP_UEQ: CC = 8; break;
215 case CmpInst::FCMP_ONE: CC = 12; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000216 }
217
218 return std::make_pair(CC, NeedSwap);
219}
220
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000221/// Adds a complex addressing mode to the given machine instr builder.
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000222/// Note, this will constrain the index register. If its not possible to
223/// constrain the given index register, then a new one will be created. The
224/// IndexReg field of the addressing mode will be updated to match in this case.
225const MachineInstrBuilder &
226X86FastISel::addFullAddress(const MachineInstrBuilder &MIB,
227 X86AddressMode &AM) {
228 // First constrain the index register. It needs to be a GR64_NOSP.
229 AM.IndexReg = constrainOperandRegClass(MIB->getDesc(), AM.IndexReg,
230 MIB->getNumOperands() +
231 X86::AddrIndexReg);
232 return ::addFullAddress(MIB, AM);
233}
234
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000235/// Check if it is possible to fold the condition from the XALU intrinsic
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000236/// into the user. The condition code will only be updated on success.
237bool X86FastISel::foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
238 const Value *Cond) {
239 if (!isa<ExtractValueInst>(Cond))
240 return false;
241
242 const auto *EV = cast<ExtractValueInst>(Cond);
243 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
244 return false;
245
246 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
247 MVT RetVT;
248 const Function *Callee = II->getCalledFunction();
249 Type *RetTy =
250 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
251 if (!isTypeLegal(RetTy, RetVT))
252 return false;
253
254 if (RetVT != MVT::i32 && RetVT != MVT::i64)
255 return false;
256
257 X86::CondCode TmpCC;
258 switch (II->getIntrinsicID()) {
259 default: return false;
260 case Intrinsic::sadd_with_overflow:
261 case Intrinsic::ssub_with_overflow:
262 case Intrinsic::smul_with_overflow:
263 case Intrinsic::umul_with_overflow: TmpCC = X86::COND_O; break;
264 case Intrinsic::uadd_with_overflow:
265 case Intrinsic::usub_with_overflow: TmpCC = X86::COND_B; break;
266 }
267
268 // Check if both instructions are in the same basic block.
269 if (II->getParent() != I->getParent())
270 return false;
271
272 // Make sure nothing is in the way
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000273 BasicBlock::const_iterator Start(I);
274 BasicBlock::const_iterator End(II);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000275 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
276 // We only expect extractvalue instructions between the intrinsic and the
277 // instruction to be selected.
278 if (!isa<ExtractValueInst>(Itr))
279 return false;
280
281 // Check that the extractvalue operand comes from the intrinsic.
282 const auto *EVI = cast<ExtractValueInst>(Itr);
283 if (EVI->getAggregateOperand() != II)
284 return false;
285 }
286
287 CC = TmpCC;
288 return true;
289}
290
291bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Rui Ueyama49a3ad22019-07-16 04:46:31 +0000292 EVT evt = TLI.getValueType(DL, Ty, /*AllowUnknown=*/true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000293 if (evt == MVT::Other || !evt.isSimple())
294 // Unhandled type. Halt "fast" selection and bail.
295 return false;
296
297 VT = evt.getSimpleVT();
298 // For now, require SSE/SSE2 for performing floating-point operations,
299 // since x87 requires additional work.
300 if (VT == MVT::f64 && !X86ScalarSSEf64)
301 return false;
302 if (VT == MVT::f32 && !X86ScalarSSEf32)
303 return false;
304 // Similarly, no f80 support yet.
305 if (VT == MVT::f80)
306 return false;
307 // We only handle legal types. For example, on x86-32 the instruction
308 // selector contains all of the 64-bit instructions from x86-64,
309 // under the assumption that i64 won't be used if the target doesn't
310 // support it.
311 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
312}
313
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000314/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
315/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
316/// Return true and the result register by reference if it is possible.
Craig Topper682cc092019-05-11 05:18:58 +0000317bool X86FastISel::X86FastEmitLoad(MVT VT, X86AddressMode &AM,
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000318 MachineMemOperand *MMO, unsigned &ResultReg,
319 unsigned Alignment) {
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000320 bool HasSSE41 = Subtarget->hasSSE41();
Craig Topperca9c0802016-06-02 04:19:45 +0000321 bool HasAVX = Subtarget->hasAVX();
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000322 bool HasAVX2 = Subtarget->hasAVX2();
Craig Topperdfc4fc92016-09-05 23:58:40 +0000323 bool HasAVX512 = Subtarget->hasAVX512();
324 bool HasVLX = Subtarget->hasVLX();
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000325 bool IsNonTemporal = MMO && MMO->isNonTemporal();
326
Craig Topper682cc092019-05-11 05:18:58 +0000327 // Treat i1 loads the same as i8 loads. Masking will be done when storing.
328 if (VT == MVT::i1)
329 VT = MVT::i8;
330
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000331 // Get opcode and regclass of the output for the given load instruction.
332 unsigned Opc = 0;
Craig Topper682cc092019-05-11 05:18:58 +0000333 switch (VT.SimpleTy) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000334 default: return false;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000335 case MVT::i8:
336 Opc = X86::MOV8rm;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000337 break;
338 case MVT::i16:
339 Opc = X86::MOV16rm;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000340 break;
341 case MVT::i32:
342 Opc = X86::MOV32rm;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000343 break;
344 case MVT::i64:
345 // Must be in x86-64 mode.
346 Opc = X86::MOV64rm;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000347 break;
348 case MVT::f32:
Craig Topper682cc092019-05-11 05:18:58 +0000349 if (X86ScalarSSEf32)
Craig Topper8582ecd2019-06-18 03:23:11 +0000350 Opc = HasAVX512 ? X86::VMOVSSZrm_alt :
351 HasAVX ? X86::VMOVSSrm_alt :
352 X86::MOVSSrm_alt;
Craig Topper682cc092019-05-11 05:18:58 +0000353 else
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000354 Opc = X86::LD_Fp32m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000355 break;
356 case MVT::f64:
Craig Topper682cc092019-05-11 05:18:58 +0000357 if (X86ScalarSSEf64)
Craig Topper8582ecd2019-06-18 03:23:11 +0000358 Opc = HasAVX512 ? X86::VMOVSDZrm_alt :
359 HasAVX ? X86::VMOVSDrm_alt :
360 X86::MOVSDrm_alt;
Craig Topper682cc092019-05-11 05:18:58 +0000361 else
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000362 Opc = X86::LD_Fp64m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000363 break;
364 case MVT::f80:
365 // No f80 support yet.
366 return false;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000367 case MVT::v4f32:
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000368 if (IsNonTemporal && Alignment >= 16 && HasSSE41)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000369 Opc = HasVLX ? X86::VMOVNTDQAZ128rm :
370 HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000371 else if (Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000372 Opc = HasVLX ? X86::VMOVAPSZ128rm :
373 HasAVX ? X86::VMOVAPSrm : X86::MOVAPSrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000374 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000375 Opc = HasVLX ? X86::VMOVUPSZ128rm :
376 HasAVX ? X86::VMOVUPSrm : X86::MOVUPSrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000377 break;
378 case MVT::v2f64:
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000379 if (IsNonTemporal && Alignment >= 16 && HasSSE41)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000380 Opc = HasVLX ? X86::VMOVNTDQAZ128rm :
381 HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000382 else if (Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000383 Opc = HasVLX ? X86::VMOVAPDZ128rm :
384 HasAVX ? X86::VMOVAPDrm : X86::MOVAPDrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000385 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000386 Opc = HasVLX ? X86::VMOVUPDZ128rm :
387 HasAVX ? X86::VMOVUPDrm : X86::MOVUPDrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000388 break;
389 case MVT::v4i32:
390 case MVT::v2i64:
391 case MVT::v8i16:
392 case MVT::v16i8:
Craig Topper31f7adb2019-05-11 04:19:33 +0000393 if (IsNonTemporal && Alignment >= 16 && HasSSE41)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000394 Opc = HasVLX ? X86::VMOVNTDQAZ128rm :
395 HasAVX ? X86::VMOVNTDQArm : X86::MOVNTDQArm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000396 else if (Alignment >= 16)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000397 Opc = HasVLX ? X86::VMOVDQA64Z128rm :
398 HasAVX ? X86::VMOVDQArm : X86::MOVDQArm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000399 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000400 Opc = HasVLX ? X86::VMOVDQU64Z128rm :
401 HasAVX ? X86::VMOVDQUrm : X86::MOVDQUrm;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +0000402 break;
Craig Topperca9c0802016-06-02 04:19:45 +0000403 case MVT::v8f32:
404 assert(HasAVX);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000405 if (IsNonTemporal && Alignment >= 32 && HasAVX2)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000406 Opc = HasVLX ? X86::VMOVNTDQAZ256rm : X86::VMOVNTDQAYrm;
Simon Pilgrimf7113fd2017-06-06 14:18:39 +0000407 else if (IsNonTemporal && Alignment >= 16)
408 return false; // Force split for X86::VMOVNTDQArm
Craig Topperdfc4fc92016-09-05 23:58:40 +0000409 else if (Alignment >= 32)
410 Opc = HasVLX ? X86::VMOVAPSZ256rm : X86::VMOVAPSYrm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000411 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000412 Opc = HasVLX ? X86::VMOVUPSZ256rm : X86::VMOVUPSYrm;
Craig Topperca9c0802016-06-02 04:19:45 +0000413 break;
414 case MVT::v4f64:
415 assert(HasAVX);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000416 if (IsNonTemporal && Alignment >= 32 && HasAVX2)
Craig Topper728fa7b2017-10-27 20:13:10 +0000417 Opc = HasVLX ? X86::VMOVNTDQAZ256rm : X86::VMOVNTDQAYrm;
Simon Pilgrimf7113fd2017-06-06 14:18:39 +0000418 else if (IsNonTemporal && Alignment >= 16)
419 return false; // Force split for X86::VMOVNTDQArm
Craig Topperdfc4fc92016-09-05 23:58:40 +0000420 else if (Alignment >= 32)
421 Opc = HasVLX ? X86::VMOVAPDZ256rm : X86::VMOVAPDYrm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000422 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000423 Opc = HasVLX ? X86::VMOVUPDZ256rm : X86::VMOVUPDYrm;
Craig Topperca9c0802016-06-02 04:19:45 +0000424 break;
425 case MVT::v8i32:
426 case MVT::v4i64:
427 case MVT::v16i16:
428 case MVT::v32i8:
429 assert(HasAVX);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000430 if (IsNonTemporal && Alignment >= 32 && HasAVX2)
Craig Topper728fa7b2017-10-27 20:13:10 +0000431 Opc = HasVLX ? X86::VMOVNTDQAZ256rm : X86::VMOVNTDQAYrm;
Simon Pilgrimf7113fd2017-06-06 14:18:39 +0000432 else if (IsNonTemporal && Alignment >= 16)
433 return false; // Force split for X86::VMOVNTDQArm
Craig Topperdfc4fc92016-09-05 23:58:40 +0000434 else if (Alignment >= 32)
435 Opc = HasVLX ? X86::VMOVDQA64Z256rm : X86::VMOVDQAYrm;
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000436 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000437 Opc = HasVLX ? X86::VMOVDQU64Z256rm : X86::VMOVDQUYrm;
Craig Topperca9c0802016-06-02 04:19:45 +0000438 break;
Craig Topper048a08a2016-06-02 04:51:37 +0000439 case MVT::v16f32:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000440 assert(HasAVX512);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000441 if (IsNonTemporal && Alignment >= 64)
442 Opc = X86::VMOVNTDQAZrm;
443 else
444 Opc = (Alignment >= 64) ? X86::VMOVAPSZrm : X86::VMOVUPSZrm;
Craig Topper048a08a2016-06-02 04:51:37 +0000445 break;
446 case MVT::v8f64:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000447 assert(HasAVX512);
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000448 if (IsNonTemporal && Alignment >= 64)
449 Opc = X86::VMOVNTDQAZrm;
450 else
451 Opc = (Alignment >= 64) ? X86::VMOVAPDZrm : X86::VMOVUPDZrm;
Craig Topper048a08a2016-06-02 04:51:37 +0000452 break;
453 case MVT::v8i64:
454 case MVT::v16i32:
455 case MVT::v32i16:
456 case MVT::v64i8:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000457 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000458 // Note: There are a lot more choices based on type with AVX-512, but
459 // there's really no advantage when the load isn't masked.
Simon Pilgrim35c06a02016-06-07 13:47:23 +0000460 if (IsNonTemporal && Alignment >= 64)
461 Opc = X86::VMOVNTDQAZrm;
462 else
463 Opc = (Alignment >= 64) ? X86::VMOVDQA64Zrm : X86::VMOVDQU64Zrm;
Craig Topper048a08a2016-06-02 04:51:37 +0000464 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000465 }
466
Craig Topper682cc092019-05-11 05:18:58 +0000467 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
468
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000469 ResultReg = createResultReg(RC);
470 MachineInstrBuilder MIB =
471 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
472 addFullAddress(MIB, AM);
473 if (MMO)
474 MIB->addMemOperand(*FuncInfo.MF, MMO);
475 return true;
476}
477
478/// X86FastEmitStore - Emit a machine instruction to store a value Val of
479/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
480/// and a displacement offset, or a GlobalAddress,
481/// i.e. V. Return true if it is possible.
482bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000483 X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000484 MachineMemOperand *MMO, bool Aligned) {
Simon Pilgrimb6702ea2017-04-10 16:58:07 +0000485 bool HasSSE1 = Subtarget->hasSSE1();
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000486 bool HasSSE2 = Subtarget->hasSSE2();
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000487 bool HasSSE4A = Subtarget->hasSSE4A();
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000488 bool HasAVX = Subtarget->hasAVX();
Craig Topperdfc4fc92016-09-05 23:58:40 +0000489 bool HasAVX512 = Subtarget->hasAVX512();
490 bool HasVLX = Subtarget->hasVLX();
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000491 bool IsNonTemporal = MMO && MMO->isNonTemporal();
492
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000493 // Get opcode and regclass of the output for the given store instruction.
494 unsigned Opc = 0;
495 switch (VT.getSimpleVT().SimpleTy) {
496 case MVT::f80: // No f80 support yet.
497 default: return false;
498 case MVT::i1: {
499 // Mask out all but lowest bit.
500 unsigned AndResult = createResultReg(&X86::GR8RegClass);
501 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
502 TII.get(X86::AND8ri), AndResult)
503 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
504 ValReg = AndResult;
Justin Bognerb03fd122016-08-17 05:10:15 +0000505 LLVM_FALLTHROUGH; // handle i1 as i8.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000506 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000507 case MVT::i8: Opc = X86::MOV8mr; break;
508 case MVT::i16: Opc = X86::MOV16mr; break;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000509 case MVT::i32:
510 Opc = (IsNonTemporal && HasSSE2) ? X86::MOVNTImr : X86::MOV32mr;
511 break;
512 case MVT::i64:
513 // Must be in x86-64 mode.
514 Opc = (IsNonTemporal && HasSSE2) ? X86::MOVNTI_64mr : X86::MOV64mr;
515 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000516 case MVT::f32:
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000517 if (X86ScalarSSEf32) {
518 if (IsNonTemporal && HasSSE4A)
519 Opc = X86::MOVNTSS;
520 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000521 Opc = HasAVX512 ? X86::VMOVSSZmr :
522 HasAVX ? X86::VMOVSSmr : X86::MOVSSmr;
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000523 } else
524 Opc = X86::ST_Fp32m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000525 break;
526 case MVT::f64:
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000527 if (X86ScalarSSEf32) {
528 if (IsNonTemporal && HasSSE4A)
529 Opc = X86::MOVNTSD;
530 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000531 Opc = HasAVX512 ? X86::VMOVSDZmr :
532 HasAVX ? X86::VMOVSDmr : X86::MOVSDmr;
Simon Pilgrim5b65f282015-10-17 13:04:42 +0000533 } else
534 Opc = X86::ST_Fp64m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000535 break;
Simon Pilgrimb6702ea2017-04-10 16:58:07 +0000536 case MVT::x86mmx:
537 Opc = (IsNonTemporal && HasSSE1) ? X86::MMX_MOVNTQmr : X86::MMX_MOVQ64mr;
538 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000539 case MVT::v4f32:
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000540 if (Aligned) {
541 if (IsNonTemporal)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000542 Opc = HasVLX ? X86::VMOVNTPSZ128mr :
543 HasAVX ? X86::VMOVNTPSmr : X86::MOVNTPSmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000544 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000545 Opc = HasVLX ? X86::VMOVAPSZ128mr :
546 HasAVX ? X86::VMOVAPSmr : X86::MOVAPSmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000547 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000548 Opc = HasVLX ? X86::VMOVUPSZ128mr :
549 HasAVX ? X86::VMOVUPSmr : X86::MOVUPSmr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000550 break;
551 case MVT::v2f64:
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000552 if (Aligned) {
553 if (IsNonTemporal)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000554 Opc = HasVLX ? X86::VMOVNTPDZ128mr :
555 HasAVX ? X86::VMOVNTPDmr : X86::MOVNTPDmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000556 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000557 Opc = HasVLX ? X86::VMOVAPDZ128mr :
558 HasAVX ? X86::VMOVAPDmr : X86::MOVAPDmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000559 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000560 Opc = HasVLX ? X86::VMOVUPDZ128mr :
561 HasAVX ? X86::VMOVUPDmr : X86::MOVUPDmr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000562 break;
563 case MVT::v4i32:
564 case MVT::v2i64:
565 case MVT::v8i16:
566 case MVT::v16i8:
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000567 if (Aligned) {
568 if (IsNonTemporal)
Craig Topperdfc4fc92016-09-05 23:58:40 +0000569 Opc = HasVLX ? X86::VMOVNTDQZ128mr :
570 HasAVX ? X86::VMOVNTDQmr : X86::MOVNTDQmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000571 else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000572 Opc = HasVLX ? X86::VMOVDQA64Z128mr :
573 HasAVX ? X86::VMOVDQAmr : X86::MOVDQAmr;
Andrea Di Biagioc47edbe2015-10-14 10:03:13 +0000574 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000575 Opc = HasVLX ? X86::VMOVDQU64Z128mr :
576 HasAVX ? X86::VMOVDQUmr : X86::MOVDQUmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000577 break;
578 case MVT::v8f32:
579 assert(HasAVX);
Craig Topperdfc4fc92016-09-05 23:58:40 +0000580 if (Aligned) {
581 if (IsNonTemporal)
582 Opc = HasVLX ? X86::VMOVNTPSZ256mr : X86::VMOVNTPSYmr;
583 else
584 Opc = HasVLX ? X86::VMOVAPSZ256mr : X86::VMOVAPSYmr;
585 } else
586 Opc = HasVLX ? X86::VMOVUPSZ256mr : X86::VMOVUPSYmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000587 break;
588 case MVT::v4f64:
589 assert(HasAVX);
590 if (Aligned) {
Craig Topperdfc4fc92016-09-05 23:58:40 +0000591 if (IsNonTemporal)
592 Opc = HasVLX ? X86::VMOVNTPDZ256mr : X86::VMOVNTPDYmr;
593 else
594 Opc = HasVLX ? X86::VMOVAPDZ256mr : X86::VMOVAPDYmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000595 } else
Craig Topperdfc4fc92016-09-05 23:58:40 +0000596 Opc = HasVLX ? X86::VMOVUPDZ256mr : X86::VMOVUPDYmr;
Craig Topperca9c0802016-06-02 04:19:45 +0000597 break;
598 case MVT::v8i32:
599 case MVT::v4i64:
600 case MVT::v16i16:
601 case MVT::v32i8:
602 assert(HasAVX);
Craig Topperdfc4fc92016-09-05 23:58:40 +0000603 if (Aligned) {
604 if (IsNonTemporal)
605 Opc = HasVLX ? X86::VMOVNTDQZ256mr : X86::VMOVNTDQYmr;
606 else
607 Opc = HasVLX ? X86::VMOVDQA64Z256mr : X86::VMOVDQAYmr;
608 } else
609 Opc = HasVLX ? X86::VMOVDQU64Z256mr : X86::VMOVDQUYmr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000610 break;
Craig Topper048a08a2016-06-02 04:51:37 +0000611 case MVT::v16f32:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000612 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000613 if (Aligned)
614 Opc = IsNonTemporal ? X86::VMOVNTPSZmr : X86::VMOVAPSZmr;
615 else
616 Opc = X86::VMOVUPSZmr;
617 break;
618 case MVT::v8f64:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000619 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000620 if (Aligned) {
621 Opc = IsNonTemporal ? X86::VMOVNTPDZmr : X86::VMOVAPDZmr;
622 } else
623 Opc = X86::VMOVUPDZmr;
624 break;
625 case MVT::v8i64:
626 case MVT::v16i32:
627 case MVT::v32i16:
628 case MVT::v64i8:
Craig Topperdfc4fc92016-09-05 23:58:40 +0000629 assert(HasAVX512);
Craig Topper048a08a2016-06-02 04:51:37 +0000630 // Note: There are a lot more choices based on type with AVX-512, but
631 // there's really no advantage when the store isn't masked.
632 if (Aligned)
633 Opc = IsNonTemporal ? X86::VMOVNTDQZmr : X86::VMOVDQA64Zmr;
634 else
635 Opc = X86::VMOVDQU64Zmr;
636 break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000637 }
638
Quentin Colombetbf200682016-04-27 22:33:42 +0000639 const MCInstrDesc &Desc = TII.get(Opc);
640 // Some of the instructions in the previous switch use FR128 instead
641 // of FR32 for ValReg. Make sure the register we feed the instruction
642 // matches its register class constraints.
643 // Note: This is fine to do a copy from FR32 to FR128, this is the
644 // same registers behind the scene and actually why it did not trigger
645 // any bugs before.
646 ValReg = constrainOperandRegClass(Desc, ValReg, Desc.getNumOperands() - 1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000647 MachineInstrBuilder MIB =
Quentin Colombetbf200682016-04-27 22:33:42 +0000648 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, Desc);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000649 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
650 if (MMO)
651 MIB->addMemOperand(*FuncInfo.MF, MMO);
652
653 return true;
654}
655
656bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Pete Cooperd0dae3e2015-05-05 23:41:53 +0000657 X86AddressMode &AM,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000658 MachineMemOperand *MMO, bool Aligned) {
659 // Handle 'null' like i32/i64 0.
660 if (isa<ConstantPointerNull>(Val))
661 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
662
663 // If this is a store of a simple constant, fold the constant into the store.
664 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
665 unsigned Opc = 0;
666 bool Signed = true;
667 switch (VT.getSimpleVT().SimpleTy) {
668 default: break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000669 case MVT::i1:
670 Signed = false;
671 LLVM_FALLTHROUGH; // Handle as i8.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000672 case MVT::i8: Opc = X86::MOV8mi; break;
673 case MVT::i16: Opc = X86::MOV16mi; break;
674 case MVT::i32: Opc = X86::MOV32mi; break;
675 case MVT::i64:
676 // Must be a 32-bit sign extended value.
677 if (isInt<32>(CI->getSExtValue()))
678 Opc = X86::MOV64mi32;
679 break;
680 }
681
682 if (Opc) {
683 MachineInstrBuilder MIB =
684 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
685 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
686 : CI->getZExtValue());
687 if (MMO)
688 MIB->addMemOperand(*FuncInfo.MF, MMO);
689 return true;
690 }
691 }
692
693 unsigned ValReg = getRegForValue(Val);
694 if (ValReg == 0)
695 return false;
696
697 bool ValKill = hasTrivialKill(Val);
698 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
699}
700
701/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
702/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
703/// ISD::SIGN_EXTEND).
704bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
705 unsigned Src, EVT SrcVT,
706 unsigned &ResultReg) {
707 unsigned RR = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
708 Src, /*TODO: Kill=*/false);
709 if (RR == 0)
710 return false;
711
712 ResultReg = RR;
713 return true;
714}
715
716bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
717 // Handle constant address.
718 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
719 // Can't handle alternate code models yet.
720 if (TM.getCodeModel() != CodeModel::Small)
721 return false;
722
723 // Can't handle TLS yet.
724 if (GV->isThreadLocal())
725 return false;
726
Vlad Tsyrklevichab016e02018-08-01 17:44:37 +0000727 // Can't handle !absolute_symbol references yet.
728 if (GV->isAbsoluteSymbolRef())
729 return false;
730
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000731 // RIP-relative addresses can't have additional register operands, so if
732 // we've already folded stuff into the addressing mode, just force the
733 // global value into its own register, which we can use as the basereg.
734 if (!Subtarget->isPICStyleRIPRel() ||
735 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
736 // Okay, we've committed to selecting this global. Set up the address.
737 AM.GV = GV;
738
739 // Allow the subtarget to classify the global.
Rafael Espindolaab03eb02016-05-19 22:07:57 +0000740 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000741
742 // If this reference is relative to the pic base, set it now.
743 if (isGlobalRelativeToPICBase(GVFlags)) {
744 // FIXME: How do we know Base.Reg is free??
745 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
746 }
747
748 // Unless the ABI requires an extra load, return a direct reference to
749 // the global.
750 if (!isGlobalStubReference(GVFlags)) {
751 if (Subtarget->isPICStyleRIPRel()) {
752 // Use rip-relative addressing if we can. Above we verified that the
753 // base and index registers are unused.
754 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
755 AM.Base.Reg = X86::RIP;
756 }
757 AM.GVOpFlags = GVFlags;
758 return true;
759 }
760
761 // Ok, we need to do a load from a stub. If we've already loaded from
762 // this stub, reuse the loaded pointer, otherwise emit the load now.
763 DenseMap<const Value *, unsigned>::iterator I = LocalValueMap.find(V);
764 unsigned LoadReg;
765 if (I != LocalValueMap.end() && I->second != 0) {
766 LoadReg = I->second;
767 } else {
768 // Issue load from stub.
769 unsigned Opc = 0;
770 const TargetRegisterClass *RC = nullptr;
771 X86AddressMode StubAM;
772 StubAM.Base.Reg = AM.Base.Reg;
773 StubAM.GV = GV;
774 StubAM.GVOpFlags = GVFlags;
775
776 // Prepare for inserting code in the local-value area.
777 SavePoint SaveInsertPt = enterLocalValueArea();
778
Mehdi Amini44ede332015-07-09 02:09:04 +0000779 if (TLI.getPointerTy(DL) == MVT::i64) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000780 Opc = X86::MOV64rm;
781 RC = &X86::GR64RegClass;
782
783 if (Subtarget->isPICStyleRIPRel())
784 StubAM.Base.Reg = X86::RIP;
785 } else {
786 Opc = X86::MOV32rm;
787 RC = &X86::GR32RegClass;
788 }
789
790 LoadReg = createResultReg(RC);
791 MachineInstrBuilder LoadMI =
792 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
793 addFullAddress(LoadMI, StubAM);
794
795 // Ok, back to normal mode.
796 leaveLocalValueArea(SaveInsertPt);
797
798 // Prevent loading GV stub multiple times in same MBB.
799 LocalValueMap[V] = LoadReg;
800 }
801
802 // Now construct the final address. Note that the Disp, Scale,
803 // and Index values may already be set here.
804 AM.Base.Reg = LoadReg;
805 AM.GV = nullptr;
806 return true;
807 }
808 }
809
810 // If all else fails, try to materialize the value in a register.
811 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
812 if (AM.Base.Reg == 0) {
813 AM.Base.Reg = getRegForValue(V);
814 return AM.Base.Reg != 0;
815 }
816 if (AM.IndexReg == 0) {
817 assert(AM.Scale == 1 && "Scale with no index!");
818 AM.IndexReg = getRegForValue(V);
819 return AM.IndexReg != 0;
820 }
821 }
822
823 return false;
824}
825
826/// X86SelectAddress - Attempt to fill in an address from the given value.
827///
828bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
829 SmallVector<const Value *, 32> GEPs;
830redo_gep:
831 const User *U = nullptr;
832 unsigned Opcode = Instruction::UserOp1;
833 if (const Instruction *I = dyn_cast<Instruction>(V)) {
834 // Don't walk into other basic blocks; it's possible we haven't
835 // visited them yet, so the instructions may not yet be assigned
836 // virtual registers.
837 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
838 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
839 Opcode = I->getOpcode();
840 U = I;
841 }
842 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
843 Opcode = C->getOpcode();
844 U = C;
845 }
846
847 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
848 if (Ty->getAddressSpace() > 255)
849 // Fast instruction selection doesn't support the special
850 // address spaces.
851 return false;
852
853 switch (Opcode) {
854 default: break;
855 case Instruction::BitCast:
856 // Look past bitcasts.
857 return X86SelectAddress(U->getOperand(0), AM);
858
859 case Instruction::IntToPtr:
860 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000861 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
862 TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000863 return X86SelectAddress(U->getOperand(0), AM);
864 break;
865
866 case Instruction::PtrToInt:
867 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000868 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000869 return X86SelectAddress(U->getOperand(0), AM);
870 break;
871
872 case Instruction::Alloca: {
873 // Do static allocas.
874 const AllocaInst *A = cast<AllocaInst>(V);
875 DenseMap<const AllocaInst *, int>::iterator SI =
876 FuncInfo.StaticAllocaMap.find(A);
877 if (SI != FuncInfo.StaticAllocaMap.end()) {
878 AM.BaseType = X86AddressMode::FrameIndexBase;
879 AM.Base.FrameIndex = SI->second;
880 return true;
881 }
882 break;
883 }
884
885 case Instruction::Add: {
886 // Adds of constants are common and easy enough.
887 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
888 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
889 // They have to fit in the 32-bit signed displacement field though.
890 if (isInt<32>(Disp)) {
891 AM.Disp = (uint32_t)Disp;
892 return X86SelectAddress(U->getOperand(0), AM);
893 }
894 }
895 break;
896 }
897
898 case Instruction::GetElementPtr: {
899 X86AddressMode SavedAM = AM;
900
901 // Pattern-match simple GEPs.
902 uint64_t Disp = (int32_t)AM.Disp;
903 unsigned IndexReg = AM.IndexReg;
904 unsigned Scale = AM.Scale;
905 gep_type_iterator GTI = gep_type_begin(U);
906 // Iterate through the indices, folding what we can. Constants can be
907 // folded, and one dynamic index can be handled, if the scale is supported.
908 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
909 i != e; ++i, ++GTI) {
910 const Value *Op = *i;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000911 if (StructType *STy = GTI.getStructTypeOrNull()) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000912 const StructLayout *SL = DL.getStructLayout(STy);
913 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
914 continue;
915 }
916
917 // A array/variable index is always of the form i*S where S is the
918 // constant scale size. See if we can push the scale into immediates.
919 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
920 for (;;) {
921 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
922 // Constant-offset addressing.
923 Disp += CI->getSExtValue() * S;
924 break;
925 }
926 if (canFoldAddIntoGEP(U, Op)) {
927 // A compatible add with a constant operand. Fold the constant.
928 ConstantInt *CI =
929 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
930 Disp += CI->getSExtValue() * S;
931 // Iterate on the other operand.
932 Op = cast<AddOperator>(Op)->getOperand(0);
933 continue;
934 }
935 if (IndexReg == 0 &&
936 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
937 (S == 1 || S == 2 || S == 4 || S == 8)) {
938 // Scaled-index addressing.
939 Scale = S;
940 IndexReg = getRegForGEPIndex(Op).first;
941 if (IndexReg == 0)
942 return false;
943 break;
944 }
945 // Unsupported.
946 goto unsupported_gep;
947 }
948 }
949
950 // Check for displacement overflow.
951 if (!isInt<32>(Disp))
952 break;
953
954 AM.IndexReg = IndexReg;
955 AM.Scale = Scale;
956 AM.Disp = (uint32_t)Disp;
957 GEPs.push_back(V);
958
959 if (const GetElementPtrInst *GEP =
960 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
961 // Ok, the GEP indices were covered by constant-offset and scaled-index
962 // addressing. Update the address state and move on to examining the base.
963 V = GEP;
964 goto redo_gep;
965 } else if (X86SelectAddress(U->getOperand(0), AM)) {
966 return true;
967 }
968
969 // If we couldn't merge the gep value into this addr mode, revert back to
970 // our address and just match the value instead of completely failing.
971 AM = SavedAM;
972
David Majnemerd7708772016-06-24 04:05:21 +0000973 for (const Value *I : reverse(GEPs))
974 if (handleConstantAddresses(I, AM))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000975 return true;
976
977 return false;
978 unsupported_gep:
979 // Ok, the GEP indices weren't all covered.
980 break;
981 }
982 }
983
984 return handleConstantAddresses(V, AM);
985}
986
987/// X86SelectCallAddress - Attempt to fill in an address from the given value.
988///
989bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
990 const User *U = nullptr;
991 unsigned Opcode = Instruction::UserOp1;
992 const Instruction *I = dyn_cast<Instruction>(V);
993 // Record if the value is defined in the same basic block.
994 //
995 // This information is crucial to know whether or not folding an
996 // operand is valid.
997 // Indeed, FastISel generates or reuses a virtual register for all
998 // operands of all instructions it selects. Obviously, the definition and
999 // its uses must use the same virtual register otherwise the produced
1000 // code is incorrect.
1001 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
1002 // registers for values that are alive across basic blocks. This ensures
1003 // that the values are consistently set between across basic block, even
1004 // if different instruction selection mechanisms are used (e.g., a mix of
1005 // SDISel and FastISel).
1006 // For values local to a basic block, the instruction selection process
1007 // generates these virtual registers with whatever method is appropriate
1008 // for its needs. In particular, FastISel and SDISel do not share the way
1009 // local virtual registers are set.
1010 // Therefore, this is impossible (or at least unsafe) to share values
1011 // between basic blocks unless they use the same instruction selection
1012 // method, which is not guarantee for X86.
1013 // Moreover, things like hasOneUse could not be used accurately, if we
1014 // allow to reference values across basic blocks whereas they are not
1015 // alive across basic blocks initially.
1016 bool InMBB = true;
1017 if (I) {
1018 Opcode = I->getOpcode();
1019 U = I;
1020 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
1021 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
1022 Opcode = C->getOpcode();
1023 U = C;
1024 }
1025
1026 switch (Opcode) {
1027 default: break;
1028 case Instruction::BitCast:
1029 // Look past bitcasts if its operand is in the same BB.
1030 if (InMBB)
1031 return X86SelectCallAddress(U->getOperand(0), AM);
1032 break;
1033
1034 case Instruction::IntToPtr:
1035 // Look past no-op inttoptrs if its operand is in the same BB.
1036 if (InMBB &&
Mehdi Amini44ede332015-07-09 02:09:04 +00001037 TLI.getValueType(DL, U->getOperand(0)->getType()) ==
1038 TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001039 return X86SelectCallAddress(U->getOperand(0), AM);
1040 break;
1041
1042 case Instruction::PtrToInt:
1043 // Look past no-op ptrtoints if its operand is in the same BB.
Mehdi Amini44ede332015-07-09 02:09:04 +00001044 if (InMBB && TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001045 return X86SelectCallAddress(U->getOperand(0), AM);
1046 break;
1047 }
1048
1049 // Handle constant address.
1050 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1051 // Can't handle alternate code models yet.
1052 if (TM.getCodeModel() != CodeModel::Small)
1053 return false;
1054
1055 // RIP-relative addresses can't have additional register operands.
1056 if (Subtarget->isPICStyleRIPRel() &&
1057 (AM.Base.Reg != 0 || AM.IndexReg != 0))
1058 return false;
1059
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001060 // Can't handle TLS.
1061 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1062 if (GVar->isThreadLocal())
1063 return false;
1064
1065 // Okay, we've committed to selecting this global. Set up the basic address.
1066 AM.GV = GV;
1067
Reid Kleckner7662d502017-08-05 00:10:43 +00001068 // Return a direct reference to the global. Fastisel can handle calls to
1069 // functions that require loads, such as dllimport and nonlazybind
1070 // functions.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001071 if (Subtarget->isPICStyleRIPRel()) {
1072 // Use rip-relative addressing if we can. Above we verified that the
1073 // base and index registers are unused.
1074 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
1075 AM.Base.Reg = X86::RIP;
Rafael Espindolac7e98132016-05-20 12:20:10 +00001076 } else {
1077 AM.GVOpFlags = Subtarget->classifyLocalReference(nullptr);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001078 }
1079
1080 return true;
1081 }
1082
1083 // If all else fails, try to materialize the value in a register.
1084 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
1085 if (AM.Base.Reg == 0) {
1086 AM.Base.Reg = getRegForValue(V);
1087 return AM.Base.Reg != 0;
1088 }
1089 if (AM.IndexReg == 0) {
1090 assert(AM.Scale == 1 && "Scale with no index!");
1091 AM.IndexReg = getRegForValue(V);
1092 return AM.IndexReg != 0;
1093 }
1094 }
1095
1096 return false;
1097}
1098
1099
1100/// X86SelectStore - Select and emit code to implement store instructions.
1101bool X86FastISel::X86SelectStore(const Instruction *I) {
1102 // Atomic stores need special handling.
1103 const StoreInst *S = cast<StoreInst>(I);
1104
1105 if (S->isAtomic())
1106 return false;
1107
Manman Ren57518142016-04-11 21:08:06 +00001108 const Value *PtrV = I->getOperand(1);
1109 if (TLI.supportSwiftError()) {
1110 // Swifterror values can come from either a function parameter with
1111 // swifterror attribute or an alloca with swifterror attribute.
1112 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
1113 if (Arg->hasSwiftErrorAttr())
1114 return false;
1115 }
1116
1117 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
1118 if (Alloca->isSwiftError())
1119 return false;
1120 }
1121 }
1122
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001123 const Value *Val = S->getValueOperand();
1124 const Value *Ptr = S->getPointerOperand();
1125
1126 MVT VT;
1127 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
1128 return false;
1129
1130 unsigned Alignment = S->getAlignment();
1131 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
1132 if (Alignment == 0) // Ensure that codegen never sees alignment 0
1133 Alignment = ABIAlignment;
1134 bool Aligned = Alignment >= ABIAlignment;
1135
1136 X86AddressMode AM;
1137 if (!X86SelectAddress(Ptr, AM))
1138 return false;
1139
1140 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
1141}
1142
1143/// X86SelectRet - Select and emit code to implement ret instructions.
1144bool X86FastISel::X86SelectRet(const Instruction *I) {
1145 const ReturnInst *Ret = cast<ReturnInst>(I);
1146 const Function &F = *I->getParent()->getParent();
1147 const X86MachineFunctionInfo *X86MFInfo =
1148 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
1149
1150 if (!FuncInfo.CanLowerReturn)
1151 return false;
1152
Manman Ren57518142016-04-11 21:08:06 +00001153 if (TLI.supportSwiftError() &&
1154 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError))
1155 return false;
1156
Manman Rened967f32016-01-12 01:08:46 +00001157 if (TLI.supportSplitCSR(FuncInfo.MF))
1158 return false;
1159
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001160 CallingConv::ID CC = F.getCallingConv();
1161 if (CC != CallingConv::C &&
1162 CC != CallingConv::Fast &&
Reid Klecknerf9b67b82019-10-07 22:28:58 +00001163 CC != CallingConv::Tail &&
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001164 CC != CallingConv::X86_FastCall &&
Nico Weberecdf45b2016-07-14 13:54:26 +00001165 CC != CallingConv::X86_StdCall &&
Nico Weberc7bf6462016-07-12 01:30:35 +00001166 CC != CallingConv::X86_ThisCall &&
Nico Weber8d66df12016-07-15 20:18:37 +00001167 CC != CallingConv::X86_64_SysV &&
Martin Storsjo2f24e932017-07-17 20:05:19 +00001168 CC != CallingConv::Win64)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001169 return false;
1170
Nico Weberc7bf6462016-07-12 01:30:35 +00001171 // Don't handle popping bytes if they don't fit the ret's immediate.
1172 if (!isUInt<16>(X86MFInfo->getBytesToPopOnReturn()))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001173 return false;
1174
1175 // fastcc with -tailcallopt is intended to provide a guaranteed
1176 // tail call optimization. Fastisel doesn't know how to do that.
Reid Klecknerf9b67b82019-10-07 22:28:58 +00001177 if ((CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt) ||
1178 CC == CallingConv::Tail)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001179 return false;
1180
1181 // Let SDISel handle vararg functions.
1182 if (F.isVarArg())
1183 return false;
1184
1185 // Build a list of return value registers.
1186 SmallVector<unsigned, 4> RetRegs;
1187
1188 if (Ret->getNumOperands() > 0) {
1189 SmallVector<ISD::OutputArg, 4> Outs;
Matt Arsenault81920b02018-07-28 13:25:19 +00001190 GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001191
1192 // Analyze operands of the call, assigning locations to each operand.
1193 SmallVector<CCValAssign, 16> ValLocs;
1194 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
1195 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
1196
1197 const Value *RV = Ret->getOperand(0);
1198 unsigned Reg = getRegForValue(RV);
1199 if (Reg == 0)
1200 return false;
1201
1202 // Only handle a single return value for now.
1203 if (ValLocs.size() != 1)
1204 return false;
1205
1206 CCValAssign &VA = ValLocs[0];
1207
1208 // Don't bother handling odd stuff for now.
1209 if (VA.getLocInfo() != CCValAssign::Full)
1210 return false;
1211 // Only handle register returns for now.
1212 if (!VA.isRegLoc())
1213 return false;
1214
1215 // The calling-convention tables for x87 returns don't tell
1216 // the whole story.
1217 if (VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1)
1218 return false;
1219
1220 unsigned SrcReg = Reg + VA.getValNo();
Mehdi Amini44ede332015-07-09 02:09:04 +00001221 EVT SrcVT = TLI.getValueType(DL, RV->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001222 EVT DstVT = VA.getValVT();
1223 // Special handling for extended integers.
1224 if (SrcVT != DstVT) {
1225 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
1226 return false;
1227
1228 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1229 return false;
1230
1231 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
1232
1233 if (SrcVT == MVT::i1) {
1234 if (Outs[0].Flags.isSExt())
1235 return false;
1236 SrcReg = fastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
1237 SrcVT = MVT::i8;
1238 }
1239 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
1240 ISD::SIGN_EXTEND;
1241 SrcReg = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
1242 SrcReg, /*TODO: Kill=*/false);
1243 }
1244
1245 // Make the copy.
Daniel Sanders0c476112019-08-15 19:22:08 +00001246 Register DstReg = VA.getLocReg();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001247 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
1248 // Avoid a cross-class copy. This is very unlikely.
1249 if (!SrcRC->contains(DstReg))
1250 return false;
1251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1252 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
1253
1254 // Add register to return instruction.
1255 RetRegs.push_back(VA.getLocReg());
1256 }
1257
Manman Ren1c3f65a2016-04-26 18:08:06 +00001258 // Swift calling convention does not require we copy the sret argument
1259 // into %rax/%eax for the return, and SRetReturnReg is not set for Swift.
1260
Dimitry Andric227b9282016-01-03 17:22:03 +00001261 // All x86 ABIs require that for returning structs by value we copy
1262 // the sret argument into %rax/%eax (depending on ABI) for the return.
1263 // We saved the argument into a virtual register in the entry block,
Michael Kuperstein2ea81ba2015-12-28 14:39:21 +00001264 // so now we copy the value out and into %rax/%eax.
Manman Ren1c3f65a2016-04-26 18:08:06 +00001265 if (F.hasStructRetAttr() && CC != CallingConv::Swift) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001266 unsigned Reg = X86MFInfo->getSRetReturnReg();
1267 assert(Reg &&
1268 "SRetReturnReg should have been set in LowerFormalArguments()!");
Craig Toppercc9efaf2018-09-11 17:57:23 +00001269 unsigned RetReg = Subtarget->isTarget64BitLP64() ? X86::RAX : X86::EAX;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001270 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1271 TII.get(TargetOpcode::COPY), RetReg).addReg(Reg);
1272 RetRegs.push_back(RetReg);
1273 }
1274
1275 // Now emit the RET.
Nico Weberc7bf6462016-07-12 01:30:35 +00001276 MachineInstrBuilder MIB;
1277 if (X86MFInfo->getBytesToPopOnReturn()) {
1278 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1279 TII.get(Subtarget->is64Bit() ? X86::RETIQ : X86::RETIL))
1280 .addImm(X86MFInfo->getBytesToPopOnReturn());
1281 } else {
1282 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1283 TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
1284 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001285 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1286 MIB.addReg(RetRegs[i], RegState::Implicit);
1287 return true;
1288}
1289
1290/// X86SelectLoad - Select and emit code to implement load instructions.
1291///
1292bool X86FastISel::X86SelectLoad(const Instruction *I) {
1293 const LoadInst *LI = cast<LoadInst>(I);
1294
1295 // Atomic loads need special handling.
1296 if (LI->isAtomic())
1297 return false;
1298
Manman Ren57518142016-04-11 21:08:06 +00001299 const Value *SV = I->getOperand(0);
1300 if (TLI.supportSwiftError()) {
1301 // Swifterror values can come from either a function parameter with
1302 // swifterror attribute or an alloca with swifterror attribute.
1303 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
1304 if (Arg->hasSwiftErrorAttr())
1305 return false;
1306 }
1307
1308 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
1309 if (Alloca->isSwiftError())
1310 return false;
1311 }
1312 }
1313
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001314 MVT VT;
1315 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
1316 return false;
1317
1318 const Value *Ptr = LI->getPointerOperand();
1319
1320 X86AddressMode AM;
1321 if (!X86SelectAddress(Ptr, AM))
1322 return false;
1323
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +00001324 unsigned Alignment = LI->getAlignment();
1325 unsigned ABIAlignment = DL.getABITypeAlignment(LI->getType());
1326 if (Alignment == 0) // Ensure that codegen never sees alignment 0
1327 Alignment = ABIAlignment;
1328
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001329 unsigned ResultReg = 0;
Andrea Di Biagio8f7feec2015-03-26 11:29:02 +00001330 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg,
1331 Alignment))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001332 return false;
1333
1334 updateValueMap(I, ResultReg);
1335 return true;
1336}
1337
1338static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Craig Topper9f01f602017-10-30 21:09:19 +00001339 bool HasAVX512 = Subtarget->hasAVX512();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001340 bool HasAVX = Subtarget->hasAVX();
1341 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
1342 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
1343
1344 switch (VT.getSimpleVT().SimpleTy) {
1345 default: return 0;
1346 case MVT::i8: return X86::CMP8rr;
1347 case MVT::i16: return X86::CMP16rr;
1348 case MVT::i32: return X86::CMP32rr;
1349 case MVT::i64: return X86::CMP64rr;
1350 case MVT::f32:
Craig Topper668b1ab2017-10-31 02:34:29 +00001351 return X86ScalarSSEf32
1352 ? (HasAVX512 ? X86::VUCOMISSZrr
1353 : HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr)
1354 : 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001355 case MVT::f64:
Craig Topper668b1ab2017-10-31 02:34:29 +00001356 return X86ScalarSSEf64
1357 ? (HasAVX512 ? X86::VUCOMISDZrr
1358 : HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr)
1359 : 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001360 }
1361}
1362
Rafael Espindola19141f22015-03-16 14:05:49 +00001363/// If we have a comparison with RHS as the RHS of the comparison, return an
1364/// opcode that works for the compare (e.g. CMP32ri) otherwise return 0.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001365static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Rafael Espindola933f51a2015-03-16 14:25:08 +00001366 int64_t Val = RHSC->getSExtValue();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001367 switch (VT.getSimpleVT().SimpleTy) {
1368 // Otherwise, we can't fold the immediate into this comparison.
Rafael Espindola19141f22015-03-16 14:05:49 +00001369 default:
1370 return 0;
1371 case MVT::i8:
1372 return X86::CMP8ri;
1373 case MVT::i16:
Rafael Espindola933f51a2015-03-16 14:25:08 +00001374 if (isInt<8>(Val))
1375 return X86::CMP16ri8;
Rafael Espindola19141f22015-03-16 14:05:49 +00001376 return X86::CMP16ri;
1377 case MVT::i32:
Rafael Espindola933f51a2015-03-16 14:25:08 +00001378 if (isInt<8>(Val))
1379 return X86::CMP32ri8;
Rafael Espindola19141f22015-03-16 14:05:49 +00001380 return X86::CMP32ri;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001381 case MVT::i64:
Rafael Espindola933f51a2015-03-16 14:25:08 +00001382 if (isInt<8>(Val))
1383 return X86::CMP64ri8;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001384 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1385 // field.
Rafael Espindola933f51a2015-03-16 14:25:08 +00001386 if (isInt<32>(Val))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001387 return X86::CMP64ri32;
1388 return 0;
1389 }
1390}
1391
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001392bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1, EVT VT,
1393 const DebugLoc &CurDbgLoc) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001394 unsigned Op0Reg = getRegForValue(Op0);
1395 if (Op0Reg == 0) return false;
1396
1397 // Handle 'null' like i32/i64 0.
1398 if (isa<ConstantPointerNull>(Op1))
1399 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
1400
1401 // We have two options: compare with register or immediate. If the RHS of
1402 // the compare is an immediate that we can fold into this compare, use
1403 // CMPri, otherwise use CMPrr.
1404 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1405 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
1406 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, CurDbgLoc, TII.get(CompareImmOpc))
1407 .addReg(Op0Reg)
1408 .addImm(Op1C->getSExtValue());
1409 return true;
1410 }
1411 }
1412
1413 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
1414 if (CompareOpc == 0) return false;
1415
1416 unsigned Op1Reg = getRegForValue(Op1);
1417 if (Op1Reg == 0) return false;
1418 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, CurDbgLoc, TII.get(CompareOpc))
1419 .addReg(Op0Reg)
1420 .addReg(Op1Reg);
1421
1422 return true;
1423}
1424
1425bool X86FastISel::X86SelectCmp(const Instruction *I) {
1426 const CmpInst *CI = cast<CmpInst>(I);
1427
1428 MVT VT;
1429 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
1430 return false;
1431
1432 // Try to optimize or fold the cmp.
1433 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1434 unsigned ResultReg = 0;
1435 switch (Predicate) {
1436 default: break;
1437 case CmpInst::FCMP_FALSE: {
1438 ResultReg = createResultReg(&X86::GR32RegClass);
1439 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0),
1440 ResultReg);
1441 ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1442 X86::sub_8bit);
1443 if (!ResultReg)
1444 return false;
1445 break;
1446 }
1447 case CmpInst::FCMP_TRUE: {
1448 ResultReg = createResultReg(&X86::GR8RegClass);
1449 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
1450 ResultReg).addImm(1);
1451 break;
1452 }
1453 }
1454
1455 if (ResultReg) {
1456 updateValueMap(I, ResultReg);
1457 return true;
1458 }
1459
1460 const Value *LHS = CI->getOperand(0);
1461 const Value *RHS = CI->getOperand(1);
1462
1463 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1464 // We don't have to materialize a zero constant for this case and can just use
1465 // %x again on the RHS.
1466 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1467 const auto *RHSC = dyn_cast<ConstantFP>(RHS);
1468 if (RHSC && RHSC->isNullValue())
1469 RHS = LHS;
1470 }
1471
1472 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Craig Topper428169a2016-09-05 07:14:21 +00001473 static const uint16_t SETFOpcTable[2][3] = {
Craig Topper7323c2b2019-04-05 19:27:49 +00001474 { X86::COND_E, X86::COND_NP, X86::AND8rr },
1475 { X86::COND_NE, X86::COND_P, X86::OR8rr }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001476 };
Craig Topper428169a2016-09-05 07:14:21 +00001477 const uint16_t *SETFOpc = nullptr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001478 switch (Predicate) {
1479 default: break;
1480 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1481 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1482 }
1483
1484 ResultReg = createResultReg(&X86::GR8RegClass);
1485 if (SETFOpc) {
1486 if (!X86FastEmitCompare(LHS, RHS, VT, I->getDebugLoc()))
1487 return false;
1488
1489 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1490 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
Craig Topper7323c2b2019-04-05 19:27:49 +00001491 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETCCr),
1492 FlagReg1).addImm(SETFOpc[0]);
1493 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETCCr),
1494 FlagReg2).addImm(SETFOpc[1]);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
1496 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
1497 updateValueMap(I, ResultReg);
1498 return true;
1499 }
1500
1501 X86::CondCode CC;
1502 bool SwapArgs;
Igor Bregerdb754552017-05-11 06:36:37 +00001503 std::tie(CC, SwapArgs) = X86::getX86ConditionCode(Predicate);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001504 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001505
1506 if (SwapArgs)
1507 std::swap(LHS, RHS);
1508
1509 // Emit a compare of LHS/RHS.
1510 if (!X86FastEmitCompare(LHS, RHS, VT, I->getDebugLoc()))
1511 return false;
1512
Craig Topper7323c2b2019-04-05 19:27:49 +00001513 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETCCr),
1514 ResultReg).addImm(CC);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001515 updateValueMap(I, ResultReg);
1516 return true;
1517}
1518
1519bool X86FastISel::X86SelectZExt(const Instruction *I) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001520 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001521 if (!TLI.isTypeLegal(DstVT))
1522 return false;
1523
1524 unsigned ResultReg = getRegForValue(I->getOperand(0));
1525 if (ResultReg == 0)
1526 return false;
1527
1528 // Handle zero-extension from i1 to i8, which is common.
Mehdi Amini44ede332015-07-09 02:09:04 +00001529 MVT SrcVT = TLI.getSimpleValueType(DL, I->getOperand(0)->getType());
Craig Topper088ba172016-12-05 06:09:55 +00001530 if (SrcVT == MVT::i1) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001531 // Set the high bits to zero.
1532 ResultReg = fastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1533 SrcVT = MVT::i8;
1534
1535 if (ResultReg == 0)
1536 return false;
1537 }
1538
1539 if (DstVT == MVT::i64) {
1540 // Handle extension to 64-bits via sub-register shenanigans.
1541 unsigned MovInst;
1542
1543 switch (SrcVT.SimpleTy) {
1544 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1545 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1546 case MVT::i32: MovInst = X86::MOV32rr; break;
1547 default: llvm_unreachable("Unexpected zext to i64 source type");
1548 }
1549
1550 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1551 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
1552 .addReg(ResultReg);
1553
1554 ResultReg = createResultReg(&X86::GR64RegClass);
1555 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
1556 ResultReg)
1557 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
Craig Topper619b7592017-09-02 18:53:46 +00001558 } else if (DstVT == MVT::i16) {
1559 // i8->i16 doesn't exist in the autogenerated isel table. Need to zero
1560 // extend to 32-bits and then extract down to 16-bits.
1561 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1562 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOVZX32rr8),
1563 Result32).addReg(ResultReg);
1564
1565 ResultReg = fastEmitInst_extractsubreg(MVT::i16, Result32, /*Kill=*/true,
1566 X86::sub_16bit);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001567 } else if (DstVT != MVT::i8) {
1568 ResultReg = fastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1569 ResultReg, /*Kill=*/true);
1570 if (ResultReg == 0)
1571 return false;
1572 }
1573
1574 updateValueMap(I, ResultReg);
1575 return true;
1576}
1577
Craig Topper619b7592017-09-02 18:53:46 +00001578bool X86FastISel::X86SelectSExt(const Instruction *I) {
1579 EVT DstVT = TLI.getValueType(DL, I->getType());
1580 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 sign-extension from i1 to i8.
1588 MVT SrcVT = TLI.getSimpleValueType(DL, I->getOperand(0)->getType());
1589 if (SrcVT == MVT::i1) {
1590 // Set the high bits to zero.
1591 unsigned ZExtReg = fastEmitZExtFromI1(MVT::i8, ResultReg,
1592 /*TODO: Kill=*/false);
1593 if (ZExtReg == 0)
1594 return false;
1595
1596 // Negate the result to make an 8-bit sign extended value.
1597 ResultReg = createResultReg(&X86::GR8RegClass);
1598 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::NEG8r),
1599 ResultReg).addReg(ZExtReg);
1600
1601 SrcVT = MVT::i8;
1602 }
1603
1604 if (DstVT == MVT::i16) {
1605 // i8->i16 doesn't exist in the autogenerated isel table. Need to sign
1606 // extend to 32-bits and then extract down to 16-bits.
1607 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1608 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOVSX32rr8),
1609 Result32).addReg(ResultReg);
1610
1611 ResultReg = fastEmitInst_extractsubreg(MVT::i16, Result32, /*Kill=*/true,
1612 X86::sub_16bit);
1613 } else if (DstVT != MVT::i8) {
1614 ResultReg = fastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::SIGN_EXTEND,
1615 ResultReg, /*Kill=*/true);
1616 if (ResultReg == 0)
1617 return false;
1618 }
1619
1620 updateValueMap(I, ResultReg);
1621 return true;
1622}
1623
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001624bool X86FastISel::X86SelectBranch(const Instruction *I) {
1625 // Unconditional branches are selected by tablegen-generated code.
1626 // Handle a conditional branch.
1627 const BranchInst *BI = cast<BranchInst>(I);
1628 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1629 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1630
1631 // Fold the common case of a conditional branch with a comparison
1632 // in the same block (values defined on other blocks may not have
1633 // initialized registers).
1634 X86::CondCode CC;
1635 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1636 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001637 EVT VT = TLI.getValueType(DL, CI->getOperand(0)->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001638
1639 // Try to optimize or fold the cmp.
1640 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1641 switch (Predicate) {
1642 default: break;
1643 case CmpInst::FCMP_FALSE: fastEmitBranch(FalseMBB, DbgLoc); return true;
1644 case CmpInst::FCMP_TRUE: fastEmitBranch(TrueMBB, DbgLoc); return true;
1645 }
1646
1647 const Value *CmpLHS = CI->getOperand(0);
1648 const Value *CmpRHS = CI->getOperand(1);
1649
1650 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x,
1651 // 0.0.
1652 // We don't have to materialize a zero constant for this case and can just
1653 // use %x again on the RHS.
1654 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1655 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1656 if (CmpRHSC && CmpRHSC->isNullValue())
1657 CmpRHS = CmpLHS;
1658 }
1659
1660 // Try to take advantage of fallthrough opportunities.
1661 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1662 std::swap(TrueMBB, FalseMBB);
1663 Predicate = CmpInst::getInversePredicate(Predicate);
1664 }
1665
1666 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/condition
1667 // code check. Instead two branch instructions are required to check all
1668 // the flags. First we change the predicate to a supported condition code,
1669 // which will be the first branch. Later one we will emit the second
1670 // branch.
1671 bool NeedExtraBranch = false;
1672 switch (Predicate) {
1673 default: break;
1674 case CmpInst::FCMP_OEQ:
Justin Bognerb03fd122016-08-17 05:10:15 +00001675 std::swap(TrueMBB, FalseMBB);
1676 LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001677 case CmpInst::FCMP_UNE:
1678 NeedExtraBranch = true;
1679 Predicate = CmpInst::FCMP_ONE;
1680 break;
1681 }
1682
1683 bool SwapArgs;
Igor Bregerdb754552017-05-11 06:36:37 +00001684 std::tie(CC, SwapArgs) = X86::getX86ConditionCode(Predicate);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001685 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
1686
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001687 if (SwapArgs)
1688 std::swap(CmpLHS, CmpRHS);
1689
1690 // Emit a compare of the LHS and RHS, setting the flags.
1691 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT, CI->getDebugLoc()))
1692 return false;
1693
Craig Topper80aa2292019-04-05 19:28:09 +00001694 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JCC_1))
1695 .addMBB(TrueMBB).addImm(CC);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001696
1697 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1698 // to UNE above).
1699 if (NeedExtraBranch) {
Craig Topper80aa2292019-04-05 19:28:09 +00001700 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JCC_1))
1701 .addMBB(TrueMBB).addImm(X86::COND_P);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001702 }
1703
Matthias Braun17af6072015-08-26 01:38:00 +00001704 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001705 return true;
1706 }
1707 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1708 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1709 // typically happen for _Bool and C++ bools.
1710 MVT SourceVT;
1711 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1712 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1713 unsigned TestOpc = 0;
1714 switch (SourceVT.SimpleTy) {
1715 default: break;
1716 case MVT::i8: TestOpc = X86::TEST8ri; break;
1717 case MVT::i16: TestOpc = X86::TEST16ri; break;
1718 case MVT::i32: TestOpc = X86::TEST32ri; break;
1719 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1720 }
1721 if (TestOpc) {
1722 unsigned OpReg = getRegForValue(TI->getOperand(0));
1723 if (OpReg == 0) return false;
Guy Blank9ae797a2016-08-21 08:02:27 +00001724
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001725 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
1726 .addReg(OpReg).addImm(1);
1727
Craig Topper80aa2292019-04-05 19:28:09 +00001728 unsigned JmpCond = X86::COND_NE;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001729 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1730 std::swap(TrueMBB, FalseMBB);
Craig Topper80aa2292019-04-05 19:28:09 +00001731 JmpCond = X86::COND_E;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001732 }
1733
Craig Topper80aa2292019-04-05 19:28:09 +00001734 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JCC_1))
1735 .addMBB(TrueMBB).addImm(JmpCond);
Matthias Braun17af6072015-08-26 01:38:00 +00001736
1737 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001738 return true;
1739 }
1740 }
1741 } else if (foldX86XALUIntrinsic(CC, BI, BI->getCondition())) {
1742 // Fake request the condition, otherwise the intrinsic might be completely
1743 // optimized away.
1744 unsigned TmpReg = getRegForValue(BI->getCondition());
1745 if (TmpReg == 0)
1746 return false;
1747
Craig Topper80aa2292019-04-05 19:28:09 +00001748 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JCC_1))
1749 .addMBB(TrueMBB).addImm(CC);
Matthias Braun17af6072015-08-26 01:38:00 +00001750 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001751 return true;
1752 }
1753
1754 // Otherwise do a clumsy setcc and re-test it.
1755 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1756 // in an explicit cast, so make sure to handle that correctly.
1757 unsigned OpReg = getRegForValue(BI->getCondition());
1758 if (OpReg == 0) return false;
1759
Guy Blank2bdc74a2016-09-28 11:22:17 +00001760 // In case OpReg is a K register, COPY to a GPR
1761 if (MRI.getRegClass(OpReg) == &X86::VK1RegClass) {
1762 unsigned KOpReg = OpReg;
Craig Topper058f2f62017-03-28 16:35:29 +00001763 OpReg = createResultReg(&X86::GR32RegClass);
Guy Blank2bdc74a2016-09-28 11:22:17 +00001764 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1765 TII.get(TargetOpcode::COPY), OpReg)
1766 .addReg(KOpReg);
Craig Topper058f2f62017-03-28 16:35:29 +00001767 OpReg = fastEmitInst_extractsubreg(MVT::i8, OpReg, /*Kill=*/true,
1768 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00001769 }
1770 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1771 .addReg(OpReg)
1772 .addImm(1);
Craig Topper80aa2292019-04-05 19:28:09 +00001773 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JCC_1))
1774 .addMBB(TrueMBB).addImm(X86::COND_NE);
Matthias Braun17af6072015-08-26 01:38:00 +00001775 finishCondBranch(BI->getParent(), TrueMBB, FalseMBB);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001776 return true;
1777}
1778
1779bool X86FastISel::X86SelectShift(const Instruction *I) {
1780 unsigned CReg = 0, OpReg = 0;
1781 const TargetRegisterClass *RC = nullptr;
Craig Topper9c098ed2018-03-14 17:57:19 +00001782 if (I->getType()->isIntegerTy(8)) {
1783 CReg = X86::CL;
1784 RC = &X86::GR8RegClass;
1785 switch (I->getOpcode()) {
1786 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1787 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1788 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
1789 default: return false;
1790 }
1791 } else if (I->getType()->isIntegerTy(16)) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001792 CReg = X86::CX;
1793 RC = &X86::GR16RegClass;
1794 switch (I->getOpcode()) {
Craig Topper202b5592017-10-28 19:56:56 +00001795 default: llvm_unreachable("Unexpected shift opcode");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001796 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1797 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1798 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001799 }
1800 } else if (I->getType()->isIntegerTy(32)) {
1801 CReg = X86::ECX;
1802 RC = &X86::GR32RegClass;
1803 switch (I->getOpcode()) {
Craig Topper202b5592017-10-28 19:56:56 +00001804 default: llvm_unreachable("Unexpected shift opcode");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001805 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1806 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1807 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001808 }
1809 } else if (I->getType()->isIntegerTy(64)) {
1810 CReg = X86::RCX;
1811 RC = &X86::GR64RegClass;
1812 switch (I->getOpcode()) {
Craig Topper202b5592017-10-28 19:56:56 +00001813 default: llvm_unreachable("Unexpected shift opcode");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001814 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1815 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1816 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001817 }
1818 } else {
1819 return false;
1820 }
1821
1822 MVT VT;
1823 if (!isTypeLegal(I->getType(), VT))
1824 return false;
1825
1826 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1827 if (Op0Reg == 0) return false;
1828
1829 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1830 if (Op1Reg == 0) return false;
1831 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
1832 CReg).addReg(Op1Reg);
1833
1834 // The shift instruction uses X86::CL. If we defined a super-register
1835 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Craig Topper9c098ed2018-03-14 17:57:19 +00001836 if (CReg != X86::CL)
1837 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1838 TII.get(TargetOpcode::KILL), X86::CL)
1839 .addReg(CReg, RegState::Kill);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001840
1841 unsigned ResultReg = createResultReg(RC);
1842 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
1843 .addReg(Op0Reg);
1844 updateValueMap(I, ResultReg);
1845 return true;
1846}
1847
1848bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1849 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1850 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1851 const static bool S = true; // IsSigned
1852 const static bool U = false; // !IsSigned
1853 const static unsigned Copy = TargetOpcode::COPY;
1854 // For the X86 DIV/IDIV instruction, in most cases the dividend
1855 // (numerator) must be in a specific register pair highreg:lowreg,
1856 // producing the quotient in lowreg and the remainder in highreg.
1857 // For most data types, to set up the instruction, the dividend is
1858 // copied into lowreg, and lowreg is sign-extended or zero-extended
1859 // into highreg. The exception is i8, where the dividend is defined
1860 // as a single register rather than a register pair, and we
1861 // therefore directly sign-extend or zero-extend the dividend into
1862 // lowreg, instead of copying, and ignore the highreg.
1863 const static struct DivRemEntry {
1864 // The following portion depends only on the data type.
1865 const TargetRegisterClass *RC;
1866 unsigned LowInReg; // low part of the register pair
1867 unsigned HighInReg; // high part of the register pair
1868 // The following portion depends on both the data type and the operation.
1869 struct DivRemResult {
1870 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1871 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1872 // highreg, or copying a zero into highreg.
1873 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1874 // zero/sign-extending into lowreg for i8.
1875 unsigned DivRemResultReg; // Register containing the desired result.
1876 bool IsOpSigned; // Whether to use signed or unsigned form.
1877 } ResultTable[NumOps];
1878 } OpTable[NumTypes] = {
1879 { &X86::GR8RegClass, X86::AX, 0, {
1880 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1881 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1882 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1883 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1884 }
1885 }, // i8
1886 { &X86::GR16RegClass, X86::AX, X86::DX, {
1887 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1888 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
1889 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1890 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
1891 }
1892 }, // i16
1893 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1894 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1895 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1896 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1897 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1898 }
1899 }, // i32
1900 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1901 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1902 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Craig Topper6c3f1692018-10-31 21:53:24 +00001903 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1904 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001905 }
1906 }, // i64
1907 };
1908
1909 MVT VT;
1910 if (!isTypeLegal(I->getType(), VT))
1911 return false;
1912
1913 unsigned TypeIndex, OpIndex;
1914 switch (VT.SimpleTy) {
1915 default: return false;
1916 case MVT::i8: TypeIndex = 0; break;
1917 case MVT::i16: TypeIndex = 1; break;
1918 case MVT::i32: TypeIndex = 2; break;
1919 case MVT::i64: TypeIndex = 3;
1920 if (!Subtarget->is64Bit())
1921 return false;
1922 break;
1923 }
1924
1925 switch (I->getOpcode()) {
1926 default: llvm_unreachable("Unexpected div/rem opcode");
1927 case Instruction::SDiv: OpIndex = 0; break;
1928 case Instruction::SRem: OpIndex = 1; break;
1929 case Instruction::UDiv: OpIndex = 2; break;
1930 case Instruction::URem: OpIndex = 3; break;
1931 }
1932
1933 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1934 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1935 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1936 if (Op0Reg == 0)
1937 return false;
1938 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1939 if (Op1Reg == 0)
1940 return false;
1941
1942 // Move op0 into low-order input register.
1943 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1944 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1945 // Zero-extend or sign-extend into high-order input register.
1946 if (OpEntry.OpSignExtend) {
1947 if (OpEntry.IsOpSigned)
1948 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1949 TII.get(OpEntry.OpSignExtend));
1950 else {
Craig Topper6c3f1692018-10-31 21:53:24 +00001951 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001952 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Craig Topper6c3f1692018-10-31 21:53:24 +00001953 TII.get(X86::MOV32r0), Zero32);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001954
1955 // Copy the zero into the appropriate sub/super/identical physical
1956 // register. Unfortunately the operations needed are not uniform enough
1957 // to fit neatly into the table above.
Craig Topper6c3f1692018-10-31 21:53:24 +00001958 if (VT == MVT::i16) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001959 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1960 TII.get(Copy), TypeEntry.HighInReg)
Craig Topper6c3f1692018-10-31 21:53:24 +00001961 .addReg(Zero32, 0, X86::sub_16bit);
1962 } else if (VT == MVT::i32) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001963 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1964 TII.get(Copy), TypeEntry.HighInReg)
Craig Topper6c3f1692018-10-31 21:53:24 +00001965 .addReg(Zero32);
1966 } else if (VT == MVT::i64) {
1967 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1968 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1969 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1970 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001971 }
1972 }
1973 // Generate the DIV/IDIV instruction.
1974 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1975 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00001976 // For i8 remainder, we can't reference ah directly, as we'll end
1977 // up with bogus copies like %r9b = COPY %ah. Reference ax
1978 // instead to prevent ah references in a rex instruction.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001979 //
1980 // The current assumption of the fast register allocator is that isel
Craig Topperb7e4c942017-09-26 21:35:11 +00001981 // won't generate explicit references to the GR8_NOREX registers. If
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001982 // the allocator and/or the backend get enhanced to be more robust in
1983 // that regard, this can be, and should be, removed.
1984 unsigned ResultReg = 0;
1985 if ((I->getOpcode() == Instruction::SRem ||
1986 I->getOpcode() == Instruction::URem) &&
1987 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1988 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1989 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1990 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1991 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1992
1993 // Shift AX right by 8 bits instead of using AH.
1994 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
1995 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1996
1997 // Now reference the 8-bit subreg of the result.
1998 ResultReg = fastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1999 /*Kill=*/true, X86::sub_8bit);
2000 }
2001 // Copy the result out of the physreg if we haven't already.
2002 if (!ResultReg) {
2003 ResultReg = createResultReg(TypeEntry.RC);
2004 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
2005 .addReg(OpEntry.DivRemResultReg);
2006 }
2007 updateValueMap(I, ResultReg);
2008
2009 return true;
2010}
2011
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002012/// Emit a conditional move instruction (if the are supported) to lower
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002013/// the select.
2014bool X86FastISel::X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I) {
2015 // Check if the subtarget supports these instructions.
2016 if (!Subtarget->hasCMov())
2017 return false;
2018
2019 // FIXME: Add support for i8.
2020 if (RetVT < MVT::i16 || RetVT > MVT::i64)
2021 return false;
2022
2023 const Value *Cond = I->getOperand(0);
2024 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2025 bool NeedTest = true;
2026 X86::CondCode CC = X86::COND_NE;
2027
2028 // Optimize conditions coming from a compare if both instructions are in the
2029 // same basic block (values defined in other basic blocks may not have
2030 // initialized registers).
2031 const auto *CI = dyn_cast<CmpInst>(Cond);
2032 if (CI && (CI->getParent() == I->getParent())) {
2033 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2034
2035 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Craig Topper428169a2016-09-05 07:14:21 +00002036 static const uint16_t SETFOpcTable[2][3] = {
Craig Topper7323c2b2019-04-05 19:27:49 +00002037 { X86::COND_NP, X86::COND_E, X86::TEST8rr },
2038 { X86::COND_P, X86::COND_NE, X86::OR8rr }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002039 };
Craig Topper428169a2016-09-05 07:14:21 +00002040 const uint16_t *SETFOpc = nullptr;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002041 switch (Predicate) {
2042 default: break;
2043 case CmpInst::FCMP_OEQ:
2044 SETFOpc = &SETFOpcTable[0][0];
2045 Predicate = CmpInst::ICMP_NE;
2046 break;
2047 case CmpInst::FCMP_UNE:
2048 SETFOpc = &SETFOpcTable[1][0];
2049 Predicate = CmpInst::ICMP_NE;
2050 break;
2051 }
2052
2053 bool NeedSwap;
Igor Bregerdb754552017-05-11 06:36:37 +00002054 std::tie(CC, NeedSwap) = X86::getX86ConditionCode(Predicate);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002055 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
2056
2057 const Value *CmpLHS = CI->getOperand(0);
2058 const Value *CmpRHS = CI->getOperand(1);
2059 if (NeedSwap)
2060 std::swap(CmpLHS, CmpRHS);
2061
Mehdi Amini44ede332015-07-09 02:09:04 +00002062 EVT CmpVT = TLI.getValueType(DL, CmpLHS->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002063 // Emit a compare of the LHS and RHS, setting the flags.
2064 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT, CI->getDebugLoc()))
2065 return false;
2066
2067 if (SETFOpc) {
2068 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
2069 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
Craig Topper7323c2b2019-04-05 19:27:49 +00002070 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETCCr),
2071 FlagReg1).addImm(SETFOpc[0]);
2072 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETCCr),
2073 FlagReg2).addImm(SETFOpc[1]);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002074 auto const &II = TII.get(SETFOpc[2]);
2075 if (II.getNumDefs()) {
2076 unsigned TmpReg = createResultReg(&X86::GR8RegClass);
2077 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, TmpReg)
2078 .addReg(FlagReg2).addReg(FlagReg1);
2079 } else {
2080 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
2081 .addReg(FlagReg2).addReg(FlagReg1);
2082 }
2083 }
2084 NeedTest = false;
2085 } else if (foldX86XALUIntrinsic(CC, I, Cond)) {
2086 // Fake request the condition, otherwise the intrinsic might be completely
2087 // optimized away.
2088 unsigned TmpReg = getRegForValue(Cond);
2089 if (TmpReg == 0)
2090 return false;
2091
2092 NeedTest = false;
2093 }
2094
2095 if (NeedTest) {
2096 // Selects operate on i1, however, CondReg is 8 bits width and may contain
2097 // garbage. Indeed, only the less significant bit is supposed to be
2098 // accurate. If we read more than the lsb, we may see non-zero values
2099 // whereas lsb is zero. Therefore, we have to truncate Op0Reg to i1 for
2100 // the select. This is achieved by performing TEST against 1.
2101 unsigned CondReg = getRegForValue(Cond);
2102 if (CondReg == 0)
2103 return false;
2104 bool CondIsKill = hasTrivialKill(Cond);
2105
Guy Blank2bdc74a2016-09-28 11:22:17 +00002106 // In case OpReg is a K register, COPY to a GPR
2107 if (MRI.getRegClass(CondReg) == &X86::VK1RegClass) {
2108 unsigned KCondReg = CondReg;
Craig Topper058f2f62017-03-28 16:35:29 +00002109 CondReg = createResultReg(&X86::GR32RegClass);
Guy Blank9ae797a2016-08-21 08:02:27 +00002110 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Guy Blank2bdc74a2016-09-28 11:22:17 +00002111 TII.get(TargetOpcode::COPY), CondReg)
2112 .addReg(KCondReg, getKillRegState(CondIsKill));
Craig Topper058f2f62017-03-28 16:35:29 +00002113 CondReg = fastEmitInst_extractsubreg(MVT::i8, CondReg, /*Kill=*/true,
2114 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00002115 }
2116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
2117 .addReg(CondReg, getKillRegState(CondIsKill))
2118 .addImm(1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002119 }
2120
2121 const Value *LHS = I->getOperand(1);
2122 const Value *RHS = I->getOperand(2);
2123
2124 unsigned RHSReg = getRegForValue(RHS);
2125 bool RHSIsKill = hasTrivialKill(RHS);
2126
2127 unsigned LHSReg = getRegForValue(LHS);
2128 bool LHSIsKill = hasTrivialKill(LHS);
2129
2130 if (!LHSReg || !RHSReg)
2131 return false;
2132
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00002133 const TargetRegisterInfo &TRI = *Subtarget->getRegisterInfo();
Craig Toppere0bfeb52019-04-05 19:27:41 +00002134 unsigned Opc = X86::getCMovOpcode(TRI.getRegSizeInBits(*RC)/8);
2135 unsigned ResultReg = fastEmitInst_rri(Opc, RC, RHSReg, RHSIsKill,
2136 LHSReg, LHSIsKill, CC);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002137 updateValueMap(I, ResultReg);
2138 return true;
2139}
2140
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002141/// Emit SSE or AVX instructions to lower the select.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002142///
2143/// Try to use SSE1/SSE2 instructions to simulate a select without branches.
2144/// This lowers fp selects into a CMP/AND/ANDN/OR sequence when the necessary
Sanjay Patel302404b2015-03-05 21:46:54 +00002145/// SSE instructions are available. If AVX is available, try to use a VBLENDV.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002146bool X86FastISel::X86FastEmitSSESelect(MVT RetVT, const Instruction *I) {
2147 // Optimize conditions coming from a compare if both instructions are in the
2148 // same basic block (values defined in other basic blocks may not have
2149 // initialized registers).
2150 const auto *CI = dyn_cast<FCmpInst>(I->getOperand(0));
2151 if (!CI || (CI->getParent() != I->getParent()))
2152 return false;
2153
2154 if (I->getType() != CI->getOperand(0)->getType() ||
2155 !((Subtarget->hasSSE1() && RetVT == MVT::f32) ||
2156 (Subtarget->hasSSE2() && RetVT == MVT::f64)))
2157 return false;
2158
2159 const Value *CmpLHS = CI->getOperand(0);
2160 const Value *CmpRHS = CI->getOperand(1);
2161 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2162
2163 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
2164 // We don't have to materialize a zero constant for this case and can just use
2165 // %x again on the RHS.
2166 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
2167 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
2168 if (CmpRHSC && CmpRHSC->isNullValue())
2169 CmpRHS = CmpLHS;
2170 }
2171
2172 unsigned CC;
2173 bool NeedSwap;
2174 std::tie(CC, NeedSwap) = getX86SSEConditionCode(Predicate);
Craig Topper4f8656a2017-10-09 01:05:15 +00002175 if (CC > 7 && !Subtarget->hasAVX())
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002176 return false;
2177
2178 if (NeedSwap)
2179 std::swap(CmpLHS, CmpRHS);
2180
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002181 const Value *LHS = I->getOperand(1);
2182 const Value *RHS = I->getOperand(2);
2183
2184 unsigned LHSReg = getRegForValue(LHS);
2185 bool LHSIsKill = hasTrivialKill(LHS);
2186
2187 unsigned RHSReg = getRegForValue(RHS);
2188 bool RHSIsKill = hasTrivialKill(RHS);
2189
2190 unsigned CmpLHSReg = getRegForValue(CmpLHS);
2191 bool CmpLHSIsKill = hasTrivialKill(CmpLHS);
2192
2193 unsigned CmpRHSReg = getRegForValue(CmpRHS);
2194 bool CmpRHSIsKill = hasTrivialKill(CmpRHS);
2195
2196 if (!LHSReg || !RHSReg || !CmpLHS || !CmpRHS)
2197 return false;
2198
2199 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
Sanjay Patel302404b2015-03-05 21:46:54 +00002200 unsigned ResultReg;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002201
2202 if (Subtarget->hasAVX512()) {
2203 // If we have AVX512 we can use a mask compare and masked movss/sd.
2204 const TargetRegisterClass *VR128X = &X86::VR128XRegClass;
2205 const TargetRegisterClass *VK1 = &X86::VK1RegClass;
2206
2207 unsigned CmpOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002208 (RetVT == MVT::f32) ? X86::VCMPSSZrr : X86::VCMPSDZrr;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002209 unsigned CmpReg = fastEmitInst_rri(CmpOpcode, VK1, CmpLHSReg, CmpLHSIsKill,
2210 CmpRHSReg, CmpRHSIsKill, CC);
2211
2212 // Need an IMPLICIT_DEF for the input that is used to generate the upper
2213 // bits of the result register since its not based on any of the inputs.
2214 unsigned ImplicitDefReg = createResultReg(VR128X);
2215 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2216 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2217
2218 // Place RHSReg is the passthru of the masked movss/sd operation and put
2219 // LHS in the input. The mask input comes from the compare.
2220 unsigned MovOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002221 (RetVT == MVT::f32) ? X86::VMOVSSZrrk : X86::VMOVSDZrrk;
Craig Topper7ef6ea32016-12-05 04:51:31 +00002222 unsigned MovReg = fastEmitInst_rrrr(MovOpcode, VR128X, RHSReg, RHSIsKill,
2223 CmpReg, true, ImplicitDefReg, true,
2224 LHSReg, LHSIsKill);
2225
2226 ResultReg = createResultReg(RC);
2227 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2228 TII.get(TargetOpcode::COPY), ResultReg).addReg(MovReg);
2229
2230 } else if (Subtarget->hasAVX()) {
Matthias Braun818c78d2015-08-31 18:25:11 +00002231 const TargetRegisterClass *VR128 = &X86::VR128RegClass;
2232
Sanjay Patel302404b2015-03-05 21:46:54 +00002233 // If we have AVX, create 1 blendv instead of 3 logic instructions.
2234 // Blendv was introduced with SSE 4.1, but the 2 register form implicitly
2235 // uses XMM0 as the selection register. That may need just as many
2236 // instructions as the AND/ANDN/OR sequence due to register moves, so
2237 // don't bother.
2238 unsigned CmpOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002239 (RetVT == MVT::f32) ? X86::VCMPSSrr : X86::VCMPSDrr;
Sanjay Patel302404b2015-03-05 21:46:54 +00002240 unsigned BlendOpcode =
Craig Topper088ba172016-12-05 06:09:55 +00002241 (RetVT == MVT::f32) ? X86::VBLENDVPSrr : X86::VBLENDVPDrr;
2242
Craig Topper7ef6ea32016-12-05 04:51:31 +00002243 unsigned CmpReg = fastEmitInst_rri(CmpOpcode, RC, CmpLHSReg, CmpLHSIsKill,
Sanjay Patel302404b2015-03-05 21:46:54 +00002244 CmpRHSReg, CmpRHSIsKill, CC);
Matthias Braun818c78d2015-08-31 18:25:11 +00002245 unsigned VBlendReg = fastEmitInst_rrr(BlendOpcode, VR128, RHSReg, RHSIsKill,
2246 LHSReg, LHSIsKill, CmpReg, true);
2247 ResultReg = createResultReg(RC);
2248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2249 TII.get(TargetOpcode::COPY), ResultReg).addReg(VBlendReg);
Sanjay Patel302404b2015-03-05 21:46:54 +00002250 } else {
Craig Topper74a43652019-05-11 16:00:19 +00002251 // Choose the SSE instruction sequence based on data type (float or double).
2252 static const uint16_t OpcTable[2][4] = {
2253 { X86::CMPSSrr, X86::ANDPSrr, X86::ANDNPSrr, X86::ORPSrr },
2254 { X86::CMPSDrr, X86::ANDPDrr, X86::ANDNPDrr, X86::ORPDrr }
2255 };
2256
2257 const uint16_t *Opc = nullptr;
2258 switch (RetVT.SimpleTy) {
2259 default: return false;
2260 case MVT::f32: Opc = &OpcTable[0][0]; break;
2261 case MVT::f64: Opc = &OpcTable[1][0]; break;
2262 }
2263
Craig Topper6413f8a2016-12-06 04:58:39 +00002264 const TargetRegisterClass *VR128 = &X86::VR128RegClass;
Sanjay Patel302404b2015-03-05 21:46:54 +00002265 unsigned CmpReg = fastEmitInst_rri(Opc[0], RC, CmpLHSReg, CmpLHSIsKill,
2266 CmpRHSReg, CmpRHSIsKill, CC);
Craig Topper6413f8a2016-12-06 04:58:39 +00002267 unsigned AndReg = fastEmitInst_rr(Opc[1], VR128, CmpReg, /*IsKill=*/false,
Sanjay Patel302404b2015-03-05 21:46:54 +00002268 LHSReg, LHSIsKill);
Craig Topper6413f8a2016-12-06 04:58:39 +00002269 unsigned AndNReg = fastEmitInst_rr(Opc[2], VR128, CmpReg, /*IsKill=*/true,
Sanjay Patel302404b2015-03-05 21:46:54 +00002270 RHSReg, RHSIsKill);
Craig Topper6413f8a2016-12-06 04:58:39 +00002271 unsigned OrReg = fastEmitInst_rr(Opc[3], VR128, AndNReg, /*IsKill=*/true,
2272 AndReg, /*IsKill=*/true);
2273 ResultReg = createResultReg(RC);
2274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2275 TII.get(TargetOpcode::COPY), ResultReg).addReg(OrReg);
Sanjay Patel302404b2015-03-05 21:46:54 +00002276 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002277 updateValueMap(I, ResultReg);
2278 return true;
2279}
2280
2281bool X86FastISel::X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I) {
2282 // These are pseudo CMOV instructions and will be later expanded into control-
2283 // flow.
2284 unsigned Opc;
2285 switch (RetVT.SimpleTy) {
2286 default: return false;
2287 case MVT::i8: Opc = X86::CMOV_GR8; break;
2288 case MVT::i16: Opc = X86::CMOV_GR16; break;
2289 case MVT::i32: Opc = X86::CMOV_GR32; break;
Craig Topperc9d74842019-05-11 16:00:28 +00002290 case MVT::f32: Opc = Subtarget->hasAVX512() ? X86::CMOV_FR32X
2291 : X86::CMOV_FR32; break;
2292 case MVT::f64: Opc = Subtarget->hasAVX512() ? X86::CMOV_FR64X
2293 : X86::CMOV_FR64; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002294 }
2295
2296 const Value *Cond = I->getOperand(0);
2297 X86::CondCode CC = X86::COND_NE;
2298
2299 // Optimize conditions coming from a compare if both instructions are in the
2300 // same basic block (values defined in other basic blocks may not have
2301 // initialized registers).
2302 const auto *CI = dyn_cast<CmpInst>(Cond);
2303 if (CI && (CI->getParent() == I->getParent())) {
2304 bool NeedSwap;
Igor Bregerdb754552017-05-11 06:36:37 +00002305 std::tie(CC, NeedSwap) = X86::getX86ConditionCode(CI->getPredicate());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002306 if (CC > X86::LAST_VALID_COND)
2307 return false;
2308
2309 const Value *CmpLHS = CI->getOperand(0);
2310 const Value *CmpRHS = CI->getOperand(1);
2311
2312 if (NeedSwap)
2313 std::swap(CmpLHS, CmpRHS);
2314
Mehdi Amini44ede332015-07-09 02:09:04 +00002315 EVT CmpVT = TLI.getValueType(DL, CmpLHS->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002316 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT, CI->getDebugLoc()))
2317 return false;
2318 } else {
2319 unsigned CondReg = getRegForValue(Cond);
2320 if (CondReg == 0)
2321 return false;
2322 bool CondIsKill = hasTrivialKill(Cond);
Guy Blank9ae797a2016-08-21 08:02:27 +00002323
Guy Blank2bdc74a2016-09-28 11:22:17 +00002324 // In case OpReg is a K register, COPY to a GPR
2325 if (MRI.getRegClass(CondReg) == &X86::VK1RegClass) {
2326 unsigned KCondReg = CondReg;
Craig Topper058f2f62017-03-28 16:35:29 +00002327 CondReg = createResultReg(&X86::GR32RegClass);
Guy Blank9ae797a2016-08-21 08:02:27 +00002328 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Guy Blank2bdc74a2016-09-28 11:22:17 +00002329 TII.get(TargetOpcode::COPY), CondReg)
2330 .addReg(KCondReg, getKillRegState(CondIsKill));
Craig Topper058f2f62017-03-28 16:35:29 +00002331 CondReg = fastEmitInst_extractsubreg(MVT::i8, CondReg, /*Kill=*/true,
2332 X86::sub_8bit);
Guy Blank2bdc74a2016-09-28 11:22:17 +00002333 }
2334 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
2335 .addReg(CondReg, getKillRegState(CondIsKill))
2336 .addImm(1);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002337 }
2338
2339 const Value *LHS = I->getOperand(1);
2340 const Value *RHS = I->getOperand(2);
2341
2342 unsigned LHSReg = getRegForValue(LHS);
2343 bool LHSIsKill = hasTrivialKill(LHS);
2344
2345 unsigned RHSReg = getRegForValue(RHS);
2346 bool RHSIsKill = hasTrivialKill(RHS);
2347
2348 if (!LHSReg || !RHSReg)
2349 return false;
2350
2351 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2352
2353 unsigned ResultReg =
2354 fastEmitInst_rri(Opc, RC, RHSReg, RHSIsKill, LHSReg, LHSIsKill, CC);
2355 updateValueMap(I, ResultReg);
2356 return true;
2357}
2358
2359bool X86FastISel::X86SelectSelect(const Instruction *I) {
2360 MVT RetVT;
2361 if (!isTypeLegal(I->getType(), RetVT))
2362 return false;
2363
2364 // Check if we can fold the select.
2365 if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) {
2366 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2367 const Value *Opnd = nullptr;
2368 switch (Predicate) {
2369 default: break;
2370 case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break;
2371 case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break;
2372 }
2373 // No need for a select anymore - this is an unconditional move.
2374 if (Opnd) {
2375 unsigned OpReg = getRegForValue(Opnd);
2376 if (OpReg == 0)
2377 return false;
2378 bool OpIsKill = hasTrivialKill(Opnd);
2379 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2380 unsigned ResultReg = createResultReg(RC);
2381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2382 TII.get(TargetOpcode::COPY), ResultReg)
2383 .addReg(OpReg, getKillRegState(OpIsKill));
2384 updateValueMap(I, ResultReg);
2385 return true;
2386 }
2387 }
2388
2389 // First try to use real conditional move instructions.
2390 if (X86FastEmitCMoveSelect(RetVT, I))
2391 return true;
2392
2393 // Try to use a sequence of SSE instructions to simulate a conditional move.
2394 if (X86FastEmitSSESelect(RetVT, I))
2395 return true;
2396
2397 // Fall-back to pseudo conditional move instructions, which will be later
2398 // converted to control-flow.
2399 if (X86FastEmitPseudoSelect(RetVT, I))
2400 return true;
2401
2402 return false;
2403}
2404
Craig Topperda424ba2018-07-13 22:09:30 +00002405// Common code for X86SelectSIToFP and X86SelectUIToFP.
2406bool X86FastISel::X86SelectIntToFP(const Instruction *I, bool IsSigned) {
Andrea Di Biagio98c36702015-04-20 11:56:59 +00002407 // The target-independent selection algorithm in FastISel already knows how
2408 // to select a SINT_TO_FP if the target is SSE but not AVX.
2409 // Early exit if the subtarget doesn't have AVX.
Craig Topperda424ba2018-07-13 22:09:30 +00002410 // Unsigned conversion requires avx512.
2411 bool HasAVX512 = Subtarget->hasAVX512();
2412 if (!Subtarget->hasAVX() || (!IsSigned && !HasAVX512))
Andrea Di Biagio98c36702015-04-20 11:56:59 +00002413 return false;
2414
Craig Topperf0831eef2018-07-13 21:03:43 +00002415 // TODO: We could sign extend narrower types.
2416 MVT SrcVT = TLI.getSimpleValueType(DL, I->getOperand(0)->getType());
2417 if (SrcVT != MVT::i32 && SrcVT != MVT::i64)
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002418 return false;
2419
2420 // Select integer to float/double conversion.
2421 unsigned OpReg = getRegForValue(I->getOperand(0));
2422 if (OpReg == 0)
2423 return false;
2424
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002425 unsigned Opcode;
2426
Craig Topperda424ba2018-07-13 22:09:30 +00002427 static const uint16_t SCvtOpc[2][2][2] = {
Craig Topperf0831eef2018-07-13 21:03:43 +00002428 { { X86::VCVTSI2SSrr, X86::VCVTSI642SSrr },
2429 { X86::VCVTSI2SDrr, X86::VCVTSI642SDrr } },
2430 { { X86::VCVTSI2SSZrr, X86::VCVTSI642SSZrr },
2431 { X86::VCVTSI2SDZrr, X86::VCVTSI642SDZrr } },
2432 };
Craig Topperda424ba2018-07-13 22:09:30 +00002433 static const uint16_t UCvtOpc[2][2] = {
2434 { X86::VCVTUSI2SSZrr, X86::VCVTUSI642SSZrr },
2435 { X86::VCVTUSI2SDZrr, X86::VCVTUSI642SDZrr },
2436 };
Craig Topperf0831eef2018-07-13 21:03:43 +00002437 bool Is64Bit = SrcVT == MVT::i64;
2438
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002439 if (I->getType()->isDoubleTy()) {
Craig Topperda424ba2018-07-13 22:09:30 +00002440 // s/uitofp int -> double
2441 Opcode = IsSigned ? SCvtOpc[HasAVX512][1][Is64Bit] : UCvtOpc[1][Is64Bit];
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002442 } else if (I->getType()->isFloatTy()) {
Craig Topperda424ba2018-07-13 22:09:30 +00002443 // s/uitofp int -> float
2444 Opcode = IsSigned ? SCvtOpc[HasAVX512][0][Is64Bit] : UCvtOpc[0][Is64Bit];
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002445 } else
2446 return false;
2447
Craig Topperf0831eef2018-07-13 21:03:43 +00002448 MVT DstVT = TLI.getValueType(DL, I->getType()).getSimpleVT();
2449 const TargetRegisterClass *RC = TLI.getRegClassFor(DstVT);
Andrea Di Biagiodf93ccf2015-03-04 14:23:25 +00002450 unsigned ImplicitDefReg = createResultReg(RC);
2451 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2452 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2453 unsigned ResultReg =
2454 fastEmitInst_rr(Opcode, RC, ImplicitDefReg, true, OpReg, false);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00002455 updateValueMap(I, ResultReg);
2456 return true;
2457}
2458
Craig Topperda424ba2018-07-13 22:09:30 +00002459bool X86FastISel::X86SelectSIToFP(const Instruction *I) {
2460 return X86SelectIntToFP(I, /*IsSigned*/true);
2461}
2462
2463bool X86FastISel::X86SelectUIToFP(const Instruction *I) {
2464 return X86SelectIntToFP(I, /*IsSigned*/false);
2465}
2466
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002467// Helper method used by X86SelectFPExt and X86SelectFPTrunc.
2468bool X86FastISel::X86SelectFPExtOrFPTrunc(const Instruction *I,
2469 unsigned TargetOpc,
2470 const TargetRegisterClass *RC) {
2471 assert((I->getOpcode() == Instruction::FPExt ||
2472 I->getOpcode() == Instruction::FPTrunc) &&
2473 "Instruction must be an FPExt or FPTrunc!");
Simon Pilgrim8462cc32019-05-05 20:45:20 +00002474 bool HasAVX = Subtarget->hasAVX();
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002475
2476 unsigned OpReg = getRegForValue(I->getOperand(0));
2477 if (OpReg == 0)
2478 return false;
2479
Ayman Musa9b802e42017-03-01 10:20:48 +00002480 unsigned ImplicitDefReg;
Simon Pilgrim8462cc32019-05-05 20:45:20 +00002481 if (HasAVX) {
Ayman Musa9b802e42017-03-01 10:20:48 +00002482 ImplicitDefReg = createResultReg(RC);
2483 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2484 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2485
2486 }
2487
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002488 unsigned ResultReg = createResultReg(RC);
2489 MachineInstrBuilder MIB;
2490 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpc),
2491 ResultReg);
Ayman Musa4b2c9682017-02-23 13:15:44 +00002492
Simon Pilgrim8462cc32019-05-05 20:45:20 +00002493 if (HasAVX)
Ayman Musa4b2c9682017-02-23 13:15:44 +00002494 MIB.addReg(ImplicitDefReg);
Ayman Musa9b802e42017-03-01 10:20:48 +00002495
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002496 MIB.addReg(OpReg);
2497 updateValueMap(I, ResultReg);
2498 return true;
2499}
2500
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002501bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002502 if (X86ScalarSSEf64 && I->getType()->isDoubleTy() &&
2503 I->getOperand(0)->getType()->isFloatTy()) {
Craig Topper5f2289a2017-10-29 02:50:31 +00002504 bool HasAVX512 = Subtarget->hasAVX512();
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002505 // fpext from float to double.
Craig Topper5f2289a2017-10-29 02:50:31 +00002506 unsigned Opc =
2507 HasAVX512 ? X86::VCVTSS2SDZrr
2508 : Subtarget->hasAVX() ? X86::VCVTSS2SDrr : X86::CVTSS2SDrr;
Craig Topper26f2b132019-05-11 16:00:13 +00002509 return X86SelectFPExtOrFPTrunc(I, Opc, TLI.getRegClassFor(MVT::f64));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002510 }
2511
2512 return false;
2513}
2514
2515bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002516 if (X86ScalarSSEf64 && I->getType()->isFloatTy() &&
2517 I->getOperand(0)->getType()->isDoubleTy()) {
Craig Topper5f2289a2017-10-29 02:50:31 +00002518 bool HasAVX512 = Subtarget->hasAVX512();
Andrea Di Biagio62622d22015-02-10 12:04:41 +00002519 // fptrunc from double to float.
Craig Topper5f2289a2017-10-29 02:50:31 +00002520 unsigned Opc =
2521 HasAVX512 ? X86::VCVTSD2SSZrr
2522 : Subtarget->hasAVX() ? X86::VCVTSD2SSrr : X86::CVTSD2SSrr;
Craig Topper26f2b132019-05-11 16:00:13 +00002523 return X86SelectFPExtOrFPTrunc(I, Opc, TLI.getRegClassFor(MVT::f32));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002524 }
2525
2526 return false;
2527}
2528
2529bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002530 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
2531 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002532
2533 // This code only handles truncation to byte.
Craig Topperf7ae1012017-08-30 18:08:58 +00002534 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002535 return false;
2536 if (!TLI.isTypeLegal(SrcVT))
2537 return false;
2538
2539 unsigned InputReg = getRegForValue(I->getOperand(0));
2540 if (!InputReg)
2541 // Unhandled operand. Halt "fast" selection and bail.
2542 return false;
2543
2544 if (SrcVT == MVT::i8) {
2545 // Truncate from i8 to i1; no code needed.
2546 updateValueMap(I, InputReg);
2547 return true;
2548 }
2549
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002550 // Issue an extract_subreg.
2551 unsigned ResultReg = fastEmitInst_extractsubreg(MVT::i8,
Craig Toppere92327e2017-09-18 19:21:21 +00002552 InputReg, false,
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);
Fangrui Songf78650a2018-07-30 19:41:25 +00002641
Andrea Di Biagio70351782015-02-20 19:37:14 +00002642 // 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
Hiroshi Inouec8e92452018-01-29 05:17:03 +00002699 // Always make a copy of the frame register to a vreg first, so that we
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002700 // 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
Daniel Neilson1e687242018-01-19 17:13:12 +00002750 return lowerCallTo(II, "memcpy", II->getNumArgOperands() - 1);
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
Daniel Neilson1e687242018-01-19 17:13:12 +00002765 return lowerCallTo(II, "memset", II->getNumArgOperands() - 1);
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 Topper57c45852018-07-12 19:58:06 +00002816 static const uint16_t SqrtOpc[3][2] = {
2817 { X86::SQRTSSr, X86::SQRTSDr },
2818 { X86::VSQRTSSr, X86::VSQRTSDr },
2819 { X86::VSQRTSSZr, X86::VSQRTSDZr },
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002820 };
Craig Topper57c45852018-07-12 19:58:06 +00002821 unsigned AVXLevel = Subtarget->hasAVX512() ? 2 :
2822 Subtarget->hasAVX() ? 1 :
2823 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002824 unsigned Opc;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002825 switch (VT.SimpleTy) {
2826 default: return false;
Craig Topper57c45852018-07-12 19:58:06 +00002827 case MVT::f32: Opc = SqrtOpc[AVXLevel][0]; break;
2828 case MVT::f64: Opc = SqrtOpc[AVXLevel][1]; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002829 }
2830
2831 const Value *SrcVal = II->getArgOperand(0);
2832 unsigned SrcReg = getRegForValue(SrcVal);
2833
2834 if (SrcReg == 0)
2835 return false;
2836
Craig Topper57c45852018-07-12 19:58:06 +00002837 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002838 unsigned ImplicitDefReg = 0;
Craig Topper57c45852018-07-12 19:58:06 +00002839 if (AVXLevel > 0) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002840 ImplicitDefReg = createResultReg(RC);
2841 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2842 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2843 }
2844
2845 unsigned ResultReg = createResultReg(RC);
2846 MachineInstrBuilder MIB;
2847 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
2848 ResultReg);
2849
2850 if (ImplicitDefReg)
2851 MIB.addReg(ImplicitDefReg);
2852
2853 MIB.addReg(SrcReg);
2854
2855 updateValueMap(II, ResultReg);
2856 return true;
2857 }
2858 case Intrinsic::sadd_with_overflow:
2859 case Intrinsic::uadd_with_overflow:
2860 case Intrinsic::ssub_with_overflow:
2861 case Intrinsic::usub_with_overflow:
2862 case Intrinsic::smul_with_overflow:
2863 case Intrinsic::umul_with_overflow: {
2864 // This implements the basic lowering of the xalu with overflow intrinsics
2865 // into add/sub/mul followed by either seto or setb.
2866 const Function *Callee = II->getCalledFunction();
2867 auto *Ty = cast<StructType>(Callee->getReturnType());
2868 Type *RetTy = Ty->getTypeAtIndex(0U);
Zvi Rackover6f76f462016-11-15 13:50:35 +00002869 assert(Ty->getTypeAtIndex(1)->isIntegerTy() &&
2870 Ty->getTypeAtIndex(1)->getScalarSizeInBits() == 1 &&
2871 "Overflow value expected to be an i1");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002872
2873 MVT VT;
2874 if (!isTypeLegal(RetTy, VT))
2875 return false;
2876
2877 if (VT < MVT::i8 || VT > MVT::i64)
2878 return false;
2879
2880 const Value *LHS = II->getArgOperand(0);
2881 const Value *RHS = II->getArgOperand(1);
2882
2883 // Canonicalize immediate to the RHS.
2884 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2885 isCommutativeIntrinsic(II))
2886 std::swap(LHS, RHS);
2887
Craig Topper7323c2b2019-04-05 19:27:49 +00002888 unsigned BaseOpc, CondCode;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002889 switch (II->getIntrinsicID()) {
2890 default: llvm_unreachable("Unexpected intrinsic!");
2891 case Intrinsic::sadd_with_overflow:
Craig Topper7323c2b2019-04-05 19:27:49 +00002892 BaseOpc = ISD::ADD; CondCode = X86::COND_O; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002893 case Intrinsic::uadd_with_overflow:
Craig Topper7323c2b2019-04-05 19:27:49 +00002894 BaseOpc = ISD::ADD; CondCode = X86::COND_B; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002895 case Intrinsic::ssub_with_overflow:
Craig Topper7323c2b2019-04-05 19:27:49 +00002896 BaseOpc = ISD::SUB; CondCode = X86::COND_O; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002897 case Intrinsic::usub_with_overflow:
Craig Topper7323c2b2019-04-05 19:27:49 +00002898 BaseOpc = ISD::SUB; CondCode = X86::COND_B; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002899 case Intrinsic::smul_with_overflow:
Craig Topper7323c2b2019-04-05 19:27:49 +00002900 BaseOpc = X86ISD::SMUL; CondCode = X86::COND_O; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002901 case Intrinsic::umul_with_overflow:
Craig Topper7323c2b2019-04-05 19:27:49 +00002902 BaseOpc = X86ISD::UMUL; CondCode = X86::COND_O; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002903 }
2904
2905 unsigned LHSReg = getRegForValue(LHS);
2906 if (LHSReg == 0)
2907 return false;
2908 bool LHSIsKill = hasTrivialKill(LHS);
2909
2910 unsigned ResultReg = 0;
2911 // Check if we have an immediate version.
2912 if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
Craig Topper66111882016-06-02 04:19:42 +00002913 static const uint16_t Opc[2][4] = {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002914 { X86::INC8r, X86::INC16r, X86::INC32r, X86::INC64r },
2915 { X86::DEC8r, X86::DEC16r, X86::DEC32r, X86::DEC64r }
2916 };
2917
Craig Topper9d4860e2019-01-02 19:01:05 +00002918 if (CI->isOne() && (BaseOpc == ISD::ADD || BaseOpc == ISD::SUB) &&
Craig Topper7323c2b2019-04-05 19:27:49 +00002919 CondCode == X86::COND_O) {
Craig Topper9d4860e2019-01-02 19:01:05 +00002920 // We can use INC/DEC.
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002921 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Craig Topper9d4860e2019-01-02 19:01:05 +00002922 bool IsDec = BaseOpc == ISD::SUB;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002923 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2924 TII.get(Opc[IsDec][VT.SimpleTy-MVT::i8]), ResultReg)
2925 .addReg(LHSReg, getKillRegState(LHSIsKill));
2926 } else
2927 ResultReg = fastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2928 CI->getZExtValue());
2929 }
2930
2931 unsigned RHSReg;
2932 bool RHSIsKill;
2933 if (!ResultReg) {
2934 RHSReg = getRegForValue(RHS);
2935 if (RHSReg == 0)
2936 return false;
2937 RHSIsKill = hasTrivialKill(RHS);
2938 ResultReg = fastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2939 RHSIsKill);
2940 }
2941
2942 // FastISel doesn't have a pattern for all X86::MUL*r and X86::IMUL*r. Emit
2943 // it manually.
2944 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
Craig Toppercf65c622016-03-02 04:42:31 +00002945 static const uint16_t MULOpc[] =
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002946 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
Craig Toppercf65c622016-03-02 04:42:31 +00002947 static const MCPhysReg Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002948 // First copy the first operand into RAX, which is an implicit input to
2949 // the X86::MUL*r instruction.
2950 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2951 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2952 .addReg(LHSReg, getKillRegState(LHSIsKill));
2953 ResultReg = fastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2954 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
2955 } else if (BaseOpc == X86ISD::SMUL && !ResultReg) {
Craig Toppercf65c622016-03-02 04:42:31 +00002956 static const uint16_t MULOpc[] =
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002957 { X86::IMUL8r, X86::IMUL16rr, X86::IMUL32rr, X86::IMUL64rr };
2958 if (VT == MVT::i8) {
2959 // Copy the first operand into AL, which is an implicit input to the
2960 // X86::IMUL8r instruction.
2961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2962 TII.get(TargetOpcode::COPY), X86::AL)
2963 .addReg(LHSReg, getKillRegState(LHSIsKill));
2964 ResultReg = fastEmitInst_r(MULOpc[0], TLI.getRegClassFor(VT), RHSReg,
2965 RHSIsKill);
2966 } else
2967 ResultReg = fastEmitInst_rr(MULOpc[VT.SimpleTy-MVT::i8],
2968 TLI.getRegClassFor(VT), LHSReg, LHSIsKill,
2969 RHSReg, RHSIsKill);
2970 }
2971
2972 if (!ResultReg)
2973 return false;
2974
Zvi Rackoverf0b9b57b2016-11-15 13:29:23 +00002975 // Assign to a GPR since the overflow return value is lowered to a SETcc.
2976 unsigned ResultReg2 = createResultReg(&X86::GR8RegClass);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002977 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
Craig Topper7323c2b2019-04-05 19:27:49 +00002978 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETCCr),
2979 ResultReg2).addImm(CondCode);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00002980
2981 updateValueMap(II, ResultReg, 2);
2982 return true;
2983 }
2984 case Intrinsic::x86_sse_cvttss2si:
2985 case Intrinsic::x86_sse_cvttss2si64:
2986 case Intrinsic::x86_sse2_cvttsd2si:
2987 case Intrinsic::x86_sse2_cvttsd2si64: {
2988 bool IsInputDouble;
2989 switch (II->getIntrinsicID()) {
2990 default: llvm_unreachable("Unexpected intrinsic.");
2991 case Intrinsic::x86_sse_cvttss2si:
2992 case Intrinsic::x86_sse_cvttss2si64:
2993 if (!Subtarget->hasSSE1())
2994 return false;
2995 IsInputDouble = false;
2996 break;
2997 case Intrinsic::x86_sse2_cvttsd2si:
2998 case Intrinsic::x86_sse2_cvttsd2si64:
2999 if (!Subtarget->hasSSE2())
3000 return false;
3001 IsInputDouble = true;
3002 break;
3003 }
3004
3005 Type *RetTy = II->getCalledFunction()->getReturnType();
3006 MVT VT;
3007 if (!isTypeLegal(RetTy, VT))
3008 return false;
3009
Craig Topperd43f5822018-07-12 18:03:56 +00003010 static const uint16_t CvtOpc[3][2][2] = {
3011 { { X86::CVTTSS2SIrr, X86::CVTTSS2SI64rr },
3012 { X86::CVTTSD2SIrr, X86::CVTTSD2SI64rr } },
3013 { { X86::VCVTTSS2SIrr, X86::VCVTTSS2SI64rr },
3014 { X86::VCVTTSD2SIrr, X86::VCVTTSD2SI64rr } },
3015 { { X86::VCVTTSS2SIZrr, X86::VCVTTSS2SI64Zrr },
3016 { X86::VCVTTSD2SIZrr, X86::VCVTTSD2SI64Zrr } },
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003017 };
Craig Topperd43f5822018-07-12 18:03:56 +00003018 unsigned AVXLevel = Subtarget->hasAVX512() ? 2 :
3019 Subtarget->hasAVX() ? 1 :
3020 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003021 unsigned Opc;
3022 switch (VT.SimpleTy) {
3023 default: llvm_unreachable("Unexpected result type.");
Craig Topperd43f5822018-07-12 18:03:56 +00003024 case MVT::i32: Opc = CvtOpc[AVXLevel][IsInputDouble][0]; break;
3025 case MVT::i64: Opc = CvtOpc[AVXLevel][IsInputDouble][1]; break;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003026 }
3027
3028 // Check if we can fold insertelement instructions into the convert.
3029 const Value *Op = II->getArgOperand(0);
3030 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
3031 const Value *Index = IE->getOperand(2);
3032 if (!isa<ConstantInt>(Index))
3033 break;
3034 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
3035
3036 if (Idx == 0) {
3037 Op = IE->getOperand(1);
3038 break;
3039 }
3040 Op = IE->getOperand(0);
3041 }
3042
3043 unsigned Reg = getRegForValue(Op);
3044 if (Reg == 0)
3045 return false;
3046
3047 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3048 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
3049 .addReg(Reg);
3050
3051 updateValueMap(II, ResultReg);
3052 return true;
3053 }
3054 }
3055}
3056
3057bool X86FastISel::fastLowerArguments() {
3058 if (!FuncInfo.CanLowerReturn)
3059 return false;
3060
3061 const Function *F = FuncInfo.Fn;
3062 if (F->isVarArg())
3063 return false;
3064
3065 CallingConv::ID CC = F->getCallingConv();
3066 if (CC != CallingConv::C)
3067 return false;
3068
3069 if (Subtarget->isCallingConvWin64(CC))
3070 return false;
3071
3072 if (!Subtarget->is64Bit())
3073 return false;
3074
Davide Italianoa63981a2017-07-12 15:26:06 +00003075 if (Subtarget->useSoftFloat())
3076 return false;
3077
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003078 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
3079 unsigned GPRCnt = 0;
3080 unsigned FPRCnt = 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003081 for (auto const &Arg : F->args()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00003082 if (Arg.hasAttribute(Attribute::ByVal) ||
3083 Arg.hasAttribute(Attribute::InReg) ||
3084 Arg.hasAttribute(Attribute::StructRet) ||
3085 Arg.hasAttribute(Attribute::SwiftSelf) ||
3086 Arg.hasAttribute(Attribute::SwiftError) ||
3087 Arg.hasAttribute(Attribute::Nest))
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003088 return false;
3089
3090 Type *ArgTy = Arg.getType();
3091 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3092 return false;
3093
Mehdi Amini44ede332015-07-09 02:09:04 +00003094 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003095 if (!ArgVT.isSimple()) return false;
3096 switch (ArgVT.getSimpleVT().SimpleTy) {
3097 default: return false;
3098 case MVT::i32:
3099 case MVT::i64:
3100 ++GPRCnt;
3101 break;
3102 case MVT::f32:
3103 case MVT::f64:
3104 if (!Subtarget->hasSSE1())
3105 return false;
3106 ++FPRCnt;
3107 break;
3108 }
3109
3110 if (GPRCnt > 6)
3111 return false;
3112
3113 if (FPRCnt > 8)
3114 return false;
3115 }
3116
3117 static const MCPhysReg GPR32ArgRegs[] = {
3118 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
3119 };
3120 static const MCPhysReg GPR64ArgRegs[] = {
3121 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
3122 };
3123 static const MCPhysReg XMMArgRegs[] = {
3124 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3125 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3126 };
3127
3128 unsigned GPRIdx = 0;
3129 unsigned FPRIdx = 0;
3130 for (auto const &Arg : F->args()) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003131 MVT VT = TLI.getSimpleValueType(DL, Arg.getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003132 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
3133 unsigned SrcReg;
3134 switch (VT.SimpleTy) {
3135 default: llvm_unreachable("Unexpected value type.");
3136 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
3137 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00003138 case MVT::f32: LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003139 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
3140 }
3141 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3142 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3143 // Without this, EmitLiveInCopies may eliminate the livein if its only
3144 // use is a bitcast (which isn't turned into an instruction).
3145 unsigned ResultReg = createResultReg(RC);
3146 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3147 TII.get(TargetOpcode::COPY), ResultReg)
3148 .addReg(DstReg, getKillRegState(true));
3149 updateValueMap(&Arg, ResultReg);
3150 }
3151 return true;
3152}
3153
Nico Weberaf7e8462016-07-14 01:52:51 +00003154static unsigned computeBytesPoppedByCalleeForSRet(const X86Subtarget *Subtarget,
3155 CallingConv::ID CC,
3156 ImmutableCallSite *CS) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003157 if (Subtarget->is64Bit())
3158 return 0;
3159 if (Subtarget->getTargetTriple().isOSMSVCRT())
3160 return 0;
3161 if (CC == CallingConv::Fast || CC == CallingConv::GHC ||
Reid Klecknerf9b67b82019-10-07 22:28:58 +00003162 CC == CallingConv::HiPE || CC == CallingConv::Tail)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003163 return 0;
Sanjoy Dasb11b4402015-11-04 20:33:45 +00003164
3165 if (CS)
Reid Klecknerfb502d22017-04-14 20:19:02 +00003166 if (CS->arg_empty() || !CS->paramHasAttr(0, Attribute::StructRet) ||
3167 CS->paramHasAttr(0, Attribute::InReg) || Subtarget->isTargetMCU())
Sanjoy Dasb11b4402015-11-04 20:33:45 +00003168 return 0;
3169
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003170 return 4;
3171}
3172
3173bool X86FastISel::fastLowerCall(CallLoweringInfo &CLI) {
3174 auto &OutVals = CLI.OutVals;
3175 auto &OutFlags = CLI.OutFlags;
3176 auto &OutRegs = CLI.OutRegs;
3177 auto &Ins = CLI.Ins;
3178 auto &InRegs = CLI.InRegs;
3179 CallingConv::ID CC = CLI.CallConv;
3180 bool &IsTailCall = CLI.IsTailCall;
3181 bool IsVarArg = CLI.IsVarArg;
3182 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00003183 MCSymbol *Symbol = CLI.Symbol;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003184
3185 bool Is64Bit = Subtarget->is64Bit();
3186 bool IsWin64 = Subtarget->isCallingConvWin64(CC);
3187
Oren Ben Simhondbd4bba2017-05-03 13:07:19 +00003188 const CallInst *CI =
3189 CLI.CS ? dyn_cast<CallInst>(CLI.CS->getInstruction()) : nullptr;
3190 const Function *CalledFn = CI ? CI->getCalledFunction() : nullptr;
3191
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +00003192 // Call / invoke instructions with NoCfCheck attribute require special
3193 // handling.
3194 const auto *II =
3195 CLI.CS ? dyn_cast<InvokeInst>(CLI.CS->getInstruction()) : nullptr;
3196 if ((CI && CI->doesNoCfCheck()) || (II && II->doesNoCfCheck()))
3197 return false;
3198
Oren Ben Simhondbd4bba2017-05-03 13:07:19 +00003199 // Functions with no_caller_saved_registers that need special handling.
3200 if ((CI && CI->hasFnAttr("no_caller_saved_registers")) ||
3201 (CalledFn && CalledFn->hasFnAttribute("no_caller_saved_registers")))
3202 return false;
3203
Chandler Carruthae0cafe2018-08-23 06:06:38 +00003204 // Functions using retpoline for indirect calls need to use SDISel.
3205 if (Subtarget->useRetpolineIndirectCalls())
Chandler Carruthc58f2162018-01-22 22:05:25 +00003206 return false;
3207
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003208 // Handle only C, fastcc, and webkit_js calling conventions for now.
3209 switch (CC) {
3210 default: return false;
3211 case CallingConv::C:
3212 case CallingConv::Fast:
Reid Klecknerf9b67b82019-10-07 22:28:58 +00003213 case CallingConv::Tail:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003214 case CallingConv::WebKit_JS:
Manman Renf8bdd882016-04-05 22:41:47 +00003215 case CallingConv::Swift:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003216 case CallingConv::X86_FastCall:
Nico Weberecdf45b2016-07-14 13:54:26 +00003217 case CallingConv::X86_StdCall:
Nico Weberaf7e8462016-07-14 01:52:51 +00003218 case CallingConv::X86_ThisCall:
Martin Storsjo2f24e932017-07-17 20:05:19 +00003219 case CallingConv::Win64:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003220 case CallingConv::X86_64_SysV:
3221 break;
3222 }
3223
3224 // Allow SelectionDAG isel to handle tail calls.
3225 if (IsTailCall)
3226 return false;
3227
3228 // fastcc with -tailcallopt is intended to provide a guaranteed
3229 // tail call optimization. Fastisel doesn't know how to do that.
Reid Klecknerf9b67b82019-10-07 22:28:58 +00003230 if ((CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt) ||
3231 CC == CallingConv::Tail)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003232 return false;
3233
3234 // Don't know how to handle Win64 varargs yet. Nothing special needed for
3235 // x86-32. Special handling for x86-64 is implemented.
3236 if (IsVarArg && IsWin64)
3237 return false;
3238
3239 // Don't know about inalloca yet.
3240 if (CLI.CS && CLI.CS->hasInAllocaArgument())
3241 return false;
3242
Manman Ren57518142016-04-11 21:08:06 +00003243 for (auto Flag : CLI.OutFlags)
3244 if (Flag.isSwiftError())
3245 return false;
3246
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003247 SmallVector<MVT, 16> OutVTs;
3248 SmallVector<unsigned, 16> ArgRegs;
3249
3250 // If this is a constant i1/i8/i16 argument, promote to i32 to avoid an extra
3251 // instruction. This is safe because it is common to all FastISel supported
3252 // calling conventions on x86.
3253 for (int i = 0, e = OutVals.size(); i != e; ++i) {
3254 Value *&Val = OutVals[i];
3255 ISD::ArgFlagsTy Flags = OutFlags[i];
3256 if (auto *CI = dyn_cast<ConstantInt>(Val)) {
3257 if (CI->getBitWidth() < 32) {
3258 if (Flags.isSExt())
3259 Val = ConstantExpr::getSExt(CI, Type::getInt32Ty(CI->getContext()));
3260 else
3261 Val = ConstantExpr::getZExt(CI, Type::getInt32Ty(CI->getContext()));
3262 }
3263 }
3264
3265 // Passing bools around ends up doing a trunc to i1 and passing it.
3266 // Codegen this as an argument + "and 1".
3267 MVT VT;
3268 auto *TI = dyn_cast<TruncInst>(Val);
3269 unsigned ResultReg;
3270 if (TI && TI->getType()->isIntegerTy(1) && CLI.CS &&
3271 (TI->getParent() == CLI.CS->getInstruction()->getParent()) &&
3272 TI->hasOneUse()) {
3273 Value *PrevVal = TI->getOperand(0);
3274 ResultReg = getRegForValue(PrevVal);
3275
3276 if (!ResultReg)
3277 return false;
3278
3279 if (!isTypeLegal(PrevVal->getType(), VT))
3280 return false;
3281
3282 ResultReg =
3283 fastEmit_ri(VT, VT, ISD::AND, ResultReg, hasTrivialKill(PrevVal), 1);
3284 } else {
3285 if (!isTypeLegal(Val->getType(), VT))
3286 return false;
3287 ResultReg = getRegForValue(Val);
3288 }
3289
3290 if (!ResultReg)
3291 return false;
3292
3293 ArgRegs.push_back(ResultReg);
3294 OutVTs.push_back(VT);
3295 }
3296
3297 // Analyze operands of the call, assigning locations to each operand.
3298 SmallVector<CCValAssign, 16> ArgLocs;
3299 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, CLI.RetTy->getContext());
3300
3301 // Allocate shadow area for Win64
3302 if (IsWin64)
3303 CCInfo.AllocateStack(32, 8);
3304
3305 CCInfo.AnalyzeCallOperands(OutVTs, OutFlags, CC_X86);
3306
3307 // Get a count of how many bytes are to be pushed on the stack.
Jeroen Ketema740f9d72015-09-29 10:12:57 +00003308 unsigned NumBytes = CCInfo.getAlignedCallFrameSize();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003309
3310 // Issue CALLSEQ_START
3311 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
3312 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Serge Pavlovd526b132017-05-09 13:35:13 +00003313 .addImm(NumBytes).addImm(0).addImm(0);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003314
3315 // Walk the register/memloc assignments, inserting copies/loads.
Eric Christophera1c535b2015-02-02 23:03:45 +00003316 const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003317 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3318 CCValAssign const &VA = ArgLocs[i];
3319 const Value *ArgVal = OutVals[VA.getValNo()];
3320 MVT ArgVT = OutVTs[VA.getValNo()];
3321
3322 if (ArgVT == MVT::x86mmx)
3323 return false;
3324
3325 unsigned ArgReg = ArgRegs[VA.getValNo()];
3326
3327 // Promote the value if needed.
3328 switch (VA.getLocInfo()) {
3329 case CCValAssign::Full: break;
3330 case CCValAssign::SExt: {
3331 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3332 "Unexpected extend");
David Majnemer2c5aeab2016-05-04 00:22:23 +00003333
Craig Topper088ba172016-12-05 06:09:55 +00003334 if (ArgVT == MVT::i1)
David Majnemer2c5aeab2016-05-04 00:22:23 +00003335 return false;
3336
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003337 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
3338 ArgVT, ArgReg);
3339 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
3340 ArgVT = VA.getLocVT();
3341 break;
3342 }
3343 case CCValAssign::ZExt: {
3344 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3345 "Unexpected extend");
David Majnemer2c5aeab2016-05-04 00:22:23 +00003346
3347 // Handle zero-extension from i1 to i8, which is common.
Craig Topper088ba172016-12-05 06:09:55 +00003348 if (ArgVT == MVT::i1) {
David Majnemer2c5aeab2016-05-04 00:22:23 +00003349 // Set the high bits to zero.
3350 ArgReg = fastEmitZExtFromI1(MVT::i8, ArgReg, /*TODO: Kill=*/false);
3351 ArgVT = MVT::i8;
3352
3353 if (ArgReg == 0)
3354 return false;
3355 }
3356
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003357 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
3358 ArgVT, ArgReg);
3359 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
3360 ArgVT = VA.getLocVT();
3361 break;
3362 }
3363 case CCValAssign::AExt: {
3364 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
3365 "Unexpected extend");
3366 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), ArgReg,
3367 ArgVT, ArgReg);
3368 if (!Emitted)
3369 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
3370 ArgVT, ArgReg);
3371 if (!Emitted)
3372 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
3373 ArgVT, ArgReg);
3374
3375 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
3376 ArgVT = VA.getLocVT();
3377 break;
3378 }
3379 case CCValAssign::BCvt: {
3380 ArgReg = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, ArgReg,
3381 /*TODO: Kill=*/false);
3382 assert(ArgReg && "Failed to emit a bitcast!");
3383 ArgVT = VA.getLocVT();
3384 break;
3385 }
3386 case CCValAssign::VExt:
3387 // VExt has not been implemented, so this should be impossible to reach
3388 // for now. However, fallback to Selection DAG isel once implemented.
3389 return false;
3390 case CCValAssign::AExtUpper:
3391 case CCValAssign::SExtUpper:
3392 case CCValAssign::ZExtUpper:
3393 case CCValAssign::FPExt:
Tim Northoverf1c28922019-09-12 10:22:23 +00003394 case CCValAssign::Trunc:
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003395 llvm_unreachable("Unexpected loc info!");
3396 case CCValAssign::Indirect:
3397 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
3398 // support this.
3399 return false;
3400 }
3401
3402 if (VA.isRegLoc()) {
3403 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3404 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
3405 OutRegs.push_back(VA.getLocReg());
3406 } else {
3407 assert(VA.isMemLoc());
3408
3409 // Don't emit stores for undef values.
3410 if (isa<UndefValue>(ArgVal))
3411 continue;
3412
3413 unsigned LocMemOffset = VA.getLocMemOffset();
3414 X86AddressMode AM;
3415 AM.Base.Reg = RegInfo->getStackRegister();
3416 AM.Disp = LocMemOffset;
3417 ISD::ArgFlagsTy Flags = OutFlags[VA.getValNo()];
3418 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
3419 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00003420 MachinePointerInfo::getStack(*FuncInfo.MF, LocMemOffset),
3421 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003422 if (Flags.isByVal()) {
3423 X86AddressMode SrcAM;
3424 SrcAM.Base.Reg = ArgReg;
3425 if (!TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize()))
3426 return false;
3427 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
3428 // If this is a really simple value, emit this with the Value* version
3429 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
3430 // as it can cause us to reevaluate the argument.
3431 if (!X86FastEmitStore(ArgVT, ArgVal, AM, MMO))
3432 return false;
3433 } else {
3434 bool ValIsKill = hasTrivialKill(ArgVal);
3435 if (!X86FastEmitStore(ArgVT, ArgReg, ValIsKill, AM, MMO))
3436 return false;
3437 }
3438 }
3439 }
3440
3441 // ELF / PIC requires GOT in the EBX register before function calls via PLT
3442 // GOT pointer.
3443 if (Subtarget->isPICStyleGOT()) {
3444 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
3445 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3446 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
3447 }
3448
3449 if (Is64Bit && IsVarArg && !IsWin64) {
3450 // From AMD64 ABI document:
3451 // For calls that may call functions that use varargs or stdargs
3452 // (prototype-less calls or calls to functions containing ellipsis (...) in
3453 // the declaration) %al is used as hidden argument to specify the number
3454 // of SSE registers used. The contents of %al do not need to match exactly
3455 // the number of registers, but must be an ubound on the number of SSE
3456 // registers used and is in the range 0 - 8 inclusive.
3457
3458 // Count the number of XMM registers allocated.
3459 static const MCPhysReg XMMArgRegs[] = {
3460 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3461 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3462 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003463 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003464 assert((Subtarget->hasSSE1() || !NumXMMRegs)
3465 && "SSE registers cannot be used when SSE is disabled");
3466 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
3467 X86::AL).addImm(NumXMMRegs);
3468 }
3469
3470 // Materialize callee address in a register. FIXME: GV address can be
3471 // handled with a CALLpcrel32 instead.
3472 X86AddressMode CalleeAM;
3473 if (!X86SelectCallAddress(Callee, CalleeAM))
3474 return false;
3475
3476 unsigned CalleeOp = 0;
3477 const GlobalValue *GV = nullptr;
3478 if (CalleeAM.GV != nullptr) {
3479 GV = CalleeAM.GV;
3480 } else if (CalleeAM.Base.Reg != 0) {
3481 CalleeOp = CalleeAM.Base.Reg;
3482 } else
3483 return false;
3484
3485 // Issue the call.
3486 MachineInstrBuilder MIB;
3487 if (CalleeOp) {
3488 // Register-indirect call.
3489 unsigned CallOpc = Is64Bit ? X86::CALL64r : X86::CALL32r;
3490 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
3491 .addReg(CalleeOp);
3492 } else {
3493 // Direct call.
3494 assert(GV && "Not a direct call");
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003495 // See if we need any target-specific flags on the GV operand.
Rafael Espindola46107b92016-05-19 18:49:29 +00003496 unsigned char OpFlags = Subtarget->classifyGlobalFunctionReference(GV);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003497
Reid Kleckner7662d502017-08-05 00:10:43 +00003498 // This will be a direct call, or an indirect call through memory for
3499 // NonLazyBind calls or dllimport calls.
Reid Kleckner6bf108d2019-05-07 23:06:21 +00003500 bool NeedLoad = OpFlags == X86II::MO_DLLIMPORT ||
3501 OpFlags == X86II::MO_GOTPCREL ||
3502 OpFlags == X86II::MO_COFFSTUB;
Reid Kleckner7662d502017-08-05 00:10:43 +00003503 unsigned CallOpc = NeedLoad
3504 ? (Is64Bit ? X86::CALL64m : X86::CALL32m)
3505 : (Is64Bit ? X86::CALL64pcrel32 : X86::CALLpcrel32);
3506
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003507 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Reid Kleckner7662d502017-08-05 00:10:43 +00003508 if (NeedLoad)
3509 MIB.addReg(Is64Bit ? X86::RIP : 0).addImm(1).addReg(0);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00003510 if (Symbol)
3511 MIB.addSym(Symbol, OpFlags);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003512 else
3513 MIB.addGlobalAddress(GV, 0, OpFlags);
Reid Kleckner7662d502017-08-05 00:10:43 +00003514 if (NeedLoad)
3515 MIB.addReg(0);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003516 }
3517
3518 // Add a register mask operand representing the call-preserved registers.
3519 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00003520 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003521
3522 // Add an implicit use GOT pointer in EBX.
3523 if (Subtarget->isPICStyleGOT())
3524 MIB.addReg(X86::EBX, RegState::Implicit);
3525
3526 if (Is64Bit && IsVarArg && !IsWin64)
3527 MIB.addReg(X86::AL, RegState::Implicit);
3528
3529 // Add implicit physical register uses to the call.
3530 for (auto Reg : OutRegs)
3531 MIB.addReg(Reg, RegState::Implicit);
3532
3533 // Issue CALLSEQ_END
3534 unsigned NumBytesForCalleeToPop =
Nico Weberaf7e8462016-07-14 01:52:51 +00003535 X86::isCalleePop(CC, Subtarget->is64Bit(), IsVarArg,
3536 TM.Options.GuaranteedTailCallOpt)
3537 ? NumBytes // Callee pops everything.
3538 : computeBytesPoppedByCalleeForSRet(Subtarget, CC, CLI.CS);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003539 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
3540 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
3541 .addImm(NumBytes).addImm(NumBytesForCalleeToPop);
3542
3543 // Now handle call return values.
3544 SmallVector<CCValAssign, 16> RVLocs;
3545 CCState CCRetInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs,
3546 CLI.RetTy->getContext());
3547 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
3548
3549 // Copy all of the result registers out of their specified physreg.
3550 unsigned ResultReg = FuncInfo.CreateRegs(CLI.RetTy);
3551 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3552 CCValAssign &VA = RVLocs[i];
3553 EVT CopyVT = VA.getValVT();
3554 unsigned CopyReg = ResultReg + i;
Daniel Sanders0c476112019-08-15 19:22:08 +00003555 Register SrcReg = VA.getLocReg();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003556
3557 // If this is x86-64, and we disabled SSE, we can't return FP values
3558 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
3559 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
3560 report_fatal_error("SSE register return with SSE disabled");
3561 }
3562
3563 // If we prefer to use the value in xmm registers, copy it out as f80 and
3564 // use a truncate to move it from fp stack reg to xmm reg.
Craig Topper533b1bd2017-03-30 21:02:52 +00003565 if ((SrcReg == X86::FP0 || SrcReg == X86::FP1) &&
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003566 isScalarFPTypeInSSEReg(VA.getValVT())) {
3567 CopyVT = MVT::f80;
3568 CopyReg = createResultReg(&X86::RFP80RegClass);
3569 }
3570
3571 // Copy out the result.
3572 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Craig Topper533b1bd2017-03-30 21:02:52 +00003573 TII.get(TargetOpcode::COPY), CopyReg).addReg(SrcReg);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003574 InRegs.push_back(VA.getLocReg());
3575
3576 // Round the f80 to the right size, which also moves it to the appropriate
3577 // xmm register. This is accomplished by storing the f80 value in memory
3578 // and then loading it back.
3579 if (CopyVT != VA.getValVT()) {
3580 EVT ResVT = VA.getValVT();
3581 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
3582 unsigned MemSize = ResVT.getSizeInBits()/8;
3583 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
3584 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3585 TII.get(Opc)), FI)
3586 .addReg(CopyReg);
Craig Topper8582ecd2019-06-18 03:23:11 +00003587 Opc = ResVT == MVT::f32 ? X86::MOVSSrm_alt : X86::MOVSDrm_alt;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003588 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3589 TII.get(Opc), ResultReg + i), FI);
3590 }
3591 }
3592
3593 CLI.ResultReg = ResultReg;
3594 CLI.NumResultRegs = RVLocs.size();
3595 CLI.Call = MIB;
3596
3597 return true;
3598}
3599
3600bool
3601X86FastISel::fastSelectInstruction(const Instruction *I) {
3602 switch (I->getOpcode()) {
3603 default: break;
3604 case Instruction::Load:
3605 return X86SelectLoad(I);
3606 case Instruction::Store:
3607 return X86SelectStore(I);
3608 case Instruction::Ret:
3609 return X86SelectRet(I);
3610 case Instruction::ICmp:
3611 case Instruction::FCmp:
3612 return X86SelectCmp(I);
3613 case Instruction::ZExt:
3614 return X86SelectZExt(I);
Craig Topper619b7592017-09-02 18:53:46 +00003615 case Instruction::SExt:
3616 return X86SelectSExt(I);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003617 case Instruction::Br:
3618 return X86SelectBranch(I);
3619 case Instruction::LShr:
3620 case Instruction::AShr:
3621 case Instruction::Shl:
3622 return X86SelectShift(I);
3623 case Instruction::SDiv:
3624 case Instruction::UDiv:
3625 case Instruction::SRem:
3626 case Instruction::URem:
3627 return X86SelectDivRem(I);
3628 case Instruction::Select:
3629 return X86SelectSelect(I);
3630 case Instruction::Trunc:
3631 return X86SelectTrunc(I);
3632 case Instruction::FPExt:
3633 return X86SelectFPExt(I);
3634 case Instruction::FPTrunc:
3635 return X86SelectFPTrunc(I);
Andrea Di Biagioe7b58ee2015-02-17 23:40:58 +00003636 case Instruction::SIToFP:
3637 return X86SelectSIToFP(I);
Craig Topperda424ba2018-07-13 22:09:30 +00003638 case Instruction::UIToFP:
3639 return X86SelectUIToFP(I);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003640 case Instruction::IntToPtr: // Deliberate fall-through.
3641 case Instruction::PtrToInt: {
Mehdi Amini44ede332015-07-09 02:09:04 +00003642 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType());
3643 EVT DstVT = TLI.getValueType(DL, I->getType());
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003644 if (DstVT.bitsGT(SrcVT))
3645 return X86SelectZExt(I);
3646 if (DstVT.bitsLT(SrcVT))
3647 return X86SelectTrunc(I);
3648 unsigned Reg = getRegForValue(I->getOperand(0));
3649 if (Reg == 0) return false;
3650 updateValueMap(I, Reg);
3651 return true;
3652 }
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003653 case Instruction::BitCast: {
Craig Topper29fff072019-07-01 07:09:34 +00003654 // Select SSE2/AVX bitcasts between 128/256/512 bit vector types.
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003655 if (!Subtarget->hasSSE2())
3656 return false;
3657
Craig Topper29fff072019-07-01 07:09:34 +00003658 MVT SrcVT, DstVT;
3659 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT) ||
3660 !isTypeLegal(I->getType(), DstVT))
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003661 return false;
3662
Craig Topper29fff072019-07-01 07:09:34 +00003663 // Only allow vectors that use xmm/ymm/zmm.
3664 if (!SrcVT.isVector() || !DstVT.isVector() ||
3665 SrcVT.getVectorElementType() == MVT::i1 ||
3666 DstVT.getVectorElementType() == MVT::i1)
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003667 return false;
3668
3669 unsigned Reg = getRegForValue(I->getOperand(0));
3670 if (Reg == 0)
3671 return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003672
Andrea Di Biagio77f62652015-10-02 16:08:05 +00003673 // No instruction is needed for conversion. Reuse the register used by
3674 // the fist operand.
3675 updateValueMap(I, Reg);
3676 return true;
3677 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003678 }
3679
3680 return false;
3681}
3682
3683unsigned X86FastISel::X86MaterializeInt(const ConstantInt *CI, MVT VT) {
3684 if (VT > MVT::i64)
3685 return 0;
3686
3687 uint64_t Imm = CI->getZExtValue();
3688 if (Imm == 0) {
3689 unsigned SrcReg = fastEmitInst_(X86::MOV32r0, &X86::GR32RegClass);
3690 switch (VT.SimpleTy) {
3691 default: llvm_unreachable("Unexpected value type");
3692 case MVT::i1:
3693 case MVT::i8:
3694 return fastEmitInst_extractsubreg(MVT::i8, SrcReg, /*Kill=*/true,
3695 X86::sub_8bit);
3696 case MVT::i16:
3697 return fastEmitInst_extractsubreg(MVT::i16, SrcReg, /*Kill=*/true,
3698 X86::sub_16bit);
3699 case MVT::i32:
3700 return SrcReg;
Craig Topper6c3f1692018-10-31 21:53:24 +00003701 case MVT::i64: {
3702 unsigned ResultReg = createResultReg(&X86::GR64RegClass);
3703 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3704 TII.get(TargetOpcode::SUBREG_TO_REG), ResultReg)
3705 .addImm(0).addReg(SrcReg).addImm(X86::sub_32bit);
3706 return ResultReg;
3707 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003708 }
3709 }
3710
3711 unsigned Opc = 0;
3712 switch (VT.SimpleTy) {
3713 default: llvm_unreachable("Unexpected value type");
Craig Topper058f2f62017-03-28 16:35:29 +00003714 case MVT::i1:
Craig Topper058f2f62017-03-28 16:35:29 +00003715 VT = MVT::i8;
3716 LLVM_FALLTHROUGH;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003717 case MVT::i8: Opc = X86::MOV8ri; break;
3718 case MVT::i16: Opc = X86::MOV16ri; break;
3719 case MVT::i32: Opc = X86::MOV32ri; break;
3720 case MVT::i64: {
3721 if (isUInt<32>(Imm))
Craig Topperecdab032018-09-21 23:14:05 +00003722 Opc = X86::MOV32ri64;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003723 else if (isInt<32>(Imm))
3724 Opc = X86::MOV64ri32;
3725 else
3726 Opc = X86::MOV64ri;
3727 break;
3728 }
3729 }
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003730 return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
3731}
3732
3733unsigned X86FastISel::X86MaterializeFP(const ConstantFP *CFP, MVT VT) {
3734 if (CFP->isNullValue())
3735 return fastMaterializeFloatZero(CFP);
3736
3737 // Can't handle alternate code models yet.
3738 CodeModel::Model CM = TM.getCodeModel();
3739 if (CM != CodeModel::Small && CM != CodeModel::Large)
3740 return 0;
3741
3742 // Get opcode and regclass of the output for the given load instruction.
3743 unsigned Opc = 0;
Simon Pilgrim8462cc32019-05-05 20:45:20 +00003744 bool HasAVX = Subtarget->hasAVX();
3745 bool HasAVX512 = Subtarget->hasAVX512();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003746 switch (VT.SimpleTy) {
3747 default: return 0;
3748 case MVT::f32:
Craig Topper682cc092019-05-11 05:18:58 +00003749 if (X86ScalarSSEf32)
Craig Topper8582ecd2019-06-18 03:23:11 +00003750 Opc = HasAVX512 ? X86::VMOVSSZrm_alt :
3751 HasAVX ? X86::VMOVSSrm_alt :
3752 X86::MOVSSrm_alt;
Craig Topper682cc092019-05-11 05:18:58 +00003753 else
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003754 Opc = X86::LD_Fp32m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003755 break;
3756 case MVT::f64:
Craig Topper682cc092019-05-11 05:18:58 +00003757 if (X86ScalarSSEf64)
Craig Topper8582ecd2019-06-18 03:23:11 +00003758 Opc = HasAVX512 ? X86::VMOVSDZrm_alt :
3759 HasAVX ? X86::VMOVSDrm_alt :
3760 X86::MOVSDrm_alt;
Craig Topper682cc092019-05-11 05:18:58 +00003761 else
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003762 Opc = X86::LD_Fp64m;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003763 break;
3764 case MVT::f80:
3765 // No f80 support yet.
3766 return 0;
3767 }
3768
3769 // MachineConstantPool wants an explicit alignment.
3770 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
3771 if (Align == 0) {
3772 // Alignment of vector types. FIXME!
3773 Align = DL.getTypeAllocSize(CFP->getType());
3774 }
3775
3776 // x86-32 PIC requires a PIC base register for constant pools.
3777 unsigned PICBase = 0;
Rafael Espindolac7e98132016-05-20 12:20:10 +00003778 unsigned char OpFlag = Subtarget->classifyLocalReference(nullptr);
3779 if (OpFlag == X86II::MO_PIC_BASE_OFFSET)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003780 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolac7e98132016-05-20 12:20:10 +00003781 else if (OpFlag == X86II::MO_GOTOFF)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003782 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolac7e98132016-05-20 12:20:10 +00003783 else if (Subtarget->is64Bit() && TM.getCodeModel() == CodeModel::Small)
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003784 PICBase = X86::RIP;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003785
3786 // Create the load from the constant pool.
3787 unsigned CPI = MCP.getConstantPoolIndex(CFP, Align);
Craig Topper682cc092019-05-11 05:18:58 +00003788 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003789
3790 if (CM == CodeModel::Large) {
3791 unsigned AddrReg = createResultReg(&X86::GR64RegClass);
3792 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri),
3793 AddrReg)
3794 .addConstantPoolIndex(CPI, 0, OpFlag);
3795 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3796 TII.get(Opc), ResultReg);
3797 addDirectMem(MIB, AddrReg);
3798 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
Alex Lorenze40c8a22015-08-11 23:09:45 +00003799 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
3800 MachineMemOperand::MOLoad, DL.getPointerSize(), Align);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003801 MIB->addMemOperand(*FuncInfo.MF, MMO);
3802 return ResultReg;
3803 }
3804
3805 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3806 TII.get(Opc), ResultReg),
3807 CPI, PICBase, OpFlag);
3808 return ResultReg;
3809}
3810
3811unsigned X86FastISel::X86MaterializeGV(const GlobalValue *GV, MVT VT) {
3812 // Can't handle alternate code models yet.
3813 if (TM.getCodeModel() != CodeModel::Small)
3814 return 0;
3815
3816 // Materialize addresses with LEA/MOV instructions.
3817 X86AddressMode AM;
3818 if (X86SelectAddress(GV, AM)) {
3819 // If the expression is just a basereg, then we're done, otherwise we need
3820 // to emit an LEA.
3821 if (AM.BaseType == X86AddressMode::RegBase &&
3822 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
3823 return AM.Base.Reg;
3824
3825 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3826 if (TM.getRelocationModel() == Reloc::Static &&
Mehdi Amini44ede332015-07-09 02:09:04 +00003827 TLI.getPointerTy(DL) == MVT::i64) {
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003828 // The displacement code could be more than 32 bits away so we need to use
3829 // an instruction with a 64 bit immediate
3830 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV64ri),
3831 ResultReg)
3832 .addGlobalAddress(GV);
3833 } else {
Mehdi Amini44ede332015-07-09 02:09:04 +00003834 unsigned Opc =
3835 TLI.getPointerTy(DL) == MVT::i32
3836 ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r)
3837 : X86::LEA64r;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003838 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3839 TII.get(Opc), ResultReg), AM);
3840 }
3841 return ResultReg;
3842 }
3843 return 0;
3844}
3845
3846unsigned X86FastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003847 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003848
3849 // Only handle simple types.
3850 if (!CEVT.isSimple())
3851 return 0;
3852 MVT VT = CEVT.getSimpleVT();
3853
3854 if (const auto *CI = dyn_cast<ConstantInt>(C))
3855 return X86MaterializeInt(CI, VT);
3856 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
3857 return X86MaterializeFP(CFP, VT);
3858 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
3859 return X86MaterializeGV(GV, VT);
3860
3861 return 0;
3862}
3863
3864unsigned X86FastISel::fastMaterializeAlloca(const AllocaInst *C) {
3865 // Fail on dynamic allocas. At this point, getRegForValue has already
3866 // checked its CSE maps, so if we're here trying to handle a dynamic
3867 // alloca, we're not going to succeed. X86SelectAddress has a
3868 // check for dynamic allocas, because it's called directly from
3869 // various places, but targetMaterializeAlloca also needs a check
3870 // in order to avoid recursion between getRegForValue,
3871 // X86SelectAddrss, and targetMaterializeAlloca.
3872 if (!FuncInfo.StaticAllocaMap.count(C))
3873 return 0;
3874 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
3875
3876 X86AddressMode AM;
3877 if (!X86SelectAddress(C, AM))
3878 return 0;
Mehdi Amini44ede332015-07-09 02:09:04 +00003879 unsigned Opc =
3880 TLI.getPointerTy(DL) == MVT::i32
3881 ? (Subtarget->isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r)
3882 : X86::LEA64r;
3883 const TargetRegisterClass *RC = TLI.getRegClassFor(TLI.getPointerTy(DL));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003884 unsigned ResultReg = createResultReg(RC);
3885 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3886 TII.get(Opc), ResultReg), AM);
3887 return ResultReg;
3888}
3889
3890unsigned X86FastISel::fastMaterializeFloatZero(const ConstantFP *CF) {
3891 MVT VT;
3892 if (!isTypeLegal(CF->getType(), VT))
3893 return 0;
3894
3895 // Get opcode and regclass for the given zero.
Craig Toppera827f842017-11-01 00:47:45 +00003896 bool HasAVX512 = Subtarget->hasAVX512();
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003897 unsigned Opc = 0;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003898 switch (VT.SimpleTy) {
3899 default: return 0;
3900 case MVT::f32:
Craig Topper26f2b132019-05-11 16:00:13 +00003901 if (X86ScalarSSEf32)
Craig Toppera827f842017-11-01 00:47:45 +00003902 Opc = HasAVX512 ? X86::AVX512_FsFLD0SS : X86::FsFLD0SS;
Craig Topper26f2b132019-05-11 16:00:13 +00003903 else
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003904 Opc = X86::LD_Fp032;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003905 break;
3906 case MVT::f64:
Craig Topper26f2b132019-05-11 16:00:13 +00003907 if (X86ScalarSSEf64)
Craig Toppera827f842017-11-01 00:47:45 +00003908 Opc = HasAVX512 ? X86::AVX512_FsFLD0SD : X86::FsFLD0SD;
Craig Topper26f2b132019-05-11 16:00:13 +00003909 else
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003910 Opc = X86::LD_Fp064;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003911 break;
3912 case MVT::f80:
3913 // No f80 support yet.
3914 return 0;
3915 }
3916
Craig Topper26f2b132019-05-11 16:00:13 +00003917 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003918 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
3919 return ResultReg;
3920}
3921
3922
3923bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3924 const LoadInst *LI) {
3925 const Value *Ptr = LI->getPointerOperand();
3926 X86AddressMode AM;
3927 if (!X86SelectAddress(Ptr, AM))
3928 return false;
3929
3930 const X86InstrInfo &XII = (const X86InstrInfo &)TII;
3931
3932 unsigned Size = DL.getTypeAllocSize(LI->getType());
3933 unsigned Alignment = LI->getAlignment();
3934
3935 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3936 Alignment = DL.getABITypeAlignment(LI->getType());
3937
3938 SmallVector<MachineOperand, 8> AddrOps;
3939 AM.getFullAddress(AddrOps);
3940
Keno Fischere70b31f2015-06-08 20:09:58 +00003941 MachineInstr *Result = XII.foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003942 *FuncInfo.MF, *MI, OpNo, AddrOps, FuncInfo.InsertPt, Size, Alignment,
Keno Fischere70b31f2015-06-08 20:09:58 +00003943 /*AllowCommute=*/true);
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003944 if (!Result)
3945 return false;
3946
Pete Cooperd31583d2015-05-06 21:37:19 +00003947 // The index register could be in the wrong register class. Unfortunately,
3948 // foldMemoryOperandImpl could have commuted the instruction so its not enough
3949 // to just look at OpNo + the offset to the index reg. We actually need to
3950 // scan the instruction to find the index reg and see if its the correct reg
3951 // class.
Matthias Braune41e1462015-05-29 02:56:46 +00003952 unsigned OperandNo = 0;
3953 for (MachineInstr::mop_iterator I = Result->operands_begin(),
3954 E = Result->operands_end(); I != E; ++I, ++OperandNo) {
3955 MachineOperand &MO = *I;
3956 if (!MO.isReg() || MO.isDef() || MO.getReg() != AM.IndexReg)
Pete Cooperd31583d2015-05-06 21:37:19 +00003957 continue;
3958 // Found the index reg, now try to rewrite it.
Pete Cooperd31583d2015-05-06 21:37:19 +00003959 unsigned IndexReg = constrainOperandRegClass(Result->getDesc(),
Matthias Braune41e1462015-05-29 02:56:46 +00003960 MO.getReg(), OperandNo);
3961 if (IndexReg == MO.getReg())
Pete Cooperd31583d2015-05-06 21:37:19 +00003962 continue;
Matthias Braune41e1462015-05-29 02:56:46 +00003963 MO.setReg(IndexReg);
Pete Cooperd31583d2015-05-06 21:37:19 +00003964 }
3965
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003966 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Amy Huang68c91992019-04-24 23:02:48 +00003967 Result->cloneInstrSymbols(*FuncInfo.MF, *MI);
Tim Northover256a16d2018-12-17 17:25:53 +00003968 MachineBasicBlock::iterator I(MI);
3969 removeDeadCode(I, std::next(I));
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00003970 return true;
3971}
3972
Craig Topper7ef6ea32016-12-05 04:51:31 +00003973unsigned X86FastISel::fastEmitInst_rrrr(unsigned MachineInstOpcode,
3974 const TargetRegisterClass *RC,
3975 unsigned Op0, bool Op0IsKill,
3976 unsigned Op1, bool Op1IsKill,
3977 unsigned Op2, bool Op2IsKill,
3978 unsigned Op3, bool Op3IsKill) {
3979 const MCInstrDesc &II = TII.get(MachineInstOpcode);
3980
3981 unsigned ResultReg = createResultReg(RC);
3982 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
3983 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
3984 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
Craig Topperd3762582017-10-02 05:46:53 +00003985 Op3 = constrainOperandRegClass(II, Op3, II.getNumDefs() + 3);
Craig Topper7ef6ea32016-12-05 04:51:31 +00003986
3987 if (II.getNumDefs() >= 1)
3988 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
3989 .addReg(Op0, getKillRegState(Op0IsKill))
3990 .addReg(Op1, getKillRegState(Op1IsKill))
3991 .addReg(Op2, getKillRegState(Op2IsKill))
3992 .addReg(Op3, getKillRegState(Op3IsKill));
3993 else {
3994 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
3995 .addReg(Op0, getKillRegState(Op0IsKill))
3996 .addReg(Op1, getKillRegState(Op1IsKill))
3997 .addReg(Op2, getKillRegState(Op2IsKill))
3998 .addReg(Op3, getKillRegState(Op3IsKill));
3999 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4000 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
4001 }
4002 return ResultReg;
4003}
4004
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00004005
4006namespace llvm {
4007 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
4008 const TargetLibraryInfo *libInfo) {
4009 return new X86FastISel(funcInfo, libInfo);
4010 }
4011}