blob: 3c9acba5cbe1184b6c89289916ee4b1638c42c09 [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
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000116 bool X86SelectSelect(const Instruction *I);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000117
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000118 bool X86SelectTrunc(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000119
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000120 bool X86SelectFPExt(const Instruction *I);
121 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohmanbf646f22008-09-10 21:02:08 +0000122
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000123 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
124 bool X86SelectCall(const Instruction *I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000125
Eli Friedmancd2124a2011-06-10 23:39:36 +0000126 bool DoSelectCall(const Instruction *I, const char *MemIntName);
127
Dan Gohman3691d502008-09-25 15:24:26 +0000128 const X86InstrInfo *getInstrInfo() const {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000129 return getTargetMachine()->getInstrInfo();
130 }
131 const X86TargetMachine *getTargetMachine() const {
132 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman3691d502008-09-25 15:24:26 +0000133 }
134
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000135 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
136
Craig Topper2d9361e2014-03-09 07:44:38 +0000137 unsigned TargetMaterializeConstant(const Constant *C) override;
Dan Gohman39d82f92008-09-10 20:11:02 +0000138
Craig Topper2d9361e2014-03-09 07:44:38 +0000139 unsigned TargetMaterializeAlloca(const AllocaInst *C) override;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000140
Craig Topper2d9361e2014-03-09 07:44:38 +0000141 unsigned TargetMaterializeFloatZero(const ConstantFP *CF) override;
Eli Friedman406c4712011-04-27 22:41:55 +0000142
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000143 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
144 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000145 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000146 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
147 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000148 }
149
Chris Lattner229907c2011-07-18 04:54:35 +0000150 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000151
Eli Friedman60afcc22011-05-20 22:21:04 +0000152 bool IsMemcpySmall(uint64_t Len);
153
Eli Friedmanbcc69142011-04-27 01:45:07 +0000154 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
155 X86AddressMode SrcAM, uint64_t Len);
Evan Cheng24422d42008-09-03 00:03:49 +0000156};
Wesley Peck527da1b2010-11-23 03:31:01 +0000157
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000158} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000159
Juergen Ributzkaaa602092014-06-17 21:55:43 +0000160static CmpInst::Predicate optimizeCmpPredicate(const CmpInst *CI) {
161 // If both operands are the same, then try to optimize or fold the cmp.
162 CmpInst::Predicate Predicate = CI->getPredicate();
163 if (CI->getOperand(0) != CI->getOperand(1))
164 return Predicate;
165
166 switch (Predicate) {
167 default: llvm_unreachable("Invalid predicate!");
168 case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break;
169 case CmpInst::FCMP_OEQ: Predicate = CmpInst::FCMP_ORD; break;
170 case CmpInst::FCMP_OGT: Predicate = CmpInst::FCMP_FALSE; break;
171 case CmpInst::FCMP_OGE: Predicate = CmpInst::FCMP_ORD; break;
172 case CmpInst::FCMP_OLT: Predicate = CmpInst::FCMP_FALSE; break;
173 case CmpInst::FCMP_OLE: Predicate = CmpInst::FCMP_ORD; break;
174 case CmpInst::FCMP_ONE: Predicate = CmpInst::FCMP_FALSE; break;
175 case CmpInst::FCMP_ORD: Predicate = CmpInst::FCMP_ORD; break;
176 case CmpInst::FCMP_UNO: Predicate = CmpInst::FCMP_UNO; break;
177 case CmpInst::FCMP_UEQ: Predicate = CmpInst::FCMP_TRUE; break;
178 case CmpInst::FCMP_UGT: Predicate = CmpInst::FCMP_UNO; break;
179 case CmpInst::FCMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
180 case CmpInst::FCMP_ULT: Predicate = CmpInst::FCMP_UNO; break;
181 case CmpInst::FCMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
182 case CmpInst::FCMP_UNE: Predicate = CmpInst::FCMP_UNO; break;
183 case CmpInst::FCMP_TRUE: Predicate = CmpInst::FCMP_TRUE; break;
184
185 case CmpInst::ICMP_EQ: Predicate = CmpInst::FCMP_TRUE; break;
186 case CmpInst::ICMP_NE: Predicate = CmpInst::FCMP_FALSE; break;
187 case CmpInst::ICMP_UGT: Predicate = CmpInst::FCMP_FALSE; break;
188 case CmpInst::ICMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
189 case CmpInst::ICMP_ULT: Predicate = CmpInst::FCMP_FALSE; break;
190 case CmpInst::ICMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
191 case CmpInst::ICMP_SGT: Predicate = CmpInst::FCMP_FALSE; break;
192 case CmpInst::ICMP_SGE: Predicate = CmpInst::FCMP_TRUE; break;
193 case CmpInst::ICMP_SLT: Predicate = CmpInst::FCMP_FALSE; break;
194 case CmpInst::ICMP_SLE: Predicate = CmpInst::FCMP_TRUE; break;
195 }
196
197 return Predicate;
198}
199
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +0000200static std::pair<X86::CondCode, bool>
201getX86ConditonCode(CmpInst::Predicate Predicate) {
202 X86::CondCode CC = X86::COND_INVALID;
203 bool NeedSwap = false;
204 switch (Predicate) {
205 default: break;
206 // Floating-point Predicates
207 case CmpInst::FCMP_UEQ: CC = X86::COND_E; break;
208 case CmpInst::FCMP_OLT: NeedSwap = true; // fall-through
209 case CmpInst::FCMP_OGT: CC = X86::COND_A; break;
210 case CmpInst::FCMP_OLE: NeedSwap = true; // fall-through
211 case CmpInst::FCMP_OGE: CC = X86::COND_AE; break;
212 case CmpInst::FCMP_UGT: NeedSwap = true; // fall-through
213 case CmpInst::FCMP_ULT: CC = X86::COND_B; break;
214 case CmpInst::FCMP_UGE: NeedSwap = true; // fall-through
215 case CmpInst::FCMP_ULE: CC = X86::COND_BE; break;
216 case CmpInst::FCMP_ONE: CC = X86::COND_NE; break;
217 case CmpInst::FCMP_UNO: CC = X86::COND_P; break;
218 case CmpInst::FCMP_ORD: CC = X86::COND_NP; break;
219 case CmpInst::FCMP_OEQ: // fall-through
220 case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break;
221
222 // Integer Predicates
223 case CmpInst::ICMP_EQ: CC = X86::COND_E; break;
224 case CmpInst::ICMP_NE: CC = X86::COND_NE; break;
225 case CmpInst::ICMP_UGT: CC = X86::COND_A; break;
226 case CmpInst::ICMP_UGE: CC = X86::COND_AE; break;
227 case CmpInst::ICMP_ULT: CC = X86::COND_B; break;
228 case CmpInst::ICMP_ULE: CC = X86::COND_BE; break;
229 case CmpInst::ICMP_SGT: CC = X86::COND_G; break;
230 case CmpInst::ICMP_SGE: CC = X86::COND_GE; break;
231 case CmpInst::ICMP_SLT: CC = X86::COND_L; break;
232 case CmpInst::ICMP_SLE: CC = X86::COND_LE; break;
233 }
234
235 return std::make_pair(CC, NeedSwap);
236}
237
Chris Lattner229907c2011-07-18 04:54:35 +0000238bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000239 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
240 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000241 // Unhandled type. Halt "fast" selection and bail.
242 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000243
244 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000245 // For now, require SSE/SSE2 for performing floating-point operations,
246 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000247 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000248 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000249 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000250 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000251 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000252 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000253 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000254 // We only handle legal types. For example, on x86-32 the instruction
255 // selector contains all of the 64-bit instructions from x86-64,
256 // under the assumption that i64 won't be used if the target doesn't
257 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000258 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000259}
260
261#include "X86GenCallingConv.inc"
262
Evan Chengf5bc7e52008-09-05 21:00:03 +0000263/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000264/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000265/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000266bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000267 MachineMemOperand *MMO, unsigned &ResultReg) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000268 // Get opcode and regclass of the output for the given load instruction.
269 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000270 const TargetRegisterClass *RC = nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000271 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000272 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000273 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000274 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000275 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000276 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000277 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000278 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000279 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000280 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000281 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000282 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000283 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000284 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000285 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000286 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000287 // Must be in x86-64 mode.
288 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000289 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000290 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000291 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000292 if (X86ScalarSSEf32) {
293 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000294 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000295 } else {
296 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000297 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000298 }
299 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000300 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000301 if (X86ScalarSSEf64) {
302 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000303 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000304 } else {
305 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000306 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000307 }
308 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000309 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000310 // No f80 support yet.
311 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000312 }
313
314 ResultReg = createResultReg(RC);
Juergen Ributzka349777d2014-06-12 23:27:57 +0000315 MachineInstrBuilder MIB =
316 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
317 addFullAddress(MIB, AM);
318 if (MMO)
319 MIB->addMemOperand(*FuncInfo.MF, MMO);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000320 return true;
321}
322
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000323/// X86FastEmitStore - Emit a machine instruction to store a value Val of
324/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
325/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000326/// i.e. V. Return true if it is possible.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000327bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
328 const X86AddressMode &AM,
329 MachineMemOperand *MMO, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000330 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000331 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000332 switch (VT.getSimpleVT().SimpleTy) {
333 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000334 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000335 case MVT::i1: {
336 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000337 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000338 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000339 TII.get(X86::AND8ri), AndResult)
340 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
Craig Topper4f55b0e2013-07-17 05:57:45 +0000341 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000342 }
343 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000344 case MVT::i8: Opc = X86::MOV8mr; break;
345 case MVT::i16: Opc = X86::MOV16mr; break;
346 case MVT::i32: Opc = X86::MOV32mr; break;
347 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
348 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000349 Opc = X86ScalarSSEf32 ?
350 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000351 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000352 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000353 Opc = X86ScalarSSEf64 ?
354 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000355 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000356 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000357 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000358 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000359 else
Craig Topper55475d42013-07-17 06:58:23 +0000360 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000361 break;
362 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000363 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000364 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000365 else
Craig Topperad1fff92013-07-18 07:16:44 +0000366 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000367 break;
368 case MVT::v4i32:
369 case MVT::v2i64:
370 case MVT::v8i16:
371 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000372 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000373 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000374 else
Craig Topper55475d42013-07-17 06:58:23 +0000375 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000376 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000377 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000378
Juergen Ributzka349777d2014-06-12 23:27:57 +0000379 MachineInstrBuilder MIB =
380 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
381 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
382 if (MMO)
383 MIB->addMemOperand(*FuncInfo.MF, MMO);
384
Evan Chengf5bc7e52008-09-05 21:00:03 +0000385 return true;
386}
387
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000388bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000389 const X86AddressMode &AM,
390 MachineMemOperand *MMO, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000391 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000392 if (isa<ConstantPointerNull>(Val))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000393 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000394
Chris Lattner3ba29352008-10-15 05:30:52 +0000395 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000396 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000397 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000398 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000399 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000400 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000401 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000402 case MVT::i8: Opc = X86::MOV8mi; break;
403 case MVT::i16: Opc = X86::MOV16mi; break;
404 case MVT::i32: Opc = X86::MOV32mi; break;
405 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000406 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000407 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000408 Opc = X86::MOV64mi32;
409 break;
410 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000411
Chris Lattner3ba29352008-10-15 05:30:52 +0000412 if (Opc) {
Juergen Ributzka349777d2014-06-12 23:27:57 +0000413 MachineInstrBuilder MIB =
414 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
415 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
416 : CI->getZExtValue());
417 if (MMO)
418 MIB->addMemOperand(*FuncInfo.MF, MMO);
Chris Lattner3ba29352008-10-15 05:30:52 +0000419 return true;
420 }
421 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000422
Chris Lattner3ba29352008-10-15 05:30:52 +0000423 unsigned ValReg = getRegForValue(Val);
424 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000425 return false;
426
Juergen Ributzka349777d2014-06-12 23:27:57 +0000427 bool ValKill = hasTrivialKill(Val);
428 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000429}
430
Evan Cheng6500d172008-09-08 06:35:17 +0000431/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
432/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
433/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000434bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
435 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000436 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000437 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
438 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000439 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000440 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000441
442 ResultReg = RR;
443 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000444}
445
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000446bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
447 // Handle constant address.
448 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
449 // Can't handle alternate code models yet.
450 if (TM.getCodeModel() != CodeModel::Small)
451 return false;
452
453 // Can't handle TLS yet.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000454 if (GV->isThreadLocal())
455 return false;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000456
457 // RIP-relative addresses can't have additional register operands, so if
458 // we've already folded stuff into the addressing mode, just force the
459 // global value into its own register, which we can use as the basereg.
460 if (!Subtarget->isPICStyleRIPRel() ||
461 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
462 // Okay, we've committed to selecting this global. Set up the address.
463 AM.GV = GV;
464
465 // Allow the subtarget to classify the global.
466 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
467
468 // If this reference is relative to the pic base, set it now.
469 if (isGlobalRelativeToPICBase(GVFlags)) {
470 // FIXME: How do we know Base.Reg is free??
471 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
472 }
473
474 // Unless the ABI requires an extra load, return a direct reference to
475 // the global.
476 if (!isGlobalStubReference(GVFlags)) {
477 if (Subtarget->isPICStyleRIPRel()) {
478 // Use rip-relative addressing if we can. Above we verified that the
479 // base and index registers are unused.
480 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
481 AM.Base.Reg = X86::RIP;
482 }
483 AM.GVOpFlags = GVFlags;
484 return true;
485 }
486
487 // Ok, we need to do a load from a stub. If we've already loaded from
488 // this stub, reuse the loaded pointer, otherwise emit the load now.
489 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
490 unsigned LoadReg;
491 if (I != LocalValueMap.end() && I->second != 0) {
492 LoadReg = I->second;
493 } else {
494 // Issue load from stub.
495 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000496 const TargetRegisterClass *RC = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000497 X86AddressMode StubAM;
498 StubAM.Base.Reg = AM.Base.Reg;
499 StubAM.GV = GV;
500 StubAM.GVOpFlags = GVFlags;
501
502 // Prepare for inserting code in the local-value area.
503 SavePoint SaveInsertPt = enterLocalValueArea();
504
505 if (TLI.getPointerTy() == MVT::i64) {
506 Opc = X86::MOV64rm;
507 RC = &X86::GR64RegClass;
508
509 if (Subtarget->isPICStyleRIPRel())
510 StubAM.Base.Reg = X86::RIP;
511 } else {
512 Opc = X86::MOV32rm;
513 RC = &X86::GR32RegClass;
514 }
515
516 LoadReg = createResultReg(RC);
517 MachineInstrBuilder LoadMI =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000518 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000519 addFullAddress(LoadMI, StubAM);
520
521 // Ok, back to normal mode.
522 leaveLocalValueArea(SaveInsertPt);
523
524 // Prevent loading GV stub multiple times in same MBB.
525 LocalValueMap[V] = LoadReg;
526 }
527
528 // Now construct the final address. Note that the Disp, Scale,
529 // and Index values may already be set here.
530 AM.Base.Reg = LoadReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000531 AM.GV = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000532 return true;
533 }
534 }
535
536 // If all else fails, try to materialize the value in a register.
537 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
538 if (AM.Base.Reg == 0) {
539 AM.Base.Reg = getRegForValue(V);
540 return AM.Base.Reg != 0;
541 }
542 if (AM.IndexReg == 0) {
543 assert(AM.Scale == 1 && "Scale with no index!");
544 AM.IndexReg = getRegForValue(V);
545 return AM.IndexReg != 0;
546 }
547 }
548
549 return false;
550}
551
Dan Gohman39d82f92008-09-10 20:11:02 +0000552/// X86SelectAddress - Attempt to fill in an address from the given value.
553///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000554bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000555 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000556redo_gep:
Craig Topper062a2ba2014-04-25 05:30:21 +0000557 const User *U = nullptr;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000558 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000559 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000560 // Don't walk into other basic blocks; it's possible we haven't
561 // visited them yet, so the instructions may not yet be assigned
562 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000563 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
564 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
565 Opcode = I->getOpcode();
566 U = I;
567 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000568 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000569 Opcode = C->getOpcode();
570 U = C;
571 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000572
Chris Lattner229907c2011-07-18 04:54:35 +0000573 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000574 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000575 // Fast instruction selection doesn't support the special
576 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000577 return false;
578
Dan Gohman6e005fd2008-09-18 23:23:44 +0000579 switch (Opcode) {
580 default: break;
581 case Instruction::BitCast:
582 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000583 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000584
585 case Instruction::IntToPtr:
586 // Look past no-op inttoptrs.
587 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000588 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000589 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000590
591 case Instruction::PtrToInt:
592 // Look past no-op ptrtoints.
593 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000594 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000595 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000596
597 case Instruction::Alloca: {
598 // Do static allocas.
599 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000600 DenseMap<const AllocaInst*, int>::iterator SI =
601 FuncInfo.StaticAllocaMap.find(A);
602 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000603 AM.BaseType = X86AddressMode::FrameIndexBase;
604 AM.Base.FrameIndex = SI->second;
605 return true;
606 }
607 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000608 }
609
610 case Instruction::Add: {
611 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000612 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000613 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
614 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000615 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000616 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000617 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000618 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000619 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000620 break;
621 }
622
623 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000624 X86AddressMode SavedAM = AM;
625
Dan Gohman6e005fd2008-09-18 23:23:44 +0000626 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000627 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000628 unsigned IndexReg = AM.IndexReg;
629 unsigned Scale = AM.Scale;
630 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000631 // Iterate through the indices, folding what we can. Constants can be
632 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000633 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000634 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000635 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000636 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000637 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000638 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
639 continue;
640 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000641
Chris Lattner4b026b92011-04-17 17:05:12 +0000642 // A array/variable index is always of the form i*S where S is the
643 // constant scale size. See if we can push the scale into immediates.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000644 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner4b026b92011-04-17 17:05:12 +0000645 for (;;) {
646 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
647 // Constant-offset addressing.
648 Disp += CI->getSExtValue() * S;
649 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000650 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000651 if (canFoldAddIntoGEP(U, Op)) {
652 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000653 ConstantInt *CI =
654 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
655 Disp += CI->getSExtValue() * S;
656 // Iterate on the other operand.
657 Op = cast<AddOperator>(Op)->getOperand(0);
658 continue;
659 }
660 if (IndexReg == 0 &&
661 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
662 (S == 1 || S == 2 || S == 4 || S == 8)) {
663 // Scaled-index addressing.
664 Scale = S;
665 IndexReg = getRegForGEPIndex(Op).first;
666 if (IndexReg == 0)
667 return false;
668 break;
669 }
670 // Unsupported.
671 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000672 }
673 }
Bill Wendling585a9012013-09-24 00:13:08 +0000674
Dan Gohman2564b902008-09-26 20:04:15 +0000675 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000676 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000677 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000678
Dan Gohman6e005fd2008-09-18 23:23:44 +0000679 AM.IndexReg = IndexReg;
680 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000681 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000682 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000683
684 if (const GetElementPtrInst *GEP =
685 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
686 // Ok, the GEP indices were covered by constant-offset and scaled-index
687 // addressing. Update the address state and move on to examining the base.
688 V = GEP;
689 goto redo_gep;
690 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000691 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000692 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000693
Chris Lattner4b026b92011-04-17 17:05:12 +0000694 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000695 // our address and just match the value instead of completely failing.
696 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000697
698 for (SmallVectorImpl<const Value *>::reverse_iterator
699 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
700 if (handleConstantAddresses(*I, AM))
701 return true;
702
703 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000704 unsupported_gep:
705 // Ok, the GEP indices weren't all covered.
706 break;
707 }
708 }
709
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000710 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000711}
712
Chris Lattner8212d372009-07-10 05:33:42 +0000713/// X86SelectCallAddress - Attempt to fill in an address from the given value.
714///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000715bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000716 const User *U = nullptr;
Chris Lattner8212d372009-07-10 05:33:42 +0000717 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000718 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000719 // Record if the value is defined in the same basic block.
720 //
721 // This information is crucial to know whether or not folding an
722 // operand is valid.
723 // Indeed, FastISel generates or reuses a virtual register for all
724 // operands of all instructions it selects. Obviously, the definition and
725 // its uses must use the same virtual register otherwise the produced
726 // code is incorrect.
727 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
728 // registers for values that are alive across basic blocks. This ensures
729 // that the values are consistently set between across basic block, even
730 // if different instruction selection mechanisms are used (e.g., a mix of
731 // SDISel and FastISel).
732 // For values local to a basic block, the instruction selection process
733 // generates these virtual registers with whatever method is appropriate
734 // for its needs. In particular, FastISel and SDISel do not share the way
735 // local virtual registers are set.
736 // Therefore, this is impossible (or at least unsafe) to share values
737 // between basic blocks unless they use the same instruction selection
738 // method, which is not guarantee for X86.
739 // Moreover, things like hasOneUse could not be used accurately, if we
740 // allow to reference values across basic blocks whereas they are not
741 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000742 bool InMBB = true;
743 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000744 Opcode = I->getOpcode();
745 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000746 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000747 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000748 Opcode = C->getOpcode();
749 U = C;
750 }
751
752 switch (Opcode) {
753 default: break;
754 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000755 // Look past bitcasts if its operand is in the same BB.
756 if (InMBB)
757 return X86SelectCallAddress(U->getOperand(0), AM);
758 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000759
760 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000761 // Look past no-op inttoptrs if its operand is in the same BB.
762 if (InMBB &&
763 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000764 return X86SelectCallAddress(U->getOperand(0), AM);
765 break;
766
767 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000768 // Look past no-op ptrtoints if its operand is in the same BB.
769 if (InMBB &&
770 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000771 return X86SelectCallAddress(U->getOperand(0), AM);
772 break;
773 }
774
775 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000776 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000777 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000778 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000779 return false;
780
781 // RIP-relative addresses can't have additional register operands.
782 if (Subtarget->isPICStyleRIPRel() &&
783 (AM.Base.Reg != 0 || AM.IndexReg != 0))
784 return false;
785
Rafael Espindolaea09c592014-02-18 22:05:46 +0000786 // Can't handle DbgLocLImport.
Nico Rieck7157bb72014-01-14 15:22:47 +0000787 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000788 return false;
789
790 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000791 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000792 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000793 return false;
794
795 // Okay, we've committed to selecting this global. Set up the basic address.
796 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000797
Chris Lattner7277a802009-07-10 05:45:15 +0000798 // No ABI requires an extra load for anything other than DLLImport, which
799 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000800 if (Subtarget->isPICStyleRIPRel()) {
801 // Use rip-relative addressing if we can. Above we verified that the
802 // base and index registers are unused.
803 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
804 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000805 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000806 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
807 } else if (Subtarget->isPICStyleGOT()) {
808 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000809 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000810
Chris Lattner8212d372009-07-10 05:33:42 +0000811 return true;
812 }
813
814 // If all else fails, try to materialize the value in a register.
815 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
816 if (AM.Base.Reg == 0) {
817 AM.Base.Reg = getRegForValue(V);
818 return AM.Base.Reg != 0;
819 }
820 if (AM.IndexReg == 0) {
821 assert(AM.Scale == 1 && "Scale with no index!");
822 AM.IndexReg = getRegForValue(V);
823 return AM.IndexReg != 0;
824 }
825 }
826
827 return false;
828}
829
830
Owen Anderson4f948bd2008-09-04 07:08:58 +0000831/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000832bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000833 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000834 const StoreInst *S = cast<StoreInst>(I);
835
836 if (S->isAtomic())
837 return false;
838
Juergen Ributzka349777d2014-06-12 23:27:57 +0000839 const Value *Val = S->getValueOperand();
840 const Value *Ptr = S->getPointerOperand();
Craig Topper4f55b0e2013-07-17 05:57:45 +0000841
Duncan Sandsf5dda012010-11-03 11:35:31 +0000842 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000843 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000844 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000845
Juergen Ributzka349777d2014-06-12 23:27:57 +0000846 unsigned Alignment = S->getAlignment();
847 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
848 if (Alignment == 0) // Ensure that codegen never sees alignment 0
849 Alignment = ABIAlignment;
850 bool Aligned = Alignment >= ABIAlignment;
851
Dan Gohman39d82f92008-09-10 20:11:02 +0000852 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000853 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000854 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000855
Juergen Ributzka349777d2014-06-12 23:27:57 +0000856 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000857}
858
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000859/// X86SelectRet - Select and emit code to implement ret instructions.
860bool X86FastISel::X86SelectRet(const Instruction *I) {
861 const ReturnInst *Ret = cast<ReturnInst>(I);
862 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000863 const X86MachineFunctionInfo *X86MFInfo =
864 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000865
866 if (!FuncInfo.CanLowerReturn)
867 return false;
868
869 CallingConv::ID CC = F.getCallingConv();
870 if (CC != CallingConv::C &&
871 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000872 CC != CallingConv::X86_FastCall &&
873 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000874 return false;
875
Charles Davise8f297c2013-07-12 06:02:35 +0000876 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000877 return false;
878
879 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000880 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000881 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000882
883 // fastcc with -tailcallopt is intended to provide a guaranteed
884 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000885 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000886 return false;
887
888 // Let SDISel handle vararg functions.
889 if (F.isVarArg())
890 return false;
891
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000892 // Build a list of return value registers.
893 SmallVector<unsigned, 4> RetRegs;
894
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000895 if (Ret->getNumOperands() > 0) {
896 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000897 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000898
899 // Analyze operands of the call, assigning locations to each operand.
900 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000901 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000902 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000903 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000904
905 const Value *RV = Ret->getOperand(0);
906 unsigned Reg = getRegForValue(RV);
907 if (Reg == 0)
908 return false;
909
910 // Only handle a single return value for now.
911 if (ValLocs.size() != 1)
912 return false;
913
914 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000915
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000916 // Don't bother handling odd stuff for now.
917 if (VA.getLocInfo() != CCValAssign::Full)
918 return false;
919 // Only handle register returns for now.
920 if (!VA.isRegLoc())
921 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000922
923 // The calling-convention tables for x87 returns don't tell
924 // the whole story.
925 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
926 return false;
927
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000928 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000929 EVT SrcVT = TLI.getValueType(RV->getType());
930 EVT DstVT = VA.getValVT();
931 // Special handling for extended integers.
932 if (SrcVT != DstVT) {
933 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
934 return false;
935
936 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
937 return false;
938
939 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
940
941 if (SrcVT == MVT::i1) {
942 if (Outs[0].Flags.isSExt())
943 return false;
944 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
945 SrcVT = MVT::i8;
946 }
947 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
948 ISD::SIGN_EXTEND;
949 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
950 SrcReg, /*TODO: Kill=*/false);
951 }
952
953 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000954 unsigned DstReg = VA.getLocReg();
955 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000956 // Avoid a cross-class copy. This is very unlikely.
957 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000958 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000959 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000960 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000961
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000962 // Add register to return instruction.
963 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000964 }
965
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000966 // The x86-64 ABI for returning structs by value requires that we copy
967 // the sret argument into %rax for the return. We saved the argument into
968 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000969 // and into %rax. We also do the same with %eax for Win32.
970 if (F.hasStructRetAttr() &&
Yaron Keren136fe7d2014-04-01 18:15:34 +0000971 (Subtarget->is64Bit() || Subtarget->isTargetKnownWindowsMSVC())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000972 unsigned Reg = X86MFInfo->getSRetReturnReg();
973 assert(Reg &&
974 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000975 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000976 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000977 RetReg).addReg(Reg);
978 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000979 }
980
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000981 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000982 MachineInstrBuilder MIB =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000983 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000984 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
985 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000986 return true;
987}
988
Evan Chenga41ee292008-09-03 06:44:39 +0000989/// X86SelectLoad - Select and emit code to implement load instructions.
990///
Juergen Ributzka349777d2014-06-12 23:27:57 +0000991bool X86FastISel::X86SelectLoad(const Instruction *I) {
992 const LoadInst *LI = cast<LoadInst>(I);
993
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000994 // Atomic loads need special handling.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000995 if (LI->isAtomic())
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000996 return false;
997
Duncan Sandsf5dda012010-11-03 11:35:31 +0000998 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000999 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +00001000 return false;
1001
Juergen Ributzka349777d2014-06-12 23:27:57 +00001002 const Value *Ptr = LI->getPointerOperand();
1003
Dan Gohman39d82f92008-09-10 20:11:02 +00001004 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001005 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00001006 return false;
Evan Chenga41ee292008-09-03 06:44:39 +00001007
Evan Chengf5bc7e52008-09-05 21:00:03 +00001008 unsigned ResultReg = 0;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001009 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg))
1010 return false;
1011
1012 UpdateValueMap(I, ResultReg);
1013 return true;
Evan Chenga41ee292008-09-03 06:44:39 +00001014}
1015
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001016static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001017 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +00001018 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
1019 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001020
Owen Anderson9f944592009-08-11 20:47:22 +00001021 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +00001022 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001023 case MVT::i8: return X86::CMP8rr;
1024 case MVT::i16: return X86::CMP16rr;
1025 case MVT::i32: return X86::CMP32rr;
1026 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001027 case MVT::f32:
1028 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
1029 case MVT::f64:
1030 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001031 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001032}
1033
Chris Lattner88f47542008-10-15 04:13:29 +00001034/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
1035/// of the comparison, return an opcode that works for the compare (e.g.
1036/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001037static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +00001038 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +00001039 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +00001040 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001041 case MVT::i8: return X86::CMP8ri;
1042 case MVT::i16: return X86::CMP16ri;
1043 case MVT::i32: return X86::CMP32ri;
1044 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +00001045 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1046 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +00001047 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +00001048 return X86::CMP64ri32;
1049 return 0;
1050 }
Chris Lattner88f47542008-10-15 04:13:29 +00001051}
1052
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001053bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
1054 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +00001055 unsigned Op0Reg = getRegForValue(Op0);
1056 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001057
Chris Lattnere388725a2008-10-15 05:18:04 +00001058 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +00001059 if (isa<ConstantPointerNull>(Op1))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001060 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +00001061
Chris Lattnerd46b9512008-10-15 04:26:38 +00001062 // We have two options: compare with register or immediate. If the RHS of
1063 // the compare is an immediate that we can fold into this compare, use
1064 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001065 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +00001066 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001067 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareImmOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001068 .addReg(Op0Reg)
1069 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +00001070 return true;
1071 }
1072 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001073
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001074 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +00001075 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001076
Chris Lattnerd46b9512008-10-15 04:26:38 +00001077 unsigned Op1Reg = getRegForValue(Op1);
1078 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001079 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001080 .addReg(Op0Reg)
1081 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001082
Chris Lattnerd46b9512008-10-15 04:26:38 +00001083 return true;
1084}
1085
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001086bool X86FastISel::X86SelectCmp(const Instruction *I) {
1087 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001088
Duncan Sandsf5dda012010-11-03 11:35:31 +00001089 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001090 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +00001091 return false;
1092
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001093 // Try to optimize or fold the cmp.
1094 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1095 unsigned ResultReg = 0;
1096 switch (Predicate) {
1097 default: break;
1098 case CmpInst::FCMP_FALSE: {
1099 ResultReg = createResultReg(&X86::GR32RegClass);
1100 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0),
1101 ResultReg);
1102 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1103 X86::sub_8bit);
1104 if (!ResultReg)
1105 return false;
1106 break;
1107 }
1108 case CmpInst::FCMP_TRUE: {
1109 ResultReg = createResultReg(&X86::GR8RegClass);
1110 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
1111 ResultReg).addImm(1);
1112 break;
1113 }
1114 }
1115
1116 if (ResultReg) {
1117 UpdateValueMap(I, ResultReg);
1118 return true;
1119 }
1120
1121 const Value *LHS = CI->getOperand(0);
1122 const Value *RHS = CI->getOperand(1);
1123
1124 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1125 // We don't have to materialize a zero constant for this case and can just use
1126 // %x again on the RHS.
1127 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1128 const auto *RHSC = dyn_cast<ConstantFP>(RHS);
1129 if (RHSC && RHSC->isNullValue())
1130 RHS = LHS;
1131 }
1132
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001133 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Juergen Ributzkae3570562014-06-17 14:47:45 +00001134 static unsigned SETFOpcTable[2][3] = {
1135 { X86::SETEr, X86::SETNPr, X86::AND8rr },
1136 { X86::SETNEr, X86::SETPr, X86::OR8rr }
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001137 };
1138 unsigned *SETFOpc = nullptr;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001139 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001140 default: break;
1141 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1142 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1143 }
1144
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001145 ResultReg = createResultReg(&X86::GR8RegClass);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001146 if (SETFOpc) {
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001147 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001148 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001149
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001150 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1151 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1152 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1153 FlagReg1);
1154 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1155 FlagReg2);
Juergen Ributzkae3570562014-06-17 14:47:45 +00001156 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001157 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
Chris Lattnera3596db2008-10-15 03:47:17 +00001158 UpdateValueMap(I, ResultReg);
1159 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001160 }
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001161
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001162 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001163 bool SwapArgs;
1164 std::tie(CC, SwapArgs) = getX86ConditonCode(Predicate);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001165 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1166 unsigned Opc = X86::getSETFromCond(CC);
Wesley Peck527da1b2010-11-23 03:31:01 +00001167
Chris Lattnerf32ce222008-10-15 03:52:54 +00001168 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001169 std::swap(LHS, RHS);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001170
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001171 // Emit a compare of LHS/RHS.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001172 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001173 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001174
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001175 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001176 UpdateValueMap(I, ResultReg);
1177 return true;
1178}
Evan Chenga41ee292008-09-03 06:44:39 +00001179
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001180bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001181 EVT DstVT = TLI.getValueType(I->getType());
1182 if (!TLI.isTypeLegal(DstVT))
1183 return false;
1184
1185 unsigned ResultReg = getRegForValue(I->getOperand(0));
1186 if (ResultReg == 0)
1187 return false;
1188
Tim Northover04eb4232013-05-30 10:43:18 +00001189 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001190 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001191 if (SrcVT.SimpleTy == MVT::i1) {
1192 // Set the high bits to zero.
1193 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1194 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001195
Tim Northover04eb4232013-05-30 10:43:18 +00001196 if (ResultReg == 0)
1197 return false;
1198 }
1199
1200 if (DstVT == MVT::i64) {
1201 // Handle extension to 64-bits via sub-register shenanigans.
1202 unsigned MovInst;
1203
1204 switch (SrcVT.SimpleTy) {
1205 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1206 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1207 case MVT::i32: MovInst = X86::MOV32rr; break;
1208 default: llvm_unreachable("Unexpected zext to i64 source type");
1209 }
1210
1211 unsigned Result32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001212 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
Tim Northover04eb4232013-05-30 10:43:18 +00001213 .addReg(ResultReg);
1214
1215 ResultReg = createResultReg(&X86::GR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001216 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
Tim Northover04eb4232013-05-30 10:43:18 +00001217 ResultReg)
1218 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1219 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001220 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1221 ResultReg, /*Kill=*/true);
1222 if (ResultReg == 0)
1223 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001224 }
1225
Eli Friedmanc7035512011-05-25 23:49:02 +00001226 UpdateValueMap(I, ResultReg);
1227 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001228}
1229
Chris Lattnerd46b9512008-10-15 04:26:38 +00001230
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001231bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001232 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001233 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001234 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001235 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1236 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001237
Dan Gohman42ef6692010-08-21 02:32:36 +00001238 // Fold the common case of a conditional branch with a comparison
1239 // in the same block (values defined on other blocks may not have
1240 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001241 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001242 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001243 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001244
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001245 // Try to optimize or fold the cmp.
1246 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1247 switch (Predicate) {
1248 default: break;
1249 case CmpInst::FCMP_FALSE: FastEmitBranch(FalseMBB, DbgLoc); return true;
1250 case CmpInst::FCMP_TRUE: FastEmitBranch(TrueMBB, DbgLoc); return true;
1251 }
1252
1253 const Value *CmpLHS = CI->getOperand(0);
1254 const Value *CmpRHS = CI->getOperand(1);
1255
1256 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x,
1257 // 0.0.
1258 // We don't have to materialize a zero constant for this case and can just
1259 // use %x again on the RHS.
1260 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1261 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1262 if (CmpRHSC && CmpRHSC->isNullValue())
1263 CmpRHS = CmpLHS;
1264 }
1265
Dan Gohman1ab1d312008-10-02 22:15:21 +00001266 // Try to take advantage of fallthrough opportunities.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001267 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001268 std::swap(TrueMBB, FalseMBB);
1269 Predicate = CmpInst::getInversePredicate(Predicate);
1270 }
1271
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001272 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/conditon
1273 // code check. Instead two branch instructions are required to check all
1274 // the flags. First we change the predicate to a supported conditon code,
1275 // which will be the first branch. Later one we will emit the second
1276 // branch.
1277 bool NeedExtraBranch = false;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001278 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001279 default: break;
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001280 case CmpInst::FCMP_OEQ:
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001281 std::swap(TrueMBB, FalseMBB); // fall-through
1282 case CmpInst::FCMP_UNE:
1283 NeedExtraBranch = true;
1284 Predicate = CmpInst::FCMP_ONE;
1285 break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001286 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001287
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001288 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001289 bool SwapArgs;
1290 unsigned BranchOpc;
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001291 std::tie(CC, SwapArgs) = getX86ConditonCode(Predicate);
1292 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1293
1294 BranchOpc = X86::GetCondBranchFromCond(CC);
Chris Lattner47bef252008-10-15 04:02:26 +00001295 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001296 std::swap(CmpLHS, CmpRHS);
Chris Lattner47bef252008-10-15 04:02:26 +00001297
Chris Lattnerd46b9512008-10-15 04:26:38 +00001298 // Emit a compare of the LHS and RHS, setting the flags.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001299 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT))
Chris Lattnerd46b9512008-10-15 04:26:38 +00001300 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001301
Rafael Espindolaea09c592014-02-18 22:05:46 +00001302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001303 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001304
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001305 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1306 // to UNE above).
1307 if (NeedExtraBranch) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001309 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001310 }
1311
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001312 // Obtain the branch weight and add the TrueBB to the successor list.
Juergen Ributzka454d3742014-06-13 00:45:11 +00001313 uint32_t BranchWeight = 0;
1314 if (FuncInfo.BPI)
1315 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1316 TrueMBB->getBasicBlock());
1317 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001318
1319 // Emits an unconditional branch to the FalseBB, obtains the branch
Alp Toker1d099d92014-06-19 19:41:26 +00001320 // weight, and adds it to the successor list.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001321 FastEmitBranch(FalseMBB, DbgLoc);
1322
Dan Gohman1ab1d312008-10-02 22:15:21 +00001323 return true;
1324 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001325 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1326 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1327 // typically happen for _Bool and C++ bools.
1328 MVT SourceVT;
1329 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1330 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1331 unsigned TestOpc = 0;
1332 switch (SourceVT.SimpleTy) {
1333 default: break;
1334 case MVT::i8: TestOpc = X86::TEST8ri; break;
1335 case MVT::i16: TestOpc = X86::TEST16ri; break;
1336 case MVT::i32: TestOpc = X86::TEST32ri; break;
1337 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1338 }
1339 if (TestOpc) {
1340 unsigned OpReg = getRegForValue(TI->getOperand(0));
1341 if (OpReg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001342 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001343 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001344
Chris Lattnerc59290a2011-04-19 04:26:32 +00001345 unsigned JmpOpc = X86::JNE_4;
1346 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1347 std::swap(TrueMBB, FalseMBB);
1348 JmpOpc = X86::JE_4;
1349 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001350
Rafael Espindolaea09c592014-02-18 22:05:46 +00001351 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001352 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001353 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001354 uint32_t BranchWeight = 0;
1355 if (FuncInfo.BPI)
1356 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1357 TrueMBB->getBasicBlock());
1358 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001359 return true;
1360 }
1361 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001362 }
1363
1364 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001365 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1366 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001367 unsigned OpReg = getRegForValue(BI->getCondition());
1368 if (OpReg == 0) return false;
1369
Rafael Espindolaea09c592014-02-18 22:05:46 +00001370 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Eli Friedman0eea0292011-04-27 01:34:27 +00001371 .addReg(OpReg).addImm(1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001372 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001373 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001374 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001375 uint32_t BranchWeight = 0;
1376 if (FuncInfo.BPI)
1377 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1378 TrueMBB->getBasicBlock());
1379 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Dan Gohmana5753b32008-09-05 01:06:14 +00001380 return true;
1381}
1382
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001383bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001384 unsigned CReg = 0, OpReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001385 const TargetRegisterClass *RC = nullptr;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001386 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001387 CReg = X86::CL;
1388 RC = &X86::GR8RegClass;
1389 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001390 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1391 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1392 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001393 default: return false;
1394 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001395 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001396 CReg = X86::CX;
1397 RC = &X86::GR16RegClass;
1398 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001399 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1400 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1401 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001402 default: return false;
1403 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001404 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001405 CReg = X86::ECX;
1406 RC = &X86::GR32RegClass;
1407 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001408 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1409 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1410 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001411 default: return false;
1412 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001413 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001414 CReg = X86::RCX;
1415 RC = &X86::GR64RegClass;
1416 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001417 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1418 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1419 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001420 default: return false;
1421 }
1422 } else {
1423 return false;
1424 }
1425
Duncan Sandsf5dda012010-11-03 11:35:31 +00001426 MVT VT;
1427 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001428 return false;
1429
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001430 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1431 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001432
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001433 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1434 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001435 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001436 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001437
1438 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001439 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001440 if (CReg != X86::CL)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001441 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001442 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001443 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001444
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001445 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001447 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001448 UpdateValueMap(I, ResultReg);
1449 return true;
1450}
1451
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001452bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1453 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1454 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1455 const static bool S = true; // IsSigned
1456 const static bool U = false; // !IsSigned
1457 const static unsigned Copy = TargetOpcode::COPY;
1458 // For the X86 DIV/IDIV instruction, in most cases the dividend
1459 // (numerator) must be in a specific register pair highreg:lowreg,
1460 // producing the quotient in lowreg and the remainder in highreg.
1461 // For most data types, to set up the instruction, the dividend is
1462 // copied into lowreg, and lowreg is sign-extended or zero-extended
1463 // into highreg. The exception is i8, where the dividend is defined
1464 // as a single register rather than a register pair, and we
1465 // therefore directly sign-extend or zero-extend the dividend into
1466 // lowreg, instead of copying, and ignore the highreg.
1467 const static struct DivRemEntry {
1468 // The following portion depends only on the data type.
1469 const TargetRegisterClass *RC;
1470 unsigned LowInReg; // low part of the register pair
1471 unsigned HighInReg; // high part of the register pair
1472 // The following portion depends on both the data type and the operation.
1473 struct DivRemResult {
1474 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1475 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1476 // highreg, or copying a zero into highreg.
1477 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1478 // zero/sign-extending into lowreg for i8.
1479 unsigned DivRemResultReg; // Register containing the desired result.
1480 bool IsOpSigned; // Whether to use signed or unsigned form.
1481 } ResultTable[NumOps];
1482 } OpTable[NumTypes] = {
1483 { &X86::GR8RegClass, X86::AX, 0, {
1484 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1485 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1486 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1487 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1488 }
1489 }, // i8
1490 { &X86::GR16RegClass, X86::AX, X86::DX, {
1491 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1492 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001493 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1494 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001495 }
1496 }, // i16
1497 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1498 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1499 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1500 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1501 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1502 }
1503 }, // i32
1504 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1505 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1506 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001507 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1508 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001509 }
1510 }, // i64
1511 };
1512
1513 MVT VT;
1514 if (!isTypeLegal(I->getType(), VT))
1515 return false;
1516
1517 unsigned TypeIndex, OpIndex;
1518 switch (VT.SimpleTy) {
1519 default: return false;
1520 case MVT::i8: TypeIndex = 0; break;
1521 case MVT::i16: TypeIndex = 1; break;
1522 case MVT::i32: TypeIndex = 2; break;
1523 case MVT::i64: TypeIndex = 3;
1524 if (!Subtarget->is64Bit())
1525 return false;
1526 break;
1527 }
1528
1529 switch (I->getOpcode()) {
1530 default: llvm_unreachable("Unexpected div/rem opcode");
1531 case Instruction::SDiv: OpIndex = 0; break;
1532 case Instruction::SRem: OpIndex = 1; break;
1533 case Instruction::UDiv: OpIndex = 2; break;
1534 case Instruction::URem: OpIndex = 3; break;
1535 }
1536
1537 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1538 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1539 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1540 if (Op0Reg == 0)
1541 return false;
1542 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1543 if (Op1Reg == 0)
1544 return false;
1545
1546 // Move op0 into low-order input register.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001547 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001548 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1549 // Zero-extend or sign-extend into high-order input register.
1550 if (OpEntry.OpSignExtend) {
1551 if (OpEntry.IsOpSigned)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001552 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001553 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001554 else {
1555 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001556 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001557 TII.get(X86::MOV32r0), Zero32);
1558
1559 // Copy the zero into the appropriate sub/super/identical physical
1560 // register. Unfortunately the operations needed are not uniform enough to
1561 // fit neatly into the table above.
1562 if (VT.SimpleTy == MVT::i16) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001563 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001564 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001565 .addReg(Zero32, 0, X86::sub_16bit);
1566 } else if (VT.SimpleTy == MVT::i32) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001567 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001568 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001569 .addReg(Zero32);
1570 } else if (VT.SimpleTy == MVT::i64) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001571 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001572 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1573 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1574 }
1575 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001576 }
1577 // Generate the DIV/IDIV instruction.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001578 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001579 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001580 // For i8 remainder, we can't reference AH directly, as we'll end
1581 // up with bogus copies like %R9B = COPY %AH. Reference AX
1582 // instead to prevent AH references in a REX instruction.
1583 //
1584 // The current assumption of the fast register allocator is that isel
1585 // won't generate explicit references to the GPR8_NOREX registers. If
1586 // the allocator and/or the backend get enhanced to be more robust in
1587 // that regard, this can be, and should be, removed.
1588 unsigned ResultReg = 0;
1589 if ((I->getOpcode() == Instruction::SRem ||
1590 I->getOpcode() == Instruction::URem) &&
1591 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1592 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1593 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001594 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbachc35388f2013-07-09 02:07:25 +00001595 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1596
1597 // Shift AX right by 8 bits instead of using AH.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001598 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
Jim Grosbachc35388f2013-07-09 02:07:25 +00001599 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1600
1601 // Now reference the 8-bit subreg of the result.
1602 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1603 /*Kill=*/true, X86::sub_8bit);
1604 }
1605 // Copy the result out of the physreg if we haven't already.
1606 if (!ResultReg) {
1607 ResultReg = createResultReg(TypeEntry.RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001608 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
Jim Grosbachc35388f2013-07-09 02:07:25 +00001609 .addReg(OpEntry.DivRemResultReg);
1610 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001611 UpdateValueMap(I, ResultReg);
1612
1613 return true;
1614}
1615
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001616/// \brief Emit a conditional move instruction (if the are supported) to lower
1617/// the select.
1618bool X86FastISel::X86FastEmitCMoveSelect(const Instruction *I) {
1619 MVT RetVT;
1620 if (!isTypeLegal(I->getType(), RetVT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001621 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001622
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001623 // Check if the subtarget supports these instructions.
1624 if (!Subtarget->hasCMov())
Wesley Peck527da1b2010-11-23 03:31:01 +00001625 return false;
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001626
1627 // FIXME: Add support for i8.
1628 unsigned Opc;
1629 switch (RetVT.SimpleTy) {
1630 default: return false;
1631 case MVT::i16: Opc = X86::CMOVNE16rr; break;
1632 case MVT::i32: Opc = X86::CMOVNE32rr; break;
1633 case MVT::i64: Opc = X86::CMOVNE64rr; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001634 }
1635
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001636 const Value *Cond = I->getOperand(0);
1637 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1638 bool NeedTest = true;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001639
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001640 // Optimize conditons coming from a compare.
1641 if (const auto *CI = dyn_cast<CmpInst>(Cond)) {
1642 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1643
1644 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
1645 static unsigned SETFOpcTable[2][3] = {
1646 { X86::SETNPr, X86::SETEr , X86::TEST8rr },
1647 { X86::SETPr, X86::SETNEr, X86::OR8rr }
1648 };
1649 unsigned *SETFOpc = nullptr;
1650 switch (Predicate) {
1651 default: break;
1652 case CmpInst::FCMP_OEQ:
1653 SETFOpc = &SETFOpcTable[0][0];
1654 Predicate = CmpInst::ICMP_NE;
1655 break;
1656 case CmpInst::FCMP_UNE:
1657 SETFOpc = &SETFOpcTable[1][0];
1658 Predicate = CmpInst::ICMP_NE;
1659 break;
1660 }
1661
1662 X86::CondCode CC;
1663 bool NeedSwap;
1664 std::tie(CC, NeedSwap) = getX86ConditonCode(Predicate);
1665 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
1666 Opc = X86::getCMovFromCond(CC, RC->getSize());
1667
1668 const Value *CmpLHS = CI->getOperand(0);
1669 const Value *CmpRHS = CI->getOperand(1);
1670 if (NeedSwap)
1671 std::swap(CmpLHS, CmpRHS);
1672
1673 EVT CmpVT = TLI.getValueType(CmpLHS->getType());
1674 // Emit a compare of the LHS and RHS, setting the flags.
1675 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT))
1676 return false;
1677
1678 if (SETFOpc) {
1679 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1680 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1681 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1682 FlagReg1);
1683 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1684 FlagReg2);
1685 auto const &II = TII.get(SETFOpc[2]);
1686 if (II.getNumDefs()) {
1687 unsigned TmpReg = createResultReg(&X86::GR8RegClass);
1688 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, TmpReg)
1689 .addReg(FlagReg2).addReg(FlagReg1);
1690 } else {
1691 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
1692 .addReg(FlagReg2).addReg(FlagReg1);
1693 }
1694 }
1695 NeedTest = false;
1696 }
1697
1698 if (NeedTest) {
1699 // Selects operate on i1, however, CondReg is 8 bits width and may contain
1700 // garbage. Indeed, only the less significant bit is supposed to be
1701 // accurate. If we read more than the lsb, we may see non-zero values
1702 // whereas lsb is zero. Therefore, we have to truncate Op0Reg to i1 for
1703 // the select. This is achieved by performing TEST against 1.
1704 unsigned CondReg = getRegForValue(Cond);
1705 if (CondReg == 0)
1706 return false;
1707 bool CondIsKill = hasTrivialKill(Cond);
1708
1709 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1710 .addReg(CondReg, getKillRegState(CondIsKill)).addImm(1);
1711 }
1712
1713 const Value *LHS = I->getOperand(1);
1714 const Value *RHS = I->getOperand(2);
1715
1716 unsigned RHSReg = getRegForValue(RHS);
1717 bool RHSIsKill = hasTrivialKill(RHS);
1718
1719 unsigned LHSReg = getRegForValue(LHS);
1720 bool LHSIsKill = hasTrivialKill(LHS);
1721
1722 if (!LHSReg || !RHSReg)
1723 return false;
1724
1725 unsigned ResultReg = FastEmitInst_rr(Opc, RC, RHSReg, RHSIsKill,
1726 LHSReg, LHSIsKill);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001727 UpdateValueMap(I, ResultReg);
1728 return true;
1729}
1730
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001731bool X86FastISel::X86SelectSelect(const Instruction *I) {
1732 MVT RetVT;
1733 if (!isTypeLegal(I->getType(), RetVT))
1734 return false;
1735
1736 // Check if we can fold the select.
1737 if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) {
1738 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1739 const Value *Opnd = nullptr;
1740 switch (Predicate) {
1741 default: break;
1742 case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break;
1743 case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break;
1744 }
1745 // No need for a select anymore - this is an unconditional move.
1746 if (Opnd) {
1747 unsigned OpReg = getRegForValue(Opnd);
1748 if (OpReg == 0)
1749 return false;
1750 bool OpIsKill = hasTrivialKill(Opnd);
1751 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1752 unsigned ResultReg = createResultReg(RC);
1753 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1754 TII.get(TargetOpcode::COPY), ResultReg)
1755 .addReg(OpReg, getKillRegState(OpIsKill));
1756 UpdateValueMap(I, ResultReg);
1757 return true;
1758 }
1759 }
1760
1761 // First try to use real conditional move instructions.
1762 if (X86FastEmitCMoveSelect(I))
1763 return true;
1764
1765 return false;
1766}
1767
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001768bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001769 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001770 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001771 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001772 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001773 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001774 unsigned OpReg = getRegForValue(V);
1775 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001776 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001777 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001778 TII.get(X86::CVTSS2SDrr), ResultReg)
1779 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001780 UpdateValueMap(I, ResultReg);
1781 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001782 }
1783 }
1784
1785 return false;
1786}
1787
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001788bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001789 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001790 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001791 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001792 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001793 unsigned OpReg = getRegForValue(V);
1794 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001795 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001796 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001797 TII.get(X86::CVTSD2SSrr), ResultReg)
1798 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001799 UpdateValueMap(I, ResultReg);
1800 return true;
1801 }
1802 }
1803 }
1804
1805 return false;
1806}
1807
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001808bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001809 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1810 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001811
Eli Friedmanc7035512011-05-25 23:49:02 +00001812 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001813 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001814 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001815 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001816 return false;
1817
1818 unsigned InputReg = getRegForValue(I->getOperand(0));
1819 if (!InputReg)
1820 // Unhandled operand. Halt "fast" selection and bail.
1821 return false;
1822
Eli Friedmanc7035512011-05-25 23:49:02 +00001823 if (SrcVT == MVT::i8) {
1824 // Truncate from i8 to i1; no code needed.
1825 UpdateValueMap(I, InputReg);
1826 return true;
1827 }
Evan Chengb9286692008-09-07 08:47:42 +00001828
Eli Friedmanc7035512011-05-25 23:49:02 +00001829 if (!Subtarget->is64Bit()) {
1830 // If we're on x86-32; we can't extract an i8 from a general register.
1831 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001832 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1833 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1834 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001835 unsigned CopyReg = createResultReg(CopyRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001836 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Eli Friedmanc7035512011-05-25 23:49:02 +00001837 CopyReg).addReg(InputReg);
1838 InputReg = CopyReg;
1839 }
1840
1841 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001842 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001843 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001844 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001845 if (!ResultReg)
1846 return false;
1847
1848 UpdateValueMap(I, ResultReg);
1849 return true;
1850}
1851
Eli Friedman60afcc22011-05-20 22:21:04 +00001852bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1853 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1854}
1855
Eli Friedmanbcc69142011-04-27 01:45:07 +00001856bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1857 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001858
Eli Friedmanbcc69142011-04-27 01:45:07 +00001859 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001860 if (!IsMemcpySmall(Len))
1861 return false;
1862
1863 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001864
1865 // We don't care about alignment here since we just emit integer accesses.
1866 while (Len) {
1867 MVT VT;
1868 if (Len >= 8 && i64Legal)
1869 VT = MVT::i64;
1870 else if (Len >= 4)
1871 VT = MVT::i32;
1872 else if (Len >= 2)
1873 VT = MVT::i16;
1874 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001875 VT = MVT::i8;
1876 }
1877
1878 unsigned Reg;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001879 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
1880 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
Eli Friedmanbcc69142011-04-27 01:45:07 +00001881 assert(RV && "Failed to emit load or store??");
1882
1883 unsigned Size = VT.getSizeInBits()/8;
1884 Len -= Size;
1885 DestAM.Disp += Size;
1886 SrcAM.Disp += Size;
1887 }
1888
1889 return true;
1890}
1891
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001892static bool isCommutativeIntrinsic(IntrinsicInst const &I) {
1893 switch (I.getIntrinsicID()) {
1894 case Intrinsic::sadd_with_overflow:
1895 case Intrinsic::uadd_with_overflow:
1896 case Intrinsic::smul_with_overflow:
1897 case Intrinsic::umul_with_overflow:
1898 return true;
1899 default:
1900 return false;
1901 }
1902}
1903
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001904bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001905 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001906 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001907 default: return false;
Juergen Ributzka4dc95872014-06-11 21:44:44 +00001908 case Intrinsic::frameaddress: {
1909 Type *RetTy = I.getCalledFunction()->getReturnType();
1910
1911 MVT VT;
1912 if (!isTypeLegal(RetTy, VT))
1913 return false;
1914
1915 unsigned Opc;
1916 const TargetRegisterClass *RC = nullptr;
1917
1918 switch (VT.SimpleTy) {
1919 default: llvm_unreachable("Invalid result type for frameaddress.");
1920 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
1921 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
1922 }
1923
1924 // This needs to be set before we call getFrameRegister, otherwise we get
1925 // the wrong frame register.
1926 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
1927 MFI->setFrameAddressIsTaken(true);
1928
1929 const X86RegisterInfo *RegInfo =
1930 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
1931 unsigned FrameReg = RegInfo->getFrameRegister(*(FuncInfo.MF));
1932 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
1933 (FrameReg == X86::EBP && VT == MVT::i32)) &&
1934 "Invalid Frame Register!");
1935
1936 // Always make a copy of the frame register to to a vreg first, so that we
1937 // never directly reference the frame register (the TwoAddressInstruction-
1938 // Pass doesn't like that).
1939 unsigned SrcReg = createResultReg(RC);
1940 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1941 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
1942
1943 // Now recursively load from the frame address.
1944 // movq (%rbp), %rax
1945 // movq (%rax), %rax
1946 // movq (%rax), %rax
1947 // ...
1948 unsigned DestReg;
1949 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
1950 while (Depth--) {
1951 DestReg = createResultReg(RC);
1952 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1953 TII.get(Opc), DestReg), SrcReg);
1954 SrcReg = DestReg;
1955 }
1956
1957 UpdateValueMap(&I, SrcReg);
1958 return true;
1959 }
Chris Lattner91328b32011-04-19 05:52:03 +00001960 case Intrinsic::memcpy: {
1961 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1962 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001963 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001964 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001965
Eli Friedmancd2124a2011-06-10 23:39:36 +00001966 if (isa<ConstantInt>(MCI.getLength())) {
1967 // Small memcpy's are common enough that we want to do them
1968 // without a call if possible.
1969 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1970 if (IsMemcpySmall(Len)) {
1971 X86AddressMode DestAM, SrcAM;
1972 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1973 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1974 return false;
1975 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1976 return true;
1977 }
1978 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001979
Eli Friedmancd2124a2011-06-10 23:39:36 +00001980 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1981 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001982 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001983
Eli Friedmancd2124a2011-06-10 23:39:36 +00001984 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1985 return false;
1986
1987 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001988 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001989 case Intrinsic::memset: {
1990 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001991
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001992 if (MSI.isVolatile())
1993 return false;
1994
Eli Friedmancd2124a2011-06-10 23:39:36 +00001995 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1996 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1997 return false;
1998
1999 if (MSI.getDestAddressSpace() > 255)
2000 return false;
2001
2002 return DoSelectCall(&I, "memset");
2003 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002004 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00002005 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002006 EVT PtrTy = TLI.getPointerTy();
2007
Gabor Greif83205af2010-06-26 11:51:52 +00002008 const Value *Op1 = I.getArgOperand(0); // The guard's value.
2009 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002010
Josh Magee22b8ba22013-12-19 03:17:11 +00002011 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
2012
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002013 // Grab the frame index.
2014 X86AddressMode AM;
2015 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00002016 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002017 return true;
2018 }
Dale Johannesend5575f22010-01-26 00:09:58 +00002019 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002020 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00002021 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00002022 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00002023 if (!X86SelectAddress(DI->getAddress(), AM))
2024 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00002025 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00002026 // FIXME may need to add RegState::Debug to any registers produced,
2027 // although ESP/EBP should be the only ones at the moment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002028 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002029 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00002030 return true;
2031 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002032 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002033 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002034 return true;
2035 }
Juergen Ributzka272b5702014-06-11 23:11:02 +00002036 case Intrinsic::sqrt: {
2037 if (!Subtarget->hasSSE1())
2038 return false;
2039
2040 Type *RetTy = I.getCalledFunction()->getReturnType();
2041
2042 MVT VT;
2043 if (!isTypeLegal(RetTy, VT))
2044 return false;
2045
2046 // Unfortunatelly we can't use FastEmit_r, because the AVX version of FSQRT
2047 // is not generated by FastISel yet.
2048 // FIXME: Update this code once tablegen can handle it.
2049 static const unsigned SqrtOpc[2][2] = {
2050 {X86::SQRTSSr, X86::VSQRTSSr},
2051 {X86::SQRTSDr, X86::VSQRTSDr}
2052 };
2053 bool HasAVX = Subtarget->hasAVX();
2054 unsigned Opc;
2055 const TargetRegisterClass *RC;
2056 switch (VT.SimpleTy) {
2057 default: return false;
2058 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
2059 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
2060 }
2061
2062 const Value *SrcVal = I.getArgOperand(0);
2063 unsigned SrcReg = getRegForValue(SrcVal);
2064
2065 if (SrcReg == 0)
2066 return false;
2067
2068 unsigned ImplicitDefReg = 0;
2069 if (HasAVX) {
2070 ImplicitDefReg = createResultReg(RC);
2071 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2072 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2073 }
2074
2075 unsigned ResultReg = createResultReg(RC);
2076 MachineInstrBuilder MIB;
2077 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
2078 ResultReg);
2079
2080 if (ImplicitDefReg)
2081 MIB.addReg(ImplicitDefReg);
2082
2083 MIB.addReg(SrcReg);
2084
2085 UpdateValueMap(&I, ResultReg);
2086 return true;
2087 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002088 case Intrinsic::sadd_with_overflow:
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002089 case Intrinsic::uadd_with_overflow:
2090 case Intrinsic::ssub_with_overflow:
2091 case Intrinsic::usub_with_overflow:
2092 case Intrinsic::smul_with_overflow:
2093 case Intrinsic::umul_with_overflow: {
2094 // This implements the basic lowering of the xalu with overflow intrinsics
2095 // into add/sub/mul folowed by either seto or setb.
Bill Wendling80b34b32008-12-09 02:42:50 +00002096 const Function *Callee = I.getCalledFunction();
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002097 auto *Ty = cast<StructType>(Callee->getReturnType());
2098 Type *RetTy = Ty->getTypeAtIndex(0U);
2099 Type *CondTy = Ty->getTypeAtIndex(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00002100
Duncan Sandsf5dda012010-11-03 11:35:31 +00002101 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00002102 if (!isTypeLegal(RetTy, VT))
2103 return false;
2104
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002105 if (VT < MVT::i8 || VT > MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00002106 return false;
2107
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002108 const Value *LHS = I.getArgOperand(0);
2109 const Value *RHS = I.getArgOperand(1);
2110
2111 // Canonicalize immediates to the RHS.
2112 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2113 isCommutativeIntrinsic(I))
2114 std::swap(LHS, RHS);
2115
2116 unsigned BaseOpc, CondOpc;
2117 switch (I.getIntrinsicID()) {
2118 default: llvm_unreachable("Unexpected intrinsic!");
2119 case Intrinsic::sadd_with_overflow:
2120 BaseOpc = ISD::ADD; CondOpc = X86::SETOr; break;
2121 case Intrinsic::uadd_with_overflow:
2122 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
2123 case Intrinsic::ssub_with_overflow:
2124 BaseOpc = ISD::SUB; CondOpc = X86::SETOr; break;
2125 case Intrinsic::usub_with_overflow:
2126 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
2127 case Intrinsic::smul_with_overflow:
2128 BaseOpc = ISD::MUL; CondOpc = X86::SETOr; break;
2129 case Intrinsic::umul_with_overflow:
2130 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
2131 }
2132
2133 unsigned LHSReg = getRegForValue(LHS);
2134 if (LHSReg == 0)
2135 return false;
2136 bool LHSIsKill = hasTrivialKill(LHS);
2137
2138 unsigned ResultReg = 0;
2139 // Check if we have an immediate version.
2140 if (auto const *C = dyn_cast<ConstantInt>(RHS)) {
2141 ResultReg = FastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2142 C->getZExtValue());
2143 }
2144
2145 unsigned RHSReg;
2146 bool RHSIsKill;
2147 if (!ResultReg) {
2148 RHSReg = getRegForValue(RHS);
2149 if (RHSReg == 0)
2150 return false;
2151 RHSIsKill = hasTrivialKill(RHS);
2152 ResultReg = FastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2153 RHSIsKill);
2154 }
2155
2156 // FastISel doesn't have a pattern for X86::MUL*r. Emit it manually.
2157 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
2158 static const unsigned MULOpc[] =
2159 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
2160 static const unsigned Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
2161 // First copy the first operand into RAX, which is an implicit input to
2162 // the X86::MUL*r instruction.
2163 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2164 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2165 .addReg(LHSReg, getKillRegState(LHSIsKill));
2166 ResultReg = FastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2167 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
2168 }
2169
2170 if (!ResultReg)
Bill Wendling80b34b32008-12-09 02:42:50 +00002171 return false;
2172
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002173 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
2174 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
2175 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
2176 ResultReg2);
Eli Friedmana4d4a012011-05-16 21:06:17 +00002177
2178 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00002179 return true;
2180 }
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002181 case Intrinsic::x86_sse_cvttss2si:
2182 case Intrinsic::x86_sse_cvttss2si64:
2183 case Intrinsic::x86_sse2_cvttsd2si:
2184 case Intrinsic::x86_sse2_cvttsd2si64: {
2185 bool IsInputDouble;
2186 switch (I.getIntrinsicID()) {
2187 default: llvm_unreachable("Unexpected intrinsic.");
2188 case Intrinsic::x86_sse_cvttss2si:
2189 case Intrinsic::x86_sse_cvttss2si64:
2190 if (!Subtarget->hasSSE1())
2191 return false;
2192 IsInputDouble = false;
2193 break;
2194 case Intrinsic::x86_sse2_cvttsd2si:
2195 case Intrinsic::x86_sse2_cvttsd2si64:
2196 if (!Subtarget->hasSSE2())
2197 return false;
2198 IsInputDouble = true;
2199 break;
2200 }
2201
2202 Type *RetTy = I.getCalledFunction()->getReturnType();
2203 MVT VT;
2204 if (!isTypeLegal(RetTy, VT))
2205 return false;
2206
2207 static const unsigned CvtOpc[2][2][2] = {
2208 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
2209 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
2210 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
2211 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
2212 };
2213 bool HasAVX = Subtarget->hasAVX();
2214 unsigned Opc;
2215 switch (VT.SimpleTy) {
2216 default: llvm_unreachable("Unexpected result type.");
2217 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
2218 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
2219 }
2220
2221 // Check if we can fold insertelement instructions into the convert.
2222 const Value *Op = I.getArgOperand(0);
2223 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
2224 const Value *Index = IE->getOperand(2);
2225 if (!isa<ConstantInt>(Index))
2226 break;
2227 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
2228
2229 if (Idx == 0) {
2230 Op = IE->getOperand(1);
2231 break;
2232 }
2233 Op = IE->getOperand(0);
2234 }
2235
2236 unsigned Reg = getRegForValue(Op);
2237 if (Reg == 0)
2238 return false;
2239
2240 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
2241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2242 .addReg(Reg);
2243
2244 UpdateValueMap(&I, ResultReg);
2245 return true;
2246 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002247 }
2248}
2249
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002250bool X86FastISel::FastLowerArguments() {
2251 if (!FuncInfo.CanLowerReturn)
2252 return false;
2253
2254 const Function *F = FuncInfo.Fn;
2255 if (F->isVarArg())
2256 return false;
2257
2258 CallingConv::ID CC = F->getCallingConv();
2259 if (CC != CallingConv::C)
2260 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00002261
2262 if (Subtarget->isCallingConvWin64(CC))
2263 return false;
2264
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002265 if (!Subtarget->is64Bit())
2266 return false;
2267
2268 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002269 unsigned GPRCnt = 0;
2270 unsigned FPRCnt = 0;
2271 unsigned Idx = 0;
2272 for (auto const &Arg : F->args()) {
2273 // The first argument is at index 1.
2274 ++Idx;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002275 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2276 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2277 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2278 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2279 return false;
2280
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002281 Type *ArgTy = Arg.getType();
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002282 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
2283 return false;
2284
2285 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00002286 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002287 switch (ArgVT.getSimpleVT().SimpleTy) {
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002288 default: return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002289 case MVT::i32:
2290 case MVT::i64:
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002291 ++GPRCnt;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002292 break;
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002293 case MVT::f32:
2294 case MVT::f64:
2295 if (!Subtarget->hasSSE1())
2296 return false;
2297 ++FPRCnt;
2298 break;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002299 }
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002300
2301 if (GPRCnt > 6)
2302 return false;
2303
2304 if (FPRCnt > 8)
2305 return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002306 }
2307
Craig Topper840beec2014-04-04 05:16:06 +00002308 static const MCPhysReg GPR32ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002309 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
2310 };
Craig Topper840beec2014-04-04 05:16:06 +00002311 static const MCPhysReg GPR64ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002312 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
2313 };
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002314 static const MCPhysReg XMMArgRegs[] = {
2315 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2316 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2317 };
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002318
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002319 unsigned GPRIdx = 0;
2320 unsigned FPRIdx = 0;
2321 for (auto const &Arg : F->args()) {
2322 MVT VT = TLI.getSimpleValueType(Arg.getType());
2323 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
2324 unsigned SrcReg;
2325 switch (VT.SimpleTy) {
2326 default: llvm_unreachable("Unexpected value type.");
2327 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
2328 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
2329 case MVT::f32: // fall-through
2330 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
2331 }
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002332 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2333 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2334 // Without this, EmitLiveInCopies may eliminate the livein if its only
2335 // use is a bitcast (which isn't turned into an instruction).
2336 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002337 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002338 TII.get(TargetOpcode::COPY), ResultReg)
2339 .addReg(DstReg, getKillRegState(true));
2340 UpdateValueMap(&Arg, ResultReg);
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002341 }
2342 return true;
2343}
2344
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002345bool X86FastISel::X86SelectCall(const Instruction *I) {
2346 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00002347 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002348
2349 // Can't handle inline asm yet.
2350 if (isa<InlineAsm>(Callee))
2351 return false;
2352
Bill Wendling80b34b32008-12-09 02:42:50 +00002353 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002354 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00002355 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002356
Chad Rosierdf42cf32012-12-11 00:18:02 +00002357 // Allow SelectionDAG isel to handle tail calls.
2358 if (cast<CallInst>(I)->isTailCall())
2359 return false;
2360
Craig Topper062a2ba2014-04-25 05:30:21 +00002361 return DoSelectCall(I, nullptr);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002362}
2363
Rafael Espindola73173c52012-07-25 15:42:45 +00002364static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
2365 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002366 if (Subtarget.is64Bit())
2367 return 0;
Rafael Espindola32cb5ac2013-12-12 16:06:58 +00002368 if (Subtarget.getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002369 return 0;
2370 CallingConv::ID CC = CS.getCallingConv();
2371 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
2372 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002373 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002374 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002375 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00002376 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002377 return 4;
2378}
2379
Eli Friedmancd2124a2011-06-10 23:39:36 +00002380// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
2381bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
2382 const CallInst *CI = cast<CallInst>(I);
2383 const Value *Callee = CI->getCalledValue();
2384
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002385 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002386 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002387 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00002388 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002389 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00002390 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
2391 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002392 return false;
2393
Evan Chengd10089a2010-01-27 00:00:57 +00002394 // fastcc with -tailcallopt is intended to provide a guaranteed
2395 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002396 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00002397 return false;
2398
Chris Lattner229907c2011-07-18 04:54:35 +00002399 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2400 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002401 bool isVarArg = FTy->isVarArg();
2402
2403 // Don't know how to handle Win64 varargs yet. Nothing special needed for
2404 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00002405 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002406 return false;
2407
Reid Klecknerf5b76512014-01-31 23:50:57 +00002408 // Don't know about inalloca yet.
2409 if (CS.hasInAllocaArgument())
2410 return false;
2411
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002412 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00002413 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002414 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002415 return false;
2416
Eli Friedman7b279422011-05-17 18:29:03 +00002417 // Check whether the function can return without sret-demotion.
2418 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002419 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00002420 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00002421 *FuncInfo.MF, FTy->isVarArg(),
2422 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002423 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00002424 return false;
2425
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002426 // Materialize callee address in a register. FIXME: GV address can be
2427 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00002428 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00002429 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00002430 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002431 unsigned CalleeOp = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002432 const GlobalValue *GV = nullptr;
2433 if (CalleeAM.GV != nullptr) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002434 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00002435 } else if (CalleeAM.Base.Reg != 0) {
2436 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002437 } else
2438 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002439
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002440 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002441 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002442 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002443 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002444 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00002445 unsigned arg_size = CS.arg_size();
2446 Args.reserve(arg_size);
2447 ArgVals.reserve(arg_size);
2448 ArgVTs.reserve(arg_size);
2449 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002450 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002451 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00002452 // If we're lowering a mem intrinsic instead of a regular call, skip the
2453 // last two arguments, which should not passed to the underlying functions.
2454 if (MemIntName && e-i <= 2)
2455 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002456 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002457 ISD::ArgFlagsTy Flags;
2458 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002459 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002460 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002461 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002462 Flags.setZExt();
2463
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002464 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00002465 PointerType *Ty = cast<PointerType>(ArgVal->getType());
2466 Type *ElementTy = Ty->getElementType();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002467 unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
Eli Friedman60afcc22011-05-20 22:21:04 +00002468 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
2469 if (!FrameAlign)
2470 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
2471 Flags.setByVal();
2472 Flags.setByValSize(FrameSize);
2473 Flags.setByValAlign(FrameAlign);
2474 if (!IsMemcpySmall(FrameSize))
2475 return false;
2476 }
2477
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002478 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00002479 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002480 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00002481 Flags.setNest();
2482
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002483 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
2484 // instruction. This is safe because it is common to all fastisel supported
2485 // calling conventions on x86.
2486 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
2487 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
2488 CI->getBitWidth() == 16) {
2489 if (Flags.isSExt())
2490 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
2491 else
2492 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
2493 }
2494 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002495
Chris Lattner5f4b7832011-04-19 05:09:50 +00002496 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002497
Chris Lattner34a08c22011-04-19 05:15:59 +00002498 // Passing bools around ends up doing a trunc to i1 and passing it.
2499 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00002500 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2501 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2502 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002503 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2504 ArgReg = getRegForValue(ArgVal);
2505 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002506
Chris Lattner5f4b7832011-04-19 05:09:50 +00002507 MVT ArgVT;
2508 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002509
Chris Lattner5f4b7832011-04-19 05:09:50 +00002510 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2511 ArgVal->hasOneUse(), 1);
2512 } else {
2513 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002514 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002515
Chris Lattner34a08c22011-04-19 05:15:59 +00002516 if (ArgReg == 0) return false;
2517
Chris Lattner229907c2011-07-18 04:54:35 +00002518 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002519 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002520 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002521 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002522 if (ArgVT == MVT::x86mmx)
2523 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002524 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002525 Flags.setOrigAlign(OriginalAlignment);
2526
Chris Lattner5f4b7832011-04-19 05:09:50 +00002527 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002528 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002529 ArgVTs.push_back(ArgVT);
2530 ArgFlags.push_back(Flags);
2531 }
2532
2533 // Analyze operands of the call, assigning locations to each operand.
2534 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002535 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002536 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002537
Dan Gohman47a07242010-06-01 21:09:47 +00002538 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002539 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002540 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002541
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002542 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002543
2544 // Get a count of how many bytes are to be pushed on the stack.
2545 unsigned NumBytes = CCInfo.getNextStackOffset();
2546
2547 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002548 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002549 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002550 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002551
Chris Lattner3ba29352008-10-15 05:30:52 +00002552 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002553 // copies / loads.
2554 SmallVector<unsigned, 4> RegArgs;
2555 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2556 CCValAssign &VA = ArgLocs[i];
2557 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002558 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002559
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002560 // Promote the value if needed.
2561 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002562 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002563 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002564 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2565 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002566 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2567 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002568 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002569 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002570 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002571 }
2572 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002573 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2574 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002575 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2576 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002577 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002578 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002579 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002580 }
2581 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002582 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2583 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002584 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2585 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002586 if (!Emitted)
2587 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002588 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002589 if (!Emitted)
2590 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2591 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002592
Chris Lattner2d7df022011-01-05 22:26:52 +00002593 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002594 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002595 break;
2596 }
Dan Gohman8c795692009-08-05 05:33:42 +00002597 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002598 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002599 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002600 assert(BC != 0 && "Failed to emit a bitcast!");
2601 Arg = BC;
2602 ArgVT = VA.getLocVT();
2603 break;
2604 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002605 case CCValAssign::VExt:
2606 // VExt has not been implemented, so this should be impossible to reach
2607 // for now. However, fallback to Selection DAG isel once implemented.
2608 return false;
2609 case CCValAssign::Indirect:
2610 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2611 // support this.
2612 return false;
Lang Hames06234ec2014-01-14 19:56:36 +00002613 case CCValAssign::FPExt:
2614 llvm_unreachable("Unexpected loc info!");
Evan Cheng6500d172008-09-08 06:35:17 +00002615 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002616
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002617 if (VA.isRegLoc()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002618 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2619 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002620 RegArgs.push_back(VA.getLocReg());
2621 } else {
2622 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002623 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002624 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2625 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002626 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002627 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002628 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002629 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002630
Eli Friedman60afcc22011-05-20 22:21:04 +00002631 if (Flags.isByVal()) {
2632 X86AddressMode SrcAM;
2633 SrcAM.Base.Reg = Arg;
2634 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2635 assert(Res && "memcpy length already checked!"); (void)Res;
2636 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2637 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002638 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002639 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002640 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2641 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002642 } else {
Juergen Ributzka349777d2014-06-12 23:27:57 +00002643 if (!X86FastEmitStore(ArgVT, Arg, /*ValIsKill=*/false, AM))
Lang Hames7d2f7b52011-10-18 22:11:33 +00002644 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002645 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002646 }
2647 }
2648
Dan Gohman3691d502008-09-25 15:24:26 +00002649 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002650 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002651 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002652 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002653 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2654 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002655 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002656
Charles Davise8f297c2013-07-12 06:02:35 +00002657 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002658 // Count the number of XMM registers allocated.
Craig Topper840beec2014-04-04 05:16:06 +00002659 static const MCPhysReg XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002660 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2661 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2662 };
2663 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002664 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002665 X86::AL).addImm(NumXMMRegs);
2666 }
2667
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002668 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002669 MachineInstrBuilder MIB;
2670 if (CalleeOp) {
2671 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002672 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002673 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002674 CallOpc = X86::CALL64r;
2675 else
2676 CallOpc = X86::CALL32r;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002677 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002678 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002679
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002680 } else {
2681 // Direct call.
2682 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002683 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002684 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002685 CallOpc = X86::CALL64pcrel32;
2686 else
2687 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002688
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002689 // See if we need any target-specific flags on the GV operand.
2690 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002691
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002692 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2693 // external symbols most go through the PLT in PIC mode. If the symbol
2694 // has hidden or protected visibility, or if it is static or local, then
2695 // we don't need to use the PLT - we can directly call it.
2696 if (Subtarget->isTargetELF() &&
2697 TM.getRelocationModel() == Reloc::PIC_ &&
2698 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2699 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002700 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002701 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002702 (!Subtarget->getTargetTriple().isMacOSX() ||
2703 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002704 // PC-relative references to external symbols should go through $stub,
2705 // unless we're building with the leopard linker or later, which
2706 // automatically synthesizes these stubs.
2707 OpFlags = X86II::MO_DARWIN_STUB;
2708 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002709
2710
Rafael Espindolaea09c592014-02-18 22:05:46 +00002711 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Eli Friedmancd2124a2011-06-10 23:39:36 +00002712 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002713 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002714 else
2715 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002716 }
Dan Gohman3691d502008-09-25 15:24:26 +00002717
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002718 // Add a register mask with the call-preserved registers.
2719 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2720 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2721
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002722 // Add an implicit use GOT pointer in EBX.
2723 if (Subtarget->isPICStyleGOT())
2724 MIB.addReg(X86::EBX, RegState::Implicit);
2725
Charles Davise8f297c2013-07-12 06:02:35 +00002726 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002727 MIB.addReg(X86::AL, RegState::Implicit);
2728
2729 // Add implicit physical register uses to the call.
2730 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2731 MIB.addReg(RegArgs[i], RegState::Implicit);
2732
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002733 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002734 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002735 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002736 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002737 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002738
Eli Friedman7b279422011-05-17 18:29:03 +00002739 // Build info for return calling conv lowering code.
2740 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2741 SmallVector<ISD::InputArg, 32> Ins;
2742 SmallVector<EVT, 4> RetTys;
2743 ComputeValueVTs(TLI, I->getType(), RetTys);
2744 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2745 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002746 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002747 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2748 for (unsigned j = 0; j != NumRegs; ++j) {
2749 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002750 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002751 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002752 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002753 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002754 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002755 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002756 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002757 MyFlags.Flags.setInReg();
2758 Ins.push_back(MyFlags);
2759 }
2760 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002761
Eli Friedman7b279422011-05-17 18:29:03 +00002762 // Now handle call return values.
2763 SmallVector<unsigned, 4> UsedRegs;
2764 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002765 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002766 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002767 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2768 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2769 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2770 EVT CopyVT = RVLocs[i].getValVT();
2771 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002772
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002773 // If this is a call to a function that returns an fp value on the x87 fp
2774 // stack, but where we prefer to use the value in xmm registers, copy it
2775 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002776 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002777 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002778 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002779 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002780 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002781 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00002782 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2783 TII.get(X86::FpPOP_RETVAL), CopyReg);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002784 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002785 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2786 TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002787 CopyReg).addReg(RVLocs[i].getLocReg());
2788 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002789 }
2790
Eli Friedman7b279422011-05-17 18:29:03 +00002791 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002792 // Round the F80 the right size, which also moves to the appropriate xmm
2793 // register. This is accomplished by storing the F80 value in memory and
2794 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002795 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002796 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002797 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002798 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002799 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002800 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002801 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002802 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002803 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Friedman7b279422011-05-17 18:29:03 +00002804 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002805 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002806 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002807
Eli Friedman7b279422011-05-17 18:29:03 +00002808 if (RVLocs.size())
2809 UpdateValueMap(I, ResultReg, RVLocs.size());
2810
Dan Gohman86936502010-06-18 23:28:01 +00002811 // Set all unused physreg defs as dead.
2812 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2813
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002814 return true;
2815}
2816
2817
Dan Gohmand58f3e32008-08-28 23:21:34 +00002818bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002819X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002820 switch (I->getOpcode()) {
2821 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002822 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002823 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002824 case Instruction::Store:
2825 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002826 case Instruction::Ret:
2827 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002828 case Instruction::ICmp:
2829 case Instruction::FCmp:
2830 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002831 case Instruction::ZExt:
2832 return X86SelectZExt(I);
2833 case Instruction::Br:
2834 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002835 case Instruction::Call:
2836 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002837 case Instruction::LShr:
2838 case Instruction::AShr:
2839 case Instruction::Shl:
2840 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002841 case Instruction::SDiv:
2842 case Instruction::UDiv:
2843 case Instruction::SRem:
2844 case Instruction::URem:
2845 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002846 case Instruction::Select:
2847 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002848 case Instruction::Trunc:
2849 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002850 case Instruction::FPExt:
2851 return X86SelectFPExt(I);
2852 case Instruction::FPTrunc:
2853 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002854 case Instruction::IntToPtr: // Deliberate fall-through.
2855 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002856 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2857 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002858 if (DstVT.bitsGT(SrcVT))
2859 return X86SelectZExt(I);
2860 if (DstVT.bitsLT(SrcVT))
2861 return X86SelectTrunc(I);
2862 unsigned Reg = getRegForValue(I->getOperand(0));
2863 if (Reg == 0) return false;
2864 UpdateValueMap(I, Reg);
2865 return true;
2866 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002867 }
2868
2869 return false;
2870}
2871
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002872unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002873 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002874 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002875 return 0;
2876
2877 // Can't handle alternate code models yet.
2878 if (TM.getCodeModel() != CodeModel::Small)
2879 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002880
Owen Anderson50288e32008-09-05 00:06:23 +00002881 // Get opcode and regclass of the output for the given load instruction.
2882 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002883 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002884 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002885 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002886 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002887 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002888 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002889 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002890 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002891 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002892 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002893 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002894 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002895 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002896 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002897 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002898 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002899 // Must be in x86-64 mode.
2900 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002901 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002902 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002903 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002904 if (X86ScalarSSEf32) {
2905 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002906 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002907 } else {
2908 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002909 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002910 }
2911 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002912 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002913 if (X86ScalarSSEf64) {
2914 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002915 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002916 } else {
2917 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002918 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002919 }
2920 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002921 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002922 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002923 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002924 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002925
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00002926 // Materialize addresses with LEA/MOV instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002927 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002928 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002929 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002930 // If the expression is just a basereg, then we're done, otherwise we need
2931 // to emit an LEA.
2932 if (AM.BaseType == X86AddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00002933 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
Chris Lattner48326602011-04-17 17:12:08 +00002934 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002935
Dan Gohman9801ba42008-09-19 22:16:54 +00002936 unsigned ResultReg = createResultReg(RC);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00002937 if (TM.getRelocationModel() == Reloc::Static &&
2938 TLI.getPointerTy() == MVT::i64) {
2939 // The displacement code be more than 32 bits away so we need to use
2940 // an instruction with a 64 bit immediate
2941 Opc = X86::MOV64ri;
2942 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2943 TII.get(Opc), ResultReg).addGlobalAddress(cast<GlobalValue>(C));
2944 } else {
2945 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
2946 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002947 TII.get(Opc), ResultReg), AM);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00002948 }
Owen Anderson50288e32008-09-05 00:06:23 +00002949 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002950 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002951 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002952 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002953
Owen Andersond41c7162008-09-06 01:11:01 +00002954 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002955 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002956 if (Align == 0) {
2957 // Alignment of vector types. FIXME!
Rafael Espindolaea09c592014-02-18 22:05:46 +00002958 Align = DL.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002959 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002960
Dan Gohman8392f0c2008-09-30 01:21:32 +00002961 // x86-32 PIC requires a PIC base register for constant pools.
2962 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002963 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002964 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002965 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002966 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002967 } else if (Subtarget->isPICStyleGOT()) {
2968 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002969 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002970 } else if (Subtarget->isPICStyleRIPRel() &&
2971 TM.getCodeModel() == CodeModel::Small) {
2972 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002973 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002974
2975 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002976 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002977 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002978 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002979 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002980 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002981
Owen Anderson50288e32008-09-05 00:06:23 +00002982 return ResultReg;
2983}
2984
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002985unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002986 // Fail on dynamic allocas. At this point, getRegForValue has already
2987 // checked its CSE maps, so if we're here trying to handle a dynamic
2988 // alloca, we're not going to succeed. X86SelectAddress has a
2989 // check for dynamic allocas, because it's called directly from
2990 // various places, but TargetMaterializeAlloca also needs a check
2991 // in order to avoid recursion between getRegForValue,
2992 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002993 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002994 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00002995 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002996
Dan Gohman39d82f92008-09-10 20:11:02 +00002997 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002998 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002999 return 0;
3000 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00003001 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00003002 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003003 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003004 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00003005 return ResultReg;
3006}
3007
Eli Friedman406c4712011-04-27 22:41:55 +00003008unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
3009 MVT VT;
3010 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003011 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003012
3013 // Get opcode and regclass for the given zero.
3014 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003015 const TargetRegisterClass *RC = nullptr;
Eli Friedman406c4712011-04-27 22:41:55 +00003016 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003017 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00003018 case MVT::f32:
3019 if (X86ScalarSSEf32) {
3020 Opc = X86::FsFLD0SS;
3021 RC = &X86::FR32RegClass;
3022 } else {
3023 Opc = X86::LD_Fp032;
3024 RC = &X86::RFP32RegClass;
3025 }
3026 break;
3027 case MVT::f64:
3028 if (X86ScalarSSEf64) {
3029 Opc = X86::FsFLD0SD;
3030 RC = &X86::FR64RegClass;
3031 } else {
3032 Opc = X86::LD_Fp064;
3033 RC = &X86::RFP64RegClass;
3034 }
3035 break;
3036 case MVT::f80:
3037 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003038 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003039 }
3040
3041 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003042 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Eli Friedman406c4712011-04-27 22:41:55 +00003043 return ResultReg;
3044}
3045
3046
Eli Bendersky90dd3e72013-04-19 22:29:18 +00003047bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3048 const LoadInst *LI) {
Juergen Ributzka349777d2014-06-12 23:27:57 +00003049 const Value *Ptr = LI->getPointerOperand();
Chris Lattnereeba0c72010-09-05 02:18:34 +00003050 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00003051 if (!X86SelectAddress(Ptr, AM))
Chris Lattnereeba0c72010-09-05 02:18:34 +00003052 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003053
Craig Topper55406d92012-08-11 17:46:16 +00003054 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00003055
Rafael Espindolaea09c592014-02-18 22:05:46 +00003056 unsigned Size = DL.getTypeAllocSize(LI->getType());
Chris Lattnereeba0c72010-09-05 02:18:34 +00003057 unsigned Alignment = LI->getAlignment();
3058
Juergen Ributzka349777d2014-06-12 23:27:57 +00003059 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3060 Alignment = DL.getABITypeAlignment(LI->getType());
3061
Chris Lattnereeba0c72010-09-05 02:18:34 +00003062 SmallVector<MachineOperand, 8> AddrOps;
3063 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00003064
Chris Lattnereeba0c72010-09-05 02:18:34 +00003065 MachineInstr *Result =
3066 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
Juergen Ributzka349777d2014-06-12 23:27:57 +00003067 if (!Result)
3068 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003069
Juergen Ributzka349777d2014-06-12 23:27:57 +00003070 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Chris Lattner2d186572011-01-16 02:27:38 +00003071 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00003072 MI->eraseFromParent();
3073 return true;
3074}
3075
3076
Evan Cheng24422d42008-09-03 00:03:49 +00003077namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003078 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
3079 const TargetLibraryInfo *libInfo) {
3080 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00003081 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00003082}