blob: 0be9dfa06c4469f427b16edbf456545ac1e74274 [file] [log] [blame]
Dan Gohman1adf1b02008-08-19 21:45:35 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
Evan Cheng8b19e562008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Evan Cheng88e30412008-09-03 01:04:47 +000018#include "X86RegisterInfo.h"
19#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000020#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000021#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000022#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000023#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000025#include "llvm/IntrinsicInst.h"
Jay Foad562b84b2011-04-11 09:35:34 +000026#include "llvm/Operator.h"
Dan Gohman84023e02010-07-10 09:00:22 +000027#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000028#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000029#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000030#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000033#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000035#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000036#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000037using namespace llvm;
38
Chris Lattner087fcf32009-03-08 18:44:31 +000039namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000040
Evan Chengc3f44b02008-09-03 00:03:49 +000041class X86FastISel : public FastISel {
42 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
43 /// make the right decision when generating code for different targets.
44 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000045
46 /// StackPtr - Register used as the stack pointer.
47 ///
48 unsigned StackPtr;
49
Wesley Peckbf17cfa2010-11-23 03:31:01 +000050 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000051 /// floating point ops.
52 /// When SSE is available, use it for f32 operations.
53 /// When SSE2 is available, use it for f64 operations.
54 bool X86ScalarSSEf64;
55 bool X86ScalarSSEf32;
56
Evan Cheng8b19e562008-09-03 06:44:39 +000057public:
Dan Gohmana4160c32010-07-07 16:29:44 +000058 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000059 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000060 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
61 X86ScalarSSEf64 = Subtarget->hasSSE2();
62 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000063 }
Evan Chengc3f44b02008-09-03 00:03:49 +000064
Dan Gohman46510a72010-04-15 01:51:59 +000065 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000066
Chris Lattnerbeac75d2010-09-05 02:18:34 +000067 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
68 /// vreg is being provided by the specified load instruction. If possible,
69 /// try to fold the load as an operand to the instruction, returning true if
70 /// possible.
71 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
72 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000073
Dan Gohman1adf1b02008-08-19 21:45:35 +000074#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000075
76private:
Dan Gohman46510a72010-04-15 01:51:59 +000077 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000078
Owen Andersone50ed302009-08-10 22:56:29 +000079 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Chris Lattnerb44101c2011-04-19 05:09:50 +000081 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
82 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000083
Owen Andersone50ed302009-08-10 22:56:29 +000084 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000085 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000086
Dan Gohman46510a72010-04-15 01:51:59 +000087 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
88 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000089
Dan Gohman46510a72010-04-15 01:51:59 +000090 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000093
Dan Gohman84023e02010-07-10 09:00:22 +000094 bool X86SelectRet(const Instruction *I);
95
Dan Gohman46510a72010-04-15 01:51:59 +000096 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000097
Dan Gohman46510a72010-04-15 01:51:59 +000098 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000099
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000105
Dan Gohman46510a72010-04-15 01:51:59 +0000106 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000107
Dan Gohman46510a72010-04-15 01:51:59 +0000108 bool X86SelectFPExt(const Instruction *I);
109 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000110
Dan Gohman46510a72010-04-15 01:51:59 +0000111 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000112
Dan Gohman46510a72010-04-15 01:51:59 +0000113 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
114 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000115
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000116 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000117 return getTargetMachine()->getInstrInfo();
118 }
119 const X86TargetMachine *getTargetMachine() const {
120 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000121 }
122
Dan Gohman46510a72010-04-15 01:51:59 +0000123 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000124
Dan Gohman46510a72010-04-15 01:51:59 +0000125 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000126
127 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
128 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000129 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
131 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000132 }
133
Duncan Sands1440e8b2010-11-03 11:35:31 +0000134 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000135};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000136
Chris Lattner087fcf32009-03-08 18:44:31 +0000137} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000138
Duncan Sands1440e8b2010-11-03 11:35:31 +0000139bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
140 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
141 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000142 // Unhandled type. Halt "fast" selection and bail.
143 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000144
145 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000146 // For now, require SSE/SSE2 for performing floating-point operations,
147 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000149 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000151 return false;
152 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000154 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000155 // We only handle legal types. For example, on x86-32 the instruction
156 // selector contains all of the 64-bit instructions from x86-64,
157 // under the assumption that i64 won't be used if the target doesn't
158 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000160}
161
162#include "X86GenCallingConv.inc"
163
Evan Cheng0de588f2008-09-05 21:00:03 +0000164/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000165/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000166/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000167bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000168 unsigned &ResultReg) {
169 // Get opcode and regclass of the output for the given load instruction.
170 unsigned Opc = 0;
171 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000173 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000174 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000176 Opc = X86::MOV8rm;
177 RC = X86::GR8RegisterClass;
178 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000180 Opc = X86::MOV16rm;
181 RC = X86::GR16RegisterClass;
182 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000184 Opc = X86::MOV32rm;
185 RC = X86::GR32RegisterClass;
186 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000188 // Must be in x86-64 mode.
189 Opc = X86::MOV64rm;
190 RC = X86::GR64RegisterClass;
191 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 if (Subtarget->hasSSE1()) {
194 Opc = X86::MOVSSrm;
195 RC = X86::FR32RegisterClass;
196 } else {
197 Opc = X86::LD_Fp32m;
198 RC = X86::RFP32RegisterClass;
199 }
200 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000202 if (Subtarget->hasSSE2()) {
203 Opc = X86::MOVSDrm;
204 RC = X86::FR64RegisterClass;
205 } else {
206 Opc = X86::LD_Fp64m;
207 RC = X86::RFP64RegisterClass;
208 }
209 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000211 // No f80 support yet.
212 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000213 }
214
215 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000216 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
217 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000218 return true;
219}
220
Evan Chengf3d4efe2008-09-07 09:09:33 +0000221/// X86FastEmitStore - Emit a machine instruction to store a value Val of
222/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
223/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000224/// i.e. V. Return true if it is possible.
225bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000226X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000227 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000228 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 switch (VT.getSimpleVT().SimpleTy) {
230 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000231 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000232 case MVT::i1: {
233 // Mask out all but lowest bit.
234 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000235 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000236 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
237 Val = AndResult;
238 }
239 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 case MVT::i8: Opc = X86::MOV8mr; break;
241 case MVT::i16: Opc = X86::MOV16mr; break;
242 case MVT::i32: Opc = X86::MOV32mr; break;
243 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
244 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000245 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000246 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000248 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000249 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000250 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000251
Dan Gohman84023e02010-07-10 09:00:22 +0000252 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
253 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000254 return true;
255}
256
Dan Gohman46510a72010-04-15 01:51:59 +0000257bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000258 const X86AddressMode &AM) {
259 // Handle 'null' like i32/i64 0.
260 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000261 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000262
Chris Lattner438949a2008-10-15 05:30:52 +0000263 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000264 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000265 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000266 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000268 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000269 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 case MVT::i8: Opc = X86::MOV8mi; break;
271 case MVT::i16: Opc = X86::MOV16mi; break;
272 case MVT::i32: Opc = X86::MOV32mi; break;
273 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000274 // Must be a 32-bit sign extended value.
275 if ((int)CI->getSExtValue() == CI->getSExtValue())
276 Opc = X86::MOV64mi32;
277 break;
278 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000279
Chris Lattner438949a2008-10-15 05:30:52 +0000280 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000281 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
282 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000283 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000284 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000285 return true;
286 }
287 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000288
Chris Lattner438949a2008-10-15 05:30:52 +0000289 unsigned ValReg = getRegForValue(Val);
290 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000291 return false;
292
Chris Lattner438949a2008-10-15 05:30:52 +0000293 return X86FastEmitStore(VT, ValReg, AM);
294}
295
Evan Cheng24e3a902008-09-08 06:35:17 +0000296/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
297/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
298/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000299bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
300 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000301 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000302 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
303 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000304
Owen Andersonac34a002008-09-11 19:44:55 +0000305 if (RR != 0) {
306 ResultReg = RR;
307 return true;
308 } else
309 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000310}
311
Dan Gohman0586d912008-09-10 20:11:02 +0000312/// X86SelectAddress - Attempt to fill in an address from the given value.
313///
Dan Gohman46510a72010-04-15 01:51:59 +0000314bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
315 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000316 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000317 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000318 // Don't walk into other basic blocks; it's possible we haven't
319 // visited them yet, so the instructions may not yet be assigned
320 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000321 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
322 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
323 Opcode = I->getOpcode();
324 U = I;
325 }
Dan Gohman46510a72010-04-15 01:51:59 +0000326 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000327 Opcode = C->getOpcode();
328 U = C;
329 }
Dan Gohman0586d912008-09-10 20:11:02 +0000330
Chris Lattner868ee942010-06-15 19:08:40 +0000331 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
332 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000333 // Fast instruction selection doesn't support the special
334 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000335 return false;
336
Dan Gohman35893082008-09-18 23:23:44 +0000337 switch (Opcode) {
338 default: break;
339 case Instruction::BitCast:
340 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000341 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000342
343 case Instruction::IntToPtr:
344 // Look past no-op inttoptrs.
345 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000346 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000347 break;
Dan Gohman35893082008-09-18 23:23:44 +0000348
349 case Instruction::PtrToInt:
350 // Look past no-op ptrtoints.
351 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000352 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000353 break;
Dan Gohman35893082008-09-18 23:23:44 +0000354
355 case Instruction::Alloca: {
356 // Do static allocas.
357 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000358 DenseMap<const AllocaInst*, int>::iterator SI =
359 FuncInfo.StaticAllocaMap.find(A);
360 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000361 AM.BaseType = X86AddressMode::FrameIndexBase;
362 AM.Base.FrameIndex = SI->second;
363 return true;
364 }
365 break;
Dan Gohman35893082008-09-18 23:23:44 +0000366 }
367
368 case Instruction::Add: {
369 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000370 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000371 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
372 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000373 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000374 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000375 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000376 }
Dan Gohman0586d912008-09-10 20:11:02 +0000377 }
Dan Gohman35893082008-09-18 23:23:44 +0000378 break;
379 }
380
381 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000382 X86AddressMode SavedAM = AM;
383
Dan Gohman35893082008-09-18 23:23:44 +0000384 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000385 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000386 unsigned IndexReg = AM.IndexReg;
387 unsigned Scale = AM.Scale;
388 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000389 // Iterate through the indices, folding what we can. Constants can be
390 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000391 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000392 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000393 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000394 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
395 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000396 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
397 continue;
398 }
399
400 // A array/variable index is always of the form i*S where S is the
401 // constant scale size. See if we can push the scale into immediates.
402 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
403 for (;;) {
404 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
405 // Constant-offset addressing.
406 Disp += CI->getSExtValue() * S;
407 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000408 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000409 if (isa<AddOperator>(Op) &&
410 (!isa<Instruction>(Op) ||
411 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
412 == FuncInfo.MBB) &&
413 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
414 // An add (in the same block) with a constant operand. Fold the
415 // constant.
416 ConstantInt *CI =
417 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
418 Disp += CI->getSExtValue() * S;
419 // Iterate on the other operand.
420 Op = cast<AddOperator>(Op)->getOperand(0);
421 continue;
422 }
423 if (IndexReg == 0 &&
424 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
425 (S == 1 || S == 2 || S == 4 || S == 8)) {
426 // Scaled-index addressing.
427 Scale = S;
428 IndexReg = getRegForGEPIndex(Op).first;
429 if (IndexReg == 0)
430 return false;
431 break;
432 }
433 // Unsupported.
434 goto unsupported_gep;
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 Lattnerdceb52a2011-04-17 17:05:12 +0000448 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000449 // 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)) {
Chris Lattner0a1c9972011-04-17 17:47:38 +0000460 // Can't handle alternate code models or TLS 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 Gohman46510a72010-04-15 01:51:59 +0000464 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000465 if (GVar->isThreadLocal())
466 return false;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000467
468 // RIP-relative addresses can't have additional register operands, so if
469 // we've already folded stuff into the addressing mode, just force the
470 // global value into its own register, which we can use as the basereg.
471 if (!Subtarget->isPICStyleRIPRel() ||
472 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
473 // Okay, we've committed to selecting this global. Set up the address.
474 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000475
Chris Lattner0a1c9972011-04-17 17:47:38 +0000476 // Allow the subtarget to classify the global.
477 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000478
Chris Lattner0a1c9972011-04-17 17:47:38 +0000479 // If this reference is relative to the pic base, set it now.
480 if (isGlobalRelativeToPICBase(GVFlags)) {
481 // FIXME: How do we know Base.Reg is free??
482 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000483 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000484
485 // Unless the ABI requires an extra load, return a direct reference to
486 // the global.
487 if (!isGlobalStubReference(GVFlags)) {
488 if (Subtarget->isPICStyleRIPRel()) {
489 // Use rip-relative addressing if we can. Above we verified that the
490 // base and index registers are unused.
491 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
492 AM.Base.Reg = X86::RIP;
493 }
494 AM.GVOpFlags = GVFlags;
495 return true;
496 }
497
498 // Ok, we need to do a load from a stub. If we've already loaded from
499 // this stub, reuse the loaded pointer, otherwise emit the load now.
500 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
501 unsigned LoadReg;
502 if (I != LocalValueMap.end() && I->second != 0) {
503 LoadReg = I->second;
504 } else {
505 // Issue load from stub.
506 unsigned Opc = 0;
507 const TargetRegisterClass *RC = NULL;
508 X86AddressMode StubAM;
509 StubAM.Base.Reg = AM.Base.Reg;
510 StubAM.GV = GV;
511 StubAM.GVOpFlags = GVFlags;
512
513 // Prepare for inserting code in the local-value area.
514 SavePoint SaveInsertPt = enterLocalValueArea();
515
516 if (TLI.getPointerTy() == MVT::i64) {
517 Opc = X86::MOV64rm;
518 RC = X86::GR64RegisterClass;
519
520 if (Subtarget->isPICStyleRIPRel())
521 StubAM.Base.Reg = X86::RIP;
522 } else {
523 Opc = X86::MOV32rm;
524 RC = X86::GR32RegisterClass;
525 }
526
527 LoadReg = createResultReg(RC);
528 MachineInstrBuilder LoadMI =
529 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
530 addFullAddress(LoadMI, StubAM);
531
532 // Ok, back to normal mode.
533 leaveLocalValueArea(SaveInsertPt);
534
535 // Prevent loading GV stub multiple times in same MBB.
536 LocalValueMap[V] = LoadReg;
537 }
538
539 // Now construct the final address. Note that the Disp, Scale,
540 // and Index values may already be set here.
541 AM.Base.Reg = LoadReg;
542 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000543 return true;
544 }
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);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000865 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
866 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
867 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000868 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000869 UpdateValueMap(I, ResultReg);
870 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000871 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000872 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
873 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
874 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
875 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
876 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
877 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
878 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
879 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
880 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
881 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
882 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
883 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000884
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000885 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
886 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
887 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
888 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
889 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
890 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
891 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
892 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
893 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
894 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000895 default:
896 return false;
897 }
898
Dan Gohman46510a72010-04-15 01:51:59 +0000899 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000900 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000901 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000902
Chris Lattner9a08a612008-10-15 04:26:38 +0000903 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000904 if (!X86FastEmitCompare(Op0, Op1, VT))
905 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000906
Dan Gohman84023e02010-07-10 09:00:22 +0000907 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000908 UpdateValueMap(I, ResultReg);
909 return true;
910}
Evan Cheng8b19e562008-09-03 06:44:39 +0000911
Dan Gohman46510a72010-04-15 01:51:59 +0000912bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000913 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000914 if (I->getType()->isIntegerTy(8) &&
915 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000916 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000917 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000918 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000919 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000920 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000921 UpdateValueMap(I, ResultReg);
922 return true;
923 }
924
925 return false;
926}
927
Chris Lattner9a08a612008-10-15 04:26:38 +0000928
Dan Gohman46510a72010-04-15 01:51:59 +0000929bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000930 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000931 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000932 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000933 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
934 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000935
Dan Gohman8bef7442010-08-21 02:32:36 +0000936 // Fold the common case of a conditional branch with a comparison
937 // in the same block (values defined on other blocks may not have
938 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000939 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000940 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000941 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000942
Dan Gohmand98d6202008-10-02 22:15:21 +0000943 // Try to take advantage of fallthrough opportunities.
944 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000945 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000946 std::swap(TrueMBB, FalseMBB);
947 Predicate = CmpInst::getInversePredicate(Predicate);
948 }
949
Chris Lattner871d2462008-10-15 03:58:05 +0000950 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
951 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
952
Dan Gohmand98d6202008-10-02 22:15:21 +0000953 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000954 case CmpInst::FCMP_OEQ:
955 std::swap(TrueMBB, FalseMBB);
956 Predicate = CmpInst::FCMP_UNE;
957 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000958 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
959 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
960 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
961 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
962 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
963 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
964 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
965 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
966 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
967 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
968 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
969 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
970 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000971
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000972 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
973 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
974 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
975 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
976 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
977 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
978 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
979 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
980 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
981 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000982 default:
983 return false;
984 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000985
Dan Gohman46510a72010-04-15 01:51:59 +0000986 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000987 if (SwapArgs)
988 std::swap(Op0, Op1);
989
Chris Lattner9a08a612008-10-15 04:26:38 +0000990 // Emit a compare of the LHS and RHS, setting the flags.
991 if (!X86FastEmitCompare(Op0, Op1, VT))
992 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000993
Dan Gohman84023e02010-07-10 09:00:22 +0000994 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
995 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000996
997 if (Predicate == CmpInst::FCMP_UNE) {
998 // X86 requires a second branch to handle UNE (and OEQ,
999 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001000 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1001 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001002 }
1003
Stuart Hastings3bf91252010-06-17 22:43:56 +00001004 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001005 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001006 return true;
1007 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001008 } else if (ExtractValueInst *EI =
1009 dyn_cast<ExtractValueInst>(BI->getCondition())) {
1010 // Check to see if the branch instruction is from an "arithmetic with
1011 // overflow" intrinsic. The main way these intrinsics are used is:
1012 //
1013 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1014 // %sum = extractvalue { i32, i1 } %t, 0
1015 // %obit = extractvalue { i32, i1 } %t, 1
1016 // br i1 %obit, label %overflow, label %normal
1017 //
Dan Gohman653456c2009-01-07 00:15:08 +00001018 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +00001019 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +00001020 // looking for the SETO/SETB instruction. If an instruction modifies the
1021 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1022 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001023 if (const IntrinsicInst *CI =
1024 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001025 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1026 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1027 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001028 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001029
Chris Lattnera9a42252009-04-12 07:36:01 +00001030 for (MachineBasicBlock::const_reverse_iterator
Dan Gohman84023e02010-07-10 09:00:22 +00001031 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1032 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001033 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001034
Evan Cheng1015ba72010-05-21 20:53:24 +00001035 if (MI.definesRegister(Reg)) {
Jakob Stoklund Olesen84d499a2010-07-16 22:35:34 +00001036 if (MI.isCopy()) {
1037 Reg = MI.getOperand(1).getReg();
Chris Lattnera9a42252009-04-12 07:36:01 +00001038 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001039 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001040
Chris Lattnera9a42252009-04-12 07:36:01 +00001041 SetMI = &MI;
1042 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001043 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001044
Chris Lattnera9a42252009-04-12 07:36:01 +00001045 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengc36b7062011-01-07 23:50:32 +00001046 if (TID.hasImplicitDefOfPhysReg(X86::EFLAGS) ||
1047 MI.hasUnmodeledSideEffects())
Chris Lattnera9a42252009-04-12 07:36:01 +00001048 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001049 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001050
1051 if (SetMI) {
1052 unsigned OpCode = SetMI->getOpcode();
1053
1054 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohman84023e02010-07-10 09:00:22 +00001055 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1056 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001057 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001058 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001059 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001060 return true;
1061 }
Bill Wendling9a901322008-12-10 19:44:24 +00001062 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001063 }
1064 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001065 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1066 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1067 // typically happen for _Bool and C++ bools.
1068 MVT SourceVT;
1069 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1070 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1071 unsigned TestOpc = 0;
1072 switch (SourceVT.SimpleTy) {
1073 default: break;
1074 case MVT::i8: TestOpc = X86::TEST8ri; break;
1075 case MVT::i16: TestOpc = X86::TEST16ri; break;
1076 case MVT::i32: TestOpc = X86::TEST32ri; break;
1077 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1078 }
1079 if (TestOpc) {
1080 unsigned OpReg = getRegForValue(TI->getOperand(0));
1081 if (OpReg == 0) return false;
1082 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1083 .addReg(OpReg).addImm(1);
Chris Lattnerc76d1212011-04-19 04:26:32 +00001084
1085 unsigned JmpOpc = X86::JNE_4;
1086 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1087 std::swap(TrueMBB, FalseMBB);
1088 JmpOpc = X86::JE_4;
1089 }
1090
1091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001092 .addMBB(TrueMBB);
1093 FastEmitBranch(FalseMBB, DL);
1094 FuncInfo.MBB->addSuccessor(TrueMBB);
1095 return true;
1096 }
1097 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001098 }
1099
1100 // Otherwise do a clumsy setcc and re-test it.
1101 unsigned OpReg = getRegForValue(BI->getCondition());
1102 if (OpReg == 0) return false;
1103
Dan Gohman84023e02010-07-10 09:00:22 +00001104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1105 .addReg(OpReg).addReg(OpReg);
1106 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1107 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001108 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001109 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001110 return true;
1111}
1112
Dan Gohman46510a72010-04-15 01:51:59 +00001113bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001114 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001115 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001116 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001117 CReg = X86::CL;
1118 RC = &X86::GR8RegClass;
1119 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001120 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1121 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1122 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001123 default: return false;
1124 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001125 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001126 CReg = X86::CX;
1127 RC = &X86::GR16RegClass;
1128 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001129 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1130 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1131 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001132 default: return false;
1133 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001134 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001135 CReg = X86::ECX;
1136 RC = &X86::GR32RegClass;
1137 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001138 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1139 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1140 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001141 default: return false;
1142 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001143 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001144 CReg = X86::RCX;
1145 RC = &X86::GR64RegClass;
1146 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001147 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1148 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1149 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001150 default: return false;
1151 }
1152 } else {
1153 return false;
1154 }
1155
Duncan Sands1440e8b2010-11-03 11:35:31 +00001156 MVT VT;
1157 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001158 return false;
1159
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001160 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1161 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001162
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001163 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1164 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001165 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1166 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001167
1168 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001169 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001170 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001171 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1172 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001173 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001174
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001175 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001176 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1177 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001178 UpdateValueMap(I, ResultReg);
1179 return true;
1180}
1181
Dan Gohman46510a72010-04-15 01:51:59 +00001182bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001183 MVT VT;
1184 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001185 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001186
Eric Christophere487b012010-09-29 23:00:29 +00001187 // We only use cmov here, if we don't have a cmov instruction bail.
1188 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001189
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001190 unsigned Opc = 0;
1191 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001192 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001193 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001194 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001195 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001196 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001197 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001198 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001199 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001200 RC = &X86::GR64RegClass;
1201 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001202 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001203 }
1204
1205 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1206 if (Op0Reg == 0) return false;
1207 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1208 if (Op1Reg == 0) return false;
1209 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1210 if (Op2Reg == 0) return false;
1211
Dan Gohman84023e02010-07-10 09:00:22 +00001212 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1213 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001214 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001215 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1216 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001217 UpdateValueMap(I, ResultReg);
1218 return true;
1219}
1220
Dan Gohman46510a72010-04-15 01:51:59 +00001221bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001222 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001223 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001224 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001225 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001226 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001227 unsigned OpReg = getRegForValue(V);
1228 if (OpReg == 0) return false;
1229 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001230 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1231 TII.get(X86::CVTSS2SDrr), ResultReg)
1232 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001233 UpdateValueMap(I, ResultReg);
1234 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001235 }
1236 }
1237
1238 return false;
1239}
1240
Dan Gohman46510a72010-04-15 01:51:59 +00001241bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001242 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001243 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001244 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001245 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001246 unsigned OpReg = getRegForValue(V);
1247 if (OpReg == 0) return false;
1248 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001249 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1250 TII.get(X86::CVTSD2SSrr), ResultReg)
1251 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001252 UpdateValueMap(I, ResultReg);
1253 return true;
1254 }
1255 }
1256 }
1257
1258 return false;
1259}
1260
Dan Gohman46510a72010-04-15 01:51:59 +00001261bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001262 if (Subtarget->is64Bit())
1263 // All other cases should be handled by the tblgen generated code.
1264 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001265 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1266 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001267
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001268 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001270 // All other cases should be handled by the tblgen generated code.
1271 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001273 // All other cases should be handled by the tblgen generated code.
1274 return false;
1275
1276 unsigned InputReg = getRegForValue(I->getOperand(0));
1277 if (!InputReg)
1278 // Unhandled operand. Halt "fast" selection and bail.
1279 return false;
1280
Dan Gohman62417622009-04-27 16:33:14 +00001281 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001282 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001283 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001284 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001285 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1286 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001287
1288 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001289 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001290 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001291 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001292 if (!ResultReg)
1293 return false;
1294
1295 UpdateValueMap(I, ResultReg);
1296 return true;
1297}
1298
Dan Gohman46510a72010-04-15 01:51:59 +00001299bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1300 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1301 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001302
Dan Gohman46510a72010-04-15 01:51:59 +00001303 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001304 switch (CI->getIntrinsicID()) {
1305 default: break;
1306 case Intrinsic::sadd_with_overflow:
Dan Gohman84023e02010-07-10 09:00:22 +00001307 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001308 // Cheat a little. We know that the registers for "add" and "seto" are
1309 // allocated sequentially. However, we only keep track of the register
1310 // for "add" in the value map. Use extractvalue's index to get the
1311 // correct register for "seto".
Dan Gohman84023e02010-07-10 09:00:22 +00001312 unsigned OpReg = getRegForValue(Agg);
1313 if (OpReg == 0)
1314 return false;
1315 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001316 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001317 }
Dan Gohman84023e02010-07-10 09:00:22 +00001318 }
Bill Wendling52370a12008-12-09 02:42:50 +00001319 }
1320
1321 return false;
1322}
1323
Dan Gohman46510a72010-04-15 01:51:59 +00001324bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001325 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001326 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001327 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001328 case Intrinsic::stackprotector: {
1329 // Emit code inline code to store the stack guard onto the stack.
1330 EVT PtrTy = TLI.getPointerTy();
1331
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001332 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1333 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001334
1335 // Grab the frame index.
1336 X86AddressMode AM;
1337 if (!X86SelectAddress(Slot, AM)) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001338
Eric Christopher88dee302010-03-18 21:58:33 +00001339 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001340
Eric Christopher07754c22010-03-18 20:27:26 +00001341 return true;
1342 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001343 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001344 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001345 const Type *Ty = I.getCalledFunction()->getReturnType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001346
Eric Christopherf27805b2010-03-11 06:20:22 +00001347 assert(CI && "Non-constant type in Intrinsic::objectsize?");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001348
Duncan Sands1440e8b2010-11-03 11:35:31 +00001349 MVT VT;
Eric Christopherf27805b2010-03-11 06:20:22 +00001350 if (!isTypeLegal(Ty, VT))
1351 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001352
Eric Christopherf27805b2010-03-11 06:20:22 +00001353 unsigned OpC = 0;
1354 if (VT == MVT::i32)
1355 OpC = X86::MOV32ri;
1356 else if (VT == MVT::i64)
1357 OpC = X86::MOV64ri;
1358 else
1359 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001360
Eric Christopherf27805b2010-03-11 06:20:22 +00001361 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001362 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001363 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001364 UpdateValueMap(&I, ResultReg);
1365 return true;
1366 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001367 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001368 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001369 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001370 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001371 if (!X86SelectAddress(DI->getAddress(), AM))
1372 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001373 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001374 // FIXME may need to add RegState::Debug to any registers produced,
1375 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001376 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1377 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001378 return true;
1379 }
Eric Christopher77f79892010-01-18 22:11:29 +00001380 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001382 return true;
1383 }
Bill Wendling52370a12008-12-09 02:42:50 +00001384 case Intrinsic::sadd_with_overflow:
1385 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001386 // Replace "add with overflow" intrinsics with an "add" instruction followed
1387 // by a seto/setc instruction. Later on, when the "extractvalue"
1388 // instructions are encountered, we use the fact that two registers were
1389 // created sequentially to get the correct registers for the "sum" and the
1390 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001391 const Function *Callee = I.getCalledFunction();
1392 const Type *RetTy =
1393 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1394
Duncan Sands1440e8b2010-11-03 11:35:31 +00001395 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001396 if (!isTypeLegal(RetTy, VT))
1397 return false;
1398
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001399 const Value *Op1 = I.getArgOperand(0);
1400 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001401 unsigned Reg1 = getRegForValue(Op1);
1402 unsigned Reg2 = getRegForValue(Op2);
1403
1404 if (Reg1 == 0 || Reg2 == 0)
1405 // FIXME: Handle values *not* in registers.
1406 return false;
1407
1408 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001410 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001412 OpC = X86::ADD64rr;
1413 else
1414 return false;
1415
1416 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001417 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1418 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001419 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001420
Chris Lattner8d57b772009-04-12 07:51:14 +00001421 // If the add with overflow is an intra-block value then we just want to
1422 // create temporaries for it like normal. If it is a cross-block value then
1423 // UpdateValueMap will return the cross-block register used. Since we
1424 // *really* want the value to be live in the register pair known by
1425 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1426 // the cross block case. In the non-cross-block case, we should just make
1427 // another register for the value.
1428 if (DestReg1 != ResultReg)
1429 ResultReg = DestReg1+1;
1430 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001431 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001432
Chris Lattnera9a42252009-04-12 07:36:01 +00001433 unsigned Opc = X86::SETBr;
1434 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1435 Opc = X86::SETOr;
Dan Gohman84023e02010-07-10 09:00:22 +00001436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001437 return true;
1438 }
1439 }
1440}
1441
Dan Gohman46510a72010-04-15 01:51:59 +00001442bool X86FastISel::X86SelectCall(const Instruction *I) {
1443 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001444 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001445
1446 // Can't handle inline asm yet.
1447 if (isa<InlineAsm>(Callee))
1448 return false;
1449
Bill Wendling52370a12008-12-09 02:42:50 +00001450 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001451 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001452 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001453
Evan Chengf3d4efe2008-09-07 09:09:33 +00001454 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001455 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001456 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001457 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001458 CC != CallingConv::X86_FastCall)
1459 return false;
1460
Evan Cheng381993f2010-01-27 00:00:57 +00001461 // fastcc with -tailcallopt is intended to provide a guaranteed
1462 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001463 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001464 return false;
1465
Evan Chengf3d4efe2008-09-07 09:09:33 +00001466 // Let SDISel handle vararg functions.
1467 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1468 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1469 if (FTy->isVarArg())
1470 return false;
1471
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001472 // Fast-isel doesn't know about callee-pop yet.
1473 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1474 return false;
1475
Evan Chengf3d4efe2008-09-07 09:09:33 +00001476 // Handle *simple* calls for now.
1477 const Type *RetTy = CS.getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001478 MVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001479 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001481 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001482 return false;
1483
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001484 // Materialize callee address in a register. FIXME: GV address can be
1485 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001486 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001487 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001488 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001489 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001490 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001491 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001492 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001493 } else if (CalleeAM.Base.Reg != 0) {
1494 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001495 } else
1496 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001497
Evan Chengdebdea02008-09-08 17:15:42 +00001498 // Allow calls which produce i1 results.
1499 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001500 if (RetVT == MVT::i1) {
1501 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001502 AndToI1 = true;
1503 }
1504
Evan Chengf3d4efe2008-09-07 09:09:33 +00001505 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001506 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001507 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001508 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001509 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001510 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001511 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001512 ArgVTs.reserve(CS.arg_size());
1513 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001514 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001515 i != e; ++i) {
Chris Lattnere03b8d32011-04-19 04:42:38 +00001516 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001517 ISD::ArgFlagsTy Flags;
1518 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001519 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001520 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001521 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001522 Flags.setZExt();
1523
Chris Lattnere03b8d32011-04-19 04:42:38 +00001524 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1525 // instruction. This is safe because it is common to all fastisel supported
1526 // calling conventions on x86.
1527 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1528 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1529 CI->getBitWidth() == 16) {
1530 if (Flags.isSExt())
1531 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1532 else
1533 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1534 }
1535 }
1536
Chris Lattnerb44101c2011-04-19 05:09:50 +00001537 unsigned ArgReg;
1538 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1539 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1540 ArgVal->hasOneUse()) {
1541 // Passing bools around ends up doing a trunc to i1 and passing it.
1542 // Codegen this as an argument + "and 1".
1543 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1544 ArgReg = getRegForValue(ArgVal);
1545 if (ArgReg == 0) return false;
1546
1547 MVT ArgVT;
1548 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
1549
1550 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1551 ArgVal->hasOneUse(), 1);
1552 } else {
1553 ArgReg = getRegForValue(ArgVal);
1554 if (ArgReg == 0) return false;
1555 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001556
Evan Chengf3d4efe2008-09-07 09:09:33 +00001557 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001558 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1559 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1560 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1561 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001562 return false;
1563
Chris Lattnere03b8d32011-04-19 04:42:38 +00001564 const Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001565 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001566 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001567 return false;
1568 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1569 Flags.setOrigAlign(OriginalAlignment);
1570
Chris Lattnerb44101c2011-04-19 05:09:50 +00001571 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001572 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001573 ArgVTs.push_back(ArgVT);
1574 ArgFlags.push_back(Flags);
1575 }
1576
1577 // Analyze operands of the call, assigning locations to each operand.
1578 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001579 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001580
Dan Gohmand8acddd2010-06-01 21:09:47 +00001581 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001582 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001583 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001584
Duncan Sands45907662010-10-31 13:21:44 +00001585 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001586
1587 // Get a count of how many bytes are to be pushed on the stack.
1588 unsigned NumBytes = CCInfo.getNextStackOffset();
1589
1590 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001591 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001592 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1593 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001594
Chris Lattner438949a2008-10-15 05:30:52 +00001595 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001596 // copies / loads.
1597 SmallVector<unsigned, 4> RegArgs;
1598 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1599 CCValAssign &VA = ArgLocs[i];
1600 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001601 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001602
Evan Chengf3d4efe2008-09-07 09:09:33 +00001603 // Promote the value if needed.
1604 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001605 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001606 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001607 case CCValAssign::SExt: {
1608 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1609 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001610 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001611 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001612 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001613 }
1614 case CCValAssign::ZExt: {
1615 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1616 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001617 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001618 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001619 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001620 }
1621 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001622 // We don't handle MMX parameters yet.
1623 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1624 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001625 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1626 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001627 if (!Emitted)
1628 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001629 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001630 if (!Emitted)
1631 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1632 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001633
Chris Lattnerc46ec642011-01-05 22:26:52 +00001634 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001635 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001636 break;
1637 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001638 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001639 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001640 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001641 assert(BC != 0 && "Failed to emit a bitcast!");
1642 Arg = BC;
1643 ArgVT = VA.getLocVT();
1644 break;
1645 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001646 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001647
Evan Chengf3d4efe2008-09-07 09:09:33 +00001648 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001649 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1650 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001651 RegArgs.push_back(VA.getLocReg());
1652 } else {
1653 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001654 X86AddressMode AM;
1655 AM.Base.Reg = StackPtr;
1656 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001657 const Value *ArgVal = ArgVals[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001658
Chris Lattner241ab472008-10-15 05:38:32 +00001659 // If this is a really simple value, emit this with the Value* version of
1660 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1661 // can cause us to reevaluate the argument.
1662 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1663 X86FastEmitStore(ArgVT, ArgVal, AM);
1664 else
1665 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001666 }
1667 }
1668
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001669 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001670 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001671 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001672 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001673 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1674 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001675 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001676
Evan Chengf3d4efe2008-09-07 09:09:33 +00001677 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001678 MachineInstrBuilder MIB;
1679 if (CalleeOp) {
1680 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001681 unsigned CallOpc;
1682 if (Subtarget->isTargetWin64())
1683 CallOpc = X86::WINCALL64r;
1684 else if (Subtarget->is64Bit())
1685 CallOpc = X86::CALL64r;
1686 else
1687 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001688 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1689 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001690
Chris Lattner51e8eab2009-07-09 06:34:26 +00001691 } else {
1692 // Direct call.
1693 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001694 unsigned CallOpc;
1695 if (Subtarget->isTargetWin64())
1696 CallOpc = X86::WINCALL64pcrel32;
1697 else if (Subtarget->is64Bit())
1698 CallOpc = X86::CALL64pcrel32;
1699 else
1700 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001701
Chris Lattner51e8eab2009-07-09 06:34:26 +00001702 // See if we need any target-specific flags on the GV operand.
1703 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001704
Chris Lattner51e8eab2009-07-09 06:34:26 +00001705 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1706 // external symbols most go through the PLT in PIC mode. If the symbol
1707 // has hidden or protected visibility, or if it is static or local, then
1708 // we don't need to use the PLT - we can directly call it.
1709 if (Subtarget->isTargetELF() &&
1710 TM.getRelocationModel() == Reloc::PIC_ &&
1711 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1712 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001713 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001714 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1715 Subtarget->getDarwinVers() < 9) {
1716 // PC-relative references to external symbols should go through $stub,
1717 // unless we're building with the leopard linker or later, which
1718 // automatically synthesizes these stubs.
1719 OpFlags = X86II::MO_DARWIN_STUB;
1720 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001721
1722
Dan Gohman84023e02010-07-10 09:00:22 +00001723 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1724 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001725 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001726
1727 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001728 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001729 MIB.addReg(X86::EBX);
1730
Evan Chengf3d4efe2008-09-07 09:09:33 +00001731 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001732 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1733 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001734
1735 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001736 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001737 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1738 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001739
1740 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001741 SmallVector<unsigned, 4> UsedRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001742 if (RetVT != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001743 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001744 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001745 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1746
1747 // Copy all of the result registers out of their specified physreg.
1748 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001749 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001750 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001751
Evan Chengf3d4efe2008-09-07 09:09:33 +00001752 // If this is a call to a function that returns an fp value on the x87 fp
1753 // stack, but where we prefer to use the value in xmm registers, copy it
1754 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1755 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1756 RVLocs[0].getLocReg() == X86::ST1) &&
1757 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001759 DstRC = X86::RFP80RegisterClass;
1760 }
1761
1762 unsigned ResultReg = createResultReg(DstRC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001763 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1764 ResultReg).addReg(RVLocs[0].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001765 UsedRegs.push_back(RVLocs[0].getLocReg());
1766
Evan Chengf3d4efe2008-09-07 09:09:33 +00001767 if (CopyVT != RVLocs[0].getValVT()) {
1768 // Round the F80 the right size, which also moves to the appropriate xmm
1769 // register. This is accomplished by storing the F80 value in memory and
1770 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001771 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001773 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001774 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001775 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1776 TII.get(Opc)), FI)
1777 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001778 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001779 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001781 ResultReg = createResultReg(DstRC);
Dan Gohman84023e02010-07-10 09:00:22 +00001782 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1783 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001784 }
1785
Evan Chengdebdea02008-09-08 17:15:42 +00001786 if (AndToI1) {
1787 // Mask out all but lowest bit for some call which produces an i1.
1788 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001789 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001790 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001791 ResultReg = AndResult;
1792 }
1793
Evan Chengf3d4efe2008-09-07 09:09:33 +00001794 UpdateValueMap(I, ResultReg);
1795 }
1796
Dan Gohmandb497122010-06-18 23:28:01 +00001797 // Set all unused physreg defs as dead.
1798 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1799
Evan Chengf3d4efe2008-09-07 09:09:33 +00001800 return true;
1801}
1802
1803
Dan Gohman99b21822008-08-28 23:21:34 +00001804bool
Dan Gohman46510a72010-04-15 01:51:59 +00001805X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001806 switch (I->getOpcode()) {
1807 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001808 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001809 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001810 case Instruction::Store:
1811 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001812 case Instruction::Ret:
1813 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001814 case Instruction::ICmp:
1815 case Instruction::FCmp:
1816 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001817 case Instruction::ZExt:
1818 return X86SelectZExt(I);
1819 case Instruction::Br:
1820 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001821 case Instruction::Call:
1822 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001823 case Instruction::LShr:
1824 case Instruction::AShr:
1825 case Instruction::Shl:
1826 return X86SelectShift(I);
1827 case Instruction::Select:
1828 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001829 case Instruction::Trunc:
1830 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001831 case Instruction::FPExt:
1832 return X86SelectFPExt(I);
1833 case Instruction::FPTrunc:
1834 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001835 case Instruction::ExtractValue:
1836 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001837 case Instruction::IntToPtr: // Deliberate fall-through.
1838 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001839 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1840 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001841 if (DstVT.bitsGT(SrcVT))
1842 return X86SelectZExt(I);
1843 if (DstVT.bitsLT(SrcVT))
1844 return X86SelectTrunc(I);
1845 unsigned Reg = getRegForValue(I->getOperand(0));
1846 if (Reg == 0) return false;
1847 UpdateValueMap(I, Reg);
1848 return true;
1849 }
Dan Gohman99b21822008-08-28 23:21:34 +00001850 }
1851
1852 return false;
1853}
1854
Dan Gohman46510a72010-04-15 01:51:59 +00001855unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001856 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001857 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001858 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001859
Owen Anderson95267a12008-09-05 00:06:23 +00001860 // Get opcode and regclass of the output for the given load instruction.
1861 unsigned Opc = 0;
1862 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001863 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001864 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001865 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001866 Opc = X86::MOV8rm;
1867 RC = X86::GR8RegisterClass;
1868 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001869 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001870 Opc = X86::MOV16rm;
1871 RC = X86::GR16RegisterClass;
1872 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001873 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001874 Opc = X86::MOV32rm;
1875 RC = X86::GR32RegisterClass;
1876 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001878 // Must be in x86-64 mode.
1879 Opc = X86::MOV64rm;
1880 RC = X86::GR64RegisterClass;
1881 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001882 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001883 if (Subtarget->hasSSE1()) {
1884 Opc = X86::MOVSSrm;
1885 RC = X86::FR32RegisterClass;
1886 } else {
1887 Opc = X86::LD_Fp32m;
1888 RC = X86::RFP32RegisterClass;
1889 }
1890 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001891 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001892 if (Subtarget->hasSSE2()) {
1893 Opc = X86::MOVSDrm;
1894 RC = X86::FR64RegisterClass;
1895 } else {
1896 Opc = X86::LD_Fp64m;
1897 RC = X86::RFP64RegisterClass;
1898 }
1899 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001901 // No f80 support yet.
1902 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001903 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001904
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001905 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001906 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001907 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001908 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00001909 // If the expression is just a basereg, then we're done, otherwise we need
1910 // to emit an LEA.
1911 if (AM.BaseType == X86AddressMode::RegBase &&
1912 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
1913 return AM.Base.Reg;
1914
1915 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001916 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001917 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1918 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001919 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001920 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001921 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001922 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001923
Owen Anderson3b217c62008-09-06 01:11:01 +00001924 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001925 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001926 if (Align == 0) {
1927 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001928 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001929 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001930
Dan Gohman5396c992008-09-30 01:21:32 +00001931 // x86-32 PIC requires a PIC base register for constant pools.
1932 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001933 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001934 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001935 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001936 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001937 } else if (Subtarget->isPICStyleGOT()) {
1938 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001939 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001940 } else if (Subtarget->isPICStyleRIPRel() &&
1941 TM.getCodeModel() == CodeModel::Small) {
1942 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001943 }
Dan Gohman5396c992008-09-30 01:21:32 +00001944
1945 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001946 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001947 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001948 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1949 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001950 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001951
Owen Anderson95267a12008-09-05 00:06:23 +00001952 return ResultReg;
1953}
1954
Dan Gohman46510a72010-04-15 01:51:59 +00001955unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001956 // Fail on dynamic allocas. At this point, getRegForValue has already
1957 // checked its CSE maps, so if we're here trying to handle a dynamic
1958 // alloca, we're not going to succeed. X86SelectAddress has a
1959 // check for dynamic allocas, because it's called directly from
1960 // various places, but TargetMaterializeAlloca also needs a check
1961 // in order to avoid recursion between getRegForValue,
1962 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001963 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001964 return 0;
1965
Dan Gohman0586d912008-09-10 20:11:02 +00001966 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001967 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001968 return 0;
1969 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1970 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1971 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001972 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1973 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001974 return ResultReg;
1975}
1976
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001977/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
1978/// vreg is being provided by the specified load instruction. If possible,
1979/// try to fold the load as an operand to the instruction, returning true if
1980/// possible.
1981bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
1982 const LoadInst *LI) {
1983 X86AddressMode AM;
1984 if (!X86SelectAddress(LI->getOperand(0), AM))
1985 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001986
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001987 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001988
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001989 unsigned Size = TD.getTypeAllocSize(LI->getType());
1990 unsigned Alignment = LI->getAlignment();
1991
1992 SmallVector<MachineOperand, 8> AddrOps;
1993 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001994
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001995 MachineInstr *Result =
1996 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
1997 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001998
Chris Lattnerb99fdee2011-01-16 02:27:38 +00001999 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002000 MI->eraseFromParent();
2001 return true;
2002}
2003
2004
Evan Chengc3f44b02008-09-03 00:03:49 +00002005namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00002006 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2007 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002008 }
Dan Gohman99b21822008-08-28 23:21:34 +00002009}