blob: 8c46d7f39e22b7f550e9977ce8af18dda444240a [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 Cheng88e30412008-09-03 01:04:47 +000018#include "X86RegisterInfo.h"
19#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000020#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000021#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000022#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000023#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000025#include "llvm/IntrinsicInst.h"
Jay Foad562b84b2011-04-11 09:35:34 +000026#include "llvm/Operator.h"
Dan Gohman84023e02010-07-10 09:00:22 +000027#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000028#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000029#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000030#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000033#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000035#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000036#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000037using namespace llvm;
38
Chris Lattner087fcf32009-03-08 18:44:31 +000039namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000040
Evan Chengc3f44b02008-09-03 00:03:49 +000041class X86FastISel : public FastISel {
42 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
43 /// make the right decision when generating code for different targets.
44 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000045
46 /// StackPtr - Register used as the stack pointer.
47 ///
48 unsigned StackPtr;
49
Wesley Peckbf17cfa2010-11-23 03:31:01 +000050 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000051 /// floating point ops.
52 /// When SSE is available, use it for f32 operations.
53 /// When SSE2 is available, use it for f64 operations.
54 bool X86ScalarSSEf64;
55 bool X86ScalarSSEf32;
56
Evan Cheng8b19e562008-09-03 06:44:39 +000057public:
Dan Gohmana4160c32010-07-07 16:29:44 +000058 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000059 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000060 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
61 X86ScalarSSEf64 = Subtarget->hasSSE2();
62 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000063 }
Evan Chengc3f44b02008-09-03 00:03:49 +000064
Dan Gohman46510a72010-04-15 01:51:59 +000065 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000066
Chris Lattnerbeac75d2010-09-05 02:18:34 +000067 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
68 /// vreg is being provided by the specified load instruction. If possible,
69 /// try to fold the load as an operand to the instruction, returning true if
70 /// possible.
71 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
72 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000073
Dan Gohman1adf1b02008-08-19 21:45:35 +000074#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000075
76private:
Dan Gohman46510a72010-04-15 01:51:59 +000077 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000078
Owen Andersone50ed302009-08-10 22:56:29 +000079 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Dan Gohman46510a72010-04-15 01:51:59 +000081 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000082 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000083 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000084 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 X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000114
Dan Gohman46510a72010-04-15 01:51:59 +0000115 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
116 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000117
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
129 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
130 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000131 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
133 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000134 }
135
Duncan Sands1440e8b2010-11-03 11:35:31 +0000136 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000137};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000138
Chris Lattner087fcf32009-03-08 18:44:31 +0000139} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000140
Duncan Sands1440e8b2010-11-03 11:35:31 +0000141bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
142 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
143 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000144 // Unhandled type. Halt "fast" selection and bail.
145 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000146
147 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000148 // For now, require SSE/SSE2 for performing floating-point operations,
149 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000151 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000153 return false;
154 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000156 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000157 // We only handle legal types. For example, on x86-32 the instruction
158 // selector contains all of the 64-bit instructions from x86-64,
159 // under the assumption that i64 won't be used if the target doesn't
160 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000162}
163
164#include "X86GenCallingConv.inc"
165
Evan Cheng0de588f2008-09-05 21:00:03 +0000166/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000167/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000168/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000169bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000170 unsigned &ResultReg) {
171 // Get opcode and regclass of the output for the given load instruction.
172 unsigned Opc = 0;
173 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000175 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000176 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000178 Opc = X86::MOV8rm;
179 RC = X86::GR8RegisterClass;
180 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000182 Opc = X86::MOV16rm;
183 RC = X86::GR16RegisterClass;
184 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000185 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000186 Opc = X86::MOV32rm;
187 RC = X86::GR32RegisterClass;
188 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000190 // Must be in x86-64 mode.
191 Opc = X86::MOV64rm;
192 RC = X86::GR64RegisterClass;
193 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000195 if (Subtarget->hasSSE1()) {
196 Opc = X86::MOVSSrm;
197 RC = X86::FR32RegisterClass;
198 } else {
199 Opc = X86::LD_Fp32m;
200 RC = X86::RFP32RegisterClass;
201 }
202 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000204 if (Subtarget->hasSSE2()) {
205 Opc = X86::MOVSDrm;
206 RC = X86::FR64RegisterClass;
207 } else {
208 Opc = X86::LD_Fp64m;
209 RC = X86::RFP64RegisterClass;
210 }
211 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000213 // No f80 support yet.
214 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000215 }
216
217 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000218 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
219 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 return true;
221}
222
Evan Chengf3d4efe2008-09-07 09:09:33 +0000223/// X86FastEmitStore - Emit a machine instruction to store a value Val of
224/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
225/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000226/// i.e. V. Return true if it is possible.
227bool
Owen Andersone50ed302009-08-10 22:56:29 +0000228X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000229 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000230 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000231 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 switch (VT.getSimpleVT().SimpleTy) {
233 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000234 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000235 case MVT::i1: {
236 // Mask out all but lowest bit.
237 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000239 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
240 Val = AndResult;
241 }
242 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 case MVT::i8: Opc = X86::MOV8mr; break;
244 case MVT::i16: Opc = X86::MOV16mr; break;
245 case MVT::i32: Opc = X86::MOV32mr; break;
246 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
247 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000248 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000249 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000251 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000252 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000253 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000254
Dan Gohman84023e02010-07-10 09:00:22 +0000255 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
256 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000257 return true;
258}
259
Dan Gohman46510a72010-04-15 01:51:59 +0000260bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000261 const X86AddressMode &AM) {
262 // Handle 'null' like i32/i64 0.
263 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000264 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000265
Chris Lattner438949a2008-10-15 05:30:52 +0000266 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000267 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000268 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000269 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000271 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000272 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 case MVT::i8: Opc = X86::MOV8mi; break;
274 case MVT::i16: Opc = X86::MOV16mi; break;
275 case MVT::i32: Opc = X86::MOV32mi; break;
276 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000277 // Must be a 32-bit sign extended value.
278 if ((int)CI->getSExtValue() == CI->getSExtValue())
279 Opc = X86::MOV64mi32;
280 break;
281 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000282
Chris Lattner438949a2008-10-15 05:30:52 +0000283 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000284 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
285 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000286 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000287 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000288 return true;
289 }
290 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000291
Chris Lattner438949a2008-10-15 05:30:52 +0000292 unsigned ValReg = getRegForValue(Val);
293 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000294 return false;
295
Chris Lattner438949a2008-10-15 05:30:52 +0000296 return X86FastEmitStore(VT, ValReg, AM);
297}
298
Evan Cheng24e3a902008-09-08 06:35:17 +0000299/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
300/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
301/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000302bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
303 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000304 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000305 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
306 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000307
Owen Andersonac34a002008-09-11 19:44:55 +0000308 if (RR != 0) {
309 ResultReg = RR;
310 return true;
311 } else
312 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000313}
314
Dan Gohman0586d912008-09-10 20:11:02 +0000315/// X86SelectAddress - Attempt to fill in an address from the given value.
316///
Dan Gohman46510a72010-04-15 01:51:59 +0000317bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
318 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000319 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000320 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000321 // Don't walk into other basic blocks; it's possible we haven't
322 // visited them yet, so the instructions may not yet be assigned
323 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000324 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
325 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
326 Opcode = I->getOpcode();
327 U = I;
328 }
Dan Gohman46510a72010-04-15 01:51:59 +0000329 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000330 Opcode = C->getOpcode();
331 U = C;
332 }
Dan Gohman0586d912008-09-10 20:11:02 +0000333
Chris Lattner868ee942010-06-15 19:08:40 +0000334 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
335 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000336 // Fast instruction selection doesn't support the special
337 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000338 return false;
339
Dan Gohman35893082008-09-18 23:23:44 +0000340 switch (Opcode) {
341 default: break;
342 case Instruction::BitCast:
343 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000344 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000345
346 case Instruction::IntToPtr:
347 // Look past no-op inttoptrs.
348 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000349 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000350 break;
Dan Gohman35893082008-09-18 23:23:44 +0000351
352 case Instruction::PtrToInt:
353 // Look past no-op ptrtoints.
354 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000355 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000356 break;
Dan Gohman35893082008-09-18 23:23:44 +0000357
358 case Instruction::Alloca: {
359 // Do static allocas.
360 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000361 DenseMap<const AllocaInst*, int>::iterator SI =
362 FuncInfo.StaticAllocaMap.find(A);
363 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000364 AM.BaseType = X86AddressMode::FrameIndexBase;
365 AM.Base.FrameIndex = SI->second;
366 return true;
367 }
368 break;
Dan Gohman35893082008-09-18 23:23:44 +0000369 }
370
371 case Instruction::Add: {
372 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000373 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000374 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
375 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000376 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000377 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000378 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000379 }
Dan Gohman0586d912008-09-10 20:11:02 +0000380 }
Dan Gohman35893082008-09-18 23:23:44 +0000381 break;
382 }
383
384 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000385 X86AddressMode SavedAM = AM;
386
Dan Gohman35893082008-09-18 23:23:44 +0000387 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000388 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000389 unsigned IndexReg = AM.IndexReg;
390 unsigned Scale = AM.Scale;
391 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000392 // Iterate through the indices, folding what we can. Constants can be
393 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000394 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000395 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000396 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000397 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
398 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000399 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
400 continue;
401 }
402
403 // A array/variable index is always of the form i*S where S is the
404 // constant scale size. See if we can push the scale into immediates.
405 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
406 for (;;) {
407 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
408 // Constant-offset addressing.
409 Disp += CI->getSExtValue() * S;
410 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000411 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000412 if (isa<AddOperator>(Op) &&
413 (!isa<Instruction>(Op) ||
414 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
415 == FuncInfo.MBB) &&
416 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
417 // An add (in the same block) with a constant operand. Fold the
418 // constant.
419 ConstantInt *CI =
420 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
421 Disp += CI->getSExtValue() * S;
422 // Iterate on the other operand.
423 Op = cast<AddOperator>(Op)->getOperand(0);
424 continue;
425 }
426 if (IndexReg == 0 &&
427 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
428 (S == 1 || S == 2 || S == 4 || S == 8)) {
429 // Scaled-index addressing.
430 Scale = S;
431 IndexReg = getRegForGEPIndex(Op).first;
432 if (IndexReg == 0)
433 return false;
434 break;
435 }
436 // Unsupported.
437 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000438 }
439 }
Dan Gohman09aae462008-09-26 20:04:15 +0000440 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000441 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000442 break;
Dan Gohman35893082008-09-18 23:23:44 +0000443 // Ok, the GEP indices were covered by constant-offset and scaled-index
444 // addressing. Update the address state and move on to examining the base.
445 AM.IndexReg = IndexReg;
446 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000447 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000448 if (X86SelectAddress(U->getOperand(0), AM))
449 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000450
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000451 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000452 // our address and just match the value instead of completely failing.
453 AM = SavedAM;
454 break;
Dan Gohman35893082008-09-18 23:23:44 +0000455 unsupported_gep:
456 // Ok, the GEP indices weren't all covered.
457 break;
458 }
459 }
460
461 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000462 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0a1c9972011-04-17 17:47:38 +0000463 // Can't handle alternate code models or TLS yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000464 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000465 return false;
466
Dan Gohman46510a72010-04-15 01:51:59 +0000467 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000468 if (GVar->isThreadLocal())
469 return false;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000470
471 // RIP-relative addresses can't have additional register operands, so if
472 // we've already folded stuff into the addressing mode, just force the
473 // global value into its own register, which we can use as the basereg.
474 if (!Subtarget->isPICStyleRIPRel() ||
475 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
476 // Okay, we've committed to selecting this global. Set up the address.
477 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000478
Chris Lattner0a1c9972011-04-17 17:47:38 +0000479 // Allow the subtarget to classify the global.
480 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000481
Chris Lattner0a1c9972011-04-17 17:47:38 +0000482 // If this reference is relative to the pic base, set it now.
483 if (isGlobalRelativeToPICBase(GVFlags)) {
484 // FIXME: How do we know Base.Reg is free??
485 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000486 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000487
488 // Unless the ABI requires an extra load, return a direct reference to
489 // the global.
490 if (!isGlobalStubReference(GVFlags)) {
491 if (Subtarget->isPICStyleRIPRel()) {
492 // Use rip-relative addressing if we can. Above we verified that the
493 // base and index registers are unused.
494 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
495 AM.Base.Reg = X86::RIP;
496 }
497 AM.GVOpFlags = GVFlags;
498 return true;
499 }
500
501 // Ok, we need to do a load from a stub. If we've already loaded from
502 // this stub, reuse the loaded pointer, otherwise emit the load now.
503 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
504 unsigned LoadReg;
505 if (I != LocalValueMap.end() && I->second != 0) {
506 LoadReg = I->second;
507 } else {
508 // Issue load from stub.
509 unsigned Opc = 0;
510 const TargetRegisterClass *RC = NULL;
511 X86AddressMode StubAM;
512 StubAM.Base.Reg = AM.Base.Reg;
513 StubAM.GV = GV;
514 StubAM.GVOpFlags = GVFlags;
515
516 // Prepare for inserting code in the local-value area.
517 SavePoint SaveInsertPt = enterLocalValueArea();
518
519 if (TLI.getPointerTy() == MVT::i64) {
520 Opc = X86::MOV64rm;
521 RC = X86::GR64RegisterClass;
522
523 if (Subtarget->isPICStyleRIPRel())
524 StubAM.Base.Reg = X86::RIP;
525 } else {
526 Opc = X86::MOV32rm;
527 RC = X86::GR32RegisterClass;
528 }
529
530 LoadReg = createResultReg(RC);
531 MachineInstrBuilder LoadMI =
532 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
533 addFullAddress(LoadMI, StubAM);
534
535 // Ok, back to normal mode.
536 leaveLocalValueArea(SaveInsertPt);
537
538 // Prevent loading GV stub multiple times in same MBB.
539 LocalValueMap[V] = LoadReg;
540 }
541
542 // Now construct the final address. Note that the Disp, Scale,
543 // and Index values may already be set here.
544 AM.Base.Reg = LoadReg;
545 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000546 return true;
547 }
Dan Gohman0586d912008-09-10 20:11:02 +0000548 }
549
Dan Gohman97135e12008-09-26 19:15:30 +0000550 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000551 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000552 if (AM.Base.Reg == 0) {
553 AM.Base.Reg = getRegForValue(V);
554 return AM.Base.Reg != 0;
555 }
556 if (AM.IndexReg == 0) {
557 assert(AM.Scale == 1 && "Scale with no index!");
558 AM.IndexReg = getRegForValue(V);
559 return AM.IndexReg != 0;
560 }
561 }
562
563 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000564}
565
Chris Lattner0aa43de2009-07-10 05:33:42 +0000566/// X86SelectCallAddress - Attempt to fill in an address from the given value.
567///
Dan Gohman46510a72010-04-15 01:51:59 +0000568bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
569 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000570 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000571 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000572 Opcode = I->getOpcode();
573 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000574 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000575 Opcode = C->getOpcode();
576 U = C;
577 }
578
579 switch (Opcode) {
580 default: break;
581 case Instruction::BitCast:
582 // Look past bitcasts.
583 return X86SelectCallAddress(U->getOperand(0), AM);
584
585 case Instruction::IntToPtr:
586 // Look past no-op inttoptrs.
587 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
588 return X86SelectCallAddress(U->getOperand(0), AM);
589 break;
590
591 case Instruction::PtrToInt:
592 // Look past no-op ptrtoints.
593 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
594 return X86SelectCallAddress(U->getOperand(0), AM);
595 break;
596 }
597
598 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000599 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000600 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000601 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000602 return false;
603
604 // RIP-relative addresses can't have additional register operands.
605 if (Subtarget->isPICStyleRIPRel() &&
606 (AM.Base.Reg != 0 || AM.IndexReg != 0))
607 return false;
608
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000609 // Can't handle DLLImport.
610 if (GV->hasDLLImportLinkage())
611 return false;
612
613 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000614 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000615 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000616 return false;
617
618 // Okay, we've committed to selecting this global. Set up the basic address.
619 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000620
Chris Lattnere6c07b52009-07-10 05:45:15 +0000621 // No ABI requires an extra load for anything other than DLLImport, which
622 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000623 if (Subtarget->isPICStyleRIPRel()) {
624 // Use rip-relative addressing if we can. Above we verified that the
625 // base and index registers are unused.
626 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
627 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000628 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000629 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
630 } else if (Subtarget->isPICStyleGOT()) {
631 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000632 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000633
Chris Lattner0aa43de2009-07-10 05:33:42 +0000634 return true;
635 }
636
637 // If all else fails, try to materialize the value in a register.
638 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
639 if (AM.Base.Reg == 0) {
640 AM.Base.Reg = getRegForValue(V);
641 return AM.Base.Reg != 0;
642 }
643 if (AM.IndexReg == 0) {
644 assert(AM.Scale == 1 && "Scale with no index!");
645 AM.IndexReg = getRegForValue(V);
646 return AM.IndexReg != 0;
647 }
648 }
649
650 return false;
651}
652
653
Owen Andersona3971df2008-09-04 07:08:58 +0000654/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000655bool X86FastISel::X86SelectStore(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000656 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000657 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000658 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000659
Dan Gohman0586d912008-09-10 20:11:02 +0000660 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000661 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000662 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000663
Chris Lattner438949a2008-10-15 05:30:52 +0000664 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000665}
666
Dan Gohman84023e02010-07-10 09:00:22 +0000667/// X86SelectRet - Select and emit code to implement ret instructions.
668bool X86FastISel::X86SelectRet(const Instruction *I) {
669 const ReturnInst *Ret = cast<ReturnInst>(I);
670 const Function &F = *I->getParent()->getParent();
671
672 if (!FuncInfo.CanLowerReturn)
673 return false;
674
675 CallingConv::ID CC = F.getCallingConv();
676 if (CC != CallingConv::C &&
677 CC != CallingConv::Fast &&
678 CC != CallingConv::X86_FastCall)
679 return false;
680
681 if (Subtarget->isTargetWin64())
682 return false;
683
684 // Don't handle popping bytes on return for now.
685 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
686 ->getBytesToPopOnReturn() != 0)
687 return 0;
688
689 // fastcc with -tailcallopt is intended to provide a guaranteed
690 // tail call optimization. Fastisel doesn't know how to do that.
691 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
692 return false;
693
694 // Let SDISel handle vararg functions.
695 if (F.isVarArg())
696 return false;
697
698 if (Ret->getNumOperands() > 0) {
699 SmallVector<ISD::OutputArg, 4> Outs;
700 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
701 Outs, TLI);
702
703 // Analyze operands of the call, assigning locations to each operand.
704 SmallVector<CCValAssign, 16> ValLocs;
705 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000706 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000707
708 const Value *RV = Ret->getOperand(0);
709 unsigned Reg = getRegForValue(RV);
710 if (Reg == 0)
711 return false;
712
713 // Only handle a single return value for now.
714 if (ValLocs.size() != 1)
715 return false;
716
717 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000718
Dan Gohman84023e02010-07-10 09:00:22 +0000719 // Don't bother handling odd stuff for now.
720 if (VA.getLocInfo() != CCValAssign::Full)
721 return false;
722 // Only handle register returns for now.
723 if (!VA.isRegLoc())
724 return false;
725 // TODO: For now, don't try to handle cases where getLocInfo()
726 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +0000727 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Dan Gohman84023e02010-07-10 09:00:22 +0000728 return false;
729
730 // The calling-convention tables for x87 returns don't tell
731 // the whole story.
732 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
733 return false;
734
735 // Make the copy.
736 unsigned SrcReg = Reg + VA.getValNo();
737 unsigned DstReg = VA.getLocReg();
738 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000739 // Avoid a cross-class copy. This is very unlikely.
740 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000741 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000742 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
743 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000744
745 // Mark the register as live out of the function.
746 MRI.addLiveOut(VA.getLocReg());
747 }
748
749 // Now emit the RET.
750 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
751 return true;
752}
753
Evan Cheng8b19e562008-09-03 06:44:39 +0000754/// X86SelectLoad - Select and emit code to implement load instructions.
755///
Dan Gohman46510a72010-04-15 01:51:59 +0000756bool X86FastISel::X86SelectLoad(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000757 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000758 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000759 return false;
760
Dan Gohman0586d912008-09-10 20:11:02 +0000761 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000762 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000763 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000764
Evan Cheng0de588f2008-09-05 21:00:03 +0000765 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000766 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000767 UpdateValueMap(I, ResultReg);
768 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000769 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000770 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000771}
772
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000773static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000775 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 case MVT::i8: return X86::CMP8rr;
777 case MVT::i16: return X86::CMP16rr;
778 case MVT::i32: return X86::CMP32rr;
779 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000780 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
781 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000782 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000783}
784
Chris Lattner0e13c782008-10-15 04:13:29 +0000785/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
786/// of the comparison, return an opcode that works for the compare (e.g.
787/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000788static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000790 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000791 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 case MVT::i8: return X86::CMP8ri;
793 case MVT::i16: return X86::CMP16ri;
794 case MVT::i32: return X86::CMP32ri;
795 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000796 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
797 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000798 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000799 return X86::CMP64ri32;
800 return 0;
801 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000802}
803
Dan Gohman46510a72010-04-15 01:51:59 +0000804bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
805 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000806 unsigned Op0Reg = getRegForValue(Op0);
807 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000808
Chris Lattnerd53886b2008-10-15 05:18:04 +0000809 // Handle 'null' like i32/i64 0.
810 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000811 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000812
Chris Lattner9a08a612008-10-15 04:26:38 +0000813 // We have two options: compare with register or immediate. If the RHS of
814 // the compare is an immediate that we can fold into this compare, use
815 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000816 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000817 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000818 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
819 .addReg(Op0Reg)
820 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000821 return true;
822 }
823 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000824
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000825 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000826 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000827
Chris Lattner9a08a612008-10-15 04:26:38 +0000828 unsigned Op1Reg = getRegForValue(Op1);
829 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000830 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
831 .addReg(Op0Reg)
832 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000833
Chris Lattner9a08a612008-10-15 04:26:38 +0000834 return true;
835}
836
Dan Gohman46510a72010-04-15 01:51:59 +0000837bool X86FastISel::X86SelectCmp(const Instruction *I) {
838 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000839
Duncan Sands1440e8b2010-11-03 11:35:31 +0000840 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000841 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000842 return false;
843
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000844 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000845 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000846 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000847 switch (CI->getPredicate()) {
848 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000849 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
850 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000851
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000852 unsigned EReg = createResultReg(&X86::GR8RegClass);
853 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000854 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
856 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000857 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000858 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000859 UpdateValueMap(I, ResultReg);
860 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000861 }
862 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000863 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
864 return false;
865
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000866 unsigned NEReg = createResultReg(&X86::GR8RegClass);
867 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000868 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
869 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
870 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000871 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000872 UpdateValueMap(I, ResultReg);
873 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000874 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000875 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
876 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
877 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
878 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
879 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
880 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
881 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
882 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
883 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
884 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
885 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
886 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000887
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000888 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
889 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
890 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
891 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
892 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
893 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
894 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
895 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
896 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
897 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000898 default:
899 return false;
900 }
901
Dan Gohman46510a72010-04-15 01:51:59 +0000902 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000903 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000904 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000905
Chris Lattner9a08a612008-10-15 04:26:38 +0000906 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000907 if (!X86FastEmitCompare(Op0, Op1, VT))
908 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000909
Dan Gohman84023e02010-07-10 09:00:22 +0000910 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000911 UpdateValueMap(I, ResultReg);
912 return true;
913}
Evan Cheng8b19e562008-09-03 06:44:39 +0000914
Dan Gohman46510a72010-04-15 01:51:59 +0000915bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000916 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000917 if (I->getType()->isIntegerTy(8) &&
918 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000919 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000920 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000921 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000922 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000923 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000924 UpdateValueMap(I, ResultReg);
925 return true;
926 }
927
928 return false;
929}
930
Chris Lattner9a08a612008-10-15 04:26:38 +0000931
Dan Gohman46510a72010-04-15 01:51:59 +0000932bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000933 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000934 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000935 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000936 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
937 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000938
Dan Gohman8bef7442010-08-21 02:32:36 +0000939 // Fold the common case of a conditional branch with a comparison
940 // in the same block (values defined on other blocks may not have
941 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000942 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000943 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000944 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000945
Dan Gohmand98d6202008-10-02 22:15:21 +0000946 // Try to take advantage of fallthrough opportunities.
947 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000948 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000949 std::swap(TrueMBB, FalseMBB);
950 Predicate = CmpInst::getInversePredicate(Predicate);
951 }
952
Chris Lattner871d2462008-10-15 03:58:05 +0000953 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
954 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
955
Dan Gohmand98d6202008-10-02 22:15:21 +0000956 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000957 case CmpInst::FCMP_OEQ:
958 std::swap(TrueMBB, FalseMBB);
959 Predicate = CmpInst::FCMP_UNE;
960 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000961 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
962 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
963 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
964 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
965 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
966 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
967 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
968 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
969 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
970 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
971 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
972 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
973 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000974
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000975 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
976 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
977 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
978 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
979 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
980 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
981 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
982 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
983 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
984 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000985 default:
986 return false;
987 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000988
Dan Gohman46510a72010-04-15 01:51:59 +0000989 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000990 if (SwapArgs)
991 std::swap(Op0, Op1);
992
Chris Lattner9a08a612008-10-15 04:26:38 +0000993 // Emit a compare of the LHS and RHS, setting the flags.
994 if (!X86FastEmitCompare(Op0, Op1, VT))
995 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000996
Dan Gohman84023e02010-07-10 09:00:22 +0000997 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
998 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000999
1000 if (Predicate == CmpInst::FCMP_UNE) {
1001 // X86 requires a second branch to handle UNE (and OEQ,
1002 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001003 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1004 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001005 }
1006
Stuart Hastings3bf91252010-06-17 22:43:56 +00001007 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001008 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001009 return true;
1010 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001011 } else if (ExtractValueInst *EI =
1012 dyn_cast<ExtractValueInst>(BI->getCondition())) {
1013 // Check to see if the branch instruction is from an "arithmetic with
1014 // overflow" intrinsic. The main way these intrinsics are used is:
1015 //
1016 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1017 // %sum = extractvalue { i32, i1 } %t, 0
1018 // %obit = extractvalue { i32, i1 } %t, 1
1019 // br i1 %obit, label %overflow, label %normal
1020 //
Dan Gohman653456c2009-01-07 00:15:08 +00001021 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +00001022 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +00001023 // looking for the SETO/SETB instruction. If an instruction modifies the
1024 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1025 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001026 if (const IntrinsicInst *CI =
1027 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001028 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1029 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1030 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001031 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001032
Chris Lattnera9a42252009-04-12 07:36:01 +00001033 for (MachineBasicBlock::const_reverse_iterator
Dan Gohman84023e02010-07-10 09:00:22 +00001034 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1035 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001036 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001037
Evan Cheng1015ba72010-05-21 20:53:24 +00001038 if (MI.definesRegister(Reg)) {
Jakob Stoklund Olesen84d499a2010-07-16 22:35:34 +00001039 if (MI.isCopy()) {
1040 Reg = MI.getOperand(1).getReg();
Chris Lattnera9a42252009-04-12 07:36:01 +00001041 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001042 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001043
Chris Lattnera9a42252009-04-12 07:36:01 +00001044 SetMI = &MI;
1045 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001046 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001047
Chris Lattnera9a42252009-04-12 07:36:01 +00001048 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengc36b7062011-01-07 23:50:32 +00001049 if (TID.hasImplicitDefOfPhysReg(X86::EFLAGS) ||
1050 MI.hasUnmodeledSideEffects())
Chris Lattnera9a42252009-04-12 07:36:01 +00001051 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001052 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001053
1054 if (SetMI) {
1055 unsigned OpCode = SetMI->getOpcode();
1056
1057 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohman84023e02010-07-10 09:00:22 +00001058 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1059 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001060 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001061 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001062 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001063 return true;
1064 }
Bill Wendling9a901322008-12-10 19:44:24 +00001065 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001066 }
1067 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001068 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1069 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1070 // typically happen for _Bool and C++ bools.
1071 MVT SourceVT;
1072 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1073 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1074 unsigned TestOpc = 0;
1075 switch (SourceVT.SimpleTy) {
1076 default: break;
1077 case MVT::i8: TestOpc = X86::TEST8ri; break;
1078 case MVT::i16: TestOpc = X86::TEST16ri; break;
1079 case MVT::i32: TestOpc = X86::TEST32ri; break;
1080 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1081 }
1082 if (TestOpc) {
1083 unsigned OpReg = getRegForValue(TI->getOperand(0));
1084 if (OpReg == 0) return false;
1085 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1086 .addReg(OpReg).addImm(1);
Chris Lattnerc76d1212011-04-19 04:26:32 +00001087
1088 unsigned JmpOpc = X86::JNE_4;
1089 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1090 std::swap(TrueMBB, FalseMBB);
1091 JmpOpc = X86::JE_4;
1092 }
1093
1094 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001095 .addMBB(TrueMBB);
1096 FastEmitBranch(FalseMBB, DL);
1097 FuncInfo.MBB->addSuccessor(TrueMBB);
1098 return true;
1099 }
1100 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001101 }
1102
1103 // Otherwise do a clumsy setcc and re-test it.
1104 unsigned OpReg = getRegForValue(BI->getCondition());
1105 if (OpReg == 0) return false;
1106
Dan Gohman84023e02010-07-10 09:00:22 +00001107 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1108 .addReg(OpReg).addReg(OpReg);
1109 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1110 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001111 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001112 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001113 return true;
1114}
1115
Dan Gohman46510a72010-04-15 01:51:59 +00001116bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001117 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001118 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001119 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001120 CReg = X86::CL;
1121 RC = &X86::GR8RegClass;
1122 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001123 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1124 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1125 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001126 default: return false;
1127 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001128 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001129 CReg = X86::CX;
1130 RC = &X86::GR16RegClass;
1131 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001132 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1133 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1134 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001135 default: return false;
1136 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001137 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001138 CReg = X86::ECX;
1139 RC = &X86::GR32RegClass;
1140 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001141 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1142 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1143 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001144 default: return false;
1145 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001146 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001147 CReg = X86::RCX;
1148 RC = &X86::GR64RegClass;
1149 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001150 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1151 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1152 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001153 default: return false;
1154 }
1155 } else {
1156 return false;
1157 }
1158
Duncan Sands1440e8b2010-11-03 11:35:31 +00001159 MVT VT;
1160 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001161 return false;
1162
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001163 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1164 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001165
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001166 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1167 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001168 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1169 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001170
1171 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001172 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001173 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001174 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1175 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001176 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001177
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001178 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001179 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1180 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001181 UpdateValueMap(I, ResultReg);
1182 return true;
1183}
1184
Dan Gohman46510a72010-04-15 01:51:59 +00001185bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001186 MVT VT;
1187 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001188 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001189
Eric Christophere487b012010-09-29 23:00:29 +00001190 // We only use cmov here, if we don't have a cmov instruction bail.
1191 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001192
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001193 unsigned Opc = 0;
1194 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001195 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001196 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001197 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001198 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001199 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001200 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001201 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001202 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001203 RC = &X86::GR64RegClass;
1204 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001205 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001206 }
1207
1208 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1209 if (Op0Reg == 0) return false;
1210 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1211 if (Op1Reg == 0) return false;
1212 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1213 if (Op2Reg == 0) return false;
1214
Dan Gohman84023e02010-07-10 09:00:22 +00001215 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1216 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001217 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001218 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1219 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001220 UpdateValueMap(I, ResultReg);
1221 return true;
1222}
1223
Dan Gohman46510a72010-04-15 01:51:59 +00001224bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001225 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001226 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001227 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001228 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001229 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001230 unsigned OpReg = getRegForValue(V);
1231 if (OpReg == 0) return false;
1232 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001233 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1234 TII.get(X86::CVTSS2SDrr), ResultReg)
1235 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001236 UpdateValueMap(I, ResultReg);
1237 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001238 }
1239 }
1240
1241 return false;
1242}
1243
Dan Gohman46510a72010-04-15 01:51:59 +00001244bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001245 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001246 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001247 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001248 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001249 unsigned OpReg = getRegForValue(V);
1250 if (OpReg == 0) return false;
1251 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001252 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1253 TII.get(X86::CVTSD2SSrr), ResultReg)
1254 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001255 UpdateValueMap(I, ResultReg);
1256 return true;
1257 }
1258 }
1259 }
1260
1261 return false;
1262}
1263
Dan Gohman46510a72010-04-15 01:51:59 +00001264bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001265 if (Subtarget->is64Bit())
1266 // All other cases should be handled by the tblgen generated code.
1267 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001268 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1269 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001270
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001271 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001273 // All other cases should be handled by the tblgen generated code.
1274 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001275 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001276 // All other cases should be handled by the tblgen generated code.
1277 return false;
1278
1279 unsigned InputReg = getRegForValue(I->getOperand(0));
1280 if (!InputReg)
1281 // Unhandled operand. Halt "fast" selection and bail.
1282 return false;
1283
Dan Gohman62417622009-04-27 16:33:14 +00001284 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001285 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001286 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001287 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1289 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001290
1291 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001293 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001294 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001295 if (!ResultReg)
1296 return false;
1297
1298 UpdateValueMap(I, ResultReg);
1299 return true;
1300}
1301
Dan Gohman46510a72010-04-15 01:51:59 +00001302bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1303 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1304 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001305
Dan Gohman46510a72010-04-15 01:51:59 +00001306 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001307 switch (CI->getIntrinsicID()) {
1308 default: break;
1309 case Intrinsic::sadd_with_overflow:
Dan Gohman84023e02010-07-10 09:00:22 +00001310 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001311 // Cheat a little. We know that the registers for "add" and "seto" are
1312 // allocated sequentially. However, we only keep track of the register
1313 // for "add" in the value map. Use extractvalue's index to get the
1314 // correct register for "seto".
Dan Gohman84023e02010-07-10 09:00:22 +00001315 unsigned OpReg = getRegForValue(Agg);
1316 if (OpReg == 0)
1317 return false;
1318 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001319 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001320 }
Dan Gohman84023e02010-07-10 09:00:22 +00001321 }
Bill Wendling52370a12008-12-09 02:42:50 +00001322 }
1323
1324 return false;
1325}
1326
Dan Gohman46510a72010-04-15 01:51:59 +00001327bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001328 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001329 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001330 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001331 case Intrinsic::stackprotector: {
1332 // Emit code inline code to store the stack guard onto the stack.
1333 EVT PtrTy = TLI.getPointerTy();
1334
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001335 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1336 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001337
1338 // Grab the frame index.
1339 X86AddressMode AM;
1340 if (!X86SelectAddress(Slot, AM)) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001341
Eric Christopher88dee302010-03-18 21:58:33 +00001342 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001343
Eric Christopher07754c22010-03-18 20:27:26 +00001344 return true;
1345 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001346 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001347 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001348 const Type *Ty = I.getCalledFunction()->getReturnType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001349
Eric Christopherf27805b2010-03-11 06:20:22 +00001350 assert(CI && "Non-constant type in Intrinsic::objectsize?");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001351
Duncan Sands1440e8b2010-11-03 11:35:31 +00001352 MVT VT;
Eric Christopherf27805b2010-03-11 06:20:22 +00001353 if (!isTypeLegal(Ty, VT))
1354 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001355
Eric Christopherf27805b2010-03-11 06:20:22 +00001356 unsigned OpC = 0;
1357 if (VT == MVT::i32)
1358 OpC = X86::MOV32ri;
1359 else if (VT == MVT::i64)
1360 OpC = X86::MOV64ri;
1361 else
1362 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001363
Eric Christopherf27805b2010-03-11 06:20:22 +00001364 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001365 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001366 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001367 UpdateValueMap(&I, ResultReg);
1368 return true;
1369 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001370 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001371 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001372 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001373 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001374 if (!X86SelectAddress(DI->getAddress(), AM))
1375 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001376 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001377 // FIXME may need to add RegState::Debug to any registers produced,
1378 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001379 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1380 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001381 return true;
1382 }
Eric Christopher77f79892010-01-18 22:11:29 +00001383 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001384 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001385 return true;
1386 }
Bill Wendling52370a12008-12-09 02:42:50 +00001387 case Intrinsic::sadd_with_overflow:
1388 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001389 // Replace "add with overflow" intrinsics with an "add" instruction followed
1390 // by a seto/setc instruction. Later on, when the "extractvalue"
1391 // instructions are encountered, we use the fact that two registers were
1392 // created sequentially to get the correct registers for the "sum" and the
1393 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001394 const Function *Callee = I.getCalledFunction();
1395 const Type *RetTy =
1396 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1397
Duncan Sands1440e8b2010-11-03 11:35:31 +00001398 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001399 if (!isTypeLegal(RetTy, VT))
1400 return false;
1401
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001402 const Value *Op1 = I.getArgOperand(0);
1403 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001404 unsigned Reg1 = getRegForValue(Op1);
1405 unsigned Reg2 = getRegForValue(Op2);
1406
1407 if (Reg1 == 0 || Reg2 == 0)
1408 // FIXME: Handle values *not* in registers.
1409 return false;
1410
1411 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001412 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001413 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001414 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001415 OpC = X86::ADD64rr;
1416 else
1417 return false;
1418
1419 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001420 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1421 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001422 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001423
Chris Lattner8d57b772009-04-12 07:51:14 +00001424 // If the add with overflow is an intra-block value then we just want to
1425 // create temporaries for it like normal. If it is a cross-block value then
1426 // UpdateValueMap will return the cross-block register used. Since we
1427 // *really* want the value to be live in the register pair known by
1428 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1429 // the cross block case. In the non-cross-block case, we should just make
1430 // another register for the value.
1431 if (DestReg1 != ResultReg)
1432 ResultReg = DestReg1+1;
1433 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001435
Chris Lattnera9a42252009-04-12 07:36:01 +00001436 unsigned Opc = X86::SETBr;
1437 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1438 Opc = X86::SETOr;
Dan Gohman84023e02010-07-10 09:00:22 +00001439 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001440 return true;
1441 }
1442 }
1443}
1444
Dan Gohman46510a72010-04-15 01:51:59 +00001445bool X86FastISel::X86SelectCall(const Instruction *I) {
1446 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001447 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001448
1449 // Can't handle inline asm yet.
1450 if (isa<InlineAsm>(Callee))
1451 return false;
1452
Bill Wendling52370a12008-12-09 02:42:50 +00001453 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001454 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001455 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001456
Evan Chengf3d4efe2008-09-07 09:09:33 +00001457 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001458 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001459 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001460 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001461 CC != CallingConv::X86_FastCall)
1462 return false;
1463
Evan Cheng381993f2010-01-27 00:00:57 +00001464 // fastcc with -tailcallopt is intended to provide a guaranteed
1465 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001466 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001467 return false;
1468
Evan Chengf3d4efe2008-09-07 09:09:33 +00001469 // Let SDISel handle vararg functions.
1470 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1471 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1472 if (FTy->isVarArg())
1473 return false;
1474
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001475 // Fast-isel doesn't know about callee-pop yet.
1476 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1477 return false;
1478
Evan Chengf3d4efe2008-09-07 09:09:33 +00001479 // Handle *simple* calls for now.
1480 const Type *RetTy = CS.getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001481 MVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001482 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001483 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001484 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001485 return false;
1486
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001487 // Materialize callee address in a register. FIXME: GV address can be
1488 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001489 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001490 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001491 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001492 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001493 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001494 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001495 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001496 } else if (CalleeAM.Base.Reg != 0) {
1497 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001498 } else
1499 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001500
Evan Chengdebdea02008-09-08 17:15:42 +00001501 // Allow calls which produce i1 results.
1502 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001503 if (RetVT == MVT::i1) {
1504 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001505 AndToI1 = true;
1506 }
1507
Evan Chengf3d4efe2008-09-07 09:09:33 +00001508 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001509 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001510 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001511 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001512 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001513 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001514 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001515 ArgVTs.reserve(CS.arg_size());
1516 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001517 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001518 i != e; ++i) {
Chris Lattnere03b8d32011-04-19 04:42:38 +00001519 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001520 ISD::ArgFlagsTy Flags;
1521 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001522 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001523 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001524 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001525 Flags.setZExt();
1526
Chris Lattnere03b8d32011-04-19 04:42:38 +00001527 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1528 // instruction. This is safe because it is common to all fastisel supported
1529 // calling conventions on x86.
1530 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1531 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1532 CI->getBitWidth() == 16) {
1533 if (Flags.isSExt())
1534 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1535 else
1536 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1537 }
1538 }
1539
1540 unsigned Arg = getRegForValue(ArgVal);
1541 if (Arg == 0)
1542 return false;
1543
Evan Chengf3d4efe2008-09-07 09:09:33 +00001544 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001545 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1546 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1547 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1548 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001549 return false;
1550
Chris Lattnere03b8d32011-04-19 04:42:38 +00001551 const Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001552 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001553 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001554 return false;
1555 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1556 Flags.setOrigAlign(OriginalAlignment);
1557
1558 Args.push_back(Arg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001559 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001560 ArgVTs.push_back(ArgVT);
1561 ArgFlags.push_back(Flags);
1562 }
1563
1564 // Analyze operands of the call, assigning locations to each operand.
1565 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001566 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001567
Dan Gohmand8acddd2010-06-01 21:09:47 +00001568 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001569 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001570 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001571
Duncan Sands45907662010-10-31 13:21:44 +00001572 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001573
1574 // Get a count of how many bytes are to be pushed on the stack.
1575 unsigned NumBytes = CCInfo.getNextStackOffset();
1576
1577 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001578 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001579 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1580 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001581
Chris Lattner438949a2008-10-15 05:30:52 +00001582 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001583 // copies / loads.
1584 SmallVector<unsigned, 4> RegArgs;
1585 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1586 CCValAssign &VA = ArgLocs[i];
1587 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001588 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001589
Evan Chengf3d4efe2008-09-07 09:09:33 +00001590 // Promote the value if needed.
1591 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001592 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001593 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001594 case CCValAssign::SExt: {
1595 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1596 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001597 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001598 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001599 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001600 }
1601 case CCValAssign::ZExt: {
1602 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1603 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001604 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001605 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001606 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001607 }
1608 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001609 // We don't handle MMX parameters yet.
1610 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1611 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001612 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1613 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001614 if (!Emitted)
1615 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001616 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001617 if (!Emitted)
1618 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1619 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001620
Chris Lattnerc46ec642011-01-05 22:26:52 +00001621 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001622 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001623 break;
1624 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001625 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001626 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001627 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001628 assert(BC != 0 && "Failed to emit a bitcast!");
1629 Arg = BC;
1630 ArgVT = VA.getLocVT();
1631 break;
1632 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001633 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001634
Evan Chengf3d4efe2008-09-07 09:09:33 +00001635 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001636 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1637 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001638 RegArgs.push_back(VA.getLocReg());
1639 } else {
1640 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001641 X86AddressMode AM;
1642 AM.Base.Reg = StackPtr;
1643 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001644 const Value *ArgVal = ArgVals[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001645
Chris Lattner241ab472008-10-15 05:38:32 +00001646 // If this is a really simple value, emit this with the Value* version of
1647 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1648 // can cause us to reevaluate the argument.
1649 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1650 X86FastEmitStore(ArgVT, ArgVal, AM);
1651 else
1652 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001653 }
1654 }
1655
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001656 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001657 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001658 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001659 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001660 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1661 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001662 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001663
Evan Chengf3d4efe2008-09-07 09:09:33 +00001664 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001665 MachineInstrBuilder MIB;
1666 if (CalleeOp) {
1667 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001668 unsigned CallOpc;
1669 if (Subtarget->isTargetWin64())
1670 CallOpc = X86::WINCALL64r;
1671 else if (Subtarget->is64Bit())
1672 CallOpc = X86::CALL64r;
1673 else
1674 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001675 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1676 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001677
Chris Lattner51e8eab2009-07-09 06:34:26 +00001678 } else {
1679 // Direct call.
1680 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001681 unsigned CallOpc;
1682 if (Subtarget->isTargetWin64())
1683 CallOpc = X86::WINCALL64pcrel32;
1684 else if (Subtarget->is64Bit())
1685 CallOpc = X86::CALL64pcrel32;
1686 else
1687 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001688
Chris Lattner51e8eab2009-07-09 06:34:26 +00001689 // See if we need any target-specific flags on the GV operand.
1690 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001691
Chris Lattner51e8eab2009-07-09 06:34:26 +00001692 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1693 // external symbols most go through the PLT in PIC mode. If the symbol
1694 // has hidden or protected visibility, or if it is static or local, then
1695 // we don't need to use the PLT - we can directly call it.
1696 if (Subtarget->isTargetELF() &&
1697 TM.getRelocationModel() == Reloc::PIC_ &&
1698 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1699 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001700 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001701 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1702 Subtarget->getDarwinVers() < 9) {
1703 // PC-relative references to external symbols should go through $stub,
1704 // unless we're building with the leopard linker or later, which
1705 // automatically synthesizes these stubs.
1706 OpFlags = X86II::MO_DARWIN_STUB;
1707 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001708
1709
Dan Gohman84023e02010-07-10 09:00:22 +00001710 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1711 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001712 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001713
1714 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001715 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001716 MIB.addReg(X86::EBX);
1717
Evan Chengf3d4efe2008-09-07 09:09:33 +00001718 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001719 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1720 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001721
1722 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001723 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001724 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1725 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001726
1727 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001728 SmallVector<unsigned, 4> UsedRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001729 if (RetVT != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001730 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001731 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001732 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1733
1734 // Copy all of the result registers out of their specified physreg.
1735 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001736 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001737 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001738
Evan Chengf3d4efe2008-09-07 09:09:33 +00001739 // If this is a call to a function that returns an fp value on the x87 fp
1740 // stack, but where we prefer to use the value in xmm registers, copy it
1741 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1742 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1743 RVLocs[0].getLocReg() == X86::ST1) &&
1744 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001745 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001746 DstRC = X86::RFP80RegisterClass;
1747 }
1748
1749 unsigned ResultReg = createResultReg(DstRC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001750 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1751 ResultReg).addReg(RVLocs[0].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001752 UsedRegs.push_back(RVLocs[0].getLocReg());
1753
Evan Chengf3d4efe2008-09-07 09:09:33 +00001754 if (CopyVT != RVLocs[0].getValVT()) {
1755 // Round the F80 the right size, which also moves to the appropriate xmm
1756 // register. This is accomplished by storing the F80 value in memory and
1757 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001758 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001760 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001761 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001762 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1763 TII.get(Opc)), FI)
1764 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001766 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001768 ResultReg = createResultReg(DstRC);
Dan Gohman84023e02010-07-10 09:00:22 +00001769 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1770 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001771 }
1772
Evan Chengdebdea02008-09-08 17:15:42 +00001773 if (AndToI1) {
1774 // Mask out all but lowest bit for some call which produces an i1.
1775 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001776 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001777 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001778 ResultReg = AndResult;
1779 }
1780
Evan Chengf3d4efe2008-09-07 09:09:33 +00001781 UpdateValueMap(I, ResultReg);
1782 }
1783
Dan Gohmandb497122010-06-18 23:28:01 +00001784 // Set all unused physreg defs as dead.
1785 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1786
Evan Chengf3d4efe2008-09-07 09:09:33 +00001787 return true;
1788}
1789
1790
Dan Gohman99b21822008-08-28 23:21:34 +00001791bool
Dan Gohman46510a72010-04-15 01:51:59 +00001792X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001793 switch (I->getOpcode()) {
1794 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001795 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001796 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001797 case Instruction::Store:
1798 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001799 case Instruction::Ret:
1800 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001801 case Instruction::ICmp:
1802 case Instruction::FCmp:
1803 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001804 case Instruction::ZExt:
1805 return X86SelectZExt(I);
1806 case Instruction::Br:
1807 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001808 case Instruction::Call:
1809 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001810 case Instruction::LShr:
1811 case Instruction::AShr:
1812 case Instruction::Shl:
1813 return X86SelectShift(I);
1814 case Instruction::Select:
1815 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001816 case Instruction::Trunc:
1817 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001818 case Instruction::FPExt:
1819 return X86SelectFPExt(I);
1820 case Instruction::FPTrunc:
1821 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001822 case Instruction::ExtractValue:
1823 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001824 case Instruction::IntToPtr: // Deliberate fall-through.
1825 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001826 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1827 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001828 if (DstVT.bitsGT(SrcVT))
1829 return X86SelectZExt(I);
1830 if (DstVT.bitsLT(SrcVT))
1831 return X86SelectTrunc(I);
1832 unsigned Reg = getRegForValue(I->getOperand(0));
1833 if (Reg == 0) return false;
1834 UpdateValueMap(I, Reg);
1835 return true;
1836 }
Dan Gohman99b21822008-08-28 23:21:34 +00001837 }
1838
1839 return false;
1840}
1841
Dan Gohman46510a72010-04-15 01:51:59 +00001842unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001843 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001844 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001845 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001846
Owen Anderson95267a12008-09-05 00:06:23 +00001847 // Get opcode and regclass of the output for the given load instruction.
1848 unsigned Opc = 0;
1849 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001850 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001851 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001852 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001853 Opc = X86::MOV8rm;
1854 RC = X86::GR8RegisterClass;
1855 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001857 Opc = X86::MOV16rm;
1858 RC = X86::GR16RegisterClass;
1859 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001861 Opc = X86::MOV32rm;
1862 RC = X86::GR32RegisterClass;
1863 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001865 // Must be in x86-64 mode.
1866 Opc = X86::MOV64rm;
1867 RC = X86::GR64RegisterClass;
1868 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001869 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001870 if (Subtarget->hasSSE1()) {
1871 Opc = X86::MOVSSrm;
1872 RC = X86::FR32RegisterClass;
1873 } else {
1874 Opc = X86::LD_Fp32m;
1875 RC = X86::RFP32RegisterClass;
1876 }
1877 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001878 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001879 if (Subtarget->hasSSE2()) {
1880 Opc = X86::MOVSDrm;
1881 RC = X86::FR64RegisterClass;
1882 } else {
1883 Opc = X86::LD_Fp64m;
1884 RC = X86::RFP64RegisterClass;
1885 }
1886 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001888 // No f80 support yet.
1889 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001890 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001891
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001892 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001893 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001894 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001895 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00001896 // If the expression is just a basereg, then we're done, otherwise we need
1897 // to emit an LEA.
1898 if (AM.BaseType == X86AddressMode::RegBase &&
1899 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
1900 return AM.Base.Reg;
1901
1902 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001903 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001904 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1905 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001906 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001907 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001908 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001909 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001910
Owen Anderson3b217c62008-09-06 01:11:01 +00001911 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001912 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001913 if (Align == 0) {
1914 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001915 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001916 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001917
Dan Gohman5396c992008-09-30 01:21:32 +00001918 // x86-32 PIC requires a PIC base register for constant pools.
1919 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001920 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001921 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001922 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001923 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001924 } else if (Subtarget->isPICStyleGOT()) {
1925 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001926 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001927 } else if (Subtarget->isPICStyleRIPRel() &&
1928 TM.getCodeModel() == CodeModel::Small) {
1929 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001930 }
Dan Gohman5396c992008-09-30 01:21:32 +00001931
1932 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001933 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001934 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001935 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1936 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001937 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001938
Owen Anderson95267a12008-09-05 00:06:23 +00001939 return ResultReg;
1940}
1941
Dan Gohman46510a72010-04-15 01:51:59 +00001942unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001943 // Fail on dynamic allocas. At this point, getRegForValue has already
1944 // checked its CSE maps, so if we're here trying to handle a dynamic
1945 // alloca, we're not going to succeed. X86SelectAddress has a
1946 // check for dynamic allocas, because it's called directly from
1947 // various places, but TargetMaterializeAlloca also needs a check
1948 // in order to avoid recursion between getRegForValue,
1949 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001950 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001951 return 0;
1952
Dan Gohman0586d912008-09-10 20:11:02 +00001953 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001954 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001955 return 0;
1956 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1957 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1958 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001959 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1960 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001961 return ResultReg;
1962}
1963
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001964/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
1965/// vreg is being provided by the specified load instruction. If possible,
1966/// try to fold the load as an operand to the instruction, returning true if
1967/// possible.
1968bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
1969 const LoadInst *LI) {
1970 X86AddressMode AM;
1971 if (!X86SelectAddress(LI->getOperand(0), AM))
1972 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001973
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001974 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001975
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001976 unsigned Size = TD.getTypeAllocSize(LI->getType());
1977 unsigned Alignment = LI->getAlignment();
1978
1979 SmallVector<MachineOperand, 8> AddrOps;
1980 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001981
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001982 MachineInstr *Result =
1983 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
1984 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001985
Chris Lattnerb99fdee2011-01-16 02:27:38 +00001986 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001987 MI->eraseFromParent();
1988 return true;
1989}
1990
1991
Evan Chengc3f44b02008-09-03 00:03:49 +00001992namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001993 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1994 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001995 }
Dan Gohman99b21822008-08-28 23:21:34 +00001996}