blob: 92b3d62f0cc5a9a03ec6ad88fdca44c9f61d0211 [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 Ributzka6ef06f92014-06-23 21:55:36 +0000114 bool X86FastEmitCMoveSelect(const Instruction *I);
115
Juergen Ributzka21d56082014-06-23 21:55:40 +0000116 bool X86FastEmitSSESelect(const Instruction *I);
117
Juergen Ributzkaaed5c962014-06-23 21:55:44 +0000118 bool X86FastEmitPseudoSelect(const Instruction *I);
119
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);
Evan Cheng24422d42008-09-03 00:03:49 +0000160};
Wesley Peck527da1b2010-11-23 03:31:01 +0000161
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000162} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000163
Juergen Ributzkaaa602092014-06-17 21:55:43 +0000164static CmpInst::Predicate optimizeCmpPredicate(const CmpInst *CI) {
165 // If both operands are the same, then try to optimize or fold the cmp.
166 CmpInst::Predicate Predicate = CI->getPredicate();
167 if (CI->getOperand(0) != CI->getOperand(1))
168 return Predicate;
169
170 switch (Predicate) {
171 default: llvm_unreachable("Invalid predicate!");
172 case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break;
173 case CmpInst::FCMP_OEQ: Predicate = CmpInst::FCMP_ORD; break;
174 case CmpInst::FCMP_OGT: Predicate = CmpInst::FCMP_FALSE; break;
175 case CmpInst::FCMP_OGE: Predicate = CmpInst::FCMP_ORD; break;
176 case CmpInst::FCMP_OLT: Predicate = CmpInst::FCMP_FALSE; break;
177 case CmpInst::FCMP_OLE: Predicate = CmpInst::FCMP_ORD; break;
178 case CmpInst::FCMP_ONE: Predicate = CmpInst::FCMP_FALSE; break;
179 case CmpInst::FCMP_ORD: Predicate = CmpInst::FCMP_ORD; break;
180 case CmpInst::FCMP_UNO: Predicate = CmpInst::FCMP_UNO; break;
181 case CmpInst::FCMP_UEQ: Predicate = CmpInst::FCMP_TRUE; break;
182 case CmpInst::FCMP_UGT: Predicate = CmpInst::FCMP_UNO; break;
183 case CmpInst::FCMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
184 case CmpInst::FCMP_ULT: Predicate = CmpInst::FCMP_UNO; break;
185 case CmpInst::FCMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
186 case CmpInst::FCMP_UNE: Predicate = CmpInst::FCMP_UNO; break;
187 case CmpInst::FCMP_TRUE: Predicate = CmpInst::FCMP_TRUE; break;
188
189 case CmpInst::ICMP_EQ: Predicate = CmpInst::FCMP_TRUE; break;
190 case CmpInst::ICMP_NE: Predicate = CmpInst::FCMP_FALSE; break;
191 case CmpInst::ICMP_UGT: Predicate = CmpInst::FCMP_FALSE; break;
192 case CmpInst::ICMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
193 case CmpInst::ICMP_ULT: Predicate = CmpInst::FCMP_FALSE; break;
194 case CmpInst::ICMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
195 case CmpInst::ICMP_SGT: Predicate = CmpInst::FCMP_FALSE; break;
196 case CmpInst::ICMP_SGE: Predicate = CmpInst::FCMP_TRUE; break;
197 case CmpInst::ICMP_SLT: Predicate = CmpInst::FCMP_FALSE; break;
198 case CmpInst::ICMP_SLE: Predicate = CmpInst::FCMP_TRUE; break;
199 }
200
201 return Predicate;
202}
203
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +0000204static std::pair<X86::CondCode, bool>
205getX86ConditonCode(CmpInst::Predicate Predicate) {
206 X86::CondCode CC = X86::COND_INVALID;
207 bool NeedSwap = false;
208 switch (Predicate) {
209 default: break;
210 // Floating-point Predicates
211 case CmpInst::FCMP_UEQ: CC = X86::COND_E; break;
212 case CmpInst::FCMP_OLT: NeedSwap = true; // fall-through
213 case CmpInst::FCMP_OGT: CC = X86::COND_A; break;
214 case CmpInst::FCMP_OLE: NeedSwap = true; // fall-through
215 case CmpInst::FCMP_OGE: CC = X86::COND_AE; break;
216 case CmpInst::FCMP_UGT: NeedSwap = true; // fall-through
217 case CmpInst::FCMP_ULT: CC = X86::COND_B; break;
218 case CmpInst::FCMP_UGE: NeedSwap = true; // fall-through
219 case CmpInst::FCMP_ULE: CC = X86::COND_BE; break;
220 case CmpInst::FCMP_ONE: CC = X86::COND_NE; break;
221 case CmpInst::FCMP_UNO: CC = X86::COND_P; break;
222 case CmpInst::FCMP_ORD: CC = X86::COND_NP; break;
223 case CmpInst::FCMP_OEQ: // fall-through
224 case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break;
225
226 // Integer Predicates
227 case CmpInst::ICMP_EQ: CC = X86::COND_E; break;
228 case CmpInst::ICMP_NE: CC = X86::COND_NE; break;
229 case CmpInst::ICMP_UGT: CC = X86::COND_A; break;
230 case CmpInst::ICMP_UGE: CC = X86::COND_AE; break;
231 case CmpInst::ICMP_ULT: CC = X86::COND_B; break;
232 case CmpInst::ICMP_ULE: CC = X86::COND_BE; break;
233 case CmpInst::ICMP_SGT: CC = X86::COND_G; break;
234 case CmpInst::ICMP_SGE: CC = X86::COND_GE; break;
235 case CmpInst::ICMP_SLT: CC = X86::COND_L; break;
236 case CmpInst::ICMP_SLE: CC = X86::COND_LE; break;
237 }
238
239 return std::make_pair(CC, NeedSwap);
240}
241
Juergen Ributzka21d56082014-06-23 21:55:40 +0000242static std::pair<unsigned, bool>
243getX86SSECondtionCode(CmpInst::Predicate Predicate) {
244 unsigned CC;
245 bool NeedSwap = false;
246
247 // SSE Condition code mapping:
248 // 0 - EQ
249 // 1 - LT
250 // 2 - LE
251 // 3 - UNORD
252 // 4 - NEQ
253 // 5 - NLT
254 // 6 - NLE
255 // 7 - ORD
256 switch (Predicate) {
257 default: llvm_unreachable("Unexpected predicate");
258 case CmpInst::FCMP_OEQ: CC = 0; break;
259 case CmpInst::FCMP_OGT: NeedSwap = true; // fall-through
260 case CmpInst::FCMP_OLT: CC = 1; break;
261 case CmpInst::FCMP_OGE: NeedSwap = true; // fall-through
262 case CmpInst::FCMP_OLE: CC = 2; break;
263 case CmpInst::FCMP_UNO: CC = 3; break;
264 case CmpInst::FCMP_UNE: CC = 4; break;
265 case CmpInst::FCMP_ULE: NeedSwap = true; // fall-through
266 case CmpInst::FCMP_UGE: CC = 5; break;
267 case CmpInst::FCMP_ULT: NeedSwap = true; // fall-through
268 case CmpInst::FCMP_UGT: CC = 6; break;
269 case CmpInst::FCMP_ORD: CC = 7; break;
270 case CmpInst::FCMP_UEQ:
271 case CmpInst::FCMP_ONE: CC = 8; break;
272 }
273
274 return std::make_pair(CC, NeedSwap);
275}
276
Chris Lattner229907c2011-07-18 04:54:35 +0000277bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000278 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
279 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000280 // Unhandled type. Halt "fast" selection and bail.
281 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000282
283 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000284 // For now, require SSE/SSE2 for performing floating-point operations,
285 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000286 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000287 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000288 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000289 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000290 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000291 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000292 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000293 // We only handle legal types. For example, on x86-32 the instruction
294 // selector contains all of the 64-bit instructions from x86-64,
295 // under the assumption that i64 won't be used if the target doesn't
296 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000297 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000298}
299
300#include "X86GenCallingConv.inc"
301
Evan Chengf5bc7e52008-09-05 21:00:03 +0000302/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000303/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000304/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000305bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000306 MachineMemOperand *MMO, unsigned &ResultReg) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000307 // Get opcode and regclass of the output for the given load instruction.
308 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000309 const TargetRegisterClass *RC = nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000310 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000311 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000312 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000313 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000314 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000315 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000316 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000317 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000318 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000319 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000320 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000321 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000322 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000323 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000324 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000325 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000326 // Must be in x86-64 mode.
327 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000328 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000329 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000330 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000331 if (X86ScalarSSEf32) {
332 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000333 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000334 } else {
335 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000336 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000337 }
338 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000339 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000340 if (X86ScalarSSEf64) {
341 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000342 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000343 } else {
344 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000345 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000346 }
347 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000348 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000349 // No f80 support yet.
350 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000351 }
352
353 ResultReg = createResultReg(RC);
Juergen Ributzka349777d2014-06-12 23:27:57 +0000354 MachineInstrBuilder MIB =
355 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
356 addFullAddress(MIB, AM);
357 if (MMO)
358 MIB->addMemOperand(*FuncInfo.MF, MMO);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000359 return true;
360}
361
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000362/// X86FastEmitStore - Emit a machine instruction to store a value Val of
363/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
364/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000365/// i.e. V. Return true if it is possible.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000366bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
367 const X86AddressMode &AM,
368 MachineMemOperand *MMO, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000369 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000370 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000371 switch (VT.getSimpleVT().SimpleTy) {
372 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000373 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000374 case MVT::i1: {
375 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000376 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000377 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000378 TII.get(X86::AND8ri), AndResult)
379 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
Craig Topper4f55b0e2013-07-17 05:57:45 +0000380 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000381 }
382 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000383 case MVT::i8: Opc = X86::MOV8mr; break;
384 case MVT::i16: Opc = X86::MOV16mr; break;
385 case MVT::i32: Opc = X86::MOV32mr; break;
386 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
387 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000388 Opc = X86ScalarSSEf32 ?
389 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000390 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000391 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000392 Opc = X86ScalarSSEf64 ?
393 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000394 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000395 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000396 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000397 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000398 else
Craig Topper55475d42013-07-17 06:58:23 +0000399 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000400 break;
401 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000402 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000403 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000404 else
Craig Topperad1fff92013-07-18 07:16:44 +0000405 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000406 break;
407 case MVT::v4i32:
408 case MVT::v2i64:
409 case MVT::v8i16:
410 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000411 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000412 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000413 else
Craig Topper55475d42013-07-17 06:58:23 +0000414 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000415 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000416 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000417
Juergen Ributzka349777d2014-06-12 23:27:57 +0000418 MachineInstrBuilder MIB =
419 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
420 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
421 if (MMO)
422 MIB->addMemOperand(*FuncInfo.MF, MMO);
423
Evan Chengf5bc7e52008-09-05 21:00:03 +0000424 return true;
425}
426
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000427bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000428 const X86AddressMode &AM,
429 MachineMemOperand *MMO, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000430 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000431 if (isa<ConstantPointerNull>(Val))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000432 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000433
Chris Lattner3ba29352008-10-15 05:30:52 +0000434 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000435 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000436 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000437 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000438 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000439 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000440 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000441 case MVT::i8: Opc = X86::MOV8mi; break;
442 case MVT::i16: Opc = X86::MOV16mi; break;
443 case MVT::i32: Opc = X86::MOV32mi; break;
444 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000445 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000446 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000447 Opc = X86::MOV64mi32;
448 break;
449 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000450
Chris Lattner3ba29352008-10-15 05:30:52 +0000451 if (Opc) {
Juergen Ributzka349777d2014-06-12 23:27:57 +0000452 MachineInstrBuilder MIB =
453 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
454 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
455 : CI->getZExtValue());
456 if (MMO)
457 MIB->addMemOperand(*FuncInfo.MF, MMO);
Chris Lattner3ba29352008-10-15 05:30:52 +0000458 return true;
459 }
460 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000461
Chris Lattner3ba29352008-10-15 05:30:52 +0000462 unsigned ValReg = getRegForValue(Val);
463 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000464 return false;
465
Juergen Ributzka349777d2014-06-12 23:27:57 +0000466 bool ValKill = hasTrivialKill(Val);
467 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000468}
469
Evan Cheng6500d172008-09-08 06:35:17 +0000470/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
471/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
472/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000473bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
474 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000475 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000476 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
477 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000478 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000479 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000480
481 ResultReg = RR;
482 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000483}
484
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000485bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
486 // Handle constant address.
487 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
488 // Can't handle alternate code models yet.
489 if (TM.getCodeModel() != CodeModel::Small)
490 return false;
491
492 // Can't handle TLS yet.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000493 if (GV->isThreadLocal())
494 return false;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000495
496 // RIP-relative addresses can't have additional register operands, so if
497 // we've already folded stuff into the addressing mode, just force the
498 // global value into its own register, which we can use as the basereg.
499 if (!Subtarget->isPICStyleRIPRel() ||
500 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
501 // Okay, we've committed to selecting this global. Set up the address.
502 AM.GV = GV;
503
504 // Allow the subtarget to classify the global.
505 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
506
507 // If this reference is relative to the pic base, set it now.
508 if (isGlobalRelativeToPICBase(GVFlags)) {
509 // FIXME: How do we know Base.Reg is free??
510 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
511 }
512
513 // Unless the ABI requires an extra load, return a direct reference to
514 // the global.
515 if (!isGlobalStubReference(GVFlags)) {
516 if (Subtarget->isPICStyleRIPRel()) {
517 // Use rip-relative addressing if we can. Above we verified that the
518 // base and index registers are unused.
519 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
520 AM.Base.Reg = X86::RIP;
521 }
522 AM.GVOpFlags = GVFlags;
523 return true;
524 }
525
526 // Ok, we need to do a load from a stub. If we've already loaded from
527 // this stub, reuse the loaded pointer, otherwise emit the load now.
528 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
529 unsigned LoadReg;
530 if (I != LocalValueMap.end() && I->second != 0) {
531 LoadReg = I->second;
532 } else {
533 // Issue load from stub.
534 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000535 const TargetRegisterClass *RC = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000536 X86AddressMode StubAM;
537 StubAM.Base.Reg = AM.Base.Reg;
538 StubAM.GV = GV;
539 StubAM.GVOpFlags = GVFlags;
540
541 // Prepare for inserting code in the local-value area.
542 SavePoint SaveInsertPt = enterLocalValueArea();
543
544 if (TLI.getPointerTy() == MVT::i64) {
545 Opc = X86::MOV64rm;
546 RC = &X86::GR64RegClass;
547
548 if (Subtarget->isPICStyleRIPRel())
549 StubAM.Base.Reg = X86::RIP;
550 } else {
551 Opc = X86::MOV32rm;
552 RC = &X86::GR32RegClass;
553 }
554
555 LoadReg = createResultReg(RC);
556 MachineInstrBuilder LoadMI =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000557 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000558 addFullAddress(LoadMI, StubAM);
559
560 // Ok, back to normal mode.
561 leaveLocalValueArea(SaveInsertPt);
562
563 // Prevent loading GV stub multiple times in same MBB.
564 LocalValueMap[V] = LoadReg;
565 }
566
567 // Now construct the final address. Note that the Disp, Scale,
568 // and Index values may already be set here.
569 AM.Base.Reg = LoadReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000570 AM.GV = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000571 return true;
572 }
573 }
574
575 // If all else fails, try to materialize the value in a register.
576 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
577 if (AM.Base.Reg == 0) {
578 AM.Base.Reg = getRegForValue(V);
579 return AM.Base.Reg != 0;
580 }
581 if (AM.IndexReg == 0) {
582 assert(AM.Scale == 1 && "Scale with no index!");
583 AM.IndexReg = getRegForValue(V);
584 return AM.IndexReg != 0;
585 }
586 }
587
588 return false;
589}
590
Dan Gohman39d82f92008-09-10 20:11:02 +0000591/// X86SelectAddress - Attempt to fill in an address from the given value.
592///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000593bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000594 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000595redo_gep:
Craig Topper062a2ba2014-04-25 05:30:21 +0000596 const User *U = nullptr;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000597 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000598 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000599 // Don't walk into other basic blocks; it's possible we haven't
600 // visited them yet, so the instructions may not yet be assigned
601 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000602 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
603 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
604 Opcode = I->getOpcode();
605 U = I;
606 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000607 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000608 Opcode = C->getOpcode();
609 U = C;
610 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000611
Chris Lattner229907c2011-07-18 04:54:35 +0000612 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000613 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000614 // Fast instruction selection doesn't support the special
615 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000616 return false;
617
Dan Gohman6e005fd2008-09-18 23:23:44 +0000618 switch (Opcode) {
619 default: break;
620 case Instruction::BitCast:
621 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000622 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000623
624 case Instruction::IntToPtr:
625 // Look past no-op inttoptrs.
626 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000627 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000628 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000629
630 case Instruction::PtrToInt:
631 // Look past no-op ptrtoints.
632 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000633 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000634 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000635
636 case Instruction::Alloca: {
637 // Do static allocas.
638 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000639 DenseMap<const AllocaInst*, int>::iterator SI =
640 FuncInfo.StaticAllocaMap.find(A);
641 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000642 AM.BaseType = X86AddressMode::FrameIndexBase;
643 AM.Base.FrameIndex = SI->second;
644 return true;
645 }
646 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000647 }
648
649 case Instruction::Add: {
650 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000651 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000652 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
653 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000654 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000655 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000656 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000657 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000658 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000659 break;
660 }
661
662 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000663 X86AddressMode SavedAM = AM;
664
Dan Gohman6e005fd2008-09-18 23:23:44 +0000665 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000666 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000667 unsigned IndexReg = AM.IndexReg;
668 unsigned Scale = AM.Scale;
669 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000670 // Iterate through the indices, folding what we can. Constants can be
671 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000672 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000673 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000674 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000675 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000676 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000677 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
678 continue;
679 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000680
Chris Lattner4b026b92011-04-17 17:05:12 +0000681 // A array/variable index is always of the form i*S where S is the
682 // constant scale size. See if we can push the scale into immediates.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000683 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner4b026b92011-04-17 17:05:12 +0000684 for (;;) {
685 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
686 // Constant-offset addressing.
687 Disp += CI->getSExtValue() * S;
688 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000689 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000690 if (canFoldAddIntoGEP(U, Op)) {
691 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000692 ConstantInt *CI =
693 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
694 Disp += CI->getSExtValue() * S;
695 // Iterate on the other operand.
696 Op = cast<AddOperator>(Op)->getOperand(0);
697 continue;
698 }
699 if (IndexReg == 0 &&
700 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
701 (S == 1 || S == 2 || S == 4 || S == 8)) {
702 // Scaled-index addressing.
703 Scale = S;
704 IndexReg = getRegForGEPIndex(Op).first;
705 if (IndexReg == 0)
706 return false;
707 break;
708 }
709 // Unsupported.
710 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000711 }
712 }
Bill Wendling585a9012013-09-24 00:13:08 +0000713
Dan Gohman2564b902008-09-26 20:04:15 +0000714 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000715 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000716 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000717
Dan Gohman6e005fd2008-09-18 23:23:44 +0000718 AM.IndexReg = IndexReg;
719 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000720 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000721 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000722
723 if (const GetElementPtrInst *GEP =
724 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
725 // Ok, the GEP indices were covered by constant-offset and scaled-index
726 // addressing. Update the address state and move on to examining the base.
727 V = GEP;
728 goto redo_gep;
729 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000730 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000731 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000732
Chris Lattner4b026b92011-04-17 17:05:12 +0000733 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000734 // our address and just match the value instead of completely failing.
735 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000736
737 for (SmallVectorImpl<const Value *>::reverse_iterator
738 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
739 if (handleConstantAddresses(*I, AM))
740 return true;
741
742 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000743 unsupported_gep:
744 // Ok, the GEP indices weren't all covered.
745 break;
746 }
747 }
748
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000749 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000750}
751
Chris Lattner8212d372009-07-10 05:33:42 +0000752/// X86SelectCallAddress - Attempt to fill in an address from the given value.
753///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000754bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000755 const User *U = nullptr;
Chris Lattner8212d372009-07-10 05:33:42 +0000756 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000757 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000758 // Record if the value is defined in the same basic block.
759 //
760 // This information is crucial to know whether or not folding an
761 // operand is valid.
762 // Indeed, FastISel generates or reuses a virtual register for all
763 // operands of all instructions it selects. Obviously, the definition and
764 // its uses must use the same virtual register otherwise the produced
765 // code is incorrect.
766 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
767 // registers for values that are alive across basic blocks. This ensures
768 // that the values are consistently set between across basic block, even
769 // if different instruction selection mechanisms are used (e.g., a mix of
770 // SDISel and FastISel).
771 // For values local to a basic block, the instruction selection process
772 // generates these virtual registers with whatever method is appropriate
773 // for its needs. In particular, FastISel and SDISel do not share the way
774 // local virtual registers are set.
775 // Therefore, this is impossible (or at least unsafe) to share values
776 // between basic blocks unless they use the same instruction selection
777 // method, which is not guarantee for X86.
778 // Moreover, things like hasOneUse could not be used accurately, if we
779 // allow to reference values across basic blocks whereas they are not
780 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000781 bool InMBB = true;
782 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000783 Opcode = I->getOpcode();
784 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000785 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000786 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000787 Opcode = C->getOpcode();
788 U = C;
789 }
790
791 switch (Opcode) {
792 default: break;
793 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000794 // Look past bitcasts if its operand is in the same BB.
795 if (InMBB)
796 return X86SelectCallAddress(U->getOperand(0), AM);
797 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000798
799 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000800 // Look past no-op inttoptrs if its operand is in the same BB.
801 if (InMBB &&
802 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000803 return X86SelectCallAddress(U->getOperand(0), AM);
804 break;
805
806 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000807 // Look past no-op ptrtoints if its operand is in the same BB.
808 if (InMBB &&
809 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000810 return X86SelectCallAddress(U->getOperand(0), AM);
811 break;
812 }
813
814 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000815 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000816 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000817 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000818 return false;
819
820 // RIP-relative addresses can't have additional register operands.
821 if (Subtarget->isPICStyleRIPRel() &&
822 (AM.Base.Reg != 0 || AM.IndexReg != 0))
823 return false;
824
Rafael Espindolaea09c592014-02-18 22:05:46 +0000825 // Can't handle DbgLocLImport.
Nico Rieck7157bb72014-01-14 15:22:47 +0000826 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000827 return false;
828
829 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000830 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000831 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000832 return false;
833
834 // Okay, we've committed to selecting this global. Set up the basic address.
835 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000836
Chris Lattner7277a802009-07-10 05:45:15 +0000837 // No ABI requires an extra load for anything other than DLLImport, which
838 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000839 if (Subtarget->isPICStyleRIPRel()) {
840 // Use rip-relative addressing if we can. Above we verified that the
841 // base and index registers are unused.
842 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
843 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000844 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000845 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
846 } else if (Subtarget->isPICStyleGOT()) {
847 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000848 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000849
Chris Lattner8212d372009-07-10 05:33:42 +0000850 return true;
851 }
852
853 // If all else fails, try to materialize the value in a register.
854 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
855 if (AM.Base.Reg == 0) {
856 AM.Base.Reg = getRegForValue(V);
857 return AM.Base.Reg != 0;
858 }
859 if (AM.IndexReg == 0) {
860 assert(AM.Scale == 1 && "Scale with no index!");
861 AM.IndexReg = getRegForValue(V);
862 return AM.IndexReg != 0;
863 }
864 }
865
866 return false;
867}
868
869
Owen Anderson4f948bd2008-09-04 07:08:58 +0000870/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000871bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000872 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000873 const StoreInst *S = cast<StoreInst>(I);
874
875 if (S->isAtomic())
876 return false;
877
Juergen Ributzka349777d2014-06-12 23:27:57 +0000878 const Value *Val = S->getValueOperand();
879 const Value *Ptr = S->getPointerOperand();
Craig Topper4f55b0e2013-07-17 05:57:45 +0000880
Duncan Sandsf5dda012010-11-03 11:35:31 +0000881 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000882 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000883 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000884
Juergen Ributzka349777d2014-06-12 23:27:57 +0000885 unsigned Alignment = S->getAlignment();
886 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
887 if (Alignment == 0) // Ensure that codegen never sees alignment 0
888 Alignment = ABIAlignment;
889 bool Aligned = Alignment >= ABIAlignment;
890
Dan Gohman39d82f92008-09-10 20:11:02 +0000891 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000892 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000893 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000894
Juergen Ributzka349777d2014-06-12 23:27:57 +0000895 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000896}
897
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000898/// X86SelectRet - Select and emit code to implement ret instructions.
899bool X86FastISel::X86SelectRet(const Instruction *I) {
900 const ReturnInst *Ret = cast<ReturnInst>(I);
901 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000902 const X86MachineFunctionInfo *X86MFInfo =
903 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000904
905 if (!FuncInfo.CanLowerReturn)
906 return false;
907
908 CallingConv::ID CC = F.getCallingConv();
909 if (CC != CallingConv::C &&
910 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000911 CC != CallingConv::X86_FastCall &&
912 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000913 return false;
914
Charles Davise8f297c2013-07-12 06:02:35 +0000915 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000916 return false;
917
918 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000919 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000920 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000921
922 // fastcc with -tailcallopt is intended to provide a guaranteed
923 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000924 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000925 return false;
926
927 // Let SDISel handle vararg functions.
928 if (F.isVarArg())
929 return false;
930
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000931 // Build a list of return value registers.
932 SmallVector<unsigned, 4> RetRegs;
933
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000934 if (Ret->getNumOperands() > 0) {
935 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000936 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000937
938 // Analyze operands of the call, assigning locations to each operand.
939 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000940 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000941 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000942 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000943
944 const Value *RV = Ret->getOperand(0);
945 unsigned Reg = getRegForValue(RV);
946 if (Reg == 0)
947 return false;
948
949 // Only handle a single return value for now.
950 if (ValLocs.size() != 1)
951 return false;
952
953 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000954
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000955 // Don't bother handling odd stuff for now.
956 if (VA.getLocInfo() != CCValAssign::Full)
957 return false;
958 // Only handle register returns for now.
959 if (!VA.isRegLoc())
960 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000961
962 // The calling-convention tables for x87 returns don't tell
963 // the whole story.
964 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
965 return false;
966
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000967 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000968 EVT SrcVT = TLI.getValueType(RV->getType());
969 EVT DstVT = VA.getValVT();
970 // Special handling for extended integers.
971 if (SrcVT != DstVT) {
972 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
973 return false;
974
975 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
976 return false;
977
978 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
979
980 if (SrcVT == MVT::i1) {
981 if (Outs[0].Flags.isSExt())
982 return false;
983 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
984 SrcVT = MVT::i8;
985 }
986 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
987 ISD::SIGN_EXTEND;
988 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
989 SrcReg, /*TODO: Kill=*/false);
990 }
991
992 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000993 unsigned DstReg = VA.getLocReg();
994 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000995 // Avoid a cross-class copy. This is very unlikely.
996 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000997 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000999 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001000
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001001 // Add register to return instruction.
1002 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001003 }
1004
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001005 // The x86-64 ABI for returning structs by value requires that we copy
1006 // the sret argument into %rax for the return. We saved the argument into
1007 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001008 // and into %rax. We also do the same with %eax for Win32.
1009 if (F.hasStructRetAttr() &&
Yaron Keren136fe7d2014-04-01 18:15:34 +00001010 (Subtarget->is64Bit() || Subtarget->isTargetKnownWindowsMSVC())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001011 unsigned Reg = X86MFInfo->getSRetReturnReg();
1012 assert(Reg &&
1013 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001014 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001015 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001016 RetReg).addReg(Reg);
1017 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001018 }
1019
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001020 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001021 MachineInstrBuilder MIB =
Rafael Espindolaea09c592014-02-18 22:05:46 +00001022 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001023 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1024 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001025 return true;
1026}
1027
Evan Chenga41ee292008-09-03 06:44:39 +00001028/// X86SelectLoad - Select and emit code to implement load instructions.
1029///
Juergen Ributzka349777d2014-06-12 23:27:57 +00001030bool X86FastISel::X86SelectLoad(const Instruction *I) {
1031 const LoadInst *LI = cast<LoadInst>(I);
1032
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001033 // Atomic loads need special handling.
Juergen Ributzka349777d2014-06-12 23:27:57 +00001034 if (LI->isAtomic())
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001035 return false;
1036
Duncan Sandsf5dda012010-11-03 11:35:31 +00001037 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001038 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +00001039 return false;
1040
Juergen Ributzka349777d2014-06-12 23:27:57 +00001041 const Value *Ptr = LI->getPointerOperand();
1042
Dan Gohman39d82f92008-09-10 20:11:02 +00001043 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001044 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00001045 return false;
Evan Chenga41ee292008-09-03 06:44:39 +00001046
Evan Chengf5bc7e52008-09-05 21:00:03 +00001047 unsigned ResultReg = 0;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001048 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg))
1049 return false;
1050
1051 UpdateValueMap(I, ResultReg);
1052 return true;
Evan Chenga41ee292008-09-03 06:44:39 +00001053}
1054
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001055static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001056 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +00001057 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
1058 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001059
Owen Anderson9f944592009-08-11 20:47:22 +00001060 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +00001061 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001062 case MVT::i8: return X86::CMP8rr;
1063 case MVT::i16: return X86::CMP16rr;
1064 case MVT::i32: return X86::CMP32rr;
1065 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001066 case MVT::f32:
1067 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
1068 case MVT::f64:
1069 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001070 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001071}
1072
Chris Lattner88f47542008-10-15 04:13:29 +00001073/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
1074/// of the comparison, return an opcode that works for the compare (e.g.
1075/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001076static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +00001077 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +00001078 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +00001079 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001080 case MVT::i8: return X86::CMP8ri;
1081 case MVT::i16: return X86::CMP16ri;
1082 case MVT::i32: return X86::CMP32ri;
1083 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +00001084 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1085 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +00001086 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +00001087 return X86::CMP64ri32;
1088 return 0;
1089 }
Chris Lattner88f47542008-10-15 04:13:29 +00001090}
1091
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001092bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
1093 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +00001094 unsigned Op0Reg = getRegForValue(Op0);
1095 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001096
Chris Lattnere388725a2008-10-15 05:18:04 +00001097 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +00001098 if (isa<ConstantPointerNull>(Op1))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001099 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +00001100
Chris Lattnerd46b9512008-10-15 04:26:38 +00001101 // We have two options: compare with register or immediate. If the RHS of
1102 // the compare is an immediate that we can fold into this compare, use
1103 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001104 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +00001105 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001106 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareImmOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001107 .addReg(Op0Reg)
1108 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +00001109 return true;
1110 }
1111 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001112
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001113 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +00001114 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001115
Chris Lattnerd46b9512008-10-15 04:26:38 +00001116 unsigned Op1Reg = getRegForValue(Op1);
1117 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001118 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001119 .addReg(Op0Reg)
1120 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001121
Chris Lattnerd46b9512008-10-15 04:26:38 +00001122 return true;
1123}
1124
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001125bool X86FastISel::X86SelectCmp(const Instruction *I) {
1126 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001127
Duncan Sandsf5dda012010-11-03 11:35:31 +00001128 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001129 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +00001130 return false;
1131
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001132 // Try to optimize or fold the cmp.
1133 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1134 unsigned ResultReg = 0;
1135 switch (Predicate) {
1136 default: break;
1137 case CmpInst::FCMP_FALSE: {
1138 ResultReg = createResultReg(&X86::GR32RegClass);
1139 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0),
1140 ResultReg);
1141 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1142 X86::sub_8bit);
1143 if (!ResultReg)
1144 return false;
1145 break;
1146 }
1147 case CmpInst::FCMP_TRUE: {
1148 ResultReg = createResultReg(&X86::GR8RegClass);
1149 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
1150 ResultReg).addImm(1);
1151 break;
1152 }
1153 }
1154
1155 if (ResultReg) {
1156 UpdateValueMap(I, ResultReg);
1157 return true;
1158 }
1159
1160 const Value *LHS = CI->getOperand(0);
1161 const Value *RHS = CI->getOperand(1);
1162
1163 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1164 // We don't have to materialize a zero constant for this case and can just use
1165 // %x again on the RHS.
1166 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1167 const auto *RHSC = dyn_cast<ConstantFP>(RHS);
1168 if (RHSC && RHSC->isNullValue())
1169 RHS = LHS;
1170 }
1171
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001172 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Juergen Ributzkae3570562014-06-17 14:47:45 +00001173 static unsigned SETFOpcTable[2][3] = {
1174 { X86::SETEr, X86::SETNPr, X86::AND8rr },
1175 { X86::SETNEr, X86::SETPr, X86::OR8rr }
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001176 };
1177 unsigned *SETFOpc = nullptr;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001178 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001179 default: break;
1180 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1181 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1182 }
1183
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001184 ResultReg = createResultReg(&X86::GR8RegClass);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001185 if (SETFOpc) {
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001186 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001187 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001188
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001189 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1190 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1191 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1192 FlagReg1);
1193 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1194 FlagReg2);
Juergen Ributzkae3570562014-06-17 14:47:45 +00001195 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001196 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
Chris Lattnera3596db2008-10-15 03:47:17 +00001197 UpdateValueMap(I, ResultReg);
1198 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001199 }
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001200
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001201 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001202 bool SwapArgs;
1203 std::tie(CC, SwapArgs) = getX86ConditonCode(Predicate);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001204 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1205 unsigned Opc = X86::getSETFromCond(CC);
Wesley Peck527da1b2010-11-23 03:31:01 +00001206
Chris Lattnerf32ce222008-10-15 03:52:54 +00001207 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001208 std::swap(LHS, RHS);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001209
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001210 // Emit a compare of LHS/RHS.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001211 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001212 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001213
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001214 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001215 UpdateValueMap(I, ResultReg);
1216 return true;
1217}
Evan Chenga41ee292008-09-03 06:44:39 +00001218
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001219bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001220 EVT DstVT = TLI.getValueType(I->getType());
1221 if (!TLI.isTypeLegal(DstVT))
1222 return false;
1223
1224 unsigned ResultReg = getRegForValue(I->getOperand(0));
1225 if (ResultReg == 0)
1226 return false;
1227
Tim Northover04eb4232013-05-30 10:43:18 +00001228 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001229 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001230 if (SrcVT.SimpleTy == MVT::i1) {
1231 // Set the high bits to zero.
1232 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1233 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001234
Tim Northover04eb4232013-05-30 10:43:18 +00001235 if (ResultReg == 0)
1236 return false;
1237 }
1238
1239 if (DstVT == MVT::i64) {
1240 // Handle extension to 64-bits via sub-register shenanigans.
1241 unsigned MovInst;
1242
1243 switch (SrcVT.SimpleTy) {
1244 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1245 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1246 case MVT::i32: MovInst = X86::MOV32rr; break;
1247 default: llvm_unreachable("Unexpected zext to i64 source type");
1248 }
1249
1250 unsigned Result32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
Tim Northover04eb4232013-05-30 10:43:18 +00001252 .addReg(ResultReg);
1253
1254 ResultReg = createResultReg(&X86::GR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001255 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
Tim Northover04eb4232013-05-30 10:43:18 +00001256 ResultReg)
1257 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1258 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001259 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1260 ResultReg, /*Kill=*/true);
1261 if (ResultReg == 0)
1262 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001263 }
1264
Eli Friedmanc7035512011-05-25 23:49:02 +00001265 UpdateValueMap(I, ResultReg);
1266 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001267}
1268
Chris Lattnerd46b9512008-10-15 04:26:38 +00001269
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001270bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001271 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001272 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001273 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001274 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1275 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001276
Dan Gohman42ef6692010-08-21 02:32:36 +00001277 // Fold the common case of a conditional branch with a comparison
1278 // in the same block (values defined on other blocks may not have
1279 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001280 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001281 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001282 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001283
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001284 // Try to optimize or fold the cmp.
1285 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1286 switch (Predicate) {
1287 default: break;
1288 case CmpInst::FCMP_FALSE: FastEmitBranch(FalseMBB, DbgLoc); return true;
1289 case CmpInst::FCMP_TRUE: FastEmitBranch(TrueMBB, DbgLoc); return true;
1290 }
1291
1292 const Value *CmpLHS = CI->getOperand(0);
1293 const Value *CmpRHS = CI->getOperand(1);
1294
1295 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x,
1296 // 0.0.
1297 // We don't have to materialize a zero constant for this case and can just
1298 // use %x again on the RHS.
1299 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1300 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1301 if (CmpRHSC && CmpRHSC->isNullValue())
1302 CmpRHS = CmpLHS;
1303 }
1304
Dan Gohman1ab1d312008-10-02 22:15:21 +00001305 // Try to take advantage of fallthrough opportunities.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001306 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001307 std::swap(TrueMBB, FalseMBB);
1308 Predicate = CmpInst::getInversePredicate(Predicate);
1309 }
1310
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001311 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/conditon
1312 // code check. Instead two branch instructions are required to check all
1313 // the flags. First we change the predicate to a supported conditon code,
1314 // which will be the first branch. Later one we will emit the second
1315 // branch.
1316 bool NeedExtraBranch = false;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001317 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001318 default: break;
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001319 case CmpInst::FCMP_OEQ:
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001320 std::swap(TrueMBB, FalseMBB); // fall-through
1321 case CmpInst::FCMP_UNE:
1322 NeedExtraBranch = true;
1323 Predicate = CmpInst::FCMP_ONE;
1324 break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001325 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001326
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001327 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001328 bool SwapArgs;
1329 unsigned BranchOpc;
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001330 std::tie(CC, SwapArgs) = getX86ConditonCode(Predicate);
1331 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1332
1333 BranchOpc = X86::GetCondBranchFromCond(CC);
Chris Lattner47bef252008-10-15 04:02:26 +00001334 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001335 std::swap(CmpLHS, CmpRHS);
Chris Lattner47bef252008-10-15 04:02:26 +00001336
Chris Lattnerd46b9512008-10-15 04:26:38 +00001337 // Emit a compare of the LHS and RHS, setting the flags.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001338 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT))
Chris Lattnerd46b9512008-10-15 04:26:38 +00001339 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001340
Rafael Espindolaea09c592014-02-18 22:05:46 +00001341 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001342 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001343
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001344 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1345 // to UNE above).
1346 if (NeedExtraBranch) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001348 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001349 }
1350
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001351 // Obtain the branch weight and add the TrueBB to the successor list.
Juergen Ributzka454d3742014-06-13 00:45:11 +00001352 uint32_t BranchWeight = 0;
1353 if (FuncInfo.BPI)
1354 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1355 TrueMBB->getBasicBlock());
1356 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001357
1358 // Emits an unconditional branch to the FalseBB, obtains the branch
Alp Toker1d099d92014-06-19 19:41:26 +00001359 // weight, and adds it to the successor list.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001360 FastEmitBranch(FalseMBB, DbgLoc);
1361
Dan Gohman1ab1d312008-10-02 22:15:21 +00001362 return true;
1363 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001364 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1365 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1366 // typically happen for _Bool and C++ bools.
1367 MVT SourceVT;
1368 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1369 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1370 unsigned TestOpc = 0;
1371 switch (SourceVT.SimpleTy) {
1372 default: break;
1373 case MVT::i8: TestOpc = X86::TEST8ri; break;
1374 case MVT::i16: TestOpc = X86::TEST16ri; break;
1375 case MVT::i32: TestOpc = X86::TEST32ri; break;
1376 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1377 }
1378 if (TestOpc) {
1379 unsigned OpReg = getRegForValue(TI->getOperand(0));
1380 if (OpReg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001382 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001383
Chris Lattnerc59290a2011-04-19 04:26:32 +00001384 unsigned JmpOpc = X86::JNE_4;
1385 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1386 std::swap(TrueMBB, FalseMBB);
1387 JmpOpc = X86::JE_4;
1388 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001389
Rafael Espindolaea09c592014-02-18 22:05:46 +00001390 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001391 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001392 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001393 uint32_t BranchWeight = 0;
1394 if (FuncInfo.BPI)
1395 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1396 TrueMBB->getBasicBlock());
1397 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001398 return true;
1399 }
1400 }
Juergen Ributzka2bce27e2014-06-24 23:51:21 +00001401 } else if (auto *EV = dyn_cast<ExtractValueInst>(BI->getCondition())) {
1402 bool FoldIntrinsic = false;
1403 if (const auto *II = dyn_cast<IntrinsicInst>(EV->getAggregateOperand())) {
1404 switch (II->getIntrinsicID()) {
1405 default: break;
1406 case Intrinsic::sadd_with_overflow:
1407 case Intrinsic::uadd_with_overflow:
1408 case Intrinsic::ssub_with_overflow:
1409 case Intrinsic::usub_with_overflow:
1410 case Intrinsic::smul_with_overflow:
1411 case Intrinsic::umul_with_overflow: FoldIntrinsic = true; break;
1412 }
1413
1414 // Check if both instructions are in the same basic block.
1415 if (FoldIntrinsic && (II->getParent() != I->getParent()))
1416 FoldIntrinsic = false;
1417
1418 // Make sure nothing is in the way
1419 if (FoldIntrinsic) {
1420 BasicBlock::const_iterator Start = I;
1421 BasicBlock::const_iterator End = II;
1422 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
1423 // We only expect extractvalue instructions between the intrinsic and
1424 // the branch.
1425 if (!isa<ExtractValueInst>(Itr)) {
1426 FoldIntrinsic = false;
1427 break;
1428 }
1429
1430 // Check that the extractvalue operand comes from the intrinsic.
1431 const auto *EVI = cast<ExtractValueInst>(Itr);
1432 if (EVI->getAggregateOperand() != II) {
1433 FoldIntrinsic = false;
1434 break;
1435 }
1436 }
1437 }
1438 }
1439
1440 if (FoldIntrinsic) {
1441 MVT RetVT;
1442 const IntrinsicInst *II = cast<IntrinsicInst>(EV->getAggregateOperand());
1443 const Function *Callee = II->getCalledFunction();
1444 Type *RetTy =
1445 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
1446 if (!isTypeLegal(RetTy, RetVT))
1447 return false;
1448
1449 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1450 return false;
1451
1452 // Fake request the condition, otherwise the intrinsic might be completely
1453 // optimized away.
1454 unsigned TmpReg = getRegForValue(EV);
1455 if (TmpReg == 0)
1456 return false;
1457
1458 unsigned BranchOpc = 0;
1459 switch (II->getIntrinsicID()) {
1460 default: llvm_unreachable("Unexpected intrinsic instruction.");
1461 case Intrinsic::sadd_with_overflow:
1462 case Intrinsic::ssub_with_overflow:
1463 case Intrinsic::smul_with_overflow:
1464 case Intrinsic::umul_with_overflow: BranchOpc = X86::JO_4; break;
1465 case Intrinsic::uadd_with_overflow:
1466 case Intrinsic::usub_with_overflow: BranchOpc = X86::JB_4; break;
1467 }
1468
1469 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;
1478 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001479 }
1480
1481 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001482 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1483 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001484 unsigned OpReg = getRegForValue(BI->getCondition());
1485 if (OpReg == 0) return false;
1486
Rafael Espindolaea09c592014-02-18 22:05:46 +00001487 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Eli Friedman0eea0292011-04-27 01:34:27 +00001488 .addReg(OpReg).addImm(1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001489 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001490 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001491 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001492 uint32_t BranchWeight = 0;
1493 if (FuncInfo.BPI)
1494 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1495 TrueMBB->getBasicBlock());
1496 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Dan Gohmana5753b32008-09-05 01:06:14 +00001497 return true;
1498}
1499
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001500bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001501 unsigned CReg = 0, OpReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001502 const TargetRegisterClass *RC = nullptr;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001503 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001504 CReg = X86::CL;
1505 RC = &X86::GR8RegClass;
1506 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001507 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1508 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1509 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001510 default: return false;
1511 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001512 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001513 CReg = X86::CX;
1514 RC = &X86::GR16RegClass;
1515 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001516 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1517 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1518 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001519 default: return false;
1520 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001521 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001522 CReg = X86::ECX;
1523 RC = &X86::GR32RegClass;
1524 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001525 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1526 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1527 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001528 default: return false;
1529 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001530 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001531 CReg = X86::RCX;
1532 RC = &X86::GR64RegClass;
1533 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001534 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1535 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1536 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001537 default: return false;
1538 }
1539 } else {
1540 return false;
1541 }
1542
Duncan Sandsf5dda012010-11-03 11:35:31 +00001543 MVT VT;
1544 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001545 return false;
1546
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001547 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1548 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001549
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001550 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1551 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001552 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001553 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001554
1555 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001556 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001557 if (CReg != X86::CL)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001558 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001559 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001560 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001561
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001562 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001563 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001564 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001565 UpdateValueMap(I, ResultReg);
1566 return true;
1567}
1568
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001569bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1570 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1571 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1572 const static bool S = true; // IsSigned
1573 const static bool U = false; // !IsSigned
1574 const static unsigned Copy = TargetOpcode::COPY;
1575 // For the X86 DIV/IDIV instruction, in most cases the dividend
1576 // (numerator) must be in a specific register pair highreg:lowreg,
1577 // producing the quotient in lowreg and the remainder in highreg.
1578 // For most data types, to set up the instruction, the dividend is
1579 // copied into lowreg, and lowreg is sign-extended or zero-extended
1580 // into highreg. The exception is i8, where the dividend is defined
1581 // as a single register rather than a register pair, and we
1582 // therefore directly sign-extend or zero-extend the dividend into
1583 // lowreg, instead of copying, and ignore the highreg.
1584 const static struct DivRemEntry {
1585 // The following portion depends only on the data type.
1586 const TargetRegisterClass *RC;
1587 unsigned LowInReg; // low part of the register pair
1588 unsigned HighInReg; // high part of the register pair
1589 // The following portion depends on both the data type and the operation.
1590 struct DivRemResult {
1591 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1592 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1593 // highreg, or copying a zero into highreg.
1594 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1595 // zero/sign-extending into lowreg for i8.
1596 unsigned DivRemResultReg; // Register containing the desired result.
1597 bool IsOpSigned; // Whether to use signed or unsigned form.
1598 } ResultTable[NumOps];
1599 } OpTable[NumTypes] = {
1600 { &X86::GR8RegClass, X86::AX, 0, {
1601 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1602 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1603 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1604 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1605 }
1606 }, // i8
1607 { &X86::GR16RegClass, X86::AX, X86::DX, {
1608 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1609 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001610 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1611 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001612 }
1613 }, // i16
1614 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1615 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1616 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1617 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1618 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1619 }
1620 }, // i32
1621 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1622 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1623 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001624 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1625 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001626 }
1627 }, // i64
1628 };
1629
1630 MVT VT;
1631 if (!isTypeLegal(I->getType(), VT))
1632 return false;
1633
1634 unsigned TypeIndex, OpIndex;
1635 switch (VT.SimpleTy) {
1636 default: return false;
1637 case MVT::i8: TypeIndex = 0; break;
1638 case MVT::i16: TypeIndex = 1; break;
1639 case MVT::i32: TypeIndex = 2; break;
1640 case MVT::i64: TypeIndex = 3;
1641 if (!Subtarget->is64Bit())
1642 return false;
1643 break;
1644 }
1645
1646 switch (I->getOpcode()) {
1647 default: llvm_unreachable("Unexpected div/rem opcode");
1648 case Instruction::SDiv: OpIndex = 0; break;
1649 case Instruction::SRem: OpIndex = 1; break;
1650 case Instruction::UDiv: OpIndex = 2; break;
1651 case Instruction::URem: OpIndex = 3; break;
1652 }
1653
1654 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1655 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1656 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1657 if (Op0Reg == 0)
1658 return false;
1659 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1660 if (Op1Reg == 0)
1661 return false;
1662
1663 // Move op0 into low-order input register.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001664 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001665 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1666 // Zero-extend or sign-extend into high-order input register.
1667 if (OpEntry.OpSignExtend) {
1668 if (OpEntry.IsOpSigned)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001669 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001670 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001671 else {
1672 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001673 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001674 TII.get(X86::MOV32r0), Zero32);
1675
1676 // Copy the zero into the appropriate sub/super/identical physical
1677 // register. Unfortunately the operations needed are not uniform enough to
1678 // fit neatly into the table above.
1679 if (VT.SimpleTy == MVT::i16) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001680 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001681 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001682 .addReg(Zero32, 0, X86::sub_16bit);
1683 } else if (VT.SimpleTy == MVT::i32) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001684 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001685 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001686 .addReg(Zero32);
1687 } else if (VT.SimpleTy == MVT::i64) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001688 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001689 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1690 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1691 }
1692 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001693 }
1694 // Generate the DIV/IDIV instruction.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001695 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001696 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001697 // For i8 remainder, we can't reference AH directly, as we'll end
1698 // up with bogus copies like %R9B = COPY %AH. Reference AX
1699 // instead to prevent AH references in a REX instruction.
1700 //
1701 // The current assumption of the fast register allocator is that isel
1702 // won't generate explicit references to the GPR8_NOREX registers. If
1703 // the allocator and/or the backend get enhanced to be more robust in
1704 // that regard, this can be, and should be, removed.
1705 unsigned ResultReg = 0;
1706 if ((I->getOpcode() == Instruction::SRem ||
1707 I->getOpcode() == Instruction::URem) &&
1708 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1709 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1710 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001711 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbachc35388f2013-07-09 02:07:25 +00001712 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1713
1714 // Shift AX right by 8 bits instead of using AH.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001715 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
Jim Grosbachc35388f2013-07-09 02:07:25 +00001716 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1717
1718 // Now reference the 8-bit subreg of the result.
1719 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1720 /*Kill=*/true, X86::sub_8bit);
1721 }
1722 // Copy the result out of the physreg if we haven't already.
1723 if (!ResultReg) {
1724 ResultReg = createResultReg(TypeEntry.RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001725 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
Jim Grosbachc35388f2013-07-09 02:07:25 +00001726 .addReg(OpEntry.DivRemResultReg);
1727 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001728 UpdateValueMap(I, ResultReg);
1729
1730 return true;
1731}
1732
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001733/// \brief Emit a conditional move instruction (if the are supported) to lower
1734/// the select.
1735bool X86FastISel::X86FastEmitCMoveSelect(const Instruction *I) {
1736 MVT RetVT;
1737 if (!isTypeLegal(I->getType(), RetVT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001738 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001739
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001740 // Check if the subtarget supports these instructions.
1741 if (!Subtarget->hasCMov())
Wesley Peck527da1b2010-11-23 03:31:01 +00001742 return false;
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001743
1744 // FIXME: Add support for i8.
1745 unsigned Opc;
1746 switch (RetVT.SimpleTy) {
1747 default: return false;
1748 case MVT::i16: Opc = X86::CMOVNE16rr; break;
1749 case MVT::i32: Opc = X86::CMOVNE32rr; break;
1750 case MVT::i64: Opc = X86::CMOVNE64rr; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001751 }
1752
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001753 const Value *Cond = I->getOperand(0);
1754 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1755 bool NeedTest = true;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001756
Juergen Ributzka296833c2014-06-25 20:06:12 +00001757 // Optimize conditons coming from a compare if both instructions are in the
1758 // same basic block (values defined in other basic blocks may not have
1759 // initialized registers).
1760 const auto *CI = dyn_cast<CmpInst>(Cond);
1761 if (CI && (CI->getParent() == I->getParent())) {
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001762 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1763
1764 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
1765 static unsigned SETFOpcTable[2][3] = {
1766 { X86::SETNPr, X86::SETEr , X86::TEST8rr },
1767 { X86::SETPr, X86::SETNEr, X86::OR8rr }
1768 };
1769 unsigned *SETFOpc = nullptr;
1770 switch (Predicate) {
1771 default: break;
1772 case CmpInst::FCMP_OEQ:
1773 SETFOpc = &SETFOpcTable[0][0];
1774 Predicate = CmpInst::ICMP_NE;
1775 break;
1776 case CmpInst::FCMP_UNE:
1777 SETFOpc = &SETFOpcTable[1][0];
1778 Predicate = CmpInst::ICMP_NE;
1779 break;
1780 }
1781
1782 X86::CondCode CC;
1783 bool NeedSwap;
1784 std::tie(CC, NeedSwap) = getX86ConditonCode(Predicate);
1785 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
1786 Opc = X86::getCMovFromCond(CC, RC->getSize());
1787
1788 const Value *CmpLHS = CI->getOperand(0);
1789 const Value *CmpRHS = CI->getOperand(1);
1790 if (NeedSwap)
1791 std::swap(CmpLHS, CmpRHS);
1792
1793 EVT CmpVT = TLI.getValueType(CmpLHS->getType());
1794 // Emit a compare of the LHS and RHS, setting the flags.
1795 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT))
1796 return false;
1797
1798 if (SETFOpc) {
1799 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1800 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1802 FlagReg1);
1803 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1804 FlagReg2);
1805 auto const &II = TII.get(SETFOpc[2]);
1806 if (II.getNumDefs()) {
1807 unsigned TmpReg = createResultReg(&X86::GR8RegClass);
1808 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, TmpReg)
1809 .addReg(FlagReg2).addReg(FlagReg1);
1810 } else {
1811 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
1812 .addReg(FlagReg2).addReg(FlagReg1);
1813 }
1814 }
1815 NeedTest = false;
Juergen Ributzka2bce27e2014-06-24 23:51:21 +00001816 } else if (auto *EV = dyn_cast<ExtractValueInst>(Cond)) {
1817 bool FoldIntrinsic = false;
1818 if (const auto *II = dyn_cast<IntrinsicInst>(EV->getAggregateOperand())) {
1819 switch (II->getIntrinsicID()) {
Juergen Ributzka9029bda2014-06-25 16:49:37 +00001820 default: break;
1821 case Intrinsic::sadd_with_overflow:
1822 case Intrinsic::uadd_with_overflow:
1823 case Intrinsic::ssub_with_overflow:
1824 case Intrinsic::usub_with_overflow:
1825 case Intrinsic::smul_with_overflow:
1826 case Intrinsic::umul_with_overflow: FoldIntrinsic = true; break;
Juergen Ributzka2bce27e2014-06-24 23:51:21 +00001827 }
1828
1829 // Check if both instructions are in the same basic block.
1830 if (FoldIntrinsic && (II->getParent() != I->getParent()))
1831 FoldIntrinsic = false;
1832
1833 // Make sure nothing is in the way
1834 if (FoldIntrinsic) {
1835 BasicBlock::const_iterator Start = I;
1836 BasicBlock::const_iterator End = II;
1837 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
1838 // We only expect extractvalue instructions between the intrinsic and
1839 // the branch.
1840 if (!isa<ExtractValueInst>(Itr)) {
1841 FoldIntrinsic = false;
1842 break;
1843 }
1844
1845 // Check that the extractvalue operand comes from the intrinsic.
1846 const auto *EVI = cast<ExtractValueInst>(Itr);
1847 if (EVI->getAggregateOperand() != II) {
1848 FoldIntrinsic = false;
1849 break;
1850 }
1851 }
1852 }
1853 }
1854
1855 if (FoldIntrinsic) {
1856 MVT RetVT;
1857 const IntrinsicInst *II = cast<IntrinsicInst>(EV->getAggregateOperand());
1858 const Function *Callee = II->getCalledFunction();
1859 Type *RetTy =
1860 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
1861 if (!isTypeLegal(RetTy, RetVT))
1862 return false;
1863
1864 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1865 return false;
1866
1867 // Fake request the condition, otherwise the intrinsic might be completely
1868 // optimized away.
1869 unsigned TmpReg = getRegForValue(EV);
1870 if (TmpReg == 0)
1871 return false;
1872
1873 switch (II->getIntrinsicID()) {
1874 default: llvm_unreachable("Unexpected intrinsic instruction.");
1875 case Intrinsic::sadd_with_overflow:
1876 case Intrinsic::ssub_with_overflow:
1877 case Intrinsic::smul_with_overflow:
1878 case Intrinsic::umul_with_overflow:
1879 Opc = X86::getCMovFromCond(X86::COND_O, RC->getSize());
1880 break;
1881 case Intrinsic::uadd_with_overflow:
1882 case Intrinsic::usub_with_overflow:
1883 Opc = X86::getCMovFromCond(X86::COND_B, RC->getSize());
1884 break;
1885 }
1886 NeedTest = false;
1887 }
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001888 }
1889
1890 if (NeedTest) {
1891 // Selects operate on i1, however, CondReg is 8 bits width and may contain
1892 // garbage. Indeed, only the less significant bit is supposed to be
1893 // accurate. If we read more than the lsb, we may see non-zero values
1894 // whereas lsb is zero. Therefore, we have to truncate Op0Reg to i1 for
1895 // the select. This is achieved by performing TEST against 1.
1896 unsigned CondReg = getRegForValue(Cond);
1897 if (CondReg == 0)
1898 return false;
1899 bool CondIsKill = hasTrivialKill(Cond);
1900
1901 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1902 .addReg(CondReg, getKillRegState(CondIsKill)).addImm(1);
1903 }
1904
1905 const Value *LHS = I->getOperand(1);
1906 const Value *RHS = I->getOperand(2);
1907
1908 unsigned RHSReg = getRegForValue(RHS);
1909 bool RHSIsKill = hasTrivialKill(RHS);
1910
1911 unsigned LHSReg = getRegForValue(LHS);
1912 bool LHSIsKill = hasTrivialKill(LHS);
1913
1914 if (!LHSReg || !RHSReg)
1915 return false;
1916
1917 unsigned ResultReg = FastEmitInst_rr(Opc, RC, RHSReg, RHSIsKill,
1918 LHSReg, LHSIsKill);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001919 UpdateValueMap(I, ResultReg);
1920 return true;
1921}
1922
Juergen Ributzka21d56082014-06-23 21:55:40 +00001923/// \brief Emit SSE instructions to lower the select.
1924///
1925/// Try to use SSE1/SSE2 instructions to simulate a select without branches.
1926/// This lowers fp selects into a CMP/AND/ANDN/OR sequence when the necessary
1927/// SSE instructions are available.
1928bool X86FastISel::X86FastEmitSSESelect(const Instruction *I) {
1929 MVT RetVT;
1930 if (!isTypeLegal(I->getType(), RetVT))
1931 return false;
1932
Juergen Ributzka296833c2014-06-25 20:06:12 +00001933 // Optimize conditons coming from a compare if both instructions are in the
1934 // same basic block (values defined in other basic blocks may not have
1935 // initialized registers).
Juergen Ributzka21d56082014-06-23 21:55:40 +00001936 const auto *CI = dyn_cast<FCmpInst>(I->getOperand(0));
Juergen Ributzka296833c2014-06-25 20:06:12 +00001937 if (!CI || (CI->getParent() != I->getParent()))
Juergen Ributzka21d56082014-06-23 21:55:40 +00001938 return false;
1939
1940 if (I->getType() != CI->getOperand(0)->getType() ||
1941 !((Subtarget->hasSSE1() && RetVT == MVT::f32) ||
1942 (Subtarget->hasSSE2() && RetVT == MVT::f64) ))
1943 return false;
1944
1945 const Value *CmpLHS = CI->getOperand(0);
1946 const Value *CmpRHS = CI->getOperand(1);
1947 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1948
1949 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1950 // We don't have to materialize a zero constant for this case and can just use
1951 // %x again on the RHS.
1952 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1953 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1954 if (CmpRHSC && CmpRHSC->isNullValue())
1955 CmpRHS = CmpLHS;
1956 }
1957
1958 unsigned CC;
1959 bool NeedSwap;
1960 std::tie(CC, NeedSwap) = getX86SSECondtionCode(Predicate);
1961 if (CC > 7)
1962 return false;
1963
1964 if (NeedSwap)
1965 std::swap(CmpLHS, CmpRHS);
1966
1967 static unsigned OpcTable[2][2][4] = {
1968 { { X86::CMPSSrr, X86::FsANDPSrr, X86::FsANDNPSrr, X86::FsORPSrr },
1969 { X86::VCMPSSrr, X86::VFsANDPSrr, X86::VFsANDNPSrr, X86::VFsORPSrr } },
1970 { { X86::CMPSDrr, X86::FsANDPDrr, X86::FsANDNPDrr, X86::FsORPDrr },
1971 { X86::VCMPSDrr, X86::VFsANDPDrr, X86::VFsANDNPDrr, X86::VFsORPDrr } }
1972 };
1973
1974 bool HasAVX = Subtarget->hasAVX();
1975 unsigned *Opc = nullptr;
1976 switch (RetVT.SimpleTy) {
1977 default: return false;
1978 case MVT::f32: Opc = &OpcTable[0][HasAVX][0]; break;
1979 case MVT::f64: Opc = &OpcTable[1][HasAVX][0]; break;
1980 }
1981
1982 const Value *LHS = I->getOperand(1);
1983 const Value *RHS = I->getOperand(2);
1984
1985 unsigned LHSReg = getRegForValue(LHS);
1986 bool LHSIsKill = hasTrivialKill(LHS);
1987
1988 unsigned RHSReg = getRegForValue(RHS);
1989 bool RHSIsKill = hasTrivialKill(RHS);
1990
1991 unsigned CmpLHSReg = getRegForValue(CmpLHS);
1992 bool CmpLHSIsKill = hasTrivialKill(CmpLHS);
1993
1994 unsigned CmpRHSReg = getRegForValue(CmpRHS);
1995 bool CmpRHSIsKill = hasTrivialKill(CmpRHS);
1996
1997 if (!LHSReg || !RHSReg || !CmpLHS || !CmpRHS)
1998 return false;
1999
2000 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2001 unsigned CmpReg = FastEmitInst_rri(Opc[0], RC, CmpLHSReg, CmpLHSIsKill,
2002 CmpRHSReg, CmpRHSIsKill, CC);
2003 unsigned AndReg = FastEmitInst_rr(Opc[1], RC, CmpReg, /*IsKill=*/false,
2004 LHSReg, LHSIsKill);
2005 unsigned AndNReg = FastEmitInst_rr(Opc[2], RC, CmpReg, /*IsKill=*/true,
2006 RHSReg, RHSIsKill);
2007 unsigned ResultReg = FastEmitInst_rr(Opc[3], RC, AndNReg, /*IsKill=*/true,
2008 AndReg, /*IsKill=*/true);
2009 UpdateValueMap(I, ResultReg);
2010 return true;
2011}
2012
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00002013bool X86FastISel::X86FastEmitPseudoSelect(const Instruction *I) {
2014 MVT RetVT;
2015 if (!isTypeLegal(I->getType(), RetVT))
2016 return false;
2017
2018 // These are pseudo CMOV instructions and will be later expanded into control-
2019 // flow.
2020 unsigned Opc;
2021 switch (RetVT.SimpleTy) {
2022 default: return false;
2023 case MVT::i8: Opc = X86::CMOV_GR8; break;
2024 case MVT::i16: Opc = X86::CMOV_GR16; break;
2025 case MVT::i32: Opc = X86::CMOV_GR32; break;
2026 case MVT::f32: Opc = X86::CMOV_FR32; break;
2027 case MVT::f64: Opc = X86::CMOV_FR64; break;
2028 }
2029
2030 const Value *Cond = I->getOperand(0);
2031 X86::CondCode CC = X86::COND_NE;
Juergen Ributzka296833c2014-06-25 20:06:12 +00002032
2033 // Optimize conditons coming from a compare if both instructions are in the
2034 // same basic block (values defined in other basic blocks may not have
2035 // initialized registers).
2036 const auto *CI = dyn_cast<CmpInst>(Cond);
2037 if (CI && (CI->getParent() == I->getParent())) {
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00002038 bool NeedSwap;
2039 std::tie(CC, NeedSwap) = getX86ConditonCode(CI->getPredicate());
2040 if (CC > X86::LAST_VALID_COND)
2041 return false;
2042
2043 const Value *CmpLHS = CI->getOperand(0);
2044 const Value *CmpRHS = CI->getOperand(1);
2045
2046 if (NeedSwap)
2047 std::swap(CmpLHS, CmpRHS);
2048
2049 EVT CmpVT = TLI.getValueType(CmpLHS->getType());
2050 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT))
2051 return false;
2052 } else {
2053 unsigned CondReg = getRegForValue(Cond);
2054 if (CondReg == 0)
2055 return false;
2056 bool CondIsKill = hasTrivialKill(Cond);
2057 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
2058 .addReg(CondReg, getKillRegState(CondIsKill)).addImm(1);
2059 }
2060
2061 const Value *LHS = I->getOperand(1);
2062 const Value *RHS = I->getOperand(2);
2063
2064 unsigned LHSReg = getRegForValue(LHS);
2065 bool LHSIsKill = hasTrivialKill(LHS);
2066
2067 unsigned RHSReg = getRegForValue(RHS);
2068 bool RHSIsKill = hasTrivialKill(RHS);
2069
2070 if (!LHSReg || !RHSReg)
2071 return false;
2072
2073 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2074
2075 unsigned ResultReg =
2076 FastEmitInst_rri(Opc, RC, RHSReg, RHSIsKill, LHSReg, LHSIsKill, CC);
2077 UpdateValueMap(I, ResultReg);
2078 return true;
2079}
2080
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00002081bool X86FastISel::X86SelectSelect(const Instruction *I) {
2082 MVT RetVT;
2083 if (!isTypeLegal(I->getType(), RetVT))
2084 return false;
2085
2086 // Check if we can fold the select.
2087 if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) {
2088 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2089 const Value *Opnd = nullptr;
2090 switch (Predicate) {
2091 default: break;
2092 case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break;
2093 case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break;
2094 }
2095 // No need for a select anymore - this is an unconditional move.
2096 if (Opnd) {
2097 unsigned OpReg = getRegForValue(Opnd);
2098 if (OpReg == 0)
2099 return false;
2100 bool OpIsKill = hasTrivialKill(Opnd);
2101 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2102 unsigned ResultReg = createResultReg(RC);
2103 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2104 TII.get(TargetOpcode::COPY), ResultReg)
2105 .addReg(OpReg, getKillRegState(OpIsKill));
2106 UpdateValueMap(I, ResultReg);
2107 return true;
2108 }
2109 }
2110
2111 // First try to use real conditional move instructions.
2112 if (X86FastEmitCMoveSelect(I))
2113 return true;
2114
Juergen Ributzka21d56082014-06-23 21:55:40 +00002115 // Try to use a sequence of SSE instructions to simulate a conditonal move.
2116 if (X86FastEmitSSESelect(I))
2117 return true;
2118
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00002119 // Fall-back to pseudo conditional move instructions, which will be later
2120 // converted to control-flow.
2121 if (X86FastEmitPseudoSelect(I))
2122 return true;
2123
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00002124 return false;
2125}
2126
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002127bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00002128 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002129 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00002130 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002131 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002132 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00002133 unsigned OpReg = getRegForValue(V);
2134 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00002135 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002137 TII.get(X86::CVTSS2SDrr), ResultReg)
2138 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00002139 UpdateValueMap(I, ResultReg);
2140 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00002141 }
2142 }
2143
2144 return false;
2145}
2146
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002147bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002148 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00002149 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002150 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002151 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00002152 unsigned OpReg = getRegForValue(V);
2153 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00002154 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002155 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002156 TII.get(X86::CVTSD2SSrr), ResultReg)
2157 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002158 UpdateValueMap(I, ResultReg);
2159 return true;
2160 }
2161 }
2162 }
2163
2164 return false;
2165}
2166
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002167bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002168 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2169 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00002170
Eli Friedmanc7035512011-05-25 23:49:02 +00002171 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00002172 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00002173 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00002174 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00002175 return false;
2176
2177 unsigned InputReg = getRegForValue(I->getOperand(0));
2178 if (!InputReg)
2179 // Unhandled operand. Halt "fast" selection and bail.
2180 return false;
2181
Eli Friedmanc7035512011-05-25 23:49:02 +00002182 if (SrcVT == MVT::i8) {
2183 // Truncate from i8 to i1; no code needed.
2184 UpdateValueMap(I, InputReg);
2185 return true;
2186 }
Evan Chengb9286692008-09-07 08:47:42 +00002187
Eli Friedmanc7035512011-05-25 23:49:02 +00002188 if (!Subtarget->is64Bit()) {
2189 // If we're on x86-32; we can't extract an i8 from a general register.
2190 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00002191 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
2192 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
2193 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00002194 unsigned CopyReg = createResultReg(CopyRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002195 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Eli Friedmanc7035512011-05-25 23:49:02 +00002196 CopyReg).addReg(InputReg);
2197 InputReg = CopyReg;
2198 }
2199
2200 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00002201 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00002202 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00002203 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00002204 if (!ResultReg)
2205 return false;
2206
2207 UpdateValueMap(I, ResultReg);
2208 return true;
2209}
2210
Eli Friedman60afcc22011-05-20 22:21:04 +00002211bool X86FastISel::IsMemcpySmall(uint64_t Len) {
2212 return Len <= (Subtarget->is64Bit() ? 32 : 16);
2213}
2214
Eli Friedmanbcc69142011-04-27 01:45:07 +00002215bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
2216 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00002217
Eli Friedmanbcc69142011-04-27 01:45:07 +00002218 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00002219 if (!IsMemcpySmall(Len))
2220 return false;
2221
2222 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00002223
2224 // We don't care about alignment here since we just emit integer accesses.
2225 while (Len) {
2226 MVT VT;
2227 if (Len >= 8 && i64Legal)
2228 VT = MVT::i64;
2229 else if (Len >= 4)
2230 VT = MVT::i32;
2231 else if (Len >= 2)
2232 VT = MVT::i16;
2233 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00002234 VT = MVT::i8;
2235 }
2236
2237 unsigned Reg;
Juergen Ributzka349777d2014-06-12 23:27:57 +00002238 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
2239 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
Eli Friedmanbcc69142011-04-27 01:45:07 +00002240 assert(RV && "Failed to emit load or store??");
2241
2242 unsigned Size = VT.getSizeInBits()/8;
2243 Len -= Size;
2244 DestAM.Disp += Size;
2245 SrcAM.Disp += Size;
2246 }
2247
2248 return true;
2249}
2250
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002251static bool isCommutativeIntrinsic(IntrinsicInst const &I) {
2252 switch (I.getIntrinsicID()) {
2253 case Intrinsic::sadd_with_overflow:
2254 case Intrinsic::uadd_with_overflow:
2255 case Intrinsic::smul_with_overflow:
2256 case Intrinsic::umul_with_overflow:
2257 return true;
2258 default:
2259 return false;
2260 }
2261}
2262
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002263bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00002264 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00002265 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00002266 default: return false;
Juergen Ributzka4dc95872014-06-11 21:44:44 +00002267 case Intrinsic::frameaddress: {
2268 Type *RetTy = I.getCalledFunction()->getReturnType();
2269
2270 MVT VT;
2271 if (!isTypeLegal(RetTy, VT))
2272 return false;
2273
2274 unsigned Opc;
2275 const TargetRegisterClass *RC = nullptr;
2276
2277 switch (VT.SimpleTy) {
2278 default: llvm_unreachable("Invalid result type for frameaddress.");
2279 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
2280 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
2281 }
2282
2283 // This needs to be set before we call getFrameRegister, otherwise we get
2284 // the wrong frame register.
2285 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2286 MFI->setFrameAddressIsTaken(true);
2287
2288 const X86RegisterInfo *RegInfo =
2289 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
2290 unsigned FrameReg = RegInfo->getFrameRegister(*(FuncInfo.MF));
2291 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
2292 (FrameReg == X86::EBP && VT == MVT::i32)) &&
2293 "Invalid Frame Register!");
2294
2295 // Always make a copy of the frame register to to a vreg first, so that we
2296 // never directly reference the frame register (the TwoAddressInstruction-
2297 // Pass doesn't like that).
2298 unsigned SrcReg = createResultReg(RC);
2299 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2300 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
2301
2302 // Now recursively load from the frame address.
2303 // movq (%rbp), %rax
2304 // movq (%rax), %rax
2305 // movq (%rax), %rax
2306 // ...
2307 unsigned DestReg;
2308 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2309 while (Depth--) {
2310 DestReg = createResultReg(RC);
2311 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2312 TII.get(Opc), DestReg), SrcReg);
2313 SrcReg = DestReg;
2314 }
2315
2316 UpdateValueMap(&I, SrcReg);
2317 return true;
2318 }
Chris Lattner91328b32011-04-19 05:52:03 +00002319 case Intrinsic::memcpy: {
2320 const MemCpyInst &MCI = cast<MemCpyInst>(I);
2321 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00002322 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00002323 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00002324
Eli Friedmancd2124a2011-06-10 23:39:36 +00002325 if (isa<ConstantInt>(MCI.getLength())) {
2326 // Small memcpy's are common enough that we want to do them
2327 // without a call if possible.
2328 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
2329 if (IsMemcpySmall(Len)) {
2330 X86AddressMode DestAM, SrcAM;
2331 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
2332 !X86SelectAddress(MCI.getRawSource(), SrcAM))
2333 return false;
2334 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
2335 return true;
2336 }
2337 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002338
Eli Friedmancd2124a2011-06-10 23:39:36 +00002339 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2340 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00002341 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00002342
Eli Friedmancd2124a2011-06-10 23:39:36 +00002343 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
2344 return false;
2345
2346 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00002347 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00002348 case Intrinsic::memset: {
2349 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00002350
Nick Lewyckya530a4d2011-08-02 00:40:16 +00002351 if (MSI.isVolatile())
2352 return false;
2353
Eli Friedmancd2124a2011-06-10 23:39:36 +00002354 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2355 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
2356 return false;
2357
2358 if (MSI.getDestAddressSpace() > 255)
2359 return false;
2360
2361 return DoSelectCall(&I, "memset");
2362 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002363 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00002364 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002365 EVT PtrTy = TLI.getPointerTy();
2366
Gabor Greif83205af2010-06-26 11:51:52 +00002367 const Value *Op1 = I.getArgOperand(0); // The guard's value.
2368 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002369
Josh Magee22b8ba22013-12-19 03:17:11 +00002370 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
2371
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002372 // Grab the frame index.
2373 X86AddressMode AM;
2374 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00002375 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002376 return true;
2377 }
Dale Johannesend5575f22010-01-26 00:09:58 +00002378 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002379 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00002380 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00002381 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00002382 if (!X86SelectAddress(DI->getAddress(), AM))
2383 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00002384 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00002385 // FIXME may need to add RegState::Debug to any registers produced,
2386 // although ESP/EBP should be the only ones at the moment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002387 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002388 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00002389 return true;
2390 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002391 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002392 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002393 return true;
2394 }
Juergen Ributzka272b5702014-06-11 23:11:02 +00002395 case Intrinsic::sqrt: {
2396 if (!Subtarget->hasSSE1())
2397 return false;
2398
2399 Type *RetTy = I.getCalledFunction()->getReturnType();
2400
2401 MVT VT;
2402 if (!isTypeLegal(RetTy, VT))
2403 return false;
2404
2405 // Unfortunatelly we can't use FastEmit_r, because the AVX version of FSQRT
2406 // is not generated by FastISel yet.
2407 // FIXME: Update this code once tablegen can handle it.
2408 static const unsigned SqrtOpc[2][2] = {
2409 {X86::SQRTSSr, X86::VSQRTSSr},
2410 {X86::SQRTSDr, X86::VSQRTSDr}
2411 };
2412 bool HasAVX = Subtarget->hasAVX();
2413 unsigned Opc;
2414 const TargetRegisterClass *RC;
2415 switch (VT.SimpleTy) {
2416 default: return false;
2417 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
2418 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
2419 }
2420
2421 const Value *SrcVal = I.getArgOperand(0);
2422 unsigned SrcReg = getRegForValue(SrcVal);
2423
2424 if (SrcReg == 0)
2425 return false;
2426
2427 unsigned ImplicitDefReg = 0;
2428 if (HasAVX) {
2429 ImplicitDefReg = createResultReg(RC);
2430 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2431 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2432 }
2433
2434 unsigned ResultReg = createResultReg(RC);
2435 MachineInstrBuilder MIB;
2436 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
2437 ResultReg);
2438
2439 if (ImplicitDefReg)
2440 MIB.addReg(ImplicitDefReg);
2441
2442 MIB.addReg(SrcReg);
2443
2444 UpdateValueMap(&I, ResultReg);
2445 return true;
2446 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002447 case Intrinsic::sadd_with_overflow:
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002448 case Intrinsic::uadd_with_overflow:
2449 case Intrinsic::ssub_with_overflow:
2450 case Intrinsic::usub_with_overflow:
2451 case Intrinsic::smul_with_overflow:
2452 case Intrinsic::umul_with_overflow: {
2453 // This implements the basic lowering of the xalu with overflow intrinsics
2454 // into add/sub/mul folowed by either seto or setb.
Bill Wendling80b34b32008-12-09 02:42:50 +00002455 const Function *Callee = I.getCalledFunction();
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002456 auto *Ty = cast<StructType>(Callee->getReturnType());
2457 Type *RetTy = Ty->getTypeAtIndex(0U);
2458 Type *CondTy = Ty->getTypeAtIndex(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00002459
Duncan Sandsf5dda012010-11-03 11:35:31 +00002460 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00002461 if (!isTypeLegal(RetTy, VT))
2462 return false;
2463
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002464 if (VT < MVT::i8 || VT > MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00002465 return false;
2466
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002467 const Value *LHS = I.getArgOperand(0);
2468 const Value *RHS = I.getArgOperand(1);
2469
2470 // Canonicalize immediates to the RHS.
2471 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2472 isCommutativeIntrinsic(I))
2473 std::swap(LHS, RHS);
2474
2475 unsigned BaseOpc, CondOpc;
2476 switch (I.getIntrinsicID()) {
2477 default: llvm_unreachable("Unexpected intrinsic!");
2478 case Intrinsic::sadd_with_overflow:
2479 BaseOpc = ISD::ADD; CondOpc = X86::SETOr; break;
2480 case Intrinsic::uadd_with_overflow:
2481 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
2482 case Intrinsic::ssub_with_overflow:
2483 BaseOpc = ISD::SUB; CondOpc = X86::SETOr; break;
2484 case Intrinsic::usub_with_overflow:
2485 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
2486 case Intrinsic::smul_with_overflow:
2487 BaseOpc = ISD::MUL; CondOpc = X86::SETOr; break;
2488 case Intrinsic::umul_with_overflow:
2489 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
2490 }
2491
2492 unsigned LHSReg = getRegForValue(LHS);
2493 if (LHSReg == 0)
2494 return false;
2495 bool LHSIsKill = hasTrivialKill(LHS);
2496
2497 unsigned ResultReg = 0;
2498 // Check if we have an immediate version.
2499 if (auto const *C = dyn_cast<ConstantInt>(RHS)) {
2500 ResultReg = FastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2501 C->getZExtValue());
2502 }
2503
2504 unsigned RHSReg;
2505 bool RHSIsKill;
2506 if (!ResultReg) {
2507 RHSReg = getRegForValue(RHS);
2508 if (RHSReg == 0)
2509 return false;
2510 RHSIsKill = hasTrivialKill(RHS);
2511 ResultReg = FastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2512 RHSIsKill);
2513 }
2514
2515 // FastISel doesn't have a pattern for X86::MUL*r. Emit it manually.
2516 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
2517 static const unsigned MULOpc[] =
2518 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
2519 static const unsigned Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
2520 // First copy the first operand into RAX, which is an implicit input to
2521 // the X86::MUL*r instruction.
2522 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2523 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2524 .addReg(LHSReg, getKillRegState(LHSIsKill));
2525 ResultReg = FastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2526 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
2527 }
2528
2529 if (!ResultReg)
Bill Wendling80b34b32008-12-09 02:42:50 +00002530 return false;
2531
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002532 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
2533 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
2534 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
2535 ResultReg2);
Eli Friedmana4d4a012011-05-16 21:06:17 +00002536
2537 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00002538 return true;
2539 }
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002540 case Intrinsic::x86_sse_cvttss2si:
2541 case Intrinsic::x86_sse_cvttss2si64:
2542 case Intrinsic::x86_sse2_cvttsd2si:
2543 case Intrinsic::x86_sse2_cvttsd2si64: {
2544 bool IsInputDouble;
2545 switch (I.getIntrinsicID()) {
2546 default: llvm_unreachable("Unexpected intrinsic.");
2547 case Intrinsic::x86_sse_cvttss2si:
2548 case Intrinsic::x86_sse_cvttss2si64:
2549 if (!Subtarget->hasSSE1())
2550 return false;
2551 IsInputDouble = false;
2552 break;
2553 case Intrinsic::x86_sse2_cvttsd2si:
2554 case Intrinsic::x86_sse2_cvttsd2si64:
2555 if (!Subtarget->hasSSE2())
2556 return false;
2557 IsInputDouble = true;
2558 break;
2559 }
2560
2561 Type *RetTy = I.getCalledFunction()->getReturnType();
2562 MVT VT;
2563 if (!isTypeLegal(RetTy, VT))
2564 return false;
2565
2566 static const unsigned CvtOpc[2][2][2] = {
2567 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
2568 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
2569 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
2570 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
2571 };
2572 bool HasAVX = Subtarget->hasAVX();
2573 unsigned Opc;
2574 switch (VT.SimpleTy) {
2575 default: llvm_unreachable("Unexpected result type.");
2576 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
2577 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
2578 }
2579
2580 // Check if we can fold insertelement instructions into the convert.
2581 const Value *Op = I.getArgOperand(0);
2582 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
2583 const Value *Index = IE->getOperand(2);
2584 if (!isa<ConstantInt>(Index))
2585 break;
2586 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
2587
2588 if (Idx == 0) {
2589 Op = IE->getOperand(1);
2590 break;
2591 }
2592 Op = IE->getOperand(0);
2593 }
2594
2595 unsigned Reg = getRegForValue(Op);
2596 if (Reg == 0)
2597 return false;
2598
2599 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
2600 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2601 .addReg(Reg);
2602
2603 UpdateValueMap(&I, ResultReg);
2604 return true;
2605 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002606 }
2607}
2608
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002609bool X86FastISel::FastLowerArguments() {
2610 if (!FuncInfo.CanLowerReturn)
2611 return false;
2612
2613 const Function *F = FuncInfo.Fn;
2614 if (F->isVarArg())
2615 return false;
2616
2617 CallingConv::ID CC = F->getCallingConv();
2618 if (CC != CallingConv::C)
2619 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00002620
2621 if (Subtarget->isCallingConvWin64(CC))
2622 return false;
2623
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002624 if (!Subtarget->is64Bit())
2625 return false;
2626
2627 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002628 unsigned GPRCnt = 0;
2629 unsigned FPRCnt = 0;
2630 unsigned Idx = 0;
2631 for (auto const &Arg : F->args()) {
2632 // The first argument is at index 1.
2633 ++Idx;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002634 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2635 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2636 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2637 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2638 return false;
2639
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002640 Type *ArgTy = Arg.getType();
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002641 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
2642 return false;
2643
2644 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00002645 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002646 switch (ArgVT.getSimpleVT().SimpleTy) {
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002647 default: return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002648 case MVT::i32:
2649 case MVT::i64:
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002650 ++GPRCnt;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002651 break;
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002652 case MVT::f32:
2653 case MVT::f64:
2654 if (!Subtarget->hasSSE1())
2655 return false;
2656 ++FPRCnt;
2657 break;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002658 }
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002659
2660 if (GPRCnt > 6)
2661 return false;
2662
2663 if (FPRCnt > 8)
2664 return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002665 }
2666
Craig Topper840beec2014-04-04 05:16:06 +00002667 static const MCPhysReg GPR32ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002668 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
2669 };
Craig Topper840beec2014-04-04 05:16:06 +00002670 static const MCPhysReg GPR64ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002671 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
2672 };
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002673 static const MCPhysReg XMMArgRegs[] = {
2674 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2675 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2676 };
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002677
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002678 unsigned GPRIdx = 0;
2679 unsigned FPRIdx = 0;
2680 for (auto const &Arg : F->args()) {
2681 MVT VT = TLI.getSimpleValueType(Arg.getType());
2682 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
2683 unsigned SrcReg;
2684 switch (VT.SimpleTy) {
2685 default: llvm_unreachable("Unexpected value type.");
2686 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
2687 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
2688 case MVT::f32: // fall-through
2689 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
2690 }
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002691 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2692 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2693 // Without this, EmitLiveInCopies may eliminate the livein if its only
2694 // use is a bitcast (which isn't turned into an instruction).
2695 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002696 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002697 TII.get(TargetOpcode::COPY), ResultReg)
2698 .addReg(DstReg, getKillRegState(true));
2699 UpdateValueMap(&Arg, ResultReg);
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002700 }
2701 return true;
2702}
2703
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002704bool X86FastISel::X86SelectCall(const Instruction *I) {
2705 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00002706 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002707
2708 // Can't handle inline asm yet.
2709 if (isa<InlineAsm>(Callee))
2710 return false;
2711
Bill Wendling80b34b32008-12-09 02:42:50 +00002712 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002713 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00002714 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002715
Chad Rosierdf42cf32012-12-11 00:18:02 +00002716 // Allow SelectionDAG isel to handle tail calls.
2717 if (cast<CallInst>(I)->isTailCall())
2718 return false;
2719
Craig Topper062a2ba2014-04-25 05:30:21 +00002720 return DoSelectCall(I, nullptr);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002721}
2722
Rafael Espindola73173c52012-07-25 15:42:45 +00002723static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
2724 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002725 if (Subtarget.is64Bit())
2726 return 0;
Rafael Espindola32cb5ac2013-12-12 16:06:58 +00002727 if (Subtarget.getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002728 return 0;
2729 CallingConv::ID CC = CS.getCallingConv();
2730 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
2731 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002732 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002733 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002734 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00002735 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002736 return 4;
2737}
2738
Eli Friedmancd2124a2011-06-10 23:39:36 +00002739// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
2740bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
2741 const CallInst *CI = cast<CallInst>(I);
2742 const Value *Callee = CI->getCalledValue();
2743
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002744 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002745 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002746 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00002747 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002748 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00002749 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
2750 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002751 return false;
2752
Evan Chengd10089a2010-01-27 00:00:57 +00002753 // fastcc with -tailcallopt is intended to provide a guaranteed
2754 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002755 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00002756 return false;
2757
Chris Lattner229907c2011-07-18 04:54:35 +00002758 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2759 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002760 bool isVarArg = FTy->isVarArg();
2761
2762 // Don't know how to handle Win64 varargs yet. Nothing special needed for
2763 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00002764 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002765 return false;
2766
Reid Klecknerf5b76512014-01-31 23:50:57 +00002767 // Don't know about inalloca yet.
2768 if (CS.hasInAllocaArgument())
2769 return false;
2770
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002771 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00002772 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002773 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002774 return false;
2775
Eli Friedman7b279422011-05-17 18:29:03 +00002776 // Check whether the function can return without sret-demotion.
2777 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002778 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00002779 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00002780 *FuncInfo.MF, FTy->isVarArg(),
2781 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002782 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00002783 return false;
2784
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002785 // Materialize callee address in a register. FIXME: GV address can be
2786 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00002787 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00002788 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00002789 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002790 unsigned CalleeOp = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002791 const GlobalValue *GV = nullptr;
2792 if (CalleeAM.GV != nullptr) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002793 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00002794 } else if (CalleeAM.Base.Reg != 0) {
2795 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002796 } else
2797 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002798
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002799 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002800 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002801 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002802 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002803 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00002804 unsigned arg_size = CS.arg_size();
2805 Args.reserve(arg_size);
2806 ArgVals.reserve(arg_size);
2807 ArgVTs.reserve(arg_size);
2808 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002809 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002810 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00002811 // If we're lowering a mem intrinsic instead of a regular call, skip the
2812 // last two arguments, which should not passed to the underlying functions.
2813 if (MemIntName && e-i <= 2)
2814 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002815 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002816 ISD::ArgFlagsTy Flags;
2817 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002818 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002819 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002820 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002821 Flags.setZExt();
2822
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002823 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00002824 PointerType *Ty = cast<PointerType>(ArgVal->getType());
2825 Type *ElementTy = Ty->getElementType();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002826 unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
Eli Friedman60afcc22011-05-20 22:21:04 +00002827 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
2828 if (!FrameAlign)
2829 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
2830 Flags.setByVal();
2831 Flags.setByValSize(FrameSize);
2832 Flags.setByValAlign(FrameAlign);
2833 if (!IsMemcpySmall(FrameSize))
2834 return false;
2835 }
2836
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002837 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00002838 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002839 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00002840 Flags.setNest();
2841
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002842 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
2843 // instruction. This is safe because it is common to all fastisel supported
2844 // calling conventions on x86.
2845 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
2846 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
2847 CI->getBitWidth() == 16) {
2848 if (Flags.isSExt())
2849 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
2850 else
2851 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
2852 }
2853 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002854
Chris Lattner5f4b7832011-04-19 05:09:50 +00002855 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002856
Chris Lattner34a08c22011-04-19 05:15:59 +00002857 // Passing bools around ends up doing a trunc to i1 and passing it.
2858 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00002859 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2860 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2861 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002862 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2863 ArgReg = getRegForValue(ArgVal);
2864 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002865
Chris Lattner5f4b7832011-04-19 05:09:50 +00002866 MVT ArgVT;
2867 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002868
Chris Lattner5f4b7832011-04-19 05:09:50 +00002869 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2870 ArgVal->hasOneUse(), 1);
2871 } else {
2872 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002873 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002874
Chris Lattner34a08c22011-04-19 05:15:59 +00002875 if (ArgReg == 0) return false;
2876
Chris Lattner229907c2011-07-18 04:54:35 +00002877 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002878 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002879 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002880 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002881 if (ArgVT == MVT::x86mmx)
2882 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002883 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002884 Flags.setOrigAlign(OriginalAlignment);
2885
Chris Lattner5f4b7832011-04-19 05:09:50 +00002886 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002887 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002888 ArgVTs.push_back(ArgVT);
2889 ArgFlags.push_back(Flags);
2890 }
2891
2892 // Analyze operands of the call, assigning locations to each operand.
2893 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002894 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002895 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002896
Dan Gohman47a07242010-06-01 21:09:47 +00002897 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002898 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002899 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002900
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002901 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002902
2903 // Get a count of how many bytes are to be pushed on the stack.
2904 unsigned NumBytes = CCInfo.getNextStackOffset();
2905
2906 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002907 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002908 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002909 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002910
Chris Lattner3ba29352008-10-15 05:30:52 +00002911 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002912 // copies / loads.
2913 SmallVector<unsigned, 4> RegArgs;
2914 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2915 CCValAssign &VA = ArgLocs[i];
2916 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002917 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002918
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002919 // Promote the value if needed.
2920 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002921 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002922 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002923 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2924 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002925 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2926 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002927 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002928 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002929 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002930 }
2931 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002932 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2933 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002934 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2935 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002936 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002937 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002938 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002939 }
2940 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002941 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2942 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002943 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2944 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002945 if (!Emitted)
2946 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002947 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002948 if (!Emitted)
2949 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2950 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002951
Chris Lattner2d7df022011-01-05 22:26:52 +00002952 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002953 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002954 break;
2955 }
Dan Gohman8c795692009-08-05 05:33:42 +00002956 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002957 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002958 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002959 assert(BC != 0 && "Failed to emit a bitcast!");
2960 Arg = BC;
2961 ArgVT = VA.getLocVT();
2962 break;
2963 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002964 case CCValAssign::VExt:
2965 // VExt has not been implemented, so this should be impossible to reach
2966 // for now. However, fallback to Selection DAG isel once implemented.
2967 return false;
2968 case CCValAssign::Indirect:
2969 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2970 // support this.
2971 return false;
Lang Hames06234ec2014-01-14 19:56:36 +00002972 case CCValAssign::FPExt:
2973 llvm_unreachable("Unexpected loc info!");
Evan Cheng6500d172008-09-08 06:35:17 +00002974 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002975
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002976 if (VA.isRegLoc()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002977 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2978 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002979 RegArgs.push_back(VA.getLocReg());
2980 } else {
2981 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002982 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002983 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2984 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002985 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002986 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002987 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002988 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002989
Eli Friedman60afcc22011-05-20 22:21:04 +00002990 if (Flags.isByVal()) {
2991 X86AddressMode SrcAM;
2992 SrcAM.Base.Reg = Arg;
2993 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2994 assert(Res && "memcpy length already checked!"); (void)Res;
2995 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2996 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002997 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002998 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002999 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
3000 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00003001 } else {
Juergen Ributzka349777d2014-06-12 23:27:57 +00003002 if (!X86FastEmitStore(ArgVT, Arg, /*ValIsKill=*/false, AM))
Lang Hames7d2f7b52011-10-18 22:11:33 +00003003 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00003004 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003005 }
3006 }
3007
Dan Gohman3691d502008-09-25 15:24:26 +00003008 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00003009 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00003010 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00003011 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003012 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3013 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00003014 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003015
Charles Davise8f297c2013-07-12 06:02:35 +00003016 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00003017 // Count the number of XMM registers allocated.
Craig Topper840beec2014-04-04 05:16:06 +00003018 static const MCPhysReg XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00003019 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3020 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3021 };
3022 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003023 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
Eli Friedmanee92a6b2011-04-19 17:22:22 +00003024 X86::AL).addImm(NumXMMRegs);
3025 }
3026
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003027 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00003028 MachineInstrBuilder MIB;
3029 if (CalleeOp) {
3030 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00003031 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00003032 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00003033 CallOpc = X86::CALL64r;
3034 else
3035 CallOpc = X86::CALL32r;
Rafael Espindolaea09c592014-02-18 22:05:46 +00003036 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003037 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00003038
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00003039 } else {
3040 // Direct call.
3041 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00003042 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00003043 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00003044 CallOpc = X86::CALL64pcrel32;
3045 else
3046 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00003047
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00003048 // See if we need any target-specific flags on the GV operand.
3049 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003050
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00003051 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
3052 // external symbols most go through the PLT in PIC mode. If the symbol
3053 // has hidden or protected visibility, or if it is static or local, then
3054 // we don't need to use the PLT - we can directly call it.
3055 if (Subtarget->isTargetELF() &&
3056 TM.getRelocationModel() == Reloc::PIC_ &&
3057 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
3058 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00003059 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00003060 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003061 (!Subtarget->getTargetTriple().isMacOSX() ||
3062 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00003063 // PC-relative references to external symbols should go through $stub,
3064 // unless we're building with the leopard linker or later, which
3065 // automatically synthesizes these stubs.
3066 OpFlags = X86II::MO_DARWIN_STUB;
3067 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003068
3069
Rafael Espindolaea09c592014-02-18 22:05:46 +00003070 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Eli Friedmancd2124a2011-06-10 23:39:36 +00003071 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00003072 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00003073 else
3074 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00003075 }
Dan Gohman3691d502008-09-25 15:24:26 +00003076
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00003077 // Add a register mask with the call-preserved registers.
3078 // Proper defs for return values will be added by setPhysRegsDeadExcept().
3079 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
3080
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00003081 // Add an implicit use GOT pointer in EBX.
3082 if (Subtarget->isPICStyleGOT())
3083 MIB.addReg(X86::EBX, RegState::Implicit);
3084
Charles Davise8f297c2013-07-12 06:02:35 +00003085 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00003086 MIB.addReg(X86::AL, RegState::Implicit);
3087
3088 // Add implicit physical register uses to the call.
3089 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
3090 MIB.addReg(RegArgs[i], RegState::Implicit);
3091
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003092 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00003093 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00003094 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003095 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00003096 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003097
Eli Friedman7b279422011-05-17 18:29:03 +00003098 // Build info for return calling conv lowering code.
3099 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
3100 SmallVector<ISD::InputArg, 32> Ins;
3101 SmallVector<EVT, 4> RetTys;
3102 ComputeValueVTs(TLI, I->getType(), RetTys);
3103 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
3104 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00003105 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00003106 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
3107 for (unsigned j = 0; j != NumRegs; ++j) {
3108 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00003109 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00003110 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003111 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00003112 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003113 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00003114 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003115 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00003116 MyFlags.Flags.setInReg();
3117 Ins.push_back(MyFlags);
3118 }
3119 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00003120
Eli Friedman7b279422011-05-17 18:29:03 +00003121 // Now handle call return values.
3122 SmallVector<unsigned, 4> UsedRegs;
3123 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003124 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00003125 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00003126 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
3127 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
3128 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3129 EVT CopyVT = RVLocs[i].getValVT();
3130 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00003131
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003132 // If this is a call to a function that returns an fp value on the x87 fp
3133 // stack, but where we prefer to use the value in xmm registers, copy it
3134 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00003135 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00003136 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00003137 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00003138 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00003139 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00003140 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00003141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3142 TII.get(X86::FpPOP_RETVAL), CopyReg);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00003143 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00003144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3145 TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00003146 CopyReg).addReg(RVLocs[i].getLocReg());
3147 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003148 }
3149
Eli Friedman7b279422011-05-17 18:29:03 +00003150 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003151 // Round the F80 the right size, which also moves to the appropriate xmm
3152 // register. This is accomplished by storing the F80 value in memory and
3153 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00003154 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00003155 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003156 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00003157 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003158 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003159 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00003160 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00003161 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Rafael Espindolaea09c592014-02-18 22:05:46 +00003162 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Friedman7b279422011-05-17 18:29:03 +00003163 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003164 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00003165 }
Eli Friedman83ba1502011-05-17 00:13:47 +00003166
Eli Friedman7b279422011-05-17 18:29:03 +00003167 if (RVLocs.size())
3168 UpdateValueMap(I, ResultReg, RVLocs.size());
3169
Dan Gohman86936502010-06-18 23:28:01 +00003170 // Set all unused physreg defs as dead.
3171 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
3172
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003173 return true;
3174}
3175
3176
Dan Gohmand58f3e32008-08-28 23:21:34 +00003177bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003178X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00003179 switch (I->getOpcode()) {
3180 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00003181 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00003182 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00003183 case Instruction::Store:
3184 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003185 case Instruction::Ret:
3186 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00003187 case Instruction::ICmp:
3188 case Instruction::FCmp:
3189 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00003190 case Instruction::ZExt:
3191 return X86SelectZExt(I);
3192 case Instruction::Br:
3193 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00003194 case Instruction::Call:
3195 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00003196 case Instruction::LShr:
3197 case Instruction::AShr:
3198 case Instruction::Shl:
3199 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00003200 case Instruction::SDiv:
3201 case Instruction::UDiv:
3202 case Instruction::SRem:
3203 case Instruction::URem:
3204 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00003205 case Instruction::Select:
3206 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00003207 case Instruction::Trunc:
3208 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00003209 case Instruction::FPExt:
3210 return X86SelectFPExt(I);
3211 case Instruction::FPTrunc:
3212 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00003213 case Instruction::IntToPtr: // Deliberate fall-through.
3214 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003215 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
3216 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00003217 if (DstVT.bitsGT(SrcVT))
3218 return X86SelectZExt(I);
3219 if (DstVT.bitsLT(SrcVT))
3220 return X86SelectTrunc(I);
3221 unsigned Reg = getRegForValue(I->getOperand(0));
3222 if (Reg == 0) return false;
3223 UpdateValueMap(I, Reg);
3224 return true;
3225 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00003226 }
3227
3228 return false;
3229}
3230
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003231unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003232 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00003233 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00003234 return 0;
3235
3236 // Can't handle alternate code models yet.
3237 if (TM.getCodeModel() != CodeModel::Small)
3238 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003239
Owen Anderson50288e32008-09-05 00:06:23 +00003240 // Get opcode and regclass of the output for the given load instruction.
3241 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003242 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00003243 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00003244 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00003245 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00003246 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00003247 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003248 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003249 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00003250 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00003251 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003252 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003253 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00003254 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00003255 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003256 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003257 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00003258 // Must be in x86-64 mode.
3259 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00003260 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003261 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003262 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00003263 if (X86ScalarSSEf32) {
3264 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00003265 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003266 } else {
3267 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00003268 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003269 }
3270 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003271 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00003272 if (X86ScalarSSEf64) {
3273 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00003274 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003275 } else {
3276 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00003277 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003278 }
3279 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003280 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00003281 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00003282 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00003283 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003284
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003285 // Materialize addresses with LEA/MOV instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00003286 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00003287 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00003288 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00003289 // If the expression is just a basereg, then we're done, otherwise we need
3290 // to emit an LEA.
3291 if (AM.BaseType == X86AddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00003292 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
Chris Lattner48326602011-04-17 17:12:08 +00003293 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003294
Dan Gohman9801ba42008-09-19 22:16:54 +00003295 unsigned ResultReg = createResultReg(RC);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003296 if (TM.getRelocationModel() == Reloc::Static &&
3297 TLI.getPointerTy() == MVT::i64) {
3298 // The displacement code be more than 32 bits away so we need to use
3299 // an instruction with a 64 bit immediate
3300 Opc = X86::MOV64ri;
3301 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3302 TII.get(Opc), ResultReg).addGlobalAddress(cast<GlobalValue>(C));
3303 } else {
3304 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
3305 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003306 TII.get(Opc), ResultReg), AM);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003307 }
Owen Anderson50288e32008-09-05 00:06:23 +00003308 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00003309 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00003310 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00003311 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003312
Owen Andersond41c7162008-09-06 01:11:01 +00003313 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00003314 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00003315 if (Align == 0) {
3316 // Alignment of vector types. FIXME!
Rafael Espindolaea09c592014-02-18 22:05:46 +00003317 Align = DL.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00003318 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003319
Dan Gohman8392f0c2008-09-30 01:21:32 +00003320 // x86-32 PIC requires a PIC base register for constant pools.
3321 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00003322 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00003323 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00003324 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00003325 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00003326 } else if (Subtarget->isPICStyleGOT()) {
3327 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00003328 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00003329 } else if (Subtarget->isPICStyleRIPRel() &&
3330 TM.getCodeModel() == CodeModel::Small) {
3331 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00003332 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00003333
3334 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00003335 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00003336 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003337 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003338 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00003339 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00003340
Owen Anderson50288e32008-09-05 00:06:23 +00003341 return ResultReg;
3342}
3343
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003344unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003345 // Fail on dynamic allocas. At this point, getRegForValue has already
3346 // checked its CSE maps, so if we're here trying to handle a dynamic
3347 // alloca, we're not going to succeed. X86SelectAddress has a
3348 // check for dynamic allocas, because it's called directly from
3349 // various places, but TargetMaterializeAlloca also needs a check
3350 // in order to avoid recursion between getRegForValue,
3351 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00003352 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003353 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00003354 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003355
Dan Gohman39d82f92008-09-10 20:11:02 +00003356 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00003357 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00003358 return 0;
3359 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00003360 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00003361 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003362 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003363 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00003364 return ResultReg;
3365}
3366
Eli Friedman406c4712011-04-27 22:41:55 +00003367unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
3368 MVT VT;
3369 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003370 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003371
3372 // Get opcode and regclass for the given zero.
3373 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003374 const TargetRegisterClass *RC = nullptr;
Eli Friedman406c4712011-04-27 22:41:55 +00003375 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003376 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00003377 case MVT::f32:
3378 if (X86ScalarSSEf32) {
3379 Opc = X86::FsFLD0SS;
3380 RC = &X86::FR32RegClass;
3381 } else {
3382 Opc = X86::LD_Fp032;
3383 RC = &X86::RFP32RegClass;
3384 }
3385 break;
3386 case MVT::f64:
3387 if (X86ScalarSSEf64) {
3388 Opc = X86::FsFLD0SD;
3389 RC = &X86::FR64RegClass;
3390 } else {
3391 Opc = X86::LD_Fp064;
3392 RC = &X86::RFP64RegClass;
3393 }
3394 break;
3395 case MVT::f80:
3396 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003397 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003398 }
3399
3400 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003401 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Eli Friedman406c4712011-04-27 22:41:55 +00003402 return ResultReg;
3403}
3404
3405
Eli Bendersky90dd3e72013-04-19 22:29:18 +00003406bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3407 const LoadInst *LI) {
Juergen Ributzka349777d2014-06-12 23:27:57 +00003408 const Value *Ptr = LI->getPointerOperand();
Chris Lattnereeba0c72010-09-05 02:18:34 +00003409 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00003410 if (!X86SelectAddress(Ptr, AM))
Chris Lattnereeba0c72010-09-05 02:18:34 +00003411 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003412
Craig Topper55406d92012-08-11 17:46:16 +00003413 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00003414
Rafael Espindolaea09c592014-02-18 22:05:46 +00003415 unsigned Size = DL.getTypeAllocSize(LI->getType());
Chris Lattnereeba0c72010-09-05 02:18:34 +00003416 unsigned Alignment = LI->getAlignment();
3417
Juergen Ributzka349777d2014-06-12 23:27:57 +00003418 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3419 Alignment = DL.getABITypeAlignment(LI->getType());
3420
Chris Lattnereeba0c72010-09-05 02:18:34 +00003421 SmallVector<MachineOperand, 8> AddrOps;
3422 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00003423
Chris Lattnereeba0c72010-09-05 02:18:34 +00003424 MachineInstr *Result =
3425 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
Juergen Ributzka349777d2014-06-12 23:27:57 +00003426 if (!Result)
3427 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003428
Juergen Ributzka349777d2014-06-12 23:27:57 +00003429 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Chris Lattner2d186572011-01-16 02:27:38 +00003430 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00003431 MI->eraseFromParent();
3432 return true;
3433}
3434
3435
Evan Cheng24422d42008-09-03 00:03:49 +00003436namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003437 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
3438 const TargetLibraryInfo *libInfo) {
3439 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00003440 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00003441}