blob: 88744861e86ec42d7108b60ac196b80460f8a27e [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"
Dan Gohman84023e02010-07-10 09:00:22 +000026#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000027#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000028#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000029#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000032#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000034#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000035#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000036using namespace llvm;
37
Chris Lattner087fcf32009-03-08 18:44:31 +000038namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000039
Evan Chengc3f44b02008-09-03 00:03:49 +000040class X86FastISel : public FastISel {
41 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
42 /// make the right decision when generating code for different targets.
43 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000044
45 /// StackPtr - Register used as the stack pointer.
46 ///
47 unsigned StackPtr;
48
Wesley Peckbf17cfa2010-11-23 03:31:01 +000049 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000050 /// floating point ops.
51 /// When SSE is available, use it for f32 operations.
52 /// When SSE2 is available, use it for f64 operations.
53 bool X86ScalarSSEf64;
54 bool X86ScalarSSEf32;
55
Evan Cheng8b19e562008-09-03 06:44:39 +000056public:
Dan Gohmana4160c32010-07-07 16:29:44 +000057 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000058 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000059 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
60 X86ScalarSSEf64 = Subtarget->hasSSE2();
61 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000062 }
Evan Chengc3f44b02008-09-03 00:03:49 +000063
Dan Gohman46510a72010-04-15 01:51:59 +000064 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000065
Chris Lattnerbeac75d2010-09-05 02:18:34 +000066 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
67 /// vreg is being provided by the specified load instruction. If possible,
68 /// try to fold the load as an operand to the instruction, returning true if
69 /// possible.
70 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
71 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000072
Dan Gohman1adf1b02008-08-19 21:45:35 +000073#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000074
75private:
Dan Gohman46510a72010-04-15 01:51:59 +000076 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000077
Owen Andersone50ed302009-08-10 22:56:29 +000078 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000079
Dan Gohman46510a72010-04-15 01:51:59 +000080 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000081 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000082 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000083 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000084
Owen Andersone50ed302009-08-10 22:56:29 +000085 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000086 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000087
Dan Gohman46510a72010-04-15 01:51:59 +000088 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
89 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000090
Dan Gohman46510a72010-04-15 01:51:59 +000091 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000092
Dan Gohman46510a72010-04-15 01:51:59 +000093 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000094
Dan Gohman84023e02010-07-10 09:00:22 +000095 bool X86SelectRet(const Instruction *I);
96
Dan Gohman46510a72010-04-15 01:51:59 +000097 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000098
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000100
Dan Gohman46510a72010-04-15 01:51:59 +0000101 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000102
Dan Gohman46510a72010-04-15 01:51:59 +0000103 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000104
Dan Gohman46510a72010-04-15 01:51:59 +0000105 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000108
Dan Gohman46510a72010-04-15 01:51:59 +0000109 bool X86SelectFPExt(const Instruction *I);
110 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000111
Dan Gohman46510a72010-04-15 01:51:59 +0000112 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000113
Dan Gohman46510a72010-04-15 01:51:59 +0000114 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
115 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000116
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000117 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000118 return getTargetMachine()->getInstrInfo();
119 }
120 const X86TargetMachine *getTargetMachine() const {
121 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000122 }
123
Dan Gohman46510a72010-04-15 01:51:59 +0000124 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000125
Dan Gohman46510a72010-04-15 01:51:59 +0000126 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000127
128 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
129 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000130 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
132 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000133 }
134
Duncan Sands1440e8b2010-11-03 11:35:31 +0000135 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000136};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000137
Chris Lattner087fcf32009-03-08 18:44:31 +0000138} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000139
Duncan Sands1440e8b2010-11-03 11:35:31 +0000140bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
141 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
142 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000143 // Unhandled type. Halt "fast" selection and bail.
144 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000145
146 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000147 // For now, require SSE/SSE2 for performing floating-point operations,
148 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000150 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000152 return false;
153 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000155 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000156 // We only handle legal types. For example, on x86-32 the instruction
157 // selector contains all of the 64-bit instructions from x86-64,
158 // under the assumption that i64 won't be used if the target doesn't
159 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000161}
162
163#include "X86GenCallingConv.inc"
164
Evan Cheng0de588f2008-09-05 21:00:03 +0000165/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000166/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000167/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000168bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000169 unsigned &ResultReg) {
170 // Get opcode and regclass of the output for the given load instruction.
171 unsigned Opc = 0;
172 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000174 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000175 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000176 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000177 Opc = X86::MOV8rm;
178 RC = X86::GR8RegisterClass;
179 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000181 Opc = X86::MOV16rm;
182 RC = X86::GR16RegisterClass;
183 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000185 Opc = X86::MOV32rm;
186 RC = X86::GR32RegisterClass;
187 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000189 // Must be in x86-64 mode.
190 Opc = X86::MOV64rm;
191 RC = X86::GR64RegisterClass;
192 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000194 if (Subtarget->hasSSE1()) {
195 Opc = X86::MOVSSrm;
196 RC = X86::FR32RegisterClass;
197 } else {
198 Opc = X86::LD_Fp32m;
199 RC = X86::RFP32RegisterClass;
200 }
201 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 if (Subtarget->hasSSE2()) {
204 Opc = X86::MOVSDrm;
205 RC = X86::FR64RegisterClass;
206 } else {
207 Opc = X86::LD_Fp64m;
208 RC = X86::RFP64RegisterClass;
209 }
210 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000212 // No f80 support yet.
213 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000214 }
215
216 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000217 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
218 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000219 return true;
220}
221
Evan Chengf3d4efe2008-09-07 09:09:33 +0000222/// X86FastEmitStore - Emit a machine instruction to store a value Val of
223/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
224/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000225/// i.e. V. Return true if it is possible.
226bool
Owen Andersone50ed302009-08-10 22:56:29 +0000227X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000228 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000229 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000230 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 switch (VT.getSimpleVT().SimpleTy) {
232 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000233 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000234 case MVT::i1: {
235 // Mask out all but lowest bit.
236 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000238 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
239 Val = AndResult;
240 }
241 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 case MVT::i8: Opc = X86::MOV8mr; break;
243 case MVT::i16: Opc = X86::MOV16mr; break;
244 case MVT::i32: Opc = X86::MOV32mr; break;
245 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
246 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000247 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000248 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000250 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000251 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000252 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000253
Dan Gohman84023e02010-07-10 09:00:22 +0000254 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
255 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000256 return true;
257}
258
Dan Gohman46510a72010-04-15 01:51:59 +0000259bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000260 const X86AddressMode &AM) {
261 // Handle 'null' like i32/i64 0.
262 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000263 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264
Chris Lattner438949a2008-10-15 05:30:52 +0000265 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000266 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000267 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000268 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000270 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000271 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 case MVT::i8: Opc = X86::MOV8mi; break;
273 case MVT::i16: Opc = X86::MOV16mi; break;
274 case MVT::i32: Opc = X86::MOV32mi; break;
275 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000276 // Must be a 32-bit sign extended value.
277 if ((int)CI->getSExtValue() == CI->getSExtValue())
278 Opc = X86::MOV64mi32;
279 break;
280 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000281
Chris Lattner438949a2008-10-15 05:30:52 +0000282 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000283 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
284 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000285 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000286 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000287 return true;
288 }
289 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000290
Chris Lattner438949a2008-10-15 05:30:52 +0000291 unsigned ValReg = getRegForValue(Val);
292 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000293 return false;
294
Chris Lattner438949a2008-10-15 05:30:52 +0000295 return X86FastEmitStore(VT, ValReg, AM);
296}
297
Evan Cheng24e3a902008-09-08 06:35:17 +0000298/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
299/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
300/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000301bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
302 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000303 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000304 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
305 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000306
Owen Andersonac34a002008-09-11 19:44:55 +0000307 if (RR != 0) {
308 ResultReg = RR;
309 return true;
310 } else
311 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000312}
313
Dan Gohman0586d912008-09-10 20:11:02 +0000314/// X86SelectAddress - Attempt to fill in an address from the given value.
315///
Dan Gohman46510a72010-04-15 01:51:59 +0000316bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
317 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000318 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000319 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000320 // Don't walk into other basic blocks; it's possible we haven't
321 // visited them yet, so the instructions may not yet be assigned
322 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000323 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
324 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
325 Opcode = I->getOpcode();
326 U = I;
327 }
Dan Gohman46510a72010-04-15 01:51:59 +0000328 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000329 Opcode = C->getOpcode();
330 U = C;
331 }
Dan Gohman0586d912008-09-10 20:11:02 +0000332
Chris Lattner868ee942010-06-15 19:08:40 +0000333 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
334 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000335 // Fast instruction selection doesn't support the special
336 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000337 return false;
338
Dan Gohman35893082008-09-18 23:23:44 +0000339 switch (Opcode) {
340 default: break;
341 case Instruction::BitCast:
342 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000343 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000344
345 case Instruction::IntToPtr:
346 // Look past no-op inttoptrs.
347 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000348 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000349 break;
Dan Gohman35893082008-09-18 23:23:44 +0000350
351 case Instruction::PtrToInt:
352 // Look past no-op ptrtoints.
353 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000354 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000355 break;
Dan Gohman35893082008-09-18 23:23:44 +0000356
357 case Instruction::Alloca: {
358 // Do static allocas.
359 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000360 DenseMap<const AllocaInst*, int>::iterator SI =
361 FuncInfo.StaticAllocaMap.find(A);
362 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000363 AM.BaseType = X86AddressMode::FrameIndexBase;
364 AM.Base.FrameIndex = SI->second;
365 return true;
366 }
367 break;
Dan Gohman35893082008-09-18 23:23:44 +0000368 }
369
370 case Instruction::Add: {
371 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000372 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000373 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
374 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000375 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000376 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000377 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000378 }
Dan Gohman0586d912008-09-10 20:11:02 +0000379 }
Dan Gohman35893082008-09-18 23:23:44 +0000380 break;
381 }
382
383 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000384 X86AddressMode SavedAM = AM;
385
Dan Gohman35893082008-09-18 23:23:44 +0000386 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000387 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000388 unsigned IndexReg = AM.IndexReg;
389 unsigned Scale = AM.Scale;
390 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000391 // Iterate through the indices, folding what we can. Constants can be
392 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000393 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000394 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000395 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000396 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
397 const StructLayout *SL = TD.getStructLayout(STy);
398 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
399 Disp += SL->getElementOffset(Idx);
400 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000401 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000402 for (;;) {
Dan Gohman5c87bf62010-07-01 02:27:15 +0000403 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
404 // Constant-offset addressing.
405 Disp += CI->getSExtValue() * S;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000406 break;
407 }
408 if (isa<AddOperator>(Op) &&
409 (!isa<Instruction>(Op) ||
410 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
411 == FuncInfo.MBB) &&
412 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
413 // An add (in the same block) with a constant operand. Fold the
414 // constant.
Dan Gohmanabd1d852010-07-01 02:58:21 +0000415 ConstantInt *CI =
416 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
417 Disp += CI->getSExtValue() * S;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000418 // Iterate on the other operand.
419 Op = cast<AddOperator>(Op)->getOperand(0);
420 continue;
421 }
422 if (IndexReg == 0 &&
423 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
424 (S == 1 || S == 2 || S == 4 || S == 8)) {
Dan Gohman5c87bf62010-07-01 02:27:15 +0000425 // Scaled-index addressing.
426 Scale = S;
427 IndexReg = getRegForGEPIndex(Op).first;
428 if (IndexReg == 0)
429 return false;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000430 break;
431 }
432 // Unsupported.
433 goto unsupported_gep;
434 }
Dan Gohman35893082008-09-18 23:23:44 +0000435 }
436 }
Dan Gohman09aae462008-09-26 20:04:15 +0000437 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000438 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000439 break;
Dan Gohman35893082008-09-18 23:23:44 +0000440 // Ok, the GEP indices were covered by constant-offset and scaled-index
441 // addressing. Update the address state and move on to examining the base.
442 AM.IndexReg = IndexReg;
443 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000444 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000445 if (X86SelectAddress(U->getOperand(0), AM))
446 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000447
Chris Lattner225d4ca2010-03-04 19:48:19 +0000448 // If we couldn't merge the sub value into this addr mode, revert back to
449 // our address and just match the value instead of completely failing.
450 AM = SavedAM;
451 break;
Dan Gohman35893082008-09-18 23:23:44 +0000452 unsupported_gep:
453 // Ok, the GEP indices weren't all covered.
454 break;
455 }
456 }
457
458 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000459 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000460 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000461 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000462 return false;
463
Dan Gohman97135e12008-09-26 19:15:30 +0000464 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000465 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000466 (AM.Base.Reg != 0 || AM.IndexReg != 0))
467 return false;
468
Dan Gohmane9865942009-02-23 22:03:08 +0000469 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000470 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000471 if (GVar->isThreadLocal())
472 return false;
473
Chris Lattnerff7727f2009-07-09 06:41:35 +0000474 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000475 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000476
Chris Lattner0d786dd2009-07-10 07:48:51 +0000477 // Allow the subtarget to classify the global.
478 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
479
480 // If this reference is relative to the pic base, set it now.
481 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000482 // FIXME: How do we know Base.Reg is free??
Dan Gohmana4160c32010-07-07 16:29:44 +0000483 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000484 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000485
Chris Lattner0d786dd2009-07-10 07:48:51 +0000486 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000487 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000488 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000489 if (Subtarget->isPICStyleRIPRel()) {
490 // Use rip-relative addressing if we can. Above we verified that the
491 // base and index registers are unused.
492 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
493 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000494 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000495 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000496 return true;
497 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000498
Chris Lattner0d786dd2009-07-10 07:48:51 +0000499 // Ok, we need to do a load from a stub. If we've already loaded from this
500 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000501 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
502 unsigned LoadReg;
503 if (I != LocalValueMap.end() && I->second != 0) {
504 LoadReg = I->second;
505 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000506 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000507 unsigned Opc = 0;
508 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000509 X86AddressMode StubAM;
510 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000511 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000512 StubAM.GVOpFlags = GVFlags;
513
Dan Gohman84023e02010-07-10 09:00:22 +0000514 // Prepare for inserting code in the local-value area.
Dan Gohmana10b8492010-07-14 01:07:44 +0000515 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000516
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000518 Opc = X86::MOV64rm;
519 RC = X86::GR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000520
Chris Lattner0d786dd2009-07-10 07:48:51 +0000521 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000522 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000523 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000524 Opc = X86::MOV32rm;
525 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000526 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000527
Chris Lattnerff7727f2009-07-09 06:41:35 +0000528 LoadReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000529 MachineInstrBuilder LoadMI =
530 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
531 addFullAddress(LoadMI, StubAM);
532
533 // Ok, back to normal mode.
534 leaveLocalValueArea(SaveInsertPt);
535
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000536 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000537 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000538 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000539
Chris Lattnerff7727f2009-07-09 06:41:35 +0000540 // Now construct the final address. Note that the Disp, Scale,
541 // and Index values may already be set here.
542 AM.Base.Reg = LoadReg;
543 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000544 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000545 }
546
Dan Gohman97135e12008-09-26 19:15:30 +0000547 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000548 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000549 if (AM.Base.Reg == 0) {
550 AM.Base.Reg = getRegForValue(V);
551 return AM.Base.Reg != 0;
552 }
553 if (AM.IndexReg == 0) {
554 assert(AM.Scale == 1 && "Scale with no index!");
555 AM.IndexReg = getRegForValue(V);
556 return AM.IndexReg != 0;
557 }
558 }
559
560 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000561}
562
Chris Lattner0aa43de2009-07-10 05:33:42 +0000563/// X86SelectCallAddress - Attempt to fill in an address from the given value.
564///
Dan Gohman46510a72010-04-15 01:51:59 +0000565bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
566 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000567 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000568 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000569 Opcode = I->getOpcode();
570 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000571 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000572 Opcode = C->getOpcode();
573 U = C;
574 }
575
576 switch (Opcode) {
577 default: break;
578 case Instruction::BitCast:
579 // Look past bitcasts.
580 return X86SelectCallAddress(U->getOperand(0), AM);
581
582 case Instruction::IntToPtr:
583 // Look past no-op inttoptrs.
584 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
585 return X86SelectCallAddress(U->getOperand(0), AM);
586 break;
587
588 case Instruction::PtrToInt:
589 // Look past no-op ptrtoints.
590 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
591 return X86SelectCallAddress(U->getOperand(0), AM);
592 break;
593 }
594
595 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000596 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000597 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000598 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000599 return false;
600
601 // RIP-relative addresses can't have additional register operands.
602 if (Subtarget->isPICStyleRIPRel() &&
603 (AM.Base.Reg != 0 || AM.IndexReg != 0))
604 return false;
605
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000606 // Can't handle DLLImport.
607 if (GV->hasDLLImportLinkage())
608 return false;
609
610 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000611 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000612 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000613 return false;
614
615 // Okay, we've committed to selecting this global. Set up the basic address.
616 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000617
Chris Lattnere6c07b52009-07-10 05:45:15 +0000618 // No ABI requires an extra load for anything other than DLLImport, which
619 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000620 if (Subtarget->isPICStyleRIPRel()) {
621 // Use rip-relative addressing if we can. Above we verified that the
622 // base and index registers are unused.
623 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
624 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000625 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000626 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
627 } else if (Subtarget->isPICStyleGOT()) {
628 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000629 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000630
Chris Lattner0aa43de2009-07-10 05:33:42 +0000631 return true;
632 }
633
634 // If all else fails, try to materialize the value in a register.
635 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
636 if (AM.Base.Reg == 0) {
637 AM.Base.Reg = getRegForValue(V);
638 return AM.Base.Reg != 0;
639 }
640 if (AM.IndexReg == 0) {
641 assert(AM.Scale == 1 && "Scale with no index!");
642 AM.IndexReg = getRegForValue(V);
643 return AM.IndexReg != 0;
644 }
645 }
646
647 return false;
648}
649
650
Owen Andersona3971df2008-09-04 07:08:58 +0000651/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000652bool X86FastISel::X86SelectStore(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000653 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000654 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000655 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000656
Dan Gohman0586d912008-09-10 20:11:02 +0000657 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000658 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000659 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000660
Chris Lattner438949a2008-10-15 05:30:52 +0000661 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000662}
663
Dan Gohman84023e02010-07-10 09:00:22 +0000664/// X86SelectRet - Select and emit code to implement ret instructions.
665bool X86FastISel::X86SelectRet(const Instruction *I) {
666 const ReturnInst *Ret = cast<ReturnInst>(I);
667 const Function &F = *I->getParent()->getParent();
668
669 if (!FuncInfo.CanLowerReturn)
670 return false;
671
672 CallingConv::ID CC = F.getCallingConv();
673 if (CC != CallingConv::C &&
674 CC != CallingConv::Fast &&
675 CC != CallingConv::X86_FastCall)
676 return false;
677
678 if (Subtarget->isTargetWin64())
679 return false;
680
681 // Don't handle popping bytes on return for now.
682 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
683 ->getBytesToPopOnReturn() != 0)
684 return 0;
685
686 // fastcc with -tailcallopt is intended to provide a guaranteed
687 // tail call optimization. Fastisel doesn't know how to do that.
688 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
689 return false;
690
691 // Let SDISel handle vararg functions.
692 if (F.isVarArg())
693 return false;
694
695 if (Ret->getNumOperands() > 0) {
696 SmallVector<ISD::OutputArg, 4> Outs;
697 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
698 Outs, TLI);
699
700 // Analyze operands of the call, assigning locations to each operand.
701 SmallVector<CCValAssign, 16> ValLocs;
702 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000703 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000704
705 const Value *RV = Ret->getOperand(0);
706 unsigned Reg = getRegForValue(RV);
707 if (Reg == 0)
708 return false;
709
710 // Only handle a single return value for now.
711 if (ValLocs.size() != 1)
712 return false;
713
714 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000715
Dan Gohman84023e02010-07-10 09:00:22 +0000716 // Don't bother handling odd stuff for now.
717 if (VA.getLocInfo() != CCValAssign::Full)
718 return false;
719 // Only handle register returns for now.
720 if (!VA.isRegLoc())
721 return false;
722 // TODO: For now, don't try to handle cases where getLocInfo()
723 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +0000724 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Dan Gohman84023e02010-07-10 09:00:22 +0000725 return false;
726
727 // The calling-convention tables for x87 returns don't tell
728 // the whole story.
729 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
730 return false;
731
732 // Make the copy.
733 unsigned SrcReg = Reg + VA.getValNo();
734 unsigned DstReg = VA.getLocReg();
735 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000736 // Avoid a cross-class copy. This is very unlikely.
737 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000738 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000739 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
740 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000741
742 // Mark the register as live out of the function.
743 MRI.addLiveOut(VA.getLocReg());
744 }
745
746 // Now emit the RET.
747 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
748 return true;
749}
750
Evan Cheng8b19e562008-09-03 06:44:39 +0000751/// X86SelectLoad - Select and emit code to implement load instructions.
752///
Dan Gohman46510a72010-04-15 01:51:59 +0000753bool X86FastISel::X86SelectLoad(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000754 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000755 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000756 return false;
757
Dan Gohman0586d912008-09-10 20:11:02 +0000758 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000759 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000760 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000761
Evan Cheng0de588f2008-09-05 21:00:03 +0000762 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000763 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000764 UpdateValueMap(I, ResultReg);
765 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000766 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000767 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000768}
769
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000770static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000772 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 case MVT::i8: return X86::CMP8rr;
774 case MVT::i16: return X86::CMP16rr;
775 case MVT::i32: return X86::CMP32rr;
776 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000777 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
778 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000779 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000780}
781
Chris Lattner0e13c782008-10-15 04:13:29 +0000782/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
783/// of the comparison, return an opcode that works for the compare (e.g.
784/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000785static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000787 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000788 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 case MVT::i8: return X86::CMP8ri;
790 case MVT::i16: return X86::CMP16ri;
791 case MVT::i32: return X86::CMP32ri;
792 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000793 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
794 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000795 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000796 return X86::CMP64ri32;
797 return 0;
798 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000799}
800
Dan Gohman46510a72010-04-15 01:51:59 +0000801bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
802 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000803 unsigned Op0Reg = getRegForValue(Op0);
804 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000805
Chris Lattnerd53886b2008-10-15 05:18:04 +0000806 // Handle 'null' like i32/i64 0.
807 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000808 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000809
Chris Lattner9a08a612008-10-15 04:26:38 +0000810 // We have two options: compare with register or immediate. If the RHS of
811 // the compare is an immediate that we can fold into this compare, use
812 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000813 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000814 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000815 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
816 .addReg(Op0Reg)
817 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000818 return true;
819 }
820 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000821
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000822 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000823 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000824
Chris Lattner9a08a612008-10-15 04:26:38 +0000825 unsigned Op1Reg = getRegForValue(Op1);
826 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000827 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
828 .addReg(Op0Reg)
829 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000830
Chris Lattner9a08a612008-10-15 04:26:38 +0000831 return true;
832}
833
Dan Gohman46510a72010-04-15 01:51:59 +0000834bool X86FastISel::X86SelectCmp(const Instruction *I) {
835 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000836
Duncan Sands1440e8b2010-11-03 11:35:31 +0000837 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000838 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000839 return false;
840
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000841 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000842 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000843 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000844 switch (CI->getPredicate()) {
845 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000846 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
847 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000848
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000849 unsigned EReg = createResultReg(&X86::GR8RegClass);
850 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000851 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
852 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
853 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000854 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000855 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000856 UpdateValueMap(I, ResultReg);
857 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000858 }
859 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000860 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
861 return false;
862
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000863 unsigned NEReg = createResultReg(&X86::GR8RegClass);
864 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000865 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
866 TII.get(X86::SETNEr), NEReg);
867 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
868 TII.get(X86::SETPr), PReg);
869 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
870 TII.get(X86::OR8rr), ResultReg)
871 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000872 UpdateValueMap(I, ResultReg);
873 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000874 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000875 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
876 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
877 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
878 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
879 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
880 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
881 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
882 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
883 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
884 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
885 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
886 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000887
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000888 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
889 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
890 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
891 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
892 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
893 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
894 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
895 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
896 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
897 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000898 default:
899 return false;
900 }
901
Dan Gohman46510a72010-04-15 01:51:59 +0000902 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000903 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000904 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000905
Chris Lattner9a08a612008-10-15 04:26:38 +0000906 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000907 if (!X86FastEmitCompare(Op0, Op1, VT))
908 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000909
Dan Gohman84023e02010-07-10 09:00:22 +0000910 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000911 UpdateValueMap(I, ResultReg);
912 return true;
913}
Evan Cheng8b19e562008-09-03 06:44:39 +0000914
Dan Gohman46510a72010-04-15 01:51:59 +0000915bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000916 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000917 if (I->getType()->isIntegerTy(8) &&
918 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000919 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000920 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000921 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000922 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000923 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000924 UpdateValueMap(I, ResultReg);
925 return true;
926 }
927
928 return false;
929}
930
Chris Lattner9a08a612008-10-15 04:26:38 +0000931
Dan Gohman46510a72010-04-15 01:51:59 +0000932bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000933 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000934 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000935 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000936 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
937 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000938
Dan Gohman8bef7442010-08-21 02:32:36 +0000939 // Fold the common case of a conditional branch with a comparison
940 // in the same block (values defined on other blocks may not have
941 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000942 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000943 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000944 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000945
Dan Gohmand98d6202008-10-02 22:15:21 +0000946 // Try to take advantage of fallthrough opportunities.
947 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000948 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000949 std::swap(TrueMBB, FalseMBB);
950 Predicate = CmpInst::getInversePredicate(Predicate);
951 }
952
Chris Lattner871d2462008-10-15 03:58:05 +0000953 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
954 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
955
Dan Gohmand98d6202008-10-02 22:15:21 +0000956 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000957 case CmpInst::FCMP_OEQ:
958 std::swap(TrueMBB, FalseMBB);
959 Predicate = CmpInst::FCMP_UNE;
960 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000961 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
962 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
963 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
964 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
965 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
966 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
967 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
968 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
969 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
970 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
971 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
972 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
973 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000974
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000975 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
976 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
977 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
978 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
979 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
980 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
981 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
982 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
983 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
984 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000985 default:
986 return false;
987 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000988
Dan Gohman46510a72010-04-15 01:51:59 +0000989 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000990 if (SwapArgs)
991 std::swap(Op0, Op1);
992
Chris Lattner9a08a612008-10-15 04:26:38 +0000993 // Emit a compare of the LHS and RHS, setting the flags.
994 if (!X86FastEmitCompare(Op0, Op1, VT))
995 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000996
Dan Gohman84023e02010-07-10 09:00:22 +0000997 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
998 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000999
1000 if (Predicate == CmpInst::FCMP_UNE) {
1001 // X86 requires a second branch to handle UNE (and OEQ,
1002 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001003 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1004 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001005 }
1006
Stuart Hastings3bf91252010-06-17 22:43:56 +00001007 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001008 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001009 return true;
1010 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001011 } else if (ExtractValueInst *EI =
1012 dyn_cast<ExtractValueInst>(BI->getCondition())) {
1013 // Check to see if the branch instruction is from an "arithmetic with
1014 // overflow" intrinsic. The main way these intrinsics are used is:
1015 //
1016 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1017 // %sum = extractvalue { i32, i1 } %t, 0
1018 // %obit = extractvalue { i32, i1 } %t, 1
1019 // br i1 %obit, label %overflow, label %normal
1020 //
Dan Gohman653456c2009-01-07 00:15:08 +00001021 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +00001022 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +00001023 // looking for the SETO/SETB instruction. If an instruction modifies the
1024 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1025 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001026 if (const IntrinsicInst *CI =
1027 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001028 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1029 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1030 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001031 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001032
Chris Lattnera9a42252009-04-12 07:36:01 +00001033 for (MachineBasicBlock::const_reverse_iterator
Dan Gohman84023e02010-07-10 09:00:22 +00001034 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1035 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001036 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001037
Evan Cheng1015ba72010-05-21 20:53:24 +00001038 if (MI.definesRegister(Reg)) {
Jakob Stoklund Olesen84d499a2010-07-16 22:35:34 +00001039 if (MI.isCopy()) {
1040 Reg = MI.getOperand(1).getReg();
Chris Lattnera9a42252009-04-12 07:36:01 +00001041 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001042 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001043
Chris Lattnera9a42252009-04-12 07:36:01 +00001044 SetMI = &MI;
1045 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001046 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001047
Chris Lattnera9a42252009-04-12 07:36:01 +00001048 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengc36b7062011-01-07 23:50:32 +00001049 if (TID.hasImplicitDefOfPhysReg(X86::EFLAGS) ||
1050 MI.hasUnmodeledSideEffects())
Chris Lattnera9a42252009-04-12 07:36:01 +00001051 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001052 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001053
1054 if (SetMI) {
1055 unsigned OpCode = SetMI->getOpcode();
1056
1057 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohman84023e02010-07-10 09:00:22 +00001058 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1059 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001060 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001061 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001062 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001063 return true;
1064 }
Bill Wendling9a901322008-12-10 19:44:24 +00001065 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001066 }
1067 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001068 }
1069
1070 // Otherwise do a clumsy setcc and re-test it.
1071 unsigned OpReg = getRegForValue(BI->getCondition());
1072 if (OpReg == 0) return false;
1073
Dan Gohman84023e02010-07-10 09:00:22 +00001074 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1075 .addReg(OpReg).addReg(OpReg);
1076 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1077 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001078 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001079 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001080 return true;
1081}
1082
Dan Gohman46510a72010-04-15 01:51:59 +00001083bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +00001084 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001085 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001086 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001087 CReg = X86::CL;
1088 RC = &X86::GR8RegClass;
1089 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001090 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
1091 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
1092 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001093 default: return false;
1094 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001095 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001096 CReg = X86::CX;
1097 RC = &X86::GR16RegClass;
1098 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001099 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1100 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1101 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001102 default: return false;
1103 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001104 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001105 CReg = X86::ECX;
1106 RC = &X86::GR32RegClass;
1107 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001108 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1109 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1110 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001111 default: return false;
1112 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001113 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001114 CReg = X86::RCX;
1115 RC = &X86::GR64RegClass;
1116 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001117 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1118 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1119 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001120 default: return false;
1121 }
1122 } else {
1123 return false;
1124 }
1125
Duncan Sands1440e8b2010-11-03 11:35:31 +00001126 MVT VT;
1127 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001128 return false;
1129
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001130 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1131 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001132
Chris Lattner743922e2008-09-21 21:44:29 +00001133 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001134 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001135 unsigned ResultReg = createResultReg(RC);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001137 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001138 UpdateValueMap(I, ResultReg);
1139 return true;
1140 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001141
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001142 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1143 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1145 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001146
1147 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001148 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001149 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001150 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1151 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001152 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001153
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001154 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001155 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1156 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001157 UpdateValueMap(I, ResultReg);
1158 return true;
1159}
1160
Dan Gohman46510a72010-04-15 01:51:59 +00001161bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001162 MVT VT;
1163 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001164 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001165
Eric Christophere487b012010-09-29 23:00:29 +00001166 // We only use cmov here, if we don't have a cmov instruction bail.
1167 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001168
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001169 unsigned Opc = 0;
1170 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001171 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001172 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001173 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001174 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001175 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001176 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001177 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001178 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001179 RC = &X86::GR64RegClass;
1180 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001181 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001182 }
1183
1184 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1185 if (Op0Reg == 0) return false;
1186 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1187 if (Op1Reg == 0) return false;
1188 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1189 if (Op2Reg == 0) return false;
1190
Dan Gohman84023e02010-07-10 09:00:22 +00001191 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1192 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001193 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001194 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1195 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001196 UpdateValueMap(I, ResultReg);
1197 return true;
1198}
1199
Dan Gohman46510a72010-04-15 01:51:59 +00001200bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001201 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001202 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001203 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001204 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001205 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001206 unsigned OpReg = getRegForValue(V);
1207 if (OpReg == 0) return false;
1208 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001209 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1210 TII.get(X86::CVTSS2SDrr), ResultReg)
1211 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001212 UpdateValueMap(I, ResultReg);
1213 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001214 }
1215 }
1216
1217 return false;
1218}
1219
Dan Gohman46510a72010-04-15 01:51:59 +00001220bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001221 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001222 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001223 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001224 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001225 unsigned OpReg = getRegForValue(V);
1226 if (OpReg == 0) return false;
1227 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001228 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1229 TII.get(X86::CVTSD2SSrr), ResultReg)
1230 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001231 UpdateValueMap(I, ResultReg);
1232 return true;
1233 }
1234 }
1235 }
1236
1237 return false;
1238}
1239
Dan Gohman46510a72010-04-15 01:51:59 +00001240bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001241 if (Subtarget->is64Bit())
1242 // All other cases should be handled by the tblgen generated code.
1243 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001244 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1245 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001246
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001247 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001248 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001249 // All other cases should be handled by the tblgen generated code.
1250 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001252 // All other cases should be handled by the tblgen generated code.
1253 return false;
1254
1255 unsigned InputReg = getRegForValue(I->getOperand(0));
1256 if (!InputReg)
1257 // Unhandled operand. Halt "fast" selection and bail.
1258 return false;
1259
Dan Gohman62417622009-04-27 16:33:14 +00001260 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001261 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001262 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001263 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001264 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1265 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001266
1267 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001269 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001270 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001271 if (!ResultReg)
1272 return false;
1273
1274 UpdateValueMap(I, ResultReg);
1275 return true;
1276}
1277
Dan Gohman46510a72010-04-15 01:51:59 +00001278bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1279 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1280 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001281
Dan Gohman46510a72010-04-15 01:51:59 +00001282 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001283 switch (CI->getIntrinsicID()) {
1284 default: break;
1285 case Intrinsic::sadd_with_overflow:
Dan Gohman84023e02010-07-10 09:00:22 +00001286 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001287 // Cheat a little. We know that the registers for "add" and "seto" are
1288 // allocated sequentially. However, we only keep track of the register
1289 // for "add" in the value map. Use extractvalue's index to get the
1290 // correct register for "seto".
Dan Gohman84023e02010-07-10 09:00:22 +00001291 unsigned OpReg = getRegForValue(Agg);
1292 if (OpReg == 0)
1293 return false;
1294 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001295 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001296 }
Dan Gohman84023e02010-07-10 09:00:22 +00001297 }
Bill Wendling52370a12008-12-09 02:42:50 +00001298 }
1299
1300 return false;
1301}
1302
Dan Gohman46510a72010-04-15 01:51:59 +00001303bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001304 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001305 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001306 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001307 case Intrinsic::stackprotector: {
1308 // Emit code inline code to store the stack guard onto the stack.
1309 EVT PtrTy = TLI.getPointerTy();
1310
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001311 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1312 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001313
1314 // Grab the frame index.
1315 X86AddressMode AM;
1316 if (!X86SelectAddress(Slot, AM)) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001317
Eric Christopher88dee302010-03-18 21:58:33 +00001318 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001319
Eric Christopher07754c22010-03-18 20:27:26 +00001320 return true;
1321 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001322 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001323 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001324 const Type *Ty = I.getCalledFunction()->getReturnType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001325
Eric Christopherf27805b2010-03-11 06:20:22 +00001326 assert(CI && "Non-constant type in Intrinsic::objectsize?");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001327
Duncan Sands1440e8b2010-11-03 11:35:31 +00001328 MVT VT;
Eric Christopherf27805b2010-03-11 06:20:22 +00001329 if (!isTypeLegal(Ty, VT))
1330 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001331
Eric Christopherf27805b2010-03-11 06:20:22 +00001332 unsigned OpC = 0;
1333 if (VT == MVT::i32)
1334 OpC = X86::MOV32ri;
1335 else if (VT == MVT::i64)
1336 OpC = X86::MOV64ri;
1337 else
1338 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001339
Eric Christopherf27805b2010-03-11 06:20:22 +00001340 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001341 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001342 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001343 UpdateValueMap(&I, ResultReg);
1344 return true;
1345 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001346 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001347 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001348 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001349 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001350 if (!X86SelectAddress(DI->getAddress(), AM))
1351 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001352 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001353 // FIXME may need to add RegState::Debug to any registers produced,
1354 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001355 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1356 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001357 return true;
1358 }
Eric Christopher77f79892010-01-18 22:11:29 +00001359 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001360 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001361 return true;
1362 }
Bill Wendling52370a12008-12-09 02:42:50 +00001363 case Intrinsic::sadd_with_overflow:
1364 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001365 // Replace "add with overflow" intrinsics with an "add" instruction followed
1366 // by a seto/setc instruction. Later on, when the "extractvalue"
1367 // instructions are encountered, we use the fact that two registers were
1368 // created sequentially to get the correct registers for the "sum" and the
1369 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001370 const Function *Callee = I.getCalledFunction();
1371 const Type *RetTy =
1372 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1373
Duncan Sands1440e8b2010-11-03 11:35:31 +00001374 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001375 if (!isTypeLegal(RetTy, VT))
1376 return false;
1377
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001378 const Value *Op1 = I.getArgOperand(0);
1379 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001380 unsigned Reg1 = getRegForValue(Op1);
1381 unsigned Reg2 = getRegForValue(Op2);
1382
1383 if (Reg1 == 0 || Reg2 == 0)
1384 // FIXME: Handle values *not* in registers.
1385 return false;
1386
1387 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001388 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001389 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001390 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001391 OpC = X86::ADD64rr;
1392 else
1393 return false;
1394
1395 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001396 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1397 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001398 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001399
Chris Lattner8d57b772009-04-12 07:51:14 +00001400 // If the add with overflow is an intra-block value then we just want to
1401 // create temporaries for it like normal. If it is a cross-block value then
1402 // UpdateValueMap will return the cross-block register used. Since we
1403 // *really* want the value to be live in the register pair known by
1404 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1405 // the cross block case. In the non-cross-block case, we should just make
1406 // another register for the value.
1407 if (DestReg1 != ResultReg)
1408 ResultReg = DestReg1+1;
1409 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001410 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001411
Chris Lattnera9a42252009-04-12 07:36:01 +00001412 unsigned Opc = X86::SETBr;
1413 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1414 Opc = X86::SETOr;
Dan Gohman84023e02010-07-10 09:00:22 +00001415 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001416 return true;
1417 }
1418 }
1419}
1420
Dan Gohman46510a72010-04-15 01:51:59 +00001421bool X86FastISel::X86SelectCall(const Instruction *I) {
1422 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001423 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001424
1425 // Can't handle inline asm yet.
1426 if (isa<InlineAsm>(Callee))
1427 return false;
1428
Bill Wendling52370a12008-12-09 02:42:50 +00001429 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001430 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001431 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001432
Evan Chengf3d4efe2008-09-07 09:09:33 +00001433 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001434 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001435 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001436 if (CC != CallingConv::C &&
1437 CC != CallingConv::Fast &&
1438 CC != CallingConv::X86_FastCall)
1439 return false;
1440
Evan Cheng381993f2010-01-27 00:00:57 +00001441 // fastcc with -tailcallopt is intended to provide a guaranteed
1442 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001443 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001444 return false;
1445
Evan Chengf3d4efe2008-09-07 09:09:33 +00001446 // Let SDISel handle vararg functions.
1447 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1448 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1449 if (FTy->isVarArg())
1450 return false;
1451
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001452 // Fast-isel doesn't know about callee-pop yet.
1453 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1454 return false;
1455
Evan Chengf3d4efe2008-09-07 09:09:33 +00001456 // Handle *simple* calls for now.
1457 const Type *RetTy = CS.getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001458 MVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001459 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001460 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001461 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001462 return false;
1463
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001464 // Materialize callee address in a register. FIXME: GV address can be
1465 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001466 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001467 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001468 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001469 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001470 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001471 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001472 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001473 } else if (CalleeAM.Base.Reg != 0) {
1474 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001475 } else
1476 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001477
Evan Chengdebdea02008-09-08 17:15:42 +00001478 // Allow calls which produce i1 results.
1479 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 if (RetVT == MVT::i1) {
1481 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001482 AndToI1 = true;
1483 }
1484
Evan Chengf3d4efe2008-09-07 09:09:33 +00001485 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001486 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001487 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001488 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001489 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001490 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001491 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001492 ArgVTs.reserve(CS.arg_size());
1493 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001494 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001495 i != e; ++i) {
1496 unsigned Arg = getRegForValue(*i);
1497 if (Arg == 0)
1498 return false;
1499 ISD::ArgFlagsTy Flags;
1500 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001501 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001502 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001503 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001504 Flags.setZExt();
1505
1506 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001507 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1508 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1509 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1510 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001511 return false;
1512
1513 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001514 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001515 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001516 return false;
1517 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1518 Flags.setOrigAlign(OriginalAlignment);
1519
1520 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001521 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001522 ArgVTs.push_back(ArgVT);
1523 ArgFlags.push_back(Flags);
1524 }
1525
1526 // Analyze operands of the call, assigning locations to each operand.
1527 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001528 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001529
Dan Gohmand8acddd2010-06-01 21:09:47 +00001530 // Allocate shadow area for Win64
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001531 if (Subtarget->isTargetWin64()) {
1532 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001533 }
1534
Duncan Sands45907662010-10-31 13:21:44 +00001535 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001536
1537 // Get a count of how many bytes are to be pushed on the stack.
1538 unsigned NumBytes = CCInfo.getNextStackOffset();
1539
1540 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001541 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001542 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1543 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001544
Chris Lattner438949a2008-10-15 05:30:52 +00001545 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001546 // copies / loads.
1547 SmallVector<unsigned, 4> RegArgs;
1548 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1549 CCValAssign &VA = ArgLocs[i];
1550 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001551 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001552
Evan Chengf3d4efe2008-09-07 09:09:33 +00001553 // Promote the value if needed.
1554 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001555 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001556 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001557 case CCValAssign::SExt: {
1558 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1559 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001560 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001561 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001562 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001563 }
1564 case CCValAssign::ZExt: {
1565 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1566 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001567 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001568 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001569 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001570 }
1571 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001572 // We don't handle MMX parameters yet.
1573 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1574 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001575 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1576 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001577 if (!Emitted)
1578 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001579 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001580 if (!Emitted)
1581 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1582 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001583
Chris Lattnerc46ec642011-01-05 22:26:52 +00001584 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001585 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001586 break;
1587 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001588 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001589 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001590 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001591 assert(BC != 0 && "Failed to emit a bitcast!");
1592 Arg = BC;
1593 ArgVT = VA.getLocVT();
1594 break;
1595 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001596 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001597
Evan Chengf3d4efe2008-09-07 09:09:33 +00001598 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001599 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1600 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001601 RegArgs.push_back(VA.getLocReg());
1602 } else {
1603 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001604 X86AddressMode AM;
1605 AM.Base.Reg = StackPtr;
1606 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001607 const Value *ArgVal = ArgVals[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001608
Chris Lattner241ab472008-10-15 05:38:32 +00001609 // If this is a really simple value, emit this with the Value* version of
1610 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1611 // can cause us to reevaluate the argument.
1612 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1613 X86FastEmitStore(ArgVT, ArgVal, AM);
1614 else
1615 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001616 }
1617 }
1618
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001619 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001620 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001621 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001622 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001623 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1624 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001625 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001626
Evan Chengf3d4efe2008-09-07 09:09:33 +00001627 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001628 MachineInstrBuilder MIB;
1629 if (CalleeOp) {
1630 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001631 unsigned CallOpc;
1632 if (Subtarget->isTargetWin64())
1633 CallOpc = X86::WINCALL64r;
1634 else if (Subtarget->is64Bit())
1635 CallOpc = X86::CALL64r;
1636 else
1637 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001638 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1639 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001640
Chris Lattner51e8eab2009-07-09 06:34:26 +00001641 } else {
1642 // Direct call.
1643 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001644 unsigned CallOpc;
1645 if (Subtarget->isTargetWin64())
1646 CallOpc = X86::WINCALL64pcrel32;
1647 else if (Subtarget->is64Bit())
1648 CallOpc = X86::CALL64pcrel32;
1649 else
1650 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001651
Chris Lattner51e8eab2009-07-09 06:34:26 +00001652 // See if we need any target-specific flags on the GV operand.
1653 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001654
Chris Lattner51e8eab2009-07-09 06:34:26 +00001655 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1656 // external symbols most go through the PLT in PIC mode. If the symbol
1657 // has hidden or protected visibility, or if it is static or local, then
1658 // we don't need to use the PLT - we can directly call it.
1659 if (Subtarget->isTargetELF() &&
1660 TM.getRelocationModel() == Reloc::PIC_ &&
1661 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1662 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001663 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001664 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1665 Subtarget->getDarwinVers() < 9) {
1666 // PC-relative references to external symbols should go through $stub,
1667 // unless we're building with the leopard linker or later, which
1668 // automatically synthesizes these stubs.
1669 OpFlags = X86II::MO_DARWIN_STUB;
1670 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001671
1672
Dan Gohman84023e02010-07-10 09:00:22 +00001673 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1674 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001675 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001676
1677 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001678 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001679 MIB.addReg(X86::EBX);
1680
Evan Chengf3d4efe2008-09-07 09:09:33 +00001681 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001682 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1683 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001684
1685 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001686 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001687 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1688 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001689
1690 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001691 SmallVector<unsigned, 4> UsedRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001692 if (RetVT != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001693 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001694 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001695 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1696
1697 // Copy all of the result registers out of their specified physreg.
1698 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001699 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001700 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001701
Evan Chengf3d4efe2008-09-07 09:09:33 +00001702 // If this is a call to a function that returns an fp value on the x87 fp
1703 // stack, but where we prefer to use the value in xmm registers, copy it
1704 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1705 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1706 RVLocs[0].getLocReg() == X86::ST1) &&
1707 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001709 DstRC = X86::RFP80RegisterClass;
1710 }
1711
1712 unsigned ResultReg = createResultReg(DstRC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001713 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1714 ResultReg).addReg(RVLocs[0].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001715 UsedRegs.push_back(RVLocs[0].getLocReg());
1716
Evan Chengf3d4efe2008-09-07 09:09:33 +00001717 if (CopyVT != RVLocs[0].getValVT()) {
1718 // Round the F80 the right size, which also moves to the appropriate xmm
1719 // register. This is accomplished by storing the F80 value in memory and
1720 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001721 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001723 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001724 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001725 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1726 TII.get(Opc)), FI)
1727 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001729 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001730 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001731 ResultReg = createResultReg(DstRC);
Dan Gohman84023e02010-07-10 09:00:22 +00001732 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1733 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001734 }
1735
Evan Chengdebdea02008-09-08 17:15:42 +00001736 if (AndToI1) {
1737 // Mask out all but lowest bit for some call which produces an i1.
1738 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001739 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001740 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001741 ResultReg = AndResult;
1742 }
1743
Evan Chengf3d4efe2008-09-07 09:09:33 +00001744 UpdateValueMap(I, ResultReg);
1745 }
1746
Dan Gohmandb497122010-06-18 23:28:01 +00001747 // Set all unused physreg defs as dead.
1748 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1749
Evan Chengf3d4efe2008-09-07 09:09:33 +00001750 return true;
1751}
1752
1753
Dan Gohman99b21822008-08-28 23:21:34 +00001754bool
Dan Gohman46510a72010-04-15 01:51:59 +00001755X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001756 switch (I->getOpcode()) {
1757 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001758 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001759 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001760 case Instruction::Store:
1761 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001762 case Instruction::Ret:
1763 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001764 case Instruction::ICmp:
1765 case Instruction::FCmp:
1766 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001767 case Instruction::ZExt:
1768 return X86SelectZExt(I);
1769 case Instruction::Br:
1770 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001771 case Instruction::Call:
1772 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001773 case Instruction::LShr:
1774 case Instruction::AShr:
1775 case Instruction::Shl:
1776 return X86SelectShift(I);
1777 case Instruction::Select:
1778 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001779 case Instruction::Trunc:
1780 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001781 case Instruction::FPExt:
1782 return X86SelectFPExt(I);
1783 case Instruction::FPTrunc:
1784 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001785 case Instruction::ExtractValue:
1786 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001787 case Instruction::IntToPtr: // Deliberate fall-through.
1788 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001789 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1790 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001791 if (DstVT.bitsGT(SrcVT))
1792 return X86SelectZExt(I);
1793 if (DstVT.bitsLT(SrcVT))
1794 return X86SelectTrunc(I);
1795 unsigned Reg = getRegForValue(I->getOperand(0));
1796 if (Reg == 0) return false;
1797 UpdateValueMap(I, Reg);
1798 return true;
1799 }
Dan Gohman99b21822008-08-28 23:21:34 +00001800 }
1801
1802 return false;
1803}
1804
Dan Gohman46510a72010-04-15 01:51:59 +00001805unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001806 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001807 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001808 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001809
Owen Anderson95267a12008-09-05 00:06:23 +00001810 // Get opcode and regclass of the output for the given load instruction.
1811 unsigned Opc = 0;
1812 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001813 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001814 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001816 Opc = X86::MOV8rm;
1817 RC = X86::GR8RegisterClass;
1818 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001820 Opc = X86::MOV16rm;
1821 RC = X86::GR16RegisterClass;
1822 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001823 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001824 Opc = X86::MOV32rm;
1825 RC = X86::GR32RegisterClass;
1826 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001827 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001828 // Must be in x86-64 mode.
1829 Opc = X86::MOV64rm;
1830 RC = X86::GR64RegisterClass;
1831 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001832 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001833 if (Subtarget->hasSSE1()) {
1834 Opc = X86::MOVSSrm;
1835 RC = X86::FR32RegisterClass;
1836 } else {
1837 Opc = X86::LD_Fp32m;
1838 RC = X86::RFP32RegisterClass;
1839 }
1840 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001842 if (Subtarget->hasSSE2()) {
1843 Opc = X86::MOVSDrm;
1844 RC = X86::FR64RegisterClass;
1845 } else {
1846 Opc = X86::LD_Fp64m;
1847 RC = X86::RFP64RegisterClass;
1848 }
1849 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001851 // No f80 support yet.
1852 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001853 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001854
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001855 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001856 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001857 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001858 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001860 Opc = X86::LEA32r;
1861 else
1862 Opc = X86::LEA64r;
1863 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001864 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1865 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001866 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001867 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001868 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001869 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001870
Owen Anderson3b217c62008-09-06 01:11:01 +00001871 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001872 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001873 if (Align == 0) {
1874 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001875 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001876 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001877
Dan Gohman5396c992008-09-30 01:21:32 +00001878 // x86-32 PIC requires a PIC base register for constant pools.
1879 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001880 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001881 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001882 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001883 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001884 } else if (Subtarget->isPICStyleGOT()) {
1885 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001886 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001887 } else if (Subtarget->isPICStyleRIPRel() &&
1888 TM.getCodeModel() == CodeModel::Small) {
1889 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001890 }
Dan Gohman5396c992008-09-30 01:21:32 +00001891
1892 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001893 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001894 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001895 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1896 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001897 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001898
Owen Anderson95267a12008-09-05 00:06:23 +00001899 return ResultReg;
1900}
1901
Dan Gohman46510a72010-04-15 01:51:59 +00001902unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001903 // Fail on dynamic allocas. At this point, getRegForValue has already
1904 // checked its CSE maps, so if we're here trying to handle a dynamic
1905 // alloca, we're not going to succeed. X86SelectAddress has a
1906 // check for dynamic allocas, because it's called directly from
1907 // various places, but TargetMaterializeAlloca also needs a check
1908 // in order to avoid recursion between getRegForValue,
1909 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001910 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001911 return 0;
1912
Dan Gohman0586d912008-09-10 20:11:02 +00001913 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001914 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001915 return 0;
1916 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1917 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1918 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001919 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1920 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001921 return ResultReg;
1922}
1923
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001924/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
1925/// vreg is being provided by the specified load instruction. If possible,
1926/// try to fold the load as an operand to the instruction, returning true if
1927/// possible.
1928bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
1929 const LoadInst *LI) {
1930 X86AddressMode AM;
1931 if (!X86SelectAddress(LI->getOperand(0), AM))
1932 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001933
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001934 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001935
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001936 unsigned Size = TD.getTypeAllocSize(LI->getType());
1937 unsigned Alignment = LI->getAlignment();
1938
1939 SmallVector<MachineOperand, 8> AddrOps;
1940 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001941
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001942 MachineInstr *Result =
1943 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
1944 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001945
Chris Lattnerb99fdee2011-01-16 02:27:38 +00001946 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001947 MI->eraseFromParent();
1948 return true;
1949}
1950
1951
Evan Chengc3f44b02008-09-03 00:03:49 +00001952namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001953 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1954 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001955 }
Dan Gohman99b21822008-08-28 23:21:34 +00001956}