blob: 83f2f6a202bc3f820d92edede504275b5bff4aaa [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"
Evan Cheng8b19e562008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Evan Chengef41ff62011-06-23 17:54:54 +000018#include "X86ISelLowering.h"
Evan Cheng88e30412008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000022#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000023#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000024#include "llvm/GlobalVariable.h"
Eli Friedmana6176ad2011-09-22 23:41:28 +000025#include "llvm/GlobalAlias.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000026#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000027#include "llvm/IntrinsicInst.h"
Jay Foad562b84b2011-04-11 09:35:34 +000028#include "llvm/Operator.h"
Dan Gohman84023e02010-07-10 09:00:22 +000029#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000030#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000031#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000032#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000033#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000035#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000037#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000038#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000039using namespace llvm;
40
Chris Lattner087fcf32009-03-08 18:44:31 +000041namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000042
Evan Chengc3f44b02008-09-03 00:03:49 +000043class X86FastISel : public FastISel {
44 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
45 /// make the right decision when generating code for different targets.
46 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000047
48 /// StackPtr - Register used as the stack pointer.
49 ///
50 unsigned StackPtr;
51
Wesley Peckbf17cfa2010-11-23 03:31:01 +000052 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000053 /// floating point ops.
54 /// When SSE is available, use it for f32 operations.
55 /// When SSE2 is available, use it for f64 operations.
56 bool X86ScalarSSEf64;
57 bool X86ScalarSSEf32;
58
Evan Cheng8b19e562008-09-03 06:44:39 +000059public:
Bob Wilsond49edb72012-08-03 04:06:28 +000060 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
61 const TargetLibraryInfo *libInfo)
62 : FastISel(funcInfo, libInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000063 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000064 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Craig Topper1accb7e2012-01-10 06:54:16 +000065 X86ScalarSSEf64 = Subtarget->hasSSE2();
66 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000067 }
Evan Chengc3f44b02008-09-03 00:03:49 +000068
Dan Gohman46510a72010-04-15 01:51:59 +000069 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000070
Chris Lattnerbeac75d2010-09-05 02:18:34 +000071 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
72 /// vreg is being provided by the specified load instruction. If possible,
73 /// try to fold the load as an operand to the instruction, returning true if
74 /// possible.
75 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
76 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000077
Dan Gohman1adf1b02008-08-19 21:45:35 +000078#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000079
80private:
Dan Gohman46510a72010-04-15 01:51:59 +000081 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000082
Owen Andersone50ed302009-08-10 22:56:29 +000083 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000084
Chris Lattnerb44101c2011-04-19 05:09:50 +000085 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
86 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
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
Dan Gohman46510a72010-04-15 01:51:59 +0000108 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000109
Dan Gohman46510a72010-04-15 01:51:59 +0000110 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000111
Dan Gohman46510a72010-04-15 01:51:59 +0000112 bool X86SelectFPExt(const Instruction *I);
113 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000114
Dan Gohman46510a72010-04-15 01:51:59 +0000115 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
116 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000117
Eli Friedman25255cb2011-06-10 23:39:36 +0000118 bool DoSelectCall(const Instruction *I, const char *MemIntName);
119
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000120 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000121 return getTargetMachine()->getInstrInfo();
122 }
123 const X86TargetMachine *getTargetMachine() const {
124 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000125 }
126
Dan Gohman46510a72010-04-15 01:51:59 +0000127 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000128
Dan Gohman46510a72010-04-15 01:51:59 +0000129 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000130
Eli Friedman2790ba82011-04-27 22:41:55 +0000131 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
132
Evan Chengf3d4efe2008-09-07 09:09:33 +0000133 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
134 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000135 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
137 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000138 }
139
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000140 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000141
Eli Friedmanc0883452011-05-20 22:21:04 +0000142 bool IsMemcpySmall(uint64_t Len);
143
Eli Friedmand5089a92011-04-27 01:45:07 +0000144 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
145 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000146};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000147
Chris Lattner087fcf32009-03-08 18:44:31 +0000148} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000149
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000150bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000151 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
152 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000153 // Unhandled type. Halt "fast" selection and bail.
154 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000155
156 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000157 // For now, require SSE/SSE2 for performing floating-point operations,
158 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topperf4cfc442012-08-11 17:53:00 +0000160 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topperf4cfc442012-08-11 17:53:00 +0000162 return false;
Dan Gohman9b66d732008-09-30 00:48:39 +0000163 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000165 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000166 // We only handle legal types. For example, on x86-32 the instruction
167 // selector contains all of the 64-bit instructions from x86-64,
168 // under the assumption that i64 won't be used if the target doesn't
169 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000171}
172
173#include "X86GenCallingConv.inc"
174
Evan Cheng0de588f2008-09-05 21:00:03 +0000175/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000176/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000177/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000178bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000179 unsigned &ResultReg) {
180 // Get opcode and regclass of the output for the given load instruction.
181 unsigned Opc = 0;
182 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000184 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000185 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000187 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +0000188 RC = &X86::GR8RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000189 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000191 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +0000192 RC = &X86::GR16RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000195 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000196 RC = &X86::GR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000199 // Must be in x86-64 mode.
200 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000201 RC = &X86::GR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000202 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000204 if (X86ScalarSSEf32) {
205 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +0000206 RC = &X86::FR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000207 } else {
208 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +0000209 RC = &X86::RFP32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000210 }
211 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000213 if (X86ScalarSSEf64) {
214 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +0000215 RC = &X86::FR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000216 } else {
217 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +0000218 RC = &X86::RFP64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000219 }
220 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000222 // No f80 support yet.
223 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000224 }
225
226 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000227 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
228 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000229 return true;
230}
231
Evan Chengf3d4efe2008-09-07 09:09:33 +0000232/// X86FastEmitStore - Emit a machine instruction to store a value Val of
233/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
234/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000235/// i.e. V. Return true if it is possible.
236bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000237X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000238 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000239 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 switch (VT.getSimpleVT().SimpleTy) {
241 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000242 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000243 case MVT::i1: {
244 // Mask out all but lowest bit.
Craig Topperc9099502012-04-20 06:31:50 +0000245 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000247 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
248 Val = AndResult;
249 }
250 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 case MVT::i8: Opc = X86::MOV8mr; break;
252 case MVT::i16: Opc = X86::MOV16mr; break;
253 case MVT::i32: Opc = X86::MOV32mr; break;
254 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
255 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000256 Opc = X86ScalarSSEf32 ?
257 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000258 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000260 Opc = X86ScalarSSEf64 ?
261 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000262 break;
Lang Hamese4824712011-10-18 22:11:33 +0000263 case MVT::v4f32:
264 Opc = X86::MOVAPSmr;
265 break;
266 case MVT::v2f64:
267 Opc = X86::MOVAPDmr;
268 break;
269 case MVT::v4i32:
270 case MVT::v2i64:
271 case MVT::v8i16:
272 case MVT::v16i8:
273 Opc = X86::MOVDQAmr;
274 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000275 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000276
Dan Gohman84023e02010-07-10 09:00:22 +0000277 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
278 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000279 return true;
280}
281
Dan Gohman46510a72010-04-15 01:51:59 +0000282bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000283 const X86AddressMode &AM) {
284 // Handle 'null' like i32/i64 0.
285 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000286 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000287
Chris Lattner438949a2008-10-15 05:30:52 +0000288 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000289 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000290 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000291 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000293 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000294 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 case MVT::i8: Opc = X86::MOV8mi; break;
296 case MVT::i16: Opc = X86::MOV16mi; break;
297 case MVT::i32: Opc = X86::MOV32mi; break;
298 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000299 // Must be a 32-bit sign extended value.
300 if ((int)CI->getSExtValue() == CI->getSExtValue())
301 Opc = X86::MOV64mi32;
302 break;
303 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000304
Chris Lattner438949a2008-10-15 05:30:52 +0000305 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000306 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
307 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000308 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000309 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000310 return true;
311 }
312 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000313
Chris Lattner438949a2008-10-15 05:30:52 +0000314 unsigned ValReg = getRegForValue(Val);
315 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000316 return false;
317
Chris Lattner438949a2008-10-15 05:30:52 +0000318 return X86FastEmitStore(VT, ValReg, AM);
319}
320
Evan Cheng24e3a902008-09-08 06:35:17 +0000321/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
322/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
323/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000324bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
325 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000326 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000327 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
328 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000329
Owen Andersonac34a002008-09-11 19:44:55 +0000330 if (RR != 0) {
331 ResultReg = RR;
332 return true;
333 } else
334 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000335}
336
Dan Gohman0586d912008-09-10 20:11:02 +0000337/// X86SelectAddress - Attempt to fill in an address from the given value.
338///
Dan Gohman46510a72010-04-15 01:51:59 +0000339bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
340 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000341 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000342 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000343 // Don't walk into other basic blocks; it's possible we haven't
344 // visited them yet, so the instructions may not yet be assigned
345 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000346 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
347 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
348 Opcode = I->getOpcode();
349 U = I;
350 }
Dan Gohman46510a72010-04-15 01:51:59 +0000351 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000352 Opcode = C->getOpcode();
353 U = C;
354 }
Dan Gohman0586d912008-09-10 20:11:02 +0000355
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000356 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000357 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000358 // Fast instruction selection doesn't support the special
359 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000360 return false;
361
Dan Gohman35893082008-09-18 23:23:44 +0000362 switch (Opcode) {
363 default: break;
364 case Instruction::BitCast:
365 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000366 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000367
368 case Instruction::IntToPtr:
369 // Look past no-op inttoptrs.
370 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000371 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000372 break;
Dan Gohman35893082008-09-18 23:23:44 +0000373
374 case Instruction::PtrToInt:
375 // Look past no-op ptrtoints.
376 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000377 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000378 break;
Dan Gohman35893082008-09-18 23:23:44 +0000379
380 case Instruction::Alloca: {
381 // Do static allocas.
382 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000383 DenseMap<const AllocaInst*, int>::iterator SI =
384 FuncInfo.StaticAllocaMap.find(A);
385 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000386 AM.BaseType = X86AddressMode::FrameIndexBase;
387 AM.Base.FrameIndex = SI->second;
388 return true;
389 }
390 break;
Dan Gohman35893082008-09-18 23:23:44 +0000391 }
392
393 case Instruction::Add: {
394 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000395 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000396 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
397 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000398 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000399 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000400 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000401 }
Dan Gohman0586d912008-09-10 20:11:02 +0000402 }
Dan Gohman35893082008-09-18 23:23:44 +0000403 break;
404 }
405
406 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000407 X86AddressMode SavedAM = AM;
408
Dan Gohman35893082008-09-18 23:23:44 +0000409 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000410 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000411 unsigned IndexReg = AM.IndexReg;
412 unsigned Scale = AM.Scale;
413 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000414 // Iterate through the indices, folding what we can. Constants can be
415 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000416 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000417 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000418 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000419 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000420 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000421 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
422 continue;
423 }
Eric Christopher471e4222011-06-08 23:55:35 +0000424
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000425 // A array/variable index is always of the form i*S where S is the
426 // constant scale size. See if we can push the scale into immediates.
427 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
428 for (;;) {
429 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
430 // Constant-offset addressing.
431 Disp += CI->getSExtValue() * S;
432 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000433 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000434 if (isa<AddOperator>(Op) &&
435 (!isa<Instruction>(Op) ||
436 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
437 == FuncInfo.MBB) &&
438 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
439 // An add (in the same block) with a constant operand. Fold the
440 // constant.
441 ConstantInt *CI =
442 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
443 Disp += CI->getSExtValue() * S;
444 // Iterate on the other operand.
445 Op = cast<AddOperator>(Op)->getOperand(0);
446 continue;
447 }
448 if (IndexReg == 0 &&
449 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
450 (S == 1 || S == 2 || S == 4 || S == 8)) {
451 // Scaled-index addressing.
452 Scale = S;
453 IndexReg = getRegForGEPIndex(Op).first;
454 if (IndexReg == 0)
455 return false;
456 break;
457 }
458 // Unsupported.
459 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000460 }
461 }
Dan Gohman09aae462008-09-26 20:04:15 +0000462 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000463 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000464 break;
Dan Gohman35893082008-09-18 23:23:44 +0000465 // Ok, the GEP indices were covered by constant-offset and scaled-index
466 // addressing. Update the address state and move on to examining the base.
467 AM.IndexReg = IndexReg;
468 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000469 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000470 if (X86SelectAddress(U->getOperand(0), AM))
471 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000472
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000473 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000474 // our address and just match the value instead of completely failing.
475 AM = SavedAM;
476 break;
Dan Gohman35893082008-09-18 23:23:44 +0000477 unsupported_gep:
478 // Ok, the GEP indices weren't all covered.
479 break;
480 }
481 }
482
483 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000484 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedmana6176ad2011-09-22 23:41:28 +0000485 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000486 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000487 return false;
488
Eli Friedmana6176ad2011-09-22 23:41:28 +0000489 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000490 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000491 if (GVar->isThreadLocal())
492 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000493
Eli Friedmana6176ad2011-09-22 23:41:28 +0000494 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
495 // it works...).
496 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
497 if (const GlobalVariable *GVar =
498 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
499 if (GVar->isThreadLocal())
500 return false;
501
Chris Lattner0a1c9972011-04-17 17:47:38 +0000502 // RIP-relative addresses can't have additional register operands, so if
503 // we've already folded stuff into the addressing mode, just force the
504 // global value into its own register, which we can use as the basereg.
505 if (!Subtarget->isPICStyleRIPRel() ||
506 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
507 // Okay, we've committed to selecting this global. Set up the address.
508 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000509
Chris Lattner0a1c9972011-04-17 17:47:38 +0000510 // Allow the subtarget to classify the global.
511 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000512
Chris Lattner0a1c9972011-04-17 17:47:38 +0000513 // If this reference is relative to the pic base, set it now.
514 if (isGlobalRelativeToPICBase(GVFlags)) {
515 // FIXME: How do we know Base.Reg is free??
516 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000517 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000518
519 // Unless the ABI requires an extra load, return a direct reference to
520 // the global.
521 if (!isGlobalStubReference(GVFlags)) {
522 if (Subtarget->isPICStyleRIPRel()) {
523 // Use rip-relative addressing if we can. Above we verified that the
524 // base and index registers are unused.
525 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
526 AM.Base.Reg = X86::RIP;
527 }
528 AM.GVOpFlags = GVFlags;
529 return true;
530 }
531
532 // Ok, we need to do a load from a stub. If we've already loaded from
533 // this stub, reuse the loaded pointer, otherwise emit the load now.
534 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
535 unsigned LoadReg;
536 if (I != LocalValueMap.end() && I->second != 0) {
537 LoadReg = I->second;
538 } else {
539 // Issue load from stub.
540 unsigned Opc = 0;
541 const TargetRegisterClass *RC = NULL;
542 X86AddressMode StubAM;
543 StubAM.Base.Reg = AM.Base.Reg;
544 StubAM.GV = GV;
545 StubAM.GVOpFlags = GVFlags;
546
547 // Prepare for inserting code in the local-value area.
Eric Christopher96bd4412012-10-02 21:16:50 +0000548 MachineBasicBlock::iterator SaveIter = enterLocalValueArea();
Chris Lattner0a1c9972011-04-17 17:47:38 +0000549
550 if (TLI.getPointerTy() == MVT::i64) {
551 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000552 RC = &X86::GR64RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000553
554 if (Subtarget->isPICStyleRIPRel())
555 StubAM.Base.Reg = X86::RIP;
556 } else {
557 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000558 RC = &X86::GR32RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000559 }
560
561 LoadReg = createResultReg(RC);
562 MachineInstrBuilder LoadMI =
563 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
564 addFullAddress(LoadMI, StubAM);
565
566 // Ok, back to normal mode.
Eric Christopher96bd4412012-10-02 21:16:50 +0000567 leaveLocalValueArea(SaveIter);
Chris Lattner0a1c9972011-04-17 17:47:38 +0000568
569 // Prevent loading GV stub multiple times in same MBB.
570 LocalValueMap[V] = LoadReg;
571 }
572
573 // Now construct the final address. Note that the Disp, Scale,
574 // and Index values may already be set here.
575 AM.Base.Reg = LoadReg;
576 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000577 return true;
578 }
Dan Gohman0586d912008-09-10 20:11:02 +0000579 }
580
Dan Gohman97135e12008-09-26 19:15:30 +0000581 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000582 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000583 if (AM.Base.Reg == 0) {
584 AM.Base.Reg = getRegForValue(V);
585 return AM.Base.Reg != 0;
586 }
587 if (AM.IndexReg == 0) {
588 assert(AM.Scale == 1 && "Scale with no index!");
589 AM.IndexReg = getRegForValue(V);
590 return AM.IndexReg != 0;
591 }
592 }
593
594 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000595}
596
Chris Lattner0aa43de2009-07-10 05:33:42 +0000597/// X86SelectCallAddress - Attempt to fill in an address from the given value.
598///
Dan Gohman46510a72010-04-15 01:51:59 +0000599bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
600 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000601 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000602 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000603 Opcode = I->getOpcode();
604 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000605 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000606 Opcode = C->getOpcode();
607 U = C;
608 }
609
610 switch (Opcode) {
611 default: break;
612 case Instruction::BitCast:
613 // Look past bitcasts.
614 return X86SelectCallAddress(U->getOperand(0), AM);
615
616 case Instruction::IntToPtr:
617 // Look past no-op inttoptrs.
618 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
619 return X86SelectCallAddress(U->getOperand(0), AM);
620 break;
621
622 case Instruction::PtrToInt:
623 // Look past no-op ptrtoints.
624 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
625 return X86SelectCallAddress(U->getOperand(0), AM);
626 break;
627 }
628
629 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000630 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000631 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000632 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000633 return false;
634
635 // RIP-relative addresses can't have additional register operands.
636 if (Subtarget->isPICStyleRIPRel() &&
637 (AM.Base.Reg != 0 || AM.IndexReg != 0))
638 return false;
639
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000640 // Can't handle DLLImport.
641 if (GV->hasDLLImportLinkage())
642 return false;
643
644 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000645 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000646 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000647 return false;
648
649 // Okay, we've committed to selecting this global. Set up the basic address.
650 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000651
Chris Lattnere6c07b52009-07-10 05:45:15 +0000652 // No ABI requires an extra load for anything other than DLLImport, which
653 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000654 if (Subtarget->isPICStyleRIPRel()) {
655 // Use rip-relative addressing if we can. Above we verified that the
656 // base and index registers are unused.
657 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
658 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000659 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000660 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
661 } else if (Subtarget->isPICStyleGOT()) {
662 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000663 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000664
Chris Lattner0aa43de2009-07-10 05:33:42 +0000665 return true;
666 }
667
668 // If all else fails, try to materialize the value in a register.
669 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
670 if (AM.Base.Reg == 0) {
671 AM.Base.Reg = getRegForValue(V);
672 return AM.Base.Reg != 0;
673 }
674 if (AM.IndexReg == 0) {
675 assert(AM.Scale == 1 && "Scale with no index!");
676 AM.IndexReg = getRegForValue(V);
677 return AM.IndexReg != 0;
678 }
679 }
680
681 return false;
682}
683
684
Owen Andersona3971df2008-09-04 07:08:58 +0000685/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000686bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000687 // Atomic stores need special handling.
Lang Hamese4824712011-10-18 22:11:33 +0000688 const StoreInst *S = cast<StoreInst>(I);
689
690 if (S->isAtomic())
691 return false;
692
693 unsigned SABIAlignment =
694 TD.getABITypeAlignment(S->getValueOperand()->getType());
695 if (S->getAlignment() != 0 && S->getAlignment() < SABIAlignment)
Eli Friedman4136d232011-09-02 22:33:24 +0000696 return false;
697
Duncan Sands1440e8b2010-11-03 11:35:31 +0000698 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000699 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000700 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000701
Dan Gohman0586d912008-09-10 20:11:02 +0000702 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000703 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000704 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000705
Chris Lattner438949a2008-10-15 05:30:52 +0000706 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000707}
708
Dan Gohman84023e02010-07-10 09:00:22 +0000709/// X86SelectRet - Select and emit code to implement ret instructions.
710bool X86FastISel::X86SelectRet(const Instruction *I) {
711 const ReturnInst *Ret = cast<ReturnInst>(I);
712 const Function &F = *I->getParent()->getParent();
713
714 if (!FuncInfo.CanLowerReturn)
715 return false;
716
717 CallingConv::ID CC = F.getCallingConv();
718 if (CC != CallingConv::C &&
719 CC != CallingConv::Fast &&
720 CC != CallingConv::X86_FastCall)
721 return false;
722
723 if (Subtarget->isTargetWin64())
724 return false;
725
726 // Don't handle popping bytes on return for now.
727 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
728 ->getBytesToPopOnReturn() != 0)
729 return 0;
730
731 // fastcc with -tailcallopt is intended to provide a guaranteed
732 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000733 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohman84023e02010-07-10 09:00:22 +0000734 return false;
735
736 // Let SDISel handle vararg functions.
737 if (F.isVarArg())
738 return false;
739
740 if (Ret->getNumOperands() > 0) {
741 SmallVector<ISD::OutputArg, 4> Outs;
742 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
743 Outs, TLI);
744
745 // Analyze operands of the call, assigning locations to each operand.
746 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000747 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +0000748 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000749 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000750
751 const Value *RV = Ret->getOperand(0);
752 unsigned Reg = getRegForValue(RV);
753 if (Reg == 0)
754 return false;
755
756 // Only handle a single return value for now.
757 if (ValLocs.size() != 1)
758 return false;
759
760 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000761
Dan Gohman84023e02010-07-10 09:00:22 +0000762 // Don't bother handling odd stuff for now.
763 if (VA.getLocInfo() != CCValAssign::Full)
764 return false;
765 // Only handle register returns for now.
766 if (!VA.isRegLoc())
767 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000768
769 // The calling-convention tables for x87 returns don't tell
770 // the whole story.
771 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
772 return false;
773
Eli Friedman22486c92011-05-18 23:13:10 +0000774 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000775 EVT SrcVT = TLI.getValueType(RV->getType());
776 EVT DstVT = VA.getValVT();
777 // Special handling for extended integers.
778 if (SrcVT != DstVT) {
779 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
780 return false;
781
782 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
783 return false;
784
785 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
786
787 if (SrcVT == MVT::i1) {
788 if (Outs[0].Flags.isSExt())
789 return false;
790 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
791 SrcVT = MVT::i8;
792 }
793 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
794 ISD::SIGN_EXTEND;
795 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
796 SrcReg, /*TODO: Kill=*/false);
797 }
798
799 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000800 unsigned DstReg = VA.getLocReg();
801 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000802 // Avoid a cross-class copy. This is very unlikely.
803 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000804 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000805 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
806 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000807
808 // Mark the register as live out of the function.
809 MRI.addLiveOut(VA.getLocReg());
810 }
811
812 // Now emit the RET.
813 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
814 return true;
815}
816
Evan Cheng8b19e562008-09-03 06:44:39 +0000817/// X86SelectLoad - Select and emit code to implement load instructions.
818///
Dan Gohman46510a72010-04-15 01:51:59 +0000819bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000820 // Atomic loads need special handling.
821 if (cast<LoadInst>(I)->isAtomic())
822 return false;
823
Duncan Sands1440e8b2010-11-03 11:35:31 +0000824 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000825 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000826 return false;
827
Dan Gohman0586d912008-09-10 20:11:02 +0000828 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000829 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000830 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000831
Evan Cheng0de588f2008-09-05 21:00:03 +0000832 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000833 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000834 UpdateValueMap(I, ResultReg);
835 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000836 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000837 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000838}
839
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000840static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000841 bool HasAVX = Subtarget->hasAVX();
Craig Topper1accb7e2012-01-10 06:54:16 +0000842 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
843 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000844
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000846 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000847 case MVT::i8: return X86::CMP8rr;
848 case MVT::i16: return X86::CMP16rr;
849 case MVT::i32: return X86::CMP32rr;
850 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000851 case MVT::f32:
852 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
853 case MVT::f64:
854 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000855 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000856}
857
Chris Lattner0e13c782008-10-15 04:13:29 +0000858/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
859/// of the comparison, return an opcode that works for the compare (e.g.
860/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000861static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000863 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000864 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 case MVT::i8: return X86::CMP8ri;
866 case MVT::i16: return X86::CMP16ri;
867 case MVT::i32: return X86::CMP32ri;
868 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000869 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
870 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000871 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000872 return X86::CMP64ri32;
873 return 0;
874 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000875}
876
Dan Gohman46510a72010-04-15 01:51:59 +0000877bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
878 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000879 unsigned Op0Reg = getRegForValue(Op0);
880 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000881
Chris Lattnerd53886b2008-10-15 05:18:04 +0000882 // Handle 'null' like i32/i64 0.
883 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000884 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000885
Chris Lattner9a08a612008-10-15 04:26:38 +0000886 // We have two options: compare with register or immediate. If the RHS of
887 // the compare is an immediate that we can fold into this compare, use
888 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000889 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000890 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000891 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
892 .addReg(Op0Reg)
893 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000894 return true;
895 }
896 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000897
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000898 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000899 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000900
Chris Lattner9a08a612008-10-15 04:26:38 +0000901 unsigned Op1Reg = getRegForValue(Op1);
902 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000903 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
904 .addReg(Op0Reg)
905 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000906
Chris Lattner9a08a612008-10-15 04:26:38 +0000907 return true;
908}
909
Dan Gohman46510a72010-04-15 01:51:59 +0000910bool X86FastISel::X86SelectCmp(const Instruction *I) {
911 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000912
Duncan Sands1440e8b2010-11-03 11:35:31 +0000913 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000914 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000915 return false;
916
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000917 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000918 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000919 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000920 switch (CI->getPredicate()) {
921 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000922 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
923 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000924
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000925 unsigned EReg = createResultReg(&X86::GR8RegClass);
926 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000927 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
928 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
929 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000930 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000931 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000932 UpdateValueMap(I, ResultReg);
933 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000934 }
935 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000936 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
937 return false;
938
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000939 unsigned NEReg = createResultReg(&X86::GR8RegClass);
940 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000941 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
942 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
943 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000944 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000945 UpdateValueMap(I, ResultReg);
946 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000947 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000948 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
949 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
950 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
951 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
952 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
953 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
954 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
955 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
956 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
957 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
958 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
959 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000960
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000961 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
962 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
963 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
964 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
965 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
966 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
967 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
968 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
969 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
970 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000971 default:
972 return false;
973 }
974
Dan Gohman46510a72010-04-15 01:51:59 +0000975 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000976 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000977 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000978
Chris Lattner9a08a612008-10-15 04:26:38 +0000979 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000980 if (!X86FastEmitCompare(Op0, Op1, VT))
981 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000982
Dan Gohman84023e02010-07-10 09:00:22 +0000983 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000984 UpdateValueMap(I, ResultReg);
985 return true;
986}
Evan Cheng8b19e562008-09-03 06:44:39 +0000987
Dan Gohman46510a72010-04-15 01:51:59 +0000988bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000989 // Handle zero-extension from i1 to i8, which is common.
Eric Christopher471e4222011-06-08 23:55:35 +0000990 if (!I->getOperand(0)->getType()->isIntegerTy(1))
Eli Friedman76927d732011-05-25 23:49:02 +0000991 return false;
992
993 EVT DstVT = TLI.getValueType(I->getType());
994 if (!TLI.isTypeLegal(DstVT))
995 return false;
996
997 unsigned ResultReg = getRegForValue(I->getOperand(0));
998 if (ResultReg == 0)
999 return false;
1000
1001 // Set the high bits to zero.
1002 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1003 if (ResultReg == 0)
1004 return false;
1005
1006 if (DstVT != MVT::i8) {
1007 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1008 ResultReg, /*Kill=*/true);
1009 if (ResultReg == 0)
1010 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +00001011 }
1012
Eli Friedman76927d732011-05-25 23:49:02 +00001013 UpdateValueMap(I, ResultReg);
1014 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +00001015}
1016
Chris Lattner9a08a612008-10-15 04:26:38 +00001017
Dan Gohman46510a72010-04-15 01:51:59 +00001018bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +00001019 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +00001020 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001021 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +00001022 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1023 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +00001024
Dan Gohman8bef7442010-08-21 02:32:36 +00001025 // Fold the common case of a conditional branch with a comparison
1026 // in the same block (values defined on other blocks may not have
1027 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +00001028 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +00001029 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001030 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001031
Dan Gohmand98d6202008-10-02 22:15:21 +00001032 // Try to take advantage of fallthrough opportunities.
1033 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001034 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001035 std::swap(TrueMBB, FalseMBB);
1036 Predicate = CmpInst::getInversePredicate(Predicate);
1037 }
1038
Chris Lattner871d2462008-10-15 03:58:05 +00001039 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1040 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1041
Dan Gohmand98d6202008-10-02 22:15:21 +00001042 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001043 case CmpInst::FCMP_OEQ:
1044 std::swap(TrueMBB, FalseMBB);
1045 Predicate = CmpInst::FCMP_UNE;
1046 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001047 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1048 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1049 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1050 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1051 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1052 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1053 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1054 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1055 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1056 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1057 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1058 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1059 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001060
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001061 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1062 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1063 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1064 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1065 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1066 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1067 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1068 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1069 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1070 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001071 default:
1072 return false;
1073 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001074
Dan Gohman46510a72010-04-15 01:51:59 +00001075 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001076 if (SwapArgs)
1077 std::swap(Op0, Op1);
1078
Chris Lattner9a08a612008-10-15 04:26:38 +00001079 // Emit a compare of the LHS and RHS, setting the flags.
1080 if (!X86FastEmitCompare(Op0, Op1, VT))
1081 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001082
Dan Gohman84023e02010-07-10 09:00:22 +00001083 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1084 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001085
1086 if (Predicate == CmpInst::FCMP_UNE) {
1087 // X86 requires a second branch to handle UNE (and OEQ,
1088 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001089 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1090 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001091 }
1092
Stuart Hastings3bf91252010-06-17 22:43:56 +00001093 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001094 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001095 return true;
1096 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001097 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1098 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1099 // typically happen for _Bool and C++ bools.
1100 MVT SourceVT;
1101 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1102 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1103 unsigned TestOpc = 0;
1104 switch (SourceVT.SimpleTy) {
1105 default: break;
1106 case MVT::i8: TestOpc = X86::TEST8ri; break;
1107 case MVT::i16: TestOpc = X86::TEST16ri; break;
1108 case MVT::i32: TestOpc = X86::TEST32ri; break;
1109 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1110 }
1111 if (TestOpc) {
1112 unsigned OpReg = getRegForValue(TI->getOperand(0));
1113 if (OpReg == 0) return false;
1114 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1115 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001116
Chris Lattnerc76d1212011-04-19 04:26:32 +00001117 unsigned JmpOpc = X86::JNE_4;
1118 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1119 std::swap(TrueMBB, FalseMBB);
1120 JmpOpc = X86::JE_4;
1121 }
Eric Christopher471e4222011-06-08 23:55:35 +00001122
Chris Lattnerc76d1212011-04-19 04:26:32 +00001123 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001124 .addMBB(TrueMBB);
1125 FastEmitBranch(FalseMBB, DL);
1126 FuncInfo.MBB->addSuccessor(TrueMBB);
1127 return true;
1128 }
1129 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001130 }
1131
1132 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001133 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1134 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001135 unsigned OpReg = getRegForValue(BI->getCondition());
1136 if (OpReg == 0) return false;
1137
Eli Friedman547eb4f2011-04-27 01:34:27 +00001138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1139 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001140 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1141 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001142 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001143 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001144 return true;
1145}
1146
Dan Gohman46510a72010-04-15 01:51:59 +00001147bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001148 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001149 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001150 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001151 CReg = X86::CL;
1152 RC = &X86::GR8RegClass;
1153 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001154 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1155 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1156 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001157 default: return false;
1158 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001159 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001160 CReg = X86::CX;
1161 RC = &X86::GR16RegClass;
1162 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001163 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1164 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1165 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001166 default: return false;
1167 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001168 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001169 CReg = X86::ECX;
1170 RC = &X86::GR32RegClass;
1171 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001172 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1173 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1174 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001175 default: return false;
1176 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001177 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001178 CReg = X86::RCX;
1179 RC = &X86::GR64RegClass;
1180 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001181 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1182 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1183 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001184 default: return false;
1185 }
1186 } else {
1187 return false;
1188 }
1189
Duncan Sands1440e8b2010-11-03 11:35:31 +00001190 MVT VT;
1191 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001192 return false;
1193
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001194 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1195 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001196
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001197 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1198 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001199 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1200 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001201
1202 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001203 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001204 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001205 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1206 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001207 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001208
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001209 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001210 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1211 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001212 UpdateValueMap(I, ResultReg);
1213 return true;
1214}
1215
Dan Gohman46510a72010-04-15 01:51:59 +00001216bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001217 MVT VT;
1218 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001219 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001220
Eric Christophere487b012010-09-29 23:00:29 +00001221 // We only use cmov here, if we don't have a cmov instruction bail.
1222 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001223
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001224 unsigned Opc = 0;
1225 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001226 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001227 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001228 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001229 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001230 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001231 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001232 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001233 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001234 RC = &X86::GR64RegClass;
1235 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001236 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001237 }
1238
1239 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1240 if (Op0Reg == 0) return false;
1241 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1242 if (Op1Reg == 0) return false;
1243 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1244 if (Op2Reg == 0) return false;
1245
Dan Gohman84023e02010-07-10 09:00:22 +00001246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1247 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001248 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001249 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1250 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001251 UpdateValueMap(I, ResultReg);
1252 return true;
1253}
1254
Dan Gohman46510a72010-04-15 01:51:59 +00001255bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001256 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001257 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001258 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001259 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001260 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001261 unsigned OpReg = getRegForValue(V);
1262 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001263 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001264 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1265 TII.get(X86::CVTSS2SDrr), ResultReg)
1266 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001267 UpdateValueMap(I, ResultReg);
1268 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001269 }
1270 }
1271
1272 return false;
1273}
1274
Dan Gohman46510a72010-04-15 01:51:59 +00001275bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001276 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001277 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001278 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001279 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001280 unsigned OpReg = getRegForValue(V);
1281 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001282 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001283 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1284 TII.get(X86::CVTSD2SSrr), ResultReg)
1285 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001286 UpdateValueMap(I, ResultReg);
1287 return true;
1288 }
1289 }
1290 }
1291
1292 return false;
1293}
1294
Dan Gohman46510a72010-04-15 01:51:59 +00001295bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001296 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1297 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001298
Eli Friedman76927d732011-05-25 23:49:02 +00001299 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001300 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001301 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001302 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001303 return false;
1304
1305 unsigned InputReg = getRegForValue(I->getOperand(0));
1306 if (!InputReg)
1307 // Unhandled operand. Halt "fast" selection and bail.
1308 return false;
1309
Eli Friedman76927d732011-05-25 23:49:02 +00001310 if (SrcVT == MVT::i8) {
1311 // Truncate from i8 to i1; no code needed.
1312 UpdateValueMap(I, InputReg);
1313 return true;
1314 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001315
Eli Friedman76927d732011-05-25 23:49:02 +00001316 if (!Subtarget->is64Bit()) {
1317 // If we're on x86-32; we can't extract an i8 from a general register.
1318 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperc9099502012-04-20 06:31:50 +00001319 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1320 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1321 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedman76927d732011-05-25 23:49:02 +00001322 unsigned CopyReg = createResultReg(CopyRC);
1323 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1324 CopyReg).addReg(InputReg);
1325 InputReg = CopyReg;
1326 }
1327
1328 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001329 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001330 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001331 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001332 if (!ResultReg)
1333 return false;
1334
1335 UpdateValueMap(I, ResultReg);
1336 return true;
1337}
1338
Eli Friedmanc0883452011-05-20 22:21:04 +00001339bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1340 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1341}
1342
Eli Friedmand5089a92011-04-27 01:45:07 +00001343bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1344 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001345
Eli Friedmand5089a92011-04-27 01:45:07 +00001346 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001347 if (!IsMemcpySmall(Len))
1348 return false;
1349
1350 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001351
1352 // We don't care about alignment here since we just emit integer accesses.
1353 while (Len) {
1354 MVT VT;
1355 if (Len >= 8 && i64Legal)
1356 VT = MVT::i64;
1357 else if (Len >= 4)
1358 VT = MVT::i32;
1359 else if (Len >= 2)
1360 VT = MVT::i16;
1361 else {
1362 assert(Len == 1);
1363 VT = MVT::i8;
1364 }
1365
1366 unsigned Reg;
1367 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1368 RV &= X86FastEmitStore(VT, Reg, DestAM);
1369 assert(RV && "Failed to emit load or store??");
1370
1371 unsigned Size = VT.getSizeInBits()/8;
1372 Len -= Size;
1373 DestAM.Disp += Size;
1374 SrcAM.Disp += Size;
1375 }
1376
1377 return true;
1378}
1379
Dan Gohman46510a72010-04-15 01:51:59 +00001380bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001381 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001382 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001383 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001384 case Intrinsic::memcpy: {
1385 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1386 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001387 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001388 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001389
Eli Friedman25255cb2011-06-10 23:39:36 +00001390 if (isa<ConstantInt>(MCI.getLength())) {
1391 // Small memcpy's are common enough that we want to do them
1392 // without a call if possible.
1393 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1394 if (IsMemcpySmall(Len)) {
1395 X86AddressMode DestAM, SrcAM;
1396 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1397 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1398 return false;
1399 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1400 return true;
1401 }
1402 }
Eric Christopher471e4222011-06-08 23:55:35 +00001403
Eli Friedman25255cb2011-06-10 23:39:36 +00001404 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1405 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001406 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001407
Eli Friedman25255cb2011-06-10 23:39:36 +00001408 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1409 return false;
1410
1411 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001412 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001413 case Intrinsic::memset: {
1414 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001415
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001416 if (MSI.isVolatile())
1417 return false;
1418
Eli Friedman25255cb2011-06-10 23:39:36 +00001419 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1420 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1421 return false;
1422
1423 if (MSI.getDestAddressSpace() > 255)
1424 return false;
1425
1426 return DoSelectCall(&I, "memset");
1427 }
Eric Christopher07754c22010-03-18 20:27:26 +00001428 case Intrinsic::stackprotector: {
Chad Rosiere1093e52012-05-11 19:43:29 +00001429 // Emit code to store the stack guard onto the stack.
Eric Christopher07754c22010-03-18 20:27:26 +00001430 EVT PtrTy = TLI.getPointerTy();
1431
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001432 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1433 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001434
1435 // Grab the frame index.
1436 X86AddressMode AM;
1437 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001438 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001439 return true;
1440 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001441 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001442 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001443 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001444 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001445 if (!X86SelectAddress(DI->getAddress(), AM))
1446 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001447 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001448 // FIXME may need to add RegState::Debug to any registers produced,
1449 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001450 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1451 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001452 return true;
1453 }
Eric Christopher77f79892010-01-18 22:11:29 +00001454 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001455 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001456 return true;
1457 }
Bill Wendling52370a12008-12-09 02:42:50 +00001458 case Intrinsic::sadd_with_overflow:
1459 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001460 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001461
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001462 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001463 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001464 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001465 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001466 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1467
Duncan Sands1440e8b2010-11-03 11:35:31 +00001468 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001469 if (!isTypeLegal(RetTy, VT))
1470 return false;
1471
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001472 const Value *Op1 = I.getArgOperand(0);
1473 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001474 unsigned Reg1 = getRegForValue(Op1);
1475 unsigned Reg2 = getRegForValue(Op2);
1476
1477 if (Reg1 == 0 || Reg2 == 0)
1478 // FIXME: Handle values *not* in registers.
1479 return false;
1480
1481 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001482 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001483 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001484 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001485 OpC = X86::ADD64rr;
1486 else
1487 return false;
1488
Eli Friedman482feb32011-05-16 21:06:17 +00001489 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001490 // both the returned values.
Eli Friedman482feb32011-05-16 21:06:17 +00001491 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001492 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1493 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001494
Chris Lattnera9a42252009-04-12 07:36:01 +00001495 unsigned Opc = X86::SETBr;
1496 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1497 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001498 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1499
1500 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001501 return true;
1502 }
1503 }
1504}
1505
Dan Gohman46510a72010-04-15 01:51:59 +00001506bool X86FastISel::X86SelectCall(const Instruction *I) {
1507 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001508 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001509
1510 // Can't handle inline asm yet.
1511 if (isa<InlineAsm>(Callee))
1512 return false;
1513
Bill Wendling52370a12008-12-09 02:42:50 +00001514 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001515 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001516 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001517
Eli Friedman25255cb2011-06-10 23:39:36 +00001518 return DoSelectCall(I, 0);
1519}
1520
Rafael Espindolac338fe02012-07-25 15:42:45 +00001521static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1522 const ImmutableCallSite &CS) {
Rafael Espindola742f2c92012-07-25 13:35:45 +00001523 if (Subtarget.is64Bit())
1524 return 0;
1525 if (Subtarget.isTargetWindows())
1526 return 0;
1527 CallingConv::ID CC = CS.getCallingConv();
1528 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1529 return 0;
1530 if (!CS.paramHasAttr(1, Attribute::StructRet))
1531 return 0;
Craig Topperf4cfc442012-08-11 17:53:00 +00001532 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola1cee7102012-07-25 13:41:10 +00001533 return 0;
Rafael Espindola742f2c92012-07-25 13:35:45 +00001534 return 4;
1535}
1536
Eli Friedman25255cb2011-06-10 23:39:36 +00001537// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1538bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1539 const CallInst *CI = cast<CallInst>(I);
1540 const Value *Callee = CI->getCalledValue();
1541
Evan Chengf3d4efe2008-09-07 09:09:33 +00001542 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001543 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001544 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001545 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001546 CC != CallingConv::X86_FastCall)
1547 return false;
1548
Evan Cheng381993f2010-01-27 00:00:57 +00001549 // fastcc with -tailcallopt is intended to provide a guaranteed
1550 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001551 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001552 return false;
1553
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001554 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1555 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001556 bool isVarArg = FTy->isVarArg();
1557
1558 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1559 // x86-32. Special handling for x86-64 is implemented.
1560 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001561 return false;
1562
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001563 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001564 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001565 TM.Options.GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001566 return false;
1567
Eli Friedman19515b42011-05-17 18:29:03 +00001568 // Check whether the function can return without sret-demotion.
1569 SmallVector<ISD::OutputArg, 4> Outs;
Eli Friedman19515b42011-05-17 18:29:03 +00001570 GetReturnInfo(I->getType(), CS.getAttributes().getRetAttributes(),
Eli Friedman2db0e9e2012-05-25 00:09:29 +00001571 Outs, TLI);
Eli Friedman19515b42011-05-17 18:29:03 +00001572 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001573 *FuncInfo.MF, FTy->isVarArg(),
1574 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001575 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001576 return false;
1577
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001578 // Materialize callee address in a register. FIXME: GV address can be
1579 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001580 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001581 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001582 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001583 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001584 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001585 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001586 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001587 } else if (CalleeAM.Base.Reg != 0) {
1588 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001589 } else
1590 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001591
Evan Chengf3d4efe2008-09-07 09:09:33 +00001592 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001593 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001594 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001595 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001596 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosier15b44972012-02-15 00:36:26 +00001597 unsigned arg_size = CS.arg_size();
1598 Args.reserve(arg_size);
1599 ArgVals.reserve(arg_size);
1600 ArgVTs.reserve(arg_size);
1601 ArgFlags.reserve(arg_size);
Dan Gohman46510a72010-04-15 01:51:59 +00001602 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001603 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001604 // If we're lowering a mem intrinsic instead of a regular call, skip the
1605 // last two arguments, which should not passed to the underlying functions.
1606 if (MemIntName && e-i <= 2)
1607 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001608 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001609 ISD::ArgFlagsTy Flags;
1610 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001611 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001612 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001613 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001614 Flags.setZExt();
1615
Eli Friedmanc0883452011-05-20 22:21:04 +00001616 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001617 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1618 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001619 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1620 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1621 if (!FrameAlign)
1622 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1623 Flags.setByVal();
1624 Flags.setByValSize(FrameSize);
1625 Flags.setByValAlign(FrameAlign);
1626 if (!IsMemcpySmall(FrameSize))
1627 return false;
1628 }
1629
1630 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
1631 Flags.setInReg();
1632 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
1633 Flags.setNest();
1634
Chris Lattnere03b8d32011-04-19 04:42:38 +00001635 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1636 // instruction. This is safe because it is common to all fastisel supported
1637 // calling conventions on x86.
1638 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1639 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1640 CI->getBitWidth() == 16) {
1641 if (Flags.isSExt())
1642 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1643 else
1644 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1645 }
1646 }
Eric Christopher471e4222011-06-08 23:55:35 +00001647
Chris Lattnerb44101c2011-04-19 05:09:50 +00001648 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001649
Chris Lattnerff009ad2011-04-19 05:15:59 +00001650 // Passing bools around ends up doing a trunc to i1 and passing it.
1651 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001652 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1653 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1654 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001655 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1656 ArgReg = getRegForValue(ArgVal);
1657 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001658
Chris Lattnerb44101c2011-04-19 05:09:50 +00001659 MVT ArgVT;
1660 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001661
Chris Lattnerb44101c2011-04-19 05:09:50 +00001662 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1663 ArgVal->hasOneUse(), 1);
1664 } else {
1665 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001666 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001667
Chris Lattnerff009ad2011-04-19 05:15:59 +00001668 if (ArgReg == 0) return false;
1669
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001670 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001671 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001672 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001673 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001674 if (ArgVT == MVT::x86mmx)
1675 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001676 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1677 Flags.setOrigAlign(OriginalAlignment);
1678
Chris Lattnerb44101c2011-04-19 05:09:50 +00001679 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001680 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001681 ArgVTs.push_back(ArgVT);
1682 ArgFlags.push_back(Flags);
1683 }
1684
1685 // Analyze operands of the call, assigning locations to each operand.
1686 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001687 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00001688 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001689
Dan Gohmand8acddd2010-06-01 21:09:47 +00001690 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001691 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001692 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001693
Duncan Sands45907662010-10-31 13:21:44 +00001694 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001695
1696 // Get a count of how many bytes are to be pushed on the stack.
1697 unsigned NumBytes = CCInfo.getNextStackOffset();
1698
1699 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001700 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001701 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1702 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001703
Chris Lattner438949a2008-10-15 05:30:52 +00001704 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001705 // copies / loads.
1706 SmallVector<unsigned, 4> RegArgs;
1707 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1708 CCValAssign &VA = ArgLocs[i];
1709 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001710 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001711
Evan Chengf3d4efe2008-09-07 09:09:33 +00001712 // Promote the value if needed.
1713 switch (VA.getLocInfo()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001714 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001715 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001716 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1717 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001718 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1719 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001720 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001721 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001722 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001723 }
1724 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001725 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1726 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001727 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1728 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001729 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001730 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001731 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001732 }
1733 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001734 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1735 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001736 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1737 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001738 if (!Emitted)
1739 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001740 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001741 if (!Emitted)
1742 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1743 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001744
Chris Lattnerc46ec642011-01-05 22:26:52 +00001745 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001746 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001747 break;
1748 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001749 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001750 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001751 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001752 assert(BC != 0 && "Failed to emit a bitcast!");
1753 Arg = BC;
1754 ArgVT = VA.getLocVT();
1755 break;
1756 }
Chad Rosier36ec0ca2012-07-11 19:58:38 +00001757 case CCValAssign::VExt:
1758 // VExt has not been implemented, so this should be impossible to reach
1759 // for now. However, fallback to Selection DAG isel once implemented.
1760 return false;
1761 case CCValAssign::Indirect:
1762 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
1763 // support this.
1764 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001765 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001766
Evan Chengf3d4efe2008-09-07 09:09:33 +00001767 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001768 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1769 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001770 RegArgs.push_back(VA.getLocReg());
1771 } else {
1772 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001773 X86AddressMode AM;
1774 AM.Base.Reg = StackPtr;
1775 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001776 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00001777 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001778
Eli Friedmanc0883452011-05-20 22:21:04 +00001779 if (Flags.isByVal()) {
1780 X86AddressMode SrcAM;
1781 SrcAM.Base.Reg = Arg;
1782 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
1783 assert(Res && "memcpy length already checked!"); (void)Res;
1784 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
1785 // If this is a really simple value, emit this with the Value* version
Nick Lewycky1f9c6862011-10-12 00:14:12 +00001786 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedmanc0883452011-05-20 22:21:04 +00001787 // as it can cause us to reevaluate the argument.
Lang Hamese4824712011-10-18 22:11:33 +00001788 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
1789 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001790 } else {
Lang Hamese4824712011-10-18 22:11:33 +00001791 if (!X86FastEmitStore(ArgVT, Arg, AM))
1792 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001793 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001794 }
1795 }
1796
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001797 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001798 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001799 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001800 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1802 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001803 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001804
Eli Friedman37620462011-04-19 17:22:22 +00001805 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1806 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00001807 static const uint16_t XMMArgRegs[] = {
Eli Friedman37620462011-04-19 17:22:22 +00001808 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1809 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1810 };
1811 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1812 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1813 X86::AL).addImm(NumXMMRegs);
1814 }
1815
Evan Chengf3d4efe2008-09-07 09:09:33 +00001816 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001817 MachineInstrBuilder MIB;
1818 if (CalleeOp) {
1819 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001820 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00001821 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00001822 CallOpc = X86::CALL64r;
1823 else
1824 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001825 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1826 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001827
Chris Lattner51e8eab2009-07-09 06:34:26 +00001828 } else {
1829 // Direct call.
1830 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001831 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00001832 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00001833 CallOpc = X86::CALL64pcrel32;
1834 else
1835 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001836
Chris Lattner51e8eab2009-07-09 06:34:26 +00001837 // See if we need any target-specific flags on the GV operand.
1838 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001839
Chris Lattner51e8eab2009-07-09 06:34:26 +00001840 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1841 // external symbols most go through the PLT in PIC mode. If the symbol
1842 // has hidden or protected visibility, or if it is static or local, then
1843 // we don't need to use the PLT - we can directly call it.
1844 if (Subtarget->isTargetELF() &&
1845 TM.getRelocationModel() == Reloc::PIC_ &&
1846 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1847 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001848 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001849 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001850 (!Subtarget->getTargetTriple().isMacOSX() ||
1851 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001852 // PC-relative references to external symbols should go through $stub,
1853 // unless we're building with the leopard linker or later, which
1854 // automatically synthesizes these stubs.
1855 OpFlags = X86II::MO_DARWIN_STUB;
1856 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001857
1858
Eli Friedman25255cb2011-06-10 23:39:36 +00001859 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
1860 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00001861 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00001862 else
1863 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001864 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001865
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00001866 // Add a register mask with the call-preserved registers.
1867 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1868 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
1869
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00001870 // Add an implicit use GOT pointer in EBX.
1871 if (Subtarget->isPICStyleGOT())
1872 MIB.addReg(X86::EBX, RegState::Implicit);
1873
1874 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1875 MIB.addReg(X86::AL, RegState::Implicit);
1876
1877 // Add implicit physical register uses to the call.
1878 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1879 MIB.addReg(RegArgs[i], RegState::Implicit);
1880
Evan Chengf3d4efe2008-09-07 09:09:33 +00001881 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001882 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolac338fe02012-07-25 15:42:45 +00001883 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohman84023e02010-07-10 09:00:22 +00001884 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001885 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001886
Eli Friedman19515b42011-05-17 18:29:03 +00001887 // Build info for return calling conv lowering code.
1888 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1889 SmallVector<ISD::InputArg, 32> Ins;
1890 SmallVector<EVT, 4> RetTys;
1891 ComputeValueVTs(TLI, I->getType(), RetTys);
1892 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1893 EVT VT = RetTys[i];
1894 EVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
1895 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1896 for (unsigned j = 0; j != NumRegs; ++j) {
1897 ISD::InputArg MyFlags;
1898 MyFlags.VT = RegisterVT.getSimpleVT();
1899 MyFlags.Used = !CS.getInstruction()->use_empty();
1900 if (CS.paramHasAttr(0, Attribute::SExt))
1901 MyFlags.Flags.setSExt();
1902 if (CS.paramHasAttr(0, Attribute::ZExt))
1903 MyFlags.Flags.setZExt();
1904 if (CS.paramHasAttr(0, Attribute::InReg))
1905 MyFlags.Flags.setInReg();
1906 Ins.push_back(MyFlags);
1907 }
1908 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001909
Eli Friedman19515b42011-05-17 18:29:03 +00001910 // Now handle call return values.
1911 SmallVector<unsigned, 4> UsedRegs;
1912 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001913 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00001914 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001915 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1916 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1917 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1918 EVT CopyVT = RVLocs[i].getValVT();
1919 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001920
Evan Chengf3d4efe2008-09-07 09:09:33 +00001921 // If this is a call to a function that returns an fp value on the x87 fp
1922 // stack, but where we prefer to use the value in xmm registers, copy it
1923 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00001924 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001925 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001926 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001927 CopyVT = MVT::f80;
Craig Topperc9099502012-04-20 06:31:50 +00001928 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001929 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001930 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
1931 CopyReg);
1932 } else {
1933 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1934 CopyReg).addReg(RVLocs[i].getLocReg());
1935 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001936 }
1937
Eli Friedman19515b42011-05-17 18:29:03 +00001938 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001939 // Round the F80 the right size, which also moves to the appropriate xmm
1940 // register. This is accomplished by storing the F80 value in memory and
1941 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00001942 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001943 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001944 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001945 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001946 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1947 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00001948 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00001950 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00001951 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001952 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001953 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00001954
Eli Friedman19515b42011-05-17 18:29:03 +00001955 if (RVLocs.size())
1956 UpdateValueMap(I, ResultReg, RVLocs.size());
1957
Dan Gohmandb497122010-06-18 23:28:01 +00001958 // Set all unused physreg defs as dead.
1959 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1960
Evan Chengf3d4efe2008-09-07 09:09:33 +00001961 return true;
1962}
1963
1964
Dan Gohman99b21822008-08-28 23:21:34 +00001965bool
Dan Gohman46510a72010-04-15 01:51:59 +00001966X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001967 switch (I->getOpcode()) {
1968 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001969 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001970 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001971 case Instruction::Store:
1972 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001973 case Instruction::Ret:
1974 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001975 case Instruction::ICmp:
1976 case Instruction::FCmp:
1977 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001978 case Instruction::ZExt:
1979 return X86SelectZExt(I);
1980 case Instruction::Br:
1981 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001982 case Instruction::Call:
1983 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001984 case Instruction::LShr:
1985 case Instruction::AShr:
1986 case Instruction::Shl:
1987 return X86SelectShift(I);
1988 case Instruction::Select:
1989 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001990 case Instruction::Trunc:
1991 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001992 case Instruction::FPExt:
1993 return X86SelectFPExt(I);
1994 case Instruction::FPTrunc:
1995 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001996 case Instruction::IntToPtr: // Deliberate fall-through.
1997 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001998 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1999 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00002000 if (DstVT.bitsGT(SrcVT))
2001 return X86SelectZExt(I);
2002 if (DstVT.bitsLT(SrcVT))
2003 return X86SelectTrunc(I);
2004 unsigned Reg = getRegForValue(I->getOperand(0));
2005 if (Reg == 0) return false;
2006 UpdateValueMap(I, Reg);
2007 return true;
2008 }
Dan Gohman99b21822008-08-28 23:21:34 +00002009 }
2010
2011 return false;
2012}
2013
Dan Gohman46510a72010-04-15 01:51:59 +00002014unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002015 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00002016 if (!isTypeLegal(C->getType(), VT))
Michael Liaofaa11592012-08-30 00:30:16 +00002017 return 0;
2018
2019 // Can't handle alternate code models yet.
2020 if (TM.getCodeModel() != CodeModel::Small)
2021 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002022
Owen Anderson95267a12008-09-05 00:06:23 +00002023 // Get opcode and regclass of the output for the given load instruction.
2024 unsigned Opc = 0;
2025 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002026 switch (VT.SimpleTy) {
Michael Liaofaa11592012-08-30 00:30:16 +00002027 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002028 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00002029 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +00002030 RC = &X86::GR8RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002031 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00002033 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +00002034 RC = &X86::GR16RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002035 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00002037 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +00002038 RC = &X86::GR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002039 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00002041 // Must be in x86-64 mode.
2042 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +00002043 RC = &X86::GR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002044 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002046 if (X86ScalarSSEf32) {
2047 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +00002048 RC = &X86::FR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002049 } else {
2050 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +00002051 RC = &X86::RFP32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002052 }
2053 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002054 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002055 if (X86ScalarSSEf64) {
2056 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +00002057 RC = &X86::FR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002058 } else {
2059 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +00002060 RC = &X86::RFP64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002061 }
2062 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002064 // No f80 support yet.
Michael Liaofaa11592012-08-30 00:30:16 +00002065 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002066 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002067
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002068 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002069 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002070 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002071 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002072 // If the expression is just a basereg, then we're done, otherwise we need
2073 // to emit an LEA.
2074 if (AM.BaseType == X86AddressMode::RegBase &&
2075 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2076 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002077
Chris Lattner685090f2011-04-17 17:12:08 +00002078 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002079 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002080 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2081 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002082 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002083 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002084 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002085 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002086
Owen Anderson3b217c62008-09-06 01:11:01 +00002087 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002088 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002089 if (Align == 0) {
2090 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002091 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002092 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002093
Dan Gohman5396c992008-09-30 01:21:32 +00002094 // x86-32 PIC requires a PIC base register for constant pools.
2095 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002096 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002097 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002098 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002099 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002100 } else if (Subtarget->isPICStyleGOT()) {
2101 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002102 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002103 } else if (Subtarget->isPICStyleRIPRel() &&
2104 TM.getCodeModel() == CodeModel::Small) {
2105 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002106 }
Dan Gohman5396c992008-09-30 01:21:32 +00002107
2108 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002109 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002110 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002111 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2112 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002113 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002114
Owen Anderson95267a12008-09-05 00:06:23 +00002115 return ResultReg;
2116}
2117
Dan Gohman46510a72010-04-15 01:51:59 +00002118unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002119 // Fail on dynamic allocas. At this point, getRegForValue has already
2120 // checked its CSE maps, so if we're here trying to handle a dynamic
2121 // alloca, we're not going to succeed. X86SelectAddress has a
2122 // check for dynamic allocas, because it's called directly from
2123 // various places, but TargetMaterializeAlloca also needs a check
2124 // in order to avoid recursion between getRegForValue,
2125 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002126 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002127 return 0;
2128
Dan Gohman0586d912008-09-10 20:11:02 +00002129 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002130 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002131 return 0;
2132 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper44d23822012-02-22 05:59:10 +00002133 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman0586d912008-09-10 20:11:02 +00002134 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002135 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2136 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002137 return ResultReg;
2138}
2139
Eli Friedman2790ba82011-04-27 22:41:55 +00002140unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2141 MVT VT;
2142 if (!isTypeLegal(CF->getType(), VT))
2143 return false;
2144
2145 // Get opcode and regclass for the given zero.
2146 unsigned Opc = 0;
2147 const TargetRegisterClass *RC = NULL;
2148 switch (VT.SimpleTy) {
Craig Topperf4cfc442012-08-11 17:53:00 +00002149 default: return false;
2150 case MVT::f32:
2151 if (X86ScalarSSEf32) {
2152 Opc = X86::FsFLD0SS;
2153 RC = &X86::FR32RegClass;
2154 } else {
2155 Opc = X86::LD_Fp032;
2156 RC = &X86::RFP32RegClass;
2157 }
2158 break;
2159 case MVT::f64:
2160 if (X86ScalarSSEf64) {
2161 Opc = X86::FsFLD0SD;
2162 RC = &X86::FR64RegClass;
2163 } else {
2164 Opc = X86::LD_Fp064;
2165 RC = &X86::RFP64RegClass;
2166 }
2167 break;
2168 case MVT::f80:
2169 // No f80 support yet.
2170 return false;
Eli Friedman2790ba82011-04-27 22:41:55 +00002171 }
2172
2173 unsigned ResultReg = createResultReg(RC);
2174 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2175 return ResultReg;
2176}
2177
2178
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002179/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2180/// vreg is being provided by the specified load instruction. If possible,
2181/// try to fold the load as an operand to the instruction, returning true if
2182/// possible.
2183bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2184 const LoadInst *LI) {
2185 X86AddressMode AM;
2186 if (!X86SelectAddress(LI->getOperand(0), AM))
2187 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002188
Craig Topperdca72542012-08-11 17:46:16 +00002189 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002190
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002191 unsigned Size = TD.getTypeAllocSize(LI->getType());
2192 unsigned Alignment = LI->getAlignment();
2193
2194 SmallVector<MachineOperand, 8> AddrOps;
2195 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002196
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002197 MachineInstr *Result =
2198 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2199 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002200
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002201 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002202 MI->eraseFromParent();
2203 return true;
2204}
2205
2206
Evan Chengc3f44b02008-09-03 00:03:49 +00002207namespace llvm {
Bob Wilsond49edb72012-08-03 04:06:28 +00002208 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2209 const TargetLibraryInfo *libInfo) {
2210 return new X86FastISel(funcInfo, libInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002211 }
Dan Gohman99b21822008-08-28 23:21:34 +00002212}