blob: 15894390cfa746298bc192add9b644a34edf8489 [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:
Dan Gohmana4160c32010-07-07 16:29:44 +000060 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000061 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000062 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Bruno Cardoso Lopesaed890b2011-08-01 21:54:05 +000063 X86ScalarSSEf64 = Subtarget->hasSSE2() || Subtarget->hasAVX();
64 X86ScalarSSEf32 = Subtarget->hasSSE1() || Subtarget->hasAVX();
Evan Cheng88e30412008-09-03 01:04:47 +000065 }
Evan Chengc3f44b02008-09-03 00:03:49 +000066
Dan Gohman46510a72010-04-15 01:51:59 +000067 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000068
Chris Lattnerbeac75d2010-09-05 02:18:34 +000069 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
70 /// vreg is being provided by the specified load instruction. If possible,
71 /// try to fold the load as an operand to the instruction, returning true if
72 /// possible.
73 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
74 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000075
Dan Gohman1adf1b02008-08-19 21:45:35 +000076#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000077
78private:
Dan Gohman46510a72010-04-15 01:51:59 +000079 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000080
Owen Andersone50ed302009-08-10 22:56:29 +000081 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000082
Chris Lattnerb44101c2011-04-19 05:09:50 +000083 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
84 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000085
Owen Andersone50ed302009-08-10 22:56:29 +000086 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000087 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000088
Dan Gohman46510a72010-04-15 01:51:59 +000089 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
90 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000093
Dan Gohman46510a72010-04-15 01:51:59 +000094 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000095
Dan Gohman84023e02010-07-10 09:00:22 +000096 bool X86SelectRet(const Instruction *I);
97
Dan Gohman46510a72010-04-15 01:51:59 +000098 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000099
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000105
Dan Gohman46510a72010-04-15 01:51:59 +0000106 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000107
Dan Gohman46510a72010-04-15 01:51:59 +0000108 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000109
Dan Gohman46510a72010-04-15 01:51:59 +0000110 bool X86SelectFPExt(const Instruction *I);
111 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000112
Dan Gohman46510a72010-04-15 01:51:59 +0000113 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
114 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000115
Eli Friedman25255cb2011-06-10 23:39:36 +0000116 bool DoSelectCall(const Instruction *I, const char *MemIntName);
117
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000118 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000119 return getTargetMachine()->getInstrInfo();
120 }
121 const X86TargetMachine *getTargetMachine() const {
122 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000123 }
124
Dan Gohman46510a72010-04-15 01:51:59 +0000125 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000126
Dan Gohman46510a72010-04-15 01:51:59 +0000127 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000128
Eli Friedman2790ba82011-04-27 22:41:55 +0000129 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
130
Evan Chengf3d4efe2008-09-07 09:09:33 +0000131 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
132 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000133 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
135 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000136 }
137
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000138 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000139
Eli Friedmanc0883452011-05-20 22:21:04 +0000140 bool IsMemcpySmall(uint64_t Len);
141
Eli Friedmand5089a92011-04-27 01:45:07 +0000142 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
143 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000144};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000145
Chris Lattner087fcf32009-03-08 18:44:31 +0000146} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000147
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000148bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000149 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
150 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000151 // Unhandled type. Halt "fast" selection and bail.
152 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000153
154 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000155 // For now, require SSE/SSE2 for performing floating-point operations,
156 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000158 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000160 return false;
161 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000163 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000164 // We only handle legal types. For example, on x86-32 the instruction
165 // selector contains all of the 64-bit instructions from x86-64,
166 // under the assumption that i64 won't be used if the target doesn't
167 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000169}
170
171#include "X86GenCallingConv.inc"
172
Evan Cheng0de588f2008-09-05 21:00:03 +0000173/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000174/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000175/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000176bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000177 unsigned &ResultReg) {
178 // Get opcode and regclass of the output for the given load instruction.
179 unsigned Opc = 0;
180 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000182 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000183 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000185 Opc = X86::MOV8rm;
186 RC = X86::GR8RegisterClass;
187 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000189 Opc = X86::MOV16rm;
190 RC = X86::GR16RegisterClass;
191 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 Opc = X86::MOV32rm;
194 RC = X86::GR32RegisterClass;
195 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 // Must be in x86-64 mode.
198 Opc = X86::MOV64rm;
199 RC = X86::GR64RegisterClass;
200 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000202 if (X86ScalarSSEf32) {
203 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Evan Cheng0de588f2008-09-05 21:00:03 +0000204 RC = X86::FR32RegisterClass;
205 } else {
206 Opc = X86::LD_Fp32m;
207 RC = X86::RFP32RegisterClass;
208 }
209 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000211 if (X86ScalarSSEf64) {
212 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Evan Cheng0de588f2008-09-05 21:00:03 +0000213 RC = X86::FR64RegisterClass;
214 } else {
215 Opc = X86::LD_Fp64m;
216 RC = X86::RFP64RegisterClass;
217 }
218 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000220 // No f80 support yet.
221 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000222 }
223
224 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000225 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
226 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000227 return true;
228}
229
Evan Chengf3d4efe2008-09-07 09:09:33 +0000230/// X86FastEmitStore - Emit a machine instruction to store a value Val of
231/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
232/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000233/// i.e. V. Return true if it is possible.
234bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000235X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000236 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000237 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 switch (VT.getSimpleVT().SimpleTy) {
239 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000240 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000241 case MVT::i1: {
242 // Mask out all but lowest bit.
243 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000245 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
246 Val = AndResult;
247 }
248 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 case MVT::i8: Opc = X86::MOV8mr; break;
250 case MVT::i16: Opc = X86::MOV16mr; break;
251 case MVT::i32: Opc = X86::MOV32mr; break;
252 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
253 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000254 Opc = X86ScalarSSEf32 ?
255 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000256 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000258 Opc = X86ScalarSSEf64 ?
259 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000260 break;
Lang Hamese4824712011-10-18 22:11:33 +0000261 case MVT::v4f32:
262 Opc = X86::MOVAPSmr;
263 break;
264 case MVT::v2f64:
265 Opc = X86::MOVAPDmr;
266 break;
267 case MVT::v4i32:
268 case MVT::v2i64:
269 case MVT::v8i16:
270 case MVT::v16i8:
271 Opc = X86::MOVDQAmr;
272 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000273 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000274
Dan Gohman84023e02010-07-10 09:00:22 +0000275 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
276 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000277 return true;
278}
279
Dan Gohman46510a72010-04-15 01:51:59 +0000280bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000281 const X86AddressMode &AM) {
282 // Handle 'null' like i32/i64 0.
283 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000284 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000285
Chris Lattner438949a2008-10-15 05:30:52 +0000286 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000287 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000288 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000289 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000291 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000292 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 case MVT::i8: Opc = X86::MOV8mi; break;
294 case MVT::i16: Opc = X86::MOV16mi; break;
295 case MVT::i32: Opc = X86::MOV32mi; break;
296 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000297 // Must be a 32-bit sign extended value.
298 if ((int)CI->getSExtValue() == CI->getSExtValue())
299 Opc = X86::MOV64mi32;
300 break;
301 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000302
Chris Lattner438949a2008-10-15 05:30:52 +0000303 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000304 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
305 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000306 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000307 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000308 return true;
309 }
310 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000311
Chris Lattner438949a2008-10-15 05:30:52 +0000312 unsigned ValReg = getRegForValue(Val);
313 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000314 return false;
315
Chris Lattner438949a2008-10-15 05:30:52 +0000316 return X86FastEmitStore(VT, ValReg, AM);
317}
318
Evan Cheng24e3a902008-09-08 06:35:17 +0000319/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
320/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
321/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000322bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
323 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000324 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000325 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
326 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000327
Owen Andersonac34a002008-09-11 19:44:55 +0000328 if (RR != 0) {
329 ResultReg = RR;
330 return true;
331 } else
332 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000333}
334
Dan Gohman0586d912008-09-10 20:11:02 +0000335/// X86SelectAddress - Attempt to fill in an address from the given value.
336///
Dan Gohman46510a72010-04-15 01:51:59 +0000337bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
338 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000339 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000340 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000341 // Don't walk into other basic blocks; it's possible we haven't
342 // visited them yet, so the instructions may not yet be assigned
343 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000344 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
345 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
346 Opcode = I->getOpcode();
347 U = I;
348 }
Dan Gohman46510a72010-04-15 01:51:59 +0000349 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000350 Opcode = C->getOpcode();
351 U = C;
352 }
Dan Gohman0586d912008-09-10 20:11:02 +0000353
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000354 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000355 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000356 // Fast instruction selection doesn't support the special
357 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000358 return false;
359
Dan Gohman35893082008-09-18 23:23:44 +0000360 switch (Opcode) {
361 default: break;
362 case Instruction::BitCast:
363 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000364 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000365
366 case Instruction::IntToPtr:
367 // Look past no-op inttoptrs.
368 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000369 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000370 break;
Dan Gohman35893082008-09-18 23:23:44 +0000371
372 case Instruction::PtrToInt:
373 // Look past no-op ptrtoints.
374 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000375 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000376 break;
Dan Gohman35893082008-09-18 23:23:44 +0000377
378 case Instruction::Alloca: {
379 // Do static allocas.
380 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000381 DenseMap<const AllocaInst*, int>::iterator SI =
382 FuncInfo.StaticAllocaMap.find(A);
383 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000384 AM.BaseType = X86AddressMode::FrameIndexBase;
385 AM.Base.FrameIndex = SI->second;
386 return true;
387 }
388 break;
Dan Gohman35893082008-09-18 23:23:44 +0000389 }
390
391 case Instruction::Add: {
392 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000393 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000394 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
395 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000396 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000397 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000398 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000399 }
Dan Gohman0586d912008-09-10 20:11:02 +0000400 }
Dan Gohman35893082008-09-18 23:23:44 +0000401 break;
402 }
403
404 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000405 X86AddressMode SavedAM = AM;
406
Dan Gohman35893082008-09-18 23:23:44 +0000407 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000408 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000409 unsigned IndexReg = AM.IndexReg;
410 unsigned Scale = AM.Scale;
411 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000412 // Iterate through the indices, folding what we can. Constants can be
413 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000414 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000415 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000416 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000417 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000418 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000419 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
420 continue;
421 }
Eric Christopher471e4222011-06-08 23:55:35 +0000422
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000423 // A array/variable index is always of the form i*S where S is the
424 // constant scale size. See if we can push the scale into immediates.
425 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
426 for (;;) {
427 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
428 // Constant-offset addressing.
429 Disp += CI->getSExtValue() * S;
430 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000431 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000432 if (isa<AddOperator>(Op) &&
433 (!isa<Instruction>(Op) ||
434 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
435 == FuncInfo.MBB) &&
436 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
437 // An add (in the same block) with a constant operand. Fold the
438 // constant.
439 ConstantInt *CI =
440 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
441 Disp += CI->getSExtValue() * S;
442 // Iterate on the other operand.
443 Op = cast<AddOperator>(Op)->getOperand(0);
444 continue;
445 }
446 if (IndexReg == 0 &&
447 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
448 (S == 1 || S == 2 || S == 4 || S == 8)) {
449 // Scaled-index addressing.
450 Scale = S;
451 IndexReg = getRegForGEPIndex(Op).first;
452 if (IndexReg == 0)
453 return false;
454 break;
455 }
456 // Unsupported.
457 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000458 }
459 }
Dan Gohman09aae462008-09-26 20:04:15 +0000460 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000461 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000462 break;
Dan Gohman35893082008-09-18 23:23:44 +0000463 // Ok, the GEP indices were covered by constant-offset and scaled-index
464 // addressing. Update the address state and move on to examining the base.
465 AM.IndexReg = IndexReg;
466 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000467 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000468 if (X86SelectAddress(U->getOperand(0), AM))
469 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000470
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000471 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000472 // our address and just match the value instead of completely failing.
473 AM = SavedAM;
474 break;
Dan Gohman35893082008-09-18 23:23:44 +0000475 unsupported_gep:
476 // Ok, the GEP indices weren't all covered.
477 break;
478 }
479 }
480
481 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000482 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedmana6176ad2011-09-22 23:41:28 +0000483 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000484 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000485 return false;
486
Eli Friedmana6176ad2011-09-22 23:41:28 +0000487 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000488 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000489 if (GVar->isThreadLocal())
490 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000491
Eli Friedmana6176ad2011-09-22 23:41:28 +0000492 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
493 // it works...).
494 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
495 if (const GlobalVariable *GVar =
496 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
497 if (GVar->isThreadLocal())
498 return false;
499
Chris Lattner0a1c9972011-04-17 17:47:38 +0000500 // RIP-relative addresses can't have additional register operands, so if
501 // we've already folded stuff into the addressing mode, just force the
502 // global value into its own register, which we can use as the basereg.
503 if (!Subtarget->isPICStyleRIPRel() ||
504 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
505 // Okay, we've committed to selecting this global. Set up the address.
506 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000507
Chris Lattner0a1c9972011-04-17 17:47:38 +0000508 // Allow the subtarget to classify the global.
509 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000510
Chris Lattner0a1c9972011-04-17 17:47:38 +0000511 // If this reference is relative to the pic base, set it now.
512 if (isGlobalRelativeToPICBase(GVFlags)) {
513 // FIXME: How do we know Base.Reg is free??
514 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000515 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000516
517 // Unless the ABI requires an extra load, return a direct reference to
518 // the global.
519 if (!isGlobalStubReference(GVFlags)) {
520 if (Subtarget->isPICStyleRIPRel()) {
521 // Use rip-relative addressing if we can. Above we verified that the
522 // base and index registers are unused.
523 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
524 AM.Base.Reg = X86::RIP;
525 }
526 AM.GVOpFlags = GVFlags;
527 return true;
528 }
529
530 // Ok, we need to do a load from a stub. If we've already loaded from
531 // this stub, reuse the loaded pointer, otherwise emit the load now.
532 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
533 unsigned LoadReg;
534 if (I != LocalValueMap.end() && I->second != 0) {
535 LoadReg = I->second;
536 } else {
537 // Issue load from stub.
538 unsigned Opc = 0;
539 const TargetRegisterClass *RC = NULL;
540 X86AddressMode StubAM;
541 StubAM.Base.Reg = AM.Base.Reg;
542 StubAM.GV = GV;
543 StubAM.GVOpFlags = GVFlags;
544
545 // Prepare for inserting code in the local-value area.
546 SavePoint SaveInsertPt = enterLocalValueArea();
547
548 if (TLI.getPointerTy() == MVT::i64) {
549 Opc = X86::MOV64rm;
550 RC = X86::GR64RegisterClass;
551
552 if (Subtarget->isPICStyleRIPRel())
553 StubAM.Base.Reg = X86::RIP;
554 } else {
555 Opc = X86::MOV32rm;
556 RC = X86::GR32RegisterClass;
557 }
558
559 LoadReg = createResultReg(RC);
560 MachineInstrBuilder LoadMI =
561 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
562 addFullAddress(LoadMI, StubAM);
563
564 // Ok, back to normal mode.
565 leaveLocalValueArea(SaveInsertPt);
566
567 // Prevent loading GV stub multiple times in same MBB.
568 LocalValueMap[V] = LoadReg;
569 }
570
571 // Now construct the final address. Note that the Disp, Scale,
572 // and Index values may already be set here.
573 AM.Base.Reg = LoadReg;
574 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000575 return true;
576 }
Dan Gohman0586d912008-09-10 20:11:02 +0000577 }
578
Dan Gohman97135e12008-09-26 19:15:30 +0000579 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000580 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000581 if (AM.Base.Reg == 0) {
582 AM.Base.Reg = getRegForValue(V);
583 return AM.Base.Reg != 0;
584 }
585 if (AM.IndexReg == 0) {
586 assert(AM.Scale == 1 && "Scale with no index!");
587 AM.IndexReg = getRegForValue(V);
588 return AM.IndexReg != 0;
589 }
590 }
591
592 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000593}
594
Chris Lattner0aa43de2009-07-10 05:33:42 +0000595/// X86SelectCallAddress - Attempt to fill in an address from the given value.
596///
Dan Gohman46510a72010-04-15 01:51:59 +0000597bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
598 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000599 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000600 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000601 Opcode = I->getOpcode();
602 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000603 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000604 Opcode = C->getOpcode();
605 U = C;
606 }
607
608 switch (Opcode) {
609 default: break;
610 case Instruction::BitCast:
611 // Look past bitcasts.
612 return X86SelectCallAddress(U->getOperand(0), AM);
613
614 case Instruction::IntToPtr:
615 // Look past no-op inttoptrs.
616 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
617 return X86SelectCallAddress(U->getOperand(0), AM);
618 break;
619
620 case Instruction::PtrToInt:
621 // Look past no-op ptrtoints.
622 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
623 return X86SelectCallAddress(U->getOperand(0), AM);
624 break;
625 }
626
627 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000628 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000629 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000630 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000631 return false;
632
633 // RIP-relative addresses can't have additional register operands.
634 if (Subtarget->isPICStyleRIPRel() &&
635 (AM.Base.Reg != 0 || AM.IndexReg != 0))
636 return false;
637
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000638 // Can't handle DLLImport.
639 if (GV->hasDLLImportLinkage())
640 return false;
641
642 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000643 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000644 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000645 return false;
646
647 // Okay, we've committed to selecting this global. Set up the basic address.
648 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000649
Chris Lattnere6c07b52009-07-10 05:45:15 +0000650 // No ABI requires an extra load for anything other than DLLImport, which
651 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000652 if (Subtarget->isPICStyleRIPRel()) {
653 // Use rip-relative addressing if we can. Above we verified that the
654 // base and index registers are unused.
655 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
656 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000657 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000658 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
659 } else if (Subtarget->isPICStyleGOT()) {
660 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000661 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000662
Chris Lattner0aa43de2009-07-10 05:33:42 +0000663 return true;
664 }
665
666 // If all else fails, try to materialize the value in a register.
667 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
668 if (AM.Base.Reg == 0) {
669 AM.Base.Reg = getRegForValue(V);
670 return AM.Base.Reg != 0;
671 }
672 if (AM.IndexReg == 0) {
673 assert(AM.Scale == 1 && "Scale with no index!");
674 AM.IndexReg = getRegForValue(V);
675 return AM.IndexReg != 0;
676 }
677 }
678
679 return false;
680}
681
682
Owen Andersona3971df2008-09-04 07:08:58 +0000683/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000684bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000685 // Atomic stores need special handling.
Lang Hamese4824712011-10-18 22:11:33 +0000686 const StoreInst *S = cast<StoreInst>(I);
687
688 if (S->isAtomic())
689 return false;
690
691 unsigned SABIAlignment =
692 TD.getABITypeAlignment(S->getValueOperand()->getType());
693 if (S->getAlignment() != 0 && S->getAlignment() < SABIAlignment)
Eli Friedman4136d232011-09-02 22:33:24 +0000694 return false;
695
Duncan Sands1440e8b2010-11-03 11:35:31 +0000696 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000697 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000698 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000699
Dan Gohman0586d912008-09-10 20:11:02 +0000700 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000701 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000702 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000703
Chris Lattner438949a2008-10-15 05:30:52 +0000704 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000705}
706
Dan Gohman84023e02010-07-10 09:00:22 +0000707/// X86SelectRet - Select and emit code to implement ret instructions.
708bool X86FastISel::X86SelectRet(const Instruction *I) {
709 const ReturnInst *Ret = cast<ReturnInst>(I);
710 const Function &F = *I->getParent()->getParent();
711
712 if (!FuncInfo.CanLowerReturn)
713 return false;
714
715 CallingConv::ID CC = F.getCallingConv();
716 if (CC != CallingConv::C &&
717 CC != CallingConv::Fast &&
718 CC != CallingConv::X86_FastCall)
719 return false;
720
721 if (Subtarget->isTargetWin64())
722 return false;
723
724 // Don't handle popping bytes on return for now.
725 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
726 ->getBytesToPopOnReturn() != 0)
727 return 0;
728
729 // fastcc with -tailcallopt is intended to provide a guaranteed
730 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000731 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohman84023e02010-07-10 09:00:22 +0000732 return false;
733
734 // Let SDISel handle vararg functions.
735 if (F.isVarArg())
736 return false;
737
738 if (Ret->getNumOperands() > 0) {
739 SmallVector<ISD::OutputArg, 4> Outs;
740 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
741 Outs, TLI);
742
743 // Analyze operands of the call, assigning locations to each operand.
744 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000745 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
746 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000747 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000748
749 const Value *RV = Ret->getOperand(0);
750 unsigned Reg = getRegForValue(RV);
751 if (Reg == 0)
752 return false;
753
754 // Only handle a single return value for now.
755 if (ValLocs.size() != 1)
756 return false;
757
758 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000759
Dan Gohman84023e02010-07-10 09:00:22 +0000760 // Don't bother handling odd stuff for now.
761 if (VA.getLocInfo() != CCValAssign::Full)
762 return false;
763 // Only handle register returns for now.
764 if (!VA.isRegLoc())
765 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000766
767 // The calling-convention tables for x87 returns don't tell
768 // the whole story.
769 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
770 return false;
771
Eli Friedman22486c92011-05-18 23:13:10 +0000772 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000773 EVT SrcVT = TLI.getValueType(RV->getType());
774 EVT DstVT = VA.getValVT();
775 // Special handling for extended integers.
776 if (SrcVT != DstVT) {
777 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
778 return false;
779
780 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
781 return false;
782
783 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
784
785 if (SrcVT == MVT::i1) {
786 if (Outs[0].Flags.isSExt())
787 return false;
788 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
789 SrcVT = MVT::i8;
790 }
791 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
792 ISD::SIGN_EXTEND;
793 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
794 SrcReg, /*TODO: Kill=*/false);
795 }
796
797 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000798 unsigned DstReg = VA.getLocReg();
799 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000800 // Avoid a cross-class copy. This is very unlikely.
801 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000802 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000803 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
804 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000805
806 // Mark the register as live out of the function.
807 MRI.addLiveOut(VA.getLocReg());
808 }
809
810 // Now emit the RET.
811 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
812 return true;
813}
814
Evan Cheng8b19e562008-09-03 06:44:39 +0000815/// X86SelectLoad - Select and emit code to implement load instructions.
816///
Dan Gohman46510a72010-04-15 01:51:59 +0000817bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000818 // Atomic loads need special handling.
819 if (cast<LoadInst>(I)->isAtomic())
820 return false;
821
Duncan Sands1440e8b2010-11-03 11:35:31 +0000822 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000823 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000824 return false;
825
Dan Gohman0586d912008-09-10 20:11:02 +0000826 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000827 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000828 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000829
Evan Cheng0de588f2008-09-05 21:00:03 +0000830 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000831 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000832 UpdateValueMap(I, ResultReg);
833 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000834 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000835 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000836}
837
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000838static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000839 bool HasAVX = Subtarget->hasAVX();
840 bool X86ScalarSSEf32 = HasAVX || Subtarget->hasSSE1();
841 bool X86ScalarSSEf64 = HasAVX || Subtarget->hasSSE2();
842
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000844 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 case MVT::i8: return X86::CMP8rr;
846 case MVT::i16: return X86::CMP16rr;
847 case MVT::i32: return X86::CMP32rr;
848 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000849 case MVT::f32:
850 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
851 case MVT::f64:
852 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000853 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000854}
855
Chris Lattner0e13c782008-10-15 04:13:29 +0000856/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
857/// of the comparison, return an opcode that works for the compare (e.g.
858/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000859static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000861 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000862 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 case MVT::i8: return X86::CMP8ri;
864 case MVT::i16: return X86::CMP16ri;
865 case MVT::i32: return X86::CMP32ri;
866 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000867 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
868 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000869 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000870 return X86::CMP64ri32;
871 return 0;
872 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000873}
874
Dan Gohman46510a72010-04-15 01:51:59 +0000875bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
876 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000877 unsigned Op0Reg = getRegForValue(Op0);
878 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000879
Chris Lattnerd53886b2008-10-15 05:18:04 +0000880 // Handle 'null' like i32/i64 0.
881 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000882 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000883
Chris Lattner9a08a612008-10-15 04:26:38 +0000884 // We have two options: compare with register or immediate. If the RHS of
885 // the compare is an immediate that we can fold into this compare, use
886 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000887 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000888 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
890 .addReg(Op0Reg)
891 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000892 return true;
893 }
894 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000895
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000896 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000897 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000898
Chris Lattner9a08a612008-10-15 04:26:38 +0000899 unsigned Op1Reg = getRegForValue(Op1);
900 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000901 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
902 .addReg(Op0Reg)
903 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000904
Chris Lattner9a08a612008-10-15 04:26:38 +0000905 return true;
906}
907
Dan Gohman46510a72010-04-15 01:51:59 +0000908bool X86FastISel::X86SelectCmp(const Instruction *I) {
909 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000910
Duncan Sands1440e8b2010-11-03 11:35:31 +0000911 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000912 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000913 return false;
914
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000915 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000916 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000917 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000918 switch (CI->getPredicate()) {
919 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000920 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
921 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000922
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000923 unsigned EReg = createResultReg(&X86::GR8RegClass);
924 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000925 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
926 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
927 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000928 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000929 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000930 UpdateValueMap(I, ResultReg);
931 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000932 }
933 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000934 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
935 return false;
936
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000937 unsigned NEReg = createResultReg(&X86::GR8RegClass);
938 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000939 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
940 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
941 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000942 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000943 UpdateValueMap(I, ResultReg);
944 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000945 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000946 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
947 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
948 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
949 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
950 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
951 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
952 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
953 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
954 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
955 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
956 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
957 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000958
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000959 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
960 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
961 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
962 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
963 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
964 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
965 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
966 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
967 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
968 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000969 default:
970 return false;
971 }
972
Dan Gohman46510a72010-04-15 01:51:59 +0000973 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000974 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000975 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000976
Chris Lattner9a08a612008-10-15 04:26:38 +0000977 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000978 if (!X86FastEmitCompare(Op0, Op1, VT))
979 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000980
Dan Gohman84023e02010-07-10 09:00:22 +0000981 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000982 UpdateValueMap(I, ResultReg);
983 return true;
984}
Evan Cheng8b19e562008-09-03 06:44:39 +0000985
Dan Gohman46510a72010-04-15 01:51:59 +0000986bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000987 // Handle zero-extension from i1 to i8, which is common.
Eric Christopher471e4222011-06-08 23:55:35 +0000988 if (!I->getOperand(0)->getType()->isIntegerTy(1))
Eli Friedman76927d732011-05-25 23:49:02 +0000989 return false;
990
991 EVT DstVT = TLI.getValueType(I->getType());
992 if (!TLI.isTypeLegal(DstVT))
993 return false;
994
995 unsigned ResultReg = getRegForValue(I->getOperand(0));
996 if (ResultReg == 0)
997 return false;
998
999 // Set the high bits to zero.
1000 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1001 if (ResultReg == 0)
1002 return false;
1003
1004 if (DstVT != MVT::i8) {
1005 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1006 ResultReg, /*Kill=*/true);
1007 if (ResultReg == 0)
1008 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +00001009 }
1010
Eli Friedman76927d732011-05-25 23:49:02 +00001011 UpdateValueMap(I, ResultReg);
1012 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +00001013}
1014
Chris Lattner9a08a612008-10-15 04:26:38 +00001015
Dan Gohman46510a72010-04-15 01:51:59 +00001016bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +00001017 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +00001018 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001019 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +00001020 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1021 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +00001022
Dan Gohman8bef7442010-08-21 02:32:36 +00001023 // Fold the common case of a conditional branch with a comparison
1024 // in the same block (values defined on other blocks may not have
1025 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +00001026 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +00001027 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001028 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001029
Dan Gohmand98d6202008-10-02 22:15:21 +00001030 // Try to take advantage of fallthrough opportunities.
1031 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001032 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001033 std::swap(TrueMBB, FalseMBB);
1034 Predicate = CmpInst::getInversePredicate(Predicate);
1035 }
1036
Chris Lattner871d2462008-10-15 03:58:05 +00001037 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1038 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1039
Dan Gohmand98d6202008-10-02 22:15:21 +00001040 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001041 case CmpInst::FCMP_OEQ:
1042 std::swap(TrueMBB, FalseMBB);
1043 Predicate = CmpInst::FCMP_UNE;
1044 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001045 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1046 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1047 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1048 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1049 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1050 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1051 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1052 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1053 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1054 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1055 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1056 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1057 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001058
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001059 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1060 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1061 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1062 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1063 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1064 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1065 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1066 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1067 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1068 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001069 default:
1070 return false;
1071 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001072
Dan Gohman46510a72010-04-15 01:51:59 +00001073 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001074 if (SwapArgs)
1075 std::swap(Op0, Op1);
1076
Chris Lattner9a08a612008-10-15 04:26:38 +00001077 // Emit a compare of the LHS and RHS, setting the flags.
1078 if (!X86FastEmitCompare(Op0, Op1, VT))
1079 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001080
Dan Gohman84023e02010-07-10 09:00:22 +00001081 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1082 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001083
1084 if (Predicate == CmpInst::FCMP_UNE) {
1085 // X86 requires a second branch to handle UNE (and OEQ,
1086 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001087 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1088 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001089 }
1090
Stuart Hastings3bf91252010-06-17 22:43:56 +00001091 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001092 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001093 return true;
1094 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001095 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1096 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1097 // typically happen for _Bool and C++ bools.
1098 MVT SourceVT;
1099 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1100 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1101 unsigned TestOpc = 0;
1102 switch (SourceVT.SimpleTy) {
1103 default: break;
1104 case MVT::i8: TestOpc = X86::TEST8ri; break;
1105 case MVT::i16: TestOpc = X86::TEST16ri; break;
1106 case MVT::i32: TestOpc = X86::TEST32ri; break;
1107 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1108 }
1109 if (TestOpc) {
1110 unsigned OpReg = getRegForValue(TI->getOperand(0));
1111 if (OpReg == 0) return false;
1112 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1113 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001114
Chris Lattnerc76d1212011-04-19 04:26:32 +00001115 unsigned JmpOpc = X86::JNE_4;
1116 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1117 std::swap(TrueMBB, FalseMBB);
1118 JmpOpc = X86::JE_4;
1119 }
Eric Christopher471e4222011-06-08 23:55:35 +00001120
Chris Lattnerc76d1212011-04-19 04:26:32 +00001121 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001122 .addMBB(TrueMBB);
1123 FastEmitBranch(FalseMBB, DL);
1124 FuncInfo.MBB->addSuccessor(TrueMBB);
1125 return true;
1126 }
1127 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001128 }
1129
1130 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001131 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1132 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001133 unsigned OpReg = getRegForValue(BI->getCondition());
1134 if (OpReg == 0) return false;
1135
Eli Friedman547eb4f2011-04-27 01:34:27 +00001136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1137 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1139 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001140 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001141 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001142 return true;
1143}
1144
Dan Gohman46510a72010-04-15 01:51:59 +00001145bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001146 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001147 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001148 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001149 CReg = X86::CL;
1150 RC = &X86::GR8RegClass;
1151 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001152 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1153 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1154 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001155 default: return false;
1156 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001157 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001158 CReg = X86::CX;
1159 RC = &X86::GR16RegClass;
1160 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001161 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1162 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1163 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001164 default: return false;
1165 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001166 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001167 CReg = X86::ECX;
1168 RC = &X86::GR32RegClass;
1169 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001170 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1171 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1172 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001173 default: return false;
1174 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001175 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001176 CReg = X86::RCX;
1177 RC = &X86::GR64RegClass;
1178 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001179 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1180 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1181 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001182 default: return false;
1183 }
1184 } else {
1185 return false;
1186 }
1187
Duncan Sands1440e8b2010-11-03 11:35:31 +00001188 MVT VT;
1189 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001190 return false;
1191
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001192 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1193 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001194
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001195 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1196 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001197 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1198 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001199
1200 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001201 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001202 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001203 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1204 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001205 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001206
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001207 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001208 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1209 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001210 UpdateValueMap(I, ResultReg);
1211 return true;
1212}
1213
Dan Gohman46510a72010-04-15 01:51:59 +00001214bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001215 MVT VT;
1216 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001217 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001218
Eric Christophere487b012010-09-29 23:00:29 +00001219 // We only use cmov here, if we don't have a cmov instruction bail.
1220 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001221
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001222 unsigned Opc = 0;
1223 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001224 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001225 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001226 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001227 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001228 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001229 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001230 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001231 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001232 RC = &X86::GR64RegClass;
1233 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001234 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001235 }
1236
1237 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1238 if (Op0Reg == 0) return false;
1239 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1240 if (Op1Reg == 0) return false;
1241 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1242 if (Op2Reg == 0) return false;
1243
Dan Gohman84023e02010-07-10 09:00:22 +00001244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1245 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001246 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001247 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1248 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001249 UpdateValueMap(I, ResultReg);
1250 return true;
1251}
1252
Dan Gohman46510a72010-04-15 01:51:59 +00001253bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001254 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001255 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001256 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001257 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001258 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001259 unsigned OpReg = getRegForValue(V);
1260 if (OpReg == 0) return false;
1261 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001262 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1263 TII.get(X86::CVTSS2SDrr), ResultReg)
1264 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001265 UpdateValueMap(I, ResultReg);
1266 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001267 }
1268 }
1269
1270 return false;
1271}
1272
Dan Gohman46510a72010-04-15 01:51:59 +00001273bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001274 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001275 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001276 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001277 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001278 unsigned OpReg = getRegForValue(V);
1279 if (OpReg == 0) return false;
1280 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001281 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1282 TII.get(X86::CVTSD2SSrr), ResultReg)
1283 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001284 UpdateValueMap(I, ResultReg);
1285 return true;
1286 }
1287 }
1288 }
1289
1290 return false;
1291}
1292
Dan Gohman46510a72010-04-15 01:51:59 +00001293bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001294 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1295 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001296
Eli Friedman76927d732011-05-25 23:49:02 +00001297 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001298 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001299 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001300 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001301 return false;
1302
1303 unsigned InputReg = getRegForValue(I->getOperand(0));
1304 if (!InputReg)
1305 // Unhandled operand. Halt "fast" selection and bail.
1306 return false;
1307
Eli Friedman76927d732011-05-25 23:49:02 +00001308 if (SrcVT == MVT::i8) {
1309 // Truncate from i8 to i1; no code needed.
1310 UpdateValueMap(I, InputReg);
1311 return true;
1312 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001313
Eli Friedman76927d732011-05-25 23:49:02 +00001314 if (!Subtarget->is64Bit()) {
1315 // If we're on x86-32; we can't extract an i8 from a general register.
1316 // First issue a copy to GR16_ABCD or GR32_ABCD.
1317 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
1318 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
1319 unsigned CopyReg = createResultReg(CopyRC);
1320 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1321 CopyReg).addReg(InputReg);
1322 InputReg = CopyReg;
1323 }
1324
1325 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001326 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001327 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001328 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001329 if (!ResultReg)
1330 return false;
1331
1332 UpdateValueMap(I, ResultReg);
1333 return true;
1334}
1335
Eli Friedmanc0883452011-05-20 22:21:04 +00001336bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1337 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1338}
1339
Eli Friedmand5089a92011-04-27 01:45:07 +00001340bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1341 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001342
Eli Friedmand5089a92011-04-27 01:45:07 +00001343 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001344 if (!IsMemcpySmall(Len))
1345 return false;
1346
1347 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001348
1349 // We don't care about alignment here since we just emit integer accesses.
1350 while (Len) {
1351 MVT VT;
1352 if (Len >= 8 && i64Legal)
1353 VT = MVT::i64;
1354 else if (Len >= 4)
1355 VT = MVT::i32;
1356 else if (Len >= 2)
1357 VT = MVT::i16;
1358 else {
1359 assert(Len == 1);
1360 VT = MVT::i8;
1361 }
1362
1363 unsigned Reg;
1364 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1365 RV &= X86FastEmitStore(VT, Reg, DestAM);
1366 assert(RV && "Failed to emit load or store??");
1367
1368 unsigned Size = VT.getSizeInBits()/8;
1369 Len -= Size;
1370 DestAM.Disp += Size;
1371 SrcAM.Disp += Size;
1372 }
1373
1374 return true;
1375}
1376
Dan Gohman46510a72010-04-15 01:51:59 +00001377bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001378 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001379 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001380 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001381 case Intrinsic::memcpy: {
1382 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1383 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001384 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001385 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001386
Eli Friedman25255cb2011-06-10 23:39:36 +00001387 if (isa<ConstantInt>(MCI.getLength())) {
1388 // Small memcpy's are common enough that we want to do them
1389 // without a call if possible.
1390 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1391 if (IsMemcpySmall(Len)) {
1392 X86AddressMode DestAM, SrcAM;
1393 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1394 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1395 return false;
1396 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1397 return true;
1398 }
1399 }
Eric Christopher471e4222011-06-08 23:55:35 +00001400
Eli Friedman25255cb2011-06-10 23:39:36 +00001401 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1402 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001403 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001404
Eli Friedman25255cb2011-06-10 23:39:36 +00001405 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1406 return false;
1407
1408 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001409 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001410 case Intrinsic::memset: {
1411 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001412
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001413 if (MSI.isVolatile())
1414 return false;
1415
Eli Friedman25255cb2011-06-10 23:39:36 +00001416 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1417 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1418 return false;
1419
1420 if (MSI.getDestAddressSpace() > 255)
1421 return false;
1422
1423 return DoSelectCall(&I, "memset");
1424 }
Eric Christopher07754c22010-03-18 20:27:26 +00001425 case Intrinsic::stackprotector: {
1426 // Emit code inline code to store the stack guard onto the stack.
1427 EVT PtrTy = TLI.getPointerTy();
1428
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001429 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1430 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001431
1432 // Grab the frame index.
1433 X86AddressMode AM;
1434 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001435 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001436 return true;
1437 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001438 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001439 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001440 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001441 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001442 if (!X86SelectAddress(DI->getAddress(), AM))
1443 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001444 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001445 // FIXME may need to add RegState::Debug to any registers produced,
1446 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001447 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1448 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001449 return true;
1450 }
Eric Christopher77f79892010-01-18 22:11:29 +00001451 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001452 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001453 return true;
1454 }
Bill Wendling52370a12008-12-09 02:42:50 +00001455 case Intrinsic::sadd_with_overflow:
1456 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001457 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001458
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001459 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001460 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001461 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001462 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001463 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1464
Duncan Sands1440e8b2010-11-03 11:35:31 +00001465 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001466 if (!isTypeLegal(RetTy, VT))
1467 return false;
1468
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001469 const Value *Op1 = I.getArgOperand(0);
1470 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001471 unsigned Reg1 = getRegForValue(Op1);
1472 unsigned Reg2 = getRegForValue(Op2);
1473
1474 if (Reg1 == 0 || Reg2 == 0)
1475 // FIXME: Handle values *not* in registers.
1476 return false;
1477
1478 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001479 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001480 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001481 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001482 OpC = X86::ADD64rr;
1483 else
1484 return false;
1485
Eli Friedman482feb32011-05-16 21:06:17 +00001486 // The call to CreateRegs builds two sequential registers, to store the
1487 // both the the returned values.
1488 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001489 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1490 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001491
Chris Lattnera9a42252009-04-12 07:36:01 +00001492 unsigned Opc = X86::SETBr;
1493 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1494 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1496
1497 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001498 return true;
1499 }
1500 }
1501}
1502
Dan Gohman46510a72010-04-15 01:51:59 +00001503bool X86FastISel::X86SelectCall(const Instruction *I) {
1504 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001505 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001506
1507 // Can't handle inline asm yet.
1508 if (isa<InlineAsm>(Callee))
1509 return false;
1510
Bill Wendling52370a12008-12-09 02:42:50 +00001511 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001512 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001513 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001514
Eli Friedman25255cb2011-06-10 23:39:36 +00001515 return DoSelectCall(I, 0);
1516}
1517
1518// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1519bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1520 const CallInst *CI = cast<CallInst>(I);
1521 const Value *Callee = CI->getCalledValue();
1522
Evan Chengf3d4efe2008-09-07 09:09:33 +00001523 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001524 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001525 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001526 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001527 CC != CallingConv::X86_FastCall)
1528 return false;
1529
Evan Cheng381993f2010-01-27 00:00:57 +00001530 // fastcc with -tailcallopt is intended to provide a guaranteed
1531 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001532 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001533 return false;
1534
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001535 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1536 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001537 bool isVarArg = FTy->isVarArg();
1538
1539 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1540 // x86-32. Special handling for x86-64 is implemented.
1541 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001542 return false;
1543
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001544 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001545 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001546 TM.Options.GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001547 return false;
1548
Eli Friedman19515b42011-05-17 18:29:03 +00001549 // Check whether the function can return without sret-demotion.
1550 SmallVector<ISD::OutputArg, 4> Outs;
1551 SmallVector<uint64_t, 4> Offsets;
1552 GetReturnInfo(I->getType(), CS.getAttributes().getRetAttributes(),
1553 Outs, TLI, &Offsets);
1554 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Eric Christopher471e4222011-06-08 23:55:35 +00001555 *FuncInfo.MF, FTy->isVarArg(),
1556 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001557 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001558 return false;
1559
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001560 // Materialize callee address in a register. FIXME: GV address can be
1561 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001562 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001563 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001564 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001565 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001566 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001567 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001568 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001569 } else if (CalleeAM.Base.Reg != 0) {
1570 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001571 } else
1572 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001573
Evan Chengf3d4efe2008-09-07 09:09:33 +00001574 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001575 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001576 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001577 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001578 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001579 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001580 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001581 ArgVTs.reserve(CS.arg_size());
1582 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001583 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001584 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001585 // If we're lowering a mem intrinsic instead of a regular call, skip the
1586 // last two arguments, which should not passed to the underlying functions.
1587 if (MemIntName && e-i <= 2)
1588 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001589 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001590 ISD::ArgFlagsTy Flags;
1591 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001592 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001593 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001594 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001595 Flags.setZExt();
1596
Eli Friedmanc0883452011-05-20 22:21:04 +00001597 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001598 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1599 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001600 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1601 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1602 if (!FrameAlign)
1603 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1604 Flags.setByVal();
1605 Flags.setByValSize(FrameSize);
1606 Flags.setByValAlign(FrameAlign);
1607 if (!IsMemcpySmall(FrameSize))
1608 return false;
1609 }
1610
1611 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
1612 Flags.setInReg();
1613 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
1614 Flags.setNest();
1615
Chris Lattnere03b8d32011-04-19 04:42:38 +00001616 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1617 // instruction. This is safe because it is common to all fastisel supported
1618 // calling conventions on x86.
1619 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1620 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1621 CI->getBitWidth() == 16) {
1622 if (Flags.isSExt())
1623 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1624 else
1625 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1626 }
1627 }
Eric Christopher471e4222011-06-08 23:55:35 +00001628
Chris Lattnerb44101c2011-04-19 05:09:50 +00001629 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001630
Chris Lattnerff009ad2011-04-19 05:15:59 +00001631 // Passing bools around ends up doing a trunc to i1 and passing it.
1632 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001633 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1634 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1635 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001636 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1637 ArgReg = getRegForValue(ArgVal);
1638 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001639
Chris Lattnerb44101c2011-04-19 05:09:50 +00001640 MVT ArgVT;
1641 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001642
Chris Lattnerb44101c2011-04-19 05:09:50 +00001643 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1644 ArgVal->hasOneUse(), 1);
1645 } else {
1646 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001647 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001648
Chris Lattnerff009ad2011-04-19 05:15:59 +00001649 if (ArgReg == 0) return false;
1650
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001651 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001652 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001653 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001654 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001655 if (ArgVT == MVT::x86mmx)
1656 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001657 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1658 Flags.setOrigAlign(OriginalAlignment);
1659
Chris Lattnerb44101c2011-04-19 05:09:50 +00001660 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001661 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001662 ArgVTs.push_back(ArgVT);
1663 ArgFlags.push_back(Flags);
1664 }
1665
1666 // Analyze operands of the call, assigning locations to each operand.
1667 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001668 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
1669 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001670
Dan Gohmand8acddd2010-06-01 21:09:47 +00001671 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001672 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001673 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001674
Duncan Sands45907662010-10-31 13:21:44 +00001675 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001676
1677 // Get a count of how many bytes are to be pushed on the stack.
1678 unsigned NumBytes = CCInfo.getNextStackOffset();
1679
1680 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001681 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001682 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1683 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001684
Chris Lattner438949a2008-10-15 05:30:52 +00001685 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001686 // copies / loads.
1687 SmallVector<unsigned, 4> RegArgs;
1688 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1689 CCValAssign &VA = ArgLocs[i];
1690 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001691 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001692
Evan Chengf3d4efe2008-09-07 09:09:33 +00001693 // Promote the value if needed.
1694 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001695 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001696 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001697 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001698 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1699 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001700 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1701 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001702 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001703 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001704 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001705 }
1706 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001707 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1708 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001709 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1710 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001711 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001712 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001713 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001714 }
1715 case CCValAssign::AExt: {
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::ANY_EXTEND, VA.getLocVT(),
1719 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001720 if (!Emitted)
1721 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001722 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001723 if (!Emitted)
1724 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1725 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001726
Chris Lattnerc46ec642011-01-05 22:26:52 +00001727 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001728 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001729 break;
1730 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001731 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001732 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001733 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001734 assert(BC != 0 && "Failed to emit a bitcast!");
1735 Arg = BC;
1736 ArgVT = VA.getLocVT();
1737 break;
1738 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001739 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001740
Evan Chengf3d4efe2008-09-07 09:09:33 +00001741 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001742 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1743 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001744 RegArgs.push_back(VA.getLocReg());
1745 } else {
1746 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001747 X86AddressMode AM;
1748 AM.Base.Reg = StackPtr;
1749 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001750 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00001751 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001752
Eli Friedmanc0883452011-05-20 22:21:04 +00001753 if (Flags.isByVal()) {
1754 X86AddressMode SrcAM;
1755 SrcAM.Base.Reg = Arg;
1756 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
1757 assert(Res && "memcpy length already checked!"); (void)Res;
1758 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
1759 // If this is a really simple value, emit this with the Value* version
Nick Lewycky1f9c6862011-10-12 00:14:12 +00001760 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedmanc0883452011-05-20 22:21:04 +00001761 // as it can cause us to reevaluate the argument.
Lang Hamese4824712011-10-18 22:11:33 +00001762 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
1763 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001764 } else {
Lang Hamese4824712011-10-18 22:11:33 +00001765 if (!X86FastEmitStore(ArgVT, Arg, AM))
1766 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001767 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001768 }
1769 }
1770
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001771 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001772 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001773 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001774 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001775 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1776 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001777 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001778
Eli Friedman37620462011-04-19 17:22:22 +00001779 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1780 // Count the number of XMM registers allocated.
1781 static const unsigned XMMArgRegs[] = {
1782 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1783 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1784 };
1785 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1786 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1787 X86::AL).addImm(NumXMMRegs);
1788 }
1789
Evan Chengf3d4efe2008-09-07 09:09:33 +00001790 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001791 MachineInstrBuilder MIB;
1792 if (CalleeOp) {
1793 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001794 unsigned CallOpc;
1795 if (Subtarget->isTargetWin64())
1796 CallOpc = X86::WINCALL64r;
1797 else if (Subtarget->is64Bit())
1798 CallOpc = X86::CALL64r;
1799 else
1800 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001801 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1802 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001803
Chris Lattner51e8eab2009-07-09 06:34:26 +00001804 } else {
1805 // Direct call.
1806 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001807 unsigned CallOpc;
1808 if (Subtarget->isTargetWin64())
1809 CallOpc = X86::WINCALL64pcrel32;
1810 else if (Subtarget->is64Bit())
1811 CallOpc = X86::CALL64pcrel32;
1812 else
1813 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001814
Chris Lattner51e8eab2009-07-09 06:34:26 +00001815 // See if we need any target-specific flags on the GV operand.
1816 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001817
Chris Lattner51e8eab2009-07-09 06:34:26 +00001818 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1819 // external symbols most go through the PLT in PIC mode. If the symbol
1820 // has hidden or protected visibility, or if it is static or local, then
1821 // we don't need to use the PLT - we can directly call it.
1822 if (Subtarget->isTargetELF() &&
1823 TM.getRelocationModel() == Reloc::PIC_ &&
1824 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1825 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001826 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001827 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001828 (!Subtarget->getTargetTriple().isMacOSX() ||
1829 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001830 // PC-relative references to external symbols should go through $stub,
1831 // unless we're building with the leopard linker or later, which
1832 // automatically synthesizes these stubs.
1833 OpFlags = X86II::MO_DARWIN_STUB;
1834 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001835
1836
Eli Friedman25255cb2011-06-10 23:39:36 +00001837 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
1838 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00001839 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00001840 else
1841 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001842 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001843
1844 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001845 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001846 MIB.addReg(X86::EBX);
1847
Eli Friedman37620462011-04-19 17:22:22 +00001848 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1849 MIB.addReg(X86::AL);
1850
Evan Chengf3d4efe2008-09-07 09:09:33 +00001851 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001852 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1853 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001854
1855 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001856 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eli Friedmand227eed2011-04-28 20:19:12 +00001857 unsigned NumBytesCallee = 0;
1858 if (!Subtarget->is64Bit() && CS.paramHasAttr(1, Attribute::StructRet))
1859 NumBytesCallee = 4;
Dan Gohman84023e02010-07-10 09:00:22 +00001860 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001861 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001862
Eli Friedman19515b42011-05-17 18:29:03 +00001863 // Build info for return calling conv lowering code.
1864 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1865 SmallVector<ISD::InputArg, 32> Ins;
1866 SmallVector<EVT, 4> RetTys;
1867 ComputeValueVTs(TLI, I->getType(), RetTys);
1868 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1869 EVT VT = RetTys[i];
1870 EVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
1871 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1872 for (unsigned j = 0; j != NumRegs; ++j) {
1873 ISD::InputArg MyFlags;
1874 MyFlags.VT = RegisterVT.getSimpleVT();
1875 MyFlags.Used = !CS.getInstruction()->use_empty();
1876 if (CS.paramHasAttr(0, Attribute::SExt))
1877 MyFlags.Flags.setSExt();
1878 if (CS.paramHasAttr(0, Attribute::ZExt))
1879 MyFlags.Flags.setZExt();
1880 if (CS.paramHasAttr(0, Attribute::InReg))
1881 MyFlags.Flags.setInReg();
1882 Ins.push_back(MyFlags);
1883 }
1884 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001885
Eli Friedman19515b42011-05-17 18:29:03 +00001886 // Now handle call return values.
1887 SmallVector<unsigned, 4> UsedRegs;
1888 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001889 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
1890 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001891 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1892 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1893 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1894 EVT CopyVT = RVLocs[i].getValVT();
1895 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001896
Evan Chengf3d4efe2008-09-07 09:09:33 +00001897 // If this is a call to a function that returns an fp value on the x87 fp
1898 // stack, but where we prefer to use the value in xmm registers, copy it
1899 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00001900 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001901 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001902 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001903 CopyVT = MVT::f80;
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001904 CopyReg = createResultReg(X86::RFP80RegisterClass);
1905 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001906 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
1907 CopyReg);
1908 } else {
1909 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1910 CopyReg).addReg(RVLocs[i].getLocReg());
1911 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001912 }
1913
Eli Friedman19515b42011-05-17 18:29:03 +00001914 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001915 // Round the F80 the right size, which also moves to the appropriate xmm
1916 // register. This is accomplished by storing the F80 value in memory and
1917 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00001918 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001919 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001920 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001921 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001922 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1923 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00001924 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001925 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00001926 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00001927 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001928 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001929 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00001930
Eli Friedman19515b42011-05-17 18:29:03 +00001931 if (RVLocs.size())
1932 UpdateValueMap(I, ResultReg, RVLocs.size());
1933
Dan Gohmandb497122010-06-18 23:28:01 +00001934 // Set all unused physreg defs as dead.
1935 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1936
Evan Chengf3d4efe2008-09-07 09:09:33 +00001937 return true;
1938}
1939
1940
Dan Gohman99b21822008-08-28 23:21:34 +00001941bool
Dan Gohman46510a72010-04-15 01:51:59 +00001942X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001943 switch (I->getOpcode()) {
1944 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001945 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001946 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001947 case Instruction::Store:
1948 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001949 case Instruction::Ret:
1950 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001951 case Instruction::ICmp:
1952 case Instruction::FCmp:
1953 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001954 case Instruction::ZExt:
1955 return X86SelectZExt(I);
1956 case Instruction::Br:
1957 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001958 case Instruction::Call:
1959 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001960 case Instruction::LShr:
1961 case Instruction::AShr:
1962 case Instruction::Shl:
1963 return X86SelectShift(I);
1964 case Instruction::Select:
1965 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001966 case Instruction::Trunc:
1967 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001968 case Instruction::FPExt:
1969 return X86SelectFPExt(I);
1970 case Instruction::FPTrunc:
1971 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001972 case Instruction::IntToPtr: // Deliberate fall-through.
1973 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001974 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1975 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001976 if (DstVT.bitsGT(SrcVT))
1977 return X86SelectZExt(I);
1978 if (DstVT.bitsLT(SrcVT))
1979 return X86SelectTrunc(I);
1980 unsigned Reg = getRegForValue(I->getOperand(0));
1981 if (Reg == 0) return false;
1982 UpdateValueMap(I, Reg);
1983 return true;
1984 }
Dan Gohman99b21822008-08-28 23:21:34 +00001985 }
1986
1987 return false;
1988}
1989
Dan Gohman46510a72010-04-15 01:51:59 +00001990unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001991 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001992 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001993 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001994
Owen Anderson95267a12008-09-05 00:06:23 +00001995 // Get opcode and regclass of the output for the given load instruction.
1996 unsigned Opc = 0;
1997 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001998 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001999 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00002001 Opc = X86::MOV8rm;
2002 RC = X86::GR8RegisterClass;
2003 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00002005 Opc = X86::MOV16rm;
2006 RC = X86::GR16RegisterClass;
2007 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002008 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00002009 Opc = X86::MOV32rm;
2010 RC = X86::GR32RegisterClass;
2011 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00002013 // Must be in x86-64 mode.
2014 Opc = X86::MOV64rm;
2015 RC = X86::GR64RegisterClass;
2016 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002017 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002018 if (X86ScalarSSEf32) {
2019 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Owen Anderson95267a12008-09-05 00:06:23 +00002020 RC = X86::FR32RegisterClass;
2021 } else {
2022 Opc = X86::LD_Fp32m;
2023 RC = X86::RFP32RegisterClass;
2024 }
2025 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002027 if (X86ScalarSSEf64) {
2028 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Owen Anderson95267a12008-09-05 00:06:23 +00002029 RC = X86::FR64RegisterClass;
2030 } else {
2031 Opc = X86::LD_Fp64m;
2032 RC = X86::RFP64RegisterClass;
2033 }
2034 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002036 // No f80 support yet.
2037 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00002038 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002039
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002040 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002041 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002042 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002043 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002044 // If the expression is just a basereg, then we're done, otherwise we need
2045 // to emit an LEA.
2046 if (AM.BaseType == X86AddressMode::RegBase &&
2047 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2048 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002049
Chris Lattner685090f2011-04-17 17:12:08 +00002050 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002051 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002052 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2053 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002054 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002055 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002056 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002057 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002058
Owen Anderson3b217c62008-09-06 01:11:01 +00002059 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002060 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002061 if (Align == 0) {
2062 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002063 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002064 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002065
Dan Gohman5396c992008-09-30 01:21:32 +00002066 // x86-32 PIC requires a PIC base register for constant pools.
2067 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002068 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002069 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002070 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002071 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002072 } else if (Subtarget->isPICStyleGOT()) {
2073 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002074 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002075 } else if (Subtarget->isPICStyleRIPRel() &&
2076 TM.getCodeModel() == CodeModel::Small) {
2077 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002078 }
Dan Gohman5396c992008-09-30 01:21:32 +00002079
2080 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002081 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002082 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002083 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2084 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002085 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002086
Owen Anderson95267a12008-09-05 00:06:23 +00002087 return ResultReg;
2088}
2089
Dan Gohman46510a72010-04-15 01:51:59 +00002090unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002091 // Fail on dynamic allocas. At this point, getRegForValue has already
2092 // checked its CSE maps, so if we're here trying to handle a dynamic
2093 // alloca, we're not going to succeed. X86SelectAddress has a
2094 // check for dynamic allocas, because it's called directly from
2095 // various places, but TargetMaterializeAlloca also needs a check
2096 // in order to avoid recursion between getRegForValue,
2097 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002098 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002099 return 0;
2100
Dan Gohman0586d912008-09-10 20:11:02 +00002101 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002102 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002103 return 0;
2104 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
2105 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
2106 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002107 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2108 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002109 return ResultReg;
2110}
2111
Eli Friedman2790ba82011-04-27 22:41:55 +00002112unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2113 MVT VT;
2114 if (!isTypeLegal(CF->getType(), VT))
2115 return false;
2116
2117 // Get opcode and regclass for the given zero.
2118 unsigned Opc = 0;
2119 const TargetRegisterClass *RC = NULL;
2120 switch (VT.SimpleTy) {
2121 default: return false;
2122 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002123 if (X86ScalarSSEf32) {
Jakob Stoklund Olesen0edd83b2011-11-29 22:27:25 +00002124 Opc = X86::FsFLD0SS;
Eli Friedman2790ba82011-04-27 22:41:55 +00002125 RC = X86::FR32RegisterClass;
2126 } else {
2127 Opc = X86::LD_Fp032;
2128 RC = X86::RFP32RegisterClass;
2129 }
2130 break;
2131 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002132 if (X86ScalarSSEf64) {
Jakob Stoklund Olesen0edd83b2011-11-29 22:27:25 +00002133 Opc = X86::FsFLD0SD;
Eli Friedman2790ba82011-04-27 22:41:55 +00002134 RC = X86::FR64RegisterClass;
2135 } else {
2136 Opc = X86::LD_Fp064;
2137 RC = X86::RFP64RegisterClass;
2138 }
2139 break;
2140 case MVT::f80:
2141 // No f80 support yet.
2142 return false;
2143 }
2144
2145 unsigned ResultReg = createResultReg(RC);
2146 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2147 return ResultReg;
2148}
2149
2150
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002151/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2152/// vreg is being provided by the specified load instruction. If possible,
2153/// try to fold the load as an operand to the instruction, returning true if
2154/// possible.
2155bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2156 const LoadInst *LI) {
2157 X86AddressMode AM;
2158 if (!X86SelectAddress(LI->getOperand(0), AM))
2159 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002160
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002161 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002162
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002163 unsigned Size = TD.getTypeAllocSize(LI->getType());
2164 unsigned Alignment = LI->getAlignment();
2165
2166 SmallVector<MachineOperand, 8> AddrOps;
2167 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002168
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002169 MachineInstr *Result =
2170 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2171 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002172
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002173 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002174 MI->eraseFromParent();
2175 return true;
2176}
2177
2178
Evan Chengc3f44b02008-09-03 00:03:49 +00002179namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00002180 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2181 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002182 }
Dan Gohman99b21822008-08-28 23:21:34 +00002183}