blob: af9eaf32a3325855921cfc19eee57a569c0b22ec [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
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000118 bool X86SelectSelect(const Instruction *I);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000119
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000120 bool X86SelectTrunc(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000121
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000122 bool X86SelectFPExt(const Instruction *I);
123 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohmanbf646f22008-09-10 21:02:08 +0000124
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000125 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
126 bool X86SelectCall(const Instruction *I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000127
Eli Friedmancd2124a2011-06-10 23:39:36 +0000128 bool DoSelectCall(const Instruction *I, const char *MemIntName);
129
Dan Gohman3691d502008-09-25 15:24:26 +0000130 const X86InstrInfo *getInstrInfo() const {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000131 return getTargetMachine()->getInstrInfo();
132 }
133 const X86TargetMachine *getTargetMachine() const {
134 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman3691d502008-09-25 15:24:26 +0000135 }
136
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000137 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
138
Craig Topper2d9361e2014-03-09 07:44:38 +0000139 unsigned TargetMaterializeConstant(const Constant *C) override;
Dan Gohman39d82f92008-09-10 20:11:02 +0000140
Craig Topper2d9361e2014-03-09 07:44:38 +0000141 unsigned TargetMaterializeAlloca(const AllocaInst *C) override;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000142
Craig Topper2d9361e2014-03-09 07:44:38 +0000143 unsigned TargetMaterializeFloatZero(const ConstantFP *CF) override;
Eli Friedman406c4712011-04-27 22:41:55 +0000144
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000145 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
146 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000147 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000148 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
149 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000150 }
151
Chris Lattner229907c2011-07-18 04:54:35 +0000152 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000153
Eli Friedman60afcc22011-05-20 22:21:04 +0000154 bool IsMemcpySmall(uint64_t Len);
155
Eli Friedmanbcc69142011-04-27 01:45:07 +0000156 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
157 X86AddressMode SrcAM, uint64_t Len);
Evan Cheng24422d42008-09-03 00:03:49 +0000158};
Wesley Peck527da1b2010-11-23 03:31:01 +0000159
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000160} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000161
Juergen Ributzkaaa602092014-06-17 21:55:43 +0000162static CmpInst::Predicate optimizeCmpPredicate(const CmpInst *CI) {
163 // If both operands are the same, then try to optimize or fold the cmp.
164 CmpInst::Predicate Predicate = CI->getPredicate();
165 if (CI->getOperand(0) != CI->getOperand(1))
166 return Predicate;
167
168 switch (Predicate) {
169 default: llvm_unreachable("Invalid predicate!");
170 case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break;
171 case CmpInst::FCMP_OEQ: Predicate = CmpInst::FCMP_ORD; break;
172 case CmpInst::FCMP_OGT: Predicate = CmpInst::FCMP_FALSE; break;
173 case CmpInst::FCMP_OGE: Predicate = CmpInst::FCMP_ORD; break;
174 case CmpInst::FCMP_OLT: Predicate = CmpInst::FCMP_FALSE; break;
175 case CmpInst::FCMP_OLE: Predicate = CmpInst::FCMP_ORD; break;
176 case CmpInst::FCMP_ONE: Predicate = CmpInst::FCMP_FALSE; break;
177 case CmpInst::FCMP_ORD: Predicate = CmpInst::FCMP_ORD; break;
178 case CmpInst::FCMP_UNO: Predicate = CmpInst::FCMP_UNO; break;
179 case CmpInst::FCMP_UEQ: Predicate = CmpInst::FCMP_TRUE; break;
180 case CmpInst::FCMP_UGT: Predicate = CmpInst::FCMP_UNO; break;
181 case CmpInst::FCMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
182 case CmpInst::FCMP_ULT: Predicate = CmpInst::FCMP_UNO; break;
183 case CmpInst::FCMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
184 case CmpInst::FCMP_UNE: Predicate = CmpInst::FCMP_UNO; break;
185 case CmpInst::FCMP_TRUE: Predicate = CmpInst::FCMP_TRUE; break;
186
187 case CmpInst::ICMP_EQ: Predicate = CmpInst::FCMP_TRUE; break;
188 case CmpInst::ICMP_NE: Predicate = CmpInst::FCMP_FALSE; break;
189 case CmpInst::ICMP_UGT: Predicate = CmpInst::FCMP_FALSE; break;
190 case CmpInst::ICMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
191 case CmpInst::ICMP_ULT: Predicate = CmpInst::FCMP_FALSE; break;
192 case CmpInst::ICMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
193 case CmpInst::ICMP_SGT: Predicate = CmpInst::FCMP_FALSE; break;
194 case CmpInst::ICMP_SGE: Predicate = CmpInst::FCMP_TRUE; break;
195 case CmpInst::ICMP_SLT: Predicate = CmpInst::FCMP_FALSE; break;
196 case CmpInst::ICMP_SLE: Predicate = CmpInst::FCMP_TRUE; break;
197 }
198
199 return Predicate;
200}
201
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +0000202static std::pair<X86::CondCode, bool>
203getX86ConditonCode(CmpInst::Predicate Predicate) {
204 X86::CondCode CC = X86::COND_INVALID;
205 bool NeedSwap = false;
206 switch (Predicate) {
207 default: break;
208 // Floating-point Predicates
209 case CmpInst::FCMP_UEQ: CC = X86::COND_E; break;
210 case CmpInst::FCMP_OLT: NeedSwap = true; // fall-through
211 case CmpInst::FCMP_OGT: CC = X86::COND_A; break;
212 case CmpInst::FCMP_OLE: NeedSwap = true; // fall-through
213 case CmpInst::FCMP_OGE: CC = X86::COND_AE; break;
214 case CmpInst::FCMP_UGT: NeedSwap = true; // fall-through
215 case CmpInst::FCMP_ULT: CC = X86::COND_B; break;
216 case CmpInst::FCMP_UGE: NeedSwap = true; // fall-through
217 case CmpInst::FCMP_ULE: CC = X86::COND_BE; break;
218 case CmpInst::FCMP_ONE: CC = X86::COND_NE; break;
219 case CmpInst::FCMP_UNO: CC = X86::COND_P; break;
220 case CmpInst::FCMP_ORD: CC = X86::COND_NP; break;
221 case CmpInst::FCMP_OEQ: // fall-through
222 case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break;
223
224 // Integer Predicates
225 case CmpInst::ICMP_EQ: CC = X86::COND_E; break;
226 case CmpInst::ICMP_NE: CC = X86::COND_NE; break;
227 case CmpInst::ICMP_UGT: CC = X86::COND_A; break;
228 case CmpInst::ICMP_UGE: CC = X86::COND_AE; break;
229 case CmpInst::ICMP_ULT: CC = X86::COND_B; break;
230 case CmpInst::ICMP_ULE: CC = X86::COND_BE; break;
231 case CmpInst::ICMP_SGT: CC = X86::COND_G; break;
232 case CmpInst::ICMP_SGE: CC = X86::COND_GE; break;
233 case CmpInst::ICMP_SLT: CC = X86::COND_L; break;
234 case CmpInst::ICMP_SLE: CC = X86::COND_LE; break;
235 }
236
237 return std::make_pair(CC, NeedSwap);
238}
239
Juergen Ributzka21d56082014-06-23 21:55:40 +0000240static std::pair<unsigned, bool>
241getX86SSECondtionCode(CmpInst::Predicate Predicate) {
242 unsigned CC;
243 bool NeedSwap = false;
244
245 // SSE Condition code mapping:
246 // 0 - EQ
247 // 1 - LT
248 // 2 - LE
249 // 3 - UNORD
250 // 4 - NEQ
251 // 5 - NLT
252 // 6 - NLE
253 // 7 - ORD
254 switch (Predicate) {
255 default: llvm_unreachable("Unexpected predicate");
256 case CmpInst::FCMP_OEQ: CC = 0; break;
257 case CmpInst::FCMP_OGT: NeedSwap = true; // fall-through
258 case CmpInst::FCMP_OLT: CC = 1; break;
259 case CmpInst::FCMP_OGE: NeedSwap = true; // fall-through
260 case CmpInst::FCMP_OLE: CC = 2; break;
261 case CmpInst::FCMP_UNO: CC = 3; break;
262 case CmpInst::FCMP_UNE: CC = 4; break;
263 case CmpInst::FCMP_ULE: NeedSwap = true; // fall-through
264 case CmpInst::FCMP_UGE: CC = 5; break;
265 case CmpInst::FCMP_ULT: NeedSwap = true; // fall-through
266 case CmpInst::FCMP_UGT: CC = 6; break;
267 case CmpInst::FCMP_ORD: CC = 7; break;
268 case CmpInst::FCMP_UEQ:
269 case CmpInst::FCMP_ONE: CC = 8; break;
270 }
271
272 return std::make_pair(CC, NeedSwap);
273}
274
Chris Lattner229907c2011-07-18 04:54:35 +0000275bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000276 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
277 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000278 // Unhandled type. Halt "fast" selection and bail.
279 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000280
281 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000282 // For now, require SSE/SSE2 for performing floating-point operations,
283 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000284 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000285 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000286 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000287 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000288 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000289 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000290 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000291 // We only handle legal types. For example, on x86-32 the instruction
292 // selector contains all of the 64-bit instructions from x86-64,
293 // under the assumption that i64 won't be used if the target doesn't
294 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000295 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000296}
297
298#include "X86GenCallingConv.inc"
299
Evan Chengf5bc7e52008-09-05 21:00:03 +0000300/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000301/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000302/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000303bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000304 MachineMemOperand *MMO, unsigned &ResultReg) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000305 // Get opcode and regclass of the output for the given load instruction.
306 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000307 const TargetRegisterClass *RC = nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000308 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000309 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000310 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000311 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000312 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000313 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000314 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000315 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000316 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000317 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000318 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000319 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000320 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000321 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000322 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000323 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000324 // Must be in x86-64 mode.
325 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000326 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000327 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000328 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000329 if (X86ScalarSSEf32) {
330 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000331 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000332 } else {
333 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000334 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000335 }
336 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000337 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000338 if (X86ScalarSSEf64) {
339 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000340 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000341 } else {
342 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000343 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000344 }
345 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000346 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000347 // No f80 support yet.
348 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000349 }
350
351 ResultReg = createResultReg(RC);
Juergen Ributzka349777d2014-06-12 23:27:57 +0000352 MachineInstrBuilder MIB =
353 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
354 addFullAddress(MIB, AM);
355 if (MMO)
356 MIB->addMemOperand(*FuncInfo.MF, MMO);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000357 return true;
358}
359
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000360/// X86FastEmitStore - Emit a machine instruction to store a value Val of
361/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
362/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000363/// i.e. V. Return true if it is possible.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000364bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
365 const X86AddressMode &AM,
366 MachineMemOperand *MMO, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000367 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000368 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000369 switch (VT.getSimpleVT().SimpleTy) {
370 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000371 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000372 case MVT::i1: {
373 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000374 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000375 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000376 TII.get(X86::AND8ri), AndResult)
377 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
Craig Topper4f55b0e2013-07-17 05:57:45 +0000378 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000379 }
380 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000381 case MVT::i8: Opc = X86::MOV8mr; break;
382 case MVT::i16: Opc = X86::MOV16mr; break;
383 case MVT::i32: Opc = X86::MOV32mr; break;
384 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
385 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000386 Opc = X86ScalarSSEf32 ?
387 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000388 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000389 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000390 Opc = X86ScalarSSEf64 ?
391 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000392 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000393 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000394 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000395 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000396 else
Craig Topper55475d42013-07-17 06:58:23 +0000397 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000398 break;
399 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000400 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000401 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000402 else
Craig Topperad1fff92013-07-18 07:16:44 +0000403 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000404 break;
405 case MVT::v4i32:
406 case MVT::v2i64:
407 case MVT::v8i16:
408 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000409 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000410 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000411 else
Craig Topper55475d42013-07-17 06:58:23 +0000412 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000413 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000414 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000415
Juergen Ributzka349777d2014-06-12 23:27:57 +0000416 MachineInstrBuilder MIB =
417 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
418 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
419 if (MMO)
420 MIB->addMemOperand(*FuncInfo.MF, MMO);
421
Evan Chengf5bc7e52008-09-05 21:00:03 +0000422 return true;
423}
424
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000425bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000426 const X86AddressMode &AM,
427 MachineMemOperand *MMO, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000428 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000429 if (isa<ConstantPointerNull>(Val))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000430 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000431
Chris Lattner3ba29352008-10-15 05:30:52 +0000432 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000433 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000434 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000435 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000436 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000437 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000438 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000439 case MVT::i8: Opc = X86::MOV8mi; break;
440 case MVT::i16: Opc = X86::MOV16mi; break;
441 case MVT::i32: Opc = X86::MOV32mi; break;
442 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000443 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000444 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000445 Opc = X86::MOV64mi32;
446 break;
447 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000448
Chris Lattner3ba29352008-10-15 05:30:52 +0000449 if (Opc) {
Juergen Ributzka349777d2014-06-12 23:27:57 +0000450 MachineInstrBuilder MIB =
451 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
452 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
453 : CI->getZExtValue());
454 if (MMO)
455 MIB->addMemOperand(*FuncInfo.MF, MMO);
Chris Lattner3ba29352008-10-15 05:30:52 +0000456 return true;
457 }
458 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000459
Chris Lattner3ba29352008-10-15 05:30:52 +0000460 unsigned ValReg = getRegForValue(Val);
461 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000462 return false;
463
Juergen Ributzka349777d2014-06-12 23:27:57 +0000464 bool ValKill = hasTrivialKill(Val);
465 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000466}
467
Evan Cheng6500d172008-09-08 06:35:17 +0000468/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
469/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
470/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000471bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
472 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000473 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000474 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
475 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000476 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000477 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000478
479 ResultReg = RR;
480 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000481}
482
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000483bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
484 // Handle constant address.
485 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
486 // Can't handle alternate code models yet.
487 if (TM.getCodeModel() != CodeModel::Small)
488 return false;
489
490 // Can't handle TLS yet.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000491 if (GV->isThreadLocal())
492 return false;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000493
494 // RIP-relative addresses can't have additional register operands, so if
495 // we've already folded stuff into the addressing mode, just force the
496 // global value into its own register, which we can use as the basereg.
497 if (!Subtarget->isPICStyleRIPRel() ||
498 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
499 // Okay, we've committed to selecting this global. Set up the address.
500 AM.GV = GV;
501
502 // Allow the subtarget to classify the global.
503 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
504
505 // If this reference is relative to the pic base, set it now.
506 if (isGlobalRelativeToPICBase(GVFlags)) {
507 // FIXME: How do we know Base.Reg is free??
508 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
509 }
510
511 // Unless the ABI requires an extra load, return a direct reference to
512 // the global.
513 if (!isGlobalStubReference(GVFlags)) {
514 if (Subtarget->isPICStyleRIPRel()) {
515 // Use rip-relative addressing if we can. Above we verified that the
516 // base and index registers are unused.
517 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
518 AM.Base.Reg = X86::RIP;
519 }
520 AM.GVOpFlags = GVFlags;
521 return true;
522 }
523
524 // Ok, we need to do a load from a stub. If we've already loaded from
525 // this stub, reuse the loaded pointer, otherwise emit the load now.
526 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
527 unsigned LoadReg;
528 if (I != LocalValueMap.end() && I->second != 0) {
529 LoadReg = I->second;
530 } else {
531 // Issue load from stub.
532 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000533 const TargetRegisterClass *RC = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000534 X86AddressMode StubAM;
535 StubAM.Base.Reg = AM.Base.Reg;
536 StubAM.GV = GV;
537 StubAM.GVOpFlags = GVFlags;
538
539 // Prepare for inserting code in the local-value area.
540 SavePoint SaveInsertPt = enterLocalValueArea();
541
542 if (TLI.getPointerTy() == MVT::i64) {
543 Opc = X86::MOV64rm;
544 RC = &X86::GR64RegClass;
545
546 if (Subtarget->isPICStyleRIPRel())
547 StubAM.Base.Reg = X86::RIP;
548 } else {
549 Opc = X86::MOV32rm;
550 RC = &X86::GR32RegClass;
551 }
552
553 LoadReg = createResultReg(RC);
554 MachineInstrBuilder LoadMI =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000555 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000556 addFullAddress(LoadMI, StubAM);
557
558 // Ok, back to normal mode.
559 leaveLocalValueArea(SaveInsertPt);
560
561 // Prevent loading GV stub multiple times in same MBB.
562 LocalValueMap[V] = LoadReg;
563 }
564
565 // Now construct the final address. Note that the Disp, Scale,
566 // and Index values may already be set here.
567 AM.Base.Reg = LoadReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000568 AM.GV = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000569 return true;
570 }
571 }
572
573 // If all else fails, try to materialize the value in a register.
574 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
575 if (AM.Base.Reg == 0) {
576 AM.Base.Reg = getRegForValue(V);
577 return AM.Base.Reg != 0;
578 }
579 if (AM.IndexReg == 0) {
580 assert(AM.Scale == 1 && "Scale with no index!");
581 AM.IndexReg = getRegForValue(V);
582 return AM.IndexReg != 0;
583 }
584 }
585
586 return false;
587}
588
Dan Gohman39d82f92008-09-10 20:11:02 +0000589/// X86SelectAddress - Attempt to fill in an address from the given value.
590///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000591bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000592 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000593redo_gep:
Craig Topper062a2ba2014-04-25 05:30:21 +0000594 const User *U = nullptr;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000595 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000596 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000597 // Don't walk into other basic blocks; it's possible we haven't
598 // visited them yet, so the instructions may not yet be assigned
599 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000600 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
601 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
602 Opcode = I->getOpcode();
603 U = I;
604 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000605 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000606 Opcode = C->getOpcode();
607 U = C;
608 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000609
Chris Lattner229907c2011-07-18 04:54:35 +0000610 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000611 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000612 // Fast instruction selection doesn't support the special
613 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000614 return false;
615
Dan Gohman6e005fd2008-09-18 23:23:44 +0000616 switch (Opcode) {
617 default: break;
618 case Instruction::BitCast:
619 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000620 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000621
622 case Instruction::IntToPtr:
623 // Look past no-op inttoptrs.
624 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000625 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000626 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000627
628 case Instruction::PtrToInt:
629 // Look past no-op ptrtoints.
630 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000631 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000632 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000633
634 case Instruction::Alloca: {
635 // Do static allocas.
636 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000637 DenseMap<const AllocaInst*, int>::iterator SI =
638 FuncInfo.StaticAllocaMap.find(A);
639 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000640 AM.BaseType = X86AddressMode::FrameIndexBase;
641 AM.Base.FrameIndex = SI->second;
642 return true;
643 }
644 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000645 }
646
647 case Instruction::Add: {
648 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000649 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000650 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
651 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000652 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000653 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000654 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000655 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000656 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000657 break;
658 }
659
660 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000661 X86AddressMode SavedAM = AM;
662
Dan Gohman6e005fd2008-09-18 23:23:44 +0000663 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000664 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000665 unsigned IndexReg = AM.IndexReg;
666 unsigned Scale = AM.Scale;
667 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000668 // Iterate through the indices, folding what we can. Constants can be
669 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000670 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000671 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000672 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000673 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000674 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000675 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
676 continue;
677 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000678
Chris Lattner4b026b92011-04-17 17:05:12 +0000679 // A array/variable index is always of the form i*S where S is the
680 // constant scale size. See if we can push the scale into immediates.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000681 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner4b026b92011-04-17 17:05:12 +0000682 for (;;) {
683 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
684 // Constant-offset addressing.
685 Disp += CI->getSExtValue() * S;
686 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000687 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000688 if (canFoldAddIntoGEP(U, Op)) {
689 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000690 ConstantInt *CI =
691 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
692 Disp += CI->getSExtValue() * S;
693 // Iterate on the other operand.
694 Op = cast<AddOperator>(Op)->getOperand(0);
695 continue;
696 }
697 if (IndexReg == 0 &&
698 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
699 (S == 1 || S == 2 || S == 4 || S == 8)) {
700 // Scaled-index addressing.
701 Scale = S;
702 IndexReg = getRegForGEPIndex(Op).first;
703 if (IndexReg == 0)
704 return false;
705 break;
706 }
707 // Unsupported.
708 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000709 }
710 }
Bill Wendling585a9012013-09-24 00:13:08 +0000711
Dan Gohman2564b902008-09-26 20:04:15 +0000712 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000713 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000714 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000715
Dan Gohman6e005fd2008-09-18 23:23:44 +0000716 AM.IndexReg = IndexReg;
717 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000718 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000719 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000720
721 if (const GetElementPtrInst *GEP =
722 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
723 // Ok, the GEP indices were covered by constant-offset and scaled-index
724 // addressing. Update the address state and move on to examining the base.
725 V = GEP;
726 goto redo_gep;
727 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000728 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000729 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000730
Chris Lattner4b026b92011-04-17 17:05:12 +0000731 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000732 // our address and just match the value instead of completely failing.
733 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000734
735 for (SmallVectorImpl<const Value *>::reverse_iterator
736 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
737 if (handleConstantAddresses(*I, AM))
738 return true;
739
740 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000741 unsupported_gep:
742 // Ok, the GEP indices weren't all covered.
743 break;
744 }
745 }
746
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000747 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000748}
749
Chris Lattner8212d372009-07-10 05:33:42 +0000750/// X86SelectCallAddress - Attempt to fill in an address from the given value.
751///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000752bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000753 const User *U = nullptr;
Chris Lattner8212d372009-07-10 05:33:42 +0000754 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000755 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000756 // Record if the value is defined in the same basic block.
757 //
758 // This information is crucial to know whether or not folding an
759 // operand is valid.
760 // Indeed, FastISel generates or reuses a virtual register for all
761 // operands of all instructions it selects. Obviously, the definition and
762 // its uses must use the same virtual register otherwise the produced
763 // code is incorrect.
764 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
765 // registers for values that are alive across basic blocks. This ensures
766 // that the values are consistently set between across basic block, even
767 // if different instruction selection mechanisms are used (e.g., a mix of
768 // SDISel and FastISel).
769 // For values local to a basic block, the instruction selection process
770 // generates these virtual registers with whatever method is appropriate
771 // for its needs. In particular, FastISel and SDISel do not share the way
772 // local virtual registers are set.
773 // Therefore, this is impossible (or at least unsafe) to share values
774 // between basic blocks unless they use the same instruction selection
775 // method, which is not guarantee for X86.
776 // Moreover, things like hasOneUse could not be used accurately, if we
777 // allow to reference values across basic blocks whereas they are not
778 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000779 bool InMBB = true;
780 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000781 Opcode = I->getOpcode();
782 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000783 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000784 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000785 Opcode = C->getOpcode();
786 U = C;
787 }
788
789 switch (Opcode) {
790 default: break;
791 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000792 // Look past bitcasts if its operand is in the same BB.
793 if (InMBB)
794 return X86SelectCallAddress(U->getOperand(0), AM);
795 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000796
797 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000798 // Look past no-op inttoptrs if its operand is in the same BB.
799 if (InMBB &&
800 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000801 return X86SelectCallAddress(U->getOperand(0), AM);
802 break;
803
804 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000805 // Look past no-op ptrtoints if its operand is in the same BB.
806 if (InMBB &&
807 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000808 return X86SelectCallAddress(U->getOperand(0), AM);
809 break;
810 }
811
812 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000813 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000814 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000815 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000816 return false;
817
818 // RIP-relative addresses can't have additional register operands.
819 if (Subtarget->isPICStyleRIPRel() &&
820 (AM.Base.Reg != 0 || AM.IndexReg != 0))
821 return false;
822
Rafael Espindolaea09c592014-02-18 22:05:46 +0000823 // Can't handle DbgLocLImport.
Nico Rieck7157bb72014-01-14 15:22:47 +0000824 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000825 return false;
826
827 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000828 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000829 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000830 return false;
831
832 // Okay, we've committed to selecting this global. Set up the basic address.
833 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000834
Chris Lattner7277a802009-07-10 05:45:15 +0000835 // No ABI requires an extra load for anything other than DLLImport, which
836 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000837 if (Subtarget->isPICStyleRIPRel()) {
838 // Use rip-relative addressing if we can. Above we verified that the
839 // base and index registers are unused.
840 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
841 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000842 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000843 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
844 } else if (Subtarget->isPICStyleGOT()) {
845 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000846 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000847
Chris Lattner8212d372009-07-10 05:33:42 +0000848 return true;
849 }
850
851 // If all else fails, try to materialize the value in a register.
852 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
853 if (AM.Base.Reg == 0) {
854 AM.Base.Reg = getRegForValue(V);
855 return AM.Base.Reg != 0;
856 }
857 if (AM.IndexReg == 0) {
858 assert(AM.Scale == 1 && "Scale with no index!");
859 AM.IndexReg = getRegForValue(V);
860 return AM.IndexReg != 0;
861 }
862 }
863
864 return false;
865}
866
867
Owen Anderson4f948bd2008-09-04 07:08:58 +0000868/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000869bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000870 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000871 const StoreInst *S = cast<StoreInst>(I);
872
873 if (S->isAtomic())
874 return false;
875
Juergen Ributzka349777d2014-06-12 23:27:57 +0000876 const Value *Val = S->getValueOperand();
877 const Value *Ptr = S->getPointerOperand();
Craig Topper4f55b0e2013-07-17 05:57:45 +0000878
Duncan Sandsf5dda012010-11-03 11:35:31 +0000879 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000880 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000881 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000882
Juergen Ributzka349777d2014-06-12 23:27:57 +0000883 unsigned Alignment = S->getAlignment();
884 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
885 if (Alignment == 0) // Ensure that codegen never sees alignment 0
886 Alignment = ABIAlignment;
887 bool Aligned = Alignment >= ABIAlignment;
888
Dan Gohman39d82f92008-09-10 20:11:02 +0000889 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000890 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000891 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000892
Juergen Ributzka349777d2014-06-12 23:27:57 +0000893 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000894}
895
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000896/// X86SelectRet - Select and emit code to implement ret instructions.
897bool X86FastISel::X86SelectRet(const Instruction *I) {
898 const ReturnInst *Ret = cast<ReturnInst>(I);
899 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000900 const X86MachineFunctionInfo *X86MFInfo =
901 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000902
903 if (!FuncInfo.CanLowerReturn)
904 return false;
905
906 CallingConv::ID CC = F.getCallingConv();
907 if (CC != CallingConv::C &&
908 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000909 CC != CallingConv::X86_FastCall &&
910 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000911 return false;
912
Charles Davise8f297c2013-07-12 06:02:35 +0000913 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000914 return false;
915
916 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000917 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000918 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000919
920 // fastcc with -tailcallopt is intended to provide a guaranteed
921 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000922 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000923 return false;
924
925 // Let SDISel handle vararg functions.
926 if (F.isVarArg())
927 return false;
928
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000929 // Build a list of return value registers.
930 SmallVector<unsigned, 4> RetRegs;
931
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000932 if (Ret->getNumOperands() > 0) {
933 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000934 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000935
936 // Analyze operands of the call, assigning locations to each operand.
937 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000938 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000939 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000940 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000941
942 const Value *RV = Ret->getOperand(0);
943 unsigned Reg = getRegForValue(RV);
944 if (Reg == 0)
945 return false;
946
947 // Only handle a single return value for now.
948 if (ValLocs.size() != 1)
949 return false;
950
951 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000952
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000953 // Don't bother handling odd stuff for now.
954 if (VA.getLocInfo() != CCValAssign::Full)
955 return false;
956 // Only handle register returns for now.
957 if (!VA.isRegLoc())
958 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000959
960 // The calling-convention tables for x87 returns don't tell
961 // the whole story.
962 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
963 return false;
964
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000965 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000966 EVT SrcVT = TLI.getValueType(RV->getType());
967 EVT DstVT = VA.getValVT();
968 // Special handling for extended integers.
969 if (SrcVT != DstVT) {
970 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
971 return false;
972
973 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
974 return false;
975
976 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
977
978 if (SrcVT == MVT::i1) {
979 if (Outs[0].Flags.isSExt())
980 return false;
981 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
982 SrcVT = MVT::i8;
983 }
984 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
985 ISD::SIGN_EXTEND;
986 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
987 SrcReg, /*TODO: Kill=*/false);
988 }
989
990 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000991 unsigned DstReg = VA.getLocReg();
992 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000993 // Avoid a cross-class copy. This is very unlikely.
994 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000995 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000996 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000997 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000998
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000999 // Add register to return instruction.
1000 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001001 }
1002
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001003 // The x86-64 ABI for returning structs by value requires that we copy
1004 // the sret argument into %rax for the return. We saved the argument into
1005 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001006 // and into %rax. We also do the same with %eax for Win32.
1007 if (F.hasStructRetAttr() &&
Yaron Keren136fe7d2014-04-01 18:15:34 +00001008 (Subtarget->is64Bit() || Subtarget->isTargetKnownWindowsMSVC())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001009 unsigned Reg = X86MFInfo->getSRetReturnReg();
1010 assert(Reg &&
1011 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001012 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001013 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001014 RetReg).addReg(Reg);
1015 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001016 }
1017
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001018 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001019 MachineInstrBuilder MIB =
Rafael Espindolaea09c592014-02-18 22:05:46 +00001020 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001021 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1022 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001023 return true;
1024}
1025
Evan Chenga41ee292008-09-03 06:44:39 +00001026/// X86SelectLoad - Select and emit code to implement load instructions.
1027///
Juergen Ributzka349777d2014-06-12 23:27:57 +00001028bool X86FastISel::X86SelectLoad(const Instruction *I) {
1029 const LoadInst *LI = cast<LoadInst>(I);
1030
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001031 // Atomic loads need special handling.
Juergen Ributzka349777d2014-06-12 23:27:57 +00001032 if (LI->isAtomic())
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001033 return false;
1034
Duncan Sandsf5dda012010-11-03 11:35:31 +00001035 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001036 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +00001037 return false;
1038
Juergen Ributzka349777d2014-06-12 23:27:57 +00001039 const Value *Ptr = LI->getPointerOperand();
1040
Dan Gohman39d82f92008-09-10 20:11:02 +00001041 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001042 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00001043 return false;
Evan Chenga41ee292008-09-03 06:44:39 +00001044
Evan Chengf5bc7e52008-09-05 21:00:03 +00001045 unsigned ResultReg = 0;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001046 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg))
1047 return false;
1048
1049 UpdateValueMap(I, ResultReg);
1050 return true;
Evan Chenga41ee292008-09-03 06:44:39 +00001051}
1052
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001053static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001054 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +00001055 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
1056 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001057
Owen Anderson9f944592009-08-11 20:47:22 +00001058 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +00001059 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001060 case MVT::i8: return X86::CMP8rr;
1061 case MVT::i16: return X86::CMP16rr;
1062 case MVT::i32: return X86::CMP32rr;
1063 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001064 case MVT::f32:
1065 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
1066 case MVT::f64:
1067 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001068 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001069}
1070
Chris Lattner88f47542008-10-15 04:13:29 +00001071/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
1072/// of the comparison, return an opcode that works for the compare (e.g.
1073/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001074static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +00001075 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +00001076 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +00001077 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001078 case MVT::i8: return X86::CMP8ri;
1079 case MVT::i16: return X86::CMP16ri;
1080 case MVT::i32: return X86::CMP32ri;
1081 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +00001082 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1083 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +00001084 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +00001085 return X86::CMP64ri32;
1086 return 0;
1087 }
Chris Lattner88f47542008-10-15 04:13:29 +00001088}
1089
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001090bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
1091 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +00001092 unsigned Op0Reg = getRegForValue(Op0);
1093 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001094
Chris Lattnere388725a2008-10-15 05:18:04 +00001095 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +00001096 if (isa<ConstantPointerNull>(Op1))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001097 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +00001098
Chris Lattnerd46b9512008-10-15 04:26:38 +00001099 // We have two options: compare with register or immediate. If the RHS of
1100 // the compare is an immediate that we can fold into this compare, use
1101 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001102 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +00001103 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareImmOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001105 .addReg(Op0Reg)
1106 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +00001107 return true;
1108 }
1109 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001110
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001111 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +00001112 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001113
Chris Lattnerd46b9512008-10-15 04:26:38 +00001114 unsigned Op1Reg = getRegForValue(Op1);
1115 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001117 .addReg(Op0Reg)
1118 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001119
Chris Lattnerd46b9512008-10-15 04:26:38 +00001120 return true;
1121}
1122
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001123bool X86FastISel::X86SelectCmp(const Instruction *I) {
1124 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001125
Duncan Sandsf5dda012010-11-03 11:35:31 +00001126 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001127 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +00001128 return false;
1129
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001130 // Try to optimize or fold the cmp.
1131 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1132 unsigned ResultReg = 0;
1133 switch (Predicate) {
1134 default: break;
1135 case CmpInst::FCMP_FALSE: {
1136 ResultReg = createResultReg(&X86::GR32RegClass);
1137 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0),
1138 ResultReg);
1139 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1140 X86::sub_8bit);
1141 if (!ResultReg)
1142 return false;
1143 break;
1144 }
1145 case CmpInst::FCMP_TRUE: {
1146 ResultReg = createResultReg(&X86::GR8RegClass);
1147 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
1148 ResultReg).addImm(1);
1149 break;
1150 }
1151 }
1152
1153 if (ResultReg) {
1154 UpdateValueMap(I, ResultReg);
1155 return true;
1156 }
1157
1158 const Value *LHS = CI->getOperand(0);
1159 const Value *RHS = CI->getOperand(1);
1160
1161 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1162 // We don't have to materialize a zero constant for this case and can just use
1163 // %x again on the RHS.
1164 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1165 const auto *RHSC = dyn_cast<ConstantFP>(RHS);
1166 if (RHSC && RHSC->isNullValue())
1167 RHS = LHS;
1168 }
1169
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001170 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Juergen Ributzkae3570562014-06-17 14:47:45 +00001171 static unsigned SETFOpcTable[2][3] = {
1172 { X86::SETEr, X86::SETNPr, X86::AND8rr },
1173 { X86::SETNEr, X86::SETPr, X86::OR8rr }
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001174 };
1175 unsigned *SETFOpc = nullptr;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001176 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001177 default: break;
1178 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1179 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1180 }
1181
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001182 ResultReg = createResultReg(&X86::GR8RegClass);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001183 if (SETFOpc) {
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001184 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001185 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001186
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001187 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1188 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1189 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1190 FlagReg1);
1191 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1192 FlagReg2);
Juergen Ributzkae3570562014-06-17 14:47:45 +00001193 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001194 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
Chris Lattnera3596db2008-10-15 03:47:17 +00001195 UpdateValueMap(I, ResultReg);
1196 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001197 }
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001198
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001199 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001200 bool SwapArgs;
1201 std::tie(CC, SwapArgs) = getX86ConditonCode(Predicate);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001202 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1203 unsigned Opc = X86::getSETFromCond(CC);
Wesley Peck527da1b2010-11-23 03:31:01 +00001204
Chris Lattnerf32ce222008-10-15 03:52:54 +00001205 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001206 std::swap(LHS, RHS);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001207
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001208 // Emit a compare of LHS/RHS.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001209 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001210 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001211
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001212 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001213 UpdateValueMap(I, ResultReg);
1214 return true;
1215}
Evan Chenga41ee292008-09-03 06:44:39 +00001216
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001217bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001218 EVT DstVT = TLI.getValueType(I->getType());
1219 if (!TLI.isTypeLegal(DstVT))
1220 return false;
1221
1222 unsigned ResultReg = getRegForValue(I->getOperand(0));
1223 if (ResultReg == 0)
1224 return false;
1225
Tim Northover04eb4232013-05-30 10:43:18 +00001226 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001227 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001228 if (SrcVT.SimpleTy == MVT::i1) {
1229 // Set the high bits to zero.
1230 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1231 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001232
Tim Northover04eb4232013-05-30 10:43:18 +00001233 if (ResultReg == 0)
1234 return false;
1235 }
1236
1237 if (DstVT == MVT::i64) {
1238 // Handle extension to 64-bits via sub-register shenanigans.
1239 unsigned MovInst;
1240
1241 switch (SrcVT.SimpleTy) {
1242 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1243 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1244 case MVT::i32: MovInst = X86::MOV32rr; break;
1245 default: llvm_unreachable("Unexpected zext to i64 source type");
1246 }
1247
1248 unsigned Result32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001249 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
Tim Northover04eb4232013-05-30 10:43:18 +00001250 .addReg(ResultReg);
1251
1252 ResultReg = createResultReg(&X86::GR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
Tim Northover04eb4232013-05-30 10:43:18 +00001254 ResultReg)
1255 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1256 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001257 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1258 ResultReg, /*Kill=*/true);
1259 if (ResultReg == 0)
1260 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001261 }
1262
Eli Friedmanc7035512011-05-25 23:49:02 +00001263 UpdateValueMap(I, ResultReg);
1264 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001265}
1266
Chris Lattnerd46b9512008-10-15 04:26:38 +00001267
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001268bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001269 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001270 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001271 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001272 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1273 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001274
Dan Gohman42ef6692010-08-21 02:32:36 +00001275 // Fold the common case of a conditional branch with a comparison
1276 // in the same block (values defined on other blocks may not have
1277 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001278 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001279 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001280 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001281
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001282 // Try to optimize or fold the cmp.
1283 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1284 switch (Predicate) {
1285 default: break;
1286 case CmpInst::FCMP_FALSE: FastEmitBranch(FalseMBB, DbgLoc); return true;
1287 case CmpInst::FCMP_TRUE: FastEmitBranch(TrueMBB, DbgLoc); return true;
1288 }
1289
1290 const Value *CmpLHS = CI->getOperand(0);
1291 const Value *CmpRHS = CI->getOperand(1);
1292
1293 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x,
1294 // 0.0.
1295 // We don't have to materialize a zero constant for this case and can just
1296 // use %x again on the RHS.
1297 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1298 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1299 if (CmpRHSC && CmpRHSC->isNullValue())
1300 CmpRHS = CmpLHS;
1301 }
1302
Dan Gohman1ab1d312008-10-02 22:15:21 +00001303 // Try to take advantage of fallthrough opportunities.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001304 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001305 std::swap(TrueMBB, FalseMBB);
1306 Predicate = CmpInst::getInversePredicate(Predicate);
1307 }
1308
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001309 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/conditon
1310 // code check. Instead two branch instructions are required to check all
1311 // the flags. First we change the predicate to a supported conditon code,
1312 // which will be the first branch. Later one we will emit the second
1313 // branch.
1314 bool NeedExtraBranch = false;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001315 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001316 default: break;
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001317 case CmpInst::FCMP_OEQ:
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001318 std::swap(TrueMBB, FalseMBB); // fall-through
1319 case CmpInst::FCMP_UNE:
1320 NeedExtraBranch = true;
1321 Predicate = CmpInst::FCMP_ONE;
1322 break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001323 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001324
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001325 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001326 bool SwapArgs;
1327 unsigned BranchOpc;
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001328 std::tie(CC, SwapArgs) = getX86ConditonCode(Predicate);
1329 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1330
1331 BranchOpc = X86::GetCondBranchFromCond(CC);
Chris Lattner47bef252008-10-15 04:02:26 +00001332 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001333 std::swap(CmpLHS, CmpRHS);
Chris Lattner47bef252008-10-15 04:02:26 +00001334
Chris Lattnerd46b9512008-10-15 04:26:38 +00001335 // Emit a compare of the LHS and RHS, setting the flags.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001336 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT))
Chris Lattnerd46b9512008-10-15 04:26:38 +00001337 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001338
Rafael Espindolaea09c592014-02-18 22:05:46 +00001339 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001340 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001341
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001342 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1343 // to UNE above).
1344 if (NeedExtraBranch) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001345 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001346 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001347 }
1348
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001349 // Obtain the branch weight and add the TrueBB to the successor list.
Juergen Ributzka454d3742014-06-13 00:45:11 +00001350 uint32_t BranchWeight = 0;
1351 if (FuncInfo.BPI)
1352 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1353 TrueMBB->getBasicBlock());
1354 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001355
1356 // Emits an unconditional branch to the FalseBB, obtains the branch
Alp Toker1d099d92014-06-19 19:41:26 +00001357 // weight, and adds it to the successor list.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001358 FastEmitBranch(FalseMBB, DbgLoc);
1359
Dan Gohman1ab1d312008-10-02 22:15:21 +00001360 return true;
1361 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001362 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1363 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1364 // typically happen for _Bool and C++ bools.
1365 MVT SourceVT;
1366 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1367 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1368 unsigned TestOpc = 0;
1369 switch (SourceVT.SimpleTy) {
1370 default: break;
1371 case MVT::i8: TestOpc = X86::TEST8ri; break;
1372 case MVT::i16: TestOpc = X86::TEST16ri; break;
1373 case MVT::i32: TestOpc = X86::TEST32ri; break;
1374 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1375 }
1376 if (TestOpc) {
1377 unsigned OpReg = getRegForValue(TI->getOperand(0));
1378 if (OpReg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001379 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001380 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001381
Chris Lattnerc59290a2011-04-19 04:26:32 +00001382 unsigned JmpOpc = X86::JNE_4;
1383 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1384 std::swap(TrueMBB, FalseMBB);
1385 JmpOpc = X86::JE_4;
1386 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001387
Rafael Espindolaea09c592014-02-18 22:05:46 +00001388 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001389 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001390 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001391 uint32_t BranchWeight = 0;
1392 if (FuncInfo.BPI)
1393 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1394 TrueMBB->getBasicBlock());
1395 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001396 return true;
1397 }
1398 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001399 }
1400
1401 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001402 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1403 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001404 unsigned OpReg = getRegForValue(BI->getCondition());
1405 if (OpReg == 0) return false;
1406
Rafael Espindolaea09c592014-02-18 22:05:46 +00001407 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Eli Friedman0eea0292011-04-27 01:34:27 +00001408 .addReg(OpReg).addImm(1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001409 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001410 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001411 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001412 uint32_t BranchWeight = 0;
1413 if (FuncInfo.BPI)
1414 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1415 TrueMBB->getBasicBlock());
1416 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Dan Gohmana5753b32008-09-05 01:06:14 +00001417 return true;
1418}
1419
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001420bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001421 unsigned CReg = 0, OpReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001422 const TargetRegisterClass *RC = nullptr;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001423 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001424 CReg = X86::CL;
1425 RC = &X86::GR8RegClass;
1426 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001427 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1428 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1429 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001430 default: return false;
1431 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001432 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001433 CReg = X86::CX;
1434 RC = &X86::GR16RegClass;
1435 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001436 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1437 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1438 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001439 default: return false;
1440 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001441 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001442 CReg = X86::ECX;
1443 RC = &X86::GR32RegClass;
1444 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001445 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1446 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1447 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001448 default: return false;
1449 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001450 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001451 CReg = X86::RCX;
1452 RC = &X86::GR64RegClass;
1453 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001454 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1455 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1456 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001457 default: return false;
1458 }
1459 } else {
1460 return false;
1461 }
1462
Duncan Sandsf5dda012010-11-03 11:35:31 +00001463 MVT VT;
1464 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001465 return false;
1466
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001467 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1468 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001469
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001470 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1471 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001472 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001473 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001474
1475 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001476 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001477 if (CReg != X86::CL)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001478 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001479 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001480 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001481
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001482 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001483 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001484 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001485 UpdateValueMap(I, ResultReg);
1486 return true;
1487}
1488
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001489bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1490 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1491 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1492 const static bool S = true; // IsSigned
1493 const static bool U = false; // !IsSigned
1494 const static unsigned Copy = TargetOpcode::COPY;
1495 // For the X86 DIV/IDIV instruction, in most cases the dividend
1496 // (numerator) must be in a specific register pair highreg:lowreg,
1497 // producing the quotient in lowreg and the remainder in highreg.
1498 // For most data types, to set up the instruction, the dividend is
1499 // copied into lowreg, and lowreg is sign-extended or zero-extended
1500 // into highreg. The exception is i8, where the dividend is defined
1501 // as a single register rather than a register pair, and we
1502 // therefore directly sign-extend or zero-extend the dividend into
1503 // lowreg, instead of copying, and ignore the highreg.
1504 const static struct DivRemEntry {
1505 // The following portion depends only on the data type.
1506 const TargetRegisterClass *RC;
1507 unsigned LowInReg; // low part of the register pair
1508 unsigned HighInReg; // high part of the register pair
1509 // The following portion depends on both the data type and the operation.
1510 struct DivRemResult {
1511 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1512 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1513 // highreg, or copying a zero into highreg.
1514 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1515 // zero/sign-extending into lowreg for i8.
1516 unsigned DivRemResultReg; // Register containing the desired result.
1517 bool IsOpSigned; // Whether to use signed or unsigned form.
1518 } ResultTable[NumOps];
1519 } OpTable[NumTypes] = {
1520 { &X86::GR8RegClass, X86::AX, 0, {
1521 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1522 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1523 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1524 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1525 }
1526 }, // i8
1527 { &X86::GR16RegClass, X86::AX, X86::DX, {
1528 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1529 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001530 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1531 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001532 }
1533 }, // i16
1534 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1535 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1536 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1537 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1538 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1539 }
1540 }, // i32
1541 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1542 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1543 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001544 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1545 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001546 }
1547 }, // i64
1548 };
1549
1550 MVT VT;
1551 if (!isTypeLegal(I->getType(), VT))
1552 return false;
1553
1554 unsigned TypeIndex, OpIndex;
1555 switch (VT.SimpleTy) {
1556 default: return false;
1557 case MVT::i8: TypeIndex = 0; break;
1558 case MVT::i16: TypeIndex = 1; break;
1559 case MVT::i32: TypeIndex = 2; break;
1560 case MVT::i64: TypeIndex = 3;
1561 if (!Subtarget->is64Bit())
1562 return false;
1563 break;
1564 }
1565
1566 switch (I->getOpcode()) {
1567 default: llvm_unreachable("Unexpected div/rem opcode");
1568 case Instruction::SDiv: OpIndex = 0; break;
1569 case Instruction::SRem: OpIndex = 1; break;
1570 case Instruction::UDiv: OpIndex = 2; break;
1571 case Instruction::URem: OpIndex = 3; break;
1572 }
1573
1574 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1575 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1576 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1577 if (Op0Reg == 0)
1578 return false;
1579 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1580 if (Op1Reg == 0)
1581 return false;
1582
1583 // Move op0 into low-order input register.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001584 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001585 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1586 // Zero-extend or sign-extend into high-order input register.
1587 if (OpEntry.OpSignExtend) {
1588 if (OpEntry.IsOpSigned)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001589 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001590 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001591 else {
1592 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001593 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001594 TII.get(X86::MOV32r0), Zero32);
1595
1596 // Copy the zero into the appropriate sub/super/identical physical
1597 // register. Unfortunately the operations needed are not uniform enough to
1598 // fit neatly into the table above.
1599 if (VT.SimpleTy == MVT::i16) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001600 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001601 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001602 .addReg(Zero32, 0, X86::sub_16bit);
1603 } else if (VT.SimpleTy == MVT::i32) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001604 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001605 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001606 .addReg(Zero32);
1607 } else if (VT.SimpleTy == MVT::i64) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001608 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001609 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1610 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1611 }
1612 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001613 }
1614 // Generate the DIV/IDIV instruction.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001615 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001616 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001617 // For i8 remainder, we can't reference AH directly, as we'll end
1618 // up with bogus copies like %R9B = COPY %AH. Reference AX
1619 // instead to prevent AH references in a REX instruction.
1620 //
1621 // The current assumption of the fast register allocator is that isel
1622 // won't generate explicit references to the GPR8_NOREX registers. If
1623 // the allocator and/or the backend get enhanced to be more robust in
1624 // that regard, this can be, and should be, removed.
1625 unsigned ResultReg = 0;
1626 if ((I->getOpcode() == Instruction::SRem ||
1627 I->getOpcode() == Instruction::URem) &&
1628 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1629 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1630 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001631 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbachc35388f2013-07-09 02:07:25 +00001632 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1633
1634 // Shift AX right by 8 bits instead of using AH.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001635 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
Jim Grosbachc35388f2013-07-09 02:07:25 +00001636 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1637
1638 // Now reference the 8-bit subreg of the result.
1639 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1640 /*Kill=*/true, X86::sub_8bit);
1641 }
1642 // Copy the result out of the physreg if we haven't already.
1643 if (!ResultReg) {
1644 ResultReg = createResultReg(TypeEntry.RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001645 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
Jim Grosbachc35388f2013-07-09 02:07:25 +00001646 .addReg(OpEntry.DivRemResultReg);
1647 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001648 UpdateValueMap(I, ResultReg);
1649
1650 return true;
1651}
1652
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001653/// \brief Emit a conditional move instruction (if the are supported) to lower
1654/// the select.
1655bool X86FastISel::X86FastEmitCMoveSelect(const Instruction *I) {
1656 MVT RetVT;
1657 if (!isTypeLegal(I->getType(), RetVT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001658 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001659
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001660 // Check if the subtarget supports these instructions.
1661 if (!Subtarget->hasCMov())
Wesley Peck527da1b2010-11-23 03:31:01 +00001662 return false;
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001663
1664 // FIXME: Add support for i8.
1665 unsigned Opc;
1666 switch (RetVT.SimpleTy) {
1667 default: return false;
1668 case MVT::i16: Opc = X86::CMOVNE16rr; break;
1669 case MVT::i32: Opc = X86::CMOVNE32rr; break;
1670 case MVT::i64: Opc = X86::CMOVNE64rr; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001671 }
1672
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001673 const Value *Cond = I->getOperand(0);
1674 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1675 bool NeedTest = true;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001676
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001677 // Optimize conditons coming from a compare.
1678 if (const auto *CI = dyn_cast<CmpInst>(Cond)) {
1679 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1680
1681 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
1682 static unsigned SETFOpcTable[2][3] = {
1683 { X86::SETNPr, X86::SETEr , X86::TEST8rr },
1684 { X86::SETPr, X86::SETNEr, X86::OR8rr }
1685 };
1686 unsigned *SETFOpc = nullptr;
1687 switch (Predicate) {
1688 default: break;
1689 case CmpInst::FCMP_OEQ:
1690 SETFOpc = &SETFOpcTable[0][0];
1691 Predicate = CmpInst::ICMP_NE;
1692 break;
1693 case CmpInst::FCMP_UNE:
1694 SETFOpc = &SETFOpcTable[1][0];
1695 Predicate = CmpInst::ICMP_NE;
1696 break;
1697 }
1698
1699 X86::CondCode CC;
1700 bool NeedSwap;
1701 std::tie(CC, NeedSwap) = getX86ConditonCode(Predicate);
1702 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
1703 Opc = X86::getCMovFromCond(CC, RC->getSize());
1704
1705 const Value *CmpLHS = CI->getOperand(0);
1706 const Value *CmpRHS = CI->getOperand(1);
1707 if (NeedSwap)
1708 std::swap(CmpLHS, CmpRHS);
1709
1710 EVT CmpVT = TLI.getValueType(CmpLHS->getType());
1711 // Emit a compare of the LHS and RHS, setting the flags.
1712 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT))
1713 return false;
1714
1715 if (SETFOpc) {
1716 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1717 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1718 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1719 FlagReg1);
1720 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1721 FlagReg2);
1722 auto const &II = TII.get(SETFOpc[2]);
1723 if (II.getNumDefs()) {
1724 unsigned TmpReg = createResultReg(&X86::GR8RegClass);
1725 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, TmpReg)
1726 .addReg(FlagReg2).addReg(FlagReg1);
1727 } else {
1728 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
1729 .addReg(FlagReg2).addReg(FlagReg1);
1730 }
1731 }
1732 NeedTest = false;
1733 }
1734
1735 if (NeedTest) {
1736 // Selects operate on i1, however, CondReg is 8 bits width and may contain
1737 // garbage. Indeed, only the less significant bit is supposed to be
1738 // accurate. If we read more than the lsb, we may see non-zero values
1739 // whereas lsb is zero. Therefore, we have to truncate Op0Reg to i1 for
1740 // the select. This is achieved by performing TEST against 1.
1741 unsigned CondReg = getRegForValue(Cond);
1742 if (CondReg == 0)
1743 return false;
1744 bool CondIsKill = hasTrivialKill(Cond);
1745
1746 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1747 .addReg(CondReg, getKillRegState(CondIsKill)).addImm(1);
1748 }
1749
1750 const Value *LHS = I->getOperand(1);
1751 const Value *RHS = I->getOperand(2);
1752
1753 unsigned RHSReg = getRegForValue(RHS);
1754 bool RHSIsKill = hasTrivialKill(RHS);
1755
1756 unsigned LHSReg = getRegForValue(LHS);
1757 bool LHSIsKill = hasTrivialKill(LHS);
1758
1759 if (!LHSReg || !RHSReg)
1760 return false;
1761
1762 unsigned ResultReg = FastEmitInst_rr(Opc, RC, RHSReg, RHSIsKill,
1763 LHSReg, LHSIsKill);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001764 UpdateValueMap(I, ResultReg);
1765 return true;
1766}
1767
Juergen Ributzka21d56082014-06-23 21:55:40 +00001768/// \brief Emit SSE instructions to lower the select.
1769///
1770/// Try to use SSE1/SSE2 instructions to simulate a select without branches.
1771/// This lowers fp selects into a CMP/AND/ANDN/OR sequence when the necessary
1772/// SSE instructions are available.
1773bool X86FastISel::X86FastEmitSSESelect(const Instruction *I) {
1774 MVT RetVT;
1775 if (!isTypeLegal(I->getType(), RetVT))
1776 return false;
1777
1778 const auto *CI = dyn_cast<FCmpInst>(I->getOperand(0));
1779 if (!CI)
1780 return false;
1781
1782 if (I->getType() != CI->getOperand(0)->getType() ||
1783 !((Subtarget->hasSSE1() && RetVT == MVT::f32) ||
1784 (Subtarget->hasSSE2() && RetVT == MVT::f64) ))
1785 return false;
1786
1787 const Value *CmpLHS = CI->getOperand(0);
1788 const Value *CmpRHS = CI->getOperand(1);
1789 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1790
1791 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1792 // We don't have to materialize a zero constant for this case and can just use
1793 // %x again on the RHS.
1794 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1795 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1796 if (CmpRHSC && CmpRHSC->isNullValue())
1797 CmpRHS = CmpLHS;
1798 }
1799
1800 unsigned CC;
1801 bool NeedSwap;
1802 std::tie(CC, NeedSwap) = getX86SSECondtionCode(Predicate);
1803 if (CC > 7)
1804 return false;
1805
1806 if (NeedSwap)
1807 std::swap(CmpLHS, CmpRHS);
1808
1809 static unsigned OpcTable[2][2][4] = {
1810 { { X86::CMPSSrr, X86::FsANDPSrr, X86::FsANDNPSrr, X86::FsORPSrr },
1811 { X86::VCMPSSrr, X86::VFsANDPSrr, X86::VFsANDNPSrr, X86::VFsORPSrr } },
1812 { { X86::CMPSDrr, X86::FsANDPDrr, X86::FsANDNPDrr, X86::FsORPDrr },
1813 { X86::VCMPSDrr, X86::VFsANDPDrr, X86::VFsANDNPDrr, X86::VFsORPDrr } }
1814 };
1815
1816 bool HasAVX = Subtarget->hasAVX();
1817 unsigned *Opc = nullptr;
1818 switch (RetVT.SimpleTy) {
1819 default: return false;
1820 case MVT::f32: Opc = &OpcTable[0][HasAVX][0]; break;
1821 case MVT::f64: Opc = &OpcTable[1][HasAVX][0]; break;
1822 }
1823
1824 const Value *LHS = I->getOperand(1);
1825 const Value *RHS = I->getOperand(2);
1826
1827 unsigned LHSReg = getRegForValue(LHS);
1828 bool LHSIsKill = hasTrivialKill(LHS);
1829
1830 unsigned RHSReg = getRegForValue(RHS);
1831 bool RHSIsKill = hasTrivialKill(RHS);
1832
1833 unsigned CmpLHSReg = getRegForValue(CmpLHS);
1834 bool CmpLHSIsKill = hasTrivialKill(CmpLHS);
1835
1836 unsigned CmpRHSReg = getRegForValue(CmpRHS);
1837 bool CmpRHSIsKill = hasTrivialKill(CmpRHS);
1838
1839 if (!LHSReg || !RHSReg || !CmpLHS || !CmpRHS)
1840 return false;
1841
1842 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1843 unsigned CmpReg = FastEmitInst_rri(Opc[0], RC, CmpLHSReg, CmpLHSIsKill,
1844 CmpRHSReg, CmpRHSIsKill, CC);
1845 unsigned AndReg = FastEmitInst_rr(Opc[1], RC, CmpReg, /*IsKill=*/false,
1846 LHSReg, LHSIsKill);
1847 unsigned AndNReg = FastEmitInst_rr(Opc[2], RC, CmpReg, /*IsKill=*/true,
1848 RHSReg, RHSIsKill);
1849 unsigned ResultReg = FastEmitInst_rr(Opc[3], RC, AndNReg, /*IsKill=*/true,
1850 AndReg, /*IsKill=*/true);
1851 UpdateValueMap(I, ResultReg);
1852 return true;
1853}
1854
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001855bool X86FastISel::X86SelectSelect(const Instruction *I) {
1856 MVT RetVT;
1857 if (!isTypeLegal(I->getType(), RetVT))
1858 return false;
1859
1860 // Check if we can fold the select.
1861 if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) {
1862 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1863 const Value *Opnd = nullptr;
1864 switch (Predicate) {
1865 default: break;
1866 case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break;
1867 case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break;
1868 }
1869 // No need for a select anymore - this is an unconditional move.
1870 if (Opnd) {
1871 unsigned OpReg = getRegForValue(Opnd);
1872 if (OpReg == 0)
1873 return false;
1874 bool OpIsKill = hasTrivialKill(Opnd);
1875 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1876 unsigned ResultReg = createResultReg(RC);
1877 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1878 TII.get(TargetOpcode::COPY), ResultReg)
1879 .addReg(OpReg, getKillRegState(OpIsKill));
1880 UpdateValueMap(I, ResultReg);
1881 return true;
1882 }
1883 }
1884
1885 // First try to use real conditional move instructions.
1886 if (X86FastEmitCMoveSelect(I))
1887 return true;
1888
Juergen Ributzka21d56082014-06-23 21:55:40 +00001889 // Try to use a sequence of SSE instructions to simulate a conditonal move.
1890 if (X86FastEmitSSESelect(I))
1891 return true;
1892
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001893 return false;
1894}
1895
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001896bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001897 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001898 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001899 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001900 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001901 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001902 unsigned OpReg = getRegForValue(V);
1903 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001904 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001905 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001906 TII.get(X86::CVTSS2SDrr), ResultReg)
1907 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001908 UpdateValueMap(I, ResultReg);
1909 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001910 }
1911 }
1912
1913 return false;
1914}
1915
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001916bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001917 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001918 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001919 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001920 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001921 unsigned OpReg = getRegForValue(V);
1922 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001923 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001924 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001925 TII.get(X86::CVTSD2SSrr), ResultReg)
1926 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001927 UpdateValueMap(I, ResultReg);
1928 return true;
1929 }
1930 }
1931 }
1932
1933 return false;
1934}
1935
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001936bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001937 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1938 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001939
Eli Friedmanc7035512011-05-25 23:49:02 +00001940 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001941 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001942 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001943 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001944 return false;
1945
1946 unsigned InputReg = getRegForValue(I->getOperand(0));
1947 if (!InputReg)
1948 // Unhandled operand. Halt "fast" selection and bail.
1949 return false;
1950
Eli Friedmanc7035512011-05-25 23:49:02 +00001951 if (SrcVT == MVT::i8) {
1952 // Truncate from i8 to i1; no code needed.
1953 UpdateValueMap(I, InputReg);
1954 return true;
1955 }
Evan Chengb9286692008-09-07 08:47:42 +00001956
Eli Friedmanc7035512011-05-25 23:49:02 +00001957 if (!Subtarget->is64Bit()) {
1958 // If we're on x86-32; we can't extract an i8 from a general register.
1959 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001960 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1961 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1962 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001963 unsigned CopyReg = createResultReg(CopyRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001964 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Eli Friedmanc7035512011-05-25 23:49:02 +00001965 CopyReg).addReg(InputReg);
1966 InputReg = CopyReg;
1967 }
1968
1969 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001970 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001971 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001972 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001973 if (!ResultReg)
1974 return false;
1975
1976 UpdateValueMap(I, ResultReg);
1977 return true;
1978}
1979
Eli Friedman60afcc22011-05-20 22:21:04 +00001980bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1981 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1982}
1983
Eli Friedmanbcc69142011-04-27 01:45:07 +00001984bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1985 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001986
Eli Friedmanbcc69142011-04-27 01:45:07 +00001987 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001988 if (!IsMemcpySmall(Len))
1989 return false;
1990
1991 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001992
1993 // We don't care about alignment here since we just emit integer accesses.
1994 while (Len) {
1995 MVT VT;
1996 if (Len >= 8 && i64Legal)
1997 VT = MVT::i64;
1998 else if (Len >= 4)
1999 VT = MVT::i32;
2000 else if (Len >= 2)
2001 VT = MVT::i16;
2002 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00002003 VT = MVT::i8;
2004 }
2005
2006 unsigned Reg;
Juergen Ributzka349777d2014-06-12 23:27:57 +00002007 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
2008 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
Eli Friedmanbcc69142011-04-27 01:45:07 +00002009 assert(RV && "Failed to emit load or store??");
2010
2011 unsigned Size = VT.getSizeInBits()/8;
2012 Len -= Size;
2013 DestAM.Disp += Size;
2014 SrcAM.Disp += Size;
2015 }
2016
2017 return true;
2018}
2019
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002020static bool isCommutativeIntrinsic(IntrinsicInst const &I) {
2021 switch (I.getIntrinsicID()) {
2022 case Intrinsic::sadd_with_overflow:
2023 case Intrinsic::uadd_with_overflow:
2024 case Intrinsic::smul_with_overflow:
2025 case Intrinsic::umul_with_overflow:
2026 return true;
2027 default:
2028 return false;
2029 }
2030}
2031
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002032bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00002033 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00002034 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00002035 default: return false;
Juergen Ributzka4dc95872014-06-11 21:44:44 +00002036 case Intrinsic::frameaddress: {
2037 Type *RetTy = I.getCalledFunction()->getReturnType();
2038
2039 MVT VT;
2040 if (!isTypeLegal(RetTy, VT))
2041 return false;
2042
2043 unsigned Opc;
2044 const TargetRegisterClass *RC = nullptr;
2045
2046 switch (VT.SimpleTy) {
2047 default: llvm_unreachable("Invalid result type for frameaddress.");
2048 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
2049 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
2050 }
2051
2052 // This needs to be set before we call getFrameRegister, otherwise we get
2053 // the wrong frame register.
2054 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2055 MFI->setFrameAddressIsTaken(true);
2056
2057 const X86RegisterInfo *RegInfo =
2058 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
2059 unsigned FrameReg = RegInfo->getFrameRegister(*(FuncInfo.MF));
2060 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
2061 (FrameReg == X86::EBP && VT == MVT::i32)) &&
2062 "Invalid Frame Register!");
2063
2064 // Always make a copy of the frame register to to a vreg first, so that we
2065 // never directly reference the frame register (the TwoAddressInstruction-
2066 // Pass doesn't like that).
2067 unsigned SrcReg = createResultReg(RC);
2068 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2069 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
2070
2071 // Now recursively load from the frame address.
2072 // movq (%rbp), %rax
2073 // movq (%rax), %rax
2074 // movq (%rax), %rax
2075 // ...
2076 unsigned DestReg;
2077 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2078 while (Depth--) {
2079 DestReg = createResultReg(RC);
2080 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2081 TII.get(Opc), DestReg), SrcReg);
2082 SrcReg = DestReg;
2083 }
2084
2085 UpdateValueMap(&I, SrcReg);
2086 return true;
2087 }
Chris Lattner91328b32011-04-19 05:52:03 +00002088 case Intrinsic::memcpy: {
2089 const MemCpyInst &MCI = cast<MemCpyInst>(I);
2090 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00002091 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00002092 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00002093
Eli Friedmancd2124a2011-06-10 23:39:36 +00002094 if (isa<ConstantInt>(MCI.getLength())) {
2095 // Small memcpy's are common enough that we want to do them
2096 // without a call if possible.
2097 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
2098 if (IsMemcpySmall(Len)) {
2099 X86AddressMode DestAM, SrcAM;
2100 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
2101 !X86SelectAddress(MCI.getRawSource(), SrcAM))
2102 return false;
2103 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
2104 return true;
2105 }
2106 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002107
Eli Friedmancd2124a2011-06-10 23:39:36 +00002108 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2109 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00002110 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00002111
Eli Friedmancd2124a2011-06-10 23:39:36 +00002112 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
2113 return false;
2114
2115 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00002116 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00002117 case Intrinsic::memset: {
2118 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00002119
Nick Lewyckya530a4d2011-08-02 00:40:16 +00002120 if (MSI.isVolatile())
2121 return false;
2122
Eli Friedmancd2124a2011-06-10 23:39:36 +00002123 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
2124 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
2125 return false;
2126
2127 if (MSI.getDestAddressSpace() > 255)
2128 return false;
2129
2130 return DoSelectCall(&I, "memset");
2131 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002132 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00002133 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002134 EVT PtrTy = TLI.getPointerTy();
2135
Gabor Greif83205af2010-06-26 11:51:52 +00002136 const Value *Op1 = I.getArgOperand(0); // The guard's value.
2137 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002138
Josh Magee22b8ba22013-12-19 03:17:11 +00002139 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
2140
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002141 // Grab the frame index.
2142 X86AddressMode AM;
2143 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00002144 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002145 return true;
2146 }
Dale Johannesend5575f22010-01-26 00:09:58 +00002147 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002148 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00002149 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00002150 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00002151 if (!X86SelectAddress(DI->getAddress(), AM))
2152 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00002153 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00002154 // FIXME may need to add RegState::Debug to any registers produced,
2155 // although ESP/EBP should be the only ones at the moment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002156 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002157 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00002158 return true;
2159 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002160 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002161 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002162 return true;
2163 }
Juergen Ributzka272b5702014-06-11 23:11:02 +00002164 case Intrinsic::sqrt: {
2165 if (!Subtarget->hasSSE1())
2166 return false;
2167
2168 Type *RetTy = I.getCalledFunction()->getReturnType();
2169
2170 MVT VT;
2171 if (!isTypeLegal(RetTy, VT))
2172 return false;
2173
2174 // Unfortunatelly we can't use FastEmit_r, because the AVX version of FSQRT
2175 // is not generated by FastISel yet.
2176 // FIXME: Update this code once tablegen can handle it.
2177 static const unsigned SqrtOpc[2][2] = {
2178 {X86::SQRTSSr, X86::VSQRTSSr},
2179 {X86::SQRTSDr, X86::VSQRTSDr}
2180 };
2181 bool HasAVX = Subtarget->hasAVX();
2182 unsigned Opc;
2183 const TargetRegisterClass *RC;
2184 switch (VT.SimpleTy) {
2185 default: return false;
2186 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
2187 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
2188 }
2189
2190 const Value *SrcVal = I.getArgOperand(0);
2191 unsigned SrcReg = getRegForValue(SrcVal);
2192
2193 if (SrcReg == 0)
2194 return false;
2195
2196 unsigned ImplicitDefReg = 0;
2197 if (HasAVX) {
2198 ImplicitDefReg = createResultReg(RC);
2199 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2200 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2201 }
2202
2203 unsigned ResultReg = createResultReg(RC);
2204 MachineInstrBuilder MIB;
2205 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
2206 ResultReg);
2207
2208 if (ImplicitDefReg)
2209 MIB.addReg(ImplicitDefReg);
2210
2211 MIB.addReg(SrcReg);
2212
2213 UpdateValueMap(&I, ResultReg);
2214 return true;
2215 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002216 case Intrinsic::sadd_with_overflow:
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002217 case Intrinsic::uadd_with_overflow:
2218 case Intrinsic::ssub_with_overflow:
2219 case Intrinsic::usub_with_overflow:
2220 case Intrinsic::smul_with_overflow:
2221 case Intrinsic::umul_with_overflow: {
2222 // This implements the basic lowering of the xalu with overflow intrinsics
2223 // into add/sub/mul folowed by either seto or setb.
Bill Wendling80b34b32008-12-09 02:42:50 +00002224 const Function *Callee = I.getCalledFunction();
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002225 auto *Ty = cast<StructType>(Callee->getReturnType());
2226 Type *RetTy = Ty->getTypeAtIndex(0U);
2227 Type *CondTy = Ty->getTypeAtIndex(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00002228
Duncan Sandsf5dda012010-11-03 11:35:31 +00002229 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00002230 if (!isTypeLegal(RetTy, VT))
2231 return false;
2232
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002233 if (VT < MVT::i8 || VT > MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00002234 return false;
2235
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002236 const Value *LHS = I.getArgOperand(0);
2237 const Value *RHS = I.getArgOperand(1);
2238
2239 // Canonicalize immediates to the RHS.
2240 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2241 isCommutativeIntrinsic(I))
2242 std::swap(LHS, RHS);
2243
2244 unsigned BaseOpc, CondOpc;
2245 switch (I.getIntrinsicID()) {
2246 default: llvm_unreachable("Unexpected intrinsic!");
2247 case Intrinsic::sadd_with_overflow:
2248 BaseOpc = ISD::ADD; CondOpc = X86::SETOr; break;
2249 case Intrinsic::uadd_with_overflow:
2250 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
2251 case Intrinsic::ssub_with_overflow:
2252 BaseOpc = ISD::SUB; CondOpc = X86::SETOr; break;
2253 case Intrinsic::usub_with_overflow:
2254 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
2255 case Intrinsic::smul_with_overflow:
2256 BaseOpc = ISD::MUL; CondOpc = X86::SETOr; break;
2257 case Intrinsic::umul_with_overflow:
2258 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
2259 }
2260
2261 unsigned LHSReg = getRegForValue(LHS);
2262 if (LHSReg == 0)
2263 return false;
2264 bool LHSIsKill = hasTrivialKill(LHS);
2265
2266 unsigned ResultReg = 0;
2267 // Check if we have an immediate version.
2268 if (auto const *C = dyn_cast<ConstantInt>(RHS)) {
2269 ResultReg = FastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2270 C->getZExtValue());
2271 }
2272
2273 unsigned RHSReg;
2274 bool RHSIsKill;
2275 if (!ResultReg) {
2276 RHSReg = getRegForValue(RHS);
2277 if (RHSReg == 0)
2278 return false;
2279 RHSIsKill = hasTrivialKill(RHS);
2280 ResultReg = FastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2281 RHSIsKill);
2282 }
2283
2284 // FastISel doesn't have a pattern for X86::MUL*r. Emit it manually.
2285 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
2286 static const unsigned MULOpc[] =
2287 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
2288 static const unsigned Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
2289 // First copy the first operand into RAX, which is an implicit input to
2290 // the X86::MUL*r instruction.
2291 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2292 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2293 .addReg(LHSReg, getKillRegState(LHSIsKill));
2294 ResultReg = FastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2295 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
2296 }
2297
2298 if (!ResultReg)
Bill Wendling80b34b32008-12-09 02:42:50 +00002299 return false;
2300
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002301 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
2302 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
2303 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
2304 ResultReg2);
Eli Friedmana4d4a012011-05-16 21:06:17 +00002305
2306 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00002307 return true;
2308 }
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002309 case Intrinsic::x86_sse_cvttss2si:
2310 case Intrinsic::x86_sse_cvttss2si64:
2311 case Intrinsic::x86_sse2_cvttsd2si:
2312 case Intrinsic::x86_sse2_cvttsd2si64: {
2313 bool IsInputDouble;
2314 switch (I.getIntrinsicID()) {
2315 default: llvm_unreachable("Unexpected intrinsic.");
2316 case Intrinsic::x86_sse_cvttss2si:
2317 case Intrinsic::x86_sse_cvttss2si64:
2318 if (!Subtarget->hasSSE1())
2319 return false;
2320 IsInputDouble = false;
2321 break;
2322 case Intrinsic::x86_sse2_cvttsd2si:
2323 case Intrinsic::x86_sse2_cvttsd2si64:
2324 if (!Subtarget->hasSSE2())
2325 return false;
2326 IsInputDouble = true;
2327 break;
2328 }
2329
2330 Type *RetTy = I.getCalledFunction()->getReturnType();
2331 MVT VT;
2332 if (!isTypeLegal(RetTy, VT))
2333 return false;
2334
2335 static const unsigned CvtOpc[2][2][2] = {
2336 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
2337 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
2338 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
2339 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
2340 };
2341 bool HasAVX = Subtarget->hasAVX();
2342 unsigned Opc;
2343 switch (VT.SimpleTy) {
2344 default: llvm_unreachable("Unexpected result type.");
2345 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
2346 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
2347 }
2348
2349 // Check if we can fold insertelement instructions into the convert.
2350 const Value *Op = I.getArgOperand(0);
2351 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
2352 const Value *Index = IE->getOperand(2);
2353 if (!isa<ConstantInt>(Index))
2354 break;
2355 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
2356
2357 if (Idx == 0) {
2358 Op = IE->getOperand(1);
2359 break;
2360 }
2361 Op = IE->getOperand(0);
2362 }
2363
2364 unsigned Reg = getRegForValue(Op);
2365 if (Reg == 0)
2366 return false;
2367
2368 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
2369 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2370 .addReg(Reg);
2371
2372 UpdateValueMap(&I, ResultReg);
2373 return true;
2374 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002375 }
2376}
2377
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002378bool X86FastISel::FastLowerArguments() {
2379 if (!FuncInfo.CanLowerReturn)
2380 return false;
2381
2382 const Function *F = FuncInfo.Fn;
2383 if (F->isVarArg())
2384 return false;
2385
2386 CallingConv::ID CC = F->getCallingConv();
2387 if (CC != CallingConv::C)
2388 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00002389
2390 if (Subtarget->isCallingConvWin64(CC))
2391 return false;
2392
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002393 if (!Subtarget->is64Bit())
2394 return false;
2395
2396 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002397 unsigned GPRCnt = 0;
2398 unsigned FPRCnt = 0;
2399 unsigned Idx = 0;
2400 for (auto const &Arg : F->args()) {
2401 // The first argument is at index 1.
2402 ++Idx;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002403 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2404 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2405 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2406 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2407 return false;
2408
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002409 Type *ArgTy = Arg.getType();
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002410 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
2411 return false;
2412
2413 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00002414 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002415 switch (ArgVT.getSimpleVT().SimpleTy) {
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002416 default: return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002417 case MVT::i32:
2418 case MVT::i64:
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002419 ++GPRCnt;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002420 break;
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002421 case MVT::f32:
2422 case MVT::f64:
2423 if (!Subtarget->hasSSE1())
2424 return false;
2425 ++FPRCnt;
2426 break;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002427 }
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002428
2429 if (GPRCnt > 6)
2430 return false;
2431
2432 if (FPRCnt > 8)
2433 return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002434 }
2435
Craig Topper840beec2014-04-04 05:16:06 +00002436 static const MCPhysReg GPR32ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002437 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
2438 };
Craig Topper840beec2014-04-04 05:16:06 +00002439 static const MCPhysReg GPR64ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002440 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
2441 };
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002442 static const MCPhysReg XMMArgRegs[] = {
2443 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2444 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2445 };
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002446
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002447 unsigned GPRIdx = 0;
2448 unsigned FPRIdx = 0;
2449 for (auto const &Arg : F->args()) {
2450 MVT VT = TLI.getSimpleValueType(Arg.getType());
2451 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
2452 unsigned SrcReg;
2453 switch (VT.SimpleTy) {
2454 default: llvm_unreachable("Unexpected value type.");
2455 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
2456 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
2457 case MVT::f32: // fall-through
2458 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
2459 }
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002460 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2461 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2462 // Without this, EmitLiveInCopies may eliminate the livein if its only
2463 // use is a bitcast (which isn't turned into an instruction).
2464 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002465 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002466 TII.get(TargetOpcode::COPY), ResultReg)
2467 .addReg(DstReg, getKillRegState(true));
2468 UpdateValueMap(&Arg, ResultReg);
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002469 }
2470 return true;
2471}
2472
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002473bool X86FastISel::X86SelectCall(const Instruction *I) {
2474 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00002475 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002476
2477 // Can't handle inline asm yet.
2478 if (isa<InlineAsm>(Callee))
2479 return false;
2480
Bill Wendling80b34b32008-12-09 02:42:50 +00002481 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002482 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00002483 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002484
Chad Rosierdf42cf32012-12-11 00:18:02 +00002485 // Allow SelectionDAG isel to handle tail calls.
2486 if (cast<CallInst>(I)->isTailCall())
2487 return false;
2488
Craig Topper062a2ba2014-04-25 05:30:21 +00002489 return DoSelectCall(I, nullptr);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002490}
2491
Rafael Espindola73173c52012-07-25 15:42:45 +00002492static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
2493 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002494 if (Subtarget.is64Bit())
2495 return 0;
Rafael Espindola32cb5ac2013-12-12 16:06:58 +00002496 if (Subtarget.getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002497 return 0;
2498 CallingConv::ID CC = CS.getCallingConv();
2499 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
2500 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002501 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002502 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002503 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00002504 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002505 return 4;
2506}
2507
Eli Friedmancd2124a2011-06-10 23:39:36 +00002508// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
2509bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
2510 const CallInst *CI = cast<CallInst>(I);
2511 const Value *Callee = CI->getCalledValue();
2512
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002513 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002514 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002515 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00002516 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002517 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00002518 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
2519 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002520 return false;
2521
Evan Chengd10089a2010-01-27 00:00:57 +00002522 // fastcc with -tailcallopt is intended to provide a guaranteed
2523 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002524 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00002525 return false;
2526
Chris Lattner229907c2011-07-18 04:54:35 +00002527 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2528 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002529 bool isVarArg = FTy->isVarArg();
2530
2531 // Don't know how to handle Win64 varargs yet. Nothing special needed for
2532 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00002533 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002534 return false;
2535
Reid Klecknerf5b76512014-01-31 23:50:57 +00002536 // Don't know about inalloca yet.
2537 if (CS.hasInAllocaArgument())
2538 return false;
2539
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002540 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00002541 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002542 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002543 return false;
2544
Eli Friedman7b279422011-05-17 18:29:03 +00002545 // Check whether the function can return without sret-demotion.
2546 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002547 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00002548 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00002549 *FuncInfo.MF, FTy->isVarArg(),
2550 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002551 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00002552 return false;
2553
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002554 // Materialize callee address in a register. FIXME: GV address can be
2555 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00002556 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00002557 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00002558 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002559 unsigned CalleeOp = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002560 const GlobalValue *GV = nullptr;
2561 if (CalleeAM.GV != nullptr) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002562 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00002563 } else if (CalleeAM.Base.Reg != 0) {
2564 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002565 } else
2566 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002567
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002568 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002569 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002570 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002571 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002572 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00002573 unsigned arg_size = CS.arg_size();
2574 Args.reserve(arg_size);
2575 ArgVals.reserve(arg_size);
2576 ArgVTs.reserve(arg_size);
2577 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002578 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002579 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00002580 // If we're lowering a mem intrinsic instead of a regular call, skip the
2581 // last two arguments, which should not passed to the underlying functions.
2582 if (MemIntName && e-i <= 2)
2583 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002584 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002585 ISD::ArgFlagsTy Flags;
2586 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002587 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002588 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002589 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002590 Flags.setZExt();
2591
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002592 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00002593 PointerType *Ty = cast<PointerType>(ArgVal->getType());
2594 Type *ElementTy = Ty->getElementType();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002595 unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
Eli Friedman60afcc22011-05-20 22:21:04 +00002596 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
2597 if (!FrameAlign)
2598 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
2599 Flags.setByVal();
2600 Flags.setByValSize(FrameSize);
2601 Flags.setByValAlign(FrameAlign);
2602 if (!IsMemcpySmall(FrameSize))
2603 return false;
2604 }
2605
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002606 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00002607 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002608 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00002609 Flags.setNest();
2610
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002611 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
2612 // instruction. This is safe because it is common to all fastisel supported
2613 // calling conventions on x86.
2614 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
2615 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
2616 CI->getBitWidth() == 16) {
2617 if (Flags.isSExt())
2618 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
2619 else
2620 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
2621 }
2622 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002623
Chris Lattner5f4b7832011-04-19 05:09:50 +00002624 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002625
Chris Lattner34a08c22011-04-19 05:15:59 +00002626 // Passing bools around ends up doing a trunc to i1 and passing it.
2627 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00002628 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2629 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2630 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002631 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2632 ArgReg = getRegForValue(ArgVal);
2633 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002634
Chris Lattner5f4b7832011-04-19 05:09:50 +00002635 MVT ArgVT;
2636 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002637
Chris Lattner5f4b7832011-04-19 05:09:50 +00002638 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2639 ArgVal->hasOneUse(), 1);
2640 } else {
2641 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002642 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002643
Chris Lattner34a08c22011-04-19 05:15:59 +00002644 if (ArgReg == 0) return false;
2645
Chris Lattner229907c2011-07-18 04:54:35 +00002646 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002647 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002648 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002649 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002650 if (ArgVT == MVT::x86mmx)
2651 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002652 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002653 Flags.setOrigAlign(OriginalAlignment);
2654
Chris Lattner5f4b7832011-04-19 05:09:50 +00002655 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002656 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002657 ArgVTs.push_back(ArgVT);
2658 ArgFlags.push_back(Flags);
2659 }
2660
2661 // Analyze operands of the call, assigning locations to each operand.
2662 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002663 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002664 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002665
Dan Gohman47a07242010-06-01 21:09:47 +00002666 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002667 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002668 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002669
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002670 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002671
2672 // Get a count of how many bytes are to be pushed on the stack.
2673 unsigned NumBytes = CCInfo.getNextStackOffset();
2674
2675 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002676 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002677 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002678 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002679
Chris Lattner3ba29352008-10-15 05:30:52 +00002680 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002681 // copies / loads.
2682 SmallVector<unsigned, 4> RegArgs;
2683 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2684 CCValAssign &VA = ArgLocs[i];
2685 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002686 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002687
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002688 // Promote the value if needed.
2689 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002690 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002691 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002692 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2693 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002694 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2695 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002696 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002697 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002698 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002699 }
2700 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002701 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2702 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002703 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2704 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002705 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002706 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002707 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002708 }
2709 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002710 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2711 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002712 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2713 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002714 if (!Emitted)
2715 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002716 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002717 if (!Emitted)
2718 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2719 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002720
Chris Lattner2d7df022011-01-05 22:26:52 +00002721 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002722 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002723 break;
2724 }
Dan Gohman8c795692009-08-05 05:33:42 +00002725 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002726 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002727 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002728 assert(BC != 0 && "Failed to emit a bitcast!");
2729 Arg = BC;
2730 ArgVT = VA.getLocVT();
2731 break;
2732 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002733 case CCValAssign::VExt:
2734 // VExt has not been implemented, so this should be impossible to reach
2735 // for now. However, fallback to Selection DAG isel once implemented.
2736 return false;
2737 case CCValAssign::Indirect:
2738 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2739 // support this.
2740 return false;
Lang Hames06234ec2014-01-14 19:56:36 +00002741 case CCValAssign::FPExt:
2742 llvm_unreachable("Unexpected loc info!");
Evan Cheng6500d172008-09-08 06:35:17 +00002743 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002744
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002745 if (VA.isRegLoc()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002746 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2747 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002748 RegArgs.push_back(VA.getLocReg());
2749 } else {
2750 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002751 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002752 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2753 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002754 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002755 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002756 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002757 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002758
Eli Friedman60afcc22011-05-20 22:21:04 +00002759 if (Flags.isByVal()) {
2760 X86AddressMode SrcAM;
2761 SrcAM.Base.Reg = Arg;
2762 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2763 assert(Res && "memcpy length already checked!"); (void)Res;
2764 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2765 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002766 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002767 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002768 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2769 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002770 } else {
Juergen Ributzka349777d2014-06-12 23:27:57 +00002771 if (!X86FastEmitStore(ArgVT, Arg, /*ValIsKill=*/false, AM))
Lang Hames7d2f7b52011-10-18 22:11:33 +00002772 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002773 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002774 }
2775 }
2776
Dan Gohman3691d502008-09-25 15:24:26 +00002777 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002778 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002779 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002780 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002781 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2782 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002783 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002784
Charles Davise8f297c2013-07-12 06:02:35 +00002785 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002786 // Count the number of XMM registers allocated.
Craig Topper840beec2014-04-04 05:16:06 +00002787 static const MCPhysReg XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002788 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2789 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2790 };
2791 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002792 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002793 X86::AL).addImm(NumXMMRegs);
2794 }
2795
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002796 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002797 MachineInstrBuilder MIB;
2798 if (CalleeOp) {
2799 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002800 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002801 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002802 CallOpc = X86::CALL64r;
2803 else
2804 CallOpc = X86::CALL32r;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002805 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002806 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002807
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002808 } else {
2809 // Direct call.
2810 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002811 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002812 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002813 CallOpc = X86::CALL64pcrel32;
2814 else
2815 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002816
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002817 // See if we need any target-specific flags on the GV operand.
2818 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002819
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002820 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2821 // external symbols most go through the PLT in PIC mode. If the symbol
2822 // has hidden or protected visibility, or if it is static or local, then
2823 // we don't need to use the PLT - we can directly call it.
2824 if (Subtarget->isTargetELF() &&
2825 TM.getRelocationModel() == Reloc::PIC_ &&
2826 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2827 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002828 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002829 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002830 (!Subtarget->getTargetTriple().isMacOSX() ||
2831 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002832 // PC-relative references to external symbols should go through $stub,
2833 // unless we're building with the leopard linker or later, which
2834 // automatically synthesizes these stubs.
2835 OpFlags = X86II::MO_DARWIN_STUB;
2836 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002837
2838
Rafael Espindolaea09c592014-02-18 22:05:46 +00002839 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Eli Friedmancd2124a2011-06-10 23:39:36 +00002840 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002841 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002842 else
2843 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002844 }
Dan Gohman3691d502008-09-25 15:24:26 +00002845
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002846 // Add a register mask with the call-preserved registers.
2847 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2848 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2849
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002850 // Add an implicit use GOT pointer in EBX.
2851 if (Subtarget->isPICStyleGOT())
2852 MIB.addReg(X86::EBX, RegState::Implicit);
2853
Charles Davise8f297c2013-07-12 06:02:35 +00002854 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002855 MIB.addReg(X86::AL, RegState::Implicit);
2856
2857 // Add implicit physical register uses to the call.
2858 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2859 MIB.addReg(RegArgs[i], RegState::Implicit);
2860
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002861 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002862 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002863 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002864 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002865 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002866
Eli Friedman7b279422011-05-17 18:29:03 +00002867 // Build info for return calling conv lowering code.
2868 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2869 SmallVector<ISD::InputArg, 32> Ins;
2870 SmallVector<EVT, 4> RetTys;
2871 ComputeValueVTs(TLI, I->getType(), RetTys);
2872 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2873 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002874 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002875 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2876 for (unsigned j = 0; j != NumRegs; ++j) {
2877 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002878 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002879 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002880 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002881 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002882 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002883 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002884 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002885 MyFlags.Flags.setInReg();
2886 Ins.push_back(MyFlags);
2887 }
2888 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002889
Eli Friedman7b279422011-05-17 18:29:03 +00002890 // Now handle call return values.
2891 SmallVector<unsigned, 4> UsedRegs;
2892 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002893 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002894 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002895 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2896 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2897 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2898 EVT CopyVT = RVLocs[i].getValVT();
2899 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002900
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002901 // If this is a call to a function that returns an fp value on the x87 fp
2902 // stack, but where we prefer to use the value in xmm registers, copy it
2903 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002904 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002905 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002906 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002907 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002908 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002909 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00002910 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2911 TII.get(X86::FpPOP_RETVAL), CopyReg);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002912 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002913 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2914 TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002915 CopyReg).addReg(RVLocs[i].getLocReg());
2916 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002917 }
2918
Eli Friedman7b279422011-05-17 18:29:03 +00002919 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002920 // Round the F80 the right size, which also moves to the appropriate xmm
2921 // register. This is accomplished by storing the F80 value in memory and
2922 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002923 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002924 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002925 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002926 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002927 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002928 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002929 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002930 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002931 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Friedman7b279422011-05-17 18:29:03 +00002932 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002933 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002934 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002935
Eli Friedman7b279422011-05-17 18:29:03 +00002936 if (RVLocs.size())
2937 UpdateValueMap(I, ResultReg, RVLocs.size());
2938
Dan Gohman86936502010-06-18 23:28:01 +00002939 // Set all unused physreg defs as dead.
2940 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2941
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002942 return true;
2943}
2944
2945
Dan Gohmand58f3e32008-08-28 23:21:34 +00002946bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002947X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002948 switch (I->getOpcode()) {
2949 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002950 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002951 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002952 case Instruction::Store:
2953 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002954 case Instruction::Ret:
2955 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002956 case Instruction::ICmp:
2957 case Instruction::FCmp:
2958 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002959 case Instruction::ZExt:
2960 return X86SelectZExt(I);
2961 case Instruction::Br:
2962 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002963 case Instruction::Call:
2964 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002965 case Instruction::LShr:
2966 case Instruction::AShr:
2967 case Instruction::Shl:
2968 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002969 case Instruction::SDiv:
2970 case Instruction::UDiv:
2971 case Instruction::SRem:
2972 case Instruction::URem:
2973 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002974 case Instruction::Select:
2975 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002976 case Instruction::Trunc:
2977 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002978 case Instruction::FPExt:
2979 return X86SelectFPExt(I);
2980 case Instruction::FPTrunc:
2981 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002982 case Instruction::IntToPtr: // Deliberate fall-through.
2983 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002984 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2985 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002986 if (DstVT.bitsGT(SrcVT))
2987 return X86SelectZExt(I);
2988 if (DstVT.bitsLT(SrcVT))
2989 return X86SelectTrunc(I);
2990 unsigned Reg = getRegForValue(I->getOperand(0));
2991 if (Reg == 0) return false;
2992 UpdateValueMap(I, Reg);
2993 return true;
2994 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002995 }
2996
2997 return false;
2998}
2999
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003000unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003001 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00003002 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00003003 return 0;
3004
3005 // Can't handle alternate code models yet.
3006 if (TM.getCodeModel() != CodeModel::Small)
3007 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003008
Owen Anderson50288e32008-09-05 00:06:23 +00003009 // Get opcode and regclass of the output for the given load instruction.
3010 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003011 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00003012 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00003013 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00003014 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00003015 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00003016 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003017 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003018 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00003019 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00003020 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003021 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003022 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00003023 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00003024 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003025 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003026 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00003027 // Must be in x86-64 mode.
3028 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00003029 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003030 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003031 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00003032 if (X86ScalarSSEf32) {
3033 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00003034 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003035 } else {
3036 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00003037 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003038 }
3039 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003040 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00003041 if (X86ScalarSSEf64) {
3042 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00003043 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003044 } else {
3045 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00003046 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003047 }
3048 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003049 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00003050 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00003051 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00003052 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003053
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003054 // Materialize addresses with LEA/MOV instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00003055 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00003056 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00003057 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00003058 // If the expression is just a basereg, then we're done, otherwise we need
3059 // to emit an LEA.
3060 if (AM.BaseType == X86AddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00003061 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
Chris Lattner48326602011-04-17 17:12:08 +00003062 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003063
Dan Gohman9801ba42008-09-19 22:16:54 +00003064 unsigned ResultReg = createResultReg(RC);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003065 if (TM.getRelocationModel() == Reloc::Static &&
3066 TLI.getPointerTy() == MVT::i64) {
3067 // The displacement code be more than 32 bits away so we need to use
3068 // an instruction with a 64 bit immediate
3069 Opc = X86::MOV64ri;
3070 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3071 TII.get(Opc), ResultReg).addGlobalAddress(cast<GlobalValue>(C));
3072 } else {
3073 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
3074 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003075 TII.get(Opc), ResultReg), AM);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003076 }
Owen Anderson50288e32008-09-05 00:06:23 +00003077 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00003078 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00003079 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00003080 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003081
Owen Andersond41c7162008-09-06 01:11:01 +00003082 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00003083 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00003084 if (Align == 0) {
3085 // Alignment of vector types. FIXME!
Rafael Espindolaea09c592014-02-18 22:05:46 +00003086 Align = DL.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00003087 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003088
Dan Gohman8392f0c2008-09-30 01:21:32 +00003089 // x86-32 PIC requires a PIC base register for constant pools.
3090 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00003091 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00003092 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00003093 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00003094 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00003095 } else if (Subtarget->isPICStyleGOT()) {
3096 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00003097 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00003098 } else if (Subtarget->isPICStyleRIPRel() &&
3099 TM.getCodeModel() == CodeModel::Small) {
3100 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00003101 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00003102
3103 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00003104 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00003105 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003106 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003107 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00003108 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00003109
Owen Anderson50288e32008-09-05 00:06:23 +00003110 return ResultReg;
3111}
3112
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003113unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003114 // Fail on dynamic allocas. At this point, getRegForValue has already
3115 // checked its CSE maps, so if we're here trying to handle a dynamic
3116 // alloca, we're not going to succeed. X86SelectAddress has a
3117 // check for dynamic allocas, because it's called directly from
3118 // various places, but TargetMaterializeAlloca also needs a check
3119 // in order to avoid recursion between getRegForValue,
3120 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00003121 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003122 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00003123 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003124
Dan Gohman39d82f92008-09-10 20:11:02 +00003125 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00003126 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00003127 return 0;
3128 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00003129 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00003130 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003131 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003132 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00003133 return ResultReg;
3134}
3135
Eli Friedman406c4712011-04-27 22:41:55 +00003136unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
3137 MVT VT;
3138 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003139 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003140
3141 // Get opcode and regclass for the given zero.
3142 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003143 const TargetRegisterClass *RC = nullptr;
Eli Friedman406c4712011-04-27 22:41:55 +00003144 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003145 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00003146 case MVT::f32:
3147 if (X86ScalarSSEf32) {
3148 Opc = X86::FsFLD0SS;
3149 RC = &X86::FR32RegClass;
3150 } else {
3151 Opc = X86::LD_Fp032;
3152 RC = &X86::RFP32RegClass;
3153 }
3154 break;
3155 case MVT::f64:
3156 if (X86ScalarSSEf64) {
3157 Opc = X86::FsFLD0SD;
3158 RC = &X86::FR64RegClass;
3159 } else {
3160 Opc = X86::LD_Fp064;
3161 RC = &X86::RFP64RegClass;
3162 }
3163 break;
3164 case MVT::f80:
3165 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003166 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003167 }
3168
3169 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003170 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Eli Friedman406c4712011-04-27 22:41:55 +00003171 return ResultReg;
3172}
3173
3174
Eli Bendersky90dd3e72013-04-19 22:29:18 +00003175bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3176 const LoadInst *LI) {
Juergen Ributzka349777d2014-06-12 23:27:57 +00003177 const Value *Ptr = LI->getPointerOperand();
Chris Lattnereeba0c72010-09-05 02:18:34 +00003178 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00003179 if (!X86SelectAddress(Ptr, AM))
Chris Lattnereeba0c72010-09-05 02:18:34 +00003180 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003181
Craig Topper55406d92012-08-11 17:46:16 +00003182 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00003183
Rafael Espindolaea09c592014-02-18 22:05:46 +00003184 unsigned Size = DL.getTypeAllocSize(LI->getType());
Chris Lattnereeba0c72010-09-05 02:18:34 +00003185 unsigned Alignment = LI->getAlignment();
3186
Juergen Ributzka349777d2014-06-12 23:27:57 +00003187 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3188 Alignment = DL.getABITypeAlignment(LI->getType());
3189
Chris Lattnereeba0c72010-09-05 02:18:34 +00003190 SmallVector<MachineOperand, 8> AddrOps;
3191 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00003192
Chris Lattnereeba0c72010-09-05 02:18:34 +00003193 MachineInstr *Result =
3194 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
Juergen Ributzka349777d2014-06-12 23:27:57 +00003195 if (!Result)
3196 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003197
Juergen Ributzka349777d2014-06-12 23:27:57 +00003198 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Chris Lattner2d186572011-01-16 02:27:38 +00003199 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00003200 MI->eraseFromParent();
3201 return true;
3202}
3203
3204
Evan Cheng24422d42008-09-03 00:03:49 +00003205namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003206 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
3207 const TargetLibraryInfo *libInfo) {
3208 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00003209 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00003210}