blob: 521eb30b7763725726e79e3561e86c313ca2b134 [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);
399 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
400 Disp += SL->getElementOffset(Idx);
401 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000402 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000403 for (;;) {
Dan Gohman5c87bf62010-07-01 02:27:15 +0000404 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
405 // Constant-offset addressing.
406 Disp += CI->getSExtValue() * S;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000407 break;
408 }
409 if (isa<AddOperator>(Op) &&
410 (!isa<Instruction>(Op) ||
411 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
412 == FuncInfo.MBB) &&
413 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
414 // An add (in the same block) with a constant operand. Fold the
415 // constant.
Dan Gohmanabd1d852010-07-01 02:58:21 +0000416 ConstantInt *CI =
417 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
418 Disp += CI->getSExtValue() * S;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000419 // Iterate on the other operand.
420 Op = cast<AddOperator>(Op)->getOperand(0);
421 continue;
422 }
423 if (IndexReg == 0 &&
424 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
425 (S == 1 || S == 2 || S == 4 || S == 8)) {
Dan Gohman5c87bf62010-07-01 02:27:15 +0000426 // Scaled-index addressing.
427 Scale = S;
428 IndexReg = getRegForGEPIndex(Op).first;
429 if (IndexReg == 0)
430 return false;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000431 break;
432 }
433 // Unsupported.
434 goto unsupported_gep;
435 }
Dan Gohman35893082008-09-18 23:23:44 +0000436 }
437 }
Dan Gohman09aae462008-09-26 20:04:15 +0000438 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000439 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000440 break;
Dan Gohman35893082008-09-18 23:23:44 +0000441 // Ok, the GEP indices were covered by constant-offset and scaled-index
442 // addressing. Update the address state and move on to examining the base.
443 AM.IndexReg = IndexReg;
444 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000445 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000446 if (X86SelectAddress(U->getOperand(0), AM))
447 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000448
Chris Lattner225d4ca2010-03-04 19:48:19 +0000449 // If we couldn't merge the sub value into this addr mode, revert back to
450 // our address and just match the value instead of completely failing.
451 AM = SavedAM;
452 break;
Dan Gohman35893082008-09-18 23:23:44 +0000453 unsupported_gep:
454 // Ok, the GEP indices weren't all covered.
455 break;
456 }
457 }
458
459 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000460 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000461 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000462 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000463 return false;
464
Dan Gohman97135e12008-09-26 19:15:30 +0000465 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000466 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000467 (AM.Base.Reg != 0 || AM.IndexReg != 0))
468 return false;
469
Dan Gohmane9865942009-02-23 22:03:08 +0000470 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000471 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000472 if (GVar->isThreadLocal())
473 return false;
474
Chris Lattnerff7727f2009-07-09 06:41:35 +0000475 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000476 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000477
Chris Lattner0d786dd2009-07-10 07:48:51 +0000478 // Allow the subtarget to classify the global.
479 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
480
481 // If this reference is relative to the pic base, set it now.
482 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000483 // FIXME: How do we know Base.Reg is free??
Dan Gohmana4160c32010-07-07 16:29:44 +0000484 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000485 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000486
Chris Lattner0d786dd2009-07-10 07:48:51 +0000487 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000488 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000489 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000490 if (Subtarget->isPICStyleRIPRel()) {
491 // Use rip-relative addressing if we can. Above we verified that the
492 // base and index registers are unused.
493 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
494 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000495 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000496 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000497 return true;
498 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000499
Chris Lattner0d786dd2009-07-10 07:48:51 +0000500 // Ok, we need to do a load from a stub. If we've already loaded from this
501 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000502 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
503 unsigned LoadReg;
504 if (I != LocalValueMap.end() && I->second != 0) {
505 LoadReg = I->second;
506 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000507 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000508 unsigned Opc = 0;
509 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000510 X86AddressMode StubAM;
511 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000512 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000513 StubAM.GVOpFlags = GVFlags;
514
Dan Gohman84023e02010-07-10 09:00:22 +0000515 // Prepare for inserting code in the local-value area.
Dan Gohmana10b8492010-07-14 01:07:44 +0000516 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000517
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000519 Opc = X86::MOV64rm;
520 RC = X86::GR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000521
Chris Lattner0d786dd2009-07-10 07:48:51 +0000522 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000523 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000524 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000525 Opc = X86::MOV32rm;
526 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000527 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000528
Chris Lattnerff7727f2009-07-09 06:41:35 +0000529 LoadReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000530 MachineInstrBuilder LoadMI =
531 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
532 addFullAddress(LoadMI, StubAM);
533
534 // Ok, back to normal mode.
535 leaveLocalValueArea(SaveInsertPt);
536
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000537 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000538 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000539 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000540
Chris Lattnerff7727f2009-07-09 06:41:35 +0000541 // Now construct the final address. Note that the Disp, Scale,
542 // and Index values may already be set here.
543 AM.Base.Reg = LoadReg;
544 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000545 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000546 }
547
Dan Gohman97135e12008-09-26 19:15:30 +0000548 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000549 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000550 if (AM.Base.Reg == 0) {
551 AM.Base.Reg = getRegForValue(V);
552 return AM.Base.Reg != 0;
553 }
554 if (AM.IndexReg == 0) {
555 assert(AM.Scale == 1 && "Scale with no index!");
556 AM.IndexReg = getRegForValue(V);
557 return AM.IndexReg != 0;
558 }
559 }
560
561 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000562}
563
Chris Lattner0aa43de2009-07-10 05:33:42 +0000564/// X86SelectCallAddress - Attempt to fill in an address from the given value.
565///
Dan Gohman46510a72010-04-15 01:51:59 +0000566bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
567 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000568 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000569 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000570 Opcode = I->getOpcode();
571 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000572 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000573 Opcode = C->getOpcode();
574 U = C;
575 }
576
577 switch (Opcode) {
578 default: break;
579 case Instruction::BitCast:
580 // Look past bitcasts.
581 return X86SelectCallAddress(U->getOperand(0), AM);
582
583 case Instruction::IntToPtr:
584 // Look past no-op inttoptrs.
585 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
586 return X86SelectCallAddress(U->getOperand(0), AM);
587 break;
588
589 case Instruction::PtrToInt:
590 // Look past no-op ptrtoints.
591 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
592 return X86SelectCallAddress(U->getOperand(0), AM);
593 break;
594 }
595
596 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000597 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000598 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000599 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000600 return false;
601
602 // RIP-relative addresses can't have additional register operands.
603 if (Subtarget->isPICStyleRIPRel() &&
604 (AM.Base.Reg != 0 || AM.IndexReg != 0))
605 return false;
606
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000607 // Can't handle DLLImport.
608 if (GV->hasDLLImportLinkage())
609 return false;
610
611 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000612 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000613 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000614 return false;
615
616 // Okay, we've committed to selecting this global. Set up the basic address.
617 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000618
Chris Lattnere6c07b52009-07-10 05:45:15 +0000619 // No ABI requires an extra load for anything other than DLLImport, which
620 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000621 if (Subtarget->isPICStyleRIPRel()) {
622 // Use rip-relative addressing if we can. Above we verified that the
623 // base and index registers are unused.
624 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
625 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000626 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000627 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
628 } else if (Subtarget->isPICStyleGOT()) {
629 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000630 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000631
Chris Lattner0aa43de2009-07-10 05:33:42 +0000632 return true;
633 }
634
635 // If all else fails, try to materialize the value in a register.
636 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
637 if (AM.Base.Reg == 0) {
638 AM.Base.Reg = getRegForValue(V);
639 return AM.Base.Reg != 0;
640 }
641 if (AM.IndexReg == 0) {
642 assert(AM.Scale == 1 && "Scale with no index!");
643 AM.IndexReg = getRegForValue(V);
644 return AM.IndexReg != 0;
645 }
646 }
647
648 return false;
649}
650
651
Owen Andersona3971df2008-09-04 07:08:58 +0000652/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000653bool X86FastISel::X86SelectStore(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000654 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000655 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000656 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000657
Dan Gohman0586d912008-09-10 20:11:02 +0000658 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000659 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000660 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000661
Chris Lattner438949a2008-10-15 05:30:52 +0000662 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000663}
664
Dan Gohman84023e02010-07-10 09:00:22 +0000665/// X86SelectRet - Select and emit code to implement ret instructions.
666bool X86FastISel::X86SelectRet(const Instruction *I) {
667 const ReturnInst *Ret = cast<ReturnInst>(I);
668 const Function &F = *I->getParent()->getParent();
669
670 if (!FuncInfo.CanLowerReturn)
671 return false;
672
673 CallingConv::ID CC = F.getCallingConv();
674 if (CC != CallingConv::C &&
675 CC != CallingConv::Fast &&
676 CC != CallingConv::X86_FastCall)
677 return false;
678
679 if (Subtarget->isTargetWin64())
680 return false;
681
682 // Don't handle popping bytes on return for now.
683 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
684 ->getBytesToPopOnReturn() != 0)
685 return 0;
686
687 // fastcc with -tailcallopt is intended to provide a guaranteed
688 // tail call optimization. Fastisel doesn't know how to do that.
689 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
690 return false;
691
692 // Let SDISel handle vararg functions.
693 if (F.isVarArg())
694 return false;
695
696 if (Ret->getNumOperands() > 0) {
697 SmallVector<ISD::OutputArg, 4> Outs;
698 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
699 Outs, TLI);
700
701 // Analyze operands of the call, assigning locations to each operand.
702 SmallVector<CCValAssign, 16> ValLocs;
703 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000704 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000705
706 const Value *RV = Ret->getOperand(0);
707 unsigned Reg = getRegForValue(RV);
708 if (Reg == 0)
709 return false;
710
711 // Only handle a single return value for now.
712 if (ValLocs.size() != 1)
713 return false;
714
715 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000716
Dan Gohman84023e02010-07-10 09:00:22 +0000717 // Don't bother handling odd stuff for now.
718 if (VA.getLocInfo() != CCValAssign::Full)
719 return false;
720 // Only handle register returns for now.
721 if (!VA.isRegLoc())
722 return false;
723 // TODO: For now, don't try to handle cases where getLocInfo()
724 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +0000725 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Dan Gohman84023e02010-07-10 09:00:22 +0000726 return false;
727
728 // The calling-convention tables for x87 returns don't tell
729 // the whole story.
730 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
731 return false;
732
733 // Make the copy.
734 unsigned SrcReg = Reg + VA.getValNo();
735 unsigned DstReg = VA.getLocReg();
736 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000737 // Avoid a cross-class copy. This is very unlikely.
738 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000739 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000740 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
741 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000742
743 // Mark the register as live out of the function.
744 MRI.addLiveOut(VA.getLocReg());
745 }
746
747 // Now emit the RET.
748 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
749 return true;
750}
751
Evan Cheng8b19e562008-09-03 06:44:39 +0000752/// X86SelectLoad - Select and emit code to implement load instructions.
753///
Dan Gohman46510a72010-04-15 01:51:59 +0000754bool X86FastISel::X86SelectLoad(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000755 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000756 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000757 return false;
758
Dan Gohman0586d912008-09-10 20:11:02 +0000759 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000760 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000761 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000762
Evan Cheng0de588f2008-09-05 21:00:03 +0000763 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000764 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000765 UpdateValueMap(I, ResultReg);
766 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000767 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000768 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000769}
770
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000771static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000773 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 case MVT::i8: return X86::CMP8rr;
775 case MVT::i16: return X86::CMP16rr;
776 case MVT::i32: return X86::CMP32rr;
777 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000778 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
779 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000780 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000781}
782
Chris Lattner0e13c782008-10-15 04:13:29 +0000783/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
784/// of the comparison, return an opcode that works for the compare (e.g.
785/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000786static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000788 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000789 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 case MVT::i8: return X86::CMP8ri;
791 case MVT::i16: return X86::CMP16ri;
792 case MVT::i32: return X86::CMP32ri;
793 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000794 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
795 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000796 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000797 return X86::CMP64ri32;
798 return 0;
799 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000800}
801
Dan Gohman46510a72010-04-15 01:51:59 +0000802bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
803 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000804 unsigned Op0Reg = getRegForValue(Op0);
805 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000806
Chris Lattnerd53886b2008-10-15 05:18:04 +0000807 // Handle 'null' like i32/i64 0.
808 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000809 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000810
Chris Lattner9a08a612008-10-15 04:26:38 +0000811 // We have two options: compare with register or immediate. If the RHS of
812 // the compare is an immediate that we can fold into this compare, use
813 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000814 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000815 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000816 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
817 .addReg(Op0Reg)
818 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000819 return true;
820 }
821 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000822
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000823 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000824 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000825
Chris Lattner9a08a612008-10-15 04:26:38 +0000826 unsigned Op1Reg = getRegForValue(Op1);
827 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000828 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
829 .addReg(Op0Reg)
830 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000831
Chris Lattner9a08a612008-10-15 04:26:38 +0000832 return true;
833}
834
Dan Gohman46510a72010-04-15 01:51:59 +0000835bool X86FastISel::X86SelectCmp(const Instruction *I) {
836 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000837
Duncan Sands1440e8b2010-11-03 11:35:31 +0000838 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000839 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000840 return false;
841
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000842 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000843 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000844 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000845 switch (CI->getPredicate()) {
846 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000847 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
848 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000849
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000850 unsigned EReg = createResultReg(&X86::GR8RegClass);
851 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000852 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
853 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
854 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000856 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000857 UpdateValueMap(I, ResultReg);
858 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000859 }
860 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000861 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
862 return false;
863
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000864 unsigned NEReg = createResultReg(&X86::GR8RegClass);
865 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000866 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
867 TII.get(X86::SETNEr), NEReg);
868 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
869 TII.get(X86::SETPr), PReg);
870 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
871 TII.get(X86::OR8rr), ResultReg)
872 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000873 UpdateValueMap(I, ResultReg);
874 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000875 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000876 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
877 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
878 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
879 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
880 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
881 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
882 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
883 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
884 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
885 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
886 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
887 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000888
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000889 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
890 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
891 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
892 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
893 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
894 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
895 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
896 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
897 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
898 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000899 default:
900 return false;
901 }
902
Dan Gohman46510a72010-04-15 01:51:59 +0000903 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000904 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000905 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000906
Chris Lattner9a08a612008-10-15 04:26:38 +0000907 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000908 if (!X86FastEmitCompare(Op0, Op1, VT))
909 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000910
Dan Gohman84023e02010-07-10 09:00:22 +0000911 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000912 UpdateValueMap(I, ResultReg);
913 return true;
914}
Evan Cheng8b19e562008-09-03 06:44:39 +0000915
Dan Gohman46510a72010-04-15 01:51:59 +0000916bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000917 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000918 if (I->getType()->isIntegerTy(8) &&
919 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000920 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000921 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000922 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000923 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000924 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000925 UpdateValueMap(I, ResultReg);
926 return true;
927 }
928
929 return false;
930}
931
Chris Lattner9a08a612008-10-15 04:26:38 +0000932
Dan Gohman46510a72010-04-15 01:51:59 +0000933bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000934 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000935 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000936 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000937 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
938 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000939
Dan Gohman8bef7442010-08-21 02:32:36 +0000940 // Fold the common case of a conditional branch with a comparison
941 // in the same block (values defined on other blocks may not have
942 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000943 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000944 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000945 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000946
Dan Gohmand98d6202008-10-02 22:15:21 +0000947 // Try to take advantage of fallthrough opportunities.
948 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000949 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000950 std::swap(TrueMBB, FalseMBB);
951 Predicate = CmpInst::getInversePredicate(Predicate);
952 }
953
Chris Lattner871d2462008-10-15 03:58:05 +0000954 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
955 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
956
Dan Gohmand98d6202008-10-02 22:15:21 +0000957 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000958 case CmpInst::FCMP_OEQ:
959 std::swap(TrueMBB, FalseMBB);
960 Predicate = CmpInst::FCMP_UNE;
961 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000962 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
963 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
964 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
965 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
966 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
967 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
968 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
969 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
970 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
971 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
972 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
973 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
974 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000975
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000976 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
977 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
978 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
979 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
980 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
981 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
982 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
983 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
984 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
985 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000986 default:
987 return false;
988 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000989
Dan Gohman46510a72010-04-15 01:51:59 +0000990 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000991 if (SwapArgs)
992 std::swap(Op0, Op1);
993
Chris Lattner9a08a612008-10-15 04:26:38 +0000994 // Emit a compare of the LHS and RHS, setting the flags.
995 if (!X86FastEmitCompare(Op0, Op1, VT))
996 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000997
Dan Gohman84023e02010-07-10 09:00:22 +0000998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
999 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001000
1001 if (Predicate == CmpInst::FCMP_UNE) {
1002 // X86 requires a second branch to handle UNE (and OEQ,
1003 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001004 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1005 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001006 }
1007
Stuart Hastings3bf91252010-06-17 22:43:56 +00001008 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001009 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001010 return true;
1011 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001012 } else if (ExtractValueInst *EI =
1013 dyn_cast<ExtractValueInst>(BI->getCondition())) {
1014 // Check to see if the branch instruction is from an "arithmetic with
1015 // overflow" intrinsic. The main way these intrinsics are used is:
1016 //
1017 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1018 // %sum = extractvalue { i32, i1 } %t, 0
1019 // %obit = extractvalue { i32, i1 } %t, 1
1020 // br i1 %obit, label %overflow, label %normal
1021 //
Dan Gohman653456c2009-01-07 00:15:08 +00001022 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +00001023 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +00001024 // looking for the SETO/SETB instruction. If an instruction modifies the
1025 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1026 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001027 if (const IntrinsicInst *CI =
1028 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001029 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1030 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1031 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001032 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001033
Chris Lattnera9a42252009-04-12 07:36:01 +00001034 for (MachineBasicBlock::const_reverse_iterator
Dan Gohman84023e02010-07-10 09:00:22 +00001035 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1036 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001037 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001038
Evan Cheng1015ba72010-05-21 20:53:24 +00001039 if (MI.definesRegister(Reg)) {
Jakob Stoklund Olesen84d499a2010-07-16 22:35:34 +00001040 if (MI.isCopy()) {
1041 Reg = MI.getOperand(1).getReg();
Chris Lattnera9a42252009-04-12 07:36:01 +00001042 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001043 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001044
Chris Lattnera9a42252009-04-12 07:36:01 +00001045 SetMI = &MI;
1046 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001047 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001048
Chris Lattnera9a42252009-04-12 07:36:01 +00001049 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengc36b7062011-01-07 23:50:32 +00001050 if (TID.hasImplicitDefOfPhysReg(X86::EFLAGS) ||
1051 MI.hasUnmodeledSideEffects())
Chris Lattnera9a42252009-04-12 07:36:01 +00001052 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001053 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001054
1055 if (SetMI) {
1056 unsigned OpCode = SetMI->getOpcode();
1057
1058 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohman84023e02010-07-10 09:00:22 +00001059 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1060 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001061 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001062 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001063 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001064 return true;
1065 }
Bill Wendling9a901322008-12-10 19:44:24 +00001066 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001067 }
1068 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001069 }
1070
1071 // Otherwise do a clumsy setcc and re-test it.
1072 unsigned OpReg = getRegForValue(BI->getCondition());
1073 if (OpReg == 0) return false;
1074
Dan Gohman84023e02010-07-10 09:00:22 +00001075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1076 .addReg(OpReg).addReg(OpReg);
1077 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1078 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001079 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001080 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001081 return true;
1082}
1083
Dan Gohman46510a72010-04-15 01:51:59 +00001084bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +00001085 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001086 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001087 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001088 CReg = X86::CL;
1089 RC = &X86::GR8RegClass;
1090 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001091 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
1092 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
1093 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001094 default: return false;
1095 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001096 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001097 CReg = X86::CX;
1098 RC = &X86::GR16RegClass;
1099 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001100 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1101 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1102 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001103 default: return false;
1104 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001105 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001106 CReg = X86::ECX;
1107 RC = &X86::GR32RegClass;
1108 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001109 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1110 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1111 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001112 default: return false;
1113 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001114 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001115 CReg = X86::RCX;
1116 RC = &X86::GR64RegClass;
1117 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001118 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1119 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1120 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001121 default: return false;
1122 }
1123 } else {
1124 return false;
1125 }
1126
Duncan Sands1440e8b2010-11-03 11:35:31 +00001127 MVT VT;
1128 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001129 return false;
1130
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001131 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1132 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001133
Chris Lattner743922e2008-09-21 21:44:29 +00001134 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001135 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001136 unsigned ResultReg = createResultReg(RC);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001137 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001138 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001139 UpdateValueMap(I, ResultReg);
1140 return true;
1141 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001142
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001143 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1144 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001145 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1146 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001147
1148 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001149 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001150 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001151 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1152 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001153 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001154
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001155 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001156 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1157 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001158 UpdateValueMap(I, ResultReg);
1159 return true;
1160}
1161
Dan Gohman46510a72010-04-15 01:51:59 +00001162bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001163 MVT VT;
1164 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001165 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001166
Eric Christophere487b012010-09-29 23:00:29 +00001167 // We only use cmov here, if we don't have a cmov instruction bail.
1168 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001169
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001170 unsigned Opc = 0;
1171 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001172 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001173 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001174 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001175 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001176 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001177 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001178 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001179 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001180 RC = &X86::GR64RegClass;
1181 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001182 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001183 }
1184
1185 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1186 if (Op0Reg == 0) return false;
1187 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1188 if (Op1Reg == 0) return false;
1189 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1190 if (Op2Reg == 0) return false;
1191
Dan Gohman84023e02010-07-10 09:00:22 +00001192 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1193 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001194 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001195 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1196 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001197 UpdateValueMap(I, ResultReg);
1198 return true;
1199}
1200
Dan Gohman46510a72010-04-15 01:51:59 +00001201bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001202 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001203 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001204 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001205 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001206 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001207 unsigned OpReg = getRegForValue(V);
1208 if (OpReg == 0) return false;
1209 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001210 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1211 TII.get(X86::CVTSS2SDrr), ResultReg)
1212 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001213 UpdateValueMap(I, ResultReg);
1214 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001215 }
1216 }
1217
1218 return false;
1219}
1220
Dan Gohman46510a72010-04-15 01:51:59 +00001221bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001222 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001223 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001224 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001225 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001226 unsigned OpReg = getRegForValue(V);
1227 if (OpReg == 0) return false;
1228 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001229 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1230 TII.get(X86::CVTSD2SSrr), ResultReg)
1231 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001232 UpdateValueMap(I, ResultReg);
1233 return true;
1234 }
1235 }
1236 }
1237
1238 return false;
1239}
1240
Dan Gohman46510a72010-04-15 01:51:59 +00001241bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001242 if (Subtarget->is64Bit())
1243 // All other cases should be handled by the tblgen generated code.
1244 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001245 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1246 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001247
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001248 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001249 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001250 // All other cases should be handled by the tblgen generated code.
1251 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001253 // All other cases should be handled by the tblgen generated code.
1254 return false;
1255
1256 unsigned InputReg = getRegForValue(I->getOperand(0));
1257 if (!InputReg)
1258 // Unhandled operand. Halt "fast" selection and bail.
1259 return false;
1260
Dan Gohman62417622009-04-27 16:33:14 +00001261 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001263 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001264 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001265 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1266 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001267
1268 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001270 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001271 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001272 if (!ResultReg)
1273 return false;
1274
1275 UpdateValueMap(I, ResultReg);
1276 return true;
1277}
1278
Dan Gohman46510a72010-04-15 01:51:59 +00001279bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1280 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1281 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001282
Dan Gohman46510a72010-04-15 01:51:59 +00001283 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001284 switch (CI->getIntrinsicID()) {
1285 default: break;
1286 case Intrinsic::sadd_with_overflow:
Dan Gohman84023e02010-07-10 09:00:22 +00001287 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001288 // Cheat a little. We know that the registers for "add" and "seto" are
1289 // allocated sequentially. However, we only keep track of the register
1290 // for "add" in the value map. Use extractvalue's index to get the
1291 // correct register for "seto".
Dan Gohman84023e02010-07-10 09:00:22 +00001292 unsigned OpReg = getRegForValue(Agg);
1293 if (OpReg == 0)
1294 return false;
1295 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001296 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001297 }
Dan Gohman84023e02010-07-10 09:00:22 +00001298 }
Bill Wendling52370a12008-12-09 02:42:50 +00001299 }
1300
1301 return false;
1302}
1303
Dan Gohman46510a72010-04-15 01:51:59 +00001304bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001305 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001306 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001307 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001308 case Intrinsic::stackprotector: {
1309 // Emit code inline code to store the stack guard onto the stack.
1310 EVT PtrTy = TLI.getPointerTy();
1311
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001312 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1313 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001314
1315 // Grab the frame index.
1316 X86AddressMode AM;
1317 if (!X86SelectAddress(Slot, AM)) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001318
Eric Christopher88dee302010-03-18 21:58:33 +00001319 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001320
Eric Christopher07754c22010-03-18 20:27:26 +00001321 return true;
1322 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001323 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001324 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001325 const Type *Ty = I.getCalledFunction()->getReturnType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001326
Eric Christopherf27805b2010-03-11 06:20:22 +00001327 assert(CI && "Non-constant type in Intrinsic::objectsize?");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001328
Duncan Sands1440e8b2010-11-03 11:35:31 +00001329 MVT VT;
Eric Christopherf27805b2010-03-11 06:20:22 +00001330 if (!isTypeLegal(Ty, VT))
1331 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001332
Eric Christopherf27805b2010-03-11 06:20:22 +00001333 unsigned OpC = 0;
1334 if (VT == MVT::i32)
1335 OpC = X86::MOV32ri;
1336 else if (VT == MVT::i64)
1337 OpC = X86::MOV64ri;
1338 else
1339 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001340
Eric Christopherf27805b2010-03-11 06:20:22 +00001341 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001342 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001343 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001344 UpdateValueMap(&I, ResultReg);
1345 return true;
1346 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001347 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001348 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001349 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001350 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001351 if (!X86SelectAddress(DI->getAddress(), AM))
1352 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001353 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001354 // FIXME may need to add RegState::Debug to any registers produced,
1355 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001356 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1357 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001358 return true;
1359 }
Eric Christopher77f79892010-01-18 22:11:29 +00001360 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001362 return true;
1363 }
Bill Wendling52370a12008-12-09 02:42:50 +00001364 case Intrinsic::sadd_with_overflow:
1365 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001366 // Replace "add with overflow" intrinsics with an "add" instruction followed
1367 // by a seto/setc instruction. Later on, when the "extractvalue"
1368 // instructions are encountered, we use the fact that two registers were
1369 // created sequentially to get the correct registers for the "sum" and the
1370 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001371 const Function *Callee = I.getCalledFunction();
1372 const Type *RetTy =
1373 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1374
Duncan Sands1440e8b2010-11-03 11:35:31 +00001375 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001376 if (!isTypeLegal(RetTy, VT))
1377 return false;
1378
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001379 const Value *Op1 = I.getArgOperand(0);
1380 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001381 unsigned Reg1 = getRegForValue(Op1);
1382 unsigned Reg2 = getRegForValue(Op2);
1383
1384 if (Reg1 == 0 || Reg2 == 0)
1385 // FIXME: Handle values *not* in registers.
1386 return false;
1387
1388 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001389 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001390 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001391 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001392 OpC = X86::ADD64rr;
1393 else
1394 return false;
1395
1396 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001397 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1398 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001399 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001400
Chris Lattner8d57b772009-04-12 07:51:14 +00001401 // If the add with overflow is an intra-block value then we just want to
1402 // create temporaries for it like normal. If it is a cross-block value then
1403 // UpdateValueMap will return the cross-block register used. Since we
1404 // *really* want the value to be live in the register pair known by
1405 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1406 // the cross block case. In the non-cross-block case, we should just make
1407 // another register for the value.
1408 if (DestReg1 != ResultReg)
1409 ResultReg = DestReg1+1;
1410 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001412
Chris Lattnera9a42252009-04-12 07:36:01 +00001413 unsigned Opc = X86::SETBr;
1414 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1415 Opc = X86::SETOr;
Dan Gohman84023e02010-07-10 09:00:22 +00001416 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001417 return true;
1418 }
1419 }
1420}
1421
Dan Gohman46510a72010-04-15 01:51:59 +00001422bool X86FastISel::X86SelectCall(const Instruction *I) {
1423 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001424 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001425
1426 // Can't handle inline asm yet.
1427 if (isa<InlineAsm>(Callee))
1428 return false;
1429
Bill Wendling52370a12008-12-09 02:42:50 +00001430 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001431 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001432 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001433
Evan Chengf3d4efe2008-09-07 09:09:33 +00001434 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001435 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001436 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001437 if (CC != CallingConv::C &&
1438 CC != CallingConv::Fast &&
1439 CC != CallingConv::X86_FastCall)
1440 return false;
1441
Evan Cheng381993f2010-01-27 00:00:57 +00001442 // fastcc with -tailcallopt is intended to provide a guaranteed
1443 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001444 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001445 return false;
1446
Evan Chengf3d4efe2008-09-07 09:09:33 +00001447 // Let SDISel handle vararg functions.
1448 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1449 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1450 if (FTy->isVarArg())
1451 return false;
1452
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001453 // Fast-isel doesn't know about callee-pop yet.
1454 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1455 return false;
1456
Evan Chengf3d4efe2008-09-07 09:09:33 +00001457 // Handle *simple* calls for now.
1458 const Type *RetTy = CS.getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001459 MVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001460 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001461 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001462 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001463 return false;
1464
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001465 // Materialize callee address in a register. FIXME: GV address can be
1466 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001467 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001468 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001469 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001470 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001471 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001472 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001473 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001474 } else if (CalleeAM.Base.Reg != 0) {
1475 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001476 } else
1477 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001478
Evan Chengdebdea02008-09-08 17:15:42 +00001479 // Allow calls which produce i1 results.
1480 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001481 if (RetVT == MVT::i1) {
1482 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001483 AndToI1 = true;
1484 }
1485
Evan Chengf3d4efe2008-09-07 09:09:33 +00001486 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001487 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001488 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001489 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001490 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001491 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001492 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001493 ArgVTs.reserve(CS.arg_size());
1494 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001495 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001496 i != e; ++i) {
1497 unsigned Arg = getRegForValue(*i);
1498 if (Arg == 0)
1499 return false;
1500 ISD::ArgFlagsTy Flags;
1501 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001502 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001503 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001504 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001505 Flags.setZExt();
1506
1507 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001508 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1509 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1510 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1511 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001512 return false;
1513
1514 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001515 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001516 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001517 return false;
1518 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1519 Flags.setOrigAlign(OriginalAlignment);
1520
1521 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001522 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001523 ArgVTs.push_back(ArgVT);
1524 ArgFlags.push_back(Flags);
1525 }
1526
1527 // Analyze operands of the call, assigning locations to each operand.
1528 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001529 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001530
Dan Gohmand8acddd2010-06-01 21:09:47 +00001531 // Allocate shadow area for Win64
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001532 if (Subtarget->isTargetWin64()) {
1533 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001534 }
1535
Duncan Sands45907662010-10-31 13:21:44 +00001536 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001537
1538 // Get a count of how many bytes are to be pushed on the stack.
1539 unsigned NumBytes = CCInfo.getNextStackOffset();
1540
1541 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001542 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1544 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001545
Chris Lattner438949a2008-10-15 05:30:52 +00001546 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001547 // copies / loads.
1548 SmallVector<unsigned, 4> RegArgs;
1549 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1550 CCValAssign &VA = ArgLocs[i];
1551 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001552 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001553
Evan Chengf3d4efe2008-09-07 09:09:33 +00001554 // Promote the value if needed.
1555 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001556 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001557 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001558 case CCValAssign::SExt: {
1559 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1560 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001561 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001562 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001563 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001564 }
1565 case CCValAssign::ZExt: {
1566 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1567 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001568 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001569 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001570 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001571 }
1572 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001573 // We don't handle MMX parameters yet.
1574 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1575 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001576 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1577 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001578 if (!Emitted)
1579 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001580 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001581 if (!Emitted)
1582 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1583 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001584
Chris Lattnerc46ec642011-01-05 22:26:52 +00001585 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001586 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001587 break;
1588 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001589 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001590 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001591 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001592 assert(BC != 0 && "Failed to emit a bitcast!");
1593 Arg = BC;
1594 ArgVT = VA.getLocVT();
1595 break;
1596 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001597 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001598
Evan Chengf3d4efe2008-09-07 09:09:33 +00001599 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001600 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1601 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001602 RegArgs.push_back(VA.getLocReg());
1603 } else {
1604 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001605 X86AddressMode AM;
1606 AM.Base.Reg = StackPtr;
1607 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001608 const Value *ArgVal = ArgVals[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001609
Chris Lattner241ab472008-10-15 05:38:32 +00001610 // If this is a really simple value, emit this with the Value* version of
1611 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1612 // can cause us to reevaluate the argument.
1613 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1614 X86FastEmitStore(ArgVT, ArgVal, AM);
1615 else
1616 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001617 }
1618 }
1619
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001620 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001621 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001622 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001623 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001624 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1625 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001626 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001627
Evan Chengf3d4efe2008-09-07 09:09:33 +00001628 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001629 MachineInstrBuilder MIB;
1630 if (CalleeOp) {
1631 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001632 unsigned CallOpc;
1633 if (Subtarget->isTargetWin64())
1634 CallOpc = X86::WINCALL64r;
1635 else if (Subtarget->is64Bit())
1636 CallOpc = X86::CALL64r;
1637 else
1638 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001639 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1640 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001641
Chris Lattner51e8eab2009-07-09 06:34:26 +00001642 } else {
1643 // Direct call.
1644 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001645 unsigned CallOpc;
1646 if (Subtarget->isTargetWin64())
1647 CallOpc = X86::WINCALL64pcrel32;
1648 else if (Subtarget->is64Bit())
1649 CallOpc = X86::CALL64pcrel32;
1650 else
1651 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001652
Chris Lattner51e8eab2009-07-09 06:34:26 +00001653 // See if we need any target-specific flags on the GV operand.
1654 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001655
Chris Lattner51e8eab2009-07-09 06:34:26 +00001656 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1657 // external symbols most go through the PLT in PIC mode. If the symbol
1658 // has hidden or protected visibility, or if it is static or local, then
1659 // we don't need to use the PLT - we can directly call it.
1660 if (Subtarget->isTargetELF() &&
1661 TM.getRelocationModel() == Reloc::PIC_ &&
1662 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1663 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001664 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001665 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1666 Subtarget->getDarwinVers() < 9) {
1667 // PC-relative references to external symbols should go through $stub,
1668 // unless we're building with the leopard linker or later, which
1669 // automatically synthesizes these stubs.
1670 OpFlags = X86II::MO_DARWIN_STUB;
1671 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001672
1673
Dan Gohman84023e02010-07-10 09:00:22 +00001674 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1675 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001676 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001677
1678 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001679 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001680 MIB.addReg(X86::EBX);
1681
Evan Chengf3d4efe2008-09-07 09:09:33 +00001682 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001683 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1684 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001685
1686 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001687 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001688 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1689 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001690
1691 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001692 SmallVector<unsigned, 4> UsedRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001693 if (RetVT != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001694 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001695 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001696 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1697
1698 // Copy all of the result registers out of their specified physreg.
1699 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001700 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001701 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001702
Evan Chengf3d4efe2008-09-07 09:09:33 +00001703 // If this is a call to a function that returns an fp value on the x87 fp
1704 // stack, but where we prefer to use the value in xmm registers, copy it
1705 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1706 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1707 RVLocs[0].getLocReg() == X86::ST1) &&
1708 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001709 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001710 DstRC = X86::RFP80RegisterClass;
1711 }
1712
1713 unsigned ResultReg = createResultReg(DstRC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001714 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1715 ResultReg).addReg(RVLocs[0].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001716 UsedRegs.push_back(RVLocs[0].getLocReg());
1717
Evan Chengf3d4efe2008-09-07 09:09:33 +00001718 if (CopyVT != RVLocs[0].getValVT()) {
1719 // Round the F80 the right size, which also moves to the appropriate xmm
1720 // register. This is accomplished by storing the F80 value in memory and
1721 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001722 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001724 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001725 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001726 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1727 TII.get(Opc)), FI)
1728 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001729 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001730 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001731 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001732 ResultReg = createResultReg(DstRC);
Dan Gohman84023e02010-07-10 09:00:22 +00001733 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1734 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001735 }
1736
Evan Chengdebdea02008-09-08 17:15:42 +00001737 if (AndToI1) {
1738 // Mask out all but lowest bit for some call which produces an i1.
1739 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001740 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001741 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001742 ResultReg = AndResult;
1743 }
1744
Evan Chengf3d4efe2008-09-07 09:09:33 +00001745 UpdateValueMap(I, ResultReg);
1746 }
1747
Dan Gohmandb497122010-06-18 23:28:01 +00001748 // Set all unused physreg defs as dead.
1749 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1750
Evan Chengf3d4efe2008-09-07 09:09:33 +00001751 return true;
1752}
1753
1754
Dan Gohman99b21822008-08-28 23:21:34 +00001755bool
Dan Gohman46510a72010-04-15 01:51:59 +00001756X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001757 switch (I->getOpcode()) {
1758 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001759 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001760 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001761 case Instruction::Store:
1762 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001763 case Instruction::Ret:
1764 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001765 case Instruction::ICmp:
1766 case Instruction::FCmp:
1767 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001768 case Instruction::ZExt:
1769 return X86SelectZExt(I);
1770 case Instruction::Br:
1771 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001772 case Instruction::Call:
1773 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001774 case Instruction::LShr:
1775 case Instruction::AShr:
1776 case Instruction::Shl:
1777 return X86SelectShift(I);
1778 case Instruction::Select:
1779 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001780 case Instruction::Trunc:
1781 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001782 case Instruction::FPExt:
1783 return X86SelectFPExt(I);
1784 case Instruction::FPTrunc:
1785 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001786 case Instruction::ExtractValue:
1787 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001788 case Instruction::IntToPtr: // Deliberate fall-through.
1789 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001790 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1791 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001792 if (DstVT.bitsGT(SrcVT))
1793 return X86SelectZExt(I);
1794 if (DstVT.bitsLT(SrcVT))
1795 return X86SelectTrunc(I);
1796 unsigned Reg = getRegForValue(I->getOperand(0));
1797 if (Reg == 0) return false;
1798 UpdateValueMap(I, Reg);
1799 return true;
1800 }
Dan Gohman99b21822008-08-28 23:21:34 +00001801 }
1802
1803 return false;
1804}
1805
Dan Gohman46510a72010-04-15 01:51:59 +00001806unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001807 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001808 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001809 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001810
Owen Anderson95267a12008-09-05 00:06:23 +00001811 // Get opcode and regclass of the output for the given load instruction.
1812 unsigned Opc = 0;
1813 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001814 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001815 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001816 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001817 Opc = X86::MOV8rm;
1818 RC = X86::GR8RegisterClass;
1819 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001821 Opc = X86::MOV16rm;
1822 RC = X86::GR16RegisterClass;
1823 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001825 Opc = X86::MOV32rm;
1826 RC = X86::GR32RegisterClass;
1827 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001829 // Must be in x86-64 mode.
1830 Opc = X86::MOV64rm;
1831 RC = X86::GR64RegisterClass;
1832 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001834 if (Subtarget->hasSSE1()) {
1835 Opc = X86::MOVSSrm;
1836 RC = X86::FR32RegisterClass;
1837 } else {
1838 Opc = X86::LD_Fp32m;
1839 RC = X86::RFP32RegisterClass;
1840 }
1841 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001843 if (Subtarget->hasSSE2()) {
1844 Opc = X86::MOVSDrm;
1845 RC = X86::FR64RegisterClass;
1846 } else {
1847 Opc = X86::LD_Fp64m;
1848 RC = X86::RFP64RegisterClass;
1849 }
1850 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001852 // No f80 support yet.
1853 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001854 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001855
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001856 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001857 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001858 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001859 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001861 Opc = X86::LEA32r;
1862 else
1863 Opc = X86::LEA64r;
1864 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001865 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1866 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001867 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001868 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001869 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001870 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001871
Owen Anderson3b217c62008-09-06 01:11:01 +00001872 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001873 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001874 if (Align == 0) {
1875 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001876 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001877 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001878
Dan Gohman5396c992008-09-30 01:21:32 +00001879 // x86-32 PIC requires a PIC base register for constant pools.
1880 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001881 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001882 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001883 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001884 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001885 } else if (Subtarget->isPICStyleGOT()) {
1886 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001887 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001888 } else if (Subtarget->isPICStyleRIPRel() &&
1889 TM.getCodeModel() == CodeModel::Small) {
1890 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001891 }
Dan Gohman5396c992008-09-30 01:21:32 +00001892
1893 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001894 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001895 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001896 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1897 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001898 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001899
Owen Anderson95267a12008-09-05 00:06:23 +00001900 return ResultReg;
1901}
1902
Dan Gohman46510a72010-04-15 01:51:59 +00001903unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001904 // Fail on dynamic allocas. At this point, getRegForValue has already
1905 // checked its CSE maps, so if we're here trying to handle a dynamic
1906 // alloca, we're not going to succeed. X86SelectAddress has a
1907 // check for dynamic allocas, because it's called directly from
1908 // various places, but TargetMaterializeAlloca also needs a check
1909 // in order to avoid recursion between getRegForValue,
1910 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001911 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001912 return 0;
1913
Dan Gohman0586d912008-09-10 20:11:02 +00001914 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001915 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001916 return 0;
1917 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1918 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1919 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001920 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1921 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001922 return ResultReg;
1923}
1924
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001925/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
1926/// vreg is being provided by the specified load instruction. If possible,
1927/// try to fold the load as an operand to the instruction, returning true if
1928/// possible.
1929bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
1930 const LoadInst *LI) {
1931 X86AddressMode AM;
1932 if (!X86SelectAddress(LI->getOperand(0), AM))
1933 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001934
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001935 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001936
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001937 unsigned Size = TD.getTypeAllocSize(LI->getType());
1938 unsigned Alignment = LI->getAlignment();
1939
1940 SmallVector<MachineOperand, 8> AddrOps;
1941 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001942
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001943 MachineInstr *Result =
1944 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
1945 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001946
Chris Lattnerb99fdee2011-01-16 02:27:38 +00001947 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001948 MI->eraseFromParent();
1949 return true;
1950}
1951
1952
Evan Chengc3f44b02008-09-03 00:03:49 +00001953namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001954 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1955 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001956 }
Dan Gohman99b21822008-08-28 23:21:34 +00001957}