blob: 928dea91b4fdcd7f1d727596630de5b4e4c0853b [file] [log] [blame]
Dan Gohman1adf1b02008-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 Ributzka623d2e62013-11-08 23:28:16 +000017#include "X86CallingConv.h"
Evan Chengef41ff62011-06-23 17:54:54 +000018#include "X86ISelLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "X86InstrBuilder.h"
Evan Cheng88e30412008-09-03 01:04:47 +000020#include "X86RegisterInfo.h"
21#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000022#include "X86TargetMachine.h"
Dan Gohman84023e02010-07-10 09:00:22 +000023#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000024#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000025#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000029#include "llvm/IR/CallingConv.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/GlobalAlias.h"
32#include "llvm/IR/GlobalVariable.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/Operator.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000036#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000038#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000039#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000040using namespace llvm;
41
Chris Lattner087fcf32009-03-08 18:44:31 +000042namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000043
Evan Chengc3f44b02008-09-03 00:03:49 +000044class X86FastISel : public FastISel {
45 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
46 /// make the right decision when generating code for different targets.
47 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000048
Wesley Peckbf17cfa2010-11-23 03:31:01 +000049 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000050 /// floating point ops.
51 /// When SSE is available, use it for f32 operations.
52 /// When SSE2 is available, use it for f64 operations.
53 bool X86ScalarSSEf64;
54 bool X86ScalarSSEf32;
55
Evan Cheng8b19e562008-09-03 06:44:39 +000056public:
Bob Wilsond49edb72012-08-03 04:06:28 +000057 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
58 const TargetLibraryInfo *libInfo)
59 : FastISel(funcInfo, libInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000060 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +000061 X86ScalarSSEf64 = Subtarget->hasSSE2();
62 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000063 }
Evan Chengc3f44b02008-09-03 00:03:49 +000064
Dan Gohman46510a72010-04-15 01:51:59 +000065 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000066
Eli Bendersky75299e32013-04-19 22:29:18 +000067 /// \brief The specified machine instr operand is a vreg, and that
Chris Lattnerbeac75d2010-09-05 02:18:34 +000068 /// vreg is being provided by the specified load instruction. If possible,
69 /// try to fold the load as an operand to the instruction, returning true if
70 /// possible.
Eli Bendersky75299e32013-04-19 22:29:18 +000071 virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
72 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000073
Chad Rosierfd3417d2013-02-25 21:59:35 +000074 virtual bool FastLowerArguments();
75
Dan Gohman1adf1b02008-08-19 21:45:35 +000076#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000077
78private:
Dan Gohman46510a72010-04-15 01:51:59 +000079 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000080
Owen Andersone50ed302009-08-10 22:56:29 +000081 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000082
Craig Toppere0364b62013-07-17 05:57:45 +000083 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM,
84 bool Aligned = false);
85 bool X86FastEmitStore(EVT VT, unsigned ValReg, const X86AddressMode &AM,
86 bool Aligned = false);
Evan Cheng24e3a902008-09-08 06:35:17 +000087
Owen Andersone50ed302009-08-10 22:56:29 +000088 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000089 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000090
Dan Gohman46510a72010-04-15 01:51:59 +000091 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
92 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000093
Dan Gohman46510a72010-04-15 01:51:59 +000094 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000095
Dan Gohman46510a72010-04-15 01:51:59 +000096 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000097
Dan Gohman84023e02010-07-10 09:00:22 +000098 bool X86SelectRet(const Instruction *I);
99
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000105
Dan Gohman46510a72010-04-15 01:51:59 +0000106 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000107
Eli Bendersky50125482013-04-17 20:10:13 +0000108 bool X86SelectDivRem(const Instruction *I);
109
Dan Gohman46510a72010-04-15 01:51:59 +0000110 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000111
Dan Gohman46510a72010-04-15 01:51:59 +0000112 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000113
Dan Gohman46510a72010-04-15 01:51:59 +0000114 bool X86SelectFPExt(const Instruction *I);
115 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000116
Dan Gohman46510a72010-04-15 01:51:59 +0000117 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
118 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000119
Eli Friedman25255cb2011-06-10 23:39:36 +0000120 bool DoSelectCall(const Instruction *I, const char *MemIntName);
121
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000122 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000123 return getTargetMachine()->getInstrInfo();
124 }
125 const X86TargetMachine *getTargetMachine() const {
126 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000127 }
128
Bill Wendling42d42592013-09-24 07:19:30 +0000129 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
130
Dan Gohman46510a72010-04-15 01:51:59 +0000131 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000132
Dan Gohman46510a72010-04-15 01:51:59 +0000133 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000134
Eli Friedman2790ba82011-04-27 22:41:55 +0000135 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
136
Evan Chengf3d4efe2008-09-07 09:09:33 +0000137 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
138 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000139 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
141 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000142 }
143
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000144 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000145
Eli Friedmanc0883452011-05-20 22:21:04 +0000146 bool IsMemcpySmall(uint64_t Len);
147
Eli Friedmand5089a92011-04-27 01:45:07 +0000148 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
149 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000150};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000151
Chris Lattner087fcf32009-03-08 18:44:31 +0000152} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000153
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000154bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000155 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
156 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000157 // Unhandled type. Halt "fast" selection and bail.
158 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000159
160 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000161 // For now, require SSE/SSE2 for performing floating-point operations,
162 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topperf4cfc442012-08-11 17:53:00 +0000164 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topperf4cfc442012-08-11 17:53:00 +0000166 return false;
Dan Gohman9b66d732008-09-30 00:48:39 +0000167 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000169 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000170 // We only handle legal types. For example, on x86-32 the instruction
171 // selector contains all of the 64-bit instructions from x86-64,
172 // under the assumption that i64 won't be used if the target doesn't
173 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000175}
176
177#include "X86GenCallingConv.inc"
178
Evan Cheng0de588f2008-09-05 21:00:03 +0000179/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000180/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000181/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000182bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000183 unsigned &ResultReg) {
184 // Get opcode and regclass of the output for the given load instruction.
185 unsigned Opc = 0;
186 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000188 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000189 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000191 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +0000192 RC = &X86::GR8RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000195 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +0000196 RC = &X86::GR16RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000199 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000200 RC = &X86::GR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000201 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 // Must be in x86-64 mode.
204 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000205 RC = &X86::GR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000206 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000208 if (X86ScalarSSEf32) {
209 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +0000210 RC = &X86::FR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000211 } else {
212 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +0000213 RC = &X86::RFP32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000214 }
215 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000217 if (X86ScalarSSEf64) {
218 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +0000219 RC = &X86::FR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 } else {
221 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +0000222 RC = &X86::RFP64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000223 }
224 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000226 // No f80 support yet.
227 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000228 }
229
230 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000231 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
232 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000233 return true;
234}
235
Evan Chengf3d4efe2008-09-07 09:09:33 +0000236/// X86FastEmitStore - Emit a machine instruction to store a value Val of
237/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
238/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000239/// i.e. V. Return true if it is possible.
240bool
Craig Toppere0364b62013-07-17 05:57:45 +0000241X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg,
242 const X86AddressMode &AM, bool Aligned) {
Dan Gohman863890e2008-09-08 16:31:35 +0000243 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000244 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 switch (VT.getSimpleVT().SimpleTy) {
246 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000247 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000248 case MVT::i1: {
249 // Mask out all but lowest bit.
Craig Topperc9099502012-04-20 06:31:50 +0000250 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Craig Toppere0364b62013-07-17 05:57:45 +0000252 TII.get(X86::AND8ri), AndResult).addReg(ValReg).addImm(1);
253 ValReg = AndResult;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000254 }
255 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 case MVT::i8: Opc = X86::MOV8mr; break;
257 case MVT::i16: Opc = X86::MOV16mr; break;
258 case MVT::i32: Opc = X86::MOV32mr; break;
259 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
260 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000261 Opc = X86ScalarSSEf32 ?
262 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000263 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000265 Opc = X86ScalarSSEf64 ?
266 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000267 break;
Lang Hamese4824712011-10-18 22:11:33 +0000268 case MVT::v4f32:
Craig Toppere0364b62013-07-17 05:57:45 +0000269 if (Aligned)
Craig Topper77c95b62013-07-17 06:58:23 +0000270 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Toppere0364b62013-07-17 05:57:45 +0000271 else
Craig Topper77c95b62013-07-17 06:58:23 +0000272 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hamese4824712011-10-18 22:11:33 +0000273 break;
274 case MVT::v2f64:
Craig Toppere0364b62013-07-17 05:57:45 +0000275 if (Aligned)
Craig Topperfe754512013-07-18 07:16:44 +0000276 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Toppere0364b62013-07-17 05:57:45 +0000277 else
Craig Topperfe754512013-07-18 07:16:44 +0000278 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hamese4824712011-10-18 22:11:33 +0000279 break;
280 case MVT::v4i32:
281 case MVT::v2i64:
282 case MVT::v8i16:
283 case MVT::v16i8:
Craig Toppere0364b62013-07-17 05:57:45 +0000284 if (Aligned)
Craig Topper77c95b62013-07-17 06:58:23 +0000285 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Toppere0364b62013-07-17 05:57:45 +0000286 else
Craig Topper77c95b62013-07-17 06:58:23 +0000287 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hamese4824712011-10-18 22:11:33 +0000288 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000289 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000290
Dan Gohman84023e02010-07-10 09:00:22 +0000291 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Craig Toppere0364b62013-07-17 05:57:45 +0000292 DL, TII.get(Opc)), AM).addReg(ValReg);
Evan Cheng0de588f2008-09-05 21:00:03 +0000293 return true;
294}
295
Dan Gohman46510a72010-04-15 01:51:59 +0000296bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Craig Toppere0364b62013-07-17 05:57:45 +0000297 const X86AddressMode &AM, bool Aligned) {
Chris Lattner438949a2008-10-15 05:30:52 +0000298 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000299 if (isa<ConstantPointerNull>(Val))
300 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000301
Chris Lattner438949a2008-10-15 05:30:52 +0000302 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000303 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000304 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000305 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000307 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000308 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 case MVT::i8: Opc = X86::MOV8mi; break;
310 case MVT::i16: Opc = X86::MOV16mi; break;
311 case MVT::i32: Opc = X86::MOV32mi; break;
312 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000313 // Must be a 32-bit sign extended value.
Jakub Staszakeaf77252012-11-15 19:05:23 +0000314 if (isInt<32>(CI->getSExtValue()))
Chris Lattner438949a2008-10-15 05:30:52 +0000315 Opc = X86::MOV64mi32;
316 break;
317 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000318
Chris Lattner438949a2008-10-15 05:30:52 +0000319 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000320 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
321 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000322 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000323 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000324 return true;
325 }
326 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000327
Chris Lattner438949a2008-10-15 05:30:52 +0000328 unsigned ValReg = getRegForValue(Val);
329 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000330 return false;
331
Craig Toppere0364b62013-07-17 05:57:45 +0000332 return X86FastEmitStore(VT, ValReg, AM, Aligned);
Chris Lattner438949a2008-10-15 05:30:52 +0000333}
334
Evan Cheng24e3a902008-09-08 06:35:17 +0000335/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
336/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
337/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000338bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
339 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000340 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000341 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
342 Src, /*TODO: Kill=*/false);
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000343 if (RR == 0)
Owen Andersonac34a002008-09-11 19:44:55 +0000344 return false;
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000345
346 ResultReg = RR;
347 return true;
Evan Cheng24e3a902008-09-08 06:35:17 +0000348}
349
Bill Wendling42d42592013-09-24 07:19:30 +0000350bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
351 // Handle constant address.
352 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
353 // Can't handle alternate code models yet.
354 if (TM.getCodeModel() != CodeModel::Small)
355 return false;
356
357 // Can't handle TLS yet.
358 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
359 if (GVar->isThreadLocal())
360 return false;
361
362 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
363 // it works...).
364 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
365 if (const GlobalVariable *GVar =
366 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
367 if (GVar->isThreadLocal())
368 return false;
369
370 // RIP-relative addresses can't have additional register operands, so if
371 // we've already folded stuff into the addressing mode, just force the
372 // global value into its own register, which we can use as the basereg.
373 if (!Subtarget->isPICStyleRIPRel() ||
374 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
375 // Okay, we've committed to selecting this global. Set up the address.
376 AM.GV = GV;
377
378 // Allow the subtarget to classify the global.
379 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
380
381 // If this reference is relative to the pic base, set it now.
382 if (isGlobalRelativeToPICBase(GVFlags)) {
383 // FIXME: How do we know Base.Reg is free??
384 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
385 }
386
387 // Unless the ABI requires an extra load, return a direct reference to
388 // the global.
389 if (!isGlobalStubReference(GVFlags)) {
390 if (Subtarget->isPICStyleRIPRel()) {
391 // Use rip-relative addressing if we can. Above we verified that the
392 // base and index registers are unused.
393 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
394 AM.Base.Reg = X86::RIP;
395 }
396 AM.GVOpFlags = GVFlags;
397 return true;
398 }
399
400 // Ok, we need to do a load from a stub. If we've already loaded from
401 // this stub, reuse the loaded pointer, otherwise emit the load now.
402 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
403 unsigned LoadReg;
404 if (I != LocalValueMap.end() && I->second != 0) {
405 LoadReg = I->second;
406 } else {
407 // Issue load from stub.
408 unsigned Opc = 0;
409 const TargetRegisterClass *RC = NULL;
410 X86AddressMode StubAM;
411 StubAM.Base.Reg = AM.Base.Reg;
412 StubAM.GV = GV;
413 StubAM.GVOpFlags = GVFlags;
414
415 // Prepare for inserting code in the local-value area.
416 SavePoint SaveInsertPt = enterLocalValueArea();
417
418 if (TLI.getPointerTy() == MVT::i64) {
419 Opc = X86::MOV64rm;
420 RC = &X86::GR64RegClass;
421
422 if (Subtarget->isPICStyleRIPRel())
423 StubAM.Base.Reg = X86::RIP;
424 } else {
425 Opc = X86::MOV32rm;
426 RC = &X86::GR32RegClass;
427 }
428
429 LoadReg = createResultReg(RC);
430 MachineInstrBuilder LoadMI =
431 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
432 addFullAddress(LoadMI, StubAM);
433
434 // Ok, back to normal mode.
435 leaveLocalValueArea(SaveInsertPt);
436
437 // Prevent loading GV stub multiple times in same MBB.
438 LocalValueMap[V] = LoadReg;
439 }
440
441 // Now construct the final address. Note that the Disp, Scale,
442 // and Index values may already be set here.
443 AM.Base.Reg = LoadReg;
444 AM.GV = 0;
445 return true;
446 }
447 }
448
449 // If all else fails, try to materialize the value in a register.
450 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
451 if (AM.Base.Reg == 0) {
452 AM.Base.Reg = getRegForValue(V);
453 return AM.Base.Reg != 0;
454 }
455 if (AM.IndexReg == 0) {
456 assert(AM.Scale == 1 && "Scale with no index!");
457 AM.IndexReg = getRegForValue(V);
458 return AM.IndexReg != 0;
459 }
460 }
461
462 return false;
463}
464
Dan Gohman0586d912008-09-10 20:11:02 +0000465/// X86SelectAddress - Attempt to fill in an address from the given value.
466///
Dan Gohman46510a72010-04-15 01:51:59 +0000467bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendling42d42592013-09-24 07:19:30 +0000468 SmallVector<const Value *, 32> GEPs;
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000469redo_gep:
Dan Gohman46510a72010-04-15 01:51:59 +0000470 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000471 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000472 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000473 // Don't walk into other basic blocks; it's possible we haven't
474 // visited them yet, so the instructions may not yet be assigned
475 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000476 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
477 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
478 Opcode = I->getOpcode();
479 U = I;
480 }
Dan Gohman46510a72010-04-15 01:51:59 +0000481 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000482 Opcode = C->getOpcode();
483 U = C;
484 }
Dan Gohman0586d912008-09-10 20:11:02 +0000485
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000486 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000487 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000488 // Fast instruction selection doesn't support the special
489 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000490 return false;
491
Dan Gohman35893082008-09-18 23:23:44 +0000492 switch (Opcode) {
493 default: break;
494 case Instruction::BitCast:
495 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000496 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000497
498 case Instruction::IntToPtr:
499 // Look past no-op inttoptrs.
500 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000501 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000502 break;
Dan Gohman35893082008-09-18 23:23:44 +0000503
504 case Instruction::PtrToInt:
505 // Look past no-op ptrtoints.
506 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000507 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000508 break;
Dan Gohman35893082008-09-18 23:23:44 +0000509
510 case Instruction::Alloca: {
511 // Do static allocas.
512 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000513 DenseMap<const AllocaInst*, int>::iterator SI =
514 FuncInfo.StaticAllocaMap.find(A);
515 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000516 AM.BaseType = X86AddressMode::FrameIndexBase;
517 AM.Base.FrameIndex = SI->second;
518 return true;
519 }
520 break;
Dan Gohman35893082008-09-18 23:23:44 +0000521 }
522
523 case Instruction::Add: {
524 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000525 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000526 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
527 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000528 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000529 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000530 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000531 }
Dan Gohman0586d912008-09-10 20:11:02 +0000532 }
Dan Gohman35893082008-09-18 23:23:44 +0000533 break;
534 }
535
536 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000537 X86AddressMode SavedAM = AM;
538
Dan Gohman35893082008-09-18 23:23:44 +0000539 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000540 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000541 unsigned IndexReg = AM.IndexReg;
542 unsigned Scale = AM.Scale;
543 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000544 // Iterate through the indices, folding what we can. Constants can be
545 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000546 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000547 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000548 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000549 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000550 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000551 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
552 continue;
553 }
Eric Christopher471e4222011-06-08 23:55:35 +0000554
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000555 // A array/variable index is always of the form i*S where S is the
556 // constant scale size. See if we can push the scale into immediates.
557 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
558 for (;;) {
559 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
560 // Constant-offset addressing.
561 Disp += CI->getSExtValue() * S;
562 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000563 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000564 if (isa<AddOperator>(Op) &&
565 (!isa<Instruction>(Op) ||
566 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
567 == FuncInfo.MBB) &&
568 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
569 // An add (in the same block) with a constant operand. Fold the
570 // constant.
571 ConstantInt *CI =
572 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
573 Disp += CI->getSExtValue() * S;
574 // Iterate on the other operand.
575 Op = cast<AddOperator>(Op)->getOperand(0);
576 continue;
577 }
578 if (IndexReg == 0 &&
579 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
580 (S == 1 || S == 2 || S == 4 || S == 8)) {
581 // Scaled-index addressing.
582 Scale = S;
583 IndexReg = getRegForGEPIndex(Op).first;
584 if (IndexReg == 0)
585 return false;
586 break;
587 }
588 // Unsupported.
589 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000590 }
591 }
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000592
Dan Gohman09aae462008-09-26 20:04:15 +0000593 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000594 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000595 break;
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000596
Dan Gohman35893082008-09-18 23:23:44 +0000597 AM.IndexReg = IndexReg;
598 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000599 AM.Disp = (uint32_t)Disp;
Bill Wendling42d42592013-09-24 07:19:30 +0000600 GEPs.push_back(V);
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000601
602 if (const GetElementPtrInst *GEP =
603 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
604 // Ok, the GEP indices were covered by constant-offset and scaled-index
605 // addressing. Update the address state and move on to examining the base.
606 V = GEP;
607 goto redo_gep;
608 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner225d4ca2010-03-04 19:48:19 +0000609 return true;
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000610 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000611
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000612 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000613 // our address and just match the value instead of completely failing.
614 AM = SavedAM;
Bill Wendling42d42592013-09-24 07:19:30 +0000615
616 for (SmallVectorImpl<const Value *>::reverse_iterator
617 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
618 if (handleConstantAddresses(*I, AM))
619 return true;
620
621 return false;
Dan Gohman35893082008-09-18 23:23:44 +0000622 unsupported_gep:
623 // Ok, the GEP indices weren't all covered.
624 break;
625 }
626 }
627
Bill Wendling42d42592013-09-24 07:19:30 +0000628 return handleConstantAddresses(V, AM);
Dan Gohman0586d912008-09-10 20:11:02 +0000629}
630
Chris Lattner0aa43de2009-07-10 05:33:42 +0000631/// X86SelectCallAddress - Attempt to fill in an address from the given value.
632///
Dan Gohman46510a72010-04-15 01:51:59 +0000633bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
634 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000635 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet41d6f452013-10-14 22:32:09 +0000636 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf45787c2013-10-22 21:29:08 +0000637 // Record if the value is defined in the same basic block.
638 //
639 // This information is crucial to know whether or not folding an
640 // operand is valid.
641 // Indeed, FastISel generates or reuses a virtual register for all
642 // operands of all instructions it selects. Obviously, the definition and
643 // its uses must use the same virtual register otherwise the produced
644 // code is incorrect.
645 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
646 // registers for values that are alive across basic blocks. This ensures
647 // that the values are consistently set between across basic block, even
648 // if different instruction selection mechanisms are used (e.g., a mix of
649 // SDISel and FastISel).
650 // For values local to a basic block, the instruction selection process
651 // generates these virtual registers with whatever method is appropriate
652 // for its needs. In particular, FastISel and SDISel do not share the way
653 // local virtual registers are set.
654 // Therefore, this is impossible (or at least unsafe) to share values
655 // between basic blocks unless they use the same instruction selection
656 // method, which is not guarantee for X86.
657 // Moreover, things like hasOneUse could not be used accurately, if we
658 // allow to reference values across basic blocks whereas they are not
659 // alive across basic blocks initially.
Quentin Colombet41d6f452013-10-14 22:32:09 +0000660 bool InMBB = true;
661 if (I) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000662 Opcode = I->getOpcode();
663 U = I;
Quentin Colombet41d6f452013-10-14 22:32:09 +0000664 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohman46510a72010-04-15 01:51:59 +0000665 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000666 Opcode = C->getOpcode();
667 U = C;
668 }
669
670 switch (Opcode) {
671 default: break;
672 case Instruction::BitCast:
Quentin Colombet41d6f452013-10-14 22:32:09 +0000673 // Look past bitcasts if its operand is in the same BB.
674 if (InMBB)
675 return X86SelectCallAddress(U->getOperand(0), AM);
676 break;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000677
678 case Instruction::IntToPtr:
Quentin Colombet41d6f452013-10-14 22:32:09 +0000679 // Look past no-op inttoptrs if its operand is in the same BB.
680 if (InMBB &&
681 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000682 return X86SelectCallAddress(U->getOperand(0), AM);
683 break;
684
685 case Instruction::PtrToInt:
Quentin Colombet41d6f452013-10-14 22:32:09 +0000686 // Look past no-op ptrtoints if its operand is in the same BB.
687 if (InMBB &&
688 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000689 return X86SelectCallAddress(U->getOperand(0), AM);
690 break;
691 }
692
693 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000694 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000695 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000696 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000697 return false;
698
699 // RIP-relative addresses can't have additional register operands.
700 if (Subtarget->isPICStyleRIPRel() &&
701 (AM.Base.Reg != 0 || AM.IndexReg != 0))
702 return false;
703
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000704 // Can't handle DLLImport.
705 if (GV->hasDLLImportLinkage())
706 return false;
707
708 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000709 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000710 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000711 return false;
712
713 // Okay, we've committed to selecting this global. Set up the basic address.
714 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000715
Chris Lattnere6c07b52009-07-10 05:45:15 +0000716 // No ABI requires an extra load for anything other than DLLImport, which
717 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000718 if (Subtarget->isPICStyleRIPRel()) {
719 // Use rip-relative addressing if we can. Above we verified that the
720 // base and index registers are unused.
721 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
722 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000723 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000724 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
725 } else if (Subtarget->isPICStyleGOT()) {
726 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000727 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000728
Chris Lattner0aa43de2009-07-10 05:33:42 +0000729 return true;
730 }
731
732 // If all else fails, try to materialize the value in a register.
733 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
734 if (AM.Base.Reg == 0) {
735 AM.Base.Reg = getRegForValue(V);
736 return AM.Base.Reg != 0;
737 }
738 if (AM.IndexReg == 0) {
739 assert(AM.Scale == 1 && "Scale with no index!");
740 AM.IndexReg = getRegForValue(V);
741 return AM.IndexReg != 0;
742 }
743 }
744
745 return false;
746}
747
748
Owen Andersona3971df2008-09-04 07:08:58 +0000749/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000750bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000751 // Atomic stores need special handling.
Lang Hamese4824712011-10-18 22:11:33 +0000752 const StoreInst *S = cast<StoreInst>(I);
753
754 if (S->isAtomic())
755 return false;
756
Craig Toppere0364b62013-07-17 05:57:45 +0000757 unsigned SABIAlignment =
758 TD.getABITypeAlignment(S->getValueOperand()->getType());
759 bool Aligned = S->getAlignment() == 0 || S->getAlignment() >= SABIAlignment;
760
Duncan Sands1440e8b2010-11-03 11:35:31 +0000761 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000762 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000763 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000764
Dan Gohman0586d912008-09-10 20:11:02 +0000765 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000766 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000767 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000768
Craig Toppere0364b62013-07-17 05:57:45 +0000769 return X86FastEmitStore(VT, I->getOperand(0), AM, Aligned);
Owen Andersona3971df2008-09-04 07:08:58 +0000770}
771
Dan Gohman84023e02010-07-10 09:00:22 +0000772/// X86SelectRet - Select and emit code to implement ret instructions.
773bool X86FastISel::X86SelectRet(const Instruction *I) {
774 const ReturnInst *Ret = cast<ReturnInst>(I);
775 const Function &F = *I->getParent()->getParent();
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000776 const X86MachineFunctionInfo *X86MFInfo =
777 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohman84023e02010-07-10 09:00:22 +0000778
779 if (!FuncInfo.CanLowerReturn)
780 return false;
781
782 CallingConv::ID CC = F.getCallingConv();
783 if (CC != CallingConv::C &&
784 CC != CallingConv::Fast &&
Charles Davisac226bb2013-07-12 06:02:35 +0000785 CC != CallingConv::X86_FastCall &&
786 CC != CallingConv::X86_64_SysV)
Dan Gohman84023e02010-07-10 09:00:22 +0000787 return false;
788
Charles Davisac226bb2013-07-12 06:02:35 +0000789 if (Subtarget->isCallingConvWin64(CC))
Dan Gohman84023e02010-07-10 09:00:22 +0000790 return false;
791
792 // Don't handle popping bytes on return for now.
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000793 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszakd61932b2013-02-17 18:35:25 +0000794 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000795
796 // fastcc with -tailcallopt is intended to provide a guaranteed
797 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000798 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohman84023e02010-07-10 09:00:22 +0000799 return false;
800
801 // Let SDISel handle vararg functions.
802 if (F.isVarArg())
803 return false;
804
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000805 // Build a list of return value registers.
806 SmallVector<unsigned, 4> RetRegs;
807
Dan Gohman84023e02010-07-10 09:00:22 +0000808 if (Ret->getNumOperands() > 0) {
809 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +0000810 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohman84023e02010-07-10 09:00:22 +0000811
812 // Analyze operands of the call, assigning locations to each operand.
813 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000814 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +0000815 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000816 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000817
818 const Value *RV = Ret->getOperand(0);
819 unsigned Reg = getRegForValue(RV);
820 if (Reg == 0)
821 return false;
822
823 // Only handle a single return value for now.
824 if (ValLocs.size() != 1)
825 return false;
826
827 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000828
Dan Gohman84023e02010-07-10 09:00:22 +0000829 // Don't bother handling odd stuff for now.
830 if (VA.getLocInfo() != CCValAssign::Full)
831 return false;
832 // Only handle register returns for now.
833 if (!VA.isRegLoc())
834 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000835
836 // The calling-convention tables for x87 returns don't tell
837 // the whole story.
838 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
839 return false;
840
Eli Friedman22486c92011-05-18 23:13:10 +0000841 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000842 EVT SrcVT = TLI.getValueType(RV->getType());
843 EVT DstVT = VA.getValVT();
844 // Special handling for extended integers.
845 if (SrcVT != DstVT) {
846 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
847 return false;
848
849 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
850 return false;
851
852 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
853
854 if (SrcVT == MVT::i1) {
855 if (Outs[0].Flags.isSExt())
856 return false;
857 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
858 SrcVT = MVT::i8;
859 }
860 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
861 ISD::SIGN_EXTEND;
862 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
863 SrcReg, /*TODO: Kill=*/false);
864 }
865
866 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000867 unsigned DstReg = VA.getLocReg();
868 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000869 // Avoid a cross-class copy. This is very unlikely.
870 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000871 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000872 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
873 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000874
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000875 // Add register to return instruction.
876 RetRegs.push_back(VA.getLocReg());
Dan Gohman84023e02010-07-10 09:00:22 +0000877 }
878
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000879 // The x86-64 ABI for returning structs by value requires that we copy
880 // the sret argument into %rax for the return. We saved the argument into
881 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000882 // and into %rax. We also do the same with %eax for Win32.
883 if (F.hasStructRetAttr() &&
884 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000885 unsigned Reg = X86MFInfo->getSRetReturnReg();
886 assert(Reg &&
887 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000888 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000890 RetReg).addReg(Reg);
891 RetRegs.push_back(RetReg);
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000892 }
893
Dan Gohman84023e02010-07-10 09:00:22 +0000894 // Now emit the RET.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000895 MachineInstrBuilder MIB =
896 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
897 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
898 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohman84023e02010-07-10 09:00:22 +0000899 return true;
900}
901
Evan Cheng8b19e562008-09-03 06:44:39 +0000902/// X86SelectLoad - Select and emit code to implement load instructions.
903///
Dan Gohman46510a72010-04-15 01:51:59 +0000904bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000905 // Atomic loads need special handling.
906 if (cast<LoadInst>(I)->isAtomic())
907 return false;
908
Duncan Sands1440e8b2010-11-03 11:35:31 +0000909 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000910 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000911 return false;
912
Dan Gohman0586d912008-09-10 20:11:02 +0000913 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000914 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000915 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000916
Evan Cheng0de588f2008-09-05 21:00:03 +0000917 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000918 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000919 UpdateValueMap(I, ResultReg);
920 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000921 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000922 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000923}
924
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000925static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000926 bool HasAVX = Subtarget->hasAVX();
Craig Topper1accb7e2012-01-10 06:54:16 +0000927 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
928 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000929
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000931 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 case MVT::i8: return X86::CMP8rr;
933 case MVT::i16: return X86::CMP16rr;
934 case MVT::i32: return X86::CMP32rr;
935 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000936 case MVT::f32:
937 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
938 case MVT::f64:
939 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000940 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000941}
942
Chris Lattner0e13c782008-10-15 04:13:29 +0000943/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
944/// of the comparison, return an opcode that works for the compare (e.g.
945/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000946static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000948 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000949 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 case MVT::i8: return X86::CMP8ri;
951 case MVT::i16: return X86::CMP16ri;
952 case MVT::i32: return X86::CMP32ri;
953 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000954 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
955 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000956 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000957 return X86::CMP64ri32;
958 return 0;
959 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000960}
961
Dan Gohman46510a72010-04-15 01:51:59 +0000962bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
963 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000964 unsigned Op0Reg = getRegForValue(Op0);
965 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000966
Chris Lattnerd53886b2008-10-15 05:18:04 +0000967 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000968 if (isa<ConstantPointerNull>(Op1))
969 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000970
Chris Lattner9a08a612008-10-15 04:26:38 +0000971 // We have two options: compare with register or immediate. If the RHS of
972 // the compare is an immediate that we can fold into this compare, use
973 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000974 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000975 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000976 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
977 .addReg(Op0Reg)
978 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000979 return true;
980 }
981 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000982
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000983 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000984 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000985
Chris Lattner9a08a612008-10-15 04:26:38 +0000986 unsigned Op1Reg = getRegForValue(Op1);
987 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000988 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
989 .addReg(Op0Reg)
990 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000991
Chris Lattner9a08a612008-10-15 04:26:38 +0000992 return true;
993}
994
Dan Gohman46510a72010-04-15 01:51:59 +0000995bool X86FastISel::X86SelectCmp(const Instruction *I) {
996 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000997
Duncan Sands1440e8b2010-11-03 11:35:31 +0000998 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000999 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +00001000 return false;
1001
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001002 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +00001003 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +00001004 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001005 switch (CI->getPredicate()) {
1006 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +00001007 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1008 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001009
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001010 unsigned EReg = createResultReg(&X86::GR8RegClass);
1011 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001012 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
1013 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1014 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001015 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001016 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +00001017 UpdateValueMap(I, ResultReg);
1018 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001019 }
1020 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +00001021 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1022 return false;
1023
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001024 unsigned NEReg = createResultReg(&X86::GR8RegClass);
1025 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +00001026 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
1027 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
1028 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +00001029 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +00001030 UpdateValueMap(I, ResultReg);
1031 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001032 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +00001033 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1034 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1035 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
1036 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
1037 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1038 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
1039 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
1040 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1041 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
1042 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
1043 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1044 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001045
Chris Lattner8aeeeb92008-10-15 03:52:54 +00001046 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1047 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1048 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1049 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1050 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1051 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
1052 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
1053 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
1054 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
1055 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001056 default:
1057 return false;
1058 }
1059
Dan Gohman46510a72010-04-15 01:51:59 +00001060 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +00001061 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +00001062 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +00001063
Chris Lattner9a08a612008-10-15 04:26:38 +00001064 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +00001065 if (!X86FastEmitCompare(Op0, Op1, VT))
1066 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001067
Dan Gohman84023e02010-07-10 09:00:22 +00001068 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001069 UpdateValueMap(I, ResultReg);
1070 return true;
1071}
Evan Cheng8b19e562008-09-03 06:44:39 +00001072
Dan Gohman46510a72010-04-15 01:51:59 +00001073bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedman76927d732011-05-25 23:49:02 +00001074 EVT DstVT = TLI.getValueType(I->getType());
1075 if (!TLI.isTypeLegal(DstVT))
1076 return false;
1077
1078 unsigned ResultReg = getRegForValue(I->getOperand(0));
1079 if (ResultReg == 0)
1080 return false;
1081
Tim Northoverda0416b2013-05-30 10:43:18 +00001082 // Handle zero-extension from i1 to i8, which is common.
Craig Topper5a0910b2013-08-15 02:33:50 +00001083 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northoverda0416b2013-05-30 10:43:18 +00001084 if (SrcVT.SimpleTy == MVT::i1) {
1085 // Set the high bits to zero.
1086 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1087 SrcVT = MVT::i8;
Eli Friedman76927d732011-05-25 23:49:02 +00001088
Tim Northoverda0416b2013-05-30 10:43:18 +00001089 if (ResultReg == 0)
1090 return false;
1091 }
1092
1093 if (DstVT == MVT::i64) {
1094 // Handle extension to 64-bits via sub-register shenanigans.
1095 unsigned MovInst;
1096
1097 switch (SrcVT.SimpleTy) {
1098 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1099 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1100 case MVT::i32: MovInst = X86::MOV32rr; break;
1101 default: llvm_unreachable("Unexpected zext to i64 source type");
1102 }
1103
1104 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1105 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovInst), Result32)
1106 .addReg(ResultReg);
1107
1108 ResultReg = createResultReg(&X86::GR64RegClass);
1109 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::SUBREG_TO_REG),
1110 ResultReg)
1111 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1112 } else if (DstVT != MVT::i8) {
Eli Friedman76927d732011-05-25 23:49:02 +00001113 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1114 ResultReg, /*Kill=*/true);
1115 if (ResultReg == 0)
1116 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +00001117 }
1118
Eli Friedman76927d732011-05-25 23:49:02 +00001119 UpdateValueMap(I, ResultReg);
1120 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +00001121}
1122
Chris Lattner9a08a612008-10-15 04:26:38 +00001123
Dan Gohman46510a72010-04-15 01:51:59 +00001124bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +00001125 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +00001126 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001127 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +00001128 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1129 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +00001130
Dan Gohman8bef7442010-08-21 02:32:36 +00001131 // Fold the common case of a conditional branch with a comparison
1132 // in the same block (values defined on other blocks may not have
1133 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +00001134 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +00001135 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001136 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001137
Dan Gohmand98d6202008-10-02 22:15:21 +00001138 // Try to take advantage of fallthrough opportunities.
1139 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001140 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001141 std::swap(TrueMBB, FalseMBB);
1142 Predicate = CmpInst::getInversePredicate(Predicate);
1143 }
1144
Chris Lattner871d2462008-10-15 03:58:05 +00001145 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1146 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1147
Dan Gohmand98d6202008-10-02 22:15:21 +00001148 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001149 case CmpInst::FCMP_OEQ:
1150 std::swap(TrueMBB, FalseMBB);
1151 Predicate = CmpInst::FCMP_UNE;
1152 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001153 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1154 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1155 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1156 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1157 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1158 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1159 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1160 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1161 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1162 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1163 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1164 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1165 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001166
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001167 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1168 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1169 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1170 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1171 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1172 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1173 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1174 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1175 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1176 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001177 default:
1178 return false;
1179 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001180
Dan Gohman46510a72010-04-15 01:51:59 +00001181 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001182 if (SwapArgs)
1183 std::swap(Op0, Op1);
1184
Chris Lattner9a08a612008-10-15 04:26:38 +00001185 // Emit a compare of the LHS and RHS, setting the flags.
1186 if (!X86FastEmitCompare(Op0, Op1, VT))
1187 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001188
Dan Gohman84023e02010-07-10 09:00:22 +00001189 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1190 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001191
1192 if (Predicate == CmpInst::FCMP_UNE) {
1193 // X86 requires a second branch to handle UNE (and OEQ,
1194 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001195 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1196 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001197 }
1198
Stuart Hastings3bf91252010-06-17 22:43:56 +00001199 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001200 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001201 return true;
1202 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001203 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1204 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1205 // typically happen for _Bool and C++ bools.
1206 MVT SourceVT;
1207 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1208 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1209 unsigned TestOpc = 0;
1210 switch (SourceVT.SimpleTy) {
1211 default: break;
1212 case MVT::i8: TestOpc = X86::TEST8ri; break;
1213 case MVT::i16: TestOpc = X86::TEST16ri; break;
1214 case MVT::i32: TestOpc = X86::TEST32ri; break;
1215 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1216 }
1217 if (TestOpc) {
1218 unsigned OpReg = getRegForValue(TI->getOperand(0));
1219 if (OpReg == 0) return false;
1220 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1221 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001222
Chris Lattnerc76d1212011-04-19 04:26:32 +00001223 unsigned JmpOpc = X86::JNE_4;
1224 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1225 std::swap(TrueMBB, FalseMBB);
1226 JmpOpc = X86::JE_4;
1227 }
Eric Christopher471e4222011-06-08 23:55:35 +00001228
Chris Lattnerc76d1212011-04-19 04:26:32 +00001229 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001230 .addMBB(TrueMBB);
1231 FastEmitBranch(FalseMBB, DL);
1232 FuncInfo.MBB->addSuccessor(TrueMBB);
1233 return true;
1234 }
1235 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001236 }
1237
1238 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001239 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1240 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001241 unsigned OpReg = getRegForValue(BI->getCondition());
1242 if (OpReg == 0) return false;
1243
Eli Friedman547eb4f2011-04-27 01:34:27 +00001244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1245 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1247 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001248 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001249 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001250 return true;
1251}
1252
Dan Gohman46510a72010-04-15 01:51:59 +00001253bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001254 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001255 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001256 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001257 CReg = X86::CL;
1258 RC = &X86::GR8RegClass;
1259 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001260 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1261 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1262 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001263 default: return false;
1264 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001265 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001266 CReg = X86::CX;
1267 RC = &X86::GR16RegClass;
1268 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001269 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1270 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1271 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001272 default: return false;
1273 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001274 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001275 CReg = X86::ECX;
1276 RC = &X86::GR32RegClass;
1277 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001278 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1279 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1280 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001281 default: return false;
1282 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001283 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001284 CReg = X86::RCX;
1285 RC = &X86::GR64RegClass;
1286 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001287 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1288 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1289 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001290 default: return false;
1291 }
1292 } else {
1293 return false;
1294 }
1295
Duncan Sands1440e8b2010-11-03 11:35:31 +00001296 MVT VT;
1297 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001298 return false;
1299
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001300 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1301 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001302
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001303 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1304 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001305 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1306 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001307
1308 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001309 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001310 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001311 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1312 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001313 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001314
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001315 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001316 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1317 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001318 UpdateValueMap(I, ResultReg);
1319 return true;
1320}
1321
Eli Bendersky50125482013-04-17 20:10:13 +00001322bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1323 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1324 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1325 const static bool S = true; // IsSigned
1326 const static bool U = false; // !IsSigned
1327 const static unsigned Copy = TargetOpcode::COPY;
1328 // For the X86 DIV/IDIV instruction, in most cases the dividend
1329 // (numerator) must be in a specific register pair highreg:lowreg,
1330 // producing the quotient in lowreg and the remainder in highreg.
1331 // For most data types, to set up the instruction, the dividend is
1332 // copied into lowreg, and lowreg is sign-extended or zero-extended
1333 // into highreg. The exception is i8, where the dividend is defined
1334 // as a single register rather than a register pair, and we
1335 // therefore directly sign-extend or zero-extend the dividend into
1336 // lowreg, instead of copying, and ignore the highreg.
1337 const static struct DivRemEntry {
1338 // The following portion depends only on the data type.
1339 const TargetRegisterClass *RC;
1340 unsigned LowInReg; // low part of the register pair
1341 unsigned HighInReg; // high part of the register pair
1342 // The following portion depends on both the data type and the operation.
1343 struct DivRemResult {
1344 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1345 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1346 // highreg, or copying a zero into highreg.
1347 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1348 // zero/sign-extending into lowreg for i8.
1349 unsigned DivRemResultReg; // Register containing the desired result.
1350 bool IsOpSigned; // Whether to use signed or unsigned form.
1351 } ResultTable[NumOps];
1352 } OpTable[NumTypes] = {
1353 { &X86::GR8RegClass, X86::AX, 0, {
1354 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1355 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1356 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1357 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1358 }
1359 }, // i8
1360 { &X86::GR16RegClass, X86::AX, X86::DX, {
1361 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1362 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover15983b82013-05-30 13:19:42 +00001363 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1364 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky50125482013-04-17 20:10:13 +00001365 }
1366 }, // i16
1367 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1368 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1369 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1370 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1371 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1372 }
1373 }, // i32
1374 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1375 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1376 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover15983b82013-05-30 13:19:42 +00001377 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1378 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky50125482013-04-17 20:10:13 +00001379 }
1380 }, // i64
1381 };
1382
1383 MVT VT;
1384 if (!isTypeLegal(I->getType(), VT))
1385 return false;
1386
1387 unsigned TypeIndex, OpIndex;
1388 switch (VT.SimpleTy) {
1389 default: return false;
1390 case MVT::i8: TypeIndex = 0; break;
1391 case MVT::i16: TypeIndex = 1; break;
1392 case MVT::i32: TypeIndex = 2; break;
1393 case MVT::i64: TypeIndex = 3;
1394 if (!Subtarget->is64Bit())
1395 return false;
1396 break;
1397 }
1398
1399 switch (I->getOpcode()) {
1400 default: llvm_unreachable("Unexpected div/rem opcode");
1401 case Instruction::SDiv: OpIndex = 0; break;
1402 case Instruction::SRem: OpIndex = 1; break;
1403 case Instruction::UDiv: OpIndex = 2; break;
1404 case Instruction::URem: OpIndex = 3; break;
1405 }
1406
1407 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1408 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1409 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1410 if (Op0Reg == 0)
1411 return false;
1412 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1413 if (Op1Reg == 0)
1414 return false;
1415
1416 // Move op0 into low-order input register.
1417 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1418 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1419 // Zero-extend or sign-extend into high-order input register.
1420 if (OpEntry.OpSignExtend) {
1421 if (OpEntry.IsOpSigned)
1422 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1423 TII.get(OpEntry.OpSignExtend));
Tim Northover15983b82013-05-30 13:19:42 +00001424 else {
1425 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Eli Bendersky50125482013-04-17 20:10:13 +00001426 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Tim Northover15983b82013-05-30 13:19:42 +00001427 TII.get(X86::MOV32r0), Zero32);
1428
1429 // Copy the zero into the appropriate sub/super/identical physical
1430 // register. Unfortunately the operations needed are not uniform enough to
1431 // fit neatly into the table above.
1432 if (VT.SimpleTy == MVT::i16) {
1433 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher3ce2a982013-06-11 23:41:41 +00001434 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover15983b82013-05-30 13:19:42 +00001435 .addReg(Zero32, 0, X86::sub_16bit);
1436 } else if (VT.SimpleTy == MVT::i32) {
1437 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher3ce2a982013-06-11 23:41:41 +00001438 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover15983b82013-05-30 13:19:42 +00001439 .addReg(Zero32);
1440 } else if (VT.SimpleTy == MVT::i64) {
1441 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1442 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1443 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1444 }
1445 }
Eli Bendersky50125482013-04-17 20:10:13 +00001446 }
1447 // Generate the DIV/IDIV instruction.
1448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1449 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachcc64dc62013-07-09 02:07:25 +00001450 // For i8 remainder, we can't reference AH directly, as we'll end
1451 // up with bogus copies like %R9B = COPY %AH. Reference AX
1452 // instead to prevent AH references in a REX instruction.
1453 //
1454 // The current assumption of the fast register allocator is that isel
1455 // won't generate explicit references to the GPR8_NOREX registers. If
1456 // the allocator and/or the backend get enhanced to be more robust in
1457 // that regard, this can be, and should be, removed.
1458 unsigned ResultReg = 0;
1459 if ((I->getOpcode() == Instruction::SRem ||
1460 I->getOpcode() == Instruction::URem) &&
1461 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1462 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1463 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1465 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1466
1467 // Shift AX right by 8 bits instead of using AH.
1468 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SHR16ri),
1469 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1470
1471 // Now reference the 8-bit subreg of the result.
1472 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1473 /*Kill=*/true, X86::sub_8bit);
1474 }
1475 // Copy the result out of the physreg if we haven't already.
1476 if (!ResultReg) {
1477 ResultReg = createResultReg(TypeEntry.RC);
1478 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Copy), ResultReg)
1479 .addReg(OpEntry.DivRemResultReg);
1480 }
Eli Bendersky50125482013-04-17 20:10:13 +00001481 UpdateValueMap(I, ResultReg);
1482
1483 return true;
1484}
1485
Dan Gohman46510a72010-04-15 01:51:59 +00001486bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001487 MVT VT;
1488 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001489 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001490
Eric Christophere487b012010-09-29 23:00:29 +00001491 // We only use cmov here, if we don't have a cmov instruction bail.
1492 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001493
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001494 unsigned Opc = 0;
1495 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001496 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001497 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001498 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001499 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001500 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001501 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001502 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001503 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001504 RC = &X86::GR64RegClass;
1505 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001506 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001507 }
1508
1509 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1510 if (Op0Reg == 0) return false;
1511 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1512 if (Op1Reg == 0) return false;
1513 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1514 if (Op2Reg == 0) return false;
1515
Dan Gohman84023e02010-07-10 09:00:22 +00001516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1517 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001518 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001519 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1520 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001521 UpdateValueMap(I, ResultReg);
1522 return true;
1523}
1524
Dan Gohman46510a72010-04-15 01:51:59 +00001525bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001526 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001527 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001528 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001529 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001530 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001531 unsigned OpReg = getRegForValue(V);
1532 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001533 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001534 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1535 TII.get(X86::CVTSS2SDrr), ResultReg)
1536 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001537 UpdateValueMap(I, ResultReg);
1538 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001539 }
1540 }
1541
1542 return false;
1543}
1544
Dan Gohman46510a72010-04-15 01:51:59 +00001545bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001546 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001547 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001548 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001549 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001550 unsigned OpReg = getRegForValue(V);
1551 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001552 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001553 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1554 TII.get(X86::CVTSD2SSrr), ResultReg)
1555 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001556 UpdateValueMap(I, ResultReg);
1557 return true;
1558 }
1559 }
1560 }
1561
1562 return false;
1563}
1564
Dan Gohman46510a72010-04-15 01:51:59 +00001565bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001566 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1567 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001568
Eli Friedman76927d732011-05-25 23:49:02 +00001569 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001570 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001571 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001572 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001573 return false;
1574
1575 unsigned InputReg = getRegForValue(I->getOperand(0));
1576 if (!InputReg)
1577 // Unhandled operand. Halt "fast" selection and bail.
1578 return false;
1579
Eli Friedman76927d732011-05-25 23:49:02 +00001580 if (SrcVT == MVT::i8) {
1581 // Truncate from i8 to i1; no code needed.
1582 UpdateValueMap(I, InputReg);
1583 return true;
1584 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001585
Eli Friedman76927d732011-05-25 23:49:02 +00001586 if (!Subtarget->is64Bit()) {
1587 // If we're on x86-32; we can't extract an i8 from a general register.
1588 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperc9099502012-04-20 06:31:50 +00001589 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1590 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1591 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedman76927d732011-05-25 23:49:02 +00001592 unsigned CopyReg = createResultReg(CopyRC);
1593 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1594 CopyReg).addReg(InputReg);
1595 InputReg = CopyReg;
1596 }
1597
1598 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001599 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001600 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001601 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001602 if (!ResultReg)
1603 return false;
1604
1605 UpdateValueMap(I, ResultReg);
1606 return true;
1607}
1608
Eli Friedmanc0883452011-05-20 22:21:04 +00001609bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1610 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1611}
1612
Eli Friedmand5089a92011-04-27 01:45:07 +00001613bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1614 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001615
Eli Friedmand5089a92011-04-27 01:45:07 +00001616 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001617 if (!IsMemcpySmall(Len))
1618 return false;
1619
1620 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001621
1622 // We don't care about alignment here since we just emit integer accesses.
1623 while (Len) {
1624 MVT VT;
1625 if (Len >= 8 && i64Legal)
1626 VT = MVT::i64;
1627 else if (Len >= 4)
1628 VT = MVT::i32;
1629 else if (Len >= 2)
1630 VT = MVT::i16;
1631 else {
Eli Friedmand5089a92011-04-27 01:45:07 +00001632 VT = MVT::i8;
1633 }
1634
1635 unsigned Reg;
1636 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1637 RV &= X86FastEmitStore(VT, Reg, DestAM);
1638 assert(RV && "Failed to emit load or store??");
1639
1640 unsigned Size = VT.getSizeInBits()/8;
1641 Len -= Size;
1642 DestAM.Disp += Size;
1643 SrcAM.Disp += Size;
1644 }
1645
1646 return true;
1647}
1648
Dan Gohman46510a72010-04-15 01:51:59 +00001649bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001650 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001651 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001652 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001653 case Intrinsic::memcpy: {
1654 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1655 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001656 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001657 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001658
Eli Friedman25255cb2011-06-10 23:39:36 +00001659 if (isa<ConstantInt>(MCI.getLength())) {
1660 // Small memcpy's are common enough that we want to do them
1661 // without a call if possible.
1662 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1663 if (IsMemcpySmall(Len)) {
1664 X86AddressMode DestAM, SrcAM;
1665 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1666 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1667 return false;
1668 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1669 return true;
1670 }
1671 }
Eric Christopher471e4222011-06-08 23:55:35 +00001672
Eli Friedman25255cb2011-06-10 23:39:36 +00001673 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1674 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001675 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001676
Eli Friedman25255cb2011-06-10 23:39:36 +00001677 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1678 return false;
1679
1680 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001681 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001682 case Intrinsic::memset: {
1683 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001684
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001685 if (MSI.isVolatile())
1686 return false;
1687
Eli Friedman25255cb2011-06-10 23:39:36 +00001688 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1689 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1690 return false;
1691
1692 if (MSI.getDestAddressSpace() > 255)
1693 return false;
1694
1695 return DoSelectCall(&I, "memset");
1696 }
Eric Christopher07754c22010-03-18 20:27:26 +00001697 case Intrinsic::stackprotector: {
Chad Rosiere1093e52012-05-11 19:43:29 +00001698 // Emit code to store the stack guard onto the stack.
Eric Christopher07754c22010-03-18 20:27:26 +00001699 EVT PtrTy = TLI.getPointerTy();
1700
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001701 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1702 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001703
1704 // Grab the frame index.
1705 X86AddressMode AM;
1706 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001707 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001708 return true;
1709 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001710 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001711 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001712 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001713 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001714 if (!X86SelectAddress(DI->getAddress(), AM))
1715 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001716 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001717 // FIXME may need to add RegState::Debug to any registers produced,
1718 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001719 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1720 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001721 return true;
1722 }
Eric Christopher77f79892010-01-18 22:11:29 +00001723 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001724 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001725 return true;
1726 }
Bill Wendling52370a12008-12-09 02:42:50 +00001727 case Intrinsic::sadd_with_overflow:
1728 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001729 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001730
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001731 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001732 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001733 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001734 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001735 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1736
Duncan Sands1440e8b2010-11-03 11:35:31 +00001737 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001738 if (!isTypeLegal(RetTy, VT))
1739 return false;
1740
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001741 const Value *Op1 = I.getArgOperand(0);
1742 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001743 unsigned Reg1 = getRegForValue(Op1);
1744 unsigned Reg2 = getRegForValue(Op2);
1745
1746 if (Reg1 == 0 || Reg2 == 0)
1747 // FIXME: Handle values *not* in registers.
1748 return false;
1749
1750 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001751 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001752 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001753 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001754 OpC = X86::ADD64rr;
1755 else
1756 return false;
1757
Eli Friedman482feb32011-05-16 21:06:17 +00001758 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001759 // both the returned values.
Eli Friedman482feb32011-05-16 21:06:17 +00001760 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001761 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1762 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001763
Chris Lattnera9a42252009-04-12 07:36:01 +00001764 unsigned Opc = X86::SETBr;
1765 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1766 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001767 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1768
1769 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001770 return true;
1771 }
1772 }
1773}
1774
Chad Rosierfd3417d2013-02-25 21:59:35 +00001775bool X86FastISel::FastLowerArguments() {
1776 if (!FuncInfo.CanLowerReturn)
1777 return false;
1778
1779 const Function *F = FuncInfo.Fn;
1780 if (F->isVarArg())
1781 return false;
1782
1783 CallingConv::ID CC = F->getCallingConv();
1784 if (CC != CallingConv::C)
1785 return false;
Charles Davisac226bb2013-07-12 06:02:35 +00001786
1787 if (Subtarget->isCallingConvWin64(CC))
1788 return false;
1789
Chad Rosierfd3417d2013-02-25 21:59:35 +00001790 if (!Subtarget->is64Bit())
1791 return false;
1792
1793 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1794 unsigned Idx = 1;
1795 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1796 I != E; ++I, ++Idx) {
1797 if (Idx > 6)
1798 return false;
1799
1800 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1801 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1802 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1803 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1804 return false;
1805
1806 Type *ArgTy = I->getType();
1807 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1808 return false;
1809
1810 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosierfe88aa02013-02-26 01:05:31 +00001811 if (!ArgVT.isSimple()) return false;
Chad Rosierfd3417d2013-02-25 21:59:35 +00001812 switch (ArgVT.getSimpleVT().SimpleTy) {
1813 case MVT::i32:
1814 case MVT::i64:
1815 break;
1816 default:
1817 return false;
1818 }
1819 }
1820
1821 static const uint16_t GPR32ArgRegs[] = {
1822 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1823 };
1824 static const uint16_t GPR64ArgRegs[] = {
1825 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1826 };
1827
1828 Idx = 0;
1829 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1830 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1831 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1832 I != E; ++I, ++Idx) {
Chad Rosierfd3417d2013-02-25 21:59:35 +00001833 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1834 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1835 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1836 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1837 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1838 // Without this, EmitLiveInCopies may eliminate the livein if its only
1839 // use is a bitcast (which isn't turned into an instruction).
1840 unsigned ResultReg = createResultReg(RC);
1841 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1842 ResultReg).addReg(DstReg, getKillRegState(true));
1843 UpdateValueMap(I, ResultReg);
1844 }
1845 return true;
1846}
1847
Dan Gohman46510a72010-04-15 01:51:59 +00001848bool X86FastISel::X86SelectCall(const Instruction *I) {
1849 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001850 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001851
1852 // Can't handle inline asm yet.
1853 if (isa<InlineAsm>(Callee))
1854 return false;
1855
Bill Wendling52370a12008-12-09 02:42:50 +00001856 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001857 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001858 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001859
Chad Rosier425e9512012-12-11 00:18:02 +00001860 // Allow SelectionDAG isel to handle tail calls.
1861 if (cast<CallInst>(I)->isTailCall())
1862 return false;
1863
Eli Friedman25255cb2011-06-10 23:39:36 +00001864 return DoSelectCall(I, 0);
1865}
1866
Rafael Espindolac338fe02012-07-25 15:42:45 +00001867static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1868 const ImmutableCallSite &CS) {
Rafael Espindola742f2c92012-07-25 13:35:45 +00001869 if (Subtarget.is64Bit())
1870 return 0;
1871 if (Subtarget.isTargetWindows())
1872 return 0;
1873 CallingConv::ID CC = CS.getCallingConv();
1874 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1875 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001876 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola742f2c92012-07-25 13:35:45 +00001877 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001878 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola1cee7102012-07-25 13:41:10 +00001879 return 0;
Rafael Espindola742f2c92012-07-25 13:35:45 +00001880 return 4;
1881}
1882
Eli Friedman25255cb2011-06-10 23:39:36 +00001883// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1884bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1885 const CallInst *CI = cast<CallInst>(I);
1886 const Value *Callee = CI->getCalledValue();
1887
Evan Chengf3d4efe2008-09-07 09:09:33 +00001888 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001889 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001890 CallingConv::ID CC = CS.getCallingConv();
Charles Davisac226bb2013-07-12 06:02:35 +00001891 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001892 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davisac226bb2013-07-12 06:02:35 +00001893 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
1894 CC != CallingConv::X86_64_SysV)
Evan Chengf3d4efe2008-09-07 09:09:33 +00001895 return false;
1896
Evan Cheng381993f2010-01-27 00:00:57 +00001897 // fastcc with -tailcallopt is intended to provide a guaranteed
1898 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001899 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001900 return false;
1901
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001902 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1903 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001904 bool isVarArg = FTy->isVarArg();
1905
1906 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1907 // x86-32. Special handling for x86-64 is implemented.
Charles Davisac226bb2013-07-12 06:02:35 +00001908 if (isVarArg && isWin64)
Evan Chengf3d4efe2008-09-07 09:09:33 +00001909 return false;
1910
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001911 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001912 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001913 TM.Options.GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001914 return false;
1915
Eli Friedman19515b42011-05-17 18:29:03 +00001916 // Check whether the function can return without sret-demotion.
1917 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001918 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman19515b42011-05-17 18:29:03 +00001919 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001920 *FuncInfo.MF, FTy->isVarArg(),
1921 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001922 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001923 return false;
1924
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001925 // Materialize callee address in a register. FIXME: GV address can be
1926 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001927 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001928 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001929 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001930 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001931 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001932 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001933 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001934 } else if (CalleeAM.Base.Reg != 0) {
1935 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001936 } else
1937 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001938
Evan Chengf3d4efe2008-09-07 09:09:33 +00001939 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001940 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001941 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001942 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001943 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosier15b44972012-02-15 00:36:26 +00001944 unsigned arg_size = CS.arg_size();
1945 Args.reserve(arg_size);
1946 ArgVals.reserve(arg_size);
1947 ArgVTs.reserve(arg_size);
1948 ArgFlags.reserve(arg_size);
Dan Gohman46510a72010-04-15 01:51:59 +00001949 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001950 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001951 // If we're lowering a mem intrinsic instead of a regular call, skip the
1952 // last two arguments, which should not passed to the underlying functions.
1953 if (MemIntName && e-i <= 2)
1954 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001955 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001956 ISD::ArgFlagsTy Flags;
1957 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling034b94b2012-12-19 07:18:57 +00001958 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001959 Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00001960 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001961 Flags.setZExt();
1962
Bill Wendling034b94b2012-12-19 07:18:57 +00001963 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001964 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1965 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001966 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1967 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1968 if (!FrameAlign)
1969 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1970 Flags.setByVal();
1971 Flags.setByValSize(FrameSize);
1972 Flags.setByValAlign(FrameAlign);
1973 if (!IsMemcpySmall(FrameSize))
1974 return false;
1975 }
1976
Bill Wendling034b94b2012-12-19 07:18:57 +00001977 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedmanc0883452011-05-20 22:21:04 +00001978 Flags.setInReg();
Bill Wendling034b94b2012-12-19 07:18:57 +00001979 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedmanc0883452011-05-20 22:21:04 +00001980 Flags.setNest();
1981
Chris Lattnere03b8d32011-04-19 04:42:38 +00001982 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1983 // instruction. This is safe because it is common to all fastisel supported
1984 // calling conventions on x86.
1985 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1986 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1987 CI->getBitWidth() == 16) {
1988 if (Flags.isSExt())
1989 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1990 else
1991 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1992 }
1993 }
Eric Christopher471e4222011-06-08 23:55:35 +00001994
Chris Lattnerb44101c2011-04-19 05:09:50 +00001995 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001996
Chris Lattnerff009ad2011-04-19 05:15:59 +00001997 // Passing bools around ends up doing a trunc to i1 and passing it.
1998 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001999 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2000 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2001 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00002002 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2003 ArgReg = getRegForValue(ArgVal);
2004 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00002005
Chris Lattnerb44101c2011-04-19 05:09:50 +00002006 MVT ArgVT;
2007 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00002008
Chris Lattnerb44101c2011-04-19 05:09:50 +00002009 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2010 ArgVal->hasOneUse(), 1);
2011 } else {
2012 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00002013 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00002014
Chris Lattnerff009ad2011-04-19 05:15:59 +00002015 if (ArgReg == 0) return false;
2016
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002017 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002018 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00002019 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00002020 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002021 if (ArgVT == MVT::x86mmx)
2022 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00002023 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2024 Flags.setOrigAlign(OriginalAlignment);
2025
Chris Lattnerb44101c2011-04-19 05:09:50 +00002026 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00002027 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002028 ArgVTs.push_back(ArgVT);
2029 ArgFlags.push_back(Flags);
2030 }
2031
2032 // Analyze operands of the call, assigning locations to each operand.
2033 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002034 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00002035 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002036
Dan Gohmand8acddd2010-06-01 21:09:47 +00002037 // Allocate shadow area for Win64
Charles Davisac226bb2013-07-12 06:02:35 +00002038 if (isWin64)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002039 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00002040
Duncan Sands45907662010-10-31 13:21:44 +00002041 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002042
2043 // Get a count of how many bytes are to be pushed on the stack.
2044 unsigned NumBytes = CCInfo.getNextStackOffset();
2045
2046 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00002047 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00002048 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
2049 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002050
Chris Lattner438949a2008-10-15 05:30:52 +00002051 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00002052 // copies / loads.
2053 SmallVector<unsigned, 4> RegArgs;
2054 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2055 CCValAssign &VA = ArgLocs[i];
2056 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00002057 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002058
Evan Chengf3d4efe2008-09-07 09:09:33 +00002059 // Promote the value if needed.
2060 switch (VA.getLocInfo()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00002061 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00002062 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00002063 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2064 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00002065 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2066 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00002067 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00002068 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00002069 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00002070 }
2071 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00002072 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2073 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00002074 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2075 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00002076 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00002077 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00002078 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00002079 }
2080 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00002081 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2082 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00002083 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2084 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00002085 if (!Emitted)
2086 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00002087 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00002088 if (!Emitted)
2089 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2090 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002091
Chris Lattnerc46ec642011-01-05 22:26:52 +00002092 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00002093 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00002094 break;
2095 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00002096 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002097 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002098 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00002099 assert(BC != 0 && "Failed to emit a bitcast!");
2100 Arg = BC;
2101 ArgVT = VA.getLocVT();
2102 break;
2103 }
Chad Rosier36ec0ca2012-07-11 19:58:38 +00002104 case CCValAssign::VExt:
2105 // VExt has not been implemented, so this should be impossible to reach
2106 // for now. However, fallback to Selection DAG isel once implemented.
2107 return false;
2108 case CCValAssign::Indirect:
2109 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2110 // support this.
2111 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00002112 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002113
Evan Chengf3d4efe2008-09-07 09:09:33 +00002114 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002115 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2116 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002117 RegArgs.push_back(VA.getLocReg());
2118 } else {
2119 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00002120 X86AddressMode AM;
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00002121 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2122 getTargetMachine()->getRegisterInfo());
Michael Liaof0e06e82012-11-01 03:47:50 +00002123 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman0586d912008-09-10 20:11:02 +00002124 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00002125 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00002126 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002127
Eli Friedmanc0883452011-05-20 22:21:04 +00002128 if (Flags.isByVal()) {
2129 X86AddressMode SrcAM;
2130 SrcAM.Base.Reg = Arg;
2131 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2132 assert(Res && "memcpy length already checked!"); (void)Res;
2133 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2134 // If this is a really simple value, emit this with the Value* version
Nick Lewycky1f9c6862011-10-12 00:14:12 +00002135 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedmanc0883452011-05-20 22:21:04 +00002136 // as it can cause us to reevaluate the argument.
Lang Hamese4824712011-10-18 22:11:33 +00002137 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2138 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002139 } else {
Lang Hamese4824712011-10-18 22:11:33 +00002140 if (!X86FastEmitStore(ArgVT, Arg, AM))
2141 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002142 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00002143 }
2144 }
2145
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002146 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002147 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00002148 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00002149 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002150 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2151 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002152 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002153
Charles Davisac226bb2013-07-12 06:02:35 +00002154 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedman37620462011-04-19 17:22:22 +00002155 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002156 static const uint16_t XMMArgRegs[] = {
Eli Friedman37620462011-04-19 17:22:22 +00002157 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2158 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2159 };
2160 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2161 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2162 X86::AL).addImm(NumXMMRegs);
2163 }
2164
Evan Chengf3d4efe2008-09-07 09:09:33 +00002165 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00002166 MachineInstrBuilder MIB;
2167 if (CalleeOp) {
2168 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00002169 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002170 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002171 CallOpc = X86::CALL64r;
2172 else
2173 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00002174 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2175 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002176
Chris Lattner51e8eab2009-07-09 06:34:26 +00002177 } else {
2178 // Direct call.
2179 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00002180 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002181 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002182 CallOpc = X86::CALL64pcrel32;
2183 else
2184 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002185
Chris Lattner51e8eab2009-07-09 06:34:26 +00002186 // See if we need any target-specific flags on the GV operand.
2187 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002188
Chris Lattner51e8eab2009-07-09 06:34:26 +00002189 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2190 // external symbols most go through the PLT in PIC mode. If the symbol
2191 // has hidden or protected visibility, or if it is static or local, then
2192 // we don't need to use the PLT - we can directly call it.
2193 if (Subtarget->isTargetELF() &&
2194 TM.getRelocationModel() == Reloc::PIC_ &&
2195 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2196 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002197 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00002198 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002199 (!Subtarget->getTargetTriple().isMacOSX() ||
2200 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00002201 // PC-relative references to external symbols should go through $stub,
2202 // unless we're building with the leopard linker or later, which
2203 // automatically synthesizes these stubs.
2204 OpFlags = X86II::MO_DARWIN_STUB;
2205 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002206
2207
Eli Friedman25255cb2011-06-10 23:39:36 +00002208 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2209 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00002210 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00002211 else
2212 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00002213 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002214
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002215 // Add a register mask with the call-preserved registers.
2216 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2217 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2218
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00002219 // Add an implicit use GOT pointer in EBX.
2220 if (Subtarget->isPICStyleGOT())
2221 MIB.addReg(X86::EBX, RegState::Implicit);
2222
Charles Davisac226bb2013-07-12 06:02:35 +00002223 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00002224 MIB.addReg(X86::AL, RegState::Implicit);
2225
2226 // Add implicit physical register uses to the call.
2227 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2228 MIB.addReg(RegArgs[i], RegState::Implicit);
2229
Evan Chengf3d4efe2008-09-07 09:09:33 +00002230 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00002231 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolac338fe02012-07-25 15:42:45 +00002232 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohman84023e02010-07-10 09:00:22 +00002233 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00002234 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002235
Eli Friedman19515b42011-05-17 18:29:03 +00002236 // Build info for return calling conv lowering code.
2237 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2238 SmallVector<ISD::InputArg, 32> Ins;
2239 SmallVector<EVT, 4> RetTys;
2240 ComputeValueVTs(TLI, I->getType(), RetTys);
2241 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2242 EVT VT = RetTys[i];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002243 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman19515b42011-05-17 18:29:03 +00002244 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2245 for (unsigned j = 0; j != NumRegs; ++j) {
2246 ISD::InputArg MyFlags;
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002247 MyFlags.VT = RegisterVT;
Eli Friedman19515b42011-05-17 18:29:03 +00002248 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling034b94b2012-12-19 07:18:57 +00002249 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002250 MyFlags.Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002251 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002252 MyFlags.Flags.setZExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002253 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman19515b42011-05-17 18:29:03 +00002254 MyFlags.Flags.setInReg();
2255 Ins.push_back(MyFlags);
2256 }
2257 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002258
Eli Friedman19515b42011-05-17 18:29:03 +00002259 // Now handle call return values.
2260 SmallVector<unsigned, 4> UsedRegs;
2261 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002262 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00002263 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00002264 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2265 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2266 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2267 EVT CopyVT = RVLocs[i].getValVT();
2268 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002269
Evan Chengf3d4efe2008-09-07 09:09:33 +00002270 // If this is a call to a function that returns an fp value on the x87 fp
2271 // stack, but where we prefer to use the value in xmm registers, copy it
2272 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00002273 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002274 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002275 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002276 CopyVT = MVT::f80;
Craig Topperc9099502012-04-20 06:31:50 +00002277 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002278 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002279 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2280 CopyReg);
2281 } else {
2282 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2283 CopyReg).addReg(RVLocs[i].getLocReg());
2284 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00002285 }
2286
Eli Friedman19515b42011-05-17 18:29:03 +00002287 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00002288 // Round the F80 the right size, which also moves to the appropriate xmm
2289 // register. This is accomplished by storing the F80 value in memory and
2290 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00002291 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00002293 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002294 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00002295 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2296 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00002297 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002298 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00002299 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00002300 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002301 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002302 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00002303
Eli Friedman19515b42011-05-17 18:29:03 +00002304 if (RVLocs.size())
2305 UpdateValueMap(I, ResultReg, RVLocs.size());
2306
Dan Gohmandb497122010-06-18 23:28:01 +00002307 // Set all unused physreg defs as dead.
2308 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2309
Evan Chengf3d4efe2008-09-07 09:09:33 +00002310 return true;
2311}
2312
2313
Dan Gohman99b21822008-08-28 23:21:34 +00002314bool
Dan Gohman46510a72010-04-15 01:51:59 +00002315X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00002316 switch (I->getOpcode()) {
2317 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00002318 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00002319 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00002320 case Instruction::Store:
2321 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00002322 case Instruction::Ret:
2323 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00002324 case Instruction::ICmp:
2325 case Instruction::FCmp:
2326 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00002327 case Instruction::ZExt:
2328 return X86SelectZExt(I);
2329 case Instruction::Br:
2330 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002331 case Instruction::Call:
2332 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002333 case Instruction::LShr:
2334 case Instruction::AShr:
2335 case Instruction::Shl:
2336 return X86SelectShift(I);
Eli Bendersky50125482013-04-17 20:10:13 +00002337 case Instruction::SDiv:
2338 case Instruction::UDiv:
2339 case Instruction::SRem:
2340 case Instruction::URem:
2341 return X86SelectDivRem(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002342 case Instruction::Select:
2343 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00002344 case Instruction::Trunc:
2345 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00002346 case Instruction::FPExt:
2347 return X86SelectFPExt(I);
2348 case Instruction::FPTrunc:
2349 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00002350 case Instruction::IntToPtr: // Deliberate fall-through.
2351 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00002352 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2353 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00002354 if (DstVT.bitsGT(SrcVT))
2355 return X86SelectZExt(I);
2356 if (DstVT.bitsLT(SrcVT))
2357 return X86SelectTrunc(I);
2358 unsigned Reg = getRegForValue(I->getOperand(0));
2359 if (Reg == 0) return false;
2360 UpdateValueMap(I, Reg);
2361 return true;
2362 }
Dan Gohman99b21822008-08-28 23:21:34 +00002363 }
2364
2365 return false;
2366}
2367
Dan Gohman46510a72010-04-15 01:51:59 +00002368unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002369 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00002370 if (!isTypeLegal(C->getType(), VT))
Michael Liaofaa11592012-08-30 00:30:16 +00002371 return 0;
2372
2373 // Can't handle alternate code models yet.
2374 if (TM.getCodeModel() != CodeModel::Small)
2375 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002376
Owen Anderson95267a12008-09-05 00:06:23 +00002377 // Get opcode and regclass of the output for the given load instruction.
2378 unsigned Opc = 0;
2379 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002380 switch (VT.SimpleTy) {
Michael Liaofaa11592012-08-30 00:30:16 +00002381 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002382 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00002383 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +00002384 RC = &X86::GR8RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002385 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002386 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00002387 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +00002388 RC = &X86::GR16RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002389 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002390 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00002391 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +00002392 RC = &X86::GR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002393 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002394 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00002395 // Must be in x86-64 mode.
2396 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +00002397 RC = &X86::GR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002398 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002399 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002400 if (X86ScalarSSEf32) {
2401 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +00002402 RC = &X86::FR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002403 } else {
2404 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +00002405 RC = &X86::RFP32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002406 }
2407 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002408 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002409 if (X86ScalarSSEf64) {
2410 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +00002411 RC = &X86::FR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002412 } else {
2413 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +00002414 RC = &X86::RFP64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002415 }
2416 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002417 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002418 // No f80 support yet.
Michael Liaofaa11592012-08-30 00:30:16 +00002419 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002420 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002421
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002422 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002423 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002424 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002425 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002426 // If the expression is just a basereg, then we're done, otherwise we need
2427 // to emit an LEA.
2428 if (AM.BaseType == X86AddressMode::RegBase &&
2429 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2430 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002431
Chris Lattner685090f2011-04-17 17:12:08 +00002432 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002433 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002434 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2435 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002436 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002437 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002438 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002439 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002440
Owen Anderson3b217c62008-09-06 01:11:01 +00002441 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002442 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002443 if (Align == 0) {
2444 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002445 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002446 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002447
Dan Gohman5396c992008-09-30 01:21:32 +00002448 // x86-32 PIC requires a PIC base register for constant pools.
2449 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002450 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002451 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002452 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002453 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002454 } else if (Subtarget->isPICStyleGOT()) {
2455 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002456 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002457 } else if (Subtarget->isPICStyleRIPRel() &&
2458 TM.getCodeModel() == CodeModel::Small) {
2459 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002460 }
Dan Gohman5396c992008-09-30 01:21:32 +00002461
2462 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002463 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002464 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002465 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2466 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002467 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002468
Owen Anderson95267a12008-09-05 00:06:23 +00002469 return ResultReg;
2470}
2471
Dan Gohman46510a72010-04-15 01:51:59 +00002472unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002473 // Fail on dynamic allocas. At this point, getRegForValue has already
2474 // checked its CSE maps, so if we're here trying to handle a dynamic
2475 // alloca, we're not going to succeed. X86SelectAddress has a
2476 // check for dynamic allocas, because it's called directly from
2477 // various places, but TargetMaterializeAlloca also needs a check
2478 // in order to avoid recursion between getRegForValue,
2479 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002480 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002481 return 0;
2482
Dan Gohman0586d912008-09-10 20:11:02 +00002483 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002484 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002485 return 0;
2486 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper44d23822012-02-22 05:59:10 +00002487 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman0586d912008-09-10 20:11:02 +00002488 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002489 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2490 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002491 return ResultReg;
2492}
2493
Eli Friedman2790ba82011-04-27 22:41:55 +00002494unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2495 MVT VT;
2496 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002497 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002498
2499 // Get opcode and regclass for the given zero.
2500 unsigned Opc = 0;
2501 const TargetRegisterClass *RC = NULL;
2502 switch (VT.SimpleTy) {
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002503 default: return 0;
Craig Topperf4cfc442012-08-11 17:53:00 +00002504 case MVT::f32:
2505 if (X86ScalarSSEf32) {
2506 Opc = X86::FsFLD0SS;
2507 RC = &X86::FR32RegClass;
2508 } else {
2509 Opc = X86::LD_Fp032;
2510 RC = &X86::RFP32RegClass;
2511 }
2512 break;
2513 case MVT::f64:
2514 if (X86ScalarSSEf64) {
2515 Opc = X86::FsFLD0SD;
2516 RC = &X86::FR64RegClass;
2517 } else {
2518 Opc = X86::LD_Fp064;
2519 RC = &X86::RFP64RegClass;
2520 }
2521 break;
2522 case MVT::f80:
2523 // No f80 support yet.
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002524 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002525 }
2526
2527 unsigned ResultReg = createResultReg(RC);
2528 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2529 return ResultReg;
2530}
2531
2532
Eli Bendersky75299e32013-04-19 22:29:18 +00002533bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2534 const LoadInst *LI) {
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002535 X86AddressMode AM;
2536 if (!X86SelectAddress(LI->getOperand(0), AM))
2537 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002538
Craig Topperdca72542012-08-11 17:46:16 +00002539 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002540
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002541 unsigned Size = TD.getTypeAllocSize(LI->getType());
2542 unsigned Alignment = LI->getAlignment();
2543
2544 SmallVector<MachineOperand, 8> AddrOps;
2545 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002546
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002547 MachineInstr *Result =
2548 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2549 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002550
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002551 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002552 MI->eraseFromParent();
2553 return true;
2554}
2555
2556
Evan Chengc3f44b02008-09-03 00:03:49 +00002557namespace llvm {
Bob Wilsond49edb72012-08-03 04:06:28 +00002558 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2559 const TargetLibraryInfo *libInfo) {
2560 return new X86FastISel(funcInfo, libInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002561 }
Dan Gohman99b21822008-08-28 23:21:34 +00002562}