blob: 69a9d60bdad8dceb098e51c2a53e42402fc85058 [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"
Dan Gohman1adf1b02008-08-19 21:45:35 +000018#include "X86ISelLowering.h"
Evan Cheng88e30412008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000022#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000023#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000024#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000025#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000026#include "llvm/IntrinsicInst.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000027#include "llvm/CodeGen/FastISel.h"
Owen Anderson95267a12008-09-05 00:06:23 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000031#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000033#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000034#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000035using namespace llvm;
36
Chris Lattner087fcf32009-03-08 18:44:31 +000037namespace {
38
Evan Chengc3f44b02008-09-03 00:03:49 +000039class X86FastISel : public FastISel {
40 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
41 /// make the right decision when generating code for different targets.
42 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000043
44 /// StackPtr - Register used as the stack pointer.
45 ///
46 unsigned StackPtr;
47
48 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
49 /// floating point ops.
50 /// When SSE is available, use it for f32 operations.
51 /// When SSE2 is available, use it for f64 operations.
52 bool X86ScalarSSEf64;
53 bool X86ScalarSSEf32;
54
Evan Cheng8b19e562008-09-03 06:44:39 +000055public:
Dan Gohman3df24e62008-09-03 23:12:08 +000056 explicit X86FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +000057 MachineModuleInfo *mmi,
Devang Patel83489bb2009-01-13 00:35:13 +000058 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +000059 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000060 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +000061 DenseMap<const AllocaInst *, int> &am
62#ifndef NDEBUG
63 , SmallSet<Instruction*, 8> &cil
64#endif
65 )
Devang Patel83489bb2009-01-13 00:35:13 +000066 : FastISel(mf, mmi, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +000067#ifndef NDEBUG
68 , cil
69#endif
70 ) {
Evan Cheng88e30412008-09-03 01:04:47 +000071 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000072 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
73 X86ScalarSSEf64 = Subtarget->hasSSE2();
74 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000075 }
Evan Chengc3f44b02008-09-03 00:03:49 +000076
Dan Gohman3df24e62008-09-03 23:12:08 +000077 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000078
Dan Gohman1adf1b02008-08-19 21:45:35 +000079#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000080
81private:
Owen Andersone50ed302009-08-10 22:56:29 +000082 bool X86FastEmitCompare(Value *LHS, Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000083
Owen Andersone50ed302009-08-10 22:56:29 +000084 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000085
Owen Andersone50ed302009-08-10 22:56:29 +000086 bool X86FastEmitStore(EVT VT, Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000087 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000088 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000089 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000090
Owen Andersone50ed302009-08-10 22:56:29 +000091 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000092 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000093
Chris Lattner0aa43de2009-07-10 05:33:42 +000094 bool X86SelectAddress(Value *V, X86AddressMode &AM);
95 bool X86SelectCallAddress(Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000096
Dan Gohman3df24e62008-09-03 23:12:08 +000097 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000098
99 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000100
101 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000102
103 bool X86SelectZExt(Instruction *I);
104
105 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000106
107 bool X86SelectShift(Instruction *I);
108
109 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000110
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000111 bool X86SelectTrunc(Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000112
Dan Gohman78efce62008-09-10 21:02:08 +0000113 bool X86SelectFPExt(Instruction *I);
114 bool X86SelectFPTrunc(Instruction *I);
115
Bill Wendling52370a12008-12-09 02:42:50 +0000116 bool X86SelectExtractValue(Instruction *I);
117
Chris Lattnera9a42252009-04-12 07:36:01 +0000118 bool X86VisitIntrinsicCall(IntrinsicInst &I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000119 bool X86SelectCall(Instruction *I);
120
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000121 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000122
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000123 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000124 return getTargetMachine()->getInstrInfo();
125 }
126 const X86TargetMachine *getTargetMachine() const {
127 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000128 }
129
Dan Gohman0586d912008-09-10 20:11:02 +0000130 unsigned TargetMaterializeConstant(Constant *C);
131
132 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000133
134 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
135 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000136 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
138 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000139 }
140
Owen Andersone50ed302009-08-10 22:56:29 +0000141 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000142};
Chris Lattner087fcf32009-03-08 18:44:31 +0000143
144} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000145
Owen Andersone50ed302009-08-10 22:56:29 +0000146bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000147 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000149 // Unhandled type. Halt "fast" selection and bail.
150 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000151
Dan Gohman9b66d732008-09-30 00:48:39 +0000152 // For now, require SSE/SSE2 for performing floating-point operations,
153 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000155 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000157 return false;
158 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000160 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000161 // We only handle legal types. For example, on x86-32 the instruction
162 // selector contains all of the 64-bit instructions from x86-64,
163 // under the assumption that i64 won't be used if the target doesn't
164 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000166}
167
168#include "X86GenCallingConv.inc"
169
170/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
171/// convention.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000172CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
173 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000174 if (Subtarget->is64Bit()) {
175 if (Subtarget->isTargetWin64())
176 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177 else
178 return CC_X86_64_C;
179 }
180
181 if (CC == CallingConv::X86_FastCall)
182 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000183 else if (CC == CallingConv::Fast)
184 return CC_X86_32_FastCC;
185 else
186 return CC_X86_32_C;
187}
188
Evan Cheng0de588f2008-09-05 21:00:03 +0000189/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000190/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000191/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000192bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 unsigned &ResultReg) {
194 // Get opcode and regclass of the output for the given load instruction.
195 unsigned Opc = 0;
196 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000198 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000199 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000201 Opc = X86::MOV8rm;
202 RC = X86::GR8RegisterClass;
203 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000205 Opc = X86::MOV16rm;
206 RC = X86::GR16RegisterClass;
207 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000209 Opc = X86::MOV32rm;
210 RC = X86::GR32RegisterClass;
211 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000213 // Must be in x86-64 mode.
214 Opc = X86::MOV64rm;
215 RC = X86::GR64RegisterClass;
216 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000218 if (Subtarget->hasSSE1()) {
219 Opc = X86::MOVSSrm;
220 RC = X86::FR32RegisterClass;
221 } else {
222 Opc = X86::LD_Fp32m;
223 RC = X86::RFP32RegisterClass;
224 }
225 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000227 if (Subtarget->hasSSE2()) {
228 Opc = X86::MOVSDrm;
229 RC = X86::FR64RegisterClass;
230 } else {
231 Opc = X86::LD_Fp64m;
232 RC = X86::RFP64RegisterClass;
233 }
234 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000236 // No f80 support yet.
237 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000238 }
239
240 ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000241 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000242 return true;
243}
244
Evan Chengf3d4efe2008-09-07 09:09:33 +0000245/// X86FastEmitStore - Emit a machine instruction to store a value Val of
246/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
247/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000248/// i.e. V. Return true if it is possible.
249bool
Owen Andersone50ed302009-08-10 22:56:29 +0000250X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000251 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000252 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000253 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 switch (VT.getSimpleVT().SimpleTy) {
255 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000256 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000257 case MVT::i1: {
258 // Mask out all but lowest bit.
259 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
260 BuildMI(MBB, DL,
261 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
262 Val = AndResult;
263 }
264 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 case MVT::i8: Opc = X86::MOV8mr; break;
266 case MVT::i16: Opc = X86::MOV16mr; break;
267 case MVT::i32: Opc = X86::MOV32mr; break;
268 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
269 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000270 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000271 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000273 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000274 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000275 }
Chris Lattner438949a2008-10-15 05:30:52 +0000276
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000277 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000278 return true;
279}
280
Owen Andersone50ed302009-08-10 22:56:29 +0000281bool X86FastISel::X86FastEmitStore(EVT VT, Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000282 const X86AddressMode &AM) {
283 // Handle 'null' like i32/i64 0.
284 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000285 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000286
287 // If this is a store of a simple constant, fold the constant into the store.
288 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
289 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000290 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000292 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000293 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 case MVT::i8: Opc = X86::MOV8mi; break;
295 case MVT::i16: Opc = X86::MOV16mi; break;
296 case MVT::i32: Opc = X86::MOV32mi; break;
297 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000298 // Must be a 32-bit sign extended value.
299 if ((int)CI->getSExtValue() == CI->getSExtValue())
300 Opc = X86::MOV64mi32;
301 break;
302 }
303
304 if (Opc) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000305 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000306 .addImm(Signed ? CI->getSExtValue() :
307 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000308 return true;
309 }
310 }
311
312 unsigned ValReg = getRegForValue(Val);
313 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000314 return false;
315
316 return X86FastEmitStore(VT, ValReg, AM);
317}
318
Evan Cheng24e3a902008-09-08 06:35:17 +0000319/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
320/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
321/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000322bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
323 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000324 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000325 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
326
327 if (RR != 0) {
328 ResultReg = RR;
329 return true;
330 } else
331 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000332}
333
Dan Gohman0586d912008-09-10 20:11:02 +0000334/// X86SelectAddress - Attempt to fill in an address from the given value.
335///
Chris Lattner0aa43de2009-07-10 05:33:42 +0000336bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM) {
Duncan Sands12513882009-06-03 12:05:18 +0000337 User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000338 unsigned Opcode = Instruction::UserOp1;
339 if (Instruction *I = dyn_cast<Instruction>(V)) {
340 Opcode = I->getOpcode();
341 U = I;
342 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
343 Opcode = C->getOpcode();
344 U = C;
345 }
Dan Gohman0586d912008-09-10 20:11:02 +0000346
Dan Gohman35893082008-09-18 23:23:44 +0000347 switch (Opcode) {
348 default: break;
349 case Instruction::BitCast:
350 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000351 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000352
353 case Instruction::IntToPtr:
354 // Look past no-op inttoptrs.
355 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000356 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000357 break;
Dan Gohman35893082008-09-18 23:23:44 +0000358
359 case Instruction::PtrToInt:
360 // Look past no-op ptrtoints.
361 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000362 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000363 break;
Dan Gohman35893082008-09-18 23:23:44 +0000364
365 case Instruction::Alloca: {
366 // Do static allocas.
367 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000368 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000369 if (SI != StaticAllocaMap.end()) {
370 AM.BaseType = X86AddressMode::FrameIndexBase;
371 AM.Base.FrameIndex = SI->second;
372 return true;
373 }
374 break;
Dan Gohman35893082008-09-18 23:23:44 +0000375 }
376
377 case Instruction::Add: {
378 // Adds of constants are common and easy enough.
379 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000380 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
381 // They have to fit in the 32-bit signed displacement field though.
382 if (isInt32(Disp)) {
383 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000384 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000385 }
Dan Gohman0586d912008-09-10 20:11:02 +0000386 }
Dan Gohman35893082008-09-18 23:23:44 +0000387 break;
388 }
389
390 case Instruction::GetElementPtr: {
391 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000392 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000393 unsigned IndexReg = AM.IndexReg;
394 unsigned Scale = AM.Scale;
395 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000396 // Iterate through the indices, folding what we can. Constants can be
397 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman35893082008-09-18 23:23:44 +0000398 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
399 i != e; ++i, ++GTI) {
400 Value *Op = *i;
401 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
402 const StructLayout *SL = TD.getStructLayout(STy);
403 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
404 Disp += SL->getElementOffset(Idx);
405 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000406 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman35893082008-09-18 23:23:44 +0000407 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
408 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000409 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000410 } else if (IndexReg == 0 &&
Chris Lattner4c1b6062009-06-27 05:24:12 +0000411 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000412 (S == 1 || S == 2 || S == 4 || S == 8)) {
413 // Scaled-index addressing.
414 Scale = S;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000415 IndexReg = getRegForGEPIndex(Op);
Dan Gohman35893082008-09-18 23:23:44 +0000416 if (IndexReg == 0)
417 return false;
418 } else
419 // Unsupported.
420 goto unsupported_gep;
421 }
422 }
Dan Gohman09aae462008-09-26 20:04:15 +0000423 // Check for displacement overflow.
424 if (!isInt32(Disp))
425 break;
Dan Gohman35893082008-09-18 23:23:44 +0000426 // Ok, the GEP indices were covered by constant-offset and scaled-index
427 // addressing. Update the address state and move on to examining the base.
428 AM.IndexReg = IndexReg;
429 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000430 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000431 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000432 unsupported_gep:
433 // Ok, the GEP indices weren't all covered.
434 break;
435 }
436 }
437
438 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000439 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000440 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000441 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000442 return false;
443
Dan Gohman97135e12008-09-26 19:15:30 +0000444 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000445 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000446 (AM.Base.Reg != 0 || AM.IndexReg != 0))
447 return false;
448
Dan Gohmane9865942009-02-23 22:03:08 +0000449 // Can't handle TLS yet.
450 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
451 if (GVar->isThreadLocal())
452 return false;
453
Chris Lattnerff7727f2009-07-09 06:41:35 +0000454 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000455 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000456
Chris Lattner0d786dd2009-07-10 07:48:51 +0000457 // Allow the subtarget to classify the global.
458 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
459
460 // If this reference is relative to the pic base, set it now.
461 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000462 // FIXME: How do we know Base.Reg is free??
Dan Gohman57c3dac2008-09-30 00:58:23 +0000463 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000464 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000465
466 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000467 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000468 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000469 if (Subtarget->isPICStyleRIPRel()) {
470 // Use rip-relative addressing if we can. Above we verified that the
471 // base and index registers are unused.
472 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
473 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000474 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000475 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000476 return true;
477 }
478
Chris Lattner0d786dd2009-07-10 07:48:51 +0000479 // Ok, we need to do a load from a stub. If we've already loaded from this
480 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000481 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
482 unsigned LoadReg;
483 if (I != LocalValueMap.end() && I->second != 0) {
484 LoadReg = I->second;
485 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000486 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000487 unsigned Opc = 0;
488 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000489 X86AddressMode StubAM;
490 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000491 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000492 StubAM.GVOpFlags = GVFlags;
493
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000495 Opc = X86::MOV64rm;
496 RC = X86::GR64RegisterClass;
497
Chris Lattner0d786dd2009-07-10 07:48:51 +0000498 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000499 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000500 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000501 Opc = X86::MOV32rm;
502 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000503 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000504
505 LoadReg = createResultReg(RC);
506 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
507
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000508 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000509 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000510 }
Chris Lattner18c59872009-06-27 04:16:01 +0000511
Chris Lattnerff7727f2009-07-09 06:41:35 +0000512 // Now construct the final address. Note that the Disp, Scale,
513 // and Index values may already be set here.
514 AM.Base.Reg = LoadReg;
515 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000516 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000517 }
518
Dan Gohman97135e12008-09-26 19:15:30 +0000519 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000520 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000521 if (AM.Base.Reg == 0) {
522 AM.Base.Reg = getRegForValue(V);
523 return AM.Base.Reg != 0;
524 }
525 if (AM.IndexReg == 0) {
526 assert(AM.Scale == 1 && "Scale with no index!");
527 AM.IndexReg = getRegForValue(V);
528 return AM.IndexReg != 0;
529 }
530 }
531
532 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000533}
534
Chris Lattner0aa43de2009-07-10 05:33:42 +0000535/// X86SelectCallAddress - Attempt to fill in an address from the given value.
536///
537bool X86FastISel::X86SelectCallAddress(Value *V, X86AddressMode &AM) {
538 User *U = NULL;
539 unsigned Opcode = Instruction::UserOp1;
540 if (Instruction *I = dyn_cast<Instruction>(V)) {
541 Opcode = I->getOpcode();
542 U = I;
543 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
544 Opcode = C->getOpcode();
545 U = C;
546 }
547
548 switch (Opcode) {
549 default: break;
550 case Instruction::BitCast:
551 // Look past bitcasts.
552 return X86SelectCallAddress(U->getOperand(0), AM);
553
554 case Instruction::IntToPtr:
555 // Look past no-op inttoptrs.
556 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
557 return X86SelectCallAddress(U->getOperand(0), AM);
558 break;
559
560 case Instruction::PtrToInt:
561 // Look past no-op ptrtoints.
562 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
563 return X86SelectCallAddress(U->getOperand(0), AM);
564 break;
565 }
566
567 // Handle constant address.
568 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
569 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000570 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000571 return false;
572
573 // RIP-relative addresses can't have additional register operands.
574 if (Subtarget->isPICStyleRIPRel() &&
575 (AM.Base.Reg != 0 || AM.IndexReg != 0))
576 return false;
577
Chris Lattner754b7652009-07-10 05:48:03 +0000578 // Can't handle TLS or DLLImport.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000579 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000580 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000581 return false;
582
583 // Okay, we've committed to selecting this global. Set up the basic address.
584 AM.GV = GV;
585
Chris Lattnere6c07b52009-07-10 05:45:15 +0000586 // No ABI requires an extra load for anything other than DLLImport, which
587 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000588 if (Subtarget->isPICStyleRIPRel()) {
589 // Use rip-relative addressing if we can. Above we verified that the
590 // base and index registers are unused.
591 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
592 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000593 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000594 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
595 } else if (Subtarget->isPICStyleGOT()) {
596 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000597 }
598
Chris Lattner0aa43de2009-07-10 05:33:42 +0000599 return true;
600 }
601
602 // If all else fails, try to materialize the value in a register.
603 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
604 if (AM.Base.Reg == 0) {
605 AM.Base.Reg = getRegForValue(V);
606 return AM.Base.Reg != 0;
607 }
608 if (AM.IndexReg == 0) {
609 assert(AM.Scale == 1 && "Scale with no index!");
610 AM.IndexReg = getRegForValue(V);
611 return AM.IndexReg != 0;
612 }
613 }
614
615 return false;
616}
617
618
Owen Andersona3971df2008-09-04 07:08:58 +0000619/// X86SelectStore - Select and emit code to implement store instructions.
620bool X86FastISel::X86SelectStore(Instruction* I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000621 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000622 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000623 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000624
Dan Gohman0586d912008-09-10 20:11:02 +0000625 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000626 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000627 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000628
Chris Lattner438949a2008-10-15 05:30:52 +0000629 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000630}
631
Evan Cheng8b19e562008-09-03 06:44:39 +0000632/// X86SelectLoad - Select and emit code to implement load instructions.
633///
Dan Gohman3df24e62008-09-03 23:12:08 +0000634bool X86FastISel::X86SelectLoad(Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000635 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000636 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000637 return false;
638
Dan Gohman0586d912008-09-10 20:11:02 +0000639 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000640 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000641 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000642
Evan Cheng0de588f2008-09-05 21:00:03 +0000643 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000644 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000645 UpdateValueMap(I, ResultReg);
646 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000647 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000648 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000649}
650
Owen Andersone50ed302009-08-10 22:56:29 +0000651static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000653 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 case MVT::i8: return X86::CMP8rr;
655 case MVT::i16: return X86::CMP16rr;
656 case MVT::i32: return X86::CMP32rr;
657 case MVT::i64: return X86::CMP64rr;
658 case MVT::f32: return X86::UCOMISSrr;
659 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000660 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000661}
662
Chris Lattner0e13c782008-10-15 04:13:29 +0000663/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
664/// of the comparison, return an opcode that works for the compare (e.g.
665/// CMP32ri) otherwise return 0.
Owen Andersone50ed302009-08-10 22:56:29 +0000666static unsigned X86ChooseCmpImmediateOpcode(EVT VT, ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000668 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000669 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 case MVT::i8: return X86::CMP8ri;
671 case MVT::i16: return X86::CMP16ri;
672 case MVT::i32: return X86::CMP32ri;
673 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000674 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
675 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000676 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000677 return X86::CMP64ri32;
678 return 0;
679 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000680}
681
Owen Andersone50ed302009-08-10 22:56:29 +0000682bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000683 unsigned Op0Reg = getRegForValue(Op0);
684 if (Op0Reg == 0) return false;
685
Chris Lattnerd53886b2008-10-15 05:18:04 +0000686 // Handle 'null' like i32/i64 0.
687 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000688 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000689
Chris Lattner9a08a612008-10-15 04:26:38 +0000690 // We have two options: compare with register or immediate. If the RHS of
691 // the compare is an immediate that we can fold into this compare, use
692 // CMPri, otherwise use CMPrr.
693 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000694 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000695 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner9a08a612008-10-15 04:26:38 +0000696 .addImm(Op1C->getSExtValue());
697 return true;
698 }
699 }
700
701 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
702 if (CompareOpc == 0) return false;
703
704 unsigned Op1Reg = getRegForValue(Op1);
705 if (Op1Reg == 0) return false;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000706 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000707
708 return true;
709}
710
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000711bool X86FastISel::X86SelectCmp(Instruction *I) {
712 CmpInst *CI = cast<CmpInst>(I);
713
Owen Andersone50ed302009-08-10 22:56:29 +0000714 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000715 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000716 return false;
717
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000718 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000719 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000720 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000721 switch (CI->getPredicate()) {
722 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000723 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
724 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000725
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000726 unsigned EReg = createResultReg(&X86::GR8RegClass);
727 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000728 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
729 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
730 BuildMI(MBB, DL,
731 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000732 UpdateValueMap(I, ResultReg);
733 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000734 }
735 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000736 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
737 return false;
738
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000739 unsigned NEReg = createResultReg(&X86::GR8RegClass);
740 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000741 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
742 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
743 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000744 UpdateValueMap(I, ResultReg);
745 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000746 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000747 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
748 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
749 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
750 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
751 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
752 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
753 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
754 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
755 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
756 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
757 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
758 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
759
760 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
761 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
762 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
763 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
764 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
765 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
766 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
767 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
768 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
769 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000770 default:
771 return false;
772 }
773
Chris Lattner9a08a612008-10-15 04:26:38 +0000774 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000775 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000776 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000777
Chris Lattner9a08a612008-10-15 04:26:38 +0000778 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000779 if (!X86FastEmitCompare(Op0, Op1, VT))
780 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000781
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000782 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000783 UpdateValueMap(I, ResultReg);
784 return true;
785}
Evan Cheng8b19e562008-09-03 06:44:39 +0000786
Dan Gohmand89ae992008-09-05 01:06:14 +0000787bool X86FastISel::X86SelectZExt(Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000788 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000789 if (I->getType()->isIntegerTy(8) &&
790 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000791 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000792 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000793 // Set the high bits to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000795 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000796 UpdateValueMap(I, ResultReg);
797 return true;
798 }
799
800 return false;
801}
802
Chris Lattner9a08a612008-10-15 04:26:38 +0000803
Dan Gohmand89ae992008-09-05 01:06:14 +0000804bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000805 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000806 // Handle a conditional branch.
807 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000808 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
809 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
810
Dan Gohmand98d6202008-10-02 22:15:21 +0000811 // Fold the common case of a conditional branch with a comparison.
812 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
813 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000814 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000815
Dan Gohmand98d6202008-10-02 22:15:21 +0000816 // Try to take advantage of fallthrough opportunities.
817 CmpInst::Predicate Predicate = CI->getPredicate();
818 if (MBB->isLayoutSuccessor(TrueMBB)) {
819 std::swap(TrueMBB, FalseMBB);
820 Predicate = CmpInst::getInversePredicate(Predicate);
821 }
822
Chris Lattner871d2462008-10-15 03:58:05 +0000823 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
824 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
825
Dan Gohmand98d6202008-10-02 22:15:21 +0000826 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000827 case CmpInst::FCMP_OEQ:
828 std::swap(TrueMBB, FalseMBB);
829 Predicate = CmpInst::FCMP_UNE;
830 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000831 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
832 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
833 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
834 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
835 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
836 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
837 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
838 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
839 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
840 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
841 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
842 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
843 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000844
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000845 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
846 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
847 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
848 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
849 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
850 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
851 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
852 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
853 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
854 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000855 default:
856 return false;
857 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000858
Chris Lattner709d8292008-10-15 04:02:26 +0000859 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
860 if (SwapArgs)
861 std::swap(Op0, Op1);
862
Chris Lattner9a08a612008-10-15 04:26:38 +0000863 // Emit a compare of the LHS and RHS, setting the flags.
864 if (!X86FastEmitCompare(Op0, Op1, VT))
865 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000866
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000867 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000868
869 if (Predicate == CmpInst::FCMP_UNE) {
870 // X86 requires a second branch to handle UNE (and OEQ,
871 // which is mapped to UNE above).
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000872 BuildMI(MBB, DL, TII.get(X86::JP_4)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000873 }
874
Dan Gohmand98d6202008-10-02 22:15:21 +0000875 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000876 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000877 return true;
878 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000879 } else if (ExtractValueInst *EI =
880 dyn_cast<ExtractValueInst>(BI->getCondition())) {
881 // Check to see if the branch instruction is from an "arithmetic with
882 // overflow" intrinsic. The main way these intrinsics are used is:
883 //
884 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
885 // %sum = extractvalue { i32, i1 } %t, 0
886 // %obit = extractvalue { i32, i1 } %t, 1
887 // br i1 %obit, label %overflow, label %normal
888 //
Dan Gohman653456c2009-01-07 00:15:08 +0000889 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000890 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000891 // looking for the SETO/SETB instruction. If an instruction modifies the
892 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
893 // convert the branch into a JO/JB instruction.
Chris Lattnera9a42252009-04-12 07:36:01 +0000894 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
895 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
896 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
897 const MachineInstr *SetMI = 0;
898 unsigned Reg = lookUpRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000899
Chris Lattnera9a42252009-04-12 07:36:01 +0000900 for (MachineBasicBlock::const_reverse_iterator
901 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
902 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000903
Chris Lattnera9a42252009-04-12 07:36:01 +0000904 if (MI.modifiesRegister(Reg)) {
905 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000906
Chris Lattnera9a42252009-04-12 07:36:01 +0000907 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
908 Reg = Src;
909 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000910 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000911
Chris Lattnera9a42252009-04-12 07:36:01 +0000912 SetMI = &MI;
913 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000914 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000915
Chris Lattnera9a42252009-04-12 07:36:01 +0000916 const TargetInstrDesc &TID = MI.getDesc();
917 if (TID.hasUnmodeledSideEffects() ||
918 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
919 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000920 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000921
922 if (SetMI) {
923 unsigned OpCode = SetMI->getOpcode();
924
925 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000926 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ?
927 X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +0000928 .addMBB(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +0000929 FastEmitBranch(FalseMBB);
930 MBB->addSuccessor(TrueMBB);
931 return true;
932 }
Bill Wendling9a901322008-12-10 19:44:24 +0000933 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000934 }
935 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000936 }
937
938 // Otherwise do a clumsy setcc and re-test it.
939 unsigned OpReg = getRegForValue(BI->getCondition());
940 if (OpReg == 0) return false;
941
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000942 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000943 BuildMI(MBB, DL, TII.get(X86::JNE_4)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000944 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000945 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000946 return true;
947}
948
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000949bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000950 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000951 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000952 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000953 CReg = X86::CL;
954 RC = &X86::GR8RegClass;
955 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000956 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
957 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
958 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000959 default: return false;
960 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000961 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000962 CReg = X86::CX;
963 RC = &X86::GR16RegClass;
964 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000965 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
966 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
967 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000968 default: return false;
969 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000970 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000971 CReg = X86::ECX;
972 RC = &X86::GR32RegClass;
973 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000974 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
975 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
976 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000977 default: return false;
978 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000979 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000980 CReg = X86::RCX;
981 RC = &X86::GR64RegClass;
982 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000983 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
984 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
985 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000986 default: return false;
987 }
988 } else {
989 return false;
990 }
991
Owen Andersone50ed302009-08-10 22:56:29 +0000992 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000993 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000994 return false;
995
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000996 unsigned Op0Reg = getRegForValue(I->getOperand(0));
997 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000998
999 // Fold immediate in shl(x,3).
1000 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
1001 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001002 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001003 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001004 UpdateValueMap(I, ResultReg);
1005 return true;
1006 }
1007
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001008 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1009 if (Op1Reg == 0) return false;
1010 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +00001011
1012 // The shift instruction uses X86::CL. If we defined a super-register
1013 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1014 // we're doing here.
1015 if (CReg != X86::CL)
Chris Lattner518bb532010-02-09 19:54:29 +00001016 BuildMI(MBB, DL, TII.get(TargetOpcode::EXTRACT_SUBREG), X86::CL)
Dan Gohman145b8282008-10-07 21:50:36 +00001017 .addReg(CReg).addImm(X86::SUBREG_8BIT);
1018
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001019 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001020 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001021 UpdateValueMap(I, ResultReg);
1022 return true;
1023}
1024
1025bool X86FastISel::X86SelectSelect(Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001026 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001027 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001028 return false;
1029
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001030 unsigned Opc = 0;
1031 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001032 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001033 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001034 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001035 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001036 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001037 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001039 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001040 RC = &X86::GR64RegClass;
1041 } else {
1042 return false;
1043 }
1044
1045 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1046 if (Op0Reg == 0) return false;
1047 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1048 if (Op1Reg == 0) return false;
1049 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1050 if (Op2Reg == 0) return false;
1051
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001052 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001053 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001054 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001055 UpdateValueMap(I, ResultReg);
1056 return true;
1057}
1058
Dan Gohman78efce62008-09-10 21:02:08 +00001059bool X86FastISel::X86SelectFPExt(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001060 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001061 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001062 I->getType()->isDoubleTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001063 Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001064 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001065 unsigned OpReg = getRegForValue(V);
1066 if (OpReg == 0) return false;
1067 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001068 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001069 UpdateValueMap(I, ResultReg);
1070 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001071 }
1072 }
1073
1074 return false;
1075}
1076
1077bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
1078 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001079 if (I->getType()->isFloatTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001080 Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001081 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001082 unsigned OpReg = getRegForValue(V);
1083 if (OpReg == 0) return false;
1084 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001085 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001086 UpdateValueMap(I, ResultReg);
1087 return true;
1088 }
1089 }
1090 }
1091
1092 return false;
1093}
1094
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001095bool X86FastISel::X86SelectTrunc(Instruction *I) {
1096 if (Subtarget->is64Bit())
1097 // All other cases should be handled by the tblgen generated code.
1098 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001099 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1100 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001101
1102 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001103 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001104 // All other cases should be handled by the tblgen generated code.
1105 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001106 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001107 // All other cases should be handled by the tblgen generated code.
1108 return false;
1109
1110 unsigned InputReg = getRegForValue(I->getOperand(0));
1111 if (!InputReg)
1112 // Unhandled operand. Halt "fast" selection and bail.
1113 return false;
1114
Dan Gohman62417622009-04-27 16:33:14 +00001115 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001116 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1117 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001118 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001119 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001120 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001121
1122 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001123 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Evan Cheng536ab132009-01-22 09:10:11 +00001124 CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001125 if (!ResultReg)
1126 return false;
1127
1128 UpdateValueMap(I, ResultReg);
1129 return true;
1130}
1131
Bill Wendling52370a12008-12-09 02:42:50 +00001132bool X86FastISel::X86SelectExtractValue(Instruction *I) {
1133 ExtractValueInst *EI = cast<ExtractValueInst>(I);
1134 Value *Agg = EI->getAggregateOperand();
1135
Chris Lattnera9a42252009-04-12 07:36:01 +00001136 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
1137 switch (CI->getIntrinsicID()) {
1138 default: break;
1139 case Intrinsic::sadd_with_overflow:
1140 case Intrinsic::uadd_with_overflow:
1141 // Cheat a little. We know that the registers for "add" and "seto" are
1142 // allocated sequentially. However, we only keep track of the register
1143 // for "add" in the value map. Use extractvalue's index to get the
1144 // correct register for "seto".
1145 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1146 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001147 }
1148 }
1149
1150 return false;
1151}
1152
Chris Lattnera9a42252009-04-12 07:36:01 +00001153bool X86FastISel::X86VisitIntrinsicCall(IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001154 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001155 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001156 default: return false;
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001157 case Intrinsic::dbg_declare: {
1158 DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
1159 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001160 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001161 if (!X86SelectAddress(DI->getAddress(), AM))
1162 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001163 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001164 addFullAddress(BuildMI(MBB, DL, II), AM).addImm(0).
1165 addMetadata(DI->getVariable());
1166 return true;
1167 }
Eric Christopher77f79892010-01-18 22:11:29 +00001168 case Intrinsic::trap: {
1169 BuildMI(MBB, DL, TII.get(X86::TRAP));
1170 return true;
1171 }
Bill Wendling52370a12008-12-09 02:42:50 +00001172 case Intrinsic::sadd_with_overflow:
1173 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001174 // Replace "add with overflow" intrinsics with an "add" instruction followed
1175 // by a seto/setc instruction. Later on, when the "extractvalue"
1176 // instructions are encountered, we use the fact that two registers were
1177 // created sequentially to get the correct registers for the "sum" and the
1178 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001179 const Function *Callee = I.getCalledFunction();
1180 const Type *RetTy =
1181 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1182
Owen Andersone50ed302009-08-10 22:56:29 +00001183 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001184 if (!isTypeLegal(RetTy, VT))
1185 return false;
1186
1187 Value *Op1 = I.getOperand(1);
1188 Value *Op2 = I.getOperand(2);
1189 unsigned Reg1 = getRegForValue(Op1);
1190 unsigned Reg2 = getRegForValue(Op2);
1191
1192 if (Reg1 == 0 || Reg2 == 0)
1193 // FIXME: Handle values *not* in registers.
1194 return false;
1195
1196 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001197 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001198 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001199 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001200 OpC = X86::ADD64rr;
1201 else
1202 return false;
1203
1204 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001205 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001206 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001207
Chris Lattner8d57b772009-04-12 07:51:14 +00001208 // If the add with overflow is an intra-block value then we just want to
1209 // create temporaries for it like normal. If it is a cross-block value then
1210 // UpdateValueMap will return the cross-block register used. Since we
1211 // *really* want the value to be live in the register pair known by
1212 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1213 // the cross block case. In the non-cross-block case, we should just make
1214 // another register for the value.
1215 if (DestReg1 != ResultReg)
1216 ResultReg = DestReg1+1;
1217 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001219
Chris Lattnera9a42252009-04-12 07:36:01 +00001220 unsigned Opc = X86::SETBr;
1221 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1222 Opc = X86::SETOr;
1223 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001224 return true;
1225 }
1226 }
1227}
1228
Evan Chengf3d4efe2008-09-07 09:09:33 +00001229bool X86FastISel::X86SelectCall(Instruction *I) {
1230 CallInst *CI = cast<CallInst>(I);
1231 Value *Callee = I->getOperand(0);
1232
1233 // Can't handle inline asm yet.
1234 if (isa<InlineAsm>(Callee))
1235 return false;
1236
Bill Wendling52370a12008-12-09 02:42:50 +00001237 // Handle intrinsic calls.
Chris Lattnera9a42252009-04-12 07:36:01 +00001238 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
1239 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001240
Evan Chengf3d4efe2008-09-07 09:09:33 +00001241 // Handle only C and fastcc calling conventions for now.
1242 CallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001243 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001244 if (CC != CallingConv::C &&
1245 CC != CallingConv::Fast &&
1246 CC != CallingConv::X86_FastCall)
1247 return false;
1248
Evan Cheng381993f2010-01-27 00:00:57 +00001249 // fastcc with -tailcallopt is intended to provide a guaranteed
1250 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001251 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001252 return false;
1253
Evan Chengf3d4efe2008-09-07 09:09:33 +00001254 // Let SDISel handle vararg functions.
1255 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1256 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1257 if (FTy->isVarArg())
1258 return false;
1259
1260 // Handle *simple* calls for now.
1261 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001262 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001263 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001265 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001266 return false;
1267
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001268 // Materialize callee address in a register. FIXME: GV address can be
1269 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001270 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001271 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001272 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001273 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001274 GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001275 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001276 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001277 } else if (CalleeAM.Base.Reg != 0) {
1278 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001279 } else
1280 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001281
Evan Chengdebdea02008-09-08 17:15:42 +00001282 // Allow calls which produce i1 results.
1283 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001284 if (RetVT == MVT::i1) {
1285 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001286 AndToI1 = true;
1287 }
1288
Evan Chengf3d4efe2008-09-07 09:09:33 +00001289 // Deal with call operands first.
Chris Lattner241ab472008-10-15 05:38:32 +00001290 SmallVector<Value*, 8> ArgVals;
1291 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001292 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001293 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001294 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001295 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001296 ArgVTs.reserve(CS.arg_size());
1297 ArgFlags.reserve(CS.arg_size());
1298 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1299 i != e; ++i) {
1300 unsigned Arg = getRegForValue(*i);
1301 if (Arg == 0)
1302 return false;
1303 ISD::ArgFlagsTy Flags;
1304 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001305 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001306 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001307 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001308 Flags.setZExt();
1309
1310 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001311 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1312 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1313 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1314 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001315 return false;
1316
1317 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001318 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001319 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001320 return false;
1321 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1322 Flags.setOrigAlign(OriginalAlignment);
1323
1324 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001325 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001326 ArgVTs.push_back(ArgVT);
1327 ArgFlags.push_back(Flags);
1328 }
1329
1330 // Analyze operands of the call, assigning locations to each operand.
1331 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001332 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001333 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1334
1335 // Get a count of how many bytes are to be pushed on the stack.
1336 unsigned NumBytes = CCInfo.getNextStackOffset();
1337
1338 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001339 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001340 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001341
Chris Lattner438949a2008-10-15 05:30:52 +00001342 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001343 // copies / loads.
1344 SmallVector<unsigned, 4> RegArgs;
1345 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1346 CCValAssign &VA = ArgLocs[i];
1347 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001348 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001349
1350 // Promote the value if needed.
1351 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001352 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001353 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001354 case CCValAssign::SExt: {
1355 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1356 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001357 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001358 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001359 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001360 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001361 }
1362 case CCValAssign::ZExt: {
1363 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1364 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001365 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001366 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001367 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001368 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001369 }
1370 case CCValAssign::AExt: {
1371 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1372 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001373 if (!Emitted)
1374 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001375 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001376 if (!Emitted)
1377 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1378 Arg, ArgVT, Arg);
1379
Chris Lattnera33649e2008-12-19 17:03:38 +00001380 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001381 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001382 break;
1383 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001384 case CCValAssign::BCvt: {
1385 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
1386 ISD::BIT_CONVERT, Arg);
1387 assert(BC != 0 && "Failed to emit a bitcast!");
1388 Arg = BC;
1389 ArgVT = VA.getLocVT();
1390 break;
1391 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001392 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001393
1394 if (VA.isRegLoc()) {
1395 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1396 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1397 Arg, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001398 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001399 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001400 RegArgs.push_back(VA.getLocReg());
1401 } else {
1402 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001403 X86AddressMode AM;
1404 AM.Base.Reg = StackPtr;
1405 AM.Disp = LocMemOffset;
Chris Lattner241ab472008-10-15 05:38:32 +00001406 Value *ArgVal = ArgVals[VA.getValNo()];
1407
1408 // If this is a really simple value, emit this with the Value* version of
1409 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1410 // can cause us to reevaluate the argument.
1411 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1412 X86FastEmitStore(ArgVT, ArgVal, AM);
1413 else
1414 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001415 }
1416 }
1417
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001418 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1419 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001420 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001421 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001422 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001423 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001424 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001425 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001426 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001427
Evan Chengf3d4efe2008-09-07 09:09:33 +00001428 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001429 MachineInstrBuilder MIB;
1430 if (CalleeOp) {
1431 // Register-indirect call.
1432 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
1433 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
1434
1435 } else {
1436 // Direct call.
1437 assert(GV && "Not a direct call");
1438 unsigned CallOpc =
1439 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1440
1441 // See if we need any target-specific flags on the GV operand.
1442 unsigned char OpFlags = 0;
1443
1444 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1445 // external symbols most go through the PLT in PIC mode. If the symbol
1446 // has hidden or protected visibility, or if it is static or local, then
1447 // we don't need to use the PLT - we can directly call it.
1448 if (Subtarget->isTargetELF() &&
1449 TM.getRelocationModel() == Reloc::PIC_ &&
1450 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1451 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001452 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001453 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1454 Subtarget->getDarwinVers() < 9) {
1455 // PC-relative references to external symbols should go through $stub,
1456 // unless we're building with the leopard linker or later, which
1457 // automatically synthesizes these stubs.
1458 OpFlags = X86II::MO_DARWIN_STUB;
1459 }
1460
1461
1462 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
1463 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001464
1465 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001466 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001467 MIB.addReg(X86::EBX);
1468
Evan Chengf3d4efe2008-09-07 09:09:33 +00001469 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001470 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1471 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001472
1473 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001474 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001475 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001476
1477 // Now handle call return value (if any).
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001479 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001480 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001481 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1482
1483 // Copy all of the result registers out of their specified physreg.
1484 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001485 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001486 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1487 TargetRegisterClass *SrcRC = DstRC;
1488
1489 // If this is a call to a function that returns an fp value on the x87 fp
1490 // stack, but where we prefer to use the value in xmm registers, copy it
1491 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1492 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1493 RVLocs[0].getLocReg() == X86::ST1) &&
1494 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001495 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001496 SrcRC = X86::RSTRegisterClass;
1497 DstRC = X86::RFP80RegisterClass;
1498 }
1499
1500 unsigned ResultReg = createResultReg(DstRC);
1501 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1502 RVLocs[0].getLocReg(), DstRC, SrcRC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001503 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001504 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001505 if (CopyVT != RVLocs[0].getValVT()) {
1506 // Round the F80 the right size, which also moves to the appropriate xmm
1507 // register. This is accomplished by storing the F80 value in memory and
1508 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001509 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001510 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001511 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001512 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001513 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001514 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001515 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001516 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001517 ResultReg = createResultReg(DstRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001518 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001519 }
1520
Evan Chengdebdea02008-09-08 17:15:42 +00001521 if (AndToI1) {
1522 // Mask out all but lowest bit for some call which produces an i1.
1523 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001524 BuildMI(MBB, DL,
1525 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001526 ResultReg = AndResult;
1527 }
1528
Evan Chengf3d4efe2008-09-07 09:09:33 +00001529 UpdateValueMap(I, ResultReg);
1530 }
1531
1532 return true;
1533}
1534
1535
Dan Gohman99b21822008-08-28 23:21:34 +00001536bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001537X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001538 switch (I->getOpcode()) {
1539 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001540 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001541 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001542 case Instruction::Store:
1543 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001544 case Instruction::ICmp:
1545 case Instruction::FCmp:
1546 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001547 case Instruction::ZExt:
1548 return X86SelectZExt(I);
1549 case Instruction::Br:
1550 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001551 case Instruction::Call:
1552 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001553 case Instruction::LShr:
1554 case Instruction::AShr:
1555 case Instruction::Shl:
1556 return X86SelectShift(I);
1557 case Instruction::Select:
1558 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001559 case Instruction::Trunc:
1560 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001561 case Instruction::FPExt:
1562 return X86SelectFPExt(I);
1563 case Instruction::FPTrunc:
1564 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001565 case Instruction::ExtractValue:
1566 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001567 case Instruction::IntToPtr: // Deliberate fall-through.
1568 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001569 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1570 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001571 if (DstVT.bitsGT(SrcVT))
1572 return X86SelectZExt(I);
1573 if (DstVT.bitsLT(SrcVT))
1574 return X86SelectTrunc(I);
1575 unsigned Reg = getRegForValue(I->getOperand(0));
1576 if (Reg == 0) return false;
1577 UpdateValueMap(I, Reg);
1578 return true;
1579 }
Dan Gohman99b21822008-08-28 23:21:34 +00001580 }
1581
1582 return false;
1583}
1584
Dan Gohman0586d912008-09-10 20:11:02 +00001585unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001586 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001587 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001588 return false;
1589
1590 // Get opcode and regclass of the output for the given load instruction.
1591 unsigned Opc = 0;
1592 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001593 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001594 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001595 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001596 Opc = X86::MOV8rm;
1597 RC = X86::GR8RegisterClass;
1598 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001599 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001600 Opc = X86::MOV16rm;
1601 RC = X86::GR16RegisterClass;
1602 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001603 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001604 Opc = X86::MOV32rm;
1605 RC = X86::GR32RegisterClass;
1606 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001607 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001608 // Must be in x86-64 mode.
1609 Opc = X86::MOV64rm;
1610 RC = X86::GR64RegisterClass;
1611 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001612 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001613 if (Subtarget->hasSSE1()) {
1614 Opc = X86::MOVSSrm;
1615 RC = X86::FR32RegisterClass;
1616 } else {
1617 Opc = X86::LD_Fp32m;
1618 RC = X86::RFP32RegisterClass;
1619 }
1620 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001621 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001622 if (Subtarget->hasSSE2()) {
1623 Opc = X86::MOVSDrm;
1624 RC = X86::FR64RegisterClass;
1625 } else {
1626 Opc = X86::LD_Fp64m;
1627 RC = X86::RFP64RegisterClass;
1628 }
1629 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001630 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001631 // No f80 support yet.
1632 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001633 }
1634
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001635 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001636 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001637 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001638 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001639 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001640 Opc = X86::LEA32r;
1641 else
1642 Opc = X86::LEA64r;
1643 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001644 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001645 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001646 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001647 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001648 }
1649
Owen Anderson3b217c62008-09-06 01:11:01 +00001650 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001651 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001652 if (Align == 0) {
1653 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001654 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001655 }
Owen Anderson95267a12008-09-05 00:06:23 +00001656
Dan Gohman5396c992008-09-30 01:21:32 +00001657 // x86-32 PIC requires a PIC base register for constant pools.
1658 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001659 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001660 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001661 OpFlag = X86II::MO_PIC_BASE_OFFSET;
1662 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1663 } else if (Subtarget->isPICStyleGOT()) {
1664 OpFlag = X86II::MO_GOTOFF;
1665 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1666 } else if (Subtarget->isPICStyleRIPRel() &&
1667 TM.getCodeModel() == CodeModel::Small) {
1668 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001669 }
Dan Gohman5396c992008-09-30 01:21:32 +00001670
1671 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001672 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001673 unsigned ResultReg = createResultReg(RC);
Chris Lattner89da6992009-06-27 01:31:51 +00001674 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
1675 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001676
Owen Anderson95267a12008-09-05 00:06:23 +00001677 return ResultReg;
1678}
1679
Dan Gohman0586d912008-09-10 20:11:02 +00001680unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001681 // Fail on dynamic allocas. At this point, getRegForValue has already
1682 // checked its CSE maps, so if we're here trying to handle a dynamic
1683 // alloca, we're not going to succeed. X86SelectAddress has a
1684 // check for dynamic allocas, because it's called directly from
1685 // various places, but TargetMaterializeAlloca also needs a check
1686 // in order to avoid recursion between getRegForValue,
1687 // X86SelectAddrss, and TargetMaterializeAlloca.
1688 if (!StaticAllocaMap.count(C))
1689 return 0;
1690
Dan Gohman0586d912008-09-10 20:11:02 +00001691 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001692 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001693 return 0;
1694 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1695 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1696 unsigned ResultReg = createResultReg(RC);
Rafael Espindola094fad32009-04-08 21:14:34 +00001697 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001698 return ResultReg;
1699}
1700
Evan Chengc3f44b02008-09-03 00:03:49 +00001701namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001702 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001703 MachineModuleInfo *mmi,
Devang Patel83489bb2009-01-13 00:35:13 +00001704 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001705 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001706 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001707 DenseMap<const AllocaInst *, int> &am
1708#ifndef NDEBUG
1709 , SmallSet<Instruction*, 8> &cil
1710#endif
1711 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001712 return new X86FastISel(mf, mmi, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001713#ifndef NDEBUG
1714 , cil
1715#endif
1716 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001717 }
Dan Gohman99b21822008-08-28 23:21:34 +00001718}