blob: 6a4a467f6a9a70632a5898449c18da1e720fb2fe [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 Ributzka2da1bbc2014-06-16 23:58:24 +0000158static std::pair<X86::CondCode, bool>
159getX86ConditonCode(CmpInst::Predicate Predicate) {
160 X86::CondCode CC = X86::COND_INVALID;
161 bool NeedSwap = false;
162 switch (Predicate) {
163 default: break;
164 // Floating-point Predicates
165 case CmpInst::FCMP_UEQ: CC = X86::COND_E; break;
166 case CmpInst::FCMP_OLT: NeedSwap = true; // fall-through
167 case CmpInst::FCMP_OGT: CC = X86::COND_A; break;
168 case CmpInst::FCMP_OLE: NeedSwap = true; // fall-through
169 case CmpInst::FCMP_OGE: CC = X86::COND_AE; break;
170 case CmpInst::FCMP_UGT: NeedSwap = true; // fall-through
171 case CmpInst::FCMP_ULT: CC = X86::COND_B; break;
172 case CmpInst::FCMP_UGE: NeedSwap = true; // fall-through
173 case CmpInst::FCMP_ULE: CC = X86::COND_BE; break;
174 case CmpInst::FCMP_ONE: CC = X86::COND_NE; break;
175 case CmpInst::FCMP_UNO: CC = X86::COND_P; break;
176 case CmpInst::FCMP_ORD: CC = X86::COND_NP; break;
177 case CmpInst::FCMP_OEQ: // fall-through
178 case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break;
179
180 // Integer Predicates
181 case CmpInst::ICMP_EQ: CC = X86::COND_E; break;
182 case CmpInst::ICMP_NE: CC = X86::COND_NE; break;
183 case CmpInst::ICMP_UGT: CC = X86::COND_A; break;
184 case CmpInst::ICMP_UGE: CC = X86::COND_AE; break;
185 case CmpInst::ICMP_ULT: CC = X86::COND_B; break;
186 case CmpInst::ICMP_ULE: CC = X86::COND_BE; break;
187 case CmpInst::ICMP_SGT: CC = X86::COND_G; break;
188 case CmpInst::ICMP_SGE: CC = X86::COND_GE; break;
189 case CmpInst::ICMP_SLT: CC = X86::COND_L; break;
190 case CmpInst::ICMP_SLE: CC = X86::COND_LE; break;
191 }
192
193 return std::make_pair(CC, NeedSwap);
194}
195
Chris Lattner229907c2011-07-18 04:54:35 +0000196bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000197 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
198 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000199 // Unhandled type. Halt "fast" selection and bail.
200 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000201
202 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000203 // For now, require SSE/SSE2 for performing floating-point operations,
204 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000205 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000206 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000207 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000208 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000209 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000210 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000211 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000212 // We only handle legal types. For example, on x86-32 the instruction
213 // selector contains all of the 64-bit instructions from x86-64,
214 // under the assumption that i64 won't be used if the target doesn't
215 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000216 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000217}
218
219#include "X86GenCallingConv.inc"
220
Evan Chengf5bc7e52008-09-05 21:00:03 +0000221/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000222/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000223/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000224bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000225 MachineMemOperand *MMO, unsigned &ResultReg) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000226 // Get opcode and regclass of the output for the given load instruction.
227 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000228 const TargetRegisterClass *RC = nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000229 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000230 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000231 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000232 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000233 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000234 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000235 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000236 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000237 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000238 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000239 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000240 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000241 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000242 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000243 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000244 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000245 // Must be in x86-64 mode.
246 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000247 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000248 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000249 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000250 if (X86ScalarSSEf32) {
251 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000252 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000253 } else {
254 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000255 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000256 }
257 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000258 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000259 if (X86ScalarSSEf64) {
260 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000261 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000262 } else {
263 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000264 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000265 }
266 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000267 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000268 // No f80 support yet.
269 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000270 }
271
272 ResultReg = createResultReg(RC);
Juergen Ributzka349777d2014-06-12 23:27:57 +0000273 MachineInstrBuilder MIB =
274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
275 addFullAddress(MIB, AM);
276 if (MMO)
277 MIB->addMemOperand(*FuncInfo.MF, MMO);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000278 return true;
279}
280
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000281/// X86FastEmitStore - Emit a machine instruction to store a value Val of
282/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
283/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000284/// i.e. V. Return true if it is possible.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000285bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
286 const X86AddressMode &AM,
287 MachineMemOperand *MMO, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000288 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000289 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000290 switch (VT.getSimpleVT().SimpleTy) {
291 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000292 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000293 case MVT::i1: {
294 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000295 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000296 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000297 TII.get(X86::AND8ri), AndResult)
298 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
Craig Topper4f55b0e2013-07-17 05:57:45 +0000299 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000300 }
301 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000302 case MVT::i8: Opc = X86::MOV8mr; break;
303 case MVT::i16: Opc = X86::MOV16mr; break;
304 case MVT::i32: Opc = X86::MOV32mr; break;
305 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
306 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000307 Opc = X86ScalarSSEf32 ?
308 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000309 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000310 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000311 Opc = X86ScalarSSEf64 ?
312 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000313 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000314 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000315 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000316 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000317 else
Craig Topper55475d42013-07-17 06:58:23 +0000318 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000319 break;
320 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000321 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000322 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000323 else
Craig Topperad1fff92013-07-18 07:16:44 +0000324 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000325 break;
326 case MVT::v4i32:
327 case MVT::v2i64:
328 case MVT::v8i16:
329 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000330 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000331 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000332 else
Craig Topper55475d42013-07-17 06:58:23 +0000333 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000334 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000335 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000336
Juergen Ributzka349777d2014-06-12 23:27:57 +0000337 MachineInstrBuilder MIB =
338 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
339 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
340 if (MMO)
341 MIB->addMemOperand(*FuncInfo.MF, MMO);
342
Evan Chengf5bc7e52008-09-05 21:00:03 +0000343 return true;
344}
345
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000346bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000347 const X86AddressMode &AM,
348 MachineMemOperand *MMO, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000349 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000350 if (isa<ConstantPointerNull>(Val))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000351 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000352
Chris Lattner3ba29352008-10-15 05:30:52 +0000353 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000354 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000355 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000356 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000357 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000358 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000359 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000360 case MVT::i8: Opc = X86::MOV8mi; break;
361 case MVT::i16: Opc = X86::MOV16mi; break;
362 case MVT::i32: Opc = X86::MOV32mi; break;
363 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000364 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000365 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000366 Opc = X86::MOV64mi32;
367 break;
368 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000369
Chris Lattner3ba29352008-10-15 05:30:52 +0000370 if (Opc) {
Juergen Ributzka349777d2014-06-12 23:27:57 +0000371 MachineInstrBuilder MIB =
372 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
373 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
374 : CI->getZExtValue());
375 if (MMO)
376 MIB->addMemOperand(*FuncInfo.MF, MMO);
Chris Lattner3ba29352008-10-15 05:30:52 +0000377 return true;
378 }
379 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000380
Chris Lattner3ba29352008-10-15 05:30:52 +0000381 unsigned ValReg = getRegForValue(Val);
382 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000383 return false;
384
Juergen Ributzka349777d2014-06-12 23:27:57 +0000385 bool ValKill = hasTrivialKill(Val);
386 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000387}
388
Evan Cheng6500d172008-09-08 06:35:17 +0000389/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
390/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
391/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000392bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
393 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000394 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000395 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
396 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000397 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000398 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000399
400 ResultReg = RR;
401 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000402}
403
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000404bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
405 // Handle constant address.
406 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
407 // Can't handle alternate code models yet.
408 if (TM.getCodeModel() != CodeModel::Small)
409 return false;
410
411 // Can't handle TLS yet.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000412 if (GV->isThreadLocal())
413 return false;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000414
415 // RIP-relative addresses can't have additional register operands, so if
416 // we've already folded stuff into the addressing mode, just force the
417 // global value into its own register, which we can use as the basereg.
418 if (!Subtarget->isPICStyleRIPRel() ||
419 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
420 // Okay, we've committed to selecting this global. Set up the address.
421 AM.GV = GV;
422
423 // Allow the subtarget to classify the global.
424 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
425
426 // If this reference is relative to the pic base, set it now.
427 if (isGlobalRelativeToPICBase(GVFlags)) {
428 // FIXME: How do we know Base.Reg is free??
429 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
430 }
431
432 // Unless the ABI requires an extra load, return a direct reference to
433 // the global.
434 if (!isGlobalStubReference(GVFlags)) {
435 if (Subtarget->isPICStyleRIPRel()) {
436 // Use rip-relative addressing if we can. Above we verified that the
437 // base and index registers are unused.
438 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
439 AM.Base.Reg = X86::RIP;
440 }
441 AM.GVOpFlags = GVFlags;
442 return true;
443 }
444
445 // Ok, we need to do a load from a stub. If we've already loaded from
446 // this stub, reuse the loaded pointer, otherwise emit the load now.
447 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
448 unsigned LoadReg;
449 if (I != LocalValueMap.end() && I->second != 0) {
450 LoadReg = I->second;
451 } else {
452 // Issue load from stub.
453 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000454 const TargetRegisterClass *RC = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000455 X86AddressMode StubAM;
456 StubAM.Base.Reg = AM.Base.Reg;
457 StubAM.GV = GV;
458 StubAM.GVOpFlags = GVFlags;
459
460 // Prepare for inserting code in the local-value area.
461 SavePoint SaveInsertPt = enterLocalValueArea();
462
463 if (TLI.getPointerTy() == MVT::i64) {
464 Opc = X86::MOV64rm;
465 RC = &X86::GR64RegClass;
466
467 if (Subtarget->isPICStyleRIPRel())
468 StubAM.Base.Reg = X86::RIP;
469 } else {
470 Opc = X86::MOV32rm;
471 RC = &X86::GR32RegClass;
472 }
473
474 LoadReg = createResultReg(RC);
475 MachineInstrBuilder LoadMI =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000476 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000477 addFullAddress(LoadMI, StubAM);
478
479 // Ok, back to normal mode.
480 leaveLocalValueArea(SaveInsertPt);
481
482 // Prevent loading GV stub multiple times in same MBB.
483 LocalValueMap[V] = LoadReg;
484 }
485
486 // Now construct the final address. Note that the Disp, Scale,
487 // and Index values may already be set here.
488 AM.Base.Reg = LoadReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000489 AM.GV = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000490 return true;
491 }
492 }
493
494 // If all else fails, try to materialize the value in a register.
495 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
496 if (AM.Base.Reg == 0) {
497 AM.Base.Reg = getRegForValue(V);
498 return AM.Base.Reg != 0;
499 }
500 if (AM.IndexReg == 0) {
501 assert(AM.Scale == 1 && "Scale with no index!");
502 AM.IndexReg = getRegForValue(V);
503 return AM.IndexReg != 0;
504 }
505 }
506
507 return false;
508}
509
Dan Gohman39d82f92008-09-10 20:11:02 +0000510/// X86SelectAddress - Attempt to fill in an address from the given value.
511///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000512bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000513 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000514redo_gep:
Craig Topper062a2ba2014-04-25 05:30:21 +0000515 const User *U = nullptr;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000516 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000517 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000518 // Don't walk into other basic blocks; it's possible we haven't
519 // visited them yet, so the instructions may not yet be assigned
520 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000521 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
522 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
523 Opcode = I->getOpcode();
524 U = I;
525 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000526 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000527 Opcode = C->getOpcode();
528 U = C;
529 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000530
Chris Lattner229907c2011-07-18 04:54:35 +0000531 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000532 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000533 // Fast instruction selection doesn't support the special
534 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000535 return false;
536
Dan Gohman6e005fd2008-09-18 23:23:44 +0000537 switch (Opcode) {
538 default: break;
539 case Instruction::BitCast:
540 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000541 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000542
543 case Instruction::IntToPtr:
544 // Look past no-op inttoptrs.
545 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000546 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000547 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000548
549 case Instruction::PtrToInt:
550 // Look past no-op ptrtoints.
551 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000552 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000553 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000554
555 case Instruction::Alloca: {
556 // Do static allocas.
557 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000558 DenseMap<const AllocaInst*, int>::iterator SI =
559 FuncInfo.StaticAllocaMap.find(A);
560 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000561 AM.BaseType = X86AddressMode::FrameIndexBase;
562 AM.Base.FrameIndex = SI->second;
563 return true;
564 }
565 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000566 }
567
568 case Instruction::Add: {
569 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000570 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000571 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
572 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000573 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000574 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000575 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000576 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000577 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000578 break;
579 }
580
581 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000582 X86AddressMode SavedAM = AM;
583
Dan Gohman6e005fd2008-09-18 23:23:44 +0000584 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000585 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000586 unsigned IndexReg = AM.IndexReg;
587 unsigned Scale = AM.Scale;
588 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000589 // Iterate through the indices, folding what we can. Constants can be
590 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000591 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000592 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000593 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000594 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000595 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000596 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
597 continue;
598 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000599
Chris Lattner4b026b92011-04-17 17:05:12 +0000600 // A array/variable index is always of the form i*S where S is the
601 // constant scale size. See if we can push the scale into immediates.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000602 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner4b026b92011-04-17 17:05:12 +0000603 for (;;) {
604 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
605 // Constant-offset addressing.
606 Disp += CI->getSExtValue() * S;
607 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000608 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000609 if (canFoldAddIntoGEP(U, Op)) {
610 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000611 ConstantInt *CI =
612 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
613 Disp += CI->getSExtValue() * S;
614 // Iterate on the other operand.
615 Op = cast<AddOperator>(Op)->getOperand(0);
616 continue;
617 }
618 if (IndexReg == 0 &&
619 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
620 (S == 1 || S == 2 || S == 4 || S == 8)) {
621 // Scaled-index addressing.
622 Scale = S;
623 IndexReg = getRegForGEPIndex(Op).first;
624 if (IndexReg == 0)
625 return false;
626 break;
627 }
628 // Unsupported.
629 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000630 }
631 }
Bill Wendling585a9012013-09-24 00:13:08 +0000632
Dan Gohman2564b902008-09-26 20:04:15 +0000633 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000634 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000635 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000636
Dan Gohman6e005fd2008-09-18 23:23:44 +0000637 AM.IndexReg = IndexReg;
638 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000639 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000640 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000641
642 if (const GetElementPtrInst *GEP =
643 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
644 // Ok, the GEP indices were covered by constant-offset and scaled-index
645 // addressing. Update the address state and move on to examining the base.
646 V = GEP;
647 goto redo_gep;
648 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000649 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000650 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000651
Chris Lattner4b026b92011-04-17 17:05:12 +0000652 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000653 // our address and just match the value instead of completely failing.
654 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000655
656 for (SmallVectorImpl<const Value *>::reverse_iterator
657 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
658 if (handleConstantAddresses(*I, AM))
659 return true;
660
661 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000662 unsupported_gep:
663 // Ok, the GEP indices weren't all covered.
664 break;
665 }
666 }
667
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000668 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000669}
670
Chris Lattner8212d372009-07-10 05:33:42 +0000671/// X86SelectCallAddress - Attempt to fill in an address from the given value.
672///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000673bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000674 const User *U = nullptr;
Chris Lattner8212d372009-07-10 05:33:42 +0000675 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000676 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000677 // Record if the value is defined in the same basic block.
678 //
679 // This information is crucial to know whether or not folding an
680 // operand is valid.
681 // Indeed, FastISel generates or reuses a virtual register for all
682 // operands of all instructions it selects. Obviously, the definition and
683 // its uses must use the same virtual register otherwise the produced
684 // code is incorrect.
685 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
686 // registers for values that are alive across basic blocks. This ensures
687 // that the values are consistently set between across basic block, even
688 // if different instruction selection mechanisms are used (e.g., a mix of
689 // SDISel and FastISel).
690 // For values local to a basic block, the instruction selection process
691 // generates these virtual registers with whatever method is appropriate
692 // for its needs. In particular, FastISel and SDISel do not share the way
693 // local virtual registers are set.
694 // Therefore, this is impossible (or at least unsafe) to share values
695 // between basic blocks unless they use the same instruction selection
696 // method, which is not guarantee for X86.
697 // Moreover, things like hasOneUse could not be used accurately, if we
698 // allow to reference values across basic blocks whereas they are not
699 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000700 bool InMBB = true;
701 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000702 Opcode = I->getOpcode();
703 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000704 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000705 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000706 Opcode = C->getOpcode();
707 U = C;
708 }
709
710 switch (Opcode) {
711 default: break;
712 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000713 // Look past bitcasts if its operand is in the same BB.
714 if (InMBB)
715 return X86SelectCallAddress(U->getOperand(0), AM);
716 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000717
718 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000719 // Look past no-op inttoptrs if its operand is in the same BB.
720 if (InMBB &&
721 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000722 return X86SelectCallAddress(U->getOperand(0), AM);
723 break;
724
725 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000726 // Look past no-op ptrtoints if its operand is in the same BB.
727 if (InMBB &&
728 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000729 return X86SelectCallAddress(U->getOperand(0), AM);
730 break;
731 }
732
733 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000734 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000735 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000736 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000737 return false;
738
739 // RIP-relative addresses can't have additional register operands.
740 if (Subtarget->isPICStyleRIPRel() &&
741 (AM.Base.Reg != 0 || AM.IndexReg != 0))
742 return false;
743
Rafael Espindolaea09c592014-02-18 22:05:46 +0000744 // Can't handle DbgLocLImport.
Nico Rieck7157bb72014-01-14 15:22:47 +0000745 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000746 return false;
747
748 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000749 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000750 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000751 return false;
752
753 // Okay, we've committed to selecting this global. Set up the basic address.
754 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000755
Chris Lattner7277a802009-07-10 05:45:15 +0000756 // No ABI requires an extra load for anything other than DLLImport, which
757 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000758 if (Subtarget->isPICStyleRIPRel()) {
759 // Use rip-relative addressing if we can. Above we verified that the
760 // base and index registers are unused.
761 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
762 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000763 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000764 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
765 } else if (Subtarget->isPICStyleGOT()) {
766 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000767 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000768
Chris Lattner8212d372009-07-10 05:33:42 +0000769 return true;
770 }
771
772 // If all else fails, try to materialize the value in a register.
773 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
774 if (AM.Base.Reg == 0) {
775 AM.Base.Reg = getRegForValue(V);
776 return AM.Base.Reg != 0;
777 }
778 if (AM.IndexReg == 0) {
779 assert(AM.Scale == 1 && "Scale with no index!");
780 AM.IndexReg = getRegForValue(V);
781 return AM.IndexReg != 0;
782 }
783 }
784
785 return false;
786}
787
788
Owen Anderson4f948bd2008-09-04 07:08:58 +0000789/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000790bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000791 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000792 const StoreInst *S = cast<StoreInst>(I);
793
794 if (S->isAtomic())
795 return false;
796
Juergen Ributzka349777d2014-06-12 23:27:57 +0000797 const Value *Val = S->getValueOperand();
798 const Value *Ptr = S->getPointerOperand();
Craig Topper4f55b0e2013-07-17 05:57:45 +0000799
Duncan Sandsf5dda012010-11-03 11:35:31 +0000800 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000801 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000802 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000803
Juergen Ributzka349777d2014-06-12 23:27:57 +0000804 unsigned Alignment = S->getAlignment();
805 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
806 if (Alignment == 0) // Ensure that codegen never sees alignment 0
807 Alignment = ABIAlignment;
808 bool Aligned = Alignment >= ABIAlignment;
809
Dan Gohman39d82f92008-09-10 20:11:02 +0000810 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000811 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000812 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000813
Juergen Ributzka349777d2014-06-12 23:27:57 +0000814 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000815}
816
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000817/// X86SelectRet - Select and emit code to implement ret instructions.
818bool X86FastISel::X86SelectRet(const Instruction *I) {
819 const ReturnInst *Ret = cast<ReturnInst>(I);
820 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000821 const X86MachineFunctionInfo *X86MFInfo =
822 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000823
824 if (!FuncInfo.CanLowerReturn)
825 return false;
826
827 CallingConv::ID CC = F.getCallingConv();
828 if (CC != CallingConv::C &&
829 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000830 CC != CallingConv::X86_FastCall &&
831 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000832 return false;
833
Charles Davise8f297c2013-07-12 06:02:35 +0000834 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000835 return false;
836
837 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000838 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000839 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000840
841 // fastcc with -tailcallopt is intended to provide a guaranteed
842 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000843 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000844 return false;
845
846 // Let SDISel handle vararg functions.
847 if (F.isVarArg())
848 return false;
849
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000850 // Build a list of return value registers.
851 SmallVector<unsigned, 4> RetRegs;
852
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000853 if (Ret->getNumOperands() > 0) {
854 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000855 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000856
857 // Analyze operands of the call, assigning locations to each operand.
858 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000859 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000860 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000861 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000862
863 const Value *RV = Ret->getOperand(0);
864 unsigned Reg = getRegForValue(RV);
865 if (Reg == 0)
866 return false;
867
868 // Only handle a single return value for now.
869 if (ValLocs.size() != 1)
870 return false;
871
872 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000873
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000874 // Don't bother handling odd stuff for now.
875 if (VA.getLocInfo() != CCValAssign::Full)
876 return false;
877 // Only handle register returns for now.
878 if (!VA.isRegLoc())
879 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000880
881 // The calling-convention tables for x87 returns don't tell
882 // the whole story.
883 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
884 return false;
885
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000886 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000887 EVT SrcVT = TLI.getValueType(RV->getType());
888 EVT DstVT = VA.getValVT();
889 // Special handling for extended integers.
890 if (SrcVT != DstVT) {
891 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
892 return false;
893
894 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
895 return false;
896
897 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
898
899 if (SrcVT == MVT::i1) {
900 if (Outs[0].Flags.isSExt())
901 return false;
902 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
903 SrcVT = MVT::i8;
904 }
905 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
906 ISD::SIGN_EXTEND;
907 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
908 SrcReg, /*TODO: Kill=*/false);
909 }
910
911 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000912 unsigned DstReg = VA.getLocReg();
913 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000914 // Avoid a cross-class copy. This is very unlikely.
915 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000916 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000917 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000918 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000919
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000920 // Add register to return instruction.
921 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000922 }
923
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000924 // The x86-64 ABI for returning structs by value requires that we copy
925 // the sret argument into %rax for the return. We saved the argument into
926 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000927 // and into %rax. We also do the same with %eax for Win32.
928 if (F.hasStructRetAttr() &&
Yaron Keren136fe7d2014-04-01 18:15:34 +0000929 (Subtarget->is64Bit() || Subtarget->isTargetKnownWindowsMSVC())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000930 unsigned Reg = X86MFInfo->getSRetReturnReg();
931 assert(Reg &&
932 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000933 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000934 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000935 RetReg).addReg(Reg);
936 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000937 }
938
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000939 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000940 MachineInstrBuilder MIB =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000941 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000942 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
943 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000944 return true;
945}
946
Evan Chenga41ee292008-09-03 06:44:39 +0000947/// X86SelectLoad - Select and emit code to implement load instructions.
948///
Juergen Ributzka349777d2014-06-12 23:27:57 +0000949bool X86FastISel::X86SelectLoad(const Instruction *I) {
950 const LoadInst *LI = cast<LoadInst>(I);
951
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000952 // Atomic loads need special handling.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000953 if (LI->isAtomic())
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000954 return false;
955
Duncan Sandsf5dda012010-11-03 11:35:31 +0000956 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000957 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +0000958 return false;
959
Juergen Ributzka349777d2014-06-12 23:27:57 +0000960 const Value *Ptr = LI->getPointerOperand();
961
Dan Gohman39d82f92008-09-10 20:11:02 +0000962 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000963 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000964 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000965
Evan Chengf5bc7e52008-09-05 21:00:03 +0000966 unsigned ResultReg = 0;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000967 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg))
968 return false;
969
970 UpdateValueMap(I, ResultReg);
971 return true;
Evan Chenga41ee292008-09-03 06:44:39 +0000972}
973
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000974static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000975 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +0000976 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
977 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000978
Owen Anderson9f944592009-08-11 20:47:22 +0000979 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +0000980 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000981 case MVT::i8: return X86::CMP8rr;
982 case MVT::i16: return X86::CMP16rr;
983 case MVT::i32: return X86::CMP32rr;
984 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000985 case MVT::f32:
986 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
987 case MVT::f64:
988 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +0000989 }
Dan Gohman1ab1d312008-10-02 22:15:21 +0000990}
991
Chris Lattner88f47542008-10-15 04:13:29 +0000992/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
993/// of the comparison, return an opcode that works for the compare (e.g.
994/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000995static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +0000996 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +0000997 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +0000998 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000999 case MVT::i8: return X86::CMP8ri;
1000 case MVT::i16: return X86::CMP16ri;
1001 case MVT::i32: return X86::CMP32ri;
1002 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +00001003 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1004 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +00001005 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +00001006 return X86::CMP64ri32;
1007 return 0;
1008 }
Chris Lattner88f47542008-10-15 04:13:29 +00001009}
1010
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001011bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
1012 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +00001013 unsigned Op0Reg = getRegForValue(Op0);
1014 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001015
Chris Lattnere388725a2008-10-15 05:18:04 +00001016 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +00001017 if (isa<ConstantPointerNull>(Op1))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001018 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +00001019
Chris Lattnerd46b9512008-10-15 04:26:38 +00001020 // We have two options: compare with register or immediate. If the RHS of
1021 // the compare is an immediate that we can fold into this compare, use
1022 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001023 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +00001024 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001025 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareImmOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001026 .addReg(Op0Reg)
1027 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +00001028 return true;
1029 }
1030 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001031
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001032 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +00001033 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001034
Chris Lattnerd46b9512008-10-15 04:26:38 +00001035 unsigned Op1Reg = getRegForValue(Op1);
1036 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001037 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001038 .addReg(Op0Reg)
1039 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001040
Chris Lattnerd46b9512008-10-15 04:26:38 +00001041 return true;
1042}
1043
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001044bool X86FastISel::X86SelectCmp(const Instruction *I) {
1045 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001046
Duncan Sandsf5dda012010-11-03 11:35:31 +00001047 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001048 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +00001049 return false;
1050
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001051 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Juergen Ributzkae3570562014-06-17 14:47:45 +00001052 static unsigned SETFOpcTable[2][3] = {
1053 { X86::SETEr, X86::SETNPr, X86::AND8rr },
1054 { X86::SETNEr, X86::SETPr, X86::OR8rr }
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001055 };
1056 unsigned *SETFOpc = nullptr;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001057 switch (CI->getPredicate()) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001058 default: break;
1059 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1060 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1061 }
1062
1063 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
1064 if (SETFOpc) {
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001065 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1066 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001067
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001068 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1069 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1070 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1071 FlagReg1);
1072 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1073 FlagReg2);
Juergen Ributzkae3570562014-06-17 14:47:45 +00001074 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001075 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
Chris Lattnera3596db2008-10-15 03:47:17 +00001076 UpdateValueMap(I, ResultReg);
1077 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001078 }
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001079
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001080 X86::CondCode CC;
1081 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1082 std::tie(CC, SwapArgs) = getX86ConditonCode(CI->getPredicate());
1083 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1084 unsigned Opc = X86::getSETFromCond(CC);
Wesley Peck527da1b2010-11-23 03:31:01 +00001085
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001086 const Value *LHS = CI->getOperand(0);
1087 const Value *RHS = CI->getOperand(1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001088 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001089 std::swap(LHS, RHS);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001090
Chris Lattnerd46b9512008-10-15 04:26:38 +00001091 // Emit a compare of Op0/Op1.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001092 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001093 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001094
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001095 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001096 UpdateValueMap(I, ResultReg);
1097 return true;
1098}
Evan Chenga41ee292008-09-03 06:44:39 +00001099
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001100bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001101 EVT DstVT = TLI.getValueType(I->getType());
1102 if (!TLI.isTypeLegal(DstVT))
1103 return false;
1104
1105 unsigned ResultReg = getRegForValue(I->getOperand(0));
1106 if (ResultReg == 0)
1107 return false;
1108
Tim Northover04eb4232013-05-30 10:43:18 +00001109 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001110 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001111 if (SrcVT.SimpleTy == MVT::i1) {
1112 // Set the high bits to zero.
1113 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1114 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001115
Tim Northover04eb4232013-05-30 10:43:18 +00001116 if (ResultReg == 0)
1117 return false;
1118 }
1119
1120 if (DstVT == MVT::i64) {
1121 // Handle extension to 64-bits via sub-register shenanigans.
1122 unsigned MovInst;
1123
1124 switch (SrcVT.SimpleTy) {
1125 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1126 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1127 case MVT::i32: MovInst = X86::MOV32rr; break;
1128 default: llvm_unreachable("Unexpected zext to i64 source type");
1129 }
1130
1131 unsigned Result32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001132 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
Tim Northover04eb4232013-05-30 10:43:18 +00001133 .addReg(ResultReg);
1134
1135 ResultReg = createResultReg(&X86::GR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
Tim Northover04eb4232013-05-30 10:43:18 +00001137 ResultReg)
1138 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1139 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001140 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1141 ResultReg, /*Kill=*/true);
1142 if (ResultReg == 0)
1143 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001144 }
1145
Eli Friedmanc7035512011-05-25 23:49:02 +00001146 UpdateValueMap(I, ResultReg);
1147 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001148}
1149
Chris Lattnerd46b9512008-10-15 04:26:38 +00001150
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001151bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001152 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001153 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001154 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001155 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1156 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001157
Dan Gohman42ef6692010-08-21 02:32:36 +00001158 // Fold the common case of a conditional branch with a comparison
1159 // in the same block (values defined on other blocks may not have
1160 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001161 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001162 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001163 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001164
Dan Gohman1ab1d312008-10-02 22:15:21 +00001165 // Try to take advantage of fallthrough opportunities.
1166 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001167 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001168 std::swap(TrueMBB, FalseMBB);
1169 Predicate = CmpInst::getInversePredicate(Predicate);
1170 }
1171
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001172 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/conditon
1173 // code check. Instead two branch instructions are required to check all
1174 // the flags. First we change the predicate to a supported conditon code,
1175 // which will be the first branch. Later one we will emit the second
1176 // branch.
1177 bool NeedExtraBranch = false;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001178 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001179 default: break;
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001180 case CmpInst::FCMP_OEQ:
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001181 std::swap(TrueMBB, FalseMBB); // fall-through
1182 case CmpInst::FCMP_UNE:
1183 NeedExtraBranch = true;
1184 Predicate = CmpInst::FCMP_ONE;
1185 break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001186 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001187
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001188 X86::CondCode CC;
1189 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1190 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1191 std::tie(CC, SwapArgs) = getX86ConditonCode(Predicate);
1192 assert(CC <= X86::LAST_VALID_COND && "Unexpected conditon code.");
1193
1194 BranchOpc = X86::GetCondBranchFromCond(CC);
1195 const Value *CmpLHS = CI->getOperand(0);
1196 const Value *CmpRHS = CI->getOperand(1);
Chris Lattner47bef252008-10-15 04:02:26 +00001197 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001198 std::swap(CmpLHS, CmpRHS);
Chris Lattner47bef252008-10-15 04:02:26 +00001199
Chris Lattnerd46b9512008-10-15 04:26:38 +00001200 // Emit a compare of the LHS and RHS, setting the flags.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001201 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT))
Chris Lattnerd46b9512008-10-15 04:26:38 +00001202 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001203
Rafael Espindolaea09c592014-02-18 22:05:46 +00001204 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001205 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001206
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001207 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1208 // to UNE above).
1209 if (NeedExtraBranch) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001210 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001211 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001212 }
1213
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001214 // Obtain the branch weight and add the TrueBB to the successor list.
Juergen Ributzka454d3742014-06-13 00:45:11 +00001215 uint32_t BranchWeight = 0;
1216 if (FuncInfo.BPI)
1217 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1218 TrueMBB->getBasicBlock());
1219 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001220
1221 // Emits an unconditional branch to the FalseBB, obtains the branch
1222 // weight, andd adds it to the successor list.
1223 FastEmitBranch(FalseMBB, DbgLoc);
1224
Dan Gohman1ab1d312008-10-02 22:15:21 +00001225 return true;
1226 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001227 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1228 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1229 // typically happen for _Bool and C++ bools.
1230 MVT SourceVT;
1231 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1232 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1233 unsigned TestOpc = 0;
1234 switch (SourceVT.SimpleTy) {
1235 default: break;
1236 case MVT::i8: TestOpc = X86::TEST8ri; break;
1237 case MVT::i16: TestOpc = X86::TEST16ri; break;
1238 case MVT::i32: TestOpc = X86::TEST32ri; break;
1239 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1240 }
1241 if (TestOpc) {
1242 unsigned OpReg = getRegForValue(TI->getOperand(0));
1243 if (OpReg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001245 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001246
Chris Lattnerc59290a2011-04-19 04:26:32 +00001247 unsigned JmpOpc = X86::JNE_4;
1248 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1249 std::swap(TrueMBB, FalseMBB);
1250 JmpOpc = X86::JE_4;
1251 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001252
Rafael Espindolaea09c592014-02-18 22:05:46 +00001253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001254 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001255 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001256 uint32_t BranchWeight = 0;
1257 if (FuncInfo.BPI)
1258 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1259 TrueMBB->getBasicBlock());
1260 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001261 return true;
1262 }
1263 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001264 }
1265
1266 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001267 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1268 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001269 unsigned OpReg = getRegForValue(BI->getCondition());
1270 if (OpReg == 0) return false;
1271
Rafael Espindolaea09c592014-02-18 22:05:46 +00001272 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Eli Friedman0eea0292011-04-27 01:34:27 +00001273 .addReg(OpReg).addImm(1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001275 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001276 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001277 uint32_t BranchWeight = 0;
1278 if (FuncInfo.BPI)
1279 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1280 TrueMBB->getBasicBlock());
1281 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Dan Gohmana5753b32008-09-05 01:06:14 +00001282 return true;
1283}
1284
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001285bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001286 unsigned CReg = 0, OpReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001287 const TargetRegisterClass *RC = nullptr;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001288 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001289 CReg = X86::CL;
1290 RC = &X86::GR8RegClass;
1291 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001292 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1293 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1294 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001295 default: return false;
1296 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001297 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001298 CReg = X86::CX;
1299 RC = &X86::GR16RegClass;
1300 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001301 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1302 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1303 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001304 default: return false;
1305 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001306 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001307 CReg = X86::ECX;
1308 RC = &X86::GR32RegClass;
1309 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001310 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1311 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1312 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001313 default: return false;
1314 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001315 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001316 CReg = X86::RCX;
1317 RC = &X86::GR64RegClass;
1318 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001319 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1320 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1321 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001322 default: return false;
1323 }
1324 } else {
1325 return false;
1326 }
1327
Duncan Sandsf5dda012010-11-03 11:35:31 +00001328 MVT VT;
1329 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001330 return false;
1331
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001332 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1333 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001334
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001335 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1336 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001337 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001338 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001339
1340 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001341 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001342 if (CReg != X86::CL)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001343 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001344 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001345 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001346
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001347 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001348 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001349 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001350 UpdateValueMap(I, ResultReg);
1351 return true;
1352}
1353
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001354bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1355 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1356 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1357 const static bool S = true; // IsSigned
1358 const static bool U = false; // !IsSigned
1359 const static unsigned Copy = TargetOpcode::COPY;
1360 // For the X86 DIV/IDIV instruction, in most cases the dividend
1361 // (numerator) must be in a specific register pair highreg:lowreg,
1362 // producing the quotient in lowreg and the remainder in highreg.
1363 // For most data types, to set up the instruction, the dividend is
1364 // copied into lowreg, and lowreg is sign-extended or zero-extended
1365 // into highreg. The exception is i8, where the dividend is defined
1366 // as a single register rather than a register pair, and we
1367 // therefore directly sign-extend or zero-extend the dividend into
1368 // lowreg, instead of copying, and ignore the highreg.
1369 const static struct DivRemEntry {
1370 // The following portion depends only on the data type.
1371 const TargetRegisterClass *RC;
1372 unsigned LowInReg; // low part of the register pair
1373 unsigned HighInReg; // high part of the register pair
1374 // The following portion depends on both the data type and the operation.
1375 struct DivRemResult {
1376 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1377 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1378 // highreg, or copying a zero into highreg.
1379 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1380 // zero/sign-extending into lowreg for i8.
1381 unsigned DivRemResultReg; // Register containing the desired result.
1382 bool IsOpSigned; // Whether to use signed or unsigned form.
1383 } ResultTable[NumOps];
1384 } OpTable[NumTypes] = {
1385 { &X86::GR8RegClass, X86::AX, 0, {
1386 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1387 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1388 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1389 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1390 }
1391 }, // i8
1392 { &X86::GR16RegClass, X86::AX, X86::DX, {
1393 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1394 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001395 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1396 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001397 }
1398 }, // i16
1399 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1400 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1401 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1402 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1403 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1404 }
1405 }, // i32
1406 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1407 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1408 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001409 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1410 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001411 }
1412 }, // i64
1413 };
1414
1415 MVT VT;
1416 if (!isTypeLegal(I->getType(), VT))
1417 return false;
1418
1419 unsigned TypeIndex, OpIndex;
1420 switch (VT.SimpleTy) {
1421 default: return false;
1422 case MVT::i8: TypeIndex = 0; break;
1423 case MVT::i16: TypeIndex = 1; break;
1424 case MVT::i32: TypeIndex = 2; break;
1425 case MVT::i64: TypeIndex = 3;
1426 if (!Subtarget->is64Bit())
1427 return false;
1428 break;
1429 }
1430
1431 switch (I->getOpcode()) {
1432 default: llvm_unreachable("Unexpected div/rem opcode");
1433 case Instruction::SDiv: OpIndex = 0; break;
1434 case Instruction::SRem: OpIndex = 1; break;
1435 case Instruction::UDiv: OpIndex = 2; break;
1436 case Instruction::URem: OpIndex = 3; break;
1437 }
1438
1439 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1440 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1441 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1442 if (Op0Reg == 0)
1443 return false;
1444 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1445 if (Op1Reg == 0)
1446 return false;
1447
1448 // Move op0 into low-order input register.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001449 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001450 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1451 // Zero-extend or sign-extend into high-order input register.
1452 if (OpEntry.OpSignExtend) {
1453 if (OpEntry.IsOpSigned)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001454 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001455 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001456 else {
1457 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001458 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001459 TII.get(X86::MOV32r0), Zero32);
1460
1461 // Copy the zero into the appropriate sub/super/identical physical
1462 // register. Unfortunately the operations needed are not uniform enough to
1463 // fit neatly into the table above.
1464 if (VT.SimpleTy == MVT::i16) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001465 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001466 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001467 .addReg(Zero32, 0, X86::sub_16bit);
1468 } else if (VT.SimpleTy == MVT::i32) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001469 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001470 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001471 .addReg(Zero32);
1472 } else if (VT.SimpleTy == MVT::i64) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001473 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001474 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1475 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1476 }
1477 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001478 }
1479 // Generate the DIV/IDIV instruction.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001480 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001481 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001482 // For i8 remainder, we can't reference AH directly, as we'll end
1483 // up with bogus copies like %R9B = COPY %AH. Reference AX
1484 // instead to prevent AH references in a REX instruction.
1485 //
1486 // The current assumption of the fast register allocator is that isel
1487 // won't generate explicit references to the GPR8_NOREX registers. If
1488 // the allocator and/or the backend get enhanced to be more robust in
1489 // that regard, this can be, and should be, removed.
1490 unsigned ResultReg = 0;
1491 if ((I->getOpcode() == Instruction::SRem ||
1492 I->getOpcode() == Instruction::URem) &&
1493 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1494 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1495 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001496 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbachc35388f2013-07-09 02:07:25 +00001497 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1498
1499 // Shift AX right by 8 bits instead of using AH.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
Jim Grosbachc35388f2013-07-09 02:07:25 +00001501 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1502
1503 // Now reference the 8-bit subreg of the result.
1504 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1505 /*Kill=*/true, X86::sub_8bit);
1506 }
1507 // Copy the result out of the physreg if we haven't already.
1508 if (!ResultReg) {
1509 ResultReg = createResultReg(TypeEntry.RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001510 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
Jim Grosbachc35388f2013-07-09 02:07:25 +00001511 .addReg(OpEntry.DivRemResultReg);
1512 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001513 UpdateValueMap(I, ResultReg);
1514
1515 return true;
1516}
1517
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001518bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001519 MVT VT;
1520 if (!isTypeLegal(I->getType(), VT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001521 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001522
Eric Christopher0574cc52010-09-29 23:00:29 +00001523 // We only use cmov here, if we don't have a cmov instruction bail.
1524 if (!Subtarget->hasCMov()) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001525
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001526 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001527 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001528 if (VT == MVT::i16) {
Dan Gohmane5560182008-09-05 21:13:04 +00001529 Opc = X86::CMOVE16rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001530 RC = &X86::GR16RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001531 } else if (VT == MVT::i32) {
Dan Gohmane5560182008-09-05 21:13:04 +00001532 Opc = X86::CMOVE32rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001533 RC = &X86::GR32RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001534 } else if (VT == MVT::i64) {
Dan Gohmane5560182008-09-05 21:13:04 +00001535 Opc = X86::CMOVE64rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001536 RC = &X86::GR64RegClass;
1537 } else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001538 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001539 }
1540
1541 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1542 if (Op0Reg == 0) return false;
1543 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1544 if (Op1Reg == 0) return false;
1545 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1546 if (Op2Reg == 0) return false;
1547
Quentin Colombet90a646e2013-12-19 18:32:04 +00001548 // Selects operate on i1, however, Op0Reg is 8 bits width and may contain
1549 // garbage. Indeed, only the less significant bit is supposed to be accurate.
1550 // If we read more than the lsb, we may see non-zero values whereas lsb
1551 // is zero. Therefore, we have to truncate Op0Reg to i1 for the select.
Alp Tokercb402912014-01-24 17:20:08 +00001552 // This is achieved by performing TEST against 1.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001553 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Quentin Colombet90a646e2013-12-19 18:32:04 +00001554 .addReg(Op0Reg).addImm(1);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001555 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001556 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001557 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001558 UpdateValueMap(I, ResultReg);
1559 return true;
1560}
1561
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001562bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001563 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001564 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001565 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001566 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001567 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001568 unsigned OpReg = getRegForValue(V);
1569 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001570 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001571 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001572 TII.get(X86::CVTSS2SDrr), ResultReg)
1573 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001574 UpdateValueMap(I, ResultReg);
1575 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001576 }
1577 }
1578
1579 return false;
1580}
1581
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001582bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001583 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001584 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001585 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001586 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001587 unsigned OpReg = getRegForValue(V);
1588 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001589 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001590 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001591 TII.get(X86::CVTSD2SSrr), ResultReg)
1592 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001593 UpdateValueMap(I, ResultReg);
1594 return true;
1595 }
1596 }
1597 }
1598
1599 return false;
1600}
1601
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001602bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001603 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1604 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001605
Eli Friedmanc7035512011-05-25 23:49:02 +00001606 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001607 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001608 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001609 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001610 return false;
1611
1612 unsigned InputReg = getRegForValue(I->getOperand(0));
1613 if (!InputReg)
1614 // Unhandled operand. Halt "fast" selection and bail.
1615 return false;
1616
Eli Friedmanc7035512011-05-25 23:49:02 +00001617 if (SrcVT == MVT::i8) {
1618 // Truncate from i8 to i1; no code needed.
1619 UpdateValueMap(I, InputReg);
1620 return true;
1621 }
Evan Chengb9286692008-09-07 08:47:42 +00001622
Eli Friedmanc7035512011-05-25 23:49:02 +00001623 if (!Subtarget->is64Bit()) {
1624 // If we're on x86-32; we can't extract an i8 from a general register.
1625 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001626 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1627 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1628 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001629 unsigned CopyReg = createResultReg(CopyRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001630 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Eli Friedmanc7035512011-05-25 23:49:02 +00001631 CopyReg).addReg(InputReg);
1632 InputReg = CopyReg;
1633 }
1634
1635 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001636 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001637 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001638 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001639 if (!ResultReg)
1640 return false;
1641
1642 UpdateValueMap(I, ResultReg);
1643 return true;
1644}
1645
Eli Friedman60afcc22011-05-20 22:21:04 +00001646bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1647 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1648}
1649
Eli Friedmanbcc69142011-04-27 01:45:07 +00001650bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1651 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001652
Eli Friedmanbcc69142011-04-27 01:45:07 +00001653 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001654 if (!IsMemcpySmall(Len))
1655 return false;
1656
1657 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001658
1659 // We don't care about alignment here since we just emit integer accesses.
1660 while (Len) {
1661 MVT VT;
1662 if (Len >= 8 && i64Legal)
1663 VT = MVT::i64;
1664 else if (Len >= 4)
1665 VT = MVT::i32;
1666 else if (Len >= 2)
1667 VT = MVT::i16;
1668 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001669 VT = MVT::i8;
1670 }
1671
1672 unsigned Reg;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001673 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
1674 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
Eli Friedmanbcc69142011-04-27 01:45:07 +00001675 assert(RV && "Failed to emit load or store??");
1676
1677 unsigned Size = VT.getSizeInBits()/8;
1678 Len -= Size;
1679 DestAM.Disp += Size;
1680 SrcAM.Disp += Size;
1681 }
1682
1683 return true;
1684}
1685
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001686static bool isCommutativeIntrinsic(IntrinsicInst const &I) {
1687 switch (I.getIntrinsicID()) {
1688 case Intrinsic::sadd_with_overflow:
1689 case Intrinsic::uadd_with_overflow:
1690 case Intrinsic::smul_with_overflow:
1691 case Intrinsic::umul_with_overflow:
1692 return true;
1693 default:
1694 return false;
1695 }
1696}
1697
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001698bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001699 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001700 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001701 default: return false;
Juergen Ributzka4dc95872014-06-11 21:44:44 +00001702 case Intrinsic::frameaddress: {
1703 Type *RetTy = I.getCalledFunction()->getReturnType();
1704
1705 MVT VT;
1706 if (!isTypeLegal(RetTy, VT))
1707 return false;
1708
1709 unsigned Opc;
1710 const TargetRegisterClass *RC = nullptr;
1711
1712 switch (VT.SimpleTy) {
1713 default: llvm_unreachable("Invalid result type for frameaddress.");
1714 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
1715 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
1716 }
1717
1718 // This needs to be set before we call getFrameRegister, otherwise we get
1719 // the wrong frame register.
1720 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
1721 MFI->setFrameAddressIsTaken(true);
1722
1723 const X86RegisterInfo *RegInfo =
1724 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
1725 unsigned FrameReg = RegInfo->getFrameRegister(*(FuncInfo.MF));
1726 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
1727 (FrameReg == X86::EBP && VT == MVT::i32)) &&
1728 "Invalid Frame Register!");
1729
1730 // Always make a copy of the frame register to to a vreg first, so that we
1731 // never directly reference the frame register (the TwoAddressInstruction-
1732 // Pass doesn't like that).
1733 unsigned SrcReg = createResultReg(RC);
1734 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1735 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
1736
1737 // Now recursively load from the frame address.
1738 // movq (%rbp), %rax
1739 // movq (%rax), %rax
1740 // movq (%rax), %rax
1741 // ...
1742 unsigned DestReg;
1743 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
1744 while (Depth--) {
1745 DestReg = createResultReg(RC);
1746 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1747 TII.get(Opc), DestReg), SrcReg);
1748 SrcReg = DestReg;
1749 }
1750
1751 UpdateValueMap(&I, SrcReg);
1752 return true;
1753 }
Chris Lattner91328b32011-04-19 05:52:03 +00001754 case Intrinsic::memcpy: {
1755 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1756 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001757 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001758 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001759
Eli Friedmancd2124a2011-06-10 23:39:36 +00001760 if (isa<ConstantInt>(MCI.getLength())) {
1761 // Small memcpy's are common enough that we want to do them
1762 // without a call if possible.
1763 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1764 if (IsMemcpySmall(Len)) {
1765 X86AddressMode DestAM, SrcAM;
1766 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1767 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1768 return false;
1769 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1770 return true;
1771 }
1772 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001773
Eli Friedmancd2124a2011-06-10 23:39:36 +00001774 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1775 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001776 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001777
Eli Friedmancd2124a2011-06-10 23:39:36 +00001778 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1779 return false;
1780
1781 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001782 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001783 case Intrinsic::memset: {
1784 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001785
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001786 if (MSI.isVolatile())
1787 return false;
1788
Eli Friedmancd2124a2011-06-10 23:39:36 +00001789 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1790 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1791 return false;
1792
1793 if (MSI.getDestAddressSpace() > 255)
1794 return false;
1795
1796 return DoSelectCall(&I, "memset");
1797 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001798 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00001799 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001800 EVT PtrTy = TLI.getPointerTy();
1801
Gabor Greif83205af2010-06-26 11:51:52 +00001802 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1803 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001804
Josh Magee22b8ba22013-12-19 03:17:11 +00001805 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
1806
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001807 // Grab the frame index.
1808 X86AddressMode AM;
1809 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00001810 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001811 return true;
1812 }
Dale Johannesend5575f22010-01-26 00:09:58 +00001813 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001814 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00001815 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00001816 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00001817 if (!X86SelectAddress(DI->getAddress(), AM))
1818 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001819 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00001820 // FIXME may need to add RegState::Debug to any registers produced,
1821 // although ESP/EBP should be the only ones at the moment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001822 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001823 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00001824 return true;
1825 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001826 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001827 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001828 return true;
1829 }
Juergen Ributzka272b5702014-06-11 23:11:02 +00001830 case Intrinsic::sqrt: {
1831 if (!Subtarget->hasSSE1())
1832 return false;
1833
1834 Type *RetTy = I.getCalledFunction()->getReturnType();
1835
1836 MVT VT;
1837 if (!isTypeLegal(RetTy, VT))
1838 return false;
1839
1840 // Unfortunatelly we can't use FastEmit_r, because the AVX version of FSQRT
1841 // is not generated by FastISel yet.
1842 // FIXME: Update this code once tablegen can handle it.
1843 static const unsigned SqrtOpc[2][2] = {
1844 {X86::SQRTSSr, X86::VSQRTSSr},
1845 {X86::SQRTSDr, X86::VSQRTSDr}
1846 };
1847 bool HasAVX = Subtarget->hasAVX();
1848 unsigned Opc;
1849 const TargetRegisterClass *RC;
1850 switch (VT.SimpleTy) {
1851 default: return false;
1852 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
1853 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
1854 }
1855
1856 const Value *SrcVal = I.getArgOperand(0);
1857 unsigned SrcReg = getRegForValue(SrcVal);
1858
1859 if (SrcReg == 0)
1860 return false;
1861
1862 unsigned ImplicitDefReg = 0;
1863 if (HasAVX) {
1864 ImplicitDefReg = createResultReg(RC);
1865 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1866 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
1867 }
1868
1869 unsigned ResultReg = createResultReg(RC);
1870 MachineInstrBuilder MIB;
1871 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1872 ResultReg);
1873
1874 if (ImplicitDefReg)
1875 MIB.addReg(ImplicitDefReg);
1876
1877 MIB.addReg(SrcReg);
1878
1879 UpdateValueMap(&I, ResultReg);
1880 return true;
1881 }
Bill Wendling80b34b32008-12-09 02:42:50 +00001882 case Intrinsic::sadd_with_overflow:
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001883 case Intrinsic::uadd_with_overflow:
1884 case Intrinsic::ssub_with_overflow:
1885 case Intrinsic::usub_with_overflow:
1886 case Intrinsic::smul_with_overflow:
1887 case Intrinsic::umul_with_overflow: {
1888 // This implements the basic lowering of the xalu with overflow intrinsics
1889 // into add/sub/mul folowed by either seto or setb.
Bill Wendling80b34b32008-12-09 02:42:50 +00001890 const Function *Callee = I.getCalledFunction();
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001891 auto *Ty = cast<StructType>(Callee->getReturnType());
1892 Type *RetTy = Ty->getTypeAtIndex(0U);
1893 Type *CondTy = Ty->getTypeAtIndex(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00001894
Duncan Sandsf5dda012010-11-03 11:35:31 +00001895 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00001896 if (!isTypeLegal(RetTy, VT))
1897 return false;
1898
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001899 if (VT < MVT::i8 || VT > MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00001900 return false;
1901
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001902 const Value *LHS = I.getArgOperand(0);
1903 const Value *RHS = I.getArgOperand(1);
1904
1905 // Canonicalize immediates to the RHS.
1906 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
1907 isCommutativeIntrinsic(I))
1908 std::swap(LHS, RHS);
1909
1910 unsigned BaseOpc, CondOpc;
1911 switch (I.getIntrinsicID()) {
1912 default: llvm_unreachable("Unexpected intrinsic!");
1913 case Intrinsic::sadd_with_overflow:
1914 BaseOpc = ISD::ADD; CondOpc = X86::SETOr; break;
1915 case Intrinsic::uadd_with_overflow:
1916 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
1917 case Intrinsic::ssub_with_overflow:
1918 BaseOpc = ISD::SUB; CondOpc = X86::SETOr; break;
1919 case Intrinsic::usub_with_overflow:
1920 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
1921 case Intrinsic::smul_with_overflow:
1922 BaseOpc = ISD::MUL; CondOpc = X86::SETOr; break;
1923 case Intrinsic::umul_with_overflow:
1924 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
1925 }
1926
1927 unsigned LHSReg = getRegForValue(LHS);
1928 if (LHSReg == 0)
1929 return false;
1930 bool LHSIsKill = hasTrivialKill(LHS);
1931
1932 unsigned ResultReg = 0;
1933 // Check if we have an immediate version.
1934 if (auto const *C = dyn_cast<ConstantInt>(RHS)) {
1935 ResultReg = FastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
1936 C->getZExtValue());
1937 }
1938
1939 unsigned RHSReg;
1940 bool RHSIsKill;
1941 if (!ResultReg) {
1942 RHSReg = getRegForValue(RHS);
1943 if (RHSReg == 0)
1944 return false;
1945 RHSIsKill = hasTrivialKill(RHS);
1946 ResultReg = FastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
1947 RHSIsKill);
1948 }
1949
1950 // FastISel doesn't have a pattern for X86::MUL*r. Emit it manually.
1951 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
1952 static const unsigned MULOpc[] =
1953 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
1954 static const unsigned Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
1955 // First copy the first operand into RAX, which is an implicit input to
1956 // the X86::MUL*r instruction.
1957 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1958 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
1959 .addReg(LHSReg, getKillRegState(LHSIsKill));
1960 ResultReg = FastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
1961 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
1962 }
1963
1964 if (!ResultReg)
Bill Wendling80b34b32008-12-09 02:42:50 +00001965 return false;
1966
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001967 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
1968 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
1969 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
1970 ResultReg2);
Eli Friedmana4d4a012011-05-16 21:06:17 +00001971
1972 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00001973 return true;
1974 }
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00001975 case Intrinsic::x86_sse_cvttss2si:
1976 case Intrinsic::x86_sse_cvttss2si64:
1977 case Intrinsic::x86_sse2_cvttsd2si:
1978 case Intrinsic::x86_sse2_cvttsd2si64: {
1979 bool IsInputDouble;
1980 switch (I.getIntrinsicID()) {
1981 default: llvm_unreachable("Unexpected intrinsic.");
1982 case Intrinsic::x86_sse_cvttss2si:
1983 case Intrinsic::x86_sse_cvttss2si64:
1984 if (!Subtarget->hasSSE1())
1985 return false;
1986 IsInputDouble = false;
1987 break;
1988 case Intrinsic::x86_sse2_cvttsd2si:
1989 case Intrinsic::x86_sse2_cvttsd2si64:
1990 if (!Subtarget->hasSSE2())
1991 return false;
1992 IsInputDouble = true;
1993 break;
1994 }
1995
1996 Type *RetTy = I.getCalledFunction()->getReturnType();
1997 MVT VT;
1998 if (!isTypeLegal(RetTy, VT))
1999 return false;
2000
2001 static const unsigned CvtOpc[2][2][2] = {
2002 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
2003 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
2004 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
2005 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
2006 };
2007 bool HasAVX = Subtarget->hasAVX();
2008 unsigned Opc;
2009 switch (VT.SimpleTy) {
2010 default: llvm_unreachable("Unexpected result type.");
2011 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
2012 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
2013 }
2014
2015 // Check if we can fold insertelement instructions into the convert.
2016 const Value *Op = I.getArgOperand(0);
2017 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
2018 const Value *Index = IE->getOperand(2);
2019 if (!isa<ConstantInt>(Index))
2020 break;
2021 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
2022
2023 if (Idx == 0) {
2024 Op = IE->getOperand(1);
2025 break;
2026 }
2027 Op = IE->getOperand(0);
2028 }
2029
2030 unsigned Reg = getRegForValue(Op);
2031 if (Reg == 0)
2032 return false;
2033
2034 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
2035 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2036 .addReg(Reg);
2037
2038 UpdateValueMap(&I, ResultReg);
2039 return true;
2040 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002041 }
2042}
2043
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002044bool X86FastISel::FastLowerArguments() {
2045 if (!FuncInfo.CanLowerReturn)
2046 return false;
2047
2048 const Function *F = FuncInfo.Fn;
2049 if (F->isVarArg())
2050 return false;
2051
2052 CallingConv::ID CC = F->getCallingConv();
2053 if (CC != CallingConv::C)
2054 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00002055
2056 if (Subtarget->isCallingConvWin64(CC))
2057 return false;
2058
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002059 if (!Subtarget->is64Bit())
2060 return false;
2061
2062 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002063 unsigned GPRCnt = 0;
2064 unsigned FPRCnt = 0;
2065 unsigned Idx = 0;
2066 for (auto const &Arg : F->args()) {
2067 // The first argument is at index 1.
2068 ++Idx;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002069 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2070 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2071 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2072 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2073 return false;
2074
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002075 Type *ArgTy = Arg.getType();
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002076 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
2077 return false;
2078
2079 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00002080 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002081 switch (ArgVT.getSimpleVT().SimpleTy) {
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002082 default: return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002083 case MVT::i32:
2084 case MVT::i64:
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002085 ++GPRCnt;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002086 break;
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002087 case MVT::f32:
2088 case MVT::f64:
2089 if (!Subtarget->hasSSE1())
2090 return false;
2091 ++FPRCnt;
2092 break;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002093 }
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002094
2095 if (GPRCnt > 6)
2096 return false;
2097
2098 if (FPRCnt > 8)
2099 return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002100 }
2101
Craig Topper840beec2014-04-04 05:16:06 +00002102 static const MCPhysReg GPR32ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002103 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
2104 };
Craig Topper840beec2014-04-04 05:16:06 +00002105 static const MCPhysReg GPR64ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002106 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
2107 };
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002108 static const MCPhysReg XMMArgRegs[] = {
2109 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2110 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2111 };
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002112
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002113 unsigned GPRIdx = 0;
2114 unsigned FPRIdx = 0;
2115 for (auto const &Arg : F->args()) {
2116 MVT VT = TLI.getSimpleValueType(Arg.getType());
2117 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
2118 unsigned SrcReg;
2119 switch (VT.SimpleTy) {
2120 default: llvm_unreachable("Unexpected value type.");
2121 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
2122 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
2123 case MVT::f32: // fall-through
2124 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
2125 }
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002126 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2127 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2128 // Without this, EmitLiveInCopies may eliminate the livein if its only
2129 // use is a bitcast (which isn't turned into an instruction).
2130 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002131 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002132 TII.get(TargetOpcode::COPY), ResultReg)
2133 .addReg(DstReg, getKillRegState(true));
2134 UpdateValueMap(&Arg, ResultReg);
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002135 }
2136 return true;
2137}
2138
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002139bool X86FastISel::X86SelectCall(const Instruction *I) {
2140 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00002141 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002142
2143 // Can't handle inline asm yet.
2144 if (isa<InlineAsm>(Callee))
2145 return false;
2146
Bill Wendling80b34b32008-12-09 02:42:50 +00002147 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002148 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00002149 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002150
Chad Rosierdf42cf32012-12-11 00:18:02 +00002151 // Allow SelectionDAG isel to handle tail calls.
2152 if (cast<CallInst>(I)->isTailCall())
2153 return false;
2154
Craig Topper062a2ba2014-04-25 05:30:21 +00002155 return DoSelectCall(I, nullptr);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002156}
2157
Rafael Espindola73173c52012-07-25 15:42:45 +00002158static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
2159 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002160 if (Subtarget.is64Bit())
2161 return 0;
Rafael Espindola32cb5ac2013-12-12 16:06:58 +00002162 if (Subtarget.getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002163 return 0;
2164 CallingConv::ID CC = CS.getCallingConv();
2165 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
2166 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002167 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002168 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002169 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00002170 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002171 return 4;
2172}
2173
Eli Friedmancd2124a2011-06-10 23:39:36 +00002174// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
2175bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
2176 const CallInst *CI = cast<CallInst>(I);
2177 const Value *Callee = CI->getCalledValue();
2178
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002179 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002180 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002181 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00002182 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002183 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00002184 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
2185 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002186 return false;
2187
Evan Chengd10089a2010-01-27 00:00:57 +00002188 // fastcc with -tailcallopt is intended to provide a guaranteed
2189 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002190 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00002191 return false;
2192
Chris Lattner229907c2011-07-18 04:54:35 +00002193 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2194 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002195 bool isVarArg = FTy->isVarArg();
2196
2197 // Don't know how to handle Win64 varargs yet. Nothing special needed for
2198 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00002199 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002200 return false;
2201
Reid Klecknerf5b76512014-01-31 23:50:57 +00002202 // Don't know about inalloca yet.
2203 if (CS.hasInAllocaArgument())
2204 return false;
2205
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002206 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00002207 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002208 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002209 return false;
2210
Eli Friedman7b279422011-05-17 18:29:03 +00002211 // Check whether the function can return without sret-demotion.
2212 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002213 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00002214 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00002215 *FuncInfo.MF, FTy->isVarArg(),
2216 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002217 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00002218 return false;
2219
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002220 // Materialize callee address in a register. FIXME: GV address can be
2221 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00002222 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00002223 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00002224 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002225 unsigned CalleeOp = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002226 const GlobalValue *GV = nullptr;
2227 if (CalleeAM.GV != nullptr) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002228 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00002229 } else if (CalleeAM.Base.Reg != 0) {
2230 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002231 } else
2232 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002233
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002234 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002235 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002236 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002237 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002238 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00002239 unsigned arg_size = CS.arg_size();
2240 Args.reserve(arg_size);
2241 ArgVals.reserve(arg_size);
2242 ArgVTs.reserve(arg_size);
2243 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002244 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002245 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00002246 // If we're lowering a mem intrinsic instead of a regular call, skip the
2247 // last two arguments, which should not passed to the underlying functions.
2248 if (MemIntName && e-i <= 2)
2249 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002250 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002251 ISD::ArgFlagsTy Flags;
2252 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002253 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002254 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002255 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002256 Flags.setZExt();
2257
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002258 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00002259 PointerType *Ty = cast<PointerType>(ArgVal->getType());
2260 Type *ElementTy = Ty->getElementType();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002261 unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
Eli Friedman60afcc22011-05-20 22:21:04 +00002262 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
2263 if (!FrameAlign)
2264 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
2265 Flags.setByVal();
2266 Flags.setByValSize(FrameSize);
2267 Flags.setByValAlign(FrameAlign);
2268 if (!IsMemcpySmall(FrameSize))
2269 return false;
2270 }
2271
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002272 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00002273 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002274 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00002275 Flags.setNest();
2276
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002277 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
2278 // instruction. This is safe because it is common to all fastisel supported
2279 // calling conventions on x86.
2280 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
2281 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
2282 CI->getBitWidth() == 16) {
2283 if (Flags.isSExt())
2284 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
2285 else
2286 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
2287 }
2288 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002289
Chris Lattner5f4b7832011-04-19 05:09:50 +00002290 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002291
Chris Lattner34a08c22011-04-19 05:15:59 +00002292 // Passing bools around ends up doing a trunc to i1 and passing it.
2293 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00002294 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2295 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2296 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002297 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2298 ArgReg = getRegForValue(ArgVal);
2299 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002300
Chris Lattner5f4b7832011-04-19 05:09:50 +00002301 MVT ArgVT;
2302 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002303
Chris Lattner5f4b7832011-04-19 05:09:50 +00002304 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2305 ArgVal->hasOneUse(), 1);
2306 } else {
2307 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002308 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002309
Chris Lattner34a08c22011-04-19 05:15:59 +00002310 if (ArgReg == 0) return false;
2311
Chris Lattner229907c2011-07-18 04:54:35 +00002312 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002313 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002314 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002315 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002316 if (ArgVT == MVT::x86mmx)
2317 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002318 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002319 Flags.setOrigAlign(OriginalAlignment);
2320
Chris Lattner5f4b7832011-04-19 05:09:50 +00002321 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002322 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002323 ArgVTs.push_back(ArgVT);
2324 ArgFlags.push_back(Flags);
2325 }
2326
2327 // Analyze operands of the call, assigning locations to each operand.
2328 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002329 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002330 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002331
Dan Gohman47a07242010-06-01 21:09:47 +00002332 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002333 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002334 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002335
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002336 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002337
2338 // Get a count of how many bytes are to be pushed on the stack.
2339 unsigned NumBytes = CCInfo.getNextStackOffset();
2340
2341 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002342 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002343 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002344 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002345
Chris Lattner3ba29352008-10-15 05:30:52 +00002346 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002347 // copies / loads.
2348 SmallVector<unsigned, 4> RegArgs;
2349 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2350 CCValAssign &VA = ArgLocs[i];
2351 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002352 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002353
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002354 // Promote the value if needed.
2355 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002356 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002357 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002358 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2359 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002360 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2361 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002362 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002363 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002364 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002365 }
2366 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002367 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2368 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002369 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2370 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002371 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002372 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002373 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002374 }
2375 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002376 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2377 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002378 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2379 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002380 if (!Emitted)
2381 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002382 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002383 if (!Emitted)
2384 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2385 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002386
Chris Lattner2d7df022011-01-05 22:26:52 +00002387 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002388 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002389 break;
2390 }
Dan Gohman8c795692009-08-05 05:33:42 +00002391 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002392 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002393 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002394 assert(BC != 0 && "Failed to emit a bitcast!");
2395 Arg = BC;
2396 ArgVT = VA.getLocVT();
2397 break;
2398 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002399 case CCValAssign::VExt:
2400 // VExt has not been implemented, so this should be impossible to reach
2401 // for now. However, fallback to Selection DAG isel once implemented.
2402 return false;
2403 case CCValAssign::Indirect:
2404 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2405 // support this.
2406 return false;
Lang Hames06234ec2014-01-14 19:56:36 +00002407 case CCValAssign::FPExt:
2408 llvm_unreachable("Unexpected loc info!");
Evan Cheng6500d172008-09-08 06:35:17 +00002409 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002410
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002411 if (VA.isRegLoc()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002412 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2413 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002414 RegArgs.push_back(VA.getLocReg());
2415 } else {
2416 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002417 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002418 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2419 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002420 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002421 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002422 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002423 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002424
Eli Friedman60afcc22011-05-20 22:21:04 +00002425 if (Flags.isByVal()) {
2426 X86AddressMode SrcAM;
2427 SrcAM.Base.Reg = Arg;
2428 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2429 assert(Res && "memcpy length already checked!"); (void)Res;
2430 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2431 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002432 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002433 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002434 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2435 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002436 } else {
Juergen Ributzka349777d2014-06-12 23:27:57 +00002437 if (!X86FastEmitStore(ArgVT, Arg, /*ValIsKill=*/false, AM))
Lang Hames7d2f7b52011-10-18 22:11:33 +00002438 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002439 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002440 }
2441 }
2442
Dan Gohman3691d502008-09-25 15:24:26 +00002443 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002444 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002445 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002446 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2448 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002449 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002450
Charles Davise8f297c2013-07-12 06:02:35 +00002451 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002452 // Count the number of XMM registers allocated.
Craig Topper840beec2014-04-04 05:16:06 +00002453 static const MCPhysReg XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002454 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2455 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2456 };
2457 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002458 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002459 X86::AL).addImm(NumXMMRegs);
2460 }
2461
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002462 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002463 MachineInstrBuilder MIB;
2464 if (CalleeOp) {
2465 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002466 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002467 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002468 CallOpc = X86::CALL64r;
2469 else
2470 CallOpc = X86::CALL32r;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002471 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002472 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002473
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002474 } else {
2475 // Direct call.
2476 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002477 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002478 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002479 CallOpc = X86::CALL64pcrel32;
2480 else
2481 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002482
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002483 // See if we need any target-specific flags on the GV operand.
2484 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002485
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002486 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2487 // external symbols most go through the PLT in PIC mode. If the symbol
2488 // has hidden or protected visibility, or if it is static or local, then
2489 // we don't need to use the PLT - we can directly call it.
2490 if (Subtarget->isTargetELF() &&
2491 TM.getRelocationModel() == Reloc::PIC_ &&
2492 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2493 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002494 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002495 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002496 (!Subtarget->getTargetTriple().isMacOSX() ||
2497 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002498 // PC-relative references to external symbols should go through $stub,
2499 // unless we're building with the leopard linker or later, which
2500 // automatically synthesizes these stubs.
2501 OpFlags = X86II::MO_DARWIN_STUB;
2502 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002503
2504
Rafael Espindolaea09c592014-02-18 22:05:46 +00002505 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Eli Friedmancd2124a2011-06-10 23:39:36 +00002506 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002507 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002508 else
2509 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002510 }
Dan Gohman3691d502008-09-25 15:24:26 +00002511
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002512 // Add a register mask with the call-preserved registers.
2513 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2514 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2515
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002516 // Add an implicit use GOT pointer in EBX.
2517 if (Subtarget->isPICStyleGOT())
2518 MIB.addReg(X86::EBX, RegState::Implicit);
2519
Charles Davise8f297c2013-07-12 06:02:35 +00002520 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002521 MIB.addReg(X86::AL, RegState::Implicit);
2522
2523 // Add implicit physical register uses to the call.
2524 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2525 MIB.addReg(RegArgs[i], RegState::Implicit);
2526
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002527 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002528 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002529 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002530 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002531 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002532
Eli Friedman7b279422011-05-17 18:29:03 +00002533 // Build info for return calling conv lowering code.
2534 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2535 SmallVector<ISD::InputArg, 32> Ins;
2536 SmallVector<EVT, 4> RetTys;
2537 ComputeValueVTs(TLI, I->getType(), RetTys);
2538 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2539 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002540 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002541 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2542 for (unsigned j = 0; j != NumRegs; ++j) {
2543 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002544 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002545 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002546 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002547 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002548 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002549 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002550 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002551 MyFlags.Flags.setInReg();
2552 Ins.push_back(MyFlags);
2553 }
2554 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002555
Eli Friedman7b279422011-05-17 18:29:03 +00002556 // Now handle call return values.
2557 SmallVector<unsigned, 4> UsedRegs;
2558 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002559 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002560 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002561 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2562 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2563 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2564 EVT CopyVT = RVLocs[i].getValVT();
2565 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002566
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002567 // If this is a call to a function that returns an fp value on the x87 fp
2568 // stack, but where we prefer to use the value in xmm registers, copy it
2569 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002570 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002571 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002572 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002573 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002574 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002575 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00002576 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2577 TII.get(X86::FpPOP_RETVAL), CopyReg);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002578 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002579 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2580 TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002581 CopyReg).addReg(RVLocs[i].getLocReg());
2582 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002583 }
2584
Eli Friedman7b279422011-05-17 18:29:03 +00002585 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002586 // Round the F80 the right size, which also moves to the appropriate xmm
2587 // register. This is accomplished by storing the F80 value in memory and
2588 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002589 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002590 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002591 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002592 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002593 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002594 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002595 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002596 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002597 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Friedman7b279422011-05-17 18:29:03 +00002598 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002599 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002600 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002601
Eli Friedman7b279422011-05-17 18:29:03 +00002602 if (RVLocs.size())
2603 UpdateValueMap(I, ResultReg, RVLocs.size());
2604
Dan Gohman86936502010-06-18 23:28:01 +00002605 // Set all unused physreg defs as dead.
2606 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2607
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002608 return true;
2609}
2610
2611
Dan Gohmand58f3e32008-08-28 23:21:34 +00002612bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002613X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002614 switch (I->getOpcode()) {
2615 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002616 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002617 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002618 case Instruction::Store:
2619 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002620 case Instruction::Ret:
2621 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002622 case Instruction::ICmp:
2623 case Instruction::FCmp:
2624 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002625 case Instruction::ZExt:
2626 return X86SelectZExt(I);
2627 case Instruction::Br:
2628 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002629 case Instruction::Call:
2630 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002631 case Instruction::LShr:
2632 case Instruction::AShr:
2633 case Instruction::Shl:
2634 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002635 case Instruction::SDiv:
2636 case Instruction::UDiv:
2637 case Instruction::SRem:
2638 case Instruction::URem:
2639 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002640 case Instruction::Select:
2641 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002642 case Instruction::Trunc:
2643 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002644 case Instruction::FPExt:
2645 return X86SelectFPExt(I);
2646 case Instruction::FPTrunc:
2647 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002648 case Instruction::IntToPtr: // Deliberate fall-through.
2649 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002650 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2651 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002652 if (DstVT.bitsGT(SrcVT))
2653 return X86SelectZExt(I);
2654 if (DstVT.bitsLT(SrcVT))
2655 return X86SelectTrunc(I);
2656 unsigned Reg = getRegForValue(I->getOperand(0));
2657 if (Reg == 0) return false;
2658 UpdateValueMap(I, Reg);
2659 return true;
2660 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002661 }
2662
2663 return false;
2664}
2665
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002666unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002667 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002668 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002669 return 0;
2670
2671 // Can't handle alternate code models yet.
2672 if (TM.getCodeModel() != CodeModel::Small)
2673 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002674
Owen Anderson50288e32008-09-05 00:06:23 +00002675 // Get opcode and regclass of the output for the given load instruction.
2676 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002677 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002678 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002679 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002680 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002681 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002682 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002683 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002684 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002685 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002686 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002687 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002688 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002689 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002690 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002691 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002692 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002693 // Must be in x86-64 mode.
2694 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002695 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002696 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002697 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002698 if (X86ScalarSSEf32) {
2699 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002700 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002701 } else {
2702 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002703 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002704 }
2705 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002706 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002707 if (X86ScalarSSEf64) {
2708 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002709 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002710 } else {
2711 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002712 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002713 }
2714 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002715 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002716 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002717 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002718 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002719
Dan Gohman9801ba42008-09-19 22:16:54 +00002720 // Materialize addresses with LEA instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002721 if (isa<GlobalValue>(C)) {
Louis Gerbargdcf00252014-06-16 20:31:50 +00002722 // LEA can only handle 32 bit immediates. Currently this happens pretty
2723 // rarely, so rather than deal with it just bail out of fast isel. If any
2724 // architectures endis up needing to use this path a lot then fast isel
2725 // could get the address with a MOV64ri and use that to load the value.
Louis Gerbarga5360c42014-06-16 17:35:40 +00002726 if (TM.getRelocationModel() == Reloc::Static && Subtarget->is64Bit())
2727 return false;
2728
Dan Gohman9801ba42008-09-19 22:16:54 +00002729 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002730 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002731 // If the expression is just a basereg, then we're done, otherwise we need
2732 // to emit an LEA.
2733 if (AM.BaseType == X86AddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00002734 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
Chris Lattner48326602011-04-17 17:12:08 +00002735 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002736
Chris Lattner48326602011-04-17 17:12:08 +00002737 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman9801ba42008-09-19 22:16:54 +00002738 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002739 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002740 TII.get(Opc), ResultReg), AM);
Owen Anderson50288e32008-09-05 00:06:23 +00002741 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002742 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002743 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002744 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002745
Owen Andersond41c7162008-09-06 01:11:01 +00002746 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002747 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002748 if (Align == 0) {
2749 // Alignment of vector types. FIXME!
Rafael Espindolaea09c592014-02-18 22:05:46 +00002750 Align = DL.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002751 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002752
Dan Gohman8392f0c2008-09-30 01:21:32 +00002753 // x86-32 PIC requires a PIC base register for constant pools.
2754 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002755 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002756 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002757 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002758 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002759 } else if (Subtarget->isPICStyleGOT()) {
2760 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002761 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002762 } else if (Subtarget->isPICStyleRIPRel() &&
2763 TM.getCodeModel() == CodeModel::Small) {
2764 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002765 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002766
2767 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002768 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002769 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002770 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002771 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002772 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002773
Owen Anderson50288e32008-09-05 00:06:23 +00002774 return ResultReg;
2775}
2776
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002777unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002778 // Fail on dynamic allocas. At this point, getRegForValue has already
2779 // checked its CSE maps, so if we're here trying to handle a dynamic
2780 // alloca, we're not going to succeed. X86SelectAddress has a
2781 // check for dynamic allocas, because it's called directly from
2782 // various places, but TargetMaterializeAlloca also needs a check
2783 // in order to avoid recursion between getRegForValue,
2784 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002785 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002786 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00002787 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002788
Dan Gohman39d82f92008-09-10 20:11:02 +00002789 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002790 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002791 return 0;
2792 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00002793 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00002794 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002795 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002796 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00002797 return ResultReg;
2798}
2799
Eli Friedman406c4712011-04-27 22:41:55 +00002800unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2801 MVT VT;
2802 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002803 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002804
2805 // Get opcode and regclass for the given zero.
2806 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002807 const TargetRegisterClass *RC = nullptr;
Eli Friedman406c4712011-04-27 22:41:55 +00002808 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002809 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00002810 case MVT::f32:
2811 if (X86ScalarSSEf32) {
2812 Opc = X86::FsFLD0SS;
2813 RC = &X86::FR32RegClass;
2814 } else {
2815 Opc = X86::LD_Fp032;
2816 RC = &X86::RFP32RegClass;
2817 }
2818 break;
2819 case MVT::f64:
2820 if (X86ScalarSSEf64) {
2821 Opc = X86::FsFLD0SD;
2822 RC = &X86::FR64RegClass;
2823 } else {
2824 Opc = X86::LD_Fp064;
2825 RC = &X86::RFP64RegClass;
2826 }
2827 break;
2828 case MVT::f80:
2829 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002830 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002831 }
2832
2833 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002834 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Eli Friedman406c4712011-04-27 22:41:55 +00002835 return ResultReg;
2836}
2837
2838
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002839bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2840 const LoadInst *LI) {
Juergen Ributzka349777d2014-06-12 23:27:57 +00002841 const Value *Ptr = LI->getPointerOperand();
Chris Lattnereeba0c72010-09-05 02:18:34 +00002842 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00002843 if (!X86SelectAddress(Ptr, AM))
Chris Lattnereeba0c72010-09-05 02:18:34 +00002844 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002845
Craig Topper55406d92012-08-11 17:46:16 +00002846 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00002847
Rafael Espindolaea09c592014-02-18 22:05:46 +00002848 unsigned Size = DL.getTypeAllocSize(LI->getType());
Chris Lattnereeba0c72010-09-05 02:18:34 +00002849 unsigned Alignment = LI->getAlignment();
2850
Juergen Ributzka349777d2014-06-12 23:27:57 +00002851 if (Alignment == 0) // Ensure that codegen never sees alignment 0
2852 Alignment = DL.getABITypeAlignment(LI->getType());
2853
Chris Lattnereeba0c72010-09-05 02:18:34 +00002854 SmallVector<MachineOperand, 8> AddrOps;
2855 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00002856
Chris Lattnereeba0c72010-09-05 02:18:34 +00002857 MachineInstr *Result =
2858 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
Juergen Ributzka349777d2014-06-12 23:27:57 +00002859 if (!Result)
2860 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002861
Juergen Ributzka349777d2014-06-12 23:27:57 +00002862 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Chris Lattner2d186572011-01-16 02:27:38 +00002863 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00002864 MI->eraseFromParent();
2865 return true;
2866}
2867
2868
Evan Cheng24422d42008-09-03 00:03:49 +00002869namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00002870 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2871 const TargetLibraryInfo *libInfo) {
2872 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00002873 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002874}