blob: 37174a14b29d2d6d6efff1dd9ae1595676170674 [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
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000114 bool X86SelectSelect(const Instruction *I);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000115
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000116 bool X86SelectTrunc(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000117
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000118 bool X86SelectFPExt(const Instruction *I);
119 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohmanbf646f22008-09-10 21:02:08 +0000120
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000121 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
122 bool X86SelectCall(const Instruction *I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000123
Eli Friedmancd2124a2011-06-10 23:39:36 +0000124 bool DoSelectCall(const Instruction *I, const char *MemIntName);
125
Dan Gohman3691d502008-09-25 15:24:26 +0000126 const X86InstrInfo *getInstrInfo() const {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000127 return getTargetMachine()->getInstrInfo();
128 }
129 const X86TargetMachine *getTargetMachine() const {
130 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman3691d502008-09-25 15:24:26 +0000131 }
132
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000133 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
134
Craig Topper2d9361e2014-03-09 07:44:38 +0000135 unsigned TargetMaterializeConstant(const Constant *C) override;
Dan Gohman39d82f92008-09-10 20:11:02 +0000136
Craig Topper2d9361e2014-03-09 07:44:38 +0000137 unsigned TargetMaterializeAlloca(const AllocaInst *C) override;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000138
Craig Topper2d9361e2014-03-09 07:44:38 +0000139 unsigned TargetMaterializeFloatZero(const ConstantFP *CF) override;
Eli Friedman406c4712011-04-27 22:41:55 +0000140
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000141 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
142 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000143 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000144 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
145 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000146 }
147
Chris Lattner229907c2011-07-18 04:54:35 +0000148 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000149
Eli Friedman60afcc22011-05-20 22:21:04 +0000150 bool IsMemcpySmall(uint64_t Len);
151
Eli Friedmanbcc69142011-04-27 01:45:07 +0000152 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
153 X86AddressMode SrcAM, uint64_t Len);
Evan Cheng24422d42008-09-03 00:03:49 +0000154};
Wesley Peck527da1b2010-11-23 03:31:01 +0000155
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000156} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000157
Juergen Ributzkaaa602092014-06-17 21:55:43 +0000158static CmpInst::Predicate optimizeCmpPredicate(const CmpInst *CI) {
159 // If both operands are the same, then try to optimize or fold the cmp.
160 CmpInst::Predicate Predicate = CI->getPredicate();
161 if (CI->getOperand(0) != CI->getOperand(1))
162 return Predicate;
163
164 switch (Predicate) {
165 default: llvm_unreachable("Invalid predicate!");
166 case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break;
167 case CmpInst::FCMP_OEQ: Predicate = CmpInst::FCMP_ORD; break;
168 case CmpInst::FCMP_OGT: Predicate = CmpInst::FCMP_FALSE; break;
169 case CmpInst::FCMP_OGE: Predicate = CmpInst::FCMP_ORD; break;
170 case CmpInst::FCMP_OLT: Predicate = CmpInst::FCMP_FALSE; break;
171 case CmpInst::FCMP_OLE: Predicate = CmpInst::FCMP_ORD; break;
172 case CmpInst::FCMP_ONE: Predicate = CmpInst::FCMP_FALSE; break;
173 case CmpInst::FCMP_ORD: Predicate = CmpInst::FCMP_ORD; break;
174 case CmpInst::FCMP_UNO: Predicate = CmpInst::FCMP_UNO; break;
175 case CmpInst::FCMP_UEQ: Predicate = CmpInst::FCMP_TRUE; break;
176 case CmpInst::FCMP_UGT: Predicate = CmpInst::FCMP_UNO; break;
177 case CmpInst::FCMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
178 case CmpInst::FCMP_ULT: Predicate = CmpInst::FCMP_UNO; break;
179 case CmpInst::FCMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
180 case CmpInst::FCMP_UNE: Predicate = CmpInst::FCMP_UNO; break;
181 case CmpInst::FCMP_TRUE: Predicate = CmpInst::FCMP_TRUE; break;
182
183 case CmpInst::ICMP_EQ: Predicate = CmpInst::FCMP_TRUE; break;
184 case CmpInst::ICMP_NE: Predicate = CmpInst::FCMP_FALSE; break;
185 case CmpInst::ICMP_UGT: Predicate = CmpInst::FCMP_FALSE; break;
186 case CmpInst::ICMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
187 case CmpInst::ICMP_ULT: Predicate = CmpInst::FCMP_FALSE; break;
188 case CmpInst::ICMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
189 case CmpInst::ICMP_SGT: Predicate = CmpInst::FCMP_FALSE; break;
190 case CmpInst::ICMP_SGE: Predicate = CmpInst::FCMP_TRUE; break;
191 case CmpInst::ICMP_SLT: Predicate = CmpInst::FCMP_FALSE; break;
192 case CmpInst::ICMP_SLE: Predicate = CmpInst::FCMP_TRUE; break;
193 }
194
195 return Predicate;
196}
197
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +0000198static std::pair<X86::CondCode, bool>
199getX86ConditonCode(CmpInst::Predicate Predicate) {
200 X86::CondCode CC = X86::COND_INVALID;
201 bool NeedSwap = false;
202 switch (Predicate) {
203 default: break;
204 // Floating-point Predicates
205 case CmpInst::FCMP_UEQ: CC = X86::COND_E; break;
206 case CmpInst::FCMP_OLT: NeedSwap = true; // fall-through
207 case CmpInst::FCMP_OGT: CC = X86::COND_A; break;
208 case CmpInst::FCMP_OLE: NeedSwap = true; // fall-through
209 case CmpInst::FCMP_OGE: CC = X86::COND_AE; break;
210 case CmpInst::FCMP_UGT: NeedSwap = true; // fall-through
211 case CmpInst::FCMP_ULT: CC = X86::COND_B; break;
212 case CmpInst::FCMP_UGE: NeedSwap = true; // fall-through
213 case CmpInst::FCMP_ULE: CC = X86::COND_BE; break;
214 case CmpInst::FCMP_ONE: CC = X86::COND_NE; break;
215 case CmpInst::FCMP_UNO: CC = X86::COND_P; break;
216 case CmpInst::FCMP_ORD: CC = X86::COND_NP; break;
217 case CmpInst::FCMP_OEQ: // fall-through
218 case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break;
219
220 // Integer Predicates
221 case CmpInst::ICMP_EQ: CC = X86::COND_E; break;
222 case CmpInst::ICMP_NE: CC = X86::COND_NE; break;
223 case CmpInst::ICMP_UGT: CC = X86::COND_A; break;
224 case CmpInst::ICMP_UGE: CC = X86::COND_AE; break;
225 case CmpInst::ICMP_ULT: CC = X86::COND_B; break;
226 case CmpInst::ICMP_ULE: CC = X86::COND_BE; break;
227 case CmpInst::ICMP_SGT: CC = X86::COND_G; break;
228 case CmpInst::ICMP_SGE: CC = X86::COND_GE; break;
229 case CmpInst::ICMP_SLT: CC = X86::COND_L; break;
230 case CmpInst::ICMP_SLE: CC = X86::COND_LE; break;
231 }
232
233 return std::make_pair(CC, NeedSwap);
234}
235
Chris Lattner229907c2011-07-18 04:54:35 +0000236bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000237 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
238 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000239 // Unhandled type. Halt "fast" selection and bail.
240 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000241
242 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000243 // For now, require SSE/SSE2 for performing floating-point operations,
244 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000245 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000246 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000247 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000248 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000249 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000250 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000251 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000252 // We only handle legal types. For example, on x86-32 the instruction
253 // selector contains all of the 64-bit instructions from x86-64,
254 // under the assumption that i64 won't be used if the target doesn't
255 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000256 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000257}
258
259#include "X86GenCallingConv.inc"
260
Evan Chengf5bc7e52008-09-05 21:00:03 +0000261/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000262/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000263/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000264bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000265 MachineMemOperand *MMO, unsigned &ResultReg) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000266 // Get opcode and regclass of the output for the given load instruction.
267 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000268 const TargetRegisterClass *RC = nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000269 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000270 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000271 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000272 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000273 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000274 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000275 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000276 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000277 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000278 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000279 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000280 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000281 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000282 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000283 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000284 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000285 // Must be in x86-64 mode.
286 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000287 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000288 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000289 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000290 if (X86ScalarSSEf32) {
291 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000292 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000293 } else {
294 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000295 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000296 }
297 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000298 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000299 if (X86ScalarSSEf64) {
300 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000301 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000302 } else {
303 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000304 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000305 }
306 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000307 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000308 // No f80 support yet.
309 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000310 }
311
312 ResultReg = createResultReg(RC);
Juergen Ributzka349777d2014-06-12 23:27:57 +0000313 MachineInstrBuilder MIB =
314 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
315 addFullAddress(MIB, AM);
316 if (MMO)
317 MIB->addMemOperand(*FuncInfo.MF, MMO);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000318 return true;
319}
320
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000321/// X86FastEmitStore - Emit a machine instruction to store a value Val of
322/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
323/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000324/// i.e. V. Return true if it is possible.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000325bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
326 const X86AddressMode &AM,
327 MachineMemOperand *MMO, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000328 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000329 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000330 switch (VT.getSimpleVT().SimpleTy) {
331 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000332 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000333 case MVT::i1: {
334 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000335 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000336 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000337 TII.get(X86::AND8ri), AndResult)
338 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
Craig Topper4f55b0e2013-07-17 05:57:45 +0000339 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000340 }
341 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000342 case MVT::i8: Opc = X86::MOV8mr; break;
343 case MVT::i16: Opc = X86::MOV16mr; break;
344 case MVT::i32: Opc = X86::MOV32mr; break;
345 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
346 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000347 Opc = X86ScalarSSEf32 ?
348 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000349 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000350 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000351 Opc = X86ScalarSSEf64 ?
352 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000353 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000354 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000355 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000356 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000357 else
Craig Topper55475d42013-07-17 06:58:23 +0000358 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000359 break;
360 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000361 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000362 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000363 else
Craig Topperad1fff92013-07-18 07:16:44 +0000364 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000365 break;
366 case MVT::v4i32:
367 case MVT::v2i64:
368 case MVT::v8i16:
369 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000370 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000371 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000372 else
Craig Topper55475d42013-07-17 06:58:23 +0000373 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000374 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000375 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000376
Juergen Ributzka349777d2014-06-12 23:27:57 +0000377 MachineInstrBuilder MIB =
378 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
379 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
380 if (MMO)
381 MIB->addMemOperand(*FuncInfo.MF, MMO);
382
Evan Chengf5bc7e52008-09-05 21:00:03 +0000383 return true;
384}
385
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000386bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000387 const X86AddressMode &AM,
388 MachineMemOperand *MMO, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000389 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000390 if (isa<ConstantPointerNull>(Val))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000391 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000392
Chris Lattner3ba29352008-10-15 05:30:52 +0000393 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000394 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000395 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000396 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000397 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000398 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000399 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000400 case MVT::i8: Opc = X86::MOV8mi; break;
401 case MVT::i16: Opc = X86::MOV16mi; break;
402 case MVT::i32: Opc = X86::MOV32mi; break;
403 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000404 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000405 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000406 Opc = X86::MOV64mi32;
407 break;
408 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000409
Chris Lattner3ba29352008-10-15 05:30:52 +0000410 if (Opc) {
Juergen Ributzka349777d2014-06-12 23:27:57 +0000411 MachineInstrBuilder MIB =
412 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
413 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
414 : CI->getZExtValue());
415 if (MMO)
416 MIB->addMemOperand(*FuncInfo.MF, MMO);
Chris Lattner3ba29352008-10-15 05:30:52 +0000417 return true;
418 }
419 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000420
Chris Lattner3ba29352008-10-15 05:30:52 +0000421 unsigned ValReg = getRegForValue(Val);
422 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000423 return false;
424
Juergen Ributzka349777d2014-06-12 23:27:57 +0000425 bool ValKill = hasTrivialKill(Val);
426 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000427}
428
Evan Cheng6500d172008-09-08 06:35:17 +0000429/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
430/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
431/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000432bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
433 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000434 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000435 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
436 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000437 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000438 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000439
440 ResultReg = RR;
441 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000442}
443
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000444bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
445 // Handle constant address.
446 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
447 // Can't handle alternate code models yet.
448 if (TM.getCodeModel() != CodeModel::Small)
449 return false;
450
451 // Can't handle TLS yet.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000452 if (GV->isThreadLocal())
453 return false;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000454
455 // RIP-relative addresses can't have additional register operands, so if
456 // we've already folded stuff into the addressing mode, just force the
457 // global value into its own register, which we can use as the basereg.
458 if (!Subtarget->isPICStyleRIPRel() ||
459 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
460 // Okay, we've committed to selecting this global. Set up the address.
461 AM.GV = GV;
462
463 // Allow the subtarget to classify the global.
464 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
465
466 // If this reference is relative to the pic base, set it now.
467 if (isGlobalRelativeToPICBase(GVFlags)) {
468 // FIXME: How do we know Base.Reg is free??
469 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
470 }
471
472 // Unless the ABI requires an extra load, return a direct reference to
473 // the global.
474 if (!isGlobalStubReference(GVFlags)) {
475 if (Subtarget->isPICStyleRIPRel()) {
476 // Use rip-relative addressing if we can. Above we verified that the
477 // base and index registers are unused.
478 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
479 AM.Base.Reg = X86::RIP;
480 }
481 AM.GVOpFlags = GVFlags;
482 return true;
483 }
484
485 // Ok, we need to do a load from a stub. If we've already loaded from
486 // this stub, reuse the loaded pointer, otherwise emit the load now.
487 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
488 unsigned LoadReg;
489 if (I != LocalValueMap.end() && I->second != 0) {
490 LoadReg = I->second;
491 } else {
492 // Issue load from stub.
493 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000494 const TargetRegisterClass *RC = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000495 X86AddressMode StubAM;
496 StubAM.Base.Reg = AM.Base.Reg;
497 StubAM.GV = GV;
498 StubAM.GVOpFlags = GVFlags;
499
500 // Prepare for inserting code in the local-value area.
501 SavePoint SaveInsertPt = enterLocalValueArea();
502
503 if (TLI.getPointerTy() == MVT::i64) {
504 Opc = X86::MOV64rm;
505 RC = &X86::GR64RegClass;
506
507 if (Subtarget->isPICStyleRIPRel())
508 StubAM.Base.Reg = X86::RIP;
509 } else {
510 Opc = X86::MOV32rm;
511 RC = &X86::GR32RegClass;
512 }
513
514 LoadReg = createResultReg(RC);
515 MachineInstrBuilder LoadMI =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000517 addFullAddress(LoadMI, StubAM);
518
519 // Ok, back to normal mode.
520 leaveLocalValueArea(SaveInsertPt);
521
522 // Prevent loading GV stub multiple times in same MBB.
523 LocalValueMap[V] = LoadReg;
524 }
525
526 // Now construct the final address. Note that the Disp, Scale,
527 // and Index values may already be set here.
528 AM.Base.Reg = LoadReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000529 AM.GV = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000530 return true;
531 }
532 }
533
534 // If all else fails, try to materialize the value in a register.
535 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
536 if (AM.Base.Reg == 0) {
537 AM.Base.Reg = getRegForValue(V);
538 return AM.Base.Reg != 0;
539 }
540 if (AM.IndexReg == 0) {
541 assert(AM.Scale == 1 && "Scale with no index!");
542 AM.IndexReg = getRegForValue(V);
543 return AM.IndexReg != 0;
544 }
545 }
546
547 return false;
548}
549
Dan Gohman39d82f92008-09-10 20:11:02 +0000550/// X86SelectAddress - Attempt to fill in an address from the given value.
551///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000552bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000553 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000554redo_gep:
Craig Topper062a2ba2014-04-25 05:30:21 +0000555 const User *U = nullptr;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000556 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000557 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000558 // Don't walk into other basic blocks; it's possible we haven't
559 // visited them yet, so the instructions may not yet be assigned
560 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000561 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
562 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
563 Opcode = I->getOpcode();
564 U = I;
565 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000566 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000567 Opcode = C->getOpcode();
568 U = C;
569 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000570
Chris Lattner229907c2011-07-18 04:54:35 +0000571 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000572 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000573 // Fast instruction selection doesn't support the special
574 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000575 return false;
576
Dan Gohman6e005fd2008-09-18 23:23:44 +0000577 switch (Opcode) {
578 default: break;
579 case Instruction::BitCast:
580 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000581 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000582
583 case Instruction::IntToPtr:
584 // Look past no-op inttoptrs.
585 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000586 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000587 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000588
589 case Instruction::PtrToInt:
590 // Look past no-op ptrtoints.
591 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000592 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000593 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000594
595 case Instruction::Alloca: {
596 // Do static allocas.
597 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000598 DenseMap<const AllocaInst*, int>::iterator SI =
599 FuncInfo.StaticAllocaMap.find(A);
600 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000601 AM.BaseType = X86AddressMode::FrameIndexBase;
602 AM.Base.FrameIndex = SI->second;
603 return true;
604 }
605 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000606 }
607
608 case Instruction::Add: {
609 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000610 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000611 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
612 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000613 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000614 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000615 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000616 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000617 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000618 break;
619 }
620
621 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000622 X86AddressMode SavedAM = AM;
623
Dan Gohman6e005fd2008-09-18 23:23:44 +0000624 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000625 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000626 unsigned IndexReg = AM.IndexReg;
627 unsigned Scale = AM.Scale;
628 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000629 // Iterate through the indices, folding what we can. Constants can be
630 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000631 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000632 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000633 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000634 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000635 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000636 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
637 continue;
638 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000639
Chris Lattner4b026b92011-04-17 17:05:12 +0000640 // A array/variable index is always of the form i*S where S is the
641 // constant scale size. See if we can push the scale into immediates.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000642 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner4b026b92011-04-17 17:05:12 +0000643 for (;;) {
644 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
645 // Constant-offset addressing.
646 Disp += CI->getSExtValue() * S;
647 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000648 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000649 if (canFoldAddIntoGEP(U, Op)) {
650 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000651 ConstantInt *CI =
652 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
653 Disp += CI->getSExtValue() * S;
654 // Iterate on the other operand.
655 Op = cast<AddOperator>(Op)->getOperand(0);
656 continue;
657 }
658 if (IndexReg == 0 &&
659 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
660 (S == 1 || S == 2 || S == 4 || S == 8)) {
661 // Scaled-index addressing.
662 Scale = S;
663 IndexReg = getRegForGEPIndex(Op).first;
664 if (IndexReg == 0)
665 return false;
666 break;
667 }
668 // Unsupported.
669 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000670 }
671 }
Bill Wendling585a9012013-09-24 00:13:08 +0000672
Dan Gohman2564b902008-09-26 20:04:15 +0000673 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000674 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000675 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000676
Dan Gohman6e005fd2008-09-18 23:23:44 +0000677 AM.IndexReg = IndexReg;
678 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000679 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000680 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000681
682 if (const GetElementPtrInst *GEP =
683 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
684 // Ok, the GEP indices were covered by constant-offset and scaled-index
685 // addressing. Update the address state and move on to examining the base.
686 V = GEP;
687 goto redo_gep;
688 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000689 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000690 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000691
Chris Lattner4b026b92011-04-17 17:05:12 +0000692 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000693 // our address and just match the value instead of completely failing.
694 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000695
696 for (SmallVectorImpl<const Value *>::reverse_iterator
697 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
698 if (handleConstantAddresses(*I, AM))
699 return true;
700
701 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000702 unsupported_gep:
703 // Ok, the GEP indices weren't all covered.
704 break;
705 }
706 }
707
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000708 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000709}
710
Chris Lattner8212d372009-07-10 05:33:42 +0000711/// X86SelectCallAddress - Attempt to fill in an address from the given value.
712///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000713bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000714 const User *U = nullptr;
Chris Lattner8212d372009-07-10 05:33:42 +0000715 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000716 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000717 // Record if the value is defined in the same basic block.
718 //
719 // This information is crucial to know whether or not folding an
720 // operand is valid.
721 // Indeed, FastISel generates or reuses a virtual register for all
722 // operands of all instructions it selects. Obviously, the definition and
723 // its uses must use the same virtual register otherwise the produced
724 // code is incorrect.
725 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
726 // registers for values that are alive across basic blocks. This ensures
727 // that the values are consistently set between across basic block, even
728 // if different instruction selection mechanisms are used (e.g., a mix of
729 // SDISel and FastISel).
730 // For values local to a basic block, the instruction selection process
731 // generates these virtual registers with whatever method is appropriate
732 // for its needs. In particular, FastISel and SDISel do not share the way
733 // local virtual registers are set.
734 // Therefore, this is impossible (or at least unsafe) to share values
735 // between basic blocks unless they use the same instruction selection
736 // method, which is not guarantee for X86.
737 // Moreover, things like hasOneUse could not be used accurately, if we
738 // allow to reference values across basic blocks whereas they are not
739 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000740 bool InMBB = true;
741 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000742 Opcode = I->getOpcode();
743 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000744 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000745 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000746 Opcode = C->getOpcode();
747 U = C;
748 }
749
750 switch (Opcode) {
751 default: break;
752 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000753 // Look past bitcasts if its operand is in the same BB.
754 if (InMBB)
755 return X86SelectCallAddress(U->getOperand(0), AM);
756 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000757
758 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000759 // Look past no-op inttoptrs if its operand is in the same BB.
760 if (InMBB &&
761 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000762 return X86SelectCallAddress(U->getOperand(0), AM);
763 break;
764
765 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000766 // Look past no-op ptrtoints if its operand is in the same BB.
767 if (InMBB &&
768 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000769 return X86SelectCallAddress(U->getOperand(0), AM);
770 break;
771 }
772
773 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000774 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000775 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000776 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000777 return false;
778
779 // RIP-relative addresses can't have additional register operands.
780 if (Subtarget->isPICStyleRIPRel() &&
781 (AM.Base.Reg != 0 || AM.IndexReg != 0))
782 return false;
783
Rafael Espindolaea09c592014-02-18 22:05:46 +0000784 // Can't handle DbgLocLImport.
Nico Rieck7157bb72014-01-14 15:22:47 +0000785 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000786 return false;
787
788 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000789 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000790 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000791 return false;
792
793 // Okay, we've committed to selecting this global. Set up the basic address.
794 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000795
Chris Lattner7277a802009-07-10 05:45:15 +0000796 // No ABI requires an extra load for anything other than DLLImport, which
797 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000798 if (Subtarget->isPICStyleRIPRel()) {
799 // Use rip-relative addressing if we can. Above we verified that the
800 // base and index registers are unused.
801 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
802 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000803 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000804 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
805 } else if (Subtarget->isPICStyleGOT()) {
806 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000807 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000808
Chris Lattner8212d372009-07-10 05:33:42 +0000809 return true;
810 }
811
812 // If all else fails, try to materialize the value in a register.
813 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
814 if (AM.Base.Reg == 0) {
815 AM.Base.Reg = getRegForValue(V);
816 return AM.Base.Reg != 0;
817 }
818 if (AM.IndexReg == 0) {
819 assert(AM.Scale == 1 && "Scale with no index!");
820 AM.IndexReg = getRegForValue(V);
821 return AM.IndexReg != 0;
822 }
823 }
824
825 return false;
826}
827
828
Owen Anderson4f948bd2008-09-04 07:08:58 +0000829/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000830bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000831 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000832 const StoreInst *S = cast<StoreInst>(I);
833
834 if (S->isAtomic())
835 return false;
836
Juergen Ributzka349777d2014-06-12 23:27:57 +0000837 const Value *Val = S->getValueOperand();
838 const Value *Ptr = S->getPointerOperand();
Craig Topper4f55b0e2013-07-17 05:57:45 +0000839
Duncan Sandsf5dda012010-11-03 11:35:31 +0000840 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000841 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000842 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000843
Juergen Ributzka349777d2014-06-12 23:27:57 +0000844 unsigned Alignment = S->getAlignment();
845 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
846 if (Alignment == 0) // Ensure that codegen never sees alignment 0
847 Alignment = ABIAlignment;
848 bool Aligned = Alignment >= ABIAlignment;
849
Dan Gohman39d82f92008-09-10 20:11:02 +0000850 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000851 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000852 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000853
Juergen Ributzka349777d2014-06-12 23:27:57 +0000854 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000855}
856
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000857/// X86SelectRet - Select and emit code to implement ret instructions.
858bool X86FastISel::X86SelectRet(const Instruction *I) {
859 const ReturnInst *Ret = cast<ReturnInst>(I);
860 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000861 const X86MachineFunctionInfo *X86MFInfo =
862 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000863
864 if (!FuncInfo.CanLowerReturn)
865 return false;
866
867 CallingConv::ID CC = F.getCallingConv();
868 if (CC != CallingConv::C &&
869 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000870 CC != CallingConv::X86_FastCall &&
871 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000872 return false;
873
Charles Davise8f297c2013-07-12 06:02:35 +0000874 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000875 return false;
876
877 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000878 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000879 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000880
881 // fastcc with -tailcallopt is intended to provide a guaranteed
882 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000883 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000884 return false;
885
886 // Let SDISel handle vararg functions.
887 if (F.isVarArg())
888 return false;
889
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000890 // Build a list of return value registers.
891 SmallVector<unsigned, 4> RetRegs;
892
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000893 if (Ret->getNumOperands() > 0) {
894 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000895 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000896
897 // Analyze operands of the call, assigning locations to each operand.
898 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000899 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000900 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000901 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000902
903 const Value *RV = Ret->getOperand(0);
904 unsigned Reg = getRegForValue(RV);
905 if (Reg == 0)
906 return false;
907
908 // Only handle a single return value for now.
909 if (ValLocs.size() != 1)
910 return false;
911
912 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000913
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000914 // Don't bother handling odd stuff for now.
915 if (VA.getLocInfo() != CCValAssign::Full)
916 return false;
917 // Only handle register returns for now.
918 if (!VA.isRegLoc())
919 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000920
921 // The calling-convention tables for x87 returns don't tell
922 // the whole story.
923 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
924 return false;
925
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000926 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000927 EVT SrcVT = TLI.getValueType(RV->getType());
928 EVT DstVT = VA.getValVT();
929 // Special handling for extended integers.
930 if (SrcVT != DstVT) {
931 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
932 return false;
933
934 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
935 return false;
936
937 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
938
939 if (SrcVT == MVT::i1) {
940 if (Outs[0].Flags.isSExt())
941 return false;
942 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
943 SrcVT = MVT::i8;
944 }
945 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
946 ISD::SIGN_EXTEND;
947 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
948 SrcReg, /*TODO: Kill=*/false);
949 }
950
951 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000952 unsigned DstReg = VA.getLocReg();
953 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000954 // Avoid a cross-class copy. This is very unlikely.
955 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000956 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000957 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000958 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000959
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000960 // Add register to return instruction.
961 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000962 }
963
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000964 // The x86-64 ABI for returning structs by value requires that we copy
965 // the sret argument into %rax for the return. We saved the argument into
966 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000967 // and into %rax. We also do the same with %eax for Win32.
968 if (F.hasStructRetAttr() &&
Yaron Keren136fe7d2014-04-01 18:15:34 +0000969 (Subtarget->is64Bit() || Subtarget->isTargetKnownWindowsMSVC())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000970 unsigned Reg = X86MFInfo->getSRetReturnReg();
971 assert(Reg &&
972 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000973 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000974 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000975 RetReg).addReg(Reg);
976 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000977 }
978
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000979 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000980 MachineInstrBuilder MIB =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000981 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000982 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
983 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000984 return true;
985}
986
Evan Chenga41ee292008-09-03 06:44:39 +0000987/// X86SelectLoad - Select and emit code to implement load instructions.
988///
Juergen Ributzka349777d2014-06-12 23:27:57 +0000989bool X86FastISel::X86SelectLoad(const Instruction *I) {
990 const LoadInst *LI = cast<LoadInst>(I);
991
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000992 // Atomic loads need special handling.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000993 if (LI->isAtomic())
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000994 return false;
995
Duncan Sandsf5dda012010-11-03 11:35:31 +0000996 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000997 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +0000998 return false;
999
Juergen Ributzka349777d2014-06-12 23:27:57 +00001000 const Value *Ptr = LI->getPointerOperand();
1001
Dan Gohman39d82f92008-09-10 20:11:02 +00001002 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001003 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00001004 return false;
Evan Chenga41ee292008-09-03 06:44:39 +00001005
Evan Chengf5bc7e52008-09-05 21:00:03 +00001006 unsigned ResultReg = 0;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001007 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg))
1008 return false;
1009
1010 UpdateValueMap(I, ResultReg);
1011 return true;
Evan Chenga41ee292008-09-03 06:44:39 +00001012}
1013
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001014static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001015 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +00001016 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
1017 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001018
Owen Anderson9f944592009-08-11 20:47:22 +00001019 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +00001020 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001021 case MVT::i8: return X86::CMP8rr;
1022 case MVT::i16: return X86::CMP16rr;
1023 case MVT::i32: return X86::CMP32rr;
1024 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001025 case MVT::f32:
1026 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
1027 case MVT::f64:
1028 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001029 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001030}
1031
Chris Lattner88f47542008-10-15 04:13:29 +00001032/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
1033/// of the comparison, return an opcode that works for the compare (e.g.
1034/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001035static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +00001036 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +00001037 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +00001038 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001039 case MVT::i8: return X86::CMP8ri;
1040 case MVT::i16: return X86::CMP16ri;
1041 case MVT::i32: return X86::CMP32ri;
1042 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +00001043 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1044 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +00001045 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +00001046 return X86::CMP64ri32;
1047 return 0;
1048 }
Chris Lattner88f47542008-10-15 04:13:29 +00001049}
1050
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001051bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
1052 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +00001053 unsigned Op0Reg = getRegForValue(Op0);
1054 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001055
Chris Lattnere388725a2008-10-15 05:18:04 +00001056 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +00001057 if (isa<ConstantPointerNull>(Op1))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001058 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +00001059
Chris Lattnerd46b9512008-10-15 04:26:38 +00001060 // We have two options: compare with register or immediate. If the RHS of
1061 // the compare is an immediate that we can fold into this compare, use
1062 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001063 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +00001064 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001065 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareImmOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001066 .addReg(Op0Reg)
1067 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +00001068 return true;
1069 }
1070 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001071
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001072 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +00001073 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001074
Chris Lattnerd46b9512008-10-15 04:26:38 +00001075 unsigned Op1Reg = getRegForValue(Op1);
1076 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001077 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001078 .addReg(Op0Reg)
1079 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001080
Chris Lattnerd46b9512008-10-15 04:26:38 +00001081 return true;
1082}
1083
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001084bool X86FastISel::X86SelectCmp(const Instruction *I) {
1085 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001086
Duncan Sandsf5dda012010-11-03 11:35:31 +00001087 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001088 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +00001089 return false;
1090
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001091 // Try to optimize or fold the cmp.
1092 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1093 unsigned ResultReg = 0;
1094 switch (Predicate) {
1095 default: break;
1096 case CmpInst::FCMP_FALSE: {
1097 ResultReg = createResultReg(&X86::GR32RegClass);
1098 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0),
1099 ResultReg);
1100 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1101 X86::sub_8bit);
1102 if (!ResultReg)
1103 return false;
1104 break;
1105 }
1106 case CmpInst::FCMP_TRUE: {
1107 ResultReg = createResultReg(&X86::GR8RegClass);
1108 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
1109 ResultReg).addImm(1);
1110 break;
1111 }
1112 }
1113
1114 if (ResultReg) {
1115 UpdateValueMap(I, ResultReg);
1116 return true;
1117 }
1118
1119 const Value *LHS = CI->getOperand(0);
1120 const Value *RHS = CI->getOperand(1);
1121
1122 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1123 // We don't have to materialize a zero constant for this case and can just use
1124 // %x again on the RHS.
1125 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1126 const auto *RHSC = dyn_cast<ConstantFP>(RHS);
1127 if (RHSC && RHSC->isNullValue())
1128 RHS = LHS;
1129 }
1130
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001131 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Juergen Ributzkae3570562014-06-17 14:47:45 +00001132 static unsigned SETFOpcTable[2][3] = {
1133 { X86::SETEr, X86::SETNPr, X86::AND8rr },
1134 { X86::SETNEr, X86::SETPr, X86::OR8rr }
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001135 };
1136 unsigned *SETFOpc = nullptr;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001137 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001138 default: break;
1139 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1140 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1141 }
1142
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001143 ResultReg = createResultReg(&X86::GR8RegClass);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001144 if (SETFOpc) {
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001145 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001146 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001147
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001148 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1149 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1150 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1151 FlagReg1);
1152 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1153 FlagReg2);
Juergen Ributzkae3570562014-06-17 14:47:45 +00001154 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001155 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
Chris Lattnera3596db2008-10-15 03:47:17 +00001156 UpdateValueMap(I, ResultReg);
1157 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001158 }
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001159
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001160 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001161 bool SwapArgs;
1162 std::tie(CC, SwapArgs) = getX86ConditonCode(Predicate);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001163 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1164 unsigned Opc = X86::getSETFromCond(CC);
Wesley Peck527da1b2010-11-23 03:31:01 +00001165
Chris Lattnerf32ce222008-10-15 03:52:54 +00001166 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001167 std::swap(LHS, RHS);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001168
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001169 // Emit a compare of LHS/RHS.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001170 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001171 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001172
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001173 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001174 UpdateValueMap(I, ResultReg);
1175 return true;
1176}
Evan Chenga41ee292008-09-03 06:44:39 +00001177
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001178bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001179 EVT DstVT = TLI.getValueType(I->getType());
1180 if (!TLI.isTypeLegal(DstVT))
1181 return false;
1182
1183 unsigned ResultReg = getRegForValue(I->getOperand(0));
1184 if (ResultReg == 0)
1185 return false;
1186
Tim Northover04eb4232013-05-30 10:43:18 +00001187 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001188 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001189 if (SrcVT.SimpleTy == MVT::i1) {
1190 // Set the high bits to zero.
1191 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1192 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001193
Tim Northover04eb4232013-05-30 10:43:18 +00001194 if (ResultReg == 0)
1195 return false;
1196 }
1197
1198 if (DstVT == MVT::i64) {
1199 // Handle extension to 64-bits via sub-register shenanigans.
1200 unsigned MovInst;
1201
1202 switch (SrcVT.SimpleTy) {
1203 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1204 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1205 case MVT::i32: MovInst = X86::MOV32rr; break;
1206 default: llvm_unreachable("Unexpected zext to i64 source type");
1207 }
1208
1209 unsigned Result32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001210 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
Tim Northover04eb4232013-05-30 10:43:18 +00001211 .addReg(ResultReg);
1212
1213 ResultReg = createResultReg(&X86::GR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001214 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
Tim Northover04eb4232013-05-30 10:43:18 +00001215 ResultReg)
1216 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1217 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001218 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1219 ResultReg, /*Kill=*/true);
1220 if (ResultReg == 0)
1221 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001222 }
1223
Eli Friedmanc7035512011-05-25 23:49:02 +00001224 UpdateValueMap(I, ResultReg);
1225 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001226}
1227
Chris Lattnerd46b9512008-10-15 04:26:38 +00001228
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001229bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001230 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001231 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001232 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001233 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1234 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001235
Dan Gohman42ef6692010-08-21 02:32:36 +00001236 // Fold the common case of a conditional branch with a comparison
1237 // in the same block (values defined on other blocks may not have
1238 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001239 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001240 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001241 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001242
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001243 // Try to optimize or fold the cmp.
1244 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1245 switch (Predicate) {
1246 default: break;
1247 case CmpInst::FCMP_FALSE: FastEmitBranch(FalseMBB, DbgLoc); return true;
1248 case CmpInst::FCMP_TRUE: FastEmitBranch(TrueMBB, DbgLoc); return true;
1249 }
1250
1251 const Value *CmpLHS = CI->getOperand(0);
1252 const Value *CmpRHS = CI->getOperand(1);
1253
1254 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x,
1255 // 0.0.
1256 // We don't have to materialize a zero constant for this case and can just
1257 // use %x again on the RHS.
1258 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1259 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1260 if (CmpRHSC && CmpRHSC->isNullValue())
1261 CmpRHS = CmpLHS;
1262 }
1263
Dan Gohman1ab1d312008-10-02 22:15:21 +00001264 // Try to take advantage of fallthrough opportunities.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001265 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001266 std::swap(TrueMBB, FalseMBB);
1267 Predicate = CmpInst::getInversePredicate(Predicate);
1268 }
1269
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001270 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/conditon
1271 // code check. Instead two branch instructions are required to check all
1272 // the flags. First we change the predicate to a supported conditon code,
1273 // which will be the first branch. Later one we will emit the second
1274 // branch.
1275 bool NeedExtraBranch = false;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001276 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001277 default: break;
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001278 case CmpInst::FCMP_OEQ:
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001279 std::swap(TrueMBB, FalseMBB); // fall-through
1280 case CmpInst::FCMP_UNE:
1281 NeedExtraBranch = true;
1282 Predicate = CmpInst::FCMP_ONE;
1283 break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001284 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001285
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001286 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001287 bool SwapArgs;
1288 unsigned BranchOpc;
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001289 std::tie(CC, SwapArgs) = getX86ConditonCode(Predicate);
1290 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1291
1292 BranchOpc = X86::GetCondBranchFromCond(CC);
Chris Lattner47bef252008-10-15 04:02:26 +00001293 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001294 std::swap(CmpLHS, CmpRHS);
Chris Lattner47bef252008-10-15 04:02:26 +00001295
Chris Lattnerd46b9512008-10-15 04:26:38 +00001296 // Emit a compare of the LHS and RHS, setting the flags.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001297 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT))
Chris Lattnerd46b9512008-10-15 04:26:38 +00001298 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001299
Rafael Espindolaea09c592014-02-18 22:05:46 +00001300 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001301 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001302
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001303 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1304 // to UNE above).
1305 if (NeedExtraBranch) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001306 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001307 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001308 }
1309
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001310 // Obtain the branch weight and add the TrueBB to the successor list.
Juergen Ributzka454d3742014-06-13 00:45:11 +00001311 uint32_t BranchWeight = 0;
1312 if (FuncInfo.BPI)
1313 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1314 TrueMBB->getBasicBlock());
1315 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001316
1317 // Emits an unconditional branch to the FalseBB, obtains the branch
1318 // weight, andd adds it to the successor list.
1319 FastEmitBranch(FalseMBB, DbgLoc);
1320
Dan Gohman1ab1d312008-10-02 22:15:21 +00001321 return true;
1322 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001323 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1324 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1325 // typically happen for _Bool and C++ bools.
1326 MVT SourceVT;
1327 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1328 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1329 unsigned TestOpc = 0;
1330 switch (SourceVT.SimpleTy) {
1331 default: break;
1332 case MVT::i8: TestOpc = X86::TEST8ri; break;
1333 case MVT::i16: TestOpc = X86::TEST16ri; break;
1334 case MVT::i32: TestOpc = X86::TEST32ri; break;
1335 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1336 }
1337 if (TestOpc) {
1338 unsigned OpReg = getRegForValue(TI->getOperand(0));
1339 if (OpReg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001340 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001341 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001342
Chris Lattnerc59290a2011-04-19 04:26:32 +00001343 unsigned JmpOpc = X86::JNE_4;
1344 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1345 std::swap(TrueMBB, FalseMBB);
1346 JmpOpc = X86::JE_4;
1347 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001348
Rafael Espindolaea09c592014-02-18 22:05:46 +00001349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001350 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001351 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001352 uint32_t BranchWeight = 0;
1353 if (FuncInfo.BPI)
1354 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1355 TrueMBB->getBasicBlock());
1356 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001357 return true;
1358 }
1359 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001360 }
1361
1362 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001363 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1364 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001365 unsigned OpReg = getRegForValue(BI->getCondition());
1366 if (OpReg == 0) return false;
1367
Rafael Espindolaea09c592014-02-18 22:05:46 +00001368 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Eli Friedman0eea0292011-04-27 01:34:27 +00001369 .addReg(OpReg).addImm(1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001370 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001371 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001372 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001373 uint32_t BranchWeight = 0;
1374 if (FuncInfo.BPI)
1375 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1376 TrueMBB->getBasicBlock());
1377 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Dan Gohmana5753b32008-09-05 01:06:14 +00001378 return true;
1379}
1380
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001381bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001382 unsigned CReg = 0, OpReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001383 const TargetRegisterClass *RC = nullptr;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001384 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001385 CReg = X86::CL;
1386 RC = &X86::GR8RegClass;
1387 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001388 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1389 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1390 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001391 default: return false;
1392 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001393 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001394 CReg = X86::CX;
1395 RC = &X86::GR16RegClass;
1396 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001397 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1398 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1399 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001400 default: return false;
1401 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001402 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001403 CReg = X86::ECX;
1404 RC = &X86::GR32RegClass;
1405 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001406 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1407 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1408 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001409 default: return false;
1410 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001411 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001412 CReg = X86::RCX;
1413 RC = &X86::GR64RegClass;
1414 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001415 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1416 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1417 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001418 default: return false;
1419 }
1420 } else {
1421 return false;
1422 }
1423
Duncan Sandsf5dda012010-11-03 11:35:31 +00001424 MVT VT;
1425 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001426 return false;
1427
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001428 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1429 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001430
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001431 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1432 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001433 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001434 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001435
1436 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001437 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001438 if (CReg != X86::CL)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001439 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001440 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001441 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001442
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001443 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001444 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001445 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001446 UpdateValueMap(I, ResultReg);
1447 return true;
1448}
1449
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001450bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1451 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1452 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1453 const static bool S = true; // IsSigned
1454 const static bool U = false; // !IsSigned
1455 const static unsigned Copy = TargetOpcode::COPY;
1456 // For the X86 DIV/IDIV instruction, in most cases the dividend
1457 // (numerator) must be in a specific register pair highreg:lowreg,
1458 // producing the quotient in lowreg and the remainder in highreg.
1459 // For most data types, to set up the instruction, the dividend is
1460 // copied into lowreg, and lowreg is sign-extended or zero-extended
1461 // into highreg. The exception is i8, where the dividend is defined
1462 // as a single register rather than a register pair, and we
1463 // therefore directly sign-extend or zero-extend the dividend into
1464 // lowreg, instead of copying, and ignore the highreg.
1465 const static struct DivRemEntry {
1466 // The following portion depends only on the data type.
1467 const TargetRegisterClass *RC;
1468 unsigned LowInReg; // low part of the register pair
1469 unsigned HighInReg; // high part of the register pair
1470 // The following portion depends on both the data type and the operation.
1471 struct DivRemResult {
1472 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1473 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1474 // highreg, or copying a zero into highreg.
1475 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1476 // zero/sign-extending into lowreg for i8.
1477 unsigned DivRemResultReg; // Register containing the desired result.
1478 bool IsOpSigned; // Whether to use signed or unsigned form.
1479 } ResultTable[NumOps];
1480 } OpTable[NumTypes] = {
1481 { &X86::GR8RegClass, X86::AX, 0, {
1482 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1483 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1484 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1485 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1486 }
1487 }, // i8
1488 { &X86::GR16RegClass, X86::AX, X86::DX, {
1489 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1490 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001491 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1492 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001493 }
1494 }, // i16
1495 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1496 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1497 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1498 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1499 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1500 }
1501 }, // i32
1502 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1503 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1504 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001505 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1506 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001507 }
1508 }, // i64
1509 };
1510
1511 MVT VT;
1512 if (!isTypeLegal(I->getType(), VT))
1513 return false;
1514
1515 unsigned TypeIndex, OpIndex;
1516 switch (VT.SimpleTy) {
1517 default: return false;
1518 case MVT::i8: TypeIndex = 0; break;
1519 case MVT::i16: TypeIndex = 1; break;
1520 case MVT::i32: TypeIndex = 2; break;
1521 case MVT::i64: TypeIndex = 3;
1522 if (!Subtarget->is64Bit())
1523 return false;
1524 break;
1525 }
1526
1527 switch (I->getOpcode()) {
1528 default: llvm_unreachable("Unexpected div/rem opcode");
1529 case Instruction::SDiv: OpIndex = 0; break;
1530 case Instruction::SRem: OpIndex = 1; break;
1531 case Instruction::UDiv: OpIndex = 2; break;
1532 case Instruction::URem: OpIndex = 3; break;
1533 }
1534
1535 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1536 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1537 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1538 if (Op0Reg == 0)
1539 return false;
1540 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1541 if (Op1Reg == 0)
1542 return false;
1543
1544 // Move op0 into low-order input register.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001545 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001546 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1547 // Zero-extend or sign-extend into high-order input register.
1548 if (OpEntry.OpSignExtend) {
1549 if (OpEntry.IsOpSigned)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001550 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001551 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001552 else {
1553 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001554 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001555 TII.get(X86::MOV32r0), Zero32);
1556
1557 // Copy the zero into the appropriate sub/super/identical physical
1558 // register. Unfortunately the operations needed are not uniform enough to
1559 // fit neatly into the table above.
1560 if (VT.SimpleTy == MVT::i16) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001561 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001562 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001563 .addReg(Zero32, 0, X86::sub_16bit);
1564 } else if (VT.SimpleTy == MVT::i32) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001565 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001566 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001567 .addReg(Zero32);
1568 } else if (VT.SimpleTy == MVT::i64) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001569 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001570 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1571 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1572 }
1573 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001574 }
1575 // Generate the DIV/IDIV instruction.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001576 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001577 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001578 // For i8 remainder, we can't reference AH directly, as we'll end
1579 // up with bogus copies like %R9B = COPY %AH. Reference AX
1580 // instead to prevent AH references in a REX instruction.
1581 //
1582 // The current assumption of the fast register allocator is that isel
1583 // won't generate explicit references to the GPR8_NOREX registers. If
1584 // the allocator and/or the backend get enhanced to be more robust in
1585 // that regard, this can be, and should be, removed.
1586 unsigned ResultReg = 0;
1587 if ((I->getOpcode() == Instruction::SRem ||
1588 I->getOpcode() == Instruction::URem) &&
1589 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1590 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1591 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001592 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbachc35388f2013-07-09 02:07:25 +00001593 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1594
1595 // Shift AX right by 8 bits instead of using AH.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001596 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
Jim Grosbachc35388f2013-07-09 02:07:25 +00001597 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1598
1599 // Now reference the 8-bit subreg of the result.
1600 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1601 /*Kill=*/true, X86::sub_8bit);
1602 }
1603 // Copy the result out of the physreg if we haven't already.
1604 if (!ResultReg) {
1605 ResultReg = createResultReg(TypeEntry.RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001606 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
Jim Grosbachc35388f2013-07-09 02:07:25 +00001607 .addReg(OpEntry.DivRemResultReg);
1608 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001609 UpdateValueMap(I, ResultReg);
1610
1611 return true;
1612}
1613
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001614bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001615 MVT VT;
1616 if (!isTypeLegal(I->getType(), VT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001617 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001618
Eric Christopher0574cc52010-09-29 23:00:29 +00001619 // We only use cmov here, if we don't have a cmov instruction bail.
1620 if (!Subtarget->hasCMov()) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001621
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001622 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001623 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001624 if (VT == MVT::i16) {
Dan Gohmane5560182008-09-05 21:13:04 +00001625 Opc = X86::CMOVE16rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001626 RC = &X86::GR16RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001627 } else if (VT == MVT::i32) {
Dan Gohmane5560182008-09-05 21:13:04 +00001628 Opc = X86::CMOVE32rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001629 RC = &X86::GR32RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001630 } else if (VT == MVT::i64) {
Dan Gohmane5560182008-09-05 21:13:04 +00001631 Opc = X86::CMOVE64rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001632 RC = &X86::GR64RegClass;
1633 } else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001634 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001635 }
1636
1637 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1638 if (Op0Reg == 0) return false;
1639 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1640 if (Op1Reg == 0) return false;
1641 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1642 if (Op2Reg == 0) return false;
1643
Quentin Colombet90a646e2013-12-19 18:32:04 +00001644 // Selects operate on i1, however, Op0Reg is 8 bits width and may contain
1645 // garbage. Indeed, only the less significant bit is supposed to be accurate.
1646 // If we read more than the lsb, we may see non-zero values whereas lsb
1647 // is zero. Therefore, we have to truncate Op0Reg to i1 for the select.
Alp Tokercb402912014-01-24 17:20:08 +00001648 // This is achieved by performing TEST against 1.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001649 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Quentin Colombet90a646e2013-12-19 18:32:04 +00001650 .addReg(Op0Reg).addImm(1);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001651 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001652 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001653 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001654 UpdateValueMap(I, ResultReg);
1655 return true;
1656}
1657
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001658bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001659 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001660 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001661 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001662 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001663 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001664 unsigned OpReg = getRegForValue(V);
1665 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001666 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001667 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001668 TII.get(X86::CVTSS2SDrr), ResultReg)
1669 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001670 UpdateValueMap(I, ResultReg);
1671 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001672 }
1673 }
1674
1675 return false;
1676}
1677
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001678bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001679 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001680 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001681 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001682 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001683 unsigned OpReg = getRegForValue(V);
1684 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001685 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001686 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001687 TII.get(X86::CVTSD2SSrr), ResultReg)
1688 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001689 UpdateValueMap(I, ResultReg);
1690 return true;
1691 }
1692 }
1693 }
1694
1695 return false;
1696}
1697
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001698bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001699 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1700 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001701
Eli Friedmanc7035512011-05-25 23:49:02 +00001702 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001703 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001704 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001705 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001706 return false;
1707
1708 unsigned InputReg = getRegForValue(I->getOperand(0));
1709 if (!InputReg)
1710 // Unhandled operand. Halt "fast" selection and bail.
1711 return false;
1712
Eli Friedmanc7035512011-05-25 23:49:02 +00001713 if (SrcVT == MVT::i8) {
1714 // Truncate from i8 to i1; no code needed.
1715 UpdateValueMap(I, InputReg);
1716 return true;
1717 }
Evan Chengb9286692008-09-07 08:47:42 +00001718
Eli Friedmanc7035512011-05-25 23:49:02 +00001719 if (!Subtarget->is64Bit()) {
1720 // If we're on x86-32; we can't extract an i8 from a general register.
1721 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001722 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1723 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1724 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001725 unsigned CopyReg = createResultReg(CopyRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001726 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Eli Friedmanc7035512011-05-25 23:49:02 +00001727 CopyReg).addReg(InputReg);
1728 InputReg = CopyReg;
1729 }
1730
1731 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001732 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001733 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001734 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001735 if (!ResultReg)
1736 return false;
1737
1738 UpdateValueMap(I, ResultReg);
1739 return true;
1740}
1741
Eli Friedman60afcc22011-05-20 22:21:04 +00001742bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1743 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1744}
1745
Eli Friedmanbcc69142011-04-27 01:45:07 +00001746bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1747 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001748
Eli Friedmanbcc69142011-04-27 01:45:07 +00001749 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001750 if (!IsMemcpySmall(Len))
1751 return false;
1752
1753 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001754
1755 // We don't care about alignment here since we just emit integer accesses.
1756 while (Len) {
1757 MVT VT;
1758 if (Len >= 8 && i64Legal)
1759 VT = MVT::i64;
1760 else if (Len >= 4)
1761 VT = MVT::i32;
1762 else if (Len >= 2)
1763 VT = MVT::i16;
1764 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001765 VT = MVT::i8;
1766 }
1767
1768 unsigned Reg;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001769 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
1770 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
Eli Friedmanbcc69142011-04-27 01:45:07 +00001771 assert(RV && "Failed to emit load or store??");
1772
1773 unsigned Size = VT.getSizeInBits()/8;
1774 Len -= Size;
1775 DestAM.Disp += Size;
1776 SrcAM.Disp += Size;
1777 }
1778
1779 return true;
1780}
1781
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001782static bool isCommutativeIntrinsic(IntrinsicInst const &I) {
1783 switch (I.getIntrinsicID()) {
1784 case Intrinsic::sadd_with_overflow:
1785 case Intrinsic::uadd_with_overflow:
1786 case Intrinsic::smul_with_overflow:
1787 case Intrinsic::umul_with_overflow:
1788 return true;
1789 default:
1790 return false;
1791 }
1792}
1793
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001794bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001795 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001796 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001797 default: return false;
Juergen Ributzka4dc95872014-06-11 21:44:44 +00001798 case Intrinsic::frameaddress: {
1799 Type *RetTy = I.getCalledFunction()->getReturnType();
1800
1801 MVT VT;
1802 if (!isTypeLegal(RetTy, VT))
1803 return false;
1804
1805 unsigned Opc;
1806 const TargetRegisterClass *RC = nullptr;
1807
1808 switch (VT.SimpleTy) {
1809 default: llvm_unreachable("Invalid result type for frameaddress.");
1810 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
1811 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
1812 }
1813
1814 // This needs to be set before we call getFrameRegister, otherwise we get
1815 // the wrong frame register.
1816 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
1817 MFI->setFrameAddressIsTaken(true);
1818
1819 const X86RegisterInfo *RegInfo =
1820 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
1821 unsigned FrameReg = RegInfo->getFrameRegister(*(FuncInfo.MF));
1822 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
1823 (FrameReg == X86::EBP && VT == MVT::i32)) &&
1824 "Invalid Frame Register!");
1825
1826 // Always make a copy of the frame register to to a vreg first, so that we
1827 // never directly reference the frame register (the TwoAddressInstruction-
1828 // Pass doesn't like that).
1829 unsigned SrcReg = createResultReg(RC);
1830 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1831 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
1832
1833 // Now recursively load from the frame address.
1834 // movq (%rbp), %rax
1835 // movq (%rax), %rax
1836 // movq (%rax), %rax
1837 // ...
1838 unsigned DestReg;
1839 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
1840 while (Depth--) {
1841 DestReg = createResultReg(RC);
1842 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1843 TII.get(Opc), DestReg), SrcReg);
1844 SrcReg = DestReg;
1845 }
1846
1847 UpdateValueMap(&I, SrcReg);
1848 return true;
1849 }
Chris Lattner91328b32011-04-19 05:52:03 +00001850 case Intrinsic::memcpy: {
1851 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1852 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001853 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001854 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001855
Eli Friedmancd2124a2011-06-10 23:39:36 +00001856 if (isa<ConstantInt>(MCI.getLength())) {
1857 // Small memcpy's are common enough that we want to do them
1858 // without a call if possible.
1859 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1860 if (IsMemcpySmall(Len)) {
1861 X86AddressMode DestAM, SrcAM;
1862 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1863 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1864 return false;
1865 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1866 return true;
1867 }
1868 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001869
Eli Friedmancd2124a2011-06-10 23:39:36 +00001870 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1871 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001872 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001873
Eli Friedmancd2124a2011-06-10 23:39:36 +00001874 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1875 return false;
1876
1877 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001878 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001879 case Intrinsic::memset: {
1880 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001881
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001882 if (MSI.isVolatile())
1883 return false;
1884
Eli Friedmancd2124a2011-06-10 23:39:36 +00001885 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1886 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1887 return false;
1888
1889 if (MSI.getDestAddressSpace() > 255)
1890 return false;
1891
1892 return DoSelectCall(&I, "memset");
1893 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001894 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00001895 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001896 EVT PtrTy = TLI.getPointerTy();
1897
Gabor Greif83205af2010-06-26 11:51:52 +00001898 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1899 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001900
Josh Magee22b8ba22013-12-19 03:17:11 +00001901 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
1902
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001903 // Grab the frame index.
1904 X86AddressMode AM;
1905 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00001906 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001907 return true;
1908 }
Dale Johannesend5575f22010-01-26 00:09:58 +00001909 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001910 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00001911 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00001912 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00001913 if (!X86SelectAddress(DI->getAddress(), AM))
1914 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001915 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00001916 // FIXME may need to add RegState::Debug to any registers produced,
1917 // although ESP/EBP should be the only ones at the moment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001918 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001919 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00001920 return true;
1921 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001922 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001923 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001924 return true;
1925 }
Juergen Ributzka272b5702014-06-11 23:11:02 +00001926 case Intrinsic::sqrt: {
1927 if (!Subtarget->hasSSE1())
1928 return false;
1929
1930 Type *RetTy = I.getCalledFunction()->getReturnType();
1931
1932 MVT VT;
1933 if (!isTypeLegal(RetTy, VT))
1934 return false;
1935
1936 // Unfortunatelly we can't use FastEmit_r, because the AVX version of FSQRT
1937 // is not generated by FastISel yet.
1938 // FIXME: Update this code once tablegen can handle it.
1939 static const unsigned SqrtOpc[2][2] = {
1940 {X86::SQRTSSr, X86::VSQRTSSr},
1941 {X86::SQRTSDr, X86::VSQRTSDr}
1942 };
1943 bool HasAVX = Subtarget->hasAVX();
1944 unsigned Opc;
1945 const TargetRegisterClass *RC;
1946 switch (VT.SimpleTy) {
1947 default: return false;
1948 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
1949 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
1950 }
1951
1952 const Value *SrcVal = I.getArgOperand(0);
1953 unsigned SrcReg = getRegForValue(SrcVal);
1954
1955 if (SrcReg == 0)
1956 return false;
1957
1958 unsigned ImplicitDefReg = 0;
1959 if (HasAVX) {
1960 ImplicitDefReg = createResultReg(RC);
1961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1962 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
1963 }
1964
1965 unsigned ResultReg = createResultReg(RC);
1966 MachineInstrBuilder MIB;
1967 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1968 ResultReg);
1969
1970 if (ImplicitDefReg)
1971 MIB.addReg(ImplicitDefReg);
1972
1973 MIB.addReg(SrcReg);
1974
1975 UpdateValueMap(&I, ResultReg);
1976 return true;
1977 }
Bill Wendling80b34b32008-12-09 02:42:50 +00001978 case Intrinsic::sadd_with_overflow:
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001979 case Intrinsic::uadd_with_overflow:
1980 case Intrinsic::ssub_with_overflow:
1981 case Intrinsic::usub_with_overflow:
1982 case Intrinsic::smul_with_overflow:
1983 case Intrinsic::umul_with_overflow: {
1984 // This implements the basic lowering of the xalu with overflow intrinsics
1985 // into add/sub/mul folowed by either seto or setb.
Bill Wendling80b34b32008-12-09 02:42:50 +00001986 const Function *Callee = I.getCalledFunction();
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001987 auto *Ty = cast<StructType>(Callee->getReturnType());
1988 Type *RetTy = Ty->getTypeAtIndex(0U);
1989 Type *CondTy = Ty->getTypeAtIndex(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00001990
Duncan Sandsf5dda012010-11-03 11:35:31 +00001991 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00001992 if (!isTypeLegal(RetTy, VT))
1993 return false;
1994
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001995 if (VT < MVT::i8 || VT > MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00001996 return false;
1997
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001998 const Value *LHS = I.getArgOperand(0);
1999 const Value *RHS = I.getArgOperand(1);
2000
2001 // Canonicalize immediates to the RHS.
2002 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2003 isCommutativeIntrinsic(I))
2004 std::swap(LHS, RHS);
2005
2006 unsigned BaseOpc, CondOpc;
2007 switch (I.getIntrinsicID()) {
2008 default: llvm_unreachable("Unexpected intrinsic!");
2009 case Intrinsic::sadd_with_overflow:
2010 BaseOpc = ISD::ADD; CondOpc = X86::SETOr; break;
2011 case Intrinsic::uadd_with_overflow:
2012 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
2013 case Intrinsic::ssub_with_overflow:
2014 BaseOpc = ISD::SUB; CondOpc = X86::SETOr; break;
2015 case Intrinsic::usub_with_overflow:
2016 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
2017 case Intrinsic::smul_with_overflow:
2018 BaseOpc = ISD::MUL; CondOpc = X86::SETOr; break;
2019 case Intrinsic::umul_with_overflow:
2020 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
2021 }
2022
2023 unsigned LHSReg = getRegForValue(LHS);
2024 if (LHSReg == 0)
2025 return false;
2026 bool LHSIsKill = hasTrivialKill(LHS);
2027
2028 unsigned ResultReg = 0;
2029 // Check if we have an immediate version.
2030 if (auto const *C = dyn_cast<ConstantInt>(RHS)) {
2031 ResultReg = FastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2032 C->getZExtValue());
2033 }
2034
2035 unsigned RHSReg;
2036 bool RHSIsKill;
2037 if (!ResultReg) {
2038 RHSReg = getRegForValue(RHS);
2039 if (RHSReg == 0)
2040 return false;
2041 RHSIsKill = hasTrivialKill(RHS);
2042 ResultReg = FastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2043 RHSIsKill);
2044 }
2045
2046 // FastISel doesn't have a pattern for X86::MUL*r. Emit it manually.
2047 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
2048 static const unsigned MULOpc[] =
2049 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
2050 static const unsigned Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
2051 // First copy the first operand into RAX, which is an implicit input to
2052 // the X86::MUL*r instruction.
2053 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2054 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2055 .addReg(LHSReg, getKillRegState(LHSIsKill));
2056 ResultReg = FastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2057 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
2058 }
2059
2060 if (!ResultReg)
Bill Wendling80b34b32008-12-09 02:42:50 +00002061 return false;
2062
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002063 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
2064 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
2065 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
2066 ResultReg2);
Eli Friedmana4d4a012011-05-16 21:06:17 +00002067
2068 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00002069 return true;
2070 }
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002071 case Intrinsic::x86_sse_cvttss2si:
2072 case Intrinsic::x86_sse_cvttss2si64:
2073 case Intrinsic::x86_sse2_cvttsd2si:
2074 case Intrinsic::x86_sse2_cvttsd2si64: {
2075 bool IsInputDouble;
2076 switch (I.getIntrinsicID()) {
2077 default: llvm_unreachable("Unexpected intrinsic.");
2078 case Intrinsic::x86_sse_cvttss2si:
2079 case Intrinsic::x86_sse_cvttss2si64:
2080 if (!Subtarget->hasSSE1())
2081 return false;
2082 IsInputDouble = false;
2083 break;
2084 case Intrinsic::x86_sse2_cvttsd2si:
2085 case Intrinsic::x86_sse2_cvttsd2si64:
2086 if (!Subtarget->hasSSE2())
2087 return false;
2088 IsInputDouble = true;
2089 break;
2090 }
2091
2092 Type *RetTy = I.getCalledFunction()->getReturnType();
2093 MVT VT;
2094 if (!isTypeLegal(RetTy, VT))
2095 return false;
2096
2097 static const unsigned CvtOpc[2][2][2] = {
2098 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
2099 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
2100 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
2101 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
2102 };
2103 bool HasAVX = Subtarget->hasAVX();
2104 unsigned Opc;
2105 switch (VT.SimpleTy) {
2106 default: llvm_unreachable("Unexpected result type.");
2107 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
2108 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
2109 }
2110
2111 // Check if we can fold insertelement instructions into the convert.
2112 const Value *Op = I.getArgOperand(0);
2113 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
2114 const Value *Index = IE->getOperand(2);
2115 if (!isa<ConstantInt>(Index))
2116 break;
2117 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
2118
2119 if (Idx == 0) {
2120 Op = IE->getOperand(1);
2121 break;
2122 }
2123 Op = IE->getOperand(0);
2124 }
2125
2126 unsigned Reg = getRegForValue(Op);
2127 if (Reg == 0)
2128 return false;
2129
2130 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
2131 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2132 .addReg(Reg);
2133
2134 UpdateValueMap(&I, ResultReg);
2135 return true;
2136 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002137 }
2138}
2139
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002140bool X86FastISel::FastLowerArguments() {
2141 if (!FuncInfo.CanLowerReturn)
2142 return false;
2143
2144 const Function *F = FuncInfo.Fn;
2145 if (F->isVarArg())
2146 return false;
2147
2148 CallingConv::ID CC = F->getCallingConv();
2149 if (CC != CallingConv::C)
2150 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00002151
2152 if (Subtarget->isCallingConvWin64(CC))
2153 return false;
2154
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002155 if (!Subtarget->is64Bit())
2156 return false;
2157
2158 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002159 unsigned GPRCnt = 0;
2160 unsigned FPRCnt = 0;
2161 unsigned Idx = 0;
2162 for (auto const &Arg : F->args()) {
2163 // The first argument is at index 1.
2164 ++Idx;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002165 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2166 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2167 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2168 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2169 return false;
2170
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002171 Type *ArgTy = Arg.getType();
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002172 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
2173 return false;
2174
2175 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00002176 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002177 switch (ArgVT.getSimpleVT().SimpleTy) {
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002178 default: return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002179 case MVT::i32:
2180 case MVT::i64:
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002181 ++GPRCnt;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002182 break;
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002183 case MVT::f32:
2184 case MVT::f64:
2185 if (!Subtarget->hasSSE1())
2186 return false;
2187 ++FPRCnt;
2188 break;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002189 }
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002190
2191 if (GPRCnt > 6)
2192 return false;
2193
2194 if (FPRCnt > 8)
2195 return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002196 }
2197
Craig Topper840beec2014-04-04 05:16:06 +00002198 static const MCPhysReg GPR32ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002199 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
2200 };
Craig Topper840beec2014-04-04 05:16:06 +00002201 static const MCPhysReg GPR64ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002202 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
2203 };
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002204 static const MCPhysReg XMMArgRegs[] = {
2205 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2206 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2207 };
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002208
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002209 unsigned GPRIdx = 0;
2210 unsigned FPRIdx = 0;
2211 for (auto const &Arg : F->args()) {
2212 MVT VT = TLI.getSimpleValueType(Arg.getType());
2213 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
2214 unsigned SrcReg;
2215 switch (VT.SimpleTy) {
2216 default: llvm_unreachable("Unexpected value type.");
2217 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
2218 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
2219 case MVT::f32: // fall-through
2220 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
2221 }
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002222 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2223 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2224 // Without this, EmitLiveInCopies may eliminate the livein if its only
2225 // use is a bitcast (which isn't turned into an instruction).
2226 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002227 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002228 TII.get(TargetOpcode::COPY), ResultReg)
2229 .addReg(DstReg, getKillRegState(true));
2230 UpdateValueMap(&Arg, ResultReg);
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002231 }
2232 return true;
2233}
2234
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002235bool X86FastISel::X86SelectCall(const Instruction *I) {
2236 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00002237 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002238
2239 // Can't handle inline asm yet.
2240 if (isa<InlineAsm>(Callee))
2241 return false;
2242
Bill Wendling80b34b32008-12-09 02:42:50 +00002243 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002244 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00002245 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002246
Chad Rosierdf42cf32012-12-11 00:18:02 +00002247 // Allow SelectionDAG isel to handle tail calls.
2248 if (cast<CallInst>(I)->isTailCall())
2249 return false;
2250
Craig Topper062a2ba2014-04-25 05:30:21 +00002251 return DoSelectCall(I, nullptr);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002252}
2253
Rafael Espindola73173c52012-07-25 15:42:45 +00002254static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
2255 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002256 if (Subtarget.is64Bit())
2257 return 0;
Rafael Espindola32cb5ac2013-12-12 16:06:58 +00002258 if (Subtarget.getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002259 return 0;
2260 CallingConv::ID CC = CS.getCallingConv();
2261 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
2262 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002263 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002264 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002265 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00002266 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002267 return 4;
2268}
2269
Eli Friedmancd2124a2011-06-10 23:39:36 +00002270// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
2271bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
2272 const CallInst *CI = cast<CallInst>(I);
2273 const Value *Callee = CI->getCalledValue();
2274
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002275 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002276 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002277 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00002278 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002279 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00002280 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
2281 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002282 return false;
2283
Evan Chengd10089a2010-01-27 00:00:57 +00002284 // fastcc with -tailcallopt is intended to provide a guaranteed
2285 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002286 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00002287 return false;
2288
Chris Lattner229907c2011-07-18 04:54:35 +00002289 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2290 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002291 bool isVarArg = FTy->isVarArg();
2292
2293 // Don't know how to handle Win64 varargs yet. Nothing special needed for
2294 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00002295 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002296 return false;
2297
Reid Klecknerf5b76512014-01-31 23:50:57 +00002298 // Don't know about inalloca yet.
2299 if (CS.hasInAllocaArgument())
2300 return false;
2301
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002302 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00002303 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002304 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002305 return false;
2306
Eli Friedman7b279422011-05-17 18:29:03 +00002307 // Check whether the function can return without sret-demotion.
2308 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002309 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00002310 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00002311 *FuncInfo.MF, FTy->isVarArg(),
2312 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002313 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00002314 return false;
2315
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002316 // Materialize callee address in a register. FIXME: GV address can be
2317 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00002318 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00002319 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00002320 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002321 unsigned CalleeOp = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002322 const GlobalValue *GV = nullptr;
2323 if (CalleeAM.GV != nullptr) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002324 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00002325 } else if (CalleeAM.Base.Reg != 0) {
2326 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002327 } else
2328 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002329
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002330 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002331 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002332 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002333 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002334 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00002335 unsigned arg_size = CS.arg_size();
2336 Args.reserve(arg_size);
2337 ArgVals.reserve(arg_size);
2338 ArgVTs.reserve(arg_size);
2339 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002340 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002341 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00002342 // If we're lowering a mem intrinsic instead of a regular call, skip the
2343 // last two arguments, which should not passed to the underlying functions.
2344 if (MemIntName && e-i <= 2)
2345 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002346 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002347 ISD::ArgFlagsTy Flags;
2348 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002349 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002350 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002351 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002352 Flags.setZExt();
2353
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002354 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00002355 PointerType *Ty = cast<PointerType>(ArgVal->getType());
2356 Type *ElementTy = Ty->getElementType();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002357 unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
Eli Friedman60afcc22011-05-20 22:21:04 +00002358 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
2359 if (!FrameAlign)
2360 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
2361 Flags.setByVal();
2362 Flags.setByValSize(FrameSize);
2363 Flags.setByValAlign(FrameAlign);
2364 if (!IsMemcpySmall(FrameSize))
2365 return false;
2366 }
2367
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002368 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00002369 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002370 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00002371 Flags.setNest();
2372
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002373 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
2374 // instruction. This is safe because it is common to all fastisel supported
2375 // calling conventions on x86.
2376 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
2377 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
2378 CI->getBitWidth() == 16) {
2379 if (Flags.isSExt())
2380 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
2381 else
2382 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
2383 }
2384 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002385
Chris Lattner5f4b7832011-04-19 05:09:50 +00002386 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002387
Chris Lattner34a08c22011-04-19 05:15:59 +00002388 // Passing bools around ends up doing a trunc to i1 and passing it.
2389 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00002390 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2391 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2392 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002393 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2394 ArgReg = getRegForValue(ArgVal);
2395 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002396
Chris Lattner5f4b7832011-04-19 05:09:50 +00002397 MVT ArgVT;
2398 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002399
Chris Lattner5f4b7832011-04-19 05:09:50 +00002400 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2401 ArgVal->hasOneUse(), 1);
2402 } else {
2403 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002404 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002405
Chris Lattner34a08c22011-04-19 05:15:59 +00002406 if (ArgReg == 0) return false;
2407
Chris Lattner229907c2011-07-18 04:54:35 +00002408 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002409 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002410 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002411 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002412 if (ArgVT == MVT::x86mmx)
2413 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002414 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002415 Flags.setOrigAlign(OriginalAlignment);
2416
Chris Lattner5f4b7832011-04-19 05:09:50 +00002417 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002418 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002419 ArgVTs.push_back(ArgVT);
2420 ArgFlags.push_back(Flags);
2421 }
2422
2423 // Analyze operands of the call, assigning locations to each operand.
2424 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002425 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002426 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002427
Dan Gohman47a07242010-06-01 21:09:47 +00002428 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002429 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002430 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002431
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002432 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002433
2434 // Get a count of how many bytes are to be pushed on the stack.
2435 unsigned NumBytes = CCInfo.getNextStackOffset();
2436
2437 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002438 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002439 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002440 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002441
Chris Lattner3ba29352008-10-15 05:30:52 +00002442 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002443 // copies / loads.
2444 SmallVector<unsigned, 4> RegArgs;
2445 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2446 CCValAssign &VA = ArgLocs[i];
2447 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002448 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002449
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002450 // Promote the value if needed.
2451 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002452 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002453 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002454 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2455 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002456 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2457 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002458 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002459 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002460 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002461 }
2462 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002463 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2464 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002465 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2466 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002467 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002468 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002469 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002470 }
2471 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002472 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2473 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002474 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2475 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002476 if (!Emitted)
2477 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002478 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002479 if (!Emitted)
2480 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2481 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002482
Chris Lattner2d7df022011-01-05 22:26:52 +00002483 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002484 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002485 break;
2486 }
Dan Gohman8c795692009-08-05 05:33:42 +00002487 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002488 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002489 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002490 assert(BC != 0 && "Failed to emit a bitcast!");
2491 Arg = BC;
2492 ArgVT = VA.getLocVT();
2493 break;
2494 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002495 case CCValAssign::VExt:
2496 // VExt has not been implemented, so this should be impossible to reach
2497 // for now. However, fallback to Selection DAG isel once implemented.
2498 return false;
2499 case CCValAssign::Indirect:
2500 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2501 // support this.
2502 return false;
Lang Hames06234ec2014-01-14 19:56:36 +00002503 case CCValAssign::FPExt:
2504 llvm_unreachable("Unexpected loc info!");
Evan Cheng6500d172008-09-08 06:35:17 +00002505 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002506
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002507 if (VA.isRegLoc()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002508 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2509 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002510 RegArgs.push_back(VA.getLocReg());
2511 } else {
2512 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002513 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002514 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2515 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002516 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002517 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002518 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002519 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002520
Eli Friedman60afcc22011-05-20 22:21:04 +00002521 if (Flags.isByVal()) {
2522 X86AddressMode SrcAM;
2523 SrcAM.Base.Reg = Arg;
2524 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2525 assert(Res && "memcpy length already checked!"); (void)Res;
2526 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2527 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002528 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002529 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002530 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2531 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002532 } else {
Juergen Ributzka349777d2014-06-12 23:27:57 +00002533 if (!X86FastEmitStore(ArgVT, Arg, /*ValIsKill=*/false, AM))
Lang Hames7d2f7b52011-10-18 22:11:33 +00002534 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002535 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002536 }
2537 }
2538
Dan Gohman3691d502008-09-25 15:24:26 +00002539 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002540 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002541 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002542 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2544 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002545 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002546
Charles Davise8f297c2013-07-12 06:02:35 +00002547 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002548 // Count the number of XMM registers allocated.
Craig Topper840beec2014-04-04 05:16:06 +00002549 static const MCPhysReg XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002550 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2551 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2552 };
2553 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002554 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002555 X86::AL).addImm(NumXMMRegs);
2556 }
2557
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002558 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002559 MachineInstrBuilder MIB;
2560 if (CalleeOp) {
2561 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002562 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002563 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002564 CallOpc = X86::CALL64r;
2565 else
2566 CallOpc = X86::CALL32r;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002567 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002568 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002569
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002570 } else {
2571 // Direct call.
2572 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002573 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002574 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002575 CallOpc = X86::CALL64pcrel32;
2576 else
2577 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002578
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002579 // See if we need any target-specific flags on the GV operand.
2580 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002581
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002582 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2583 // external symbols most go through the PLT in PIC mode. If the symbol
2584 // has hidden or protected visibility, or if it is static or local, then
2585 // we don't need to use the PLT - we can directly call it.
2586 if (Subtarget->isTargetELF() &&
2587 TM.getRelocationModel() == Reloc::PIC_ &&
2588 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2589 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002590 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002591 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002592 (!Subtarget->getTargetTriple().isMacOSX() ||
2593 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002594 // PC-relative references to external symbols should go through $stub,
2595 // unless we're building with the leopard linker or later, which
2596 // automatically synthesizes these stubs.
2597 OpFlags = X86II::MO_DARWIN_STUB;
2598 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002599
2600
Rafael Espindolaea09c592014-02-18 22:05:46 +00002601 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Eli Friedmancd2124a2011-06-10 23:39:36 +00002602 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002603 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002604 else
2605 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002606 }
Dan Gohman3691d502008-09-25 15:24:26 +00002607
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002608 // Add a register mask with the call-preserved registers.
2609 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2610 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2611
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002612 // Add an implicit use GOT pointer in EBX.
2613 if (Subtarget->isPICStyleGOT())
2614 MIB.addReg(X86::EBX, RegState::Implicit);
2615
Charles Davise8f297c2013-07-12 06:02:35 +00002616 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002617 MIB.addReg(X86::AL, RegState::Implicit);
2618
2619 // Add implicit physical register uses to the call.
2620 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2621 MIB.addReg(RegArgs[i], RegState::Implicit);
2622
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002623 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002624 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002625 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002626 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002627 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002628
Eli Friedman7b279422011-05-17 18:29:03 +00002629 // Build info for return calling conv lowering code.
2630 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2631 SmallVector<ISD::InputArg, 32> Ins;
2632 SmallVector<EVT, 4> RetTys;
2633 ComputeValueVTs(TLI, I->getType(), RetTys);
2634 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2635 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002636 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002637 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2638 for (unsigned j = 0; j != NumRegs; ++j) {
2639 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002640 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002641 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002642 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002643 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002644 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002645 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002646 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002647 MyFlags.Flags.setInReg();
2648 Ins.push_back(MyFlags);
2649 }
2650 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002651
Eli Friedman7b279422011-05-17 18:29:03 +00002652 // Now handle call return values.
2653 SmallVector<unsigned, 4> UsedRegs;
2654 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002655 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002656 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002657 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2658 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2659 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2660 EVT CopyVT = RVLocs[i].getValVT();
2661 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002662
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002663 // If this is a call to a function that returns an fp value on the x87 fp
2664 // stack, but where we prefer to use the value in xmm registers, copy it
2665 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002666 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002667 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002668 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002669 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002670 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002671 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00002672 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2673 TII.get(X86::FpPOP_RETVAL), CopyReg);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002674 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002675 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2676 TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002677 CopyReg).addReg(RVLocs[i].getLocReg());
2678 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002679 }
2680
Eli Friedman7b279422011-05-17 18:29:03 +00002681 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002682 // Round the F80 the right size, which also moves to the appropriate xmm
2683 // register. This is accomplished by storing the F80 value in memory and
2684 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002685 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002686 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002687 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002688 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002689 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002690 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002691 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002692 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002693 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Friedman7b279422011-05-17 18:29:03 +00002694 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002695 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002696 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002697
Eli Friedman7b279422011-05-17 18:29:03 +00002698 if (RVLocs.size())
2699 UpdateValueMap(I, ResultReg, RVLocs.size());
2700
Dan Gohman86936502010-06-18 23:28:01 +00002701 // Set all unused physreg defs as dead.
2702 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2703
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002704 return true;
2705}
2706
2707
Dan Gohmand58f3e32008-08-28 23:21:34 +00002708bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002709X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002710 switch (I->getOpcode()) {
2711 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002712 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002713 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002714 case Instruction::Store:
2715 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002716 case Instruction::Ret:
2717 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002718 case Instruction::ICmp:
2719 case Instruction::FCmp:
2720 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002721 case Instruction::ZExt:
2722 return X86SelectZExt(I);
2723 case Instruction::Br:
2724 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002725 case Instruction::Call:
2726 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002727 case Instruction::LShr:
2728 case Instruction::AShr:
2729 case Instruction::Shl:
2730 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002731 case Instruction::SDiv:
2732 case Instruction::UDiv:
2733 case Instruction::SRem:
2734 case Instruction::URem:
2735 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002736 case Instruction::Select:
2737 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002738 case Instruction::Trunc:
2739 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002740 case Instruction::FPExt:
2741 return X86SelectFPExt(I);
2742 case Instruction::FPTrunc:
2743 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002744 case Instruction::IntToPtr: // Deliberate fall-through.
2745 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002746 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2747 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002748 if (DstVT.bitsGT(SrcVT))
2749 return X86SelectZExt(I);
2750 if (DstVT.bitsLT(SrcVT))
2751 return X86SelectTrunc(I);
2752 unsigned Reg = getRegForValue(I->getOperand(0));
2753 if (Reg == 0) return false;
2754 UpdateValueMap(I, Reg);
2755 return true;
2756 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002757 }
2758
2759 return false;
2760}
2761
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002762unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002763 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002764 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002765 return 0;
2766
2767 // Can't handle alternate code models yet.
2768 if (TM.getCodeModel() != CodeModel::Small)
2769 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002770
Owen Anderson50288e32008-09-05 00:06:23 +00002771 // Get opcode and regclass of the output for the given load instruction.
2772 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002773 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002774 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002775 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002776 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002777 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002778 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002779 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002780 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002781 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002782 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002783 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002784 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002785 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002786 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002787 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002788 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002789 // Must be in x86-64 mode.
2790 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002791 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002792 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002793 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002794 if (X86ScalarSSEf32) {
2795 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002796 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002797 } else {
2798 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002799 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002800 }
2801 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002802 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002803 if (X86ScalarSSEf64) {
2804 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002805 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002806 } else {
2807 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002808 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002809 }
2810 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002811 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002812 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002813 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002814 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002815
Dan Gohman9801ba42008-09-19 22:16:54 +00002816 // Materialize addresses with LEA instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002817 if (isa<GlobalValue>(C)) {
Louis Gerbargdcf00252014-06-16 20:31:50 +00002818 // LEA can only handle 32 bit immediates. Currently this happens pretty
2819 // rarely, so rather than deal with it just bail out of fast isel. If any
2820 // architectures endis up needing to use this path a lot then fast isel
2821 // could get the address with a MOV64ri and use that to load the value.
Louis Gerbarga5360c42014-06-16 17:35:40 +00002822 if (TM.getRelocationModel() == Reloc::Static && Subtarget->is64Bit())
2823 return false;
2824
Dan Gohman9801ba42008-09-19 22:16:54 +00002825 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002826 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002827 // If the expression is just a basereg, then we're done, otherwise we need
2828 // to emit an LEA.
2829 if (AM.BaseType == X86AddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00002830 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
Chris Lattner48326602011-04-17 17:12:08 +00002831 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002832
Chris Lattner48326602011-04-17 17:12:08 +00002833 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman9801ba42008-09-19 22:16:54 +00002834 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002835 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002836 TII.get(Opc), ResultReg), AM);
Owen Anderson50288e32008-09-05 00:06:23 +00002837 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002838 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002839 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002840 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002841
Owen Andersond41c7162008-09-06 01:11:01 +00002842 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002843 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002844 if (Align == 0) {
2845 // Alignment of vector types. FIXME!
Rafael Espindolaea09c592014-02-18 22:05:46 +00002846 Align = DL.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002847 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002848
Dan Gohman8392f0c2008-09-30 01:21:32 +00002849 // x86-32 PIC requires a PIC base register for constant pools.
2850 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002851 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002852 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002853 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002854 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002855 } else if (Subtarget->isPICStyleGOT()) {
2856 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002857 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002858 } else if (Subtarget->isPICStyleRIPRel() &&
2859 TM.getCodeModel() == CodeModel::Small) {
2860 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002861 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002862
2863 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002864 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002865 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002866 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002867 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002868 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002869
Owen Anderson50288e32008-09-05 00:06:23 +00002870 return ResultReg;
2871}
2872
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002873unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002874 // Fail on dynamic allocas. At this point, getRegForValue has already
2875 // checked its CSE maps, so if we're here trying to handle a dynamic
2876 // alloca, we're not going to succeed. X86SelectAddress has a
2877 // check for dynamic allocas, because it's called directly from
2878 // various places, but TargetMaterializeAlloca also needs a check
2879 // in order to avoid recursion between getRegForValue,
2880 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002881 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002882 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00002883 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002884
Dan Gohman39d82f92008-09-10 20:11:02 +00002885 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002886 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002887 return 0;
2888 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00002889 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00002890 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002891 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002892 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00002893 return ResultReg;
2894}
2895
Eli Friedman406c4712011-04-27 22:41:55 +00002896unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2897 MVT VT;
2898 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002899 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002900
2901 // Get opcode and regclass for the given zero.
2902 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002903 const TargetRegisterClass *RC = nullptr;
Eli Friedman406c4712011-04-27 22:41:55 +00002904 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002905 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00002906 case MVT::f32:
2907 if (X86ScalarSSEf32) {
2908 Opc = X86::FsFLD0SS;
2909 RC = &X86::FR32RegClass;
2910 } else {
2911 Opc = X86::LD_Fp032;
2912 RC = &X86::RFP32RegClass;
2913 }
2914 break;
2915 case MVT::f64:
2916 if (X86ScalarSSEf64) {
2917 Opc = X86::FsFLD0SD;
2918 RC = &X86::FR64RegClass;
2919 } else {
2920 Opc = X86::LD_Fp064;
2921 RC = &X86::RFP64RegClass;
2922 }
2923 break;
2924 case MVT::f80:
2925 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002926 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002927 }
2928
2929 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002930 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Eli Friedman406c4712011-04-27 22:41:55 +00002931 return ResultReg;
2932}
2933
2934
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002935bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2936 const LoadInst *LI) {
Juergen Ributzka349777d2014-06-12 23:27:57 +00002937 const Value *Ptr = LI->getPointerOperand();
Chris Lattnereeba0c72010-09-05 02:18:34 +00002938 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00002939 if (!X86SelectAddress(Ptr, AM))
Chris Lattnereeba0c72010-09-05 02:18:34 +00002940 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002941
Craig Topper55406d92012-08-11 17:46:16 +00002942 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00002943
Rafael Espindolaea09c592014-02-18 22:05:46 +00002944 unsigned Size = DL.getTypeAllocSize(LI->getType());
Chris Lattnereeba0c72010-09-05 02:18:34 +00002945 unsigned Alignment = LI->getAlignment();
2946
Juergen Ributzka349777d2014-06-12 23:27:57 +00002947 if (Alignment == 0) // Ensure that codegen never sees alignment 0
2948 Alignment = DL.getABITypeAlignment(LI->getType());
2949
Chris Lattnereeba0c72010-09-05 02:18:34 +00002950 SmallVector<MachineOperand, 8> AddrOps;
2951 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00002952
Chris Lattnereeba0c72010-09-05 02:18:34 +00002953 MachineInstr *Result =
2954 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
Juergen Ributzka349777d2014-06-12 23:27:57 +00002955 if (!Result)
2956 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002957
Juergen Ributzka349777d2014-06-12 23:27:57 +00002958 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Chris Lattner2d186572011-01-16 02:27:38 +00002959 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00002960 MI->eraseFromParent();
2961 return true;
2962}
2963
2964
Evan Cheng24422d42008-09-03 00:03:49 +00002965namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00002966 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2967 const TargetLibraryInfo *libInfo) {
2968 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00002969 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002970}