blob: e00697cecb46a8c8585c0b29b0a7b441938a76d4 [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"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Bill Wendling52370a12008-12-09 02:42:50 +000025#include "llvm/Intrinsics.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000026#include "llvm/CodeGen/FastISel.h"
Owen Anderson95267a12008-09-05 00:06:23 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000030#include "llvm/Support/CallSite.h"
Dan Gohman35893082008-09-18 23:23:44 +000031#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000032
33using namespace llvm;
34
35class X86FastISel : public FastISel {
36 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
37 /// make the right decision when generating code for different targets.
38 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000039
40 /// StackPtr - Register used as the stack pointer.
41 ///
42 unsigned StackPtr;
43
44 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
45 /// floating point ops.
46 /// When SSE is available, use it for f32 operations.
47 /// When SSE2 is available, use it for f64 operations.
48 bool X86ScalarSSEf64;
49 bool X86ScalarSSEf32;
50
Evan Cheng8b19e562008-09-03 06:44:39 +000051public:
Dan Gohman3df24e62008-09-03 23:12:08 +000052 explicit X86FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +000053 MachineModuleInfo *mmi,
Devang Patel83489bb2009-01-13 00:35:13 +000054 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +000055 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000056 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +000057 DenseMap<const AllocaInst *, int> &am
58#ifndef NDEBUG
59 , SmallSet<Instruction*, 8> &cil
60#endif
61 )
Devang Patel83489bb2009-01-13 00:35:13 +000062 : FastISel(mf, mmi, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +000063#ifndef NDEBUG
64 , cil
65#endif
66 ) {
Evan Cheng88e30412008-09-03 01:04:47 +000067 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000068 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
69 X86ScalarSSEf64 = Subtarget->hasSSE2();
70 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000071 }
Evan Chengc3f44b02008-09-03 00:03:49 +000072
Dan Gohman3df24e62008-09-03 23:12:08 +000073 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000074
Dan Gohman1adf1b02008-08-19 21:45:35 +000075#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000076
77private:
Chris Lattner9a08a612008-10-15 04:26:38 +000078 bool X86FastEmitCompare(Value *LHS, Value *RHS, MVT VT);
79
Dan Gohman0586d912008-09-10 20:11:02 +000080 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000081
Chris Lattner438949a2008-10-15 05:30:52 +000082 bool X86FastEmitStore(MVT VT, Value *Val,
83 const X86AddressMode &AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +000084 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000085 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000086
87 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
88 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000089
Dan Gohman2ff7fd12008-09-19 22:16:54 +000090 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000091
Dan Gohman3df24e62008-09-03 23:12:08 +000092 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000093
94 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000095
96 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000097
98 bool X86SelectZExt(Instruction *I);
99
100 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000101
102 bool X86SelectShift(Instruction *I);
103
104 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000105
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000106 bool X86SelectTrunc(Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000107
Dan Gohman78efce62008-09-10 21:02:08 +0000108 bool X86SelectFPExt(Instruction *I);
109 bool X86SelectFPTrunc(Instruction *I);
110
Bill Wendling52370a12008-12-09 02:42:50 +0000111 bool X86SelectExtractValue(Instruction *I);
112
113 bool X86VisitIntrinsicCall(CallInst &I, unsigned Intrinsic);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000114 bool X86SelectCall(Instruction *I);
115
116 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
117
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000118 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000119 return getTargetMachine()->getInstrInfo();
120 }
121 const X86TargetMachine *getTargetMachine() const {
122 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000123 }
124
Dan Gohman0586d912008-09-10 20:11:02 +0000125 unsigned TargetMaterializeConstant(Constant *C);
126
127 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000128
129 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
130 /// computed in an SSE register, not on the X87 floating point stack.
131 bool isScalarFPTypeInSSEReg(MVT VT) const {
132 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
133 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
134 }
135
Chris Lattner160f6cc2008-10-15 05:07:36 +0000136 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000137};
Dan Gohman99b21822008-08-28 23:21:34 +0000138
Chris Lattner160f6cc2008-10-15 05:07:36 +0000139bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
140 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000141 if (VT == MVT::Other || !VT.isSimple())
142 // Unhandled type. Halt "fast" selection and bail.
143 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000144
Dan Gohman9b66d732008-09-30 00:48:39 +0000145 // For now, require SSE/SSE2 for performing floating-point operations,
146 // since x87 requires additional work.
147 if (VT == MVT::f64 && !X86ScalarSSEf64)
148 return false;
149 if (VT == MVT::f32 && !X86ScalarSSEf32)
150 return false;
151 // Similarly, no f80 support yet.
152 if (VT == MVT::f80)
153 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000154 // We only handle legal types. For example, on x86-32 the instruction
155 // selector contains all of the 64-bit instructions from x86-64,
156 // under the assumption that i64 won't be used if the target doesn't
157 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000158 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000159}
160
161#include "X86GenCallingConv.inc"
162
163/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
164/// convention.
165CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
166 if (Subtarget->is64Bit()) {
167 if (Subtarget->isTargetWin64())
168 return CC_X86_Win64_C;
169 else if (CC == CallingConv::Fast && isTaillCall)
170 return CC_X86_64_TailCall;
171 else
172 return CC_X86_64_C;
173 }
174
175 if (CC == CallingConv::X86_FastCall)
176 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177 else if (CC == CallingConv::Fast)
178 return CC_X86_32_FastCC;
179 else
180 return CC_X86_32_C;
181}
182
Evan Cheng0de588f2008-09-05 21:00:03 +0000183/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000184/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000185/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000186bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000187 unsigned &ResultReg) {
188 // Get opcode and regclass of the output for the given load instruction.
189 unsigned Opc = 0;
190 const TargetRegisterClass *RC = NULL;
191 switch (VT.getSimpleVT()) {
192 default: return false;
193 case MVT::i8:
194 Opc = X86::MOV8rm;
195 RC = X86::GR8RegisterClass;
196 break;
197 case MVT::i16:
198 Opc = X86::MOV16rm;
199 RC = X86::GR16RegisterClass;
200 break;
201 case MVT::i32:
202 Opc = X86::MOV32rm;
203 RC = X86::GR32RegisterClass;
204 break;
205 case MVT::i64:
206 // Must be in x86-64 mode.
207 Opc = X86::MOV64rm;
208 RC = X86::GR64RegisterClass;
209 break;
210 case MVT::f32:
211 if (Subtarget->hasSSE1()) {
212 Opc = X86::MOVSSrm;
213 RC = X86::FR32RegisterClass;
214 } else {
215 Opc = X86::LD_Fp32m;
216 RC = X86::RFP32RegisterClass;
217 }
218 break;
219 case MVT::f64:
220 if (Subtarget->hasSSE2()) {
221 Opc = X86::MOVSDrm;
222 RC = X86::FR64RegisterClass;
223 } else {
224 Opc = X86::LD_Fp64m;
225 RC = X86::RFP64RegisterClass;
226 }
227 break;
228 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000229 // No f80 support yet.
230 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000231 }
232
233 ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000234 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000235 return true;
236}
237
Evan Chengf3d4efe2008-09-07 09:09:33 +0000238/// X86FastEmitStore - Emit a machine instruction to store a value Val of
239/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
240/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000241/// i.e. V. Return true if it is possible.
242bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000243X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000244 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000245 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000246 unsigned Opc = 0;
Evan Cheng0de588f2008-09-05 21:00:03 +0000247 switch (VT.getSimpleVT()) {
Chris Lattner241ab472008-10-15 05:38:32 +0000248 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000249 default: return false;
Chris Lattner241ab472008-10-15 05:38:32 +0000250 case MVT::i8: Opc = X86::MOV8mr; break;
251 case MVT::i16: Opc = X86::MOV16mr; break;
252 case MVT::i32: Opc = X86::MOV32mr; break;
253 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
Evan Cheng0de588f2008-09-05 21:00:03 +0000254 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000255 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000256 break;
257 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000258 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000259 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000260 }
Chris Lattner438949a2008-10-15 05:30:52 +0000261
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000262 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000263 return true;
264}
265
Chris Lattner438949a2008-10-15 05:30:52 +0000266bool X86FastISel::X86FastEmitStore(MVT VT, Value *Val,
267 const X86AddressMode &AM) {
268 // Handle 'null' like i32/i64 0.
269 if (isa<ConstantPointerNull>(Val))
270 Val = Constant::getNullValue(TD.getIntPtrType());
271
272 // If this is a store of a simple constant, fold the constant into the store.
273 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
274 unsigned Opc = 0;
275 switch (VT.getSimpleVT()) {
276 default: break;
277 case MVT::i8: Opc = X86::MOV8mi; break;
278 case MVT::i16: Opc = X86::MOV16mi; break;
279 case MVT::i32: Opc = X86::MOV32mi; break;
280 case MVT::i64:
281 // Must be a 32-bit sign extended value.
282 if ((int)CI->getSExtValue() == CI->getSExtValue())
283 Opc = X86::MOV64mi32;
284 break;
285 }
286
287 if (Opc) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000288 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
289 .addImm(CI->getSExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000290 return true;
291 }
292 }
293
294 unsigned ValReg = getRegForValue(Val);
295 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000296 return false;
297
298 return X86FastEmitStore(VT, ValReg, AM);
299}
300
Evan Cheng24e3a902008-09-08 06:35:17 +0000301/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
302/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
303/// ISD::SIGN_EXTEND).
304bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
305 unsigned Src, MVT SrcVT,
306 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000307 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
308
309 if (RR != 0) {
310 ResultReg = RR;
311 return true;
312 } else
313 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000314}
315
Dan Gohman0586d912008-09-10 20:11:02 +0000316/// X86SelectAddress - Attempt to fill in an address from the given value.
317///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000318bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000319 User *U;
320 unsigned Opcode = Instruction::UserOp1;
321 if (Instruction *I = dyn_cast<Instruction>(V)) {
322 Opcode = I->getOpcode();
323 U = I;
324 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
325 Opcode = C->getOpcode();
326 U = C;
327 }
Dan Gohman0586d912008-09-10 20:11:02 +0000328
Dan Gohman35893082008-09-18 23:23:44 +0000329 switch (Opcode) {
330 default: break;
331 case Instruction::BitCast:
332 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000333 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000334
335 case Instruction::IntToPtr:
336 // Look past no-op inttoptrs.
337 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000338 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000339 break;
Dan Gohman35893082008-09-18 23:23:44 +0000340
341 case Instruction::PtrToInt:
342 // Look past no-op ptrtoints.
343 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000344 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000345 break;
Dan Gohman35893082008-09-18 23:23:44 +0000346
347 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000348 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000349 // Do static allocas.
350 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000351 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000352 if (SI != StaticAllocaMap.end()) {
353 AM.BaseType = X86AddressMode::FrameIndexBase;
354 AM.Base.FrameIndex = SI->second;
355 return true;
356 }
357 break;
Dan Gohman35893082008-09-18 23:23:44 +0000358 }
359
360 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000361 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000362 // Adds of constants are common and easy enough.
363 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000364 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
365 // They have to fit in the 32-bit signed displacement field though.
366 if (isInt32(Disp)) {
367 AM.Disp = (uint32_t)Disp;
368 return X86SelectAddress(U->getOperand(0), AM, isCall);
369 }
Dan Gohman0586d912008-09-10 20:11:02 +0000370 }
Dan Gohman35893082008-09-18 23:23:44 +0000371 break;
372 }
373
374 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000375 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000376 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000377 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000378 unsigned IndexReg = AM.IndexReg;
379 unsigned Scale = AM.Scale;
380 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000381 // Iterate through the indices, folding what we can. Constants can be
382 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman35893082008-09-18 23:23:44 +0000383 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
384 i != e; ++i, ++GTI) {
385 Value *Op = *i;
386 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
387 const StructLayout *SL = TD.getStructLayout(STy);
388 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
389 Disp += SL->getElementOffset(Idx);
390 } else {
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000391 uint64_t S = TD.getTypePaddedSize(GTI.getIndexedType());
Dan Gohman35893082008-09-18 23:23:44 +0000392 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
393 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000394 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000395 } else if (IndexReg == 0 &&
Dan Gohman97135e12008-09-26 19:15:30 +0000396 (!AM.GV ||
397 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000398 (S == 1 || S == 2 || S == 4 || S == 8)) {
399 // Scaled-index addressing.
400 Scale = S;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000401 IndexReg = getRegForGEPIndex(Op);
Dan Gohman35893082008-09-18 23:23:44 +0000402 if (IndexReg == 0)
403 return false;
404 } else
405 // Unsupported.
406 goto unsupported_gep;
407 }
408 }
Dan Gohman09aae462008-09-26 20:04:15 +0000409 // Check for displacement overflow.
410 if (!isInt32(Disp))
411 break;
Dan Gohman35893082008-09-18 23:23:44 +0000412 // Ok, the GEP indices were covered by constant-offset and scaled-index
413 // addressing. Update the address state and move on to examining the base.
414 AM.IndexReg = IndexReg;
415 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000416 AM.Disp = (uint32_t)Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000417 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000418 unsupported_gep:
419 // Ok, the GEP indices weren't all covered.
420 break;
421 }
422 }
423
424 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000425 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000426 // Can't handle alternate code models yet.
427 if (TM.getCodeModel() != CodeModel::Default &&
428 TM.getCodeModel() != CodeModel::Small)
429 return false;
430
Dan Gohman97135e12008-09-26 19:15:30 +0000431 // RIP-relative addresses can't have additional register operands.
432 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
433 (AM.Base.Reg != 0 || AM.IndexReg != 0))
434 return false;
435
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000436 // Set up the basic address.
437 AM.GV = GV;
438 if (!isCall &&
439 TM.getRelocationModel() == Reloc::PIC_ &&
440 !Subtarget->is64Bit())
Dan Gohman57c3dac2008-09-30 00:58:23 +0000441 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000442
443 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000444 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
445 // Check to see if we've already materialized this
446 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000447 if (unsigned Reg = LocalValueMap[V]) {
448 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000449 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000450 return true;
451 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000452 // Issue load from stub if necessary.
453 unsigned Opc = 0;
454 const TargetRegisterClass *RC = NULL;
455 if (TLI.getPointerTy() == MVT::i32) {
456 Opc = X86::MOV32rm;
457 RC = X86::GR32RegisterClass;
458 } else {
459 Opc = X86::MOV64rm;
460 RC = X86::GR64RegisterClass;
461 }
Dan Gohman789ce772008-09-25 23:34:02 +0000462
463 X86AddressMode StubAM;
464 StubAM.Base.Reg = AM.Base.Reg;
465 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000466 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000467 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), StubAM);
Dan Gohman789ce772008-09-25 23:34:02 +0000468
469 // Now construct the final address. Note that the Disp, Scale,
470 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000471 AM.Base.Reg = ResultReg;
472 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000473
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000474 // Prevent loading GV stub multiple times in same MBB.
475 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000476 }
477 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000478 }
479
Dan Gohman97135e12008-09-26 19:15:30 +0000480 // If all else fails, try to materialize the value in a register.
Dan Gohman7962e852008-09-29 21:13:15 +0000481 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000482 if (AM.Base.Reg == 0) {
483 AM.Base.Reg = getRegForValue(V);
484 return AM.Base.Reg != 0;
485 }
486 if (AM.IndexReg == 0) {
487 assert(AM.Scale == 1 && "Scale with no index!");
488 AM.IndexReg = getRegForValue(V);
489 return AM.IndexReg != 0;
490 }
491 }
492
493 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000494}
495
Owen Andersona3971df2008-09-04 07:08:58 +0000496/// X86SelectStore - Select and emit code to implement store instructions.
497bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000498 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000499 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000500 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000501
Dan Gohman0586d912008-09-10 20:11:02 +0000502 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000503 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000504 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000505
Chris Lattner438949a2008-10-15 05:30:52 +0000506 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000507}
508
Evan Cheng8b19e562008-09-03 06:44:39 +0000509/// X86SelectLoad - Select and emit code to implement load instructions.
510///
Dan Gohman3df24e62008-09-03 23:12:08 +0000511bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000512 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000513 if (!isTypeLegal(I->getType(), VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000514 return false;
515
Dan Gohman0586d912008-09-10 20:11:02 +0000516 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000517 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000518 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000519
Evan Cheng0de588f2008-09-05 21:00:03 +0000520 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000521 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000522 UpdateValueMap(I, ResultReg);
523 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000524 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000525 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000526}
527
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000528static unsigned X86ChooseCmpOpcode(MVT VT) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000529 switch (VT.getSimpleVT()) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000530 default: return 0;
531 case MVT::i8: return X86::CMP8rr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000532 case MVT::i16: return X86::CMP16rr;
533 case MVT::i32: return X86::CMP32rr;
534 case MVT::i64: return X86::CMP64rr;
535 case MVT::f32: return X86::UCOMISSrr;
536 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000537 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000538}
539
Chris Lattner0e13c782008-10-15 04:13:29 +0000540/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
541/// of the comparison, return an opcode that works for the compare (e.g.
542/// CMP32ri) otherwise return 0.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000543static unsigned X86ChooseCmpImmediateOpcode(MVT VT, ConstantInt *RHSC) {
544 switch (VT.getSimpleVT()) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000545 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000546 default: return 0;
547 case MVT::i8: return X86::CMP8ri;
548 case MVT::i16: return X86::CMP16ri;
549 case MVT::i32: return X86::CMP32ri;
550 case MVT::i64:
551 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
552 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000553 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000554 return X86::CMP64ri32;
555 return 0;
556 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000557}
558
Chris Lattner9a08a612008-10-15 04:26:38 +0000559bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, MVT VT) {
560 unsigned Op0Reg = getRegForValue(Op0);
561 if (Op0Reg == 0) return false;
562
Chris Lattnerd53886b2008-10-15 05:18:04 +0000563 // Handle 'null' like i32/i64 0.
564 if (isa<ConstantPointerNull>(Op1))
565 Op1 = Constant::getNullValue(TD.getIntPtrType());
566
Chris Lattner9a08a612008-10-15 04:26:38 +0000567 // We have two options: compare with register or immediate. If the RHS of
568 // the compare is an immediate that we can fold into this compare, use
569 // CMPri, otherwise use CMPrr.
570 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000571 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000572 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner9a08a612008-10-15 04:26:38 +0000573 .addImm(Op1C->getSExtValue());
574 return true;
575 }
576 }
577
578 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
579 if (CompareOpc == 0) return false;
580
581 unsigned Op1Reg = getRegForValue(Op1);
582 if (Op1Reg == 0) return false;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000583 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000584
585 return true;
586}
587
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000588bool X86FastISel::X86SelectCmp(Instruction *I) {
589 CmpInst *CI = cast<CmpInst>(I);
590
Dan Gohman9b66d732008-09-30 00:48:39 +0000591 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000592 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000593 return false;
594
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000595 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000596 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000597 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000598 switch (CI->getPredicate()) {
599 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000600 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
601 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000602
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000603 unsigned EReg = createResultReg(&X86::GR8RegClass);
604 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000605 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
606 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
607 BuildMI(MBB, DL,
608 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000609 UpdateValueMap(I, ResultReg);
610 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000611 }
612 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000613 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
614 return false;
615
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000616 unsigned NEReg = createResultReg(&X86::GR8RegClass);
617 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000618 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
619 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
620 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000621 UpdateValueMap(I, ResultReg);
622 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000623 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000624 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
625 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
626 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
627 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
628 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
629 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
630 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
631 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
632 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
633 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
634 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
635 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
636
637 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
638 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
639 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
640 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
641 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
642 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
643 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
644 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
645 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
646 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000647 default:
648 return false;
649 }
650
Chris Lattner9a08a612008-10-15 04:26:38 +0000651 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000652 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000653 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000654
Chris Lattner9a08a612008-10-15 04:26:38 +0000655 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000656 if (!X86FastEmitCompare(Op0, Op1, VT))
657 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000658
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000659 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000660 UpdateValueMap(I, ResultReg);
661 return true;
662}
Evan Cheng8b19e562008-09-03 06:44:39 +0000663
Dan Gohmand89ae992008-09-05 01:06:14 +0000664bool X86FastISel::X86SelectZExt(Instruction *I) {
665 // Special-case hack: The only i1 values we know how to produce currently
666 // set the upper bits of an i8 value to zero.
667 if (I->getType() == Type::Int8Ty &&
668 I->getOperand(0)->getType() == Type::Int1Ty) {
669 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000670 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000671 UpdateValueMap(I, ResultReg);
672 return true;
673 }
674
675 return false;
676}
677
Chris Lattner9a08a612008-10-15 04:26:38 +0000678
Dan Gohmand89ae992008-09-05 01:06:14 +0000679bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000680 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000681 // Handle a conditional branch.
682 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000683 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
684 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
685
Dan Gohmand98d6202008-10-02 22:15:21 +0000686 // Fold the common case of a conditional branch with a comparison.
687 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
688 if (CI->hasOneUse()) {
689 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000690
Dan Gohmand98d6202008-10-02 22:15:21 +0000691 // Try to take advantage of fallthrough opportunities.
692 CmpInst::Predicate Predicate = CI->getPredicate();
693 if (MBB->isLayoutSuccessor(TrueMBB)) {
694 std::swap(TrueMBB, FalseMBB);
695 Predicate = CmpInst::getInversePredicate(Predicate);
696 }
697
Chris Lattner871d2462008-10-15 03:58:05 +0000698 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
699 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
700
Dan Gohmand98d6202008-10-02 22:15:21 +0000701 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000702 case CmpInst::FCMP_OEQ:
703 std::swap(TrueMBB, FalseMBB);
704 Predicate = CmpInst::FCMP_UNE;
705 // FALL THROUGH
706 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE; break;
Chris Lattner871d2462008-10-15 03:58:05 +0000707 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
708 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
709 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
710 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
711 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
712 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
713 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
714 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
715 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
716 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
717 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
718 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000719
Chris Lattner871d2462008-10-15 03:58:05 +0000720 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
721 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
722 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
723 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
724 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
725 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
726 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
727 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
728 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
729 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000730 default:
731 return false;
732 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000733
Chris Lattner709d8292008-10-15 04:02:26 +0000734 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
735 if (SwapArgs)
736 std::swap(Op0, Op1);
737
Chris Lattner9a08a612008-10-15 04:26:38 +0000738 // Emit a compare of the LHS and RHS, setting the flags.
739 if (!X86FastEmitCompare(Op0, Op1, VT))
740 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000741
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000742 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000743
744 if (Predicate == CmpInst::FCMP_UNE) {
745 // X86 requires a second branch to handle UNE (and OEQ,
746 // which is mapped to UNE above).
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000747 BuildMI(MBB, DL, TII.get(X86::JP)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000748 }
749
Dan Gohmand98d6202008-10-02 22:15:21 +0000750 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000751 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000752 return true;
753 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000754 } else if (ExtractValueInst *EI =
755 dyn_cast<ExtractValueInst>(BI->getCondition())) {
756 // Check to see if the branch instruction is from an "arithmetic with
757 // overflow" intrinsic. The main way these intrinsics are used is:
758 //
759 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
760 // %sum = extractvalue { i32, i1 } %t, 0
761 // %obit = extractvalue { i32, i1 } %t, 1
762 // br i1 %obit, label %overflow, label %normal
763 //
Dan Gohman653456c2009-01-07 00:15:08 +0000764 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000765 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000766 // looking for the SETO/SETB instruction. If an instruction modifies the
767 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
768 // convert the branch into a JO/JB instruction.
Bill Wendling30a64a72008-12-09 23:19:12 +0000769
Bill Wendling9a901322008-12-10 19:44:24 +0000770 Value *Agg = EI->getAggregateOperand();
Bill Wendling30a64a72008-12-09 23:19:12 +0000771
Bill Wendling9a901322008-12-10 19:44:24 +0000772 if (CallInst *CI = dyn_cast<CallInst>(Agg)) {
773 Function *F = CI->getCalledFunction();
Bill Wendling30a64a72008-12-09 23:19:12 +0000774
Bill Wendling9a901322008-12-10 19:44:24 +0000775 if (F && F->isDeclaration()) {
776 switch (F->getIntrinsicID()) {
777 default: break;
778 case Intrinsic::sadd_with_overflow:
779 case Intrinsic::uadd_with_overflow: {
780 const MachineInstr *SetMI = 0;
781 unsigned Reg = lookUpRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000782
Bill Wendling9a901322008-12-10 19:44:24 +0000783 for (MachineBasicBlock::const_reverse_iterator
784 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
785 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000786
Bill Wendling9a901322008-12-10 19:44:24 +0000787 if (MI.modifiesRegister(Reg)) {
Evan Cheng04ee5a12009-01-20 19:12:24 +0000788 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000789
Evan Cheng04ee5a12009-01-20 19:12:24 +0000790 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
Bill Wendling9a901322008-12-10 19:44:24 +0000791 Reg = Src;
792 continue;
793 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000794
Bill Wendling9a901322008-12-10 19:44:24 +0000795 SetMI = &MI;
796 break;
797 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000798
Bill Wendling9a901322008-12-10 19:44:24 +0000799 const TargetInstrDesc &TID = MI.getDesc();
800 const unsigned *ImpDefs = TID.getImplicitDefs();
801
802 if (TID.hasUnmodeledSideEffects()) break;
803
804 bool ModifiesEFlags = false;
805
806 if (ImpDefs) {
807 for (unsigned u = 0; ImpDefs[u]; ++u)
808 if (ImpDefs[u] == X86::EFLAGS) {
809 ModifiesEFlags = true;
810 break;
811 }
812 }
813
814 if (ModifiesEFlags) break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000815 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000816
Bill Wendling9a901322008-12-10 19:44:24 +0000817 if (SetMI) {
818 unsigned OpCode = SetMI->getOpcode();
Bill Wendling30a64a72008-12-09 23:19:12 +0000819
Dan Gohman653456c2009-01-07 00:15:08 +0000820 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000821 BuildMI(MBB, DL, TII.get((OpCode == X86::SETOr) ?
Dan Gohman653456c2009-01-07 00:15:08 +0000822 X86::JO : X86::JB)).addMBB(TrueMBB);
Bill Wendling9a901322008-12-10 19:44:24 +0000823 FastEmitBranch(FalseMBB);
824 MBB->addSuccessor(TrueMBB);
825 return true;
826 }
827 }
828 }
829 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000830 }
831 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000832 }
833
834 // Otherwise do a clumsy setcc and re-test it.
835 unsigned OpReg = getRegForValue(BI->getCondition());
836 if (OpReg == 0) return false;
837
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000838 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
839 BuildMI(MBB, DL, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000840 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000841 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000842 return true;
843}
844
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000845bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000846 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000847 const TargetRegisterClass *RC = NULL;
848 if (I->getType() == Type::Int8Ty) {
849 CReg = X86::CL;
850 RC = &X86::GR8RegClass;
851 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000852 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
853 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
854 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000855 default: return false;
856 }
857 } else if (I->getType() == Type::Int16Ty) {
858 CReg = X86::CX;
859 RC = &X86::GR16RegClass;
860 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000861 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
862 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
863 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000864 default: return false;
865 }
866 } else if (I->getType() == Type::Int32Ty) {
867 CReg = X86::ECX;
868 RC = &X86::GR32RegClass;
869 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000870 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
871 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
872 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000873 default: return false;
874 }
875 } else if (I->getType() == Type::Int64Ty) {
876 CReg = X86::RCX;
877 RC = &X86::GR64RegClass;
878 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000879 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
880 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
881 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000882 default: return false;
883 }
884 } else {
885 return false;
886 }
887
Chris Lattner160f6cc2008-10-15 05:07:36 +0000888 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
889 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000890 return false;
891
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000892 unsigned Op0Reg = getRegForValue(I->getOperand(0));
893 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000894
895 // Fold immediate in shl(x,3).
896 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
897 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000898 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +0000899 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +0000900 UpdateValueMap(I, ResultReg);
901 return true;
902 }
903
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000904 unsigned Op1Reg = getRegForValue(I->getOperand(1));
905 if (Op1Reg == 0) return false;
906 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000907
908 // The shift instruction uses X86::CL. If we defined a super-register
909 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
910 // we're doing here.
911 if (CReg != X86::CL)
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000912 BuildMI(MBB, DL, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
Dan Gohman145b8282008-10-07 21:50:36 +0000913 .addReg(CReg).addImm(X86::SUBREG_8BIT);
914
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000915 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000916 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000917 UpdateValueMap(I, ResultReg);
918 return true;
919}
920
921bool X86FastISel::X86SelectSelect(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000922 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
923 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
924 return false;
925
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000926 unsigned Opc = 0;
927 const TargetRegisterClass *RC = NULL;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000928 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +0000929 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000930 RC = &X86::GR16RegClass;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000931 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +0000932 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000933 RC = &X86::GR32RegClass;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000934 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +0000935 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000936 RC = &X86::GR64RegClass;
937 } else {
938 return false;
939 }
940
941 unsigned Op0Reg = getRegForValue(I->getOperand(0));
942 if (Op0Reg == 0) return false;
943 unsigned Op1Reg = getRegForValue(I->getOperand(1));
944 if (Op1Reg == 0) return false;
945 unsigned Op2Reg = getRegForValue(I->getOperand(2));
946 if (Op2Reg == 0) return false;
947
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000948 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000949 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000950 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000951 UpdateValueMap(I, ResultReg);
952 return true;
953}
954
Dan Gohman78efce62008-09-10 21:02:08 +0000955bool X86FastISel::X86SelectFPExt(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000956 // fpext from float to double.
957 if (Subtarget->hasSSE2() && I->getType() == Type::DoubleTy) {
958 Value *V = I->getOperand(0);
959 if (V->getType() == Type::FloatTy) {
960 unsigned OpReg = getRegForValue(V);
961 if (OpReg == 0) return false;
962 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000963 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +0000964 UpdateValueMap(I, ResultReg);
965 return true;
Dan Gohman78efce62008-09-10 21:02:08 +0000966 }
967 }
968
969 return false;
970}
971
972bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
973 if (Subtarget->hasSSE2()) {
974 if (I->getType() == Type::FloatTy) {
975 Value *V = I->getOperand(0);
976 if (V->getType() == Type::DoubleTy) {
977 unsigned OpReg = getRegForValue(V);
978 if (OpReg == 0) return false;
979 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000980 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +0000981 UpdateValueMap(I, ResultReg);
982 return true;
983 }
984 }
985 }
986
987 return false;
988}
989
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000990bool X86FastISel::X86SelectTrunc(Instruction *I) {
991 if (Subtarget->is64Bit())
992 // All other cases should be handled by the tblgen generated code.
993 return false;
994 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
995 MVT DstVT = TLI.getValueType(I->getType());
996 if (DstVT != MVT::i8)
997 // All other cases should be handled by the tblgen generated code.
998 return false;
999 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
1000 // All other cases should be handled by the tblgen generated code.
1001 return false;
1002
1003 unsigned InputReg = getRegForValue(I->getOperand(0));
1004 if (!InputReg)
1005 // Unhandled operand. Halt "fast" selection and bail.
1006 return false;
1007
1008 // First issue a copy to GR16_ or GR32_.
1009 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
1010 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
1011 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
1012 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001013 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001014
1015 // Then issue an extract_subreg.
Evan Cheng536ab132009-01-22 09:10:11 +00001016 unsigned ResultReg = FastEmitInst_extractsubreg(DstVT.getSimpleVT(),
1017 CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001018 if (!ResultReg)
1019 return false;
1020
1021 UpdateValueMap(I, ResultReg);
1022 return true;
1023}
1024
Bill Wendling52370a12008-12-09 02:42:50 +00001025bool X86FastISel::X86SelectExtractValue(Instruction *I) {
1026 ExtractValueInst *EI = cast<ExtractValueInst>(I);
1027 Value *Agg = EI->getAggregateOperand();
1028
1029 if (CallInst *CI = dyn_cast<CallInst>(Agg)) {
1030 Function *F = CI->getCalledFunction();
1031
1032 if (F && F->isDeclaration()) {
1033 switch (F->getIntrinsicID()) {
1034 default: break;
1035 case Intrinsic::sadd_with_overflow:
1036 case Intrinsic::uadd_with_overflow:
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001037 // Cheat a little. We know that the registers for "add" and "seto" are
1038 // allocated sequentially. However, we only keep track of the register
1039 // for "add" in the value map. Use extractvalue's index to get the
1040 // correct register for "seto".
Bill Wendling52370a12008-12-09 02:42:50 +00001041 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1042 return true;
1043 }
1044 }
1045 }
1046
1047 return false;
1048}
1049
1050bool X86FastISel::X86VisitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
1051 // FIXME: Handle more intrinsics.
1052 switch (Intrinsic) {
1053 default: return false;
1054 case Intrinsic::sadd_with_overflow:
1055 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001056 // Replace "add with overflow" intrinsics with an "add" instruction followed
1057 // by a seto/setc instruction. Later on, when the "extractvalue"
1058 // instructions are encountered, we use the fact that two registers were
1059 // created sequentially to get the correct registers for the "sum" and the
1060 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001061 MVT VT;
1062 const Function *Callee = I.getCalledFunction();
1063 const Type *RetTy =
1064 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1065
1066 if (!isTypeLegal(RetTy, VT))
1067 return false;
1068
1069 Value *Op1 = I.getOperand(1);
1070 Value *Op2 = I.getOperand(2);
1071 unsigned Reg1 = getRegForValue(Op1);
1072 unsigned Reg2 = getRegForValue(Op2);
1073
1074 if (Reg1 == 0 || Reg2 == 0)
1075 // FIXME: Handle values *not* in registers.
1076 return false;
1077
1078 unsigned OpC = 0;
1079
1080 if (VT == MVT::i32)
1081 OpC = X86::ADD32rr;
1082 else if (VT == MVT::i64)
1083 OpC = X86::ADD64rr;
1084 else
1085 return false;
1086
1087 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001088 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Bill Wendling52370a12008-12-09 02:42:50 +00001089 UpdateValueMap(&I, ResultReg);
1090
1091 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001092 BuildMI(MBB, DL, TII.get((Intrinsic == Intrinsic::sadd_with_overflow) ?
Dan Gohman653456c2009-01-07 00:15:08 +00001093 X86::SETOr : X86::SETBr), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001094 return true;
1095 }
1096 }
1097}
1098
Evan Chengf3d4efe2008-09-07 09:09:33 +00001099bool X86FastISel::X86SelectCall(Instruction *I) {
1100 CallInst *CI = cast<CallInst>(I);
1101 Value *Callee = I->getOperand(0);
1102
1103 // Can't handle inline asm yet.
1104 if (isa<InlineAsm>(Callee))
1105 return false;
1106
Bill Wendling52370a12008-12-09 02:42:50 +00001107 // Handle intrinsic calls.
1108 if (Function *F = CI->getCalledFunction())
1109 if (F->isDeclaration())
1110 if (unsigned IID = F->getIntrinsicID())
1111 return X86VisitIntrinsicCall(*CI, IID);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001112
Evan Chengf3d4efe2008-09-07 09:09:33 +00001113 // Handle only C and fastcc calling conventions for now.
1114 CallSite CS(CI);
1115 unsigned CC = CS.getCallingConv();
1116 if (CC != CallingConv::C &&
1117 CC != CallingConv::Fast &&
1118 CC != CallingConv::X86_FastCall)
1119 return false;
1120
1121 // Let SDISel handle vararg functions.
1122 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1123 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1124 if (FTy->isVarArg())
1125 return false;
1126
1127 // Handle *simple* calls for now.
1128 const Type *RetTy = CS.getType();
1129 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001130 if (RetTy == Type::VoidTy)
1131 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001132 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001133 return false;
1134
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001135 // Materialize callee address in a register. FIXME: GV address can be
1136 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001137 X86AddressMode CalleeAM;
1138 if (!X86SelectAddress(Callee, CalleeAM, true))
1139 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001140 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001141 GlobalValue *GV = 0;
1142 if (CalleeAM.Base.Reg != 0) {
1143 assert(CalleeAM.GV == 0);
1144 CalleeOp = CalleeAM.Base.Reg;
1145 } else if (CalleeAM.GV != 0) {
1146 assert(CalleeAM.GV != 0);
1147 GV = CalleeAM.GV;
1148 } else
1149 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001150
Evan Chengdebdea02008-09-08 17:15:42 +00001151 // Allow calls which produce i1 results.
1152 bool AndToI1 = false;
1153 if (RetVT == MVT::i1) {
1154 RetVT = MVT::i8;
1155 AndToI1 = true;
1156 }
1157
Evan Chengf3d4efe2008-09-07 09:09:33 +00001158 // Deal with call operands first.
Chris Lattner241ab472008-10-15 05:38:32 +00001159 SmallVector<Value*, 8> ArgVals;
1160 SmallVector<unsigned, 8> Args;
1161 SmallVector<MVT, 8> ArgVTs;
1162 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001163 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001164 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001165 ArgVTs.reserve(CS.arg_size());
1166 ArgFlags.reserve(CS.arg_size());
1167 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1168 i != e; ++i) {
1169 unsigned Arg = getRegForValue(*i);
1170 if (Arg == 0)
1171 return false;
1172 ISD::ArgFlagsTy Flags;
1173 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001174 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001175 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001176 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001177 Flags.setZExt();
1178
1179 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001180 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1181 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1182 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1183 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001184 return false;
1185
1186 const Type *ArgTy = (*i)->getType();
1187 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001188 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001189 return false;
1190 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1191 Flags.setOrigAlign(OriginalAlignment);
1192
1193 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001194 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001195 ArgVTs.push_back(ArgVT);
1196 ArgFlags.push_back(Flags);
1197 }
1198
1199 // Analyze operands of the call, assigning locations to each operand.
1200 SmallVector<CCValAssign, 16> ArgLocs;
1201 CCState CCInfo(CC, false, TM, ArgLocs);
1202 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1203
1204 // Get a count of how many bytes are to be pushed on the stack.
1205 unsigned NumBytes = CCInfo.getNextStackOffset();
1206
1207 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001208 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001209 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001210
Chris Lattner438949a2008-10-15 05:30:52 +00001211 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001212 // copies / loads.
1213 SmallVector<unsigned, 4> RegArgs;
1214 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1215 CCValAssign &VA = ArgLocs[i];
1216 unsigned Arg = Args[VA.getValNo()];
1217 MVT ArgVT = ArgVTs[VA.getValNo()];
1218
1219 // Promote the value if needed.
1220 switch (VA.getLocInfo()) {
1221 default: assert(0 && "Unknown loc info!");
1222 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001223 case CCValAssign::SExt: {
1224 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1225 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001226 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001227 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001228 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001229 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001230 }
1231 case CCValAssign::ZExt: {
1232 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1233 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001234 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001235 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001236 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001237 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001238 }
1239 case CCValAssign::AExt: {
1240 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1241 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001242 if (!Emitted)
1243 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001244 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001245 if (!Emitted)
1246 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1247 Arg, ArgVT, Arg);
1248
Chris Lattnera33649e2008-12-19 17:03:38 +00001249 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001250 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001251 break;
1252 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001253 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001254
1255 if (VA.isRegLoc()) {
1256 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1257 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1258 Arg, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001259 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001260 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001261 RegArgs.push_back(VA.getLocReg());
1262 } else {
1263 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001264 X86AddressMode AM;
1265 AM.Base.Reg = StackPtr;
1266 AM.Disp = LocMemOffset;
Chris Lattner241ab472008-10-15 05:38:32 +00001267 Value *ArgVal = ArgVals[VA.getValNo()];
1268
1269 // If this is a really simple value, emit this with the Value* version of
1270 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1271 // can cause us to reevaluate the argument.
1272 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1273 X86FastEmitStore(ArgVT, ArgVal, AM);
1274 else
1275 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001276 }
1277 }
1278
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001279 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1280 // GOT pointer.
1281 if (!Subtarget->is64Bit() &&
1282 TM.getRelocationModel() == Reloc::PIC_ &&
1283 Subtarget->isPICStyleGOT()) {
1284 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001285 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001286 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001287 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001288 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001289 }
1290
Evan Chengf3d4efe2008-09-07 09:09:33 +00001291 // Issue the call.
1292 unsigned CallOpc = CalleeOp
1293 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1294 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1295 MachineInstrBuilder MIB = CalleeOp
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001296 ? BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp)
1297 : BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001298
1299 // Add an implicit use GOT pointer in EBX.
1300 if (!Subtarget->is64Bit() &&
1301 TM.getRelocationModel() == Reloc::PIC_ &&
1302 Subtarget->isPICStyleGOT())
1303 MIB.addReg(X86::EBX);
1304
Evan Chengf3d4efe2008-09-07 09:09:33 +00001305 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001306 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1307 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001308
1309 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001310 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001311 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001312
1313 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001314 if (RetVT.getSimpleVT() != MVT::isVoid) {
1315 SmallVector<CCValAssign, 16> RVLocs;
1316 CCState CCInfo(CC, false, TM, RVLocs);
1317 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1318
1319 // Copy all of the result registers out of their specified physreg.
1320 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1321 MVT CopyVT = RVLocs[0].getValVT();
1322 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1323 TargetRegisterClass *SrcRC = DstRC;
1324
1325 // If this is a call to a function that returns an fp value on the x87 fp
1326 // stack, but where we prefer to use the value in xmm registers, copy it
1327 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1328 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1329 RVLocs[0].getLocReg() == X86::ST1) &&
1330 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1331 CopyVT = MVT::f80;
1332 SrcRC = X86::RSTRegisterClass;
1333 DstRC = X86::RFP80RegisterClass;
1334 }
1335
1336 unsigned ResultReg = createResultReg(DstRC);
1337 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1338 RVLocs[0].getLocReg(), DstRC, SrcRC);
Chris Lattnera33649e2008-12-19 17:03:38 +00001339 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001340 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001341 if (CopyVT != RVLocs[0].getValVT()) {
1342 // Round the F80 the right size, which also moves to the appropriate xmm
1343 // register. This is accomplished by storing the F80 value in memory and
1344 // then loading it back. Ewww...
1345 MVT ResVT = RVLocs[0].getValVT();
1346 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1347 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001348 int FI = MFI.CreateStackObject(MemSize, MemSize);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001349 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001350 DstRC = ResVT == MVT::f32
1351 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1352 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1353 ResultReg = createResultReg(DstRC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001354 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001355 }
1356
Evan Chengdebdea02008-09-08 17:15:42 +00001357 if (AndToI1) {
1358 // Mask out all but lowest bit for some call which produces an i1.
1359 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001360 BuildMI(MBB, DL,
1361 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001362 ResultReg = AndResult;
1363 }
1364
Evan Chengf3d4efe2008-09-07 09:09:33 +00001365 UpdateValueMap(I, ResultReg);
1366 }
1367
1368 return true;
1369}
1370
1371
Dan Gohman99b21822008-08-28 23:21:34 +00001372bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001373X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001374 switch (I->getOpcode()) {
1375 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001376 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001377 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001378 case Instruction::Store:
1379 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001380 case Instruction::ICmp:
1381 case Instruction::FCmp:
1382 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001383 case Instruction::ZExt:
1384 return X86SelectZExt(I);
1385 case Instruction::Br:
1386 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001387 case Instruction::Call:
1388 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001389 case Instruction::LShr:
1390 case Instruction::AShr:
1391 case Instruction::Shl:
1392 return X86SelectShift(I);
1393 case Instruction::Select:
1394 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001395 case Instruction::Trunc:
1396 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001397 case Instruction::FPExt:
1398 return X86SelectFPExt(I);
1399 case Instruction::FPTrunc:
1400 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001401 case Instruction::ExtractValue:
1402 return X86SelectExtractValue(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001403 }
1404
1405 return false;
1406}
1407
Dan Gohman0586d912008-09-10 20:11:02 +00001408unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001409 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001410 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001411 return false;
1412
1413 // Get opcode and regclass of the output for the given load instruction.
1414 unsigned Opc = 0;
1415 const TargetRegisterClass *RC = NULL;
1416 switch (VT.getSimpleVT()) {
1417 default: return false;
1418 case MVT::i8:
1419 Opc = X86::MOV8rm;
1420 RC = X86::GR8RegisterClass;
1421 break;
1422 case MVT::i16:
1423 Opc = X86::MOV16rm;
1424 RC = X86::GR16RegisterClass;
1425 break;
1426 case MVT::i32:
1427 Opc = X86::MOV32rm;
1428 RC = X86::GR32RegisterClass;
1429 break;
1430 case MVT::i64:
1431 // Must be in x86-64 mode.
1432 Opc = X86::MOV64rm;
1433 RC = X86::GR64RegisterClass;
1434 break;
1435 case MVT::f32:
1436 if (Subtarget->hasSSE1()) {
1437 Opc = X86::MOVSSrm;
1438 RC = X86::FR32RegisterClass;
1439 } else {
1440 Opc = X86::LD_Fp32m;
1441 RC = X86::RFP32RegisterClass;
1442 }
1443 break;
1444 case MVT::f64:
1445 if (Subtarget->hasSSE2()) {
1446 Opc = X86::MOVSDrm;
1447 RC = X86::FR64RegisterClass;
1448 } else {
1449 Opc = X86::LD_Fp64m;
1450 RC = X86::RFP64RegisterClass;
1451 }
1452 break;
1453 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001454 // No f80 support yet.
1455 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001456 }
1457
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001458 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001459 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001460 X86AddressMode AM;
1461 if (X86SelectAddress(C, AM, false)) {
1462 if (TLI.getPointerTy() == MVT::i32)
1463 Opc = X86::LEA32r;
1464 else
1465 Opc = X86::LEA64r;
1466 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001467 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001468 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001469 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001470 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001471 }
1472
Owen Anderson3b217c62008-09-06 01:11:01 +00001473 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001474 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001475 if (Align == 0) {
1476 // Alignment of vector types. FIXME!
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00001477 Align = TD.getTypePaddedSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001478 Align = Log2_64(Align);
1479 }
Owen Anderson95267a12008-09-05 00:06:23 +00001480
Dan Gohman5396c992008-09-30 01:21:32 +00001481 // x86-32 PIC requires a PIC base register for constant pools.
1482 unsigned PICBase = 0;
1483 if (TM.getRelocationModel() == Reloc::PIC_ &&
1484 !Subtarget->is64Bit())
1485 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1486
1487 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001488 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001489 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001490 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), MCPOffset,
Dan Gohman5396c992008-09-30 01:21:32 +00001491 PICBase);
1492
Owen Anderson95267a12008-09-05 00:06:23 +00001493 return ResultReg;
1494}
1495
Dan Gohman0586d912008-09-10 20:11:02 +00001496unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001497 // Fail on dynamic allocas. At this point, getRegForValue has already
1498 // checked its CSE maps, so if we're here trying to handle a dynamic
1499 // alloca, we're not going to succeed. X86SelectAddress has a
1500 // check for dynamic allocas, because it's called directly from
1501 // various places, but TargetMaterializeAlloca also needs a check
1502 // in order to avoid recursion between getRegForValue,
1503 // X86SelectAddrss, and TargetMaterializeAlloca.
1504 if (!StaticAllocaMap.count(C))
1505 return 0;
1506
Dan Gohman0586d912008-09-10 20:11:02 +00001507 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001508 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001509 return 0;
1510 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1511 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1512 unsigned ResultReg = createResultReg(RC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001513 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001514 return ResultReg;
1515}
1516
Evan Chengc3f44b02008-09-03 00:03:49 +00001517namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001518 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001519 MachineModuleInfo *mmi,
Devang Patel83489bb2009-01-13 00:35:13 +00001520 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001521 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001522 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001523 DenseMap<const AllocaInst *, int> &am
1524#ifndef NDEBUG
1525 , SmallSet<Instruction*, 8> &cil
1526#endif
1527 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001528 return new X86FastISel(mf, mmi, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001529#ifndef NDEBUG
1530 , cil
1531#endif
1532 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001533 }
Dan Gohman99b21822008-08-28 23:21:34 +00001534}