blob: d9f8967dbf0c8cd781d6fdbf1077c2dfcf08937a [file] [log] [blame]
Dan Gohmandaef7f42008-08-19 21:45:35 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
Juergen Ributzka9969d3e2013-11-08 23:28:16 +000017#include "X86CallingConv.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "X86InstrBuilder.h"
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +000019#include "X86InstrInfo.h"
Craig Topperc6d4efa2014-03-19 06:53:25 +000020#include "X86MachineFunctionInfo.h"
Evan Cheng8f23ec92008-09-03 01:04:47 +000021#include "X86RegisterInfo.h"
22#include "X86Subtarget.h"
Dan Gohman49e19e92008-08-22 00:20:26 +000023#include "X86TargetMachine.h"
Juergen Ributzka454d3742014-06-13 00:45:11 +000024#include "llvm/Analysis/BranchProbabilityInfo.h"
Dan Gohmand7b5ce32010-07-10 09:00:22 +000025#include "llvm/CodeGen/Analysis.h"
Evan Cheng24422d42008-09-03 00:03:49 +000026#include "llvm/CodeGen/FastISel.h"
Dan Gohman87fb4e82010-07-07 16:29:44 +000027#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson50288e32008-09-05 00:06:23 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng6c8f55c2008-09-07 09:09:33 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson0673a8a2008-08-29 17:45:56 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000031#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000034#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/GlobalAlias.h"
36#include "llvm/IR/GlobalVariable.h"
37#include "llvm/IR/Instructions.h"
38#include "llvm/IR/IntrinsicInst.h"
39#include "llvm/IR/Operator.h"
Torok Edwin56d06592009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
Evan Chengd10089a2010-01-27 00:00:57 +000041#include "llvm/Target/TargetOptions.h"
Evan Cheng24422d42008-09-03 00:03:49 +000042using namespace llvm;
43
Chris Lattnerd5ac9d82009-03-08 18:44:31 +000044namespace {
Wesley Peck527da1b2010-11-23 03:31:01 +000045
Craig Topper26696312014-03-18 07:27:13 +000046class X86FastISel final : public FastISel {
Evan Cheng24422d42008-09-03 00:03:49 +000047 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
48 /// make the right decision when generating code for different targets.
49 const X86Subtarget *Subtarget;
Evan Cheng6c8f55c2008-09-07 09:09:33 +000050
Wesley Peck527da1b2010-11-23 03:31:01 +000051 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Cheng6c8f55c2008-09-07 09:09:33 +000052 /// floating point ops.
53 /// When SSE is available, use it for f32 operations.
54 /// When SSE2 is available, use it for f64 operations.
55 bool X86ScalarSSEf64;
56 bool X86ScalarSSEf32;
57
Evan Chenga41ee292008-09-03 06:44:39 +000058public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000059 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
60 const TargetLibraryInfo *libInfo)
61 : FastISel(funcInfo, libInfo) {
Evan Cheng8f23ec92008-09-03 01:04:47 +000062 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topperb0c0f722012-01-10 06:54:16 +000063 X86ScalarSSEf64 = Subtarget->hasSSE2();
64 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng8f23ec92008-09-03 01:04:47 +000065 }
Evan Cheng24422d42008-09-03 00:03:49 +000066
Craig Topper2d9361e2014-03-09 07:44:38 +000067 bool TargetSelectInstruction(const Instruction *I) override;
Evan Cheng24422d42008-09-03 00:03:49 +000068
Eli Bendersky90dd3e72013-04-19 22:29:18 +000069 /// \brief The specified machine instr operand is a vreg, and that
Chris Lattnereeba0c72010-09-05 02:18:34 +000070 /// vreg is being provided by the specified load instruction. If possible,
71 /// try to fold the load as an operand to the instruction, returning true if
72 /// possible.
Craig Topper2d9361e2014-03-09 07:44:38 +000073 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
74 const LoadInst *LI) override;
Wesley Peck527da1b2010-11-23 03:31:01 +000075
Craig Topper2d9361e2014-03-09 07:44:38 +000076 bool FastLowerArguments() override;
Chad Rosiera92ef4b2013-02-25 21:59:35 +000077
Dan Gohmandaef7f42008-08-19 21:45:35 +000078#include "X86GenFastISel.inc"
Evan Chenga41ee292008-09-03 06:44:39 +000079
80private:
Dan Gohmanbcaf6812010-04-15 01:51:59 +000081 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peck527da1b2010-11-23 03:31:01 +000082
Juergen Ributzka349777d2014-06-12 23:27:57 +000083 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, MachineMemOperand *MMO,
84 unsigned &ResultReg);
Evan Chengf5bc7e52008-09-05 21:00:03 +000085
Craig Topper4f55b0e2013-07-17 05:57:45 +000086 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +000087 MachineMemOperand *MMO = nullptr, bool Aligned = false);
88 bool X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
89 const X86AddressMode &AM,
90 MachineMemOperand *MMO = nullptr, bool Aligned = false);
Evan Cheng6500d172008-09-08 06:35:17 +000091
Owen Anderson53aa7a92009-08-10 22:56:29 +000092 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +000093 unsigned &ResultReg);
Wesley Peck527da1b2010-11-23 03:31:01 +000094
Dan Gohmanbcaf6812010-04-15 01:51:59 +000095 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
96 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman39d82f92008-09-10 20:11:02 +000097
Dan Gohmanbcaf6812010-04-15 01:51:59 +000098 bool X86SelectLoad(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +000099
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000100 bool X86SelectStore(const Instruction *I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000101
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000102 bool X86SelectRet(const Instruction *I);
103
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000104 bool X86SelectCmp(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000105
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000106 bool X86SelectZExt(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000107
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000108 bool X86SelectBranch(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000109
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000110 bool X86SelectShift(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000111
Eli Bendersky24a36eb2013-04-17 20:10:13 +0000112 bool X86SelectDivRem(const Instruction *I);
113
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000114 bool X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I);
Juergen Ributzka6ef06f92014-06-23 21:55:36 +0000115
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000116 bool X86FastEmitSSESelect(MVT RetVT, const Instruction *I);
Juergen Ributzka21d56082014-06-23 21:55:40 +0000117
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000118 bool X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I);
Juergen Ributzkaaed5c962014-06-23 21:55:44 +0000119
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000120 bool X86SelectSelect(const Instruction *I);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000121
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000122 bool X86SelectTrunc(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000123
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000124 bool X86SelectFPExt(const Instruction *I);
125 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohmanbf646f22008-09-10 21:02:08 +0000126
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000127 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
128 bool X86SelectCall(const Instruction *I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000129
Eli Friedmancd2124a2011-06-10 23:39:36 +0000130 bool DoSelectCall(const Instruction *I, const char *MemIntName);
131
Dan Gohman3691d502008-09-25 15:24:26 +0000132 const X86InstrInfo *getInstrInfo() const {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000133 return getTargetMachine()->getInstrInfo();
134 }
135 const X86TargetMachine *getTargetMachine() const {
136 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman3691d502008-09-25 15:24:26 +0000137 }
138
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000139 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
140
Craig Topper2d9361e2014-03-09 07:44:38 +0000141 unsigned TargetMaterializeConstant(const Constant *C) override;
Dan Gohman39d82f92008-09-10 20:11:02 +0000142
Craig Topper2d9361e2014-03-09 07:44:38 +0000143 unsigned TargetMaterializeAlloca(const AllocaInst *C) override;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000144
Craig Topper2d9361e2014-03-09 07:44:38 +0000145 unsigned TargetMaterializeFloatZero(const ConstantFP *CF) override;
Eli Friedman406c4712011-04-27 22:41:55 +0000146
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000147 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
148 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000149 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000150 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
151 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000152 }
153
Chris Lattner229907c2011-07-18 04:54:35 +0000154 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000155
Eli Friedman60afcc22011-05-20 22:21:04 +0000156 bool IsMemcpySmall(uint64_t Len);
157
Eli Friedmanbcc69142011-04-27 01:45:07 +0000158 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
159 X86AddressMode SrcAM, uint64_t Len);
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000160
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000161 bool foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
162 const Value *Cond);
Evan Cheng24422d42008-09-03 00:03:49 +0000163};
Wesley Peck527da1b2010-11-23 03:31:01 +0000164
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000165} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000166
Juergen Ributzkaaa602092014-06-17 21:55:43 +0000167static CmpInst::Predicate optimizeCmpPredicate(const CmpInst *CI) {
168 // If both operands are the same, then try to optimize or fold the cmp.
169 CmpInst::Predicate Predicate = CI->getPredicate();
170 if (CI->getOperand(0) != CI->getOperand(1))
171 return Predicate;
172
173 switch (Predicate) {
174 default: llvm_unreachable("Invalid predicate!");
175 case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break;
176 case CmpInst::FCMP_OEQ: Predicate = CmpInst::FCMP_ORD; break;
177 case CmpInst::FCMP_OGT: Predicate = CmpInst::FCMP_FALSE; break;
178 case CmpInst::FCMP_OGE: Predicate = CmpInst::FCMP_ORD; break;
179 case CmpInst::FCMP_OLT: Predicate = CmpInst::FCMP_FALSE; break;
180 case CmpInst::FCMP_OLE: Predicate = CmpInst::FCMP_ORD; break;
181 case CmpInst::FCMP_ONE: Predicate = CmpInst::FCMP_FALSE; break;
182 case CmpInst::FCMP_ORD: Predicate = CmpInst::FCMP_ORD; break;
183 case CmpInst::FCMP_UNO: Predicate = CmpInst::FCMP_UNO; break;
184 case CmpInst::FCMP_UEQ: Predicate = CmpInst::FCMP_TRUE; break;
185 case CmpInst::FCMP_UGT: Predicate = CmpInst::FCMP_UNO; break;
186 case CmpInst::FCMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
187 case CmpInst::FCMP_ULT: Predicate = CmpInst::FCMP_UNO; break;
188 case CmpInst::FCMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
189 case CmpInst::FCMP_UNE: Predicate = CmpInst::FCMP_UNO; break;
190 case CmpInst::FCMP_TRUE: Predicate = CmpInst::FCMP_TRUE; break;
191
192 case CmpInst::ICMP_EQ: Predicate = CmpInst::FCMP_TRUE; break;
193 case CmpInst::ICMP_NE: Predicate = CmpInst::FCMP_FALSE; break;
194 case CmpInst::ICMP_UGT: Predicate = CmpInst::FCMP_FALSE; break;
195 case CmpInst::ICMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
196 case CmpInst::ICMP_ULT: Predicate = CmpInst::FCMP_FALSE; break;
197 case CmpInst::ICMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
198 case CmpInst::ICMP_SGT: Predicate = CmpInst::FCMP_FALSE; break;
199 case CmpInst::ICMP_SGE: Predicate = CmpInst::FCMP_TRUE; break;
200 case CmpInst::ICMP_SLT: Predicate = CmpInst::FCMP_FALSE; break;
201 case CmpInst::ICMP_SLE: Predicate = CmpInst::FCMP_TRUE; break;
202 }
203
204 return Predicate;
205}
206
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +0000207static std::pair<X86::CondCode, bool>
Craig Topper9f62d802014-06-27 05:18:21 +0000208getX86ConditionCode(CmpInst::Predicate Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +0000209 X86::CondCode CC = X86::COND_INVALID;
210 bool NeedSwap = false;
211 switch (Predicate) {
212 default: break;
213 // Floating-point Predicates
214 case CmpInst::FCMP_UEQ: CC = X86::COND_E; break;
215 case CmpInst::FCMP_OLT: NeedSwap = true; // fall-through
216 case CmpInst::FCMP_OGT: CC = X86::COND_A; break;
217 case CmpInst::FCMP_OLE: NeedSwap = true; // fall-through
218 case CmpInst::FCMP_OGE: CC = X86::COND_AE; break;
219 case CmpInst::FCMP_UGT: NeedSwap = true; // fall-through
220 case CmpInst::FCMP_ULT: CC = X86::COND_B; break;
221 case CmpInst::FCMP_UGE: NeedSwap = true; // fall-through
222 case CmpInst::FCMP_ULE: CC = X86::COND_BE; break;
223 case CmpInst::FCMP_ONE: CC = X86::COND_NE; break;
224 case CmpInst::FCMP_UNO: CC = X86::COND_P; break;
225 case CmpInst::FCMP_ORD: CC = X86::COND_NP; break;
226 case CmpInst::FCMP_OEQ: // fall-through
227 case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break;
228
229 // Integer Predicates
230 case CmpInst::ICMP_EQ: CC = X86::COND_E; break;
231 case CmpInst::ICMP_NE: CC = X86::COND_NE; break;
232 case CmpInst::ICMP_UGT: CC = X86::COND_A; break;
233 case CmpInst::ICMP_UGE: CC = X86::COND_AE; break;
234 case CmpInst::ICMP_ULT: CC = X86::COND_B; break;
235 case CmpInst::ICMP_ULE: CC = X86::COND_BE; break;
236 case CmpInst::ICMP_SGT: CC = X86::COND_G; break;
237 case CmpInst::ICMP_SGE: CC = X86::COND_GE; break;
238 case CmpInst::ICMP_SLT: CC = X86::COND_L; break;
239 case CmpInst::ICMP_SLE: CC = X86::COND_LE; break;
240 }
241
242 return std::make_pair(CC, NeedSwap);
243}
244
Juergen Ributzka21d56082014-06-23 21:55:40 +0000245static std::pair<unsigned, bool>
Juergen Ributzka345589e2014-06-27 17:16:34 +0000246getX86SSEConditionCode(CmpInst::Predicate Predicate) {
Juergen Ributzka21d56082014-06-23 21:55:40 +0000247 unsigned CC;
248 bool NeedSwap = false;
249
250 // SSE Condition code mapping:
251 // 0 - EQ
252 // 1 - LT
253 // 2 - LE
254 // 3 - UNORD
255 // 4 - NEQ
256 // 5 - NLT
257 // 6 - NLE
258 // 7 - ORD
259 switch (Predicate) {
260 default: llvm_unreachable("Unexpected predicate");
261 case CmpInst::FCMP_OEQ: CC = 0; break;
262 case CmpInst::FCMP_OGT: NeedSwap = true; // fall-through
263 case CmpInst::FCMP_OLT: CC = 1; break;
264 case CmpInst::FCMP_OGE: NeedSwap = true; // fall-through
265 case CmpInst::FCMP_OLE: CC = 2; break;
266 case CmpInst::FCMP_UNO: CC = 3; break;
267 case CmpInst::FCMP_UNE: CC = 4; break;
268 case CmpInst::FCMP_ULE: NeedSwap = true; // fall-through
269 case CmpInst::FCMP_UGE: CC = 5; break;
270 case CmpInst::FCMP_ULT: NeedSwap = true; // fall-through
271 case CmpInst::FCMP_UGT: CC = 6; break;
272 case CmpInst::FCMP_ORD: CC = 7; break;
273 case CmpInst::FCMP_UEQ:
274 case CmpInst::FCMP_ONE: CC = 8; break;
275 }
276
277 return std::make_pair(CC, NeedSwap);
278}
279
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000280/// \brief Check if it is possible to fold the condition from the XALU intrinsic
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000281/// into the user. The condition code will only be updated on success.
282bool X86FastISel::foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
283 const Value *Cond) {
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000284 if (!isa<ExtractValueInst>(Cond))
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000285 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000286
287 const auto *EV = cast<ExtractValueInst>(Cond);
288 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000289 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000290
291 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
292 MVT RetVT;
293 const Function *Callee = II->getCalledFunction();
294 Type *RetTy =
295 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
296 if (!isTypeLegal(RetTy, RetVT))
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000297 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000298
299 if (RetVT != MVT::i32 && RetVT != MVT::i64)
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000300 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000301
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000302 X86::CondCode TmpCC;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000303 switch (II->getIntrinsicID()) {
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000304 default: return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000305 case Intrinsic::sadd_with_overflow:
306 case Intrinsic::ssub_with_overflow:
307 case Intrinsic::smul_with_overflow:
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000308 case Intrinsic::umul_with_overflow: TmpCC = X86::COND_O; break;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000309 case Intrinsic::uadd_with_overflow:
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000310 case Intrinsic::usub_with_overflow: TmpCC = X86::COND_B; break;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000311 }
312
313 // Check if both instructions are in the same basic block.
314 if (II->getParent() != I->getParent())
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000315 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000316
317 // Make sure nothing is in the way
318 BasicBlock::const_iterator Start = I;
319 BasicBlock::const_iterator End = II;
320 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
321 // We only expect extractvalue instructions between the intrinsic and the
322 // instruction to be selected.
323 if (!isa<ExtractValueInst>(Itr))
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000324 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000325
326 // Check that the extractvalue operand comes from the intrinsic.
327 const auto *EVI = cast<ExtractValueInst>(Itr);
328 if (EVI->getAggregateOperand() != II)
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000329 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000330 }
331
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000332 CC = TmpCC;
333 return true;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000334}
335
Chris Lattner229907c2011-07-18 04:54:35 +0000336bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000337 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
338 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000339 // Unhandled type. Halt "fast" selection and bail.
340 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000341
342 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000343 // For now, require SSE/SSE2 for performing floating-point operations,
344 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000345 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000346 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000347 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000348 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000349 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000350 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000351 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000352 // We only handle legal types. For example, on x86-32 the instruction
353 // selector contains all of the 64-bit instructions from x86-64,
354 // under the assumption that i64 won't be used if the target doesn't
355 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000356 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000357}
358
359#include "X86GenCallingConv.inc"
360
Evan Chengf5bc7e52008-09-05 21:00:03 +0000361/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000362/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000363/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000364bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000365 MachineMemOperand *MMO, unsigned &ResultReg) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000366 // Get opcode and regclass of the output for the given load instruction.
367 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000368 const TargetRegisterClass *RC = nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000369 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000370 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000371 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000372 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000373 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000374 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000375 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000376 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000377 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000378 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000379 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000380 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000381 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000382 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000383 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000384 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000385 // Must be in x86-64 mode.
386 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000387 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000388 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000389 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000390 if (X86ScalarSSEf32) {
391 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000392 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000393 } else {
394 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000395 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000396 }
397 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000398 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000399 if (X86ScalarSSEf64) {
400 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000401 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000402 } else {
403 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000404 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000405 }
406 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000407 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000408 // No f80 support yet.
409 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000410 }
411
412 ResultReg = createResultReg(RC);
Juergen Ributzka349777d2014-06-12 23:27:57 +0000413 MachineInstrBuilder MIB =
414 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
415 addFullAddress(MIB, AM);
416 if (MMO)
417 MIB->addMemOperand(*FuncInfo.MF, MMO);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000418 return true;
419}
420
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000421/// X86FastEmitStore - Emit a machine instruction to store a value Val of
422/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
423/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000424/// i.e. V. Return true if it is possible.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000425bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
426 const X86AddressMode &AM,
427 MachineMemOperand *MMO, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000428 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000429 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000430 switch (VT.getSimpleVT().SimpleTy) {
431 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000432 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000433 case MVT::i1: {
434 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000435 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000437 TII.get(X86::AND8ri), AndResult)
438 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
Craig Topper4f55b0e2013-07-17 05:57:45 +0000439 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000440 }
441 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000442 case MVT::i8: Opc = X86::MOV8mr; break;
443 case MVT::i16: Opc = X86::MOV16mr; break;
444 case MVT::i32: Opc = X86::MOV32mr; break;
445 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
446 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000447 Opc = X86ScalarSSEf32 ?
448 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000449 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000450 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000451 Opc = X86ScalarSSEf64 ?
452 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000453 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000454 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000455 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000456 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000457 else
Craig Topper55475d42013-07-17 06:58:23 +0000458 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000459 break;
460 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000461 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000462 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000463 else
Craig Topperad1fff92013-07-18 07:16:44 +0000464 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000465 break;
466 case MVT::v4i32:
467 case MVT::v2i64:
468 case MVT::v8i16:
469 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000470 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000471 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000472 else
Craig Topper55475d42013-07-17 06:58:23 +0000473 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000474 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000475 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000476
Juergen Ributzka349777d2014-06-12 23:27:57 +0000477 MachineInstrBuilder MIB =
478 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
479 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
480 if (MMO)
481 MIB->addMemOperand(*FuncInfo.MF, MMO);
482
Evan Chengf5bc7e52008-09-05 21:00:03 +0000483 return true;
484}
485
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000486bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000487 const X86AddressMode &AM,
488 MachineMemOperand *MMO, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000489 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000490 if (isa<ConstantPointerNull>(Val))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000491 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000492
Chris Lattner3ba29352008-10-15 05:30:52 +0000493 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000494 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000495 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000496 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000497 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000498 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000499 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000500 case MVT::i8: Opc = X86::MOV8mi; break;
501 case MVT::i16: Opc = X86::MOV16mi; break;
502 case MVT::i32: Opc = X86::MOV32mi; break;
503 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000504 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000505 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000506 Opc = X86::MOV64mi32;
507 break;
508 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000509
Chris Lattner3ba29352008-10-15 05:30:52 +0000510 if (Opc) {
Juergen Ributzka349777d2014-06-12 23:27:57 +0000511 MachineInstrBuilder MIB =
512 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
513 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
514 : CI->getZExtValue());
515 if (MMO)
516 MIB->addMemOperand(*FuncInfo.MF, MMO);
Chris Lattner3ba29352008-10-15 05:30:52 +0000517 return true;
518 }
519 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000520
Chris Lattner3ba29352008-10-15 05:30:52 +0000521 unsigned ValReg = getRegForValue(Val);
522 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000523 return false;
524
Juergen Ributzka349777d2014-06-12 23:27:57 +0000525 bool ValKill = hasTrivialKill(Val);
526 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000527}
528
Evan Cheng6500d172008-09-08 06:35:17 +0000529/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
530/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
531/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000532bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
533 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000534 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000535 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
536 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000537 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000538 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000539
540 ResultReg = RR;
541 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000542}
543
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000544bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
545 // Handle constant address.
546 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
547 // Can't handle alternate code models yet.
548 if (TM.getCodeModel() != CodeModel::Small)
549 return false;
550
551 // Can't handle TLS yet.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000552 if (GV->isThreadLocal())
553 return false;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000554
555 // RIP-relative addresses can't have additional register operands, so if
556 // we've already folded stuff into the addressing mode, just force the
557 // global value into its own register, which we can use as the basereg.
558 if (!Subtarget->isPICStyleRIPRel() ||
559 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
560 // Okay, we've committed to selecting this global. Set up the address.
561 AM.GV = GV;
562
563 // Allow the subtarget to classify the global.
564 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
565
566 // If this reference is relative to the pic base, set it now.
567 if (isGlobalRelativeToPICBase(GVFlags)) {
568 // FIXME: How do we know Base.Reg is free??
569 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
570 }
571
572 // Unless the ABI requires an extra load, return a direct reference to
573 // the global.
574 if (!isGlobalStubReference(GVFlags)) {
575 if (Subtarget->isPICStyleRIPRel()) {
576 // Use rip-relative addressing if we can. Above we verified that the
577 // base and index registers are unused.
578 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
579 AM.Base.Reg = X86::RIP;
580 }
581 AM.GVOpFlags = GVFlags;
582 return true;
583 }
584
585 // Ok, we need to do a load from a stub. If we've already loaded from
586 // this stub, reuse the loaded pointer, otherwise emit the load now.
587 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
588 unsigned LoadReg;
589 if (I != LocalValueMap.end() && I->second != 0) {
590 LoadReg = I->second;
591 } else {
592 // Issue load from stub.
593 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000594 const TargetRegisterClass *RC = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000595 X86AddressMode StubAM;
596 StubAM.Base.Reg = AM.Base.Reg;
597 StubAM.GV = GV;
598 StubAM.GVOpFlags = GVFlags;
599
600 // Prepare for inserting code in the local-value area.
601 SavePoint SaveInsertPt = enterLocalValueArea();
602
603 if (TLI.getPointerTy() == MVT::i64) {
604 Opc = X86::MOV64rm;
605 RC = &X86::GR64RegClass;
606
607 if (Subtarget->isPICStyleRIPRel())
608 StubAM.Base.Reg = X86::RIP;
609 } else {
610 Opc = X86::MOV32rm;
611 RC = &X86::GR32RegClass;
612 }
613
614 LoadReg = createResultReg(RC);
615 MachineInstrBuilder LoadMI =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000616 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000617 addFullAddress(LoadMI, StubAM);
618
619 // Ok, back to normal mode.
620 leaveLocalValueArea(SaveInsertPt);
621
622 // Prevent loading GV stub multiple times in same MBB.
623 LocalValueMap[V] = LoadReg;
624 }
625
626 // Now construct the final address. Note that the Disp, Scale,
627 // and Index values may already be set here.
628 AM.Base.Reg = LoadReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000629 AM.GV = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000630 return true;
631 }
632 }
633
634 // If all else fails, try to materialize the value in a register.
635 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
636 if (AM.Base.Reg == 0) {
637 AM.Base.Reg = getRegForValue(V);
638 return AM.Base.Reg != 0;
639 }
640 if (AM.IndexReg == 0) {
641 assert(AM.Scale == 1 && "Scale with no index!");
642 AM.IndexReg = getRegForValue(V);
643 return AM.IndexReg != 0;
644 }
645 }
646
647 return false;
648}
649
Dan Gohman39d82f92008-09-10 20:11:02 +0000650/// X86SelectAddress - Attempt to fill in an address from the given value.
651///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000652bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000653 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000654redo_gep:
Craig Topper062a2ba2014-04-25 05:30:21 +0000655 const User *U = nullptr;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000656 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000657 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000658 // Don't walk into other basic blocks; it's possible we haven't
659 // visited them yet, so the instructions may not yet be assigned
660 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000661 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
662 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
663 Opcode = I->getOpcode();
664 U = I;
665 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000666 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000667 Opcode = C->getOpcode();
668 U = C;
669 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000670
Chris Lattner229907c2011-07-18 04:54:35 +0000671 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000672 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000673 // Fast instruction selection doesn't support the special
674 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000675 return false;
676
Dan Gohman6e005fd2008-09-18 23:23:44 +0000677 switch (Opcode) {
678 default: break;
679 case Instruction::BitCast:
680 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000681 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000682
683 case Instruction::IntToPtr:
684 // Look past no-op inttoptrs.
685 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000686 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000687 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000688
689 case Instruction::PtrToInt:
690 // Look past no-op ptrtoints.
691 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000692 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000693 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000694
695 case Instruction::Alloca: {
696 // Do static allocas.
697 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000698 DenseMap<const AllocaInst*, int>::iterator SI =
699 FuncInfo.StaticAllocaMap.find(A);
700 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000701 AM.BaseType = X86AddressMode::FrameIndexBase;
702 AM.Base.FrameIndex = SI->second;
703 return true;
704 }
705 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000706 }
707
708 case Instruction::Add: {
709 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000710 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000711 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
712 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000713 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000714 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000715 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000716 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000717 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000718 break;
719 }
720
721 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000722 X86AddressMode SavedAM = AM;
723
Dan Gohman6e005fd2008-09-18 23:23:44 +0000724 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000725 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000726 unsigned IndexReg = AM.IndexReg;
727 unsigned Scale = AM.Scale;
728 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000729 // Iterate through the indices, folding what we can. Constants can be
730 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000731 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000732 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000733 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000734 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000735 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000736 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
737 continue;
738 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000739
Chris Lattner4b026b92011-04-17 17:05:12 +0000740 // A array/variable index is always of the form i*S where S is the
741 // constant scale size. See if we can push the scale into immediates.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000742 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner4b026b92011-04-17 17:05:12 +0000743 for (;;) {
744 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
745 // Constant-offset addressing.
746 Disp += CI->getSExtValue() * S;
747 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000748 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000749 if (canFoldAddIntoGEP(U, Op)) {
750 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000751 ConstantInt *CI =
752 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
753 Disp += CI->getSExtValue() * S;
754 // Iterate on the other operand.
755 Op = cast<AddOperator>(Op)->getOperand(0);
756 continue;
757 }
758 if (IndexReg == 0 &&
759 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
760 (S == 1 || S == 2 || S == 4 || S == 8)) {
761 // Scaled-index addressing.
762 Scale = S;
763 IndexReg = getRegForGEPIndex(Op).first;
764 if (IndexReg == 0)
765 return false;
766 break;
767 }
768 // Unsupported.
769 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000770 }
771 }
Bill Wendling585a9012013-09-24 00:13:08 +0000772
Dan Gohman2564b902008-09-26 20:04:15 +0000773 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000774 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000775 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000776
Dan Gohman6e005fd2008-09-18 23:23:44 +0000777 AM.IndexReg = IndexReg;
778 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000779 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000780 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000781
782 if (const GetElementPtrInst *GEP =
783 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
784 // Ok, the GEP indices were covered by constant-offset and scaled-index
785 // addressing. Update the address state and move on to examining the base.
786 V = GEP;
787 goto redo_gep;
788 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000789 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000790 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000791
Chris Lattner4b026b92011-04-17 17:05:12 +0000792 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000793 // our address and just match the value instead of completely failing.
794 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000795
796 for (SmallVectorImpl<const Value *>::reverse_iterator
797 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
798 if (handleConstantAddresses(*I, AM))
799 return true;
800
801 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000802 unsupported_gep:
803 // Ok, the GEP indices weren't all covered.
804 break;
805 }
806 }
807
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000808 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000809}
810
Chris Lattner8212d372009-07-10 05:33:42 +0000811/// X86SelectCallAddress - Attempt to fill in an address from the given value.
812///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000813bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000814 const User *U = nullptr;
Chris Lattner8212d372009-07-10 05:33:42 +0000815 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000816 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000817 // Record if the value is defined in the same basic block.
818 //
819 // This information is crucial to know whether or not folding an
820 // operand is valid.
821 // Indeed, FastISel generates or reuses a virtual register for all
822 // operands of all instructions it selects. Obviously, the definition and
823 // its uses must use the same virtual register otherwise the produced
824 // code is incorrect.
825 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
826 // registers for values that are alive across basic blocks. This ensures
827 // that the values are consistently set between across basic block, even
828 // if different instruction selection mechanisms are used (e.g., a mix of
829 // SDISel and FastISel).
830 // For values local to a basic block, the instruction selection process
831 // generates these virtual registers with whatever method is appropriate
832 // for its needs. In particular, FastISel and SDISel do not share the way
833 // local virtual registers are set.
834 // Therefore, this is impossible (or at least unsafe) to share values
835 // between basic blocks unless they use the same instruction selection
836 // method, which is not guarantee for X86.
837 // Moreover, things like hasOneUse could not be used accurately, if we
838 // allow to reference values across basic blocks whereas they are not
839 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000840 bool InMBB = true;
841 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000842 Opcode = I->getOpcode();
843 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000844 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000845 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000846 Opcode = C->getOpcode();
847 U = C;
848 }
849
850 switch (Opcode) {
851 default: break;
852 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000853 // Look past bitcasts if its operand is in the same BB.
854 if (InMBB)
855 return X86SelectCallAddress(U->getOperand(0), AM);
856 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000857
858 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000859 // Look past no-op inttoptrs if its operand is in the same BB.
860 if (InMBB &&
861 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000862 return X86SelectCallAddress(U->getOperand(0), AM);
863 break;
864
865 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000866 // Look past no-op ptrtoints if its operand is in the same BB.
867 if (InMBB &&
868 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000869 return X86SelectCallAddress(U->getOperand(0), AM);
870 break;
871 }
872
873 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000874 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000875 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000876 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000877 return false;
878
879 // RIP-relative addresses can't have additional register operands.
880 if (Subtarget->isPICStyleRIPRel() &&
881 (AM.Base.Reg != 0 || AM.IndexReg != 0))
882 return false;
883
Saleem Abdulrasoole3c3fe52014-06-30 03:11:18 +0000884 // Can't handle DLL Import.
Nico Rieck7157bb72014-01-14 15:22:47 +0000885 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000886 return false;
887
888 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000889 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000890 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000891 return false;
892
893 // Okay, we've committed to selecting this global. Set up the basic address.
894 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000895
Chris Lattner7277a802009-07-10 05:45:15 +0000896 // No ABI requires an extra load for anything other than DLLImport, which
897 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000898 if (Subtarget->isPICStyleRIPRel()) {
899 // Use rip-relative addressing if we can. Above we verified that the
900 // base and index registers are unused.
901 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
902 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000903 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000904 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
905 } else if (Subtarget->isPICStyleGOT()) {
906 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000907 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000908
Chris Lattner8212d372009-07-10 05:33:42 +0000909 return true;
910 }
911
912 // If all else fails, try to materialize the value in a register.
913 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
914 if (AM.Base.Reg == 0) {
915 AM.Base.Reg = getRegForValue(V);
916 return AM.Base.Reg != 0;
917 }
918 if (AM.IndexReg == 0) {
919 assert(AM.Scale == 1 && "Scale with no index!");
920 AM.IndexReg = getRegForValue(V);
921 return AM.IndexReg != 0;
922 }
923 }
924
925 return false;
926}
927
928
Owen Anderson4f948bd2008-09-04 07:08:58 +0000929/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000930bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000931 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000932 const StoreInst *S = cast<StoreInst>(I);
933
934 if (S->isAtomic())
935 return false;
936
Juergen Ributzka349777d2014-06-12 23:27:57 +0000937 const Value *Val = S->getValueOperand();
938 const Value *Ptr = S->getPointerOperand();
Craig Topper4f55b0e2013-07-17 05:57:45 +0000939
Duncan Sandsf5dda012010-11-03 11:35:31 +0000940 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000941 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000942 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000943
Juergen Ributzka349777d2014-06-12 23:27:57 +0000944 unsigned Alignment = S->getAlignment();
945 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
946 if (Alignment == 0) // Ensure that codegen never sees alignment 0
947 Alignment = ABIAlignment;
948 bool Aligned = Alignment >= ABIAlignment;
949
Dan Gohman39d82f92008-09-10 20:11:02 +0000950 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000951 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000952 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000953
Juergen Ributzka349777d2014-06-12 23:27:57 +0000954 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000955}
956
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000957/// X86SelectRet - Select and emit code to implement ret instructions.
958bool X86FastISel::X86SelectRet(const Instruction *I) {
959 const ReturnInst *Ret = cast<ReturnInst>(I);
960 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000961 const X86MachineFunctionInfo *X86MFInfo =
962 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000963
964 if (!FuncInfo.CanLowerReturn)
965 return false;
966
967 CallingConv::ID CC = F.getCallingConv();
968 if (CC != CallingConv::C &&
969 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000970 CC != CallingConv::X86_FastCall &&
971 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000972 return false;
973
Charles Davise8f297c2013-07-12 06:02:35 +0000974 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000975 return false;
976
977 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000978 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000979 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000980
981 // fastcc with -tailcallopt is intended to provide a guaranteed
982 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000983 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000984 return false;
985
986 // Let SDISel handle vararg functions.
987 if (F.isVarArg())
988 return false;
989
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000990 // Build a list of return value registers.
991 SmallVector<unsigned, 4> RetRegs;
992
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000993 if (Ret->getNumOperands() > 0) {
994 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000995 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000996
997 // Analyze operands of the call, assigning locations to each operand.
998 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000999 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00001000 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +00001001 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001002
1003 const Value *RV = Ret->getOperand(0);
1004 unsigned Reg = getRegForValue(RV);
1005 if (Reg == 0)
1006 return false;
1007
1008 // Only handle a single return value for now.
1009 if (ValLocs.size() != 1)
1010 return false;
1011
1012 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +00001013
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001014 // Don't bother handling odd stuff for now.
1015 if (VA.getLocInfo() != CCValAssign::Full)
1016 return false;
1017 // Only handle register returns for now.
1018 if (!VA.isRegLoc())
1019 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001020
1021 // The calling-convention tables for x87 returns don't tell
1022 // the whole story.
1023 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
1024 return false;
1025
Eli Friedman6fc94dd2011-05-18 23:13:10 +00001026 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +00001027 EVT SrcVT = TLI.getValueType(RV->getType());
1028 EVT DstVT = VA.getValVT();
1029 // Special handling for extended integers.
1030 if (SrcVT != DstVT) {
1031 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
1032 return false;
1033
1034 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1035 return false;
1036
1037 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
1038
1039 if (SrcVT == MVT::i1) {
1040 if (Outs[0].Flags.isSExt())
1041 return false;
1042 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
1043 SrcVT = MVT::i8;
1044 }
1045 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
1046 ISD::SIGN_EXTEND;
1047 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
1048 SrcReg, /*TODO: Kill=*/false);
1049 }
1050
1051 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001052 unsigned DstReg = VA.getLocReg();
1053 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +00001054 // Avoid a cross-class copy. This is very unlikely.
1055 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001056 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001057 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +00001058 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001059
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001060 // Add register to return instruction.
1061 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001062 }
1063
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001064 // The x86-64 ABI for returning structs by value requires that we copy
1065 // the sret argument into %rax for the return. We saved the argument into
1066 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001067 // and into %rax. We also do the same with %eax for Win32.
1068 if (F.hasStructRetAttr() &&
Yaron Keren136fe7d2014-04-01 18:15:34 +00001069 (Subtarget->is64Bit() || Subtarget->isTargetKnownWindowsMSVC())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001070 unsigned Reg = X86MFInfo->getSRetReturnReg();
1071 assert(Reg &&
1072 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001073 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001074 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001075 RetReg).addReg(Reg);
1076 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001077 }
1078
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001079 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001080 MachineInstrBuilder MIB =
Rafael Espindolaea09c592014-02-18 22:05:46 +00001081 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001082 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1083 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001084 return true;
1085}
1086
Evan Chenga41ee292008-09-03 06:44:39 +00001087/// X86SelectLoad - Select and emit code to implement load instructions.
1088///
Juergen Ributzka349777d2014-06-12 23:27:57 +00001089bool X86FastISel::X86SelectLoad(const Instruction *I) {
1090 const LoadInst *LI = cast<LoadInst>(I);
1091
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001092 // Atomic loads need special handling.
Juergen Ributzka349777d2014-06-12 23:27:57 +00001093 if (LI->isAtomic())
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001094 return false;
1095
Duncan Sandsf5dda012010-11-03 11:35:31 +00001096 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001097 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +00001098 return false;
1099
Juergen Ributzka349777d2014-06-12 23:27:57 +00001100 const Value *Ptr = LI->getPointerOperand();
1101
Dan Gohman39d82f92008-09-10 20:11:02 +00001102 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001103 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00001104 return false;
Evan Chenga41ee292008-09-03 06:44:39 +00001105
Evan Chengf5bc7e52008-09-05 21:00:03 +00001106 unsigned ResultReg = 0;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001107 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg))
1108 return false;
1109
1110 UpdateValueMap(I, ResultReg);
1111 return true;
Evan Chenga41ee292008-09-03 06:44:39 +00001112}
1113
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001114static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001115 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +00001116 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
1117 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001118
Owen Anderson9f944592009-08-11 20:47:22 +00001119 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +00001120 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001121 case MVT::i8: return X86::CMP8rr;
1122 case MVT::i16: return X86::CMP16rr;
1123 case MVT::i32: return X86::CMP32rr;
1124 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001125 case MVT::f32:
1126 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
1127 case MVT::f64:
1128 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001129 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001130}
1131
Chris Lattner88f47542008-10-15 04:13:29 +00001132/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
1133/// of the comparison, return an opcode that works for the compare (e.g.
1134/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001135static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +00001136 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +00001137 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +00001138 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001139 case MVT::i8: return X86::CMP8ri;
1140 case MVT::i16: return X86::CMP16ri;
1141 case MVT::i32: return X86::CMP32ri;
1142 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +00001143 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1144 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +00001145 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +00001146 return X86::CMP64ri32;
1147 return 0;
1148 }
Chris Lattner88f47542008-10-15 04:13:29 +00001149}
1150
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001151bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
1152 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +00001153 unsigned Op0Reg = getRegForValue(Op0);
1154 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001155
Chris Lattnere388725a2008-10-15 05:18:04 +00001156 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +00001157 if (isa<ConstantPointerNull>(Op1))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001158 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +00001159
Chris Lattnerd46b9512008-10-15 04:26:38 +00001160 // We have two options: compare with register or immediate. If the RHS of
1161 // the compare is an immediate that we can fold into this compare, use
1162 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001163 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +00001164 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001165 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareImmOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001166 .addReg(Op0Reg)
1167 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +00001168 return true;
1169 }
1170 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001171
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001172 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +00001173 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001174
Chris Lattnerd46b9512008-10-15 04:26:38 +00001175 unsigned Op1Reg = getRegForValue(Op1);
1176 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001177 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001178 .addReg(Op0Reg)
1179 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001180
Chris Lattnerd46b9512008-10-15 04:26:38 +00001181 return true;
1182}
1183
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001184bool X86FastISel::X86SelectCmp(const Instruction *I) {
1185 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001186
Duncan Sandsf5dda012010-11-03 11:35:31 +00001187 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001188 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +00001189 return false;
1190
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001191 // Try to optimize or fold the cmp.
1192 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1193 unsigned ResultReg = 0;
1194 switch (Predicate) {
1195 default: break;
1196 case CmpInst::FCMP_FALSE: {
1197 ResultReg = createResultReg(&X86::GR32RegClass);
1198 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0),
1199 ResultReg);
1200 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1201 X86::sub_8bit);
1202 if (!ResultReg)
1203 return false;
1204 break;
1205 }
1206 case CmpInst::FCMP_TRUE: {
1207 ResultReg = createResultReg(&X86::GR8RegClass);
1208 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
1209 ResultReg).addImm(1);
1210 break;
1211 }
1212 }
1213
1214 if (ResultReg) {
1215 UpdateValueMap(I, ResultReg);
1216 return true;
1217 }
1218
1219 const Value *LHS = CI->getOperand(0);
1220 const Value *RHS = CI->getOperand(1);
1221
1222 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1223 // We don't have to materialize a zero constant for this case and can just use
1224 // %x again on the RHS.
1225 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1226 const auto *RHSC = dyn_cast<ConstantFP>(RHS);
1227 if (RHSC && RHSC->isNullValue())
1228 RHS = LHS;
1229 }
1230
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001231 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Juergen Ributzkae3570562014-06-17 14:47:45 +00001232 static unsigned SETFOpcTable[2][3] = {
1233 { X86::SETEr, X86::SETNPr, X86::AND8rr },
1234 { X86::SETNEr, X86::SETPr, X86::OR8rr }
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001235 };
1236 unsigned *SETFOpc = nullptr;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001237 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001238 default: break;
1239 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1240 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1241 }
1242
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001243 ResultReg = createResultReg(&X86::GR8RegClass);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001244 if (SETFOpc) {
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001245 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001246 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001247
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001248 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1249 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1250 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1251 FlagReg1);
1252 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1253 FlagReg2);
Juergen Ributzkae3570562014-06-17 14:47:45 +00001254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001255 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
Chris Lattnera3596db2008-10-15 03:47:17 +00001256 UpdateValueMap(I, ResultReg);
1257 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001258 }
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001259
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001260 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001261 bool SwapArgs;
Craig Topper9f62d802014-06-27 05:18:21 +00001262 std::tie(CC, SwapArgs) = getX86ConditionCode(Predicate);
Juergen Ributzka345589e2014-06-27 17:16:34 +00001263 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001264 unsigned Opc = X86::getSETFromCond(CC);
Wesley Peck527da1b2010-11-23 03:31:01 +00001265
Chris Lattnerf32ce222008-10-15 03:52:54 +00001266 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001267 std::swap(LHS, RHS);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001268
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001269 // Emit a compare of LHS/RHS.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001270 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001271 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001272
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001273 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001274 UpdateValueMap(I, ResultReg);
1275 return true;
1276}
Evan Chenga41ee292008-09-03 06:44:39 +00001277
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001278bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001279 EVT DstVT = TLI.getValueType(I->getType());
1280 if (!TLI.isTypeLegal(DstVT))
1281 return false;
1282
1283 unsigned ResultReg = getRegForValue(I->getOperand(0));
1284 if (ResultReg == 0)
1285 return false;
1286
Tim Northover04eb4232013-05-30 10:43:18 +00001287 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001288 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001289 if (SrcVT.SimpleTy == MVT::i1) {
1290 // Set the high bits to zero.
1291 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1292 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001293
Tim Northover04eb4232013-05-30 10:43:18 +00001294 if (ResultReg == 0)
1295 return false;
1296 }
1297
1298 if (DstVT == MVT::i64) {
1299 // Handle extension to 64-bits via sub-register shenanigans.
1300 unsigned MovInst;
1301
1302 switch (SrcVT.SimpleTy) {
1303 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1304 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1305 case MVT::i32: MovInst = X86::MOV32rr; break;
1306 default: llvm_unreachable("Unexpected zext to i64 source type");
1307 }
1308
1309 unsigned Result32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001310 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
Tim Northover04eb4232013-05-30 10:43:18 +00001311 .addReg(ResultReg);
1312
1313 ResultReg = createResultReg(&X86::GR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001314 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
Tim Northover04eb4232013-05-30 10:43:18 +00001315 ResultReg)
1316 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1317 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001318 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1319 ResultReg, /*Kill=*/true);
1320 if (ResultReg == 0)
1321 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001322 }
1323
Eli Friedmanc7035512011-05-25 23:49:02 +00001324 UpdateValueMap(I, ResultReg);
1325 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001326}
1327
Chris Lattnerd46b9512008-10-15 04:26:38 +00001328
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001329bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001330 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001331 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001332 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001333 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1334 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001335
Dan Gohman42ef6692010-08-21 02:32:36 +00001336 // Fold the common case of a conditional branch with a comparison
1337 // in the same block (values defined on other blocks may not have
1338 // initialized registers).
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001339 X86::CondCode CC;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001340 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001341 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001342 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001343
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001344 // Try to optimize or fold the cmp.
1345 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1346 switch (Predicate) {
1347 default: break;
1348 case CmpInst::FCMP_FALSE: FastEmitBranch(FalseMBB, DbgLoc); return true;
1349 case CmpInst::FCMP_TRUE: FastEmitBranch(TrueMBB, DbgLoc); return true;
1350 }
1351
1352 const Value *CmpLHS = CI->getOperand(0);
1353 const Value *CmpRHS = CI->getOperand(1);
1354
1355 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x,
1356 // 0.0.
1357 // We don't have to materialize a zero constant for this case and can just
1358 // use %x again on the RHS.
1359 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1360 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1361 if (CmpRHSC && CmpRHSC->isNullValue())
1362 CmpRHS = CmpLHS;
1363 }
1364
Dan Gohman1ab1d312008-10-02 22:15:21 +00001365 // Try to take advantage of fallthrough opportunities.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001366 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001367 std::swap(TrueMBB, FalseMBB);
1368 Predicate = CmpInst::getInversePredicate(Predicate);
1369 }
1370
Juergen Ributzka345589e2014-06-27 17:16:34 +00001371 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/condition
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001372 // code check. Instead two branch instructions are required to check all
Juergen Ributzka345589e2014-06-27 17:16:34 +00001373 // the flags. First we change the predicate to a supported condition code,
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001374 // which will be the first branch. Later one we will emit the second
1375 // branch.
1376 bool NeedExtraBranch = false;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001377 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001378 default: break;
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001379 case CmpInst::FCMP_OEQ:
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001380 std::swap(TrueMBB, FalseMBB); // fall-through
1381 case CmpInst::FCMP_UNE:
1382 NeedExtraBranch = true;
1383 Predicate = CmpInst::FCMP_ONE;
1384 break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001385 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001386
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001387 bool SwapArgs;
1388 unsigned BranchOpc;
Craig Topper9f62d802014-06-27 05:18:21 +00001389 std::tie(CC, SwapArgs) = getX86ConditionCode(Predicate);
Juergen Ributzka345589e2014-06-27 17:16:34 +00001390 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001391
1392 BranchOpc = X86::GetCondBranchFromCond(CC);
Chris Lattner47bef252008-10-15 04:02:26 +00001393 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001394 std::swap(CmpLHS, CmpRHS);
Chris Lattner47bef252008-10-15 04:02:26 +00001395
Chris Lattnerd46b9512008-10-15 04:26:38 +00001396 // Emit a compare of the LHS and RHS, setting the flags.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001397 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT))
Chris Lattnerd46b9512008-10-15 04:26:38 +00001398 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001399
Rafael Espindolaea09c592014-02-18 22:05:46 +00001400 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001401 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001402
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001403 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1404 // to UNE above).
1405 if (NeedExtraBranch) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001406 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001407 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001408 }
1409
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001410 // Obtain the branch weight and add the TrueBB to the successor list.
Juergen Ributzka454d3742014-06-13 00:45:11 +00001411 uint32_t BranchWeight = 0;
1412 if (FuncInfo.BPI)
1413 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1414 TrueMBB->getBasicBlock());
1415 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001416
1417 // Emits an unconditional branch to the FalseBB, obtains the branch
Alp Toker1d099d92014-06-19 19:41:26 +00001418 // weight, and adds it to the successor list.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001419 FastEmitBranch(FalseMBB, DbgLoc);
1420
Dan Gohman1ab1d312008-10-02 22:15:21 +00001421 return true;
1422 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001423 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1424 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1425 // typically happen for _Bool and C++ bools.
1426 MVT SourceVT;
1427 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1428 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1429 unsigned TestOpc = 0;
1430 switch (SourceVT.SimpleTy) {
1431 default: break;
1432 case MVT::i8: TestOpc = X86::TEST8ri; break;
1433 case MVT::i16: TestOpc = X86::TEST16ri; break;
1434 case MVT::i32: TestOpc = X86::TEST32ri; break;
1435 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1436 }
1437 if (TestOpc) {
1438 unsigned OpReg = getRegForValue(TI->getOperand(0));
1439 if (OpReg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001440 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001441 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001442
Chris Lattnerc59290a2011-04-19 04:26:32 +00001443 unsigned JmpOpc = X86::JNE_4;
1444 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1445 std::swap(TrueMBB, FalseMBB);
1446 JmpOpc = X86::JE_4;
1447 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001448
Rafael Espindolaea09c592014-02-18 22:05:46 +00001449 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001450 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001451 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001452 uint32_t BranchWeight = 0;
1453 if (FuncInfo.BPI)
1454 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1455 TrueMBB->getBasicBlock());
1456 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001457 return true;
1458 }
1459 }
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001460 } else if (foldX86XALUIntrinsic(CC, BI, BI->getCondition())) {
1461 // Fake request the condition, otherwise the intrinsic might be completely
1462 // optimized away.
1463 unsigned TmpReg = getRegForValue(BI->getCondition());
1464 if (TmpReg == 0)
1465 return false;
Juergen Ributzka2bce27e2014-06-24 23:51:21 +00001466
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001467 unsigned BranchOpc = X86::GetCondBranchFromCond(CC);
Juergen Ributzka2bce27e2014-06-24 23:51:21 +00001468
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001469 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
1470 .addMBB(TrueMBB);
1471 FastEmitBranch(FalseMBB, DbgLoc);
1472 uint32_t BranchWeight = 0;
1473 if (FuncInfo.BPI)
1474 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1475 TrueMBB->getBasicBlock());
1476 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
1477 return true;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001478 }
1479
1480 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001481 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1482 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001483 unsigned OpReg = getRegForValue(BI->getCondition());
1484 if (OpReg == 0) return false;
1485
Rafael Espindolaea09c592014-02-18 22:05:46 +00001486 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Eli Friedman0eea0292011-04-27 01:34:27 +00001487 .addReg(OpReg).addImm(1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001488 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001489 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001490 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001491 uint32_t BranchWeight = 0;
1492 if (FuncInfo.BPI)
1493 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1494 TrueMBB->getBasicBlock());
1495 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Dan Gohmana5753b32008-09-05 01:06:14 +00001496 return true;
1497}
1498
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001499bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001500 unsigned CReg = 0, OpReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001501 const TargetRegisterClass *RC = nullptr;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001502 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001503 CReg = X86::CL;
1504 RC = &X86::GR8RegClass;
1505 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001506 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1507 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1508 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001509 default: return false;
1510 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001511 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001512 CReg = X86::CX;
1513 RC = &X86::GR16RegClass;
1514 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001515 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1516 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1517 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001518 default: return false;
1519 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001520 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001521 CReg = X86::ECX;
1522 RC = &X86::GR32RegClass;
1523 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001524 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1525 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1526 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001527 default: return false;
1528 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001529 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001530 CReg = X86::RCX;
1531 RC = &X86::GR64RegClass;
1532 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001533 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1534 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1535 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001536 default: return false;
1537 }
1538 } else {
1539 return false;
1540 }
1541
Duncan Sandsf5dda012010-11-03 11:35:31 +00001542 MVT VT;
1543 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001544 return false;
1545
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001546 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1547 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001548
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001549 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1550 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001551 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001552 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001553
1554 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001555 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001556 if (CReg != X86::CL)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001557 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001558 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001559 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001560
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001561 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001562 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001563 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001564 UpdateValueMap(I, ResultReg);
1565 return true;
1566}
1567
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001568bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1569 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1570 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1571 const static bool S = true; // IsSigned
1572 const static bool U = false; // !IsSigned
1573 const static unsigned Copy = TargetOpcode::COPY;
1574 // For the X86 DIV/IDIV instruction, in most cases the dividend
1575 // (numerator) must be in a specific register pair highreg:lowreg,
1576 // producing the quotient in lowreg and the remainder in highreg.
1577 // For most data types, to set up the instruction, the dividend is
1578 // copied into lowreg, and lowreg is sign-extended or zero-extended
1579 // into highreg. The exception is i8, where the dividend is defined
1580 // as a single register rather than a register pair, and we
1581 // therefore directly sign-extend or zero-extend the dividend into
1582 // lowreg, instead of copying, and ignore the highreg.
1583 const static struct DivRemEntry {
1584 // The following portion depends only on the data type.
1585 const TargetRegisterClass *RC;
1586 unsigned LowInReg; // low part of the register pair
1587 unsigned HighInReg; // high part of the register pair
1588 // The following portion depends on both the data type and the operation.
1589 struct DivRemResult {
1590 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1591 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1592 // highreg, or copying a zero into highreg.
1593 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1594 // zero/sign-extending into lowreg for i8.
1595 unsigned DivRemResultReg; // Register containing the desired result.
1596 bool IsOpSigned; // Whether to use signed or unsigned form.
1597 } ResultTable[NumOps];
1598 } OpTable[NumTypes] = {
1599 { &X86::GR8RegClass, X86::AX, 0, {
1600 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1601 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1602 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1603 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1604 }
1605 }, // i8
1606 { &X86::GR16RegClass, X86::AX, X86::DX, {
1607 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1608 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001609 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1610 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001611 }
1612 }, // i16
1613 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1614 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1615 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1616 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1617 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1618 }
1619 }, // i32
1620 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1621 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1622 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001623 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1624 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001625 }
1626 }, // i64
1627 };
1628
1629 MVT VT;
1630 if (!isTypeLegal(I->getType(), VT))
1631 return false;
1632
1633 unsigned TypeIndex, OpIndex;
1634 switch (VT.SimpleTy) {
1635 default: return false;
1636 case MVT::i8: TypeIndex = 0; break;
1637 case MVT::i16: TypeIndex = 1; break;
1638 case MVT::i32: TypeIndex = 2; break;
1639 case MVT::i64: TypeIndex = 3;
1640 if (!Subtarget->is64Bit())
1641 return false;
1642 break;
1643 }
1644
1645 switch (I->getOpcode()) {
1646 default: llvm_unreachable("Unexpected div/rem opcode");
1647 case Instruction::SDiv: OpIndex = 0; break;
1648 case Instruction::SRem: OpIndex = 1; break;
1649 case Instruction::UDiv: OpIndex = 2; break;
1650 case Instruction::URem: OpIndex = 3; break;
1651 }
1652
1653 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1654 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1655 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1656 if (Op0Reg == 0)
1657 return false;
1658 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1659 if (Op1Reg == 0)
1660 return false;
1661
1662 // Move op0 into low-order input register.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001663 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001664 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1665 // Zero-extend or sign-extend into high-order input register.
1666 if (OpEntry.OpSignExtend) {
1667 if (OpEntry.IsOpSigned)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001668 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001669 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001670 else {
1671 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001672 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001673 TII.get(X86::MOV32r0), Zero32);
1674
1675 // Copy the zero into the appropriate sub/super/identical physical
1676 // register. Unfortunately the operations needed are not uniform enough to
1677 // fit neatly into the table above.
1678 if (VT.SimpleTy == MVT::i16) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001680 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001681 .addReg(Zero32, 0, X86::sub_16bit);
1682 } else if (VT.SimpleTy == MVT::i32) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001683 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001684 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001685 .addReg(Zero32);
1686 } else if (VT.SimpleTy == MVT::i64) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001687 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001688 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1689 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1690 }
1691 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001692 }
1693 // Generate the DIV/IDIV instruction.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001694 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001695 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001696 // For i8 remainder, we can't reference AH directly, as we'll end
1697 // up with bogus copies like %R9B = COPY %AH. Reference AX
1698 // instead to prevent AH references in a REX instruction.
1699 //
1700 // The current assumption of the fast register allocator is that isel
1701 // won't generate explicit references to the GPR8_NOREX registers. If
1702 // the allocator and/or the backend get enhanced to be more robust in
1703 // that regard, this can be, and should be, removed.
1704 unsigned ResultReg = 0;
1705 if ((I->getOpcode() == Instruction::SRem ||
1706 I->getOpcode() == Instruction::URem) &&
1707 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1708 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1709 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001710 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbachc35388f2013-07-09 02:07:25 +00001711 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1712
1713 // Shift AX right by 8 bits instead of using AH.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001714 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
Jim Grosbachc35388f2013-07-09 02:07:25 +00001715 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1716
1717 // Now reference the 8-bit subreg of the result.
1718 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1719 /*Kill=*/true, X86::sub_8bit);
1720 }
1721 // Copy the result out of the physreg if we haven't already.
1722 if (!ResultReg) {
1723 ResultReg = createResultReg(TypeEntry.RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001724 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
Jim Grosbachc35388f2013-07-09 02:07:25 +00001725 .addReg(OpEntry.DivRemResultReg);
1726 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001727 UpdateValueMap(I, ResultReg);
1728
1729 return true;
1730}
1731
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001732/// \brief Emit a conditional move instruction (if the are supported) to lower
1733/// the select.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001734bool X86FastISel::X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I) {
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001735 // Check if the subtarget supports these instructions.
1736 if (!Subtarget->hasCMov())
Wesley Peck527da1b2010-11-23 03:31:01 +00001737 return false;
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001738
1739 // FIXME: Add support for i8.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001740 if (RetVT < MVT::i16 || RetVT > MVT::i64)
1741 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001742
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001743 const Value *Cond = I->getOperand(0);
1744 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1745 bool NeedTest = true;
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001746 X86::CondCode CC = X86::COND_NE;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001747
Juergen Ributzka345589e2014-06-27 17:16:34 +00001748 // Optimize conditions coming from a compare if both instructions are in the
Juergen Ributzka296833c2014-06-25 20:06:12 +00001749 // same basic block (values defined in other basic blocks may not have
1750 // initialized registers).
1751 const auto *CI = dyn_cast<CmpInst>(Cond);
1752 if (CI && (CI->getParent() == I->getParent())) {
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001753 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1754
1755 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
1756 static unsigned SETFOpcTable[2][3] = {
1757 { X86::SETNPr, X86::SETEr , X86::TEST8rr },
1758 { X86::SETPr, X86::SETNEr, X86::OR8rr }
1759 };
1760 unsigned *SETFOpc = nullptr;
1761 switch (Predicate) {
1762 default: break;
1763 case CmpInst::FCMP_OEQ:
1764 SETFOpc = &SETFOpcTable[0][0];
1765 Predicate = CmpInst::ICMP_NE;
1766 break;
1767 case CmpInst::FCMP_UNE:
1768 SETFOpc = &SETFOpcTable[1][0];
1769 Predicate = CmpInst::ICMP_NE;
1770 break;
1771 }
1772
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001773 bool NeedSwap;
Craig Topper9f62d802014-06-27 05:18:21 +00001774 std::tie(CC, NeedSwap) = getX86ConditionCode(Predicate);
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001775 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001776
1777 const Value *CmpLHS = CI->getOperand(0);
1778 const Value *CmpRHS = CI->getOperand(1);
1779 if (NeedSwap)
1780 std::swap(CmpLHS, CmpRHS);
1781
1782 EVT CmpVT = TLI.getValueType(CmpLHS->getType());
1783 // Emit a compare of the LHS and RHS, setting the flags.
1784 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT))
1785 return false;
1786
1787 if (SETFOpc) {
1788 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1789 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1790 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1791 FlagReg1);
1792 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1793 FlagReg2);
1794 auto const &II = TII.get(SETFOpc[2]);
1795 if (II.getNumDefs()) {
1796 unsigned TmpReg = createResultReg(&X86::GR8RegClass);
1797 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, TmpReg)
1798 .addReg(FlagReg2).addReg(FlagReg1);
1799 } else {
1800 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
1801 .addReg(FlagReg2).addReg(FlagReg1);
1802 }
1803 }
1804 NeedTest = false;
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001805 } else if (foldX86XALUIntrinsic(CC, I, Cond)) {
1806 // Fake request the condition, otherwise the intrinsic might be completely
1807 // optimized away.
1808 unsigned TmpReg = getRegForValue(Cond);
1809 if (TmpReg == 0)
1810 return false;
Juergen Ributzka2bce27e2014-06-24 23:51:21 +00001811
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001812 NeedTest = false;
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001813 }
1814
1815 if (NeedTest) {
1816 // Selects operate on i1, however, CondReg is 8 bits width and may contain
1817 // garbage. Indeed, only the less significant bit is supposed to be
1818 // accurate. If we read more than the lsb, we may see non-zero values
1819 // whereas lsb is zero. Therefore, we have to truncate Op0Reg to i1 for
1820 // the select. This is achieved by performing TEST against 1.
1821 unsigned CondReg = getRegForValue(Cond);
1822 if (CondReg == 0)
1823 return false;
1824 bool CondIsKill = hasTrivialKill(Cond);
1825
1826 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1827 .addReg(CondReg, getKillRegState(CondIsKill)).addImm(1);
1828 }
1829
1830 const Value *LHS = I->getOperand(1);
1831 const Value *RHS = I->getOperand(2);
1832
1833 unsigned RHSReg = getRegForValue(RHS);
1834 bool RHSIsKill = hasTrivialKill(RHS);
1835
1836 unsigned LHSReg = getRegForValue(LHS);
1837 bool LHSIsKill = hasTrivialKill(LHS);
1838
1839 if (!LHSReg || !RHSReg)
1840 return false;
1841
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001842 unsigned Opc = X86::getCMovFromCond(CC, RC->getSize());
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001843 unsigned ResultReg = FastEmitInst_rr(Opc, RC, RHSReg, RHSIsKill,
1844 LHSReg, LHSIsKill);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001845 UpdateValueMap(I, ResultReg);
1846 return true;
1847}
1848
Juergen Ributzka21d56082014-06-23 21:55:40 +00001849/// \brief Emit SSE instructions to lower the select.
1850///
1851/// Try to use SSE1/SSE2 instructions to simulate a select without branches.
1852/// This lowers fp selects into a CMP/AND/ANDN/OR sequence when the necessary
1853/// SSE instructions are available.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001854bool X86FastISel::X86FastEmitSSESelect(MVT RetVT, const Instruction *I) {
Juergen Ributzka345589e2014-06-27 17:16:34 +00001855 // Optimize conditions coming from a compare if both instructions are in the
Juergen Ributzka296833c2014-06-25 20:06:12 +00001856 // same basic block (values defined in other basic blocks may not have
1857 // initialized registers).
Juergen Ributzka21d56082014-06-23 21:55:40 +00001858 const auto *CI = dyn_cast<FCmpInst>(I->getOperand(0));
Juergen Ributzka296833c2014-06-25 20:06:12 +00001859 if (!CI || (CI->getParent() != I->getParent()))
Juergen Ributzka21d56082014-06-23 21:55:40 +00001860 return false;
1861
1862 if (I->getType() != CI->getOperand(0)->getType() ||
1863 !((Subtarget->hasSSE1() && RetVT == MVT::f32) ||
1864 (Subtarget->hasSSE2() && RetVT == MVT::f64) ))
1865 return false;
1866
1867 const Value *CmpLHS = CI->getOperand(0);
1868 const Value *CmpRHS = CI->getOperand(1);
1869 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1870
1871 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1872 // We don't have to materialize a zero constant for this case and can just use
1873 // %x again on the RHS.
1874 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1875 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1876 if (CmpRHSC && CmpRHSC->isNullValue())
1877 CmpRHS = CmpLHS;
1878 }
1879
1880 unsigned CC;
1881 bool NeedSwap;
Juergen Ributzka345589e2014-06-27 17:16:34 +00001882 std::tie(CC, NeedSwap) = getX86SSEConditionCode(Predicate);
Juergen Ributzka21d56082014-06-23 21:55:40 +00001883 if (CC > 7)
1884 return false;
1885
1886 if (NeedSwap)
1887 std::swap(CmpLHS, CmpRHS);
1888
1889 static unsigned OpcTable[2][2][4] = {
1890 { { X86::CMPSSrr, X86::FsANDPSrr, X86::FsANDNPSrr, X86::FsORPSrr },
1891 { X86::VCMPSSrr, X86::VFsANDPSrr, X86::VFsANDNPSrr, X86::VFsORPSrr } },
1892 { { X86::CMPSDrr, X86::FsANDPDrr, X86::FsANDNPDrr, X86::FsORPDrr },
1893 { X86::VCMPSDrr, X86::VFsANDPDrr, X86::VFsANDNPDrr, X86::VFsORPDrr } }
1894 };
1895
1896 bool HasAVX = Subtarget->hasAVX();
1897 unsigned *Opc = nullptr;
1898 switch (RetVT.SimpleTy) {
1899 default: return false;
1900 case MVT::f32: Opc = &OpcTable[0][HasAVX][0]; break;
1901 case MVT::f64: Opc = &OpcTable[1][HasAVX][0]; break;
1902 }
1903
1904 const Value *LHS = I->getOperand(1);
1905 const Value *RHS = I->getOperand(2);
1906
1907 unsigned LHSReg = getRegForValue(LHS);
1908 bool LHSIsKill = hasTrivialKill(LHS);
1909
1910 unsigned RHSReg = getRegForValue(RHS);
1911 bool RHSIsKill = hasTrivialKill(RHS);
1912
1913 unsigned CmpLHSReg = getRegForValue(CmpLHS);
1914 bool CmpLHSIsKill = hasTrivialKill(CmpLHS);
1915
1916 unsigned CmpRHSReg = getRegForValue(CmpRHS);
1917 bool CmpRHSIsKill = hasTrivialKill(CmpRHS);
1918
1919 if (!LHSReg || !RHSReg || !CmpLHS || !CmpRHS)
1920 return false;
1921
1922 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1923 unsigned CmpReg = FastEmitInst_rri(Opc[0], RC, CmpLHSReg, CmpLHSIsKill,
1924 CmpRHSReg, CmpRHSIsKill, CC);
1925 unsigned AndReg = FastEmitInst_rr(Opc[1], RC, CmpReg, /*IsKill=*/false,
1926 LHSReg, LHSIsKill);
1927 unsigned AndNReg = FastEmitInst_rr(Opc[2], RC, CmpReg, /*IsKill=*/true,
1928 RHSReg, RHSIsKill);
1929 unsigned ResultReg = FastEmitInst_rr(Opc[3], RC, AndNReg, /*IsKill=*/true,
1930 AndReg, /*IsKill=*/true);
1931 UpdateValueMap(I, ResultReg);
1932 return true;
1933}
1934
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001935bool X86FastISel::X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I) {
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00001936 // These are pseudo CMOV instructions and will be later expanded into control-
1937 // flow.
1938 unsigned Opc;
1939 switch (RetVT.SimpleTy) {
1940 default: return false;
1941 case MVT::i8: Opc = X86::CMOV_GR8; break;
1942 case MVT::i16: Opc = X86::CMOV_GR16; break;
1943 case MVT::i32: Opc = X86::CMOV_GR32; break;
1944 case MVT::f32: Opc = X86::CMOV_FR32; break;
1945 case MVT::f64: Opc = X86::CMOV_FR64; break;
1946 }
1947
1948 const Value *Cond = I->getOperand(0);
1949 X86::CondCode CC = X86::COND_NE;
Juergen Ributzka296833c2014-06-25 20:06:12 +00001950
Juergen Ributzka345589e2014-06-27 17:16:34 +00001951 // Optimize conditions coming from a compare if both instructions are in the
Juergen Ributzka296833c2014-06-25 20:06:12 +00001952 // same basic block (values defined in other basic blocks may not have
1953 // initialized registers).
1954 const auto *CI = dyn_cast<CmpInst>(Cond);
1955 if (CI && (CI->getParent() == I->getParent())) {
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00001956 bool NeedSwap;
Craig Topper9f62d802014-06-27 05:18:21 +00001957 std::tie(CC, NeedSwap) = getX86ConditionCode(CI->getPredicate());
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00001958 if (CC > X86::LAST_VALID_COND)
1959 return false;
1960
1961 const Value *CmpLHS = CI->getOperand(0);
1962 const Value *CmpRHS = CI->getOperand(1);
1963
1964 if (NeedSwap)
1965 std::swap(CmpLHS, CmpRHS);
1966
1967 EVT CmpVT = TLI.getValueType(CmpLHS->getType());
1968 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT))
1969 return false;
1970 } else {
1971 unsigned CondReg = getRegForValue(Cond);
1972 if (CondReg == 0)
1973 return false;
1974 bool CondIsKill = hasTrivialKill(Cond);
1975 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1976 .addReg(CondReg, getKillRegState(CondIsKill)).addImm(1);
1977 }
1978
1979 const Value *LHS = I->getOperand(1);
1980 const Value *RHS = I->getOperand(2);
1981
1982 unsigned LHSReg = getRegForValue(LHS);
1983 bool LHSIsKill = hasTrivialKill(LHS);
1984
1985 unsigned RHSReg = getRegForValue(RHS);
1986 bool RHSIsKill = hasTrivialKill(RHS);
1987
1988 if (!LHSReg || !RHSReg)
1989 return false;
1990
1991 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1992
1993 unsigned ResultReg =
1994 FastEmitInst_rri(Opc, RC, RHSReg, RHSIsKill, LHSReg, LHSIsKill, CC);
1995 UpdateValueMap(I, ResultReg);
1996 return true;
1997}
1998
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001999bool X86FastISel::X86SelectSelect(const Instruction *I) {
2000 MVT RetVT;
2001 if (!isTypeLegal(I->getType(), RetVT))
2002 return false;
2003
2004 // Check if we can fold the select.
2005 if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) {
2006 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2007 const Value *Opnd = nullptr;
2008 switch (Predicate) {
2009 default: break;
2010 case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break;
2011 case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break;
2012 }
2013 // No need for a select anymore - this is an unconditional move.
2014 if (Opnd) {
2015 unsigned OpReg = getRegForValue(Opnd);
2016 if (OpReg == 0)
2017 return false;
2018 bool OpIsKill = hasTrivialKill(Opnd);
2019 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2020 unsigned ResultReg = createResultReg(RC);
2021 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2022 TII.get(TargetOpcode::COPY), ResultReg)
2023 .addReg(OpReg, getKillRegState(OpIsKill));
2024 UpdateValueMap(I, ResultReg);
2025 return true;
2026 }
2027 }
2028
2029 // First try to use real conditional move instructions.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00002030 if (X86FastEmitCMoveSelect(RetVT, I))
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00002031 return true;
2032
Juergen Ributzka345589e2014-06-27 17:16:34 +00002033 // Try to use a sequence of SSE instructions to simulate a conditional move.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00002034 if (X86FastEmitSSESelect(RetVT, I))
Juergen Ributzka21d56082014-06-23 21:55:40 +00002035 return true;
2036
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00002037 // Fall-back to pseudo conditional move instructions, which will be later
2038 // converted to control-flow.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00002039 if (X86FastEmitPseudoSelect(RetVT, I))
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00002040 return true;
2041
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00002042 return false;
2043}
2044
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002045bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00002046 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002047 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00002048 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002049 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002050 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00002051 unsigned OpReg = getRegForValue(V);
2052 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00002053 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002054 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002055 TII.get(X86::CVTSS2SDrr), ResultReg)
2056 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00002057 UpdateValueMap(I, ResultReg);
2058 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00002059 }
2060 }
2061
2062 return false;
2063}
2064
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002065bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002066 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00002067 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002068 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002069 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00002070 unsigned OpReg = getRegForValue(V);
2071 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00002072 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002073 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002074 TII.get(X86::CVTSD2SSrr), ResultReg)
2075 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002076 UpdateValueMap(I, ResultReg);
2077 return true;
2078 }
2079 }
2080 }
2081
2082 return false;
2083}
2084
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002085bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002086 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2087 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00002088
Eli Friedmanc7035512011-05-25 23:49:02 +00002089 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00002090 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00002091 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00002092 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00002093 return false;
2094
2095 unsigned InputReg = getRegForValue(I->getOperand(0));
2096 if (!InputReg)
2097 // Unhandled operand. Halt "fast" selection and bail.
2098 return false;
2099
Eli Friedmanc7035512011-05-25 23:49:02 +00002100 if (SrcVT == MVT::i8) {
2101 // Truncate from i8 to i1; no code needed.
2102 UpdateValueMap(I, InputReg);
2103 return true;
2104 }
Evan Chengb9286692008-09-07 08:47:42 +00002105
Eli Friedmanc7035512011-05-25 23:49:02 +00002106 if (!Subtarget->is64Bit()) {
2107 // If we're on x86-32; we can't extract an i8 from a general register.
2108 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00002109 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
2110 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
2111 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00002112 unsigned CopyReg = createResultReg(CopyRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002113 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Eli Friedmanc7035512011-05-25 23:49:02 +00002114 CopyReg).addReg(InputReg);
2115 InputReg = CopyReg;
2116 }
2117
2118 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00002119 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00002120 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00002121 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00002122 if (!ResultReg)
2123 return false;
2124
2125 UpdateValueMap(I, ResultReg);
2126 return true;
2127}
2128
Eli Friedman60afcc22011-05-20 22:21:04 +00002129bool X86FastISel::IsMemcpySmall(uint64_t Len) {
2130 return Len <= (Subtarget->is64Bit() ? 32 : 16);
2131}
2132
Eli Friedmanbcc69142011-04-27 01:45:07 +00002133bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
2134 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00002135
Eli Friedmanbcc69142011-04-27 01:45:07 +00002136 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00002137 if (!IsMemcpySmall(Len))
2138 return false;
2139
2140 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00002141
2142 // We don't care about alignment here since we just emit integer accesses.
2143 while (Len) {
2144 MVT VT;
2145 if (Len >= 8 && i64Legal)
2146 VT = MVT::i64;
2147 else if (Len >= 4)
2148 VT = MVT::i32;
2149 else if (Len >= 2)
2150 VT = MVT::i16;
2151 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00002152 VT = MVT::i8;
2153 }
2154
2155 unsigned Reg;
Juergen Ributzka349777d2014-06-12 23:27:57 +00002156 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
2157 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
Eli Friedmanbcc69142011-04-27 01:45:07 +00002158 assert(RV && "Failed to emit load or store??");
2159
2160 unsigned Size = VT.getSizeInBits()/8;
2161 Len -= Size;
2162 DestAM.Disp += Size;
2163 SrcAM.Disp += Size;
2164 }
2165
2166 return true;
2167}
2168
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002169static bool isCommutativeIntrinsic(IntrinsicInst const &I) {
2170 switch (I.getIntrinsicID()) {
2171 case Intrinsic::sadd_with_overflow:
2172 case Intrinsic::uadd_with_overflow:
2173 case Intrinsic::smul_with_overflow:
2174 case Intrinsic::umul_with_overflow:
2175 return true;
2176 default:
2177 return false;
2178 }
2179}
2180
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002181bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00002182 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00002183 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00002184 default: return false;
Juergen Ributzka4dc95872014-06-11 21:44:44 +00002185 case Intrinsic::frameaddress: {
2186 Type *RetTy = I.getCalledFunction()->getReturnType();
2187
2188 MVT VT;
2189 if (!isTypeLegal(RetTy, VT))
2190 return false;
2191
2192 unsigned Opc;
2193 const TargetRegisterClass *RC = nullptr;
2194
2195 switch (VT.SimpleTy) {
2196 default: llvm_unreachable("Invalid result type for frameaddress.");
2197 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
2198 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
2199 }
2200
2201 // This needs to be set before we call getFrameRegister, otherwise we get
2202 // the wrong frame register.
2203 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2204 MFI->setFrameAddressIsTaken(true);
2205
2206 const X86RegisterInfo *RegInfo =
2207 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
2208 unsigned FrameReg = RegInfo->getFrameRegister(*(FuncInfo.MF));
2209 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
2210 (FrameReg == X86::EBP && VT == MVT::i32)) &&
2211 "Invalid Frame Register!");
2212
2213 // Always make a copy of the frame register to to a vreg first, so that we
2214 // never directly reference the frame register (the TwoAddressInstruction-
2215 // Pass doesn't like that).
2216 unsigned SrcReg = createResultReg(RC);
2217 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2218 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
2219
2220 // Now recursively load from the frame address.
2221 // movq (%rbp), %rax
2222 // movq (%rax), %rax
2223 // movq (%rax), %rax
2224 // ...
2225 unsigned DestReg;
2226 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2227 while (Depth--) {
2228 DestReg = createResultReg(RC);
2229 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2230 TII.get(Opc), DestReg), SrcReg);
2231 SrcReg = DestReg;
2232 }
2233
2234 UpdateValueMap(&I, SrcReg);
2235 return true;
2236 }
Chris Lattner91328b32011-04-19 05:52:03 +00002237 case Intrinsic::memcpy: {
2238 const MemCpyInst &MCI = cast<MemCpyInst>(I);
2239 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00002240 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00002241 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00002242
Eli Friedmancd2124a2011-06-10 23:39:36 +00002243 if (isa<ConstantInt>(MCI.getLength())) {
2244 // Small memcpy's are common enough that we want to do them
2245 // without a call if possible.
2246 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
2247 if (IsMemcpySmall(Len)) {
2248 X86AddressMode DestAM, SrcAM;
2249 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
2250 !X86SelectAddress(MCI.getRawSource(), SrcAM))
2251 return false;
2252 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
2253 return true;
2254 }
2255 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002256
Eli Friedmancd2124a2011-06-10 23:39:36 +00002257 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2258 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00002259 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00002260
Eli Friedmancd2124a2011-06-10 23:39:36 +00002261 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
2262 return false;
2263
2264 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00002265 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00002266 case Intrinsic::memset: {
2267 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00002268
Nick Lewyckya530a4d2011-08-02 00:40:16 +00002269 if (MSI.isVolatile())
2270 return false;
2271
Eli Friedmancd2124a2011-06-10 23:39:36 +00002272 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2273 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
2274 return false;
2275
2276 if (MSI.getDestAddressSpace() > 255)
2277 return false;
2278
2279 return DoSelectCall(&I, "memset");
2280 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002281 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00002282 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002283 EVT PtrTy = TLI.getPointerTy();
2284
Gabor Greif83205af2010-06-26 11:51:52 +00002285 const Value *Op1 = I.getArgOperand(0); // The guard's value.
2286 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002287
Josh Magee22b8ba22013-12-19 03:17:11 +00002288 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
2289
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002290 // Grab the frame index.
2291 X86AddressMode AM;
2292 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00002293 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002294 return true;
2295 }
Dale Johannesend5575f22010-01-26 00:09:58 +00002296 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002297 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00002298 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00002299 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00002300 if (!X86SelectAddress(DI->getAddress(), AM))
2301 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00002302 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00002303 // FIXME may need to add RegState::Debug to any registers produced,
2304 // although ESP/EBP should be the only ones at the moment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002305 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002306 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00002307 return true;
2308 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002309 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002310 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002311 return true;
2312 }
Juergen Ributzka272b5702014-06-11 23:11:02 +00002313 case Intrinsic::sqrt: {
2314 if (!Subtarget->hasSSE1())
2315 return false;
2316
2317 Type *RetTy = I.getCalledFunction()->getReturnType();
2318
2319 MVT VT;
2320 if (!isTypeLegal(RetTy, VT))
2321 return false;
2322
Juergen Ributzka345589e2014-06-27 17:16:34 +00002323 // Unfortunately we can't use FastEmit_r, because the AVX version of FSQRT
Juergen Ributzka272b5702014-06-11 23:11:02 +00002324 // is not generated by FastISel yet.
2325 // FIXME: Update this code once tablegen can handle it.
2326 static const unsigned SqrtOpc[2][2] = {
2327 {X86::SQRTSSr, X86::VSQRTSSr},
2328 {X86::SQRTSDr, X86::VSQRTSDr}
2329 };
2330 bool HasAVX = Subtarget->hasAVX();
2331 unsigned Opc;
2332 const TargetRegisterClass *RC;
2333 switch (VT.SimpleTy) {
2334 default: return false;
2335 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
2336 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
2337 }
2338
2339 const Value *SrcVal = I.getArgOperand(0);
2340 unsigned SrcReg = getRegForValue(SrcVal);
2341
2342 if (SrcReg == 0)
2343 return false;
2344
2345 unsigned ImplicitDefReg = 0;
2346 if (HasAVX) {
2347 ImplicitDefReg = createResultReg(RC);
2348 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2349 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2350 }
2351
2352 unsigned ResultReg = createResultReg(RC);
2353 MachineInstrBuilder MIB;
2354 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
2355 ResultReg);
2356
2357 if (ImplicitDefReg)
2358 MIB.addReg(ImplicitDefReg);
2359
2360 MIB.addReg(SrcReg);
2361
2362 UpdateValueMap(&I, ResultReg);
2363 return true;
2364 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002365 case Intrinsic::sadd_with_overflow:
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002366 case Intrinsic::uadd_with_overflow:
2367 case Intrinsic::ssub_with_overflow:
2368 case Intrinsic::usub_with_overflow:
2369 case Intrinsic::smul_with_overflow:
2370 case Intrinsic::umul_with_overflow: {
2371 // This implements the basic lowering of the xalu with overflow intrinsics
Juergen Ributzka345589e2014-06-27 17:16:34 +00002372 // into add/sub/mul followed by either seto or setb.
Bill Wendling80b34b32008-12-09 02:42:50 +00002373 const Function *Callee = I.getCalledFunction();
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002374 auto *Ty = cast<StructType>(Callee->getReturnType());
2375 Type *RetTy = Ty->getTypeAtIndex(0U);
2376 Type *CondTy = Ty->getTypeAtIndex(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00002377
Duncan Sandsf5dda012010-11-03 11:35:31 +00002378 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00002379 if (!isTypeLegal(RetTy, VT))
2380 return false;
2381
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002382 if (VT < MVT::i8 || VT > MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00002383 return false;
2384
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002385 const Value *LHS = I.getArgOperand(0);
2386 const Value *RHS = I.getArgOperand(1);
2387
Juergen Ributzka345589e2014-06-27 17:16:34 +00002388 // Canonicalize immediate to the RHS.
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002389 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2390 isCommutativeIntrinsic(I))
2391 std::swap(LHS, RHS);
2392
2393 unsigned BaseOpc, CondOpc;
2394 switch (I.getIntrinsicID()) {
2395 default: llvm_unreachable("Unexpected intrinsic!");
2396 case Intrinsic::sadd_with_overflow:
2397 BaseOpc = ISD::ADD; CondOpc = X86::SETOr; break;
2398 case Intrinsic::uadd_with_overflow:
2399 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
2400 case Intrinsic::ssub_with_overflow:
2401 BaseOpc = ISD::SUB; CondOpc = X86::SETOr; break;
2402 case Intrinsic::usub_with_overflow:
2403 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
2404 case Intrinsic::smul_with_overflow:
2405 BaseOpc = ISD::MUL; CondOpc = X86::SETOr; break;
2406 case Intrinsic::umul_with_overflow:
2407 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
2408 }
2409
2410 unsigned LHSReg = getRegForValue(LHS);
2411 if (LHSReg == 0)
2412 return false;
2413 bool LHSIsKill = hasTrivialKill(LHS);
2414
2415 unsigned ResultReg = 0;
2416 // Check if we have an immediate version.
2417 if (auto const *C = dyn_cast<ConstantInt>(RHS)) {
2418 ResultReg = FastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2419 C->getZExtValue());
2420 }
2421
2422 unsigned RHSReg;
2423 bool RHSIsKill;
2424 if (!ResultReg) {
2425 RHSReg = getRegForValue(RHS);
2426 if (RHSReg == 0)
2427 return false;
2428 RHSIsKill = hasTrivialKill(RHS);
2429 ResultReg = FastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2430 RHSIsKill);
2431 }
2432
2433 // FastISel doesn't have a pattern for X86::MUL*r. Emit it manually.
2434 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
2435 static const unsigned MULOpc[] =
2436 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
2437 static const unsigned Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
2438 // First copy the first operand into RAX, which is an implicit input to
2439 // the X86::MUL*r instruction.
2440 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2441 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2442 .addReg(LHSReg, getKillRegState(LHSIsKill));
2443 ResultReg = FastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2444 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
2445 }
2446
2447 if (!ResultReg)
Bill Wendling80b34b32008-12-09 02:42:50 +00002448 return false;
2449
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002450 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
2451 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
2452 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
2453 ResultReg2);
Eli Friedmana4d4a012011-05-16 21:06:17 +00002454
2455 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00002456 return true;
2457 }
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002458 case Intrinsic::x86_sse_cvttss2si:
2459 case Intrinsic::x86_sse_cvttss2si64:
2460 case Intrinsic::x86_sse2_cvttsd2si:
2461 case Intrinsic::x86_sse2_cvttsd2si64: {
2462 bool IsInputDouble;
2463 switch (I.getIntrinsicID()) {
2464 default: llvm_unreachable("Unexpected intrinsic.");
2465 case Intrinsic::x86_sse_cvttss2si:
2466 case Intrinsic::x86_sse_cvttss2si64:
2467 if (!Subtarget->hasSSE1())
2468 return false;
2469 IsInputDouble = false;
2470 break;
2471 case Intrinsic::x86_sse2_cvttsd2si:
2472 case Intrinsic::x86_sse2_cvttsd2si64:
2473 if (!Subtarget->hasSSE2())
2474 return false;
2475 IsInputDouble = true;
2476 break;
2477 }
2478
2479 Type *RetTy = I.getCalledFunction()->getReturnType();
2480 MVT VT;
2481 if (!isTypeLegal(RetTy, VT))
2482 return false;
2483
2484 static const unsigned CvtOpc[2][2][2] = {
2485 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
2486 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
2487 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
2488 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
2489 };
2490 bool HasAVX = Subtarget->hasAVX();
2491 unsigned Opc;
2492 switch (VT.SimpleTy) {
2493 default: llvm_unreachable("Unexpected result type.");
2494 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
2495 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
2496 }
2497
2498 // Check if we can fold insertelement instructions into the convert.
2499 const Value *Op = I.getArgOperand(0);
2500 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
2501 const Value *Index = IE->getOperand(2);
2502 if (!isa<ConstantInt>(Index))
2503 break;
2504 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
2505
2506 if (Idx == 0) {
2507 Op = IE->getOperand(1);
2508 break;
2509 }
2510 Op = IE->getOperand(0);
2511 }
2512
2513 unsigned Reg = getRegForValue(Op);
2514 if (Reg == 0)
2515 return false;
2516
2517 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
2518 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2519 .addReg(Reg);
2520
2521 UpdateValueMap(&I, ResultReg);
2522 return true;
2523 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002524 }
2525}
2526
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002527bool X86FastISel::FastLowerArguments() {
2528 if (!FuncInfo.CanLowerReturn)
2529 return false;
2530
2531 const Function *F = FuncInfo.Fn;
2532 if (F->isVarArg())
2533 return false;
2534
2535 CallingConv::ID CC = F->getCallingConv();
2536 if (CC != CallingConv::C)
2537 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00002538
2539 if (Subtarget->isCallingConvWin64(CC))
2540 return false;
2541
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002542 if (!Subtarget->is64Bit())
2543 return false;
2544
2545 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002546 unsigned GPRCnt = 0;
2547 unsigned FPRCnt = 0;
2548 unsigned Idx = 0;
2549 for (auto const &Arg : F->args()) {
2550 // The first argument is at index 1.
2551 ++Idx;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002552 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2553 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2554 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2555 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2556 return false;
2557
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002558 Type *ArgTy = Arg.getType();
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002559 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
2560 return false;
2561
2562 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00002563 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002564 switch (ArgVT.getSimpleVT().SimpleTy) {
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002565 default: return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002566 case MVT::i32:
2567 case MVT::i64:
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002568 ++GPRCnt;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002569 break;
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002570 case MVT::f32:
2571 case MVT::f64:
2572 if (!Subtarget->hasSSE1())
2573 return false;
2574 ++FPRCnt;
2575 break;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002576 }
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002577
2578 if (GPRCnt > 6)
2579 return false;
2580
2581 if (FPRCnt > 8)
2582 return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002583 }
2584
Craig Topper840beec2014-04-04 05:16:06 +00002585 static const MCPhysReg GPR32ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002586 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
2587 };
Craig Topper840beec2014-04-04 05:16:06 +00002588 static const MCPhysReg GPR64ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002589 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
2590 };
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002591 static const MCPhysReg XMMArgRegs[] = {
2592 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2593 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2594 };
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002595
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002596 unsigned GPRIdx = 0;
2597 unsigned FPRIdx = 0;
2598 for (auto const &Arg : F->args()) {
2599 MVT VT = TLI.getSimpleValueType(Arg.getType());
2600 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
2601 unsigned SrcReg;
2602 switch (VT.SimpleTy) {
2603 default: llvm_unreachable("Unexpected value type.");
2604 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
2605 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
2606 case MVT::f32: // fall-through
2607 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
2608 }
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002609 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2610 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2611 // Without this, EmitLiveInCopies may eliminate the livein if its only
2612 // use is a bitcast (which isn't turned into an instruction).
2613 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002614 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002615 TII.get(TargetOpcode::COPY), ResultReg)
2616 .addReg(DstReg, getKillRegState(true));
2617 UpdateValueMap(&Arg, ResultReg);
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002618 }
2619 return true;
2620}
2621
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002622bool X86FastISel::X86SelectCall(const Instruction *I) {
2623 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00002624 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002625
2626 // Can't handle inline asm yet.
2627 if (isa<InlineAsm>(Callee))
2628 return false;
2629
Bill Wendling80b34b32008-12-09 02:42:50 +00002630 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002631 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00002632 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002633
Chad Rosierdf42cf32012-12-11 00:18:02 +00002634 // Allow SelectionDAG isel to handle tail calls.
2635 if (cast<CallInst>(I)->isTailCall())
2636 return false;
2637
Craig Topper062a2ba2014-04-25 05:30:21 +00002638 return DoSelectCall(I, nullptr);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002639}
2640
Rafael Espindola73173c52012-07-25 15:42:45 +00002641static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
2642 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002643 if (Subtarget.is64Bit())
2644 return 0;
Rafael Espindola32cb5ac2013-12-12 16:06:58 +00002645 if (Subtarget.getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002646 return 0;
2647 CallingConv::ID CC = CS.getCallingConv();
2648 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
2649 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002650 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002651 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002652 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00002653 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002654 return 4;
2655}
2656
Eli Friedmancd2124a2011-06-10 23:39:36 +00002657// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
2658bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
2659 const CallInst *CI = cast<CallInst>(I);
2660 const Value *Callee = CI->getCalledValue();
2661
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002662 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002663 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002664 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00002665 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002666 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00002667 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
2668 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002669 return false;
2670
Evan Chengd10089a2010-01-27 00:00:57 +00002671 // fastcc with -tailcallopt is intended to provide a guaranteed
2672 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002673 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00002674 return false;
2675
Chris Lattner229907c2011-07-18 04:54:35 +00002676 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2677 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002678 bool isVarArg = FTy->isVarArg();
2679
2680 // Don't know how to handle Win64 varargs yet. Nothing special needed for
2681 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00002682 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002683 return false;
2684
Reid Klecknerf5b76512014-01-31 23:50:57 +00002685 // Don't know about inalloca yet.
2686 if (CS.hasInAllocaArgument())
2687 return false;
2688
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002689 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00002690 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002691 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002692 return false;
2693
Eli Friedman7b279422011-05-17 18:29:03 +00002694 // Check whether the function can return without sret-demotion.
2695 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002696 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00002697 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00002698 *FuncInfo.MF, FTy->isVarArg(),
2699 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002700 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00002701 return false;
2702
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002703 // Materialize callee address in a register. FIXME: GV address can be
2704 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00002705 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00002706 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00002707 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002708 unsigned CalleeOp = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002709 const GlobalValue *GV = nullptr;
2710 if (CalleeAM.GV != nullptr) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002711 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00002712 } else if (CalleeAM.Base.Reg != 0) {
2713 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002714 } else
2715 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002716
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002717 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002718 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002719 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002720 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002721 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00002722 unsigned arg_size = CS.arg_size();
2723 Args.reserve(arg_size);
2724 ArgVals.reserve(arg_size);
2725 ArgVTs.reserve(arg_size);
2726 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002727 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002728 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00002729 // If we're lowering a mem intrinsic instead of a regular call, skip the
2730 // last two arguments, which should not passed to the underlying functions.
2731 if (MemIntName && e-i <= 2)
2732 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002733 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002734 ISD::ArgFlagsTy Flags;
2735 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002736 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002737 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002738 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002739 Flags.setZExt();
2740
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002741 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00002742 PointerType *Ty = cast<PointerType>(ArgVal->getType());
2743 Type *ElementTy = Ty->getElementType();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002744 unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
Eli Friedman60afcc22011-05-20 22:21:04 +00002745 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
2746 if (!FrameAlign)
2747 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
2748 Flags.setByVal();
2749 Flags.setByValSize(FrameSize);
2750 Flags.setByValAlign(FrameAlign);
2751 if (!IsMemcpySmall(FrameSize))
2752 return false;
2753 }
2754
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002755 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00002756 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002757 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00002758 Flags.setNest();
2759
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002760 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
2761 // instruction. This is safe because it is common to all fastisel supported
2762 // calling conventions on x86.
2763 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
2764 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
2765 CI->getBitWidth() == 16) {
2766 if (Flags.isSExt())
2767 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
2768 else
2769 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
2770 }
2771 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002772
Chris Lattner5f4b7832011-04-19 05:09:50 +00002773 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002774
Chris Lattner34a08c22011-04-19 05:15:59 +00002775 // Passing bools around ends up doing a trunc to i1 and passing it.
2776 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00002777 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2778 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2779 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002780 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2781 ArgReg = getRegForValue(ArgVal);
2782 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002783
Chris Lattner5f4b7832011-04-19 05:09:50 +00002784 MVT ArgVT;
2785 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002786
Chris Lattner5f4b7832011-04-19 05:09:50 +00002787 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2788 ArgVal->hasOneUse(), 1);
2789 } else {
2790 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002791 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002792
Chris Lattner34a08c22011-04-19 05:15:59 +00002793 if (ArgReg == 0) return false;
2794
Chris Lattner229907c2011-07-18 04:54:35 +00002795 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002796 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002797 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002798 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002799 if (ArgVT == MVT::x86mmx)
2800 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002801 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002802 Flags.setOrigAlign(OriginalAlignment);
2803
Chris Lattner5f4b7832011-04-19 05:09:50 +00002804 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002805 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002806 ArgVTs.push_back(ArgVT);
2807 ArgFlags.push_back(Flags);
2808 }
2809
2810 // Analyze operands of the call, assigning locations to each operand.
2811 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002812 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002813 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002814
Dan Gohman47a07242010-06-01 21:09:47 +00002815 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002816 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002817 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002818
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002819 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002820
2821 // Get a count of how many bytes are to be pushed on the stack.
2822 unsigned NumBytes = CCInfo.getNextStackOffset();
2823
2824 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002825 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002826 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002827 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002828
Chris Lattner3ba29352008-10-15 05:30:52 +00002829 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002830 // copies / loads.
2831 SmallVector<unsigned, 4> RegArgs;
2832 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2833 CCValAssign &VA = ArgLocs[i];
2834 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002835 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002836
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002837 // Promote the value if needed.
2838 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002839 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002840 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002841 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2842 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002843 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2844 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002845 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002846 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002847 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002848 }
2849 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002850 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2851 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002852 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2853 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002854 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002855 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002856 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002857 }
2858 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002859 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2860 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002861 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2862 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002863 if (!Emitted)
2864 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002865 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002866 if (!Emitted)
2867 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2868 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002869
Chris Lattner2d7df022011-01-05 22:26:52 +00002870 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002871 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002872 break;
2873 }
Dan Gohman8c795692009-08-05 05:33:42 +00002874 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002875 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002876 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002877 assert(BC != 0 && "Failed to emit a bitcast!");
2878 Arg = BC;
2879 ArgVT = VA.getLocVT();
2880 break;
2881 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002882 case CCValAssign::VExt:
2883 // VExt has not been implemented, so this should be impossible to reach
2884 // for now. However, fallback to Selection DAG isel once implemented.
2885 return false;
2886 case CCValAssign::Indirect:
2887 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2888 // support this.
2889 return false;
Lang Hames06234ec2014-01-14 19:56:36 +00002890 case CCValAssign::FPExt:
2891 llvm_unreachable("Unexpected loc info!");
Evan Cheng6500d172008-09-08 06:35:17 +00002892 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002893
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002894 if (VA.isRegLoc()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002895 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2896 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002897 RegArgs.push_back(VA.getLocReg());
2898 } else {
2899 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002900 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002901 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2902 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002903 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002904 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002905 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002906 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002907
Eli Friedman60afcc22011-05-20 22:21:04 +00002908 if (Flags.isByVal()) {
2909 X86AddressMode SrcAM;
2910 SrcAM.Base.Reg = Arg;
2911 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2912 assert(Res && "memcpy length already checked!"); (void)Res;
2913 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2914 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002915 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002916 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002917 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2918 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002919 } else {
Juergen Ributzka349777d2014-06-12 23:27:57 +00002920 if (!X86FastEmitStore(ArgVT, Arg, /*ValIsKill=*/false, AM))
Lang Hames7d2f7b52011-10-18 22:11:33 +00002921 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002922 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002923 }
2924 }
2925
Dan Gohman3691d502008-09-25 15:24:26 +00002926 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002927 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002928 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002929 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002930 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2931 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002932 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002933
Charles Davise8f297c2013-07-12 06:02:35 +00002934 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002935 // Count the number of XMM registers allocated.
Craig Topper840beec2014-04-04 05:16:06 +00002936 static const MCPhysReg XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002937 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2938 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2939 };
2940 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002941 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002942 X86::AL).addImm(NumXMMRegs);
2943 }
2944
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002945 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002946 MachineInstrBuilder MIB;
2947 if (CalleeOp) {
2948 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002949 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002950 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002951 CallOpc = X86::CALL64r;
2952 else
2953 CallOpc = X86::CALL32r;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002954 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002955 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002956
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002957 } else {
2958 // Direct call.
2959 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002960 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002961 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002962 CallOpc = X86::CALL64pcrel32;
2963 else
2964 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002965
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002966 // See if we need any target-specific flags on the GV operand.
2967 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002968
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002969 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2970 // external symbols most go through the PLT in PIC mode. If the symbol
2971 // has hidden or protected visibility, or if it is static or local, then
2972 // we don't need to use the PLT - we can directly call it.
2973 if (Subtarget->isTargetELF() &&
2974 TM.getRelocationModel() == Reloc::PIC_ &&
2975 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2976 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002977 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002978 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002979 (!Subtarget->getTargetTriple().isMacOSX() ||
2980 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002981 // PC-relative references to external symbols should go through $stub,
2982 // unless we're building with the leopard linker or later, which
2983 // automatically synthesizes these stubs.
2984 OpFlags = X86II::MO_DARWIN_STUB;
2985 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002986
2987
Rafael Espindolaea09c592014-02-18 22:05:46 +00002988 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Eli Friedmancd2124a2011-06-10 23:39:36 +00002989 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002990 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002991 else
2992 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002993 }
Dan Gohman3691d502008-09-25 15:24:26 +00002994
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002995 // Add a register mask with the call-preserved registers.
2996 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2997 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2998
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002999 // Add an implicit use GOT pointer in EBX.
3000 if (Subtarget->isPICStyleGOT())
3001 MIB.addReg(X86::EBX, RegState::Implicit);
3002
Charles Davise8f297c2013-07-12 06:02:35 +00003003 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00003004 MIB.addReg(X86::AL, RegState::Implicit);
3005
3006 // Add implicit physical register uses to the call.
3007 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
3008 MIB.addReg(RegArgs[i], RegState::Implicit);
3009
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003010 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00003011 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00003012 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003013 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00003014 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003015
Eli Friedman7b279422011-05-17 18:29:03 +00003016 // Build info for return calling conv lowering code.
3017 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
3018 SmallVector<ISD::InputArg, 32> Ins;
3019 SmallVector<EVT, 4> RetTys;
3020 ComputeValueVTs(TLI, I->getType(), RetTys);
3021 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
3022 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00003023 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00003024 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
3025 for (unsigned j = 0; j != NumRegs; ++j) {
3026 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00003027 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00003028 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003029 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00003030 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003031 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00003032 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003033 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00003034 MyFlags.Flags.setInReg();
3035 Ins.push_back(MyFlags);
3036 }
3037 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00003038
Eli Friedman7b279422011-05-17 18:29:03 +00003039 // Now handle call return values.
3040 SmallVector<unsigned, 4> UsedRegs;
3041 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003042 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00003043 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00003044 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
3045 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
3046 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3047 EVT CopyVT = RVLocs[i].getValVT();
3048 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00003049
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003050 // If this is a call to a function that returns an fp value on the x87 fp
3051 // stack, but where we prefer to use the value in xmm registers, copy it
3052 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00003053 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00003054 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00003055 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00003056 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00003057 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00003058 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00003059 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3060 TII.get(X86::FpPOP_RETVAL), CopyReg);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00003061 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00003062 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3063 TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00003064 CopyReg).addReg(RVLocs[i].getLocReg());
3065 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003066 }
3067
Eli Friedman7b279422011-05-17 18:29:03 +00003068 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003069 // Round the F80 the right size, which also moves to the appropriate xmm
3070 // register. This is accomplished by storing the F80 value in memory and
3071 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00003072 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00003073 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003074 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00003075 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003076 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003077 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00003078 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00003079 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Rafael Espindolaea09c592014-02-18 22:05:46 +00003080 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Friedman7b279422011-05-17 18:29:03 +00003081 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003082 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00003083 }
Eli Friedman83ba1502011-05-17 00:13:47 +00003084
Eli Friedman7b279422011-05-17 18:29:03 +00003085 if (RVLocs.size())
3086 UpdateValueMap(I, ResultReg, RVLocs.size());
3087
Dan Gohman86936502010-06-18 23:28:01 +00003088 // Set all unused physreg defs as dead.
3089 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
3090
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003091 return true;
3092}
3093
3094
Dan Gohmand58f3e32008-08-28 23:21:34 +00003095bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003096X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00003097 switch (I->getOpcode()) {
3098 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00003099 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00003100 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00003101 case Instruction::Store:
3102 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003103 case Instruction::Ret:
3104 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00003105 case Instruction::ICmp:
3106 case Instruction::FCmp:
3107 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00003108 case Instruction::ZExt:
3109 return X86SelectZExt(I);
3110 case Instruction::Br:
3111 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003112 case Instruction::Call:
3113 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00003114 case Instruction::LShr:
3115 case Instruction::AShr:
3116 case Instruction::Shl:
3117 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00003118 case Instruction::SDiv:
3119 case Instruction::UDiv:
3120 case Instruction::SRem:
3121 case Instruction::URem:
3122 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00003123 case Instruction::Select:
3124 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00003125 case Instruction::Trunc:
3126 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00003127 case Instruction::FPExt:
3128 return X86SelectFPExt(I);
3129 case Instruction::FPTrunc:
3130 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00003131 case Instruction::IntToPtr: // Deliberate fall-through.
3132 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003133 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
3134 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00003135 if (DstVT.bitsGT(SrcVT))
3136 return X86SelectZExt(I);
3137 if (DstVT.bitsLT(SrcVT))
3138 return X86SelectTrunc(I);
3139 unsigned Reg = getRegForValue(I->getOperand(0));
3140 if (Reg == 0) return false;
3141 UpdateValueMap(I, Reg);
3142 return true;
3143 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00003144 }
3145
3146 return false;
3147}
3148
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003149unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003150 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00003151 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00003152 return 0;
3153
3154 // Can't handle alternate code models yet.
3155 if (TM.getCodeModel() != CodeModel::Small)
3156 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003157
Owen Anderson50288e32008-09-05 00:06:23 +00003158 // Get opcode and regclass of the output for the given load instruction.
3159 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003160 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00003161 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00003162 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00003163 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00003164 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00003165 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003166 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003167 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00003168 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00003169 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003170 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003171 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00003172 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00003173 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003174 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003175 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00003176 // Must be in x86-64 mode.
3177 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00003178 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003179 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003180 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00003181 if (X86ScalarSSEf32) {
3182 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00003183 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003184 } else {
3185 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00003186 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003187 }
3188 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003189 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00003190 if (X86ScalarSSEf64) {
3191 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00003192 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003193 } else {
3194 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00003195 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003196 }
3197 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003198 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00003199 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00003200 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00003201 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003202
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003203 // Materialize addresses with LEA/MOV instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00003204 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00003205 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00003206 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00003207 // If the expression is just a basereg, then we're done, otherwise we need
3208 // to emit an LEA.
3209 if (AM.BaseType == X86AddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00003210 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
Chris Lattner48326602011-04-17 17:12:08 +00003211 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003212
Dan Gohman9801ba42008-09-19 22:16:54 +00003213 unsigned ResultReg = createResultReg(RC);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003214 if (TM.getRelocationModel() == Reloc::Static &&
3215 TLI.getPointerTy() == MVT::i64) {
3216 // The displacement code be more than 32 bits away so we need to use
3217 // an instruction with a 64 bit immediate
3218 Opc = X86::MOV64ri;
3219 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3220 TII.get(Opc), ResultReg).addGlobalAddress(cast<GlobalValue>(C));
3221 } else {
3222 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
3223 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003224 TII.get(Opc), ResultReg), AM);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003225 }
Owen Anderson50288e32008-09-05 00:06:23 +00003226 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00003227 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00003228 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00003229 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003230
Owen Andersond41c7162008-09-06 01:11:01 +00003231 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00003232 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00003233 if (Align == 0) {
3234 // Alignment of vector types. FIXME!
Rafael Espindolaea09c592014-02-18 22:05:46 +00003235 Align = DL.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00003236 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003237
Dan Gohman8392f0c2008-09-30 01:21:32 +00003238 // x86-32 PIC requires a PIC base register for constant pools.
3239 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00003240 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00003241 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00003242 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00003243 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00003244 } else if (Subtarget->isPICStyleGOT()) {
3245 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00003246 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00003247 } else if (Subtarget->isPICStyleRIPRel() &&
3248 TM.getCodeModel() == CodeModel::Small) {
3249 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00003250 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00003251
3252 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00003253 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00003254 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003255 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003256 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00003257 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00003258
Owen Anderson50288e32008-09-05 00:06:23 +00003259 return ResultReg;
3260}
3261
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003262unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003263 // Fail on dynamic allocas. At this point, getRegForValue has already
3264 // checked its CSE maps, so if we're here trying to handle a dynamic
3265 // alloca, we're not going to succeed. X86SelectAddress has a
3266 // check for dynamic allocas, because it's called directly from
3267 // various places, but TargetMaterializeAlloca also needs a check
3268 // in order to avoid recursion between getRegForValue,
3269 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00003270 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003271 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00003272 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003273
Dan Gohman39d82f92008-09-10 20:11:02 +00003274 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00003275 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00003276 return 0;
3277 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00003278 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00003279 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003280 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003281 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00003282 return ResultReg;
3283}
3284
Eli Friedman406c4712011-04-27 22:41:55 +00003285unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
3286 MVT VT;
3287 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003288 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003289
3290 // Get opcode and regclass for the given zero.
3291 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003292 const TargetRegisterClass *RC = nullptr;
Eli Friedman406c4712011-04-27 22:41:55 +00003293 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003294 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00003295 case MVT::f32:
3296 if (X86ScalarSSEf32) {
3297 Opc = X86::FsFLD0SS;
3298 RC = &X86::FR32RegClass;
3299 } else {
3300 Opc = X86::LD_Fp032;
3301 RC = &X86::RFP32RegClass;
3302 }
3303 break;
3304 case MVT::f64:
3305 if (X86ScalarSSEf64) {
3306 Opc = X86::FsFLD0SD;
3307 RC = &X86::FR64RegClass;
3308 } else {
3309 Opc = X86::LD_Fp064;
3310 RC = &X86::RFP64RegClass;
3311 }
3312 break;
3313 case MVT::f80:
3314 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003315 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003316 }
3317
3318 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003319 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Eli Friedman406c4712011-04-27 22:41:55 +00003320 return ResultReg;
3321}
3322
3323
Eli Bendersky90dd3e72013-04-19 22:29:18 +00003324bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3325 const LoadInst *LI) {
Juergen Ributzka349777d2014-06-12 23:27:57 +00003326 const Value *Ptr = LI->getPointerOperand();
Chris Lattnereeba0c72010-09-05 02:18:34 +00003327 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00003328 if (!X86SelectAddress(Ptr, AM))
Chris Lattnereeba0c72010-09-05 02:18:34 +00003329 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003330
Craig Topper55406d92012-08-11 17:46:16 +00003331 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00003332
Rafael Espindolaea09c592014-02-18 22:05:46 +00003333 unsigned Size = DL.getTypeAllocSize(LI->getType());
Chris Lattnereeba0c72010-09-05 02:18:34 +00003334 unsigned Alignment = LI->getAlignment();
3335
Juergen Ributzka349777d2014-06-12 23:27:57 +00003336 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3337 Alignment = DL.getABITypeAlignment(LI->getType());
3338
Chris Lattnereeba0c72010-09-05 02:18:34 +00003339 SmallVector<MachineOperand, 8> AddrOps;
3340 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00003341
Chris Lattnereeba0c72010-09-05 02:18:34 +00003342 MachineInstr *Result =
3343 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
Juergen Ributzka349777d2014-06-12 23:27:57 +00003344 if (!Result)
3345 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003346
Juergen Ributzka349777d2014-06-12 23:27:57 +00003347 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Chris Lattner2d186572011-01-16 02:27:38 +00003348 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00003349 MI->eraseFromParent();
3350 return true;
3351}
3352
3353
Evan Cheng24422d42008-09-03 00:03:49 +00003354namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003355 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
3356 const TargetLibraryInfo *libInfo) {
3357 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00003358 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00003359}