blob: 1bec8e879209078fb682daf5c0be28f65b7b96ff [file] [log] [blame]
Dan Gohmanbd6a0332008-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 Cheng8700bb92008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Dan Gohmanbd6a0332008-08-19 21:45:35 +000018#include "X86ISelLowering.h"
Evan Cheng651677b2008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohmane97f1a32008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Evan Cheng8016fb82008-09-07 09:09:33 +000022#include "llvm/CallingConv.h"
Dan Gohmane1cdaa62008-09-04 23:26:51 +000023#include "llvm/DerivedTypes.h"
Dan Gohman55f84b82009-02-23 22:03:08 +000024#include "llvm/GlobalVariable.h"
Evan Cheng8016fb82008-09-07 09:09:33 +000025#include "llvm/Instructions.h"
Chris Lattnera6a19bd2009-04-12 07:36:01 +000026#include "llvm/IntrinsicInst.h"
Evan Cheng5a0f5912008-09-03 00:03:49 +000027#include "llvm/CodeGen/FastISel.h"
Owen Anderson51f958e2008-09-05 00:06:23 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng8016fb82008-09-07 09:09:33 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson4ec36c32008-08-29 17:45:56 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng8016fb82008-09-07 09:09:33 +000031#include "llvm/Support/CallSite.h"
Edwin Török675d5622009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Dan Gohman3d9f55f2008-09-18 23:23:44 +000033#include "llvm/Support/GetElementPtrTypeIterator.h"
Dan Gohman477b0582009-05-04 19:50:33 +000034#include "llvm/Target/TargetOptions.h"
Evan Cheng5a0f5912008-09-03 00:03:49 +000035using namespace llvm;
36
Chris Lattnerffa5fc62009-03-08 18:44:31 +000037namespace {
38
Evan Cheng5a0f5912008-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 Cheng8016fb82008-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 Cheng8700bb92008-09-03 06:44:39 +000055public:
Dan Gohmanca4857a2008-09-03 23:12:08 +000056 explicit X86FastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +000057 MachineModuleInfo *mmi,
Devang Patelfcf1c752009-01-13 00:35:13 +000058 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +000059 DenseMap<const Value *, unsigned> &vm,
Dan Gohmand6211a72008-09-10 20:11:02 +000060 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +000061 DenseMap<const AllocaInst *, int> &am
62#ifndef NDEBUG
63 , SmallSet<Instruction*, 8> &cil
64#endif
65 )
Devang Patelfcf1c752009-01-13 00:35:13 +000066 : FastISel(mf, mmi, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +000067#ifndef NDEBUG
68 , cil
69#endif
70 ) {
Evan Cheng651677b2008-09-03 01:04:47 +000071 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Cheng8016fb82008-09-07 09:09:33 +000072 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
73 X86ScalarSSEf64 = Subtarget->hasSSE2();
74 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng651677b2008-09-03 01:04:47 +000075 }
Evan Cheng5a0f5912008-09-03 00:03:49 +000076
Dan Gohmanca4857a2008-09-03 23:12:08 +000077 virtual bool TargetSelectInstruction(Instruction *I);
Evan Cheng5a0f5912008-09-03 00:03:49 +000078
Dan Gohmanbd6a0332008-08-19 21:45:35 +000079#include "X86GenFastISel.inc"
Evan Cheng8700bb92008-09-03 06:44:39 +000080
81private:
Owen Andersonac9de032009-08-10 22:56:29 +000082 bool X86FastEmitCompare(Value *LHS, Value *RHS, EVT VT);
Chris Lattner1c921862008-10-15 04:26:38 +000083
Owen Andersonac9de032009-08-10 22:56:29 +000084 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Chengcb5422c2008-09-05 21:00:03 +000085
Owen Andersonac9de032009-08-10 22:56:29 +000086 bool X86FastEmitStore(EVT VT, Value *Val,
Chris Lattner65839062008-10-15 05:30:52 +000087 const X86AddressMode &AM);
Owen Andersonac9de032009-08-10 22:56:29 +000088 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohmand6211a72008-09-10 20:11:02 +000089 const X86AddressMode &AM);
Evan Cheng2a719f82008-09-08 06:35:17 +000090
Owen Andersonac9de032009-08-10 22:56:29 +000091 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng2a719f82008-09-08 06:35:17 +000092 unsigned &ResultReg);
Evan Chengcb5422c2008-09-05 21:00:03 +000093
Chris Lattner4ba2d032009-07-10 05:33:42 +000094 bool X86SelectAddress(Value *V, X86AddressMode &AM);
95 bool X86SelectCallAddress(Value *V, X86AddressMode &AM);
Dan Gohmand6211a72008-09-10 20:11:02 +000096
Dan Gohmanca4857a2008-09-03 23:12:08 +000097 bool X86SelectLoad(Instruction *I);
Owen Andersonf4e3ec82008-09-04 07:08:58 +000098
99 bool X86SelectStore(Instruction *I);
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000100
101 bool X86SelectCmp(Instruction *I);
Dan Gohman4a7d4d62008-09-05 01:06:14 +0000102
103 bool X86SelectZExt(Instruction *I);
104
105 bool X86SelectBranch(Instruction *I);
Dan Gohman9ba59f82008-09-05 18:30:08 +0000106
Evan Chengb10ba152010-01-11 22:59:27 +0000107 bool X86SelectOR(Instruction *I);
108
Dan Gohman9ba59f82008-09-05 18:30:08 +0000109 bool X86SelectShift(Instruction *I);
110
111 bool X86SelectSelect(Instruction *I);
Evan Chengcb5422c2008-09-05 21:00:03 +0000112
Evan Cheng303530d2008-09-07 08:47:42 +0000113 bool X86SelectTrunc(Instruction *I);
Dan Gohman8766d8e2008-10-02 22:15:21 +0000114
Dan Gohman658637e2008-09-10 21:02:08 +0000115 bool X86SelectFPExt(Instruction *I);
116 bool X86SelectFPTrunc(Instruction *I);
117
Bill Wendling33fe51e2008-12-09 02:42:50 +0000118 bool X86SelectExtractValue(Instruction *I);
119
Chris Lattnera6a19bd2009-04-12 07:36:01 +0000120 bool X86VisitIntrinsicCall(IntrinsicInst &I);
Evan Cheng8016fb82008-09-07 09:09:33 +0000121 bool X86SelectCall(Instruction *I);
122
Sandeep Patel5838baa2009-09-02 08:44:58 +0000123 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Evan Cheng8016fb82008-09-07 09:09:33 +0000124
Dan Gohman9039d6b2008-09-25 15:24:26 +0000125 const X86InstrInfo *getInstrInfo() const {
Dan Gohmanc6413362008-09-26 19:15:30 +0000126 return getTargetMachine()->getInstrInfo();
127 }
128 const X86TargetMachine *getTargetMachine() const {
129 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman9039d6b2008-09-25 15:24:26 +0000130 }
131
Dan Gohmand6211a72008-09-10 20:11:02 +0000132 unsigned TargetMaterializeConstant(Constant *C);
133
134 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Cheng8016fb82008-09-07 09:09:33 +0000135
136 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
137 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersonac9de032009-08-10 22:56:29 +0000138 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000139 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
140 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng8016fb82008-09-07 09:09:33 +0000141 }
142
Owen Andersonac9de032009-08-10 22:56:29 +0000143 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Cheng5a0f5912008-09-03 00:03:49 +0000144};
Chris Lattnerffa5fc62009-03-08 18:44:31 +0000145
146} // end anonymous namespace.
Dan Gohmancb9b4d32008-08-28 23:21:34 +0000147
Owen Andersonac9de032009-08-10 22:56:29 +0000148bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattnerffba2be2008-10-15 05:07:36 +0000149 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000150 if (VT == MVT::Other || !VT.isSimple())
Evan Cheng8016fb82008-09-07 09:09:33 +0000151 // Unhandled type. Halt "fast" selection and bail.
152 return false;
Chris Lattnerffba2be2008-10-15 05:07:36 +0000153
Dan Gohman8f3e7d92008-09-30 00:48:39 +0000154 // For now, require SSE/SSE2 for performing floating-point operations,
155 // since x87 requires additional work.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000156 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman8f3e7d92008-09-30 00:48:39 +0000157 return false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000158 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman8f3e7d92008-09-30 00:48:39 +0000159 return false;
160 // Similarly, no f80 support yet.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000161 if (VT == MVT::f80)
Dan Gohman8f3e7d92008-09-30 00:48:39 +0000162 return false;
Evan Cheng8016fb82008-09-07 09:09:33 +0000163 // We only handle legal types. For example, on x86-32 the instruction
164 // selector contains all of the 64-bit instructions from x86-64,
165 // under the assumption that i64 won't be used if the target doesn't
166 // support it.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000167 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng8016fb82008-09-07 09:09:33 +0000168}
169
170#include "X86GenCallingConv.inc"
171
172/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
173/// convention.
Sandeep Patel5838baa2009-09-02 08:44:58 +0000174CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
175 bool isTaillCall) {
Evan Cheng8016fb82008-09-07 09:09:33 +0000176 if (Subtarget->is64Bit()) {
177 if (Subtarget->isTargetWin64())
178 return CC_X86_Win64_C;
Evan Cheng8016fb82008-09-07 09:09:33 +0000179 else
180 return CC_X86_64_C;
181 }
182
183 if (CC == CallingConv::X86_FastCall)
184 return CC_X86_32_FastCall;
Evan Cheng8016fb82008-09-07 09:09:33 +0000185 else if (CC == CallingConv::Fast)
186 return CC_X86_32_FastCC;
187 else
188 return CC_X86_32_C;
189}
190
Evan Chengcb5422c2008-09-05 21:00:03 +0000191/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng8016fb82008-09-07 09:09:33 +0000192/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengcb5422c2008-09-05 21:00:03 +0000193/// Return true and the result register by reference if it is possible.
Owen Andersonac9de032009-08-10 22:56:29 +0000194bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Chengcb5422c2008-09-05 21:00:03 +0000195 unsigned &ResultReg) {
196 // Get opcode and regclass of the output for the given load instruction.
197 unsigned Opc = 0;
198 const TargetRegisterClass *RC = NULL;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000199 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengcb5422c2008-09-05 21:00:03 +0000200 default: return false;
Dan Gohmanb59f15a2009-08-27 00:31:47 +0000201 case MVT::i1:
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000202 case MVT::i8:
Evan Chengcb5422c2008-09-05 21:00:03 +0000203 Opc = X86::MOV8rm;
204 RC = X86::GR8RegisterClass;
205 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000206 case MVT::i16:
Evan Chengcb5422c2008-09-05 21:00:03 +0000207 Opc = X86::MOV16rm;
208 RC = X86::GR16RegisterClass;
209 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000210 case MVT::i32:
Evan Chengcb5422c2008-09-05 21:00:03 +0000211 Opc = X86::MOV32rm;
212 RC = X86::GR32RegisterClass;
213 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000214 case MVT::i64:
Evan Chengcb5422c2008-09-05 21:00:03 +0000215 // Must be in x86-64 mode.
216 Opc = X86::MOV64rm;
217 RC = X86::GR64RegisterClass;
218 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000219 case MVT::f32:
Evan Chengcb5422c2008-09-05 21:00:03 +0000220 if (Subtarget->hasSSE1()) {
221 Opc = X86::MOVSSrm;
222 RC = X86::FR32RegisterClass;
223 } else {
224 Opc = X86::LD_Fp32m;
225 RC = X86::RFP32RegisterClass;
226 }
227 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000228 case MVT::f64:
Evan Chengcb5422c2008-09-05 21:00:03 +0000229 if (Subtarget->hasSSE2()) {
230 Opc = X86::MOVSDrm;
231 RC = X86::FR64RegisterClass;
232 } else {
233 Opc = X86::LD_Fp64m;
234 RC = X86::RFP64RegisterClass;
235 }
236 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000237 case MVT::f80:
Dan Gohman17a47142008-09-26 01:39:32 +0000238 // No f80 support yet.
239 return false;
Evan Chengcb5422c2008-09-05 21:00:03 +0000240 }
241
242 ResultReg = createResultReg(RC);
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000243 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Chengcb5422c2008-09-05 21:00:03 +0000244 return true;
245}
246
Evan Cheng8016fb82008-09-07 09:09:33 +0000247/// X86FastEmitStore - Emit a machine instruction to store a value Val of
248/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
249/// and a displacement offset, or a GlobalAddress,
Evan Chengcb5422c2008-09-05 21:00:03 +0000250/// i.e. V. Return true if it is possible.
251bool
Owen Andersonac9de032009-08-10 22:56:29 +0000252X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohmand6211a72008-09-10 20:11:02 +0000253 const X86AddressMode &AM) {
Dan Gohman630b0dd2008-09-08 16:31:35 +0000254 // Get opcode and regclass of the output for the given store instruction.
Evan Chengcb5422c2008-09-05 21:00:03 +0000255 unsigned Opc = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000256 switch (VT.getSimpleVT().SimpleTy) {
257 case MVT::f80: // No f80 support yet.
Evan Chengcb5422c2008-09-05 21:00:03 +0000258 default: return false;
Dan Gohmanb59f15a2009-08-27 00:31:47 +0000259 case MVT::i1: {
260 // Mask out all but lowest bit.
261 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
262 BuildMI(MBB, DL,
263 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
264 Val = AndResult;
265 }
266 // FALLTHROUGH, handling i1 as i8.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000267 case MVT::i8: Opc = X86::MOV8mr; break;
268 case MVT::i16: Opc = X86::MOV16mr; break;
269 case MVT::i32: Opc = X86::MOV32mr; break;
270 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
271 case MVT::f32:
Chris Lattner65839062008-10-15 05:30:52 +0000272 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Chengcb5422c2008-09-05 21:00:03 +0000273 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000274 case MVT::f64:
Chris Lattner65839062008-10-15 05:30:52 +0000275 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Chengcb5422c2008-09-05 21:00:03 +0000276 break;
Evan Chengcb5422c2008-09-05 21:00:03 +0000277 }
Chris Lattner65839062008-10-15 05:30:52 +0000278
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000279 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Chengcb5422c2008-09-05 21:00:03 +0000280 return true;
281}
282
Owen Andersonac9de032009-08-10 22:56:29 +0000283bool X86FastISel::X86FastEmitStore(EVT VT, Value *Val,
Chris Lattner65839062008-10-15 05:30:52 +0000284 const X86AddressMode &AM) {
285 // Handle 'null' like i32/i64 0.
286 if (isa<ConstantPointerNull>(Val))
Owen Anderson35b47072009-08-13 21:58:54 +0000287 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner65839062008-10-15 05:30:52 +0000288
289 // If this is a store of a simple constant, fold the constant into the store.
290 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
291 unsigned Opc = 0;
Dan Gohmanb59f15a2009-08-27 00:31:47 +0000292 bool Signed = true;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000293 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner65839062008-10-15 05:30:52 +0000294 default: break;
Dan Gohmanb59f15a2009-08-27 00:31:47 +0000295 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000296 case MVT::i8: Opc = X86::MOV8mi; break;
297 case MVT::i16: Opc = X86::MOV16mi; break;
298 case MVT::i32: Opc = X86::MOV32mi; break;
299 case MVT::i64:
Chris Lattner65839062008-10-15 05:30:52 +0000300 // Must be a 32-bit sign extended value.
301 if ((int)CI->getSExtValue() == CI->getSExtValue())
302 Opc = X86::MOV64mi32;
303 break;
304 }
305
306 if (Opc) {
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000307 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
Dan Gohmanb59f15a2009-08-27 00:31:47 +0000308 .addImm(Signed ? CI->getSExtValue() :
309 CI->getZExtValue());
Chris Lattner65839062008-10-15 05:30:52 +0000310 return true;
311 }
312 }
313
314 unsigned ValReg = getRegForValue(Val);
315 if (ValReg == 0)
Chris Lattner65839062008-10-15 05:30:52 +0000316 return false;
317
318 return X86FastEmitStore(VT, ValReg, AM);
319}
320
Evan Cheng2a719f82008-09-08 06:35:17 +0000321/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
322/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
323/// ISD::SIGN_EXTEND).
Owen Andersonac9de032009-08-10 22:56:29 +0000324bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
325 unsigned Src, EVT SrcVT,
Evan Cheng2a719f82008-09-08 06:35:17 +0000326 unsigned &ResultReg) {
Owen Anderson9b24e3f2008-09-11 19:44:55 +0000327 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
328
329 if (RR != 0) {
330 ResultReg = RR;
331 return true;
332 } else
333 return false;
Evan Cheng2a719f82008-09-08 06:35:17 +0000334}
335
Dan Gohmand6211a72008-09-10 20:11:02 +0000336/// X86SelectAddress - Attempt to fill in an address from the given value.
337///
Chris Lattner4ba2d032009-07-10 05:33:42 +0000338bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM) {
Duncan Sands12ea2a82009-06-03 12:05:18 +0000339 User *U = NULL;
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000340 unsigned Opcode = Instruction::UserOp1;
341 if (Instruction *I = dyn_cast<Instruction>(V)) {
342 Opcode = I->getOpcode();
343 U = I;
344 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
345 Opcode = C->getOpcode();
346 U = C;
347 }
Dan Gohmand6211a72008-09-10 20:11:02 +0000348
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000349 switch (Opcode) {
350 default: break;
351 case Instruction::BitCast:
352 // Look past bitcasts.
Chris Lattner4ba2d032009-07-10 05:33:42 +0000353 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000354
355 case Instruction::IntToPtr:
356 // Look past no-op inttoptrs.
357 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner4ba2d032009-07-10 05:33:42 +0000358 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman677db972008-12-08 23:50:06 +0000359 break;
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000360
361 case Instruction::PtrToInt:
362 // Look past no-op ptrtoints.
363 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner4ba2d032009-07-10 05:33:42 +0000364 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman677db972008-12-08 23:50:06 +0000365 break;
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000366
367 case Instruction::Alloca: {
368 // Do static allocas.
369 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmand6211a72008-09-10 20:11:02 +0000370 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohmanc6413362008-09-26 19:15:30 +0000371 if (SI != StaticAllocaMap.end()) {
372 AM.BaseType = X86AddressMode::FrameIndexBase;
373 AM.Base.FrameIndex = SI->second;
374 return true;
375 }
376 break;
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000377 }
378
379 case Instruction::Add: {
380 // Adds of constants are common and easy enough.
381 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohmanf9d724c2008-09-26 20:04:15 +0000382 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
383 // They have to fit in the 32-bit signed displacement field though.
384 if (isInt32(Disp)) {
385 AM.Disp = (uint32_t)Disp;
Chris Lattner4ba2d032009-07-10 05:33:42 +0000386 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanf9d724c2008-09-26 20:04:15 +0000387 }
Dan Gohmand6211a72008-09-10 20:11:02 +0000388 }
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000389 break;
390 }
391
392 case Instruction::GetElementPtr: {
393 // Pattern-match simple GEPs.
Dan Gohmanf9d724c2008-09-26 20:04:15 +0000394 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000395 unsigned IndexReg = AM.IndexReg;
396 unsigned Scale = AM.Scale;
397 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman009a81f2008-12-08 07:57:47 +0000398 // Iterate through the indices, folding what we can. Constants can be
399 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000400 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
401 i != e; ++i, ++GTI) {
402 Value *Op = *i;
403 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
404 const StructLayout *SL = TD.getStructLayout(STy);
405 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
406 Disp += SL->getElementOffset(Idx);
407 } else {
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000408 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000409 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
410 // Constant-offset addressing.
Dan Gohmanf9d724c2008-09-26 20:04:15 +0000411 Disp += CI->getSExtValue() * S;
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000412 } else if (IndexReg == 0 &&
Chris Lattner6e6a8832009-06-27 05:24:12 +0000413 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000414 (S == 1 || S == 2 || S == 4 || S == 8)) {
415 // Scaled-index addressing.
416 Scale = S;
Dan Gohman009a81f2008-12-08 07:57:47 +0000417 IndexReg = getRegForGEPIndex(Op);
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000418 if (IndexReg == 0)
419 return false;
420 } else
421 // Unsupported.
422 goto unsupported_gep;
423 }
424 }
Dan Gohmanf9d724c2008-09-26 20:04:15 +0000425 // Check for displacement overflow.
426 if (!isInt32(Disp))
427 break;
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000428 // Ok, the GEP indices were covered by constant-offset and scaled-index
429 // addressing. Update the address state and move on to examining the base.
430 AM.IndexReg = IndexReg;
431 AM.Scale = Scale;
Dan Gohmanf9d724c2008-09-26 20:04:15 +0000432 AM.Disp = (uint32_t)Disp;
Chris Lattner4ba2d032009-07-10 05:33:42 +0000433 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman3d9f55f2008-09-18 23:23:44 +0000434 unsupported_gep:
435 // Ok, the GEP indices weren't all covered.
436 break;
437 }
438 }
439
440 // Handle constant address.
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000441 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman9039d6b2008-09-25 15:24:26 +0000442 // Can't handle alternate code models yet.
Chris Lattner7a975f82009-07-10 21:03:06 +0000443 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman9039d6b2008-09-25 15:24:26 +0000444 return false;
445
Dan Gohmanc6413362008-09-26 19:15:30 +0000446 // RIP-relative addresses can't have additional register operands.
Chris Lattner6e6a8832009-06-27 05:24:12 +0000447 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohmanc6413362008-09-26 19:15:30 +0000448 (AM.Base.Reg != 0 || AM.IndexReg != 0))
449 return false;
450
Dan Gohman55f84b82009-02-23 22:03:08 +0000451 // Can't handle TLS yet.
452 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
453 if (GVar->isThreadLocal())
454 return false;
455
Chris Lattner915cc302009-07-09 06:41:35 +0000456 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman9039d6b2008-09-25 15:24:26 +0000457 AM.GV = GV;
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000458
Chris Lattnerf37431f2009-07-10 07:48:51 +0000459 // Allow the subtarget to classify the global.
460 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
461
462 // If this reference is relative to the pic base, set it now.
463 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattnera3bde622009-07-09 06:59:17 +0000464 // FIXME: How do we know Base.Reg is free??
Dan Gohman882ab732008-09-30 00:58:23 +0000465 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Chris Lattnera3bde622009-07-09 06:59:17 +0000466 }
Chris Lattnerf37431f2009-07-10 07:48:51 +0000467
468 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattner915cc302009-07-09 06:41:35 +0000469 // the global.
Chris Lattnerf37431f2009-07-10 07:48:51 +0000470 if (!isGlobalStubReference(GVFlags)) {
Chris Lattner915cc302009-07-09 06:41:35 +0000471 if (Subtarget->isPICStyleRIPRel()) {
472 // Use rip-relative addressing if we can. Above we verified that the
473 // base and index registers are unused.
474 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
475 AM.Base.Reg = X86::RIP;
Dan Gohman9ee93e92008-09-19 23:42:04 +0000476 }
Chris Lattnerf37431f2009-07-10 07:48:51 +0000477 AM.GVOpFlags = GVFlags;
Chris Lattner915cc302009-07-09 06:41:35 +0000478 return true;
479 }
480
Chris Lattnerf37431f2009-07-10 07:48:51 +0000481 // Ok, we need to do a load from a stub. If we've already loaded from this
482 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattner915cc302009-07-09 06:41:35 +0000483 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
484 unsigned LoadReg;
485 if (I != LocalValueMap.end() && I->second != 0) {
486 LoadReg = I->second;
487 } else {
Chris Lattnerd617fe72009-07-01 03:27:19 +0000488 // Issue load from stub.
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000489 unsigned Opc = 0;
490 const TargetRegisterClass *RC = NULL;
Dan Gohmanf0800e32008-09-25 23:34:02 +0000491 X86AddressMode StubAM;
492 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattnera3bde622009-07-09 06:59:17 +0000493 StubAM.GV = GV;
Chris Lattnerf37431f2009-07-10 07:48:51 +0000494 StubAM.GVOpFlags = GVFlags;
495
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000496 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattnera3bde622009-07-09 06:59:17 +0000497 Opc = X86::MOV64rm;
498 RC = X86::GR64RegisterClass;
499
Chris Lattnerf37431f2009-07-10 07:48:51 +0000500 if (Subtarget->isPICStyleRIPRel())
Chris Lattnera3bde622009-07-09 06:59:17 +0000501 StubAM.Base.Reg = X86::RIP;
Chris Lattnera3bde622009-07-09 06:59:17 +0000502 } else {
Chris Lattnerd617fe72009-07-01 03:27:19 +0000503 Opc = X86::MOV32rm;
504 RC = X86::GR32RegisterClass;
Chris Lattnerd617fe72009-07-01 03:27:19 +0000505 }
Chris Lattner915cc302009-07-09 06:41:35 +0000506
507 LoadReg = createResultReg(RC);
508 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
509
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000510 // Prevent loading GV stub multiple times in same MBB.
Chris Lattner915cc302009-07-09 06:41:35 +0000511 LocalValueMap[V] = LoadReg;
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000512 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000513
Chris Lattner915cc302009-07-09 06:41:35 +0000514 // Now construct the final address. Note that the Disp, Scale,
515 // and Index values may already be set here.
516 AM.Base.Reg = LoadReg;
517 AM.GV = 0;
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000518 return true;
Dan Gohmand6211a72008-09-10 20:11:02 +0000519 }
520
Dan Gohmanc6413362008-09-26 19:15:30 +0000521 // If all else fails, try to materialize the value in a register.
Chris Lattner6e6a8832009-06-27 05:24:12 +0000522 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanc6413362008-09-26 19:15:30 +0000523 if (AM.Base.Reg == 0) {
524 AM.Base.Reg = getRegForValue(V);
525 return AM.Base.Reg != 0;
526 }
527 if (AM.IndexReg == 0) {
528 assert(AM.Scale == 1 && "Scale with no index!");
529 AM.IndexReg = getRegForValue(V);
530 return AM.IndexReg != 0;
531 }
532 }
533
534 return false;
Dan Gohmand6211a72008-09-10 20:11:02 +0000535}
536
Chris Lattner4ba2d032009-07-10 05:33:42 +0000537/// X86SelectCallAddress - Attempt to fill in an address from the given value.
538///
539bool X86FastISel::X86SelectCallAddress(Value *V, X86AddressMode &AM) {
540 User *U = NULL;
541 unsigned Opcode = Instruction::UserOp1;
542 if (Instruction *I = dyn_cast<Instruction>(V)) {
543 Opcode = I->getOpcode();
544 U = I;
545 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
546 Opcode = C->getOpcode();
547 U = C;
548 }
549
550 switch (Opcode) {
551 default: break;
552 case Instruction::BitCast:
553 // Look past bitcasts.
554 return X86SelectCallAddress(U->getOperand(0), AM);
555
556 case Instruction::IntToPtr:
557 // Look past no-op inttoptrs.
558 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
559 return X86SelectCallAddress(U->getOperand(0), AM);
560 break;
561
562 case Instruction::PtrToInt:
563 // Look past no-op ptrtoints.
564 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
565 return X86SelectCallAddress(U->getOperand(0), AM);
566 break;
567 }
568
569 // Handle constant address.
570 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
571 // Can't handle alternate code models yet.
Chris Lattner7a975f82009-07-10 21:03:06 +0000572 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner4ba2d032009-07-10 05:33:42 +0000573 return false;
574
575 // RIP-relative addresses can't have additional register operands.
576 if (Subtarget->isPICStyleRIPRel() &&
577 (AM.Base.Reg != 0 || AM.IndexReg != 0))
578 return false;
579
Chris Lattner180a7ee2009-07-10 05:48:03 +0000580 // Can't handle TLS or DLLImport.
Chris Lattner4ba2d032009-07-10 05:33:42 +0000581 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattner08323962009-07-10 05:45:15 +0000582 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner4ba2d032009-07-10 05:33:42 +0000583 return false;
584
585 // Okay, we've committed to selecting this global. Set up the basic address.
586 AM.GV = GV;
587
Chris Lattner08323962009-07-10 05:45:15 +0000588 // No ABI requires an extra load for anything other than DLLImport, which
589 // we rejected above. Return a direct reference to the global.
Chris Lattner08323962009-07-10 05:45:15 +0000590 if (Subtarget->isPICStyleRIPRel()) {
591 // Use rip-relative addressing if we can. Above we verified that the
592 // base and index registers are unused.
593 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
594 AM.Base.Reg = X86::RIP;
Chris Lattner2e9393c2009-07-10 21:00:45 +0000595 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner08323962009-07-10 05:45:15 +0000596 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
597 } else if (Subtarget->isPICStyleGOT()) {
598 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner4ba2d032009-07-10 05:33:42 +0000599 }
600
Chris Lattner4ba2d032009-07-10 05:33:42 +0000601 return true;
602 }
603
604 // If all else fails, try to materialize the value in a register.
605 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
606 if (AM.Base.Reg == 0) {
607 AM.Base.Reg = getRegForValue(V);
608 return AM.Base.Reg != 0;
609 }
610 if (AM.IndexReg == 0) {
611 assert(AM.Scale == 1 && "Scale with no index!");
612 AM.IndexReg = getRegForValue(V);
613 return AM.IndexReg != 0;
614 }
615 }
616
617 return false;
618}
619
620
Owen Andersonf4e3ec82008-09-04 07:08:58 +0000621/// X86SelectStore - Select and emit code to implement store instructions.
622bool X86FastISel::X86SelectStore(Instruction* I) {
Owen Andersonac9de032009-08-10 22:56:29 +0000623 EVT VT;
Dan Gohmanb59f15a2009-08-27 00:31:47 +0000624 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersonf4e3ec82008-09-04 07:08:58 +0000625 return false;
Owen Andersonf4e3ec82008-09-04 07:08:58 +0000626
Dan Gohmand6211a72008-09-10 20:11:02 +0000627 X86AddressMode AM;
Chris Lattner4ba2d032009-07-10 05:33:42 +0000628 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohmand6211a72008-09-10 20:11:02 +0000629 return false;
Owen Andersonf4e3ec82008-09-04 07:08:58 +0000630
Chris Lattner65839062008-10-15 05:30:52 +0000631 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersonf4e3ec82008-09-04 07:08:58 +0000632}
633
Evan Cheng8700bb92008-09-03 06:44:39 +0000634/// X86SelectLoad - Select and emit code to implement load instructions.
635///
Dan Gohmanca4857a2008-09-03 23:12:08 +0000636bool X86FastISel::X86SelectLoad(Instruction *I) {
Owen Andersonac9de032009-08-10 22:56:29 +0000637 EVT VT;
Dan Gohmanb59f15a2009-08-27 00:31:47 +0000638 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8700bb92008-09-03 06:44:39 +0000639 return false;
640
Dan Gohmand6211a72008-09-10 20:11:02 +0000641 X86AddressMode AM;
Chris Lattner4ba2d032009-07-10 05:33:42 +0000642 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohmand6211a72008-09-10 20:11:02 +0000643 return false;
Evan Cheng8700bb92008-09-03 06:44:39 +0000644
Evan Chengcb5422c2008-09-05 21:00:03 +0000645 unsigned ResultReg = 0;
Dan Gohmand6211a72008-09-10 20:11:02 +0000646 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Chengcb5422c2008-09-05 21:00:03 +0000647 UpdateValueMap(I, ResultReg);
648 return true;
Evan Cheng8700bb92008-09-03 06:44:39 +0000649 }
Evan Chengcb5422c2008-09-05 21:00:03 +0000650 return false;
Evan Cheng8700bb92008-09-03 06:44:39 +0000651}
652
Owen Andersonac9de032009-08-10 22:56:29 +0000653static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000654 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner44384eb2008-10-15 04:32:45 +0000655 default: return 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000656 case MVT::i8: return X86::CMP8rr;
657 case MVT::i16: return X86::CMP16rr;
658 case MVT::i32: return X86::CMP32rr;
659 case MVT::i64: return X86::CMP64rr;
660 case MVT::f32: return X86::UCOMISSrr;
661 case MVT::f64: return X86::UCOMISDrr;
Dan Gohman8766d8e2008-10-02 22:15:21 +0000662 }
Dan Gohman8766d8e2008-10-02 22:15:21 +0000663}
664
Chris Lattnerfec8b6d2008-10-15 04:13:29 +0000665/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
666/// of the comparison, return an opcode that works for the compare (e.g.
667/// CMP32ri) otherwise return 0.
Owen Andersonac9de032009-08-10 22:56:29 +0000668static unsigned X86ChooseCmpImmediateOpcode(EVT VT, ConstantInt *RHSC) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000669 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerfec8b6d2008-10-15 04:13:29 +0000670 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner44384eb2008-10-15 04:32:45 +0000671 default: return 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000672 case MVT::i8: return X86::CMP8ri;
673 case MVT::i16: return X86::CMP16ri;
674 case MVT::i32: return X86::CMP32ri;
675 case MVT::i64:
Chris Lattner44384eb2008-10-15 04:32:45 +0000676 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
677 // field.
Chris Lattner65839062008-10-15 05:30:52 +0000678 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner44384eb2008-10-15 04:32:45 +0000679 return X86::CMP64ri32;
680 return 0;
681 }
Chris Lattnerfec8b6d2008-10-15 04:13:29 +0000682}
683
Owen Andersonac9de032009-08-10 22:56:29 +0000684bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, EVT VT) {
Chris Lattner1c921862008-10-15 04:26:38 +0000685 unsigned Op0Reg = getRegForValue(Op0);
686 if (Op0Reg == 0) return false;
687
Chris Lattnerdcf5d392008-10-15 05:18:04 +0000688 // Handle 'null' like i32/i64 0.
689 if (isa<ConstantPointerNull>(Op1))
Owen Anderson35b47072009-08-13 21:58:54 +0000690 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerdcf5d392008-10-15 05:18:04 +0000691
Chris Lattner1c921862008-10-15 04:26:38 +0000692 // We have two options: compare with register or immediate. If the RHS of
693 // the compare is an immediate that we can fold into this compare, use
694 // CMPri, otherwise use CMPrr.
695 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner44384eb2008-10-15 04:32:45 +0000696 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000697 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
Chris Lattner1c921862008-10-15 04:26:38 +0000698 .addImm(Op1C->getSExtValue());
699 return true;
700 }
701 }
702
703 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
704 if (CompareOpc == 0) return false;
705
706 unsigned Op1Reg = getRegForValue(Op1);
707 if (Op1Reg == 0) return false;
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000708 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner1c921862008-10-15 04:26:38 +0000709
710 return true;
711}
712
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000713bool X86FastISel::X86SelectCmp(Instruction *I) {
714 CmpInst *CI = cast<CmpInst>(I);
715
Owen Andersonac9de032009-08-10 22:56:29 +0000716 EVT VT;
Chris Lattnerffba2be2008-10-15 05:07:36 +0000717 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman2481a2e2008-09-05 01:33:56 +0000718 return false;
719
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000720 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattnera5fe6e32008-10-15 03:47:17 +0000721 unsigned SetCCOpc;
Chris Lattner45814ac2008-10-15 03:52:54 +0000722 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000723 switch (CI->getPredicate()) {
724 case CmpInst::FCMP_OEQ: {
Chris Lattneree82bd52008-10-15 04:29:23 +0000725 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
726 return false;
Chris Lattner1c921862008-10-15 04:26:38 +0000727
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000728 unsigned EReg = createResultReg(&X86::GR8RegClass);
729 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000730 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
731 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
732 BuildMI(MBB, DL,
733 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattnera5fe6e32008-10-15 03:47:17 +0000734 UpdateValueMap(I, ResultReg);
735 return true;
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000736 }
737 case CmpInst::FCMP_UNE: {
Chris Lattneree82bd52008-10-15 04:29:23 +0000738 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
739 return false;
740
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000741 unsigned NEReg = createResultReg(&X86::GR8RegClass);
742 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000743 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
744 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
745 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattnera5fe6e32008-10-15 03:47:17 +0000746 UpdateValueMap(I, ResultReg);
747 return true;
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000748 }
Chris Lattner45814ac2008-10-15 03:52:54 +0000749 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
750 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
751 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
752 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
753 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
754 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
755 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
756 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
757 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
758 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
759 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
760 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
761
762 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
763 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
764 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
765 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
766 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
767 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
768 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
769 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
770 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
771 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000772 default:
773 return false;
774 }
775
Chris Lattner1c921862008-10-15 04:26:38 +0000776 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner45814ac2008-10-15 03:52:54 +0000777 if (SwapArgs)
Chris Lattner1c921862008-10-15 04:26:38 +0000778 std::swap(Op0, Op1);
Chris Lattner45814ac2008-10-15 03:52:54 +0000779
Chris Lattner1c921862008-10-15 04:26:38 +0000780 // Emit a compare of Op0/Op1.
Chris Lattneree82bd52008-10-15 04:29:23 +0000781 if (!X86FastEmitCompare(Op0, Op1, VT))
782 return false;
Chris Lattner1c921862008-10-15 04:26:38 +0000783
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000784 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohmane1cdaa62008-09-04 23:26:51 +0000785 UpdateValueMap(I, ResultReg);
786 return true;
787}
Evan Cheng8700bb92008-09-03 06:44:39 +0000788
Dan Gohman4a7d4d62008-09-05 01:06:14 +0000789bool X86FastISel::X86SelectZExt(Instruction *I) {
Dan Gohman01648d92009-03-13 20:42:20 +0000790 // Handle zero-extension from i1 to i8, which is common.
Benjamin Kramer0461f522010-01-05 20:07:06 +0000791 if (I->getType()->isInteger(8) &&
792 I->getOperand(0)->getType()->isInteger(1)) {
Dan Gohman4a7d4d62008-09-05 01:06:14 +0000793 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanfc0625f2008-09-05 01:15:35 +0000794 if (ResultReg == 0) return false;
Dan Gohman01648d92009-03-13 20:42:20 +0000795 // Set the high bits to zero.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000796 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg);
Dan Gohman01648d92009-03-13 20:42:20 +0000797 if (ResultReg == 0) return false;
Dan Gohman4a7d4d62008-09-05 01:06:14 +0000798 UpdateValueMap(I, ResultReg);
799 return true;
800 }
801
802 return false;
803}
804
Chris Lattner1c921862008-10-15 04:26:38 +0000805
Dan Gohman4a7d4d62008-09-05 01:06:14 +0000806bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohman4a7d4d62008-09-05 01:06:14 +0000807 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman8766d8e2008-10-02 22:15:21 +0000808 // Handle a conditional branch.
809 BranchInst *BI = cast<BranchInst>(I);
Dan Gohman4a7d4d62008-09-05 01:06:14 +0000810 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
811 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
812
Dan Gohman8766d8e2008-10-02 22:15:21 +0000813 // Fold the common case of a conditional branch with a comparison.
814 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
815 if (CI->hasOneUse()) {
Owen Andersonac9de032009-08-10 22:56:29 +0000816 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohman4a7d4d62008-09-05 01:06:14 +0000817
Dan Gohman8766d8e2008-10-02 22:15:21 +0000818 // Try to take advantage of fallthrough opportunities.
819 CmpInst::Predicate Predicate = CI->getPredicate();
820 if (MBB->isLayoutSuccessor(TrueMBB)) {
821 std::swap(TrueMBB, FalseMBB);
822 Predicate = CmpInst::getInversePredicate(Predicate);
823 }
824
Chris Lattner51c6d172008-10-15 03:58:05 +0000825 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
826 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
827
Dan Gohman8766d8e2008-10-02 22:15:21 +0000828 switch (Predicate) {
Dan Gohmane9d8fa42008-10-21 18:24:51 +0000829 case CmpInst::FCMP_OEQ:
830 std::swap(TrueMBB, FalseMBB);
831 Predicate = CmpInst::FCMP_UNE;
832 // FALL THROUGH
833 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE; break;
Chris Lattner51c6d172008-10-15 03:58:05 +0000834 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
835 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
836 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
837 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
838 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
839 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
840 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
841 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
842 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
843 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
844 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
845 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner1c921862008-10-15 04:26:38 +0000846
Chris Lattner51c6d172008-10-15 03:58:05 +0000847 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
848 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
849 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
850 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
851 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
852 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
853 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
854 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
855 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
856 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohman8766d8e2008-10-02 22:15:21 +0000857 default:
858 return false;
859 }
Chris Lattnera5fe6e32008-10-15 03:47:17 +0000860
Chris Lattner73ec6822008-10-15 04:02:26 +0000861 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
862 if (SwapArgs)
863 std::swap(Op0, Op1);
864
Chris Lattner1c921862008-10-15 04:26:38 +0000865 // Emit a compare of the LHS and RHS, setting the flags.
866 if (!X86FastEmitCompare(Op0, Op1, VT))
867 return false;
Chris Lattnerfec8b6d2008-10-15 04:13:29 +0000868
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000869 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohmane9d8fa42008-10-21 18:24:51 +0000870
871 if (Predicate == CmpInst::FCMP_UNE) {
872 // X86 requires a second branch to handle UNE (and OEQ,
873 // which is mapped to UNE above).
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000874 BuildMI(MBB, DL, TII.get(X86::JP)).addMBB(TrueMBB);
Dan Gohmane9d8fa42008-10-21 18:24:51 +0000875 }
876
Dan Gohman8766d8e2008-10-02 22:15:21 +0000877 FastEmitBranch(FalseMBB);
Dan Gohman831db402008-10-07 22:10:33 +0000878 MBB->addSuccessor(TrueMBB);
Dan Gohman8766d8e2008-10-02 22:15:21 +0000879 return true;
880 }
Bill Wendling1203edc2008-12-09 23:19:12 +0000881 } else if (ExtractValueInst *EI =
882 dyn_cast<ExtractValueInst>(BI->getCondition())) {
883 // Check to see if the branch instruction is from an "arithmetic with
884 // overflow" intrinsic. The main way these intrinsics are used is:
885 //
886 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
887 // %sum = extractvalue { i32, i1 } %t, 0
888 // %obit = extractvalue { i32, i1 } %t, 1
889 // br i1 %obit, label %overflow, label %normal
890 //
Dan Gohman0fc9ed62009-01-07 00:15:08 +0000891 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling1203edc2008-12-09 23:19:12 +0000892 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman0fc9ed62009-01-07 00:15:08 +0000893 // looking for the SETO/SETB instruction. If an instruction modifies the
894 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
895 // convert the branch into a JO/JB instruction.
Chris Lattnera6a19bd2009-04-12 07:36:01 +0000896 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
897 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
898 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
899 const MachineInstr *SetMI = 0;
900 unsigned Reg = lookUpRegForValue(EI);
Bill Wendling1203edc2008-12-09 23:19:12 +0000901
Chris Lattnera6a19bd2009-04-12 07:36:01 +0000902 for (MachineBasicBlock::const_reverse_iterator
903 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
904 const MachineInstr &MI = *RI;
Bill Wendling1203edc2008-12-09 23:19:12 +0000905
Chris Lattnera6a19bd2009-04-12 07:36:01 +0000906 if (MI.modifiesRegister(Reg)) {
907 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling1203edc2008-12-09 23:19:12 +0000908
Chris Lattnera6a19bd2009-04-12 07:36:01 +0000909 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
910 Reg = Src;
911 continue;
Bill Wendlingb372ca482008-12-10 19:44:24 +0000912 }
Bill Wendling1203edc2008-12-09 23:19:12 +0000913
Chris Lattnera6a19bd2009-04-12 07:36:01 +0000914 SetMI = &MI;
915 break;
Bill Wendling1203edc2008-12-09 23:19:12 +0000916 }
Bill Wendling1203edc2008-12-09 23:19:12 +0000917
Chris Lattnera6a19bd2009-04-12 07:36:01 +0000918 const TargetInstrDesc &TID = MI.getDesc();
919 if (TID.hasUnmodeledSideEffects() ||
920 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
921 break;
Bill Wendlingb372ca482008-12-10 19:44:24 +0000922 }
Chris Lattnera6a19bd2009-04-12 07:36:01 +0000923
924 if (SetMI) {
925 unsigned OpCode = SetMI->getOpcode();
926
927 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Chris Lattner7dd0d262009-04-12 07:51:14 +0000928 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ? X86::JO : X86::JB))
929 .addMBB(TrueMBB);
Chris Lattnera6a19bd2009-04-12 07:36:01 +0000930 FastEmitBranch(FalseMBB);
931 MBB->addSuccessor(TrueMBB);
932 return true;
933 }
Bill Wendlingb372ca482008-12-10 19:44:24 +0000934 }
Bill Wendling1203edc2008-12-09 23:19:12 +0000935 }
936 }
Dan Gohman8766d8e2008-10-02 22:15:21 +0000937 }
938
939 // Otherwise do a clumsy setcc and re-test it.
940 unsigned OpReg = getRegForValue(BI->getCondition());
941 if (OpReg == 0) return false;
942
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000943 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
944 BuildMI(MBB, DL, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohman8766d8e2008-10-02 22:15:21 +0000945 FastEmitBranch(FalseMBB);
Dan Gohman831db402008-10-07 22:10:33 +0000946 MBB->addSuccessor(TrueMBB);
Dan Gohman4a7d4d62008-09-05 01:06:14 +0000947 return true;
948}
949
Evan Chengb10ba152010-01-11 22:59:27 +0000950bool X86FastISel::X86SelectOR(Instruction *I) {
951 // FIXME: This is necessary because tablegen stopped generate fastisel
952 // patterns after 93152 and 93191 (which turns OR to ADD if the set
953 // bits in the source operands are known not to overlap).
954 const TargetRegisterClass *RC = NULL;
955 unsigned OpReg = 0, OpImm = 0;
956 if (I->getType()->isInteger(16)) {
957 RC = X86::GR16RegisterClass;
958 OpReg = X86::OR16rr; OpImm = X86::OR16ri;
959 } else if (I->getType()->isInteger(32)) {
960 RC = X86::GR32RegisterClass;
961 OpReg = X86::OR32rr; OpImm = X86::OR32ri;
962 } else if (I->getType()->isInteger(64)) {
963 RC = X86::GR64RegisterClass;
964 OpReg = X86::OR32rr; OpImm = X86::OR32ri;
965 } else
966 return false;
967
968 unsigned Op0Reg = getRegForValue(I->getOperand(0));
969 if (Op0Reg == 0) return false;
970
971 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
972 unsigned ResultReg = createResultReg(RC);
973 BuildMI(MBB, DL, TII.get(OpImm), ResultReg).addReg(Op0Reg)
974 .addImm(CI->getZExtValue());
975 UpdateValueMap(I, ResultReg);
976 return true;
977 }
978
979 unsigned Op1Reg = getRegForValue(I->getOperand(1));
980 if (Op1Reg == 0) return false;
981
982 unsigned ResultReg = createResultReg(RC);
983 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg).addReg(Op1Reg);
984 UpdateValueMap(I, ResultReg);
985 return true;
986}
987
Dan Gohman9ba59f82008-09-05 18:30:08 +0000988bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner16cfae12008-09-21 21:44:29 +0000989 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohman9ba59f82008-09-05 18:30:08 +0000990 const TargetRegisterClass *RC = NULL;
Benjamin Kramer0461f522010-01-05 20:07:06 +0000991 if (I->getType()->isInteger(8)) {
Dan Gohman9ba59f82008-09-05 18:30:08 +0000992 CReg = X86::CL;
993 RC = &X86::GR8RegClass;
994 switch (I->getOpcode()) {
Chris Lattner16cfae12008-09-21 21:44:29 +0000995 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
996 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
997 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohman9ba59f82008-09-05 18:30:08 +0000998 default: return false;
999 }
Benjamin Kramer0461f522010-01-05 20:07:06 +00001000 } else if (I->getType()->isInteger(16)) {
Dan Gohman9ba59f82008-09-05 18:30:08 +00001001 CReg = X86::CX;
1002 RC = &X86::GR16RegClass;
1003 switch (I->getOpcode()) {
Chris Lattner16cfae12008-09-21 21:44:29 +00001004 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1005 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1006 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohman9ba59f82008-09-05 18:30:08 +00001007 default: return false;
1008 }
Benjamin Kramer0461f522010-01-05 20:07:06 +00001009 } else if (I->getType()->isInteger(32)) {
Dan Gohman9ba59f82008-09-05 18:30:08 +00001010 CReg = X86::ECX;
1011 RC = &X86::GR32RegClass;
1012 switch (I->getOpcode()) {
Chris Lattner16cfae12008-09-21 21:44:29 +00001013 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1014 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1015 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohman9ba59f82008-09-05 18:30:08 +00001016 default: return false;
1017 }
Benjamin Kramer0461f522010-01-05 20:07:06 +00001018 } else if (I->getType()->isInteger(64)) {
Dan Gohman9ba59f82008-09-05 18:30:08 +00001019 CReg = X86::RCX;
1020 RC = &X86::GR64RegClass;
1021 switch (I->getOpcode()) {
Chris Lattner16cfae12008-09-21 21:44:29 +00001022 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1023 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1024 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohman9ba59f82008-09-05 18:30:08 +00001025 default: return false;
1026 }
1027 } else {
1028 return false;
1029 }
1030
Owen Andersonac9de032009-08-10 22:56:29 +00001031 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001032 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanc75d4352008-09-05 21:27:34 +00001033 return false;
1034
Dan Gohman9ba59f82008-09-05 18:30:08 +00001035 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1036 if (Op0Reg == 0) return false;
Chris Lattner16cfae12008-09-21 21:44:29 +00001037
1038 // Fold immediate in shl(x,3).
1039 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
1040 unsigned ResultReg = createResultReg(RC);
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001041 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmandb8419c2008-12-20 17:19:40 +00001042 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner16cfae12008-09-21 21:44:29 +00001043 UpdateValueMap(I, ResultReg);
1044 return true;
1045 }
1046
Dan Gohman9ba59f82008-09-05 18:30:08 +00001047 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1048 if (Op1Reg == 0) return false;
1049 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman74a3fa92008-10-07 21:50:36 +00001050
1051 // The shift instruction uses X86::CL. If we defined a super-register
1052 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1053 // we're doing here.
1054 if (CReg != X86::CL)
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001055 BuildMI(MBB, DL, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
Dan Gohman74a3fa92008-10-07 21:50:36 +00001056 .addReg(CReg).addImm(X86::SUBREG_8BIT);
1057
Dan Gohman9ba59f82008-09-05 18:30:08 +00001058 unsigned ResultReg = createResultReg(RC);
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001059 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohman9ba59f82008-09-05 18:30:08 +00001060 UpdateValueMap(I, ResultReg);
1061 return true;
1062}
1063
1064bool X86FastISel::X86SelectSelect(Instruction *I) {
Owen Andersonac9de032009-08-10 22:56:29 +00001065 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001066 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattnerffba2be2008-10-15 05:07:36 +00001067 return false;
1068
Dan Gohman9ba59f82008-09-05 18:30:08 +00001069 unsigned Opc = 0;
1070 const TargetRegisterClass *RC = NULL;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001071 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohmanbe324562008-09-05 21:13:04 +00001072 Opc = X86::CMOVE16rr;
Dan Gohman9ba59f82008-09-05 18:30:08 +00001073 RC = &X86::GR16RegClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001074 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohmanbe324562008-09-05 21:13:04 +00001075 Opc = X86::CMOVE32rr;
Dan Gohman9ba59f82008-09-05 18:30:08 +00001076 RC = &X86::GR32RegClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001077 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohmanbe324562008-09-05 21:13:04 +00001078 Opc = X86::CMOVE64rr;
Dan Gohman9ba59f82008-09-05 18:30:08 +00001079 RC = &X86::GR64RegClass;
1080 } else {
1081 return false;
1082 }
1083
1084 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1085 if (Op0Reg == 0) return false;
1086 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1087 if (Op1Reg == 0) return false;
1088 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1089 if (Op2Reg == 0) return false;
1090
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001091 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohman9ba59f82008-09-05 18:30:08 +00001092 unsigned ResultReg = createResultReg(RC);
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001093 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman9ba59f82008-09-05 18:30:08 +00001094 UpdateValueMap(I, ResultReg);
1095 return true;
1096}
1097
Dan Gohman658637e2008-09-10 21:02:08 +00001098bool X86FastISel::X86SelectFPExt(Instruction *I) {
Chris Lattnerffba2be2008-10-15 05:07:36 +00001099 // fpext from float to double.
Owen Anderson35b47072009-08-13 21:58:54 +00001100 if (Subtarget->hasSSE2() &&
Chris Lattner82cdc062009-10-05 05:54:46 +00001101 I->getType()->isDoubleTy()) {
Chris Lattnerffba2be2008-10-15 05:07:36 +00001102 Value *V = I->getOperand(0);
Chris Lattner82cdc062009-10-05 05:54:46 +00001103 if (V->getType()->isFloatTy()) {
Chris Lattnerffba2be2008-10-15 05:07:36 +00001104 unsigned OpReg = getRegForValue(V);
1105 if (OpReg == 0) return false;
1106 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001107 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattnerffba2be2008-10-15 05:07:36 +00001108 UpdateValueMap(I, ResultReg);
1109 return true;
Dan Gohman658637e2008-09-10 21:02:08 +00001110 }
1111 }
1112
1113 return false;
1114}
1115
1116bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
1117 if (Subtarget->hasSSE2()) {
Chris Lattner82cdc062009-10-05 05:54:46 +00001118 if (I->getType()->isFloatTy()) {
Dan Gohman658637e2008-09-10 21:02:08 +00001119 Value *V = I->getOperand(0);
Chris Lattner82cdc062009-10-05 05:54:46 +00001120 if (V->getType()->isDoubleTy()) {
Dan Gohman658637e2008-09-10 21:02:08 +00001121 unsigned OpReg = getRegForValue(V);
1122 if (OpReg == 0) return false;
1123 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001124 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman658637e2008-09-10 21:02:08 +00001125 UpdateValueMap(I, ResultReg);
1126 return true;
1127 }
1128 }
1129 }
1130
1131 return false;
1132}
1133
Evan Cheng303530d2008-09-07 08:47:42 +00001134bool X86FastISel::X86SelectTrunc(Instruction *I) {
1135 if (Subtarget->is64Bit())
1136 // All other cases should be handled by the tblgen generated code.
1137 return false;
Owen Andersonac9de032009-08-10 22:56:29 +00001138 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1139 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner43af4982009-03-13 16:36:42 +00001140
1141 // This code only handles truncation to byte right now.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001142 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng303530d2008-09-07 08:47:42 +00001143 // All other cases should be handled by the tblgen generated code.
1144 return false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001145 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng303530d2008-09-07 08:47:42 +00001146 // All other cases should be handled by the tblgen generated code.
1147 return false;
1148
1149 unsigned InputReg = getRegForValue(I->getOperand(0));
1150 if (!InputReg)
1151 // Unhandled operand. Halt "fast" selection and bail.
1152 return false;
1153
Dan Gohman6e438702009-04-27 16:33:14 +00001154 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001155 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1156 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman6e438702009-04-27 16:33:14 +00001157 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng303530d2008-09-07 08:47:42 +00001158 unsigned CopyReg = createResultReg(CopyRC);
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001159 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng303530d2008-09-07 08:47:42 +00001160
1161 // Then issue an extract_subreg.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001162 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Evan Chengbfda7272009-01-22 09:10:11 +00001163 CopyReg, X86::SUBREG_8BIT);
Evan Cheng303530d2008-09-07 08:47:42 +00001164 if (!ResultReg)
1165 return false;
1166
1167 UpdateValueMap(I, ResultReg);
1168 return true;
1169}
1170
Bill Wendling33fe51e2008-12-09 02:42:50 +00001171bool X86FastISel::X86SelectExtractValue(Instruction *I) {
1172 ExtractValueInst *EI = cast<ExtractValueInst>(I);
1173 Value *Agg = EI->getAggregateOperand();
1174
Chris Lattnera6a19bd2009-04-12 07:36:01 +00001175 if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
1176 switch (CI->getIntrinsicID()) {
1177 default: break;
1178 case Intrinsic::sadd_with_overflow:
1179 case Intrinsic::uadd_with_overflow:
1180 // Cheat a little. We know that the registers for "add" and "seto" are
1181 // allocated sequentially. However, we only keep track of the register
1182 // for "add" in the value map. Use extractvalue's index to get the
1183 // correct register for "seto".
1184 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
1185 return true;
Bill Wendling33fe51e2008-12-09 02:42:50 +00001186 }
1187 }
1188
1189 return false;
1190}
1191
Chris Lattnera6a19bd2009-04-12 07:36:01 +00001192bool X86FastISel::X86VisitIntrinsicCall(IntrinsicInst &I) {
Bill Wendling33fe51e2008-12-09 02:42:50 +00001193 // FIXME: Handle more intrinsics.
Chris Lattnera6a19bd2009-04-12 07:36:01 +00001194 switch (I.getIntrinsicID()) {
Bill Wendling33fe51e2008-12-09 02:42:50 +00001195 default: return false;
1196 case Intrinsic::sadd_with_overflow:
1197 case Intrinsic::uadd_with_overflow: {
Bill Wendling9ae4f872008-12-09 07:55:31 +00001198 // Replace "add with overflow" intrinsics with an "add" instruction followed
1199 // by a seto/setc instruction. Later on, when the "extractvalue"
1200 // instructions are encountered, we use the fact that two registers were
1201 // created sequentially to get the correct registers for the "sum" and the
1202 // "overflow bit".
Bill Wendling33fe51e2008-12-09 02:42:50 +00001203 const Function *Callee = I.getCalledFunction();
1204 const Type *RetTy =
1205 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1206
Owen Andersonac9de032009-08-10 22:56:29 +00001207 EVT VT;
Bill Wendling33fe51e2008-12-09 02:42:50 +00001208 if (!isTypeLegal(RetTy, VT))
1209 return false;
1210
1211 Value *Op1 = I.getOperand(1);
1212 Value *Op2 = I.getOperand(2);
1213 unsigned Reg1 = getRegForValue(Op1);
1214 unsigned Reg2 = getRegForValue(Op2);
1215
1216 if (Reg1 == 0 || Reg2 == 0)
1217 // FIXME: Handle values *not* in registers.
1218 return false;
1219
1220 unsigned OpC = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001221 if (VT == MVT::i32)
Bill Wendling33fe51e2008-12-09 02:42:50 +00001222 OpC = X86::ADD32rr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001223 else if (VT == MVT::i64)
Bill Wendling33fe51e2008-12-09 02:42:50 +00001224 OpC = X86::ADD64rr;
1225 else
1226 return false;
1227
1228 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001229 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner7dd0d262009-04-12 07:51:14 +00001230 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling33fe51e2008-12-09 02:42:50 +00001231
Chris Lattner7dd0d262009-04-12 07:51:14 +00001232 // If the add with overflow is an intra-block value then we just want to
1233 // create temporaries for it like normal. If it is a cross-block value then
1234 // UpdateValueMap will return the cross-block register used. Since we
1235 // *really* want the value to be live in the register pair known by
1236 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1237 // the cross block case. In the non-cross-block case, we should just make
1238 // another register for the value.
1239 if (DestReg1 != ResultReg)
1240 ResultReg = DestReg1+1;
1241 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001242 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner7dd0d262009-04-12 07:51:14 +00001243
Chris Lattnera6a19bd2009-04-12 07:36:01 +00001244 unsigned Opc = X86::SETBr;
1245 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1246 Opc = X86::SETOr;
1247 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling33fe51e2008-12-09 02:42:50 +00001248 return true;
1249 }
1250 }
1251}
1252
Evan Cheng8016fb82008-09-07 09:09:33 +00001253bool X86FastISel::X86SelectCall(Instruction *I) {
1254 CallInst *CI = cast<CallInst>(I);
1255 Value *Callee = I->getOperand(0);
1256
1257 // Can't handle inline asm yet.
1258 if (isa<InlineAsm>(Callee))
1259 return false;
1260
Bill Wendling33fe51e2008-12-09 02:42:50 +00001261 // Handle intrinsic calls.
Chris Lattnera6a19bd2009-04-12 07:36:01 +00001262 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
1263 return X86VisitIntrinsicCall(*II);
Evan Cheng8016fb82008-09-07 09:09:33 +00001264
Evan Cheng8016fb82008-09-07 09:09:33 +00001265 // Handle only C and fastcc calling conventions for now.
1266 CallSite CS(CI);
Sandeep Patel5838baa2009-09-02 08:44:58 +00001267 CallingConv::ID CC = CS.getCallingConv();
Evan Cheng8016fb82008-09-07 09:09:33 +00001268 if (CC != CallingConv::C &&
1269 CC != CallingConv::Fast &&
1270 CC != CallingConv::X86_FastCall)
1271 return false;
1272
Dan Gohman07968482010-01-11 17:14:46 +00001273 // fastcc with -tailcallopt is intended to provide a guaranteed
1274 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman477b0582009-05-04 19:50:33 +00001275 if (CC == CallingConv::Fast && PerformTailCallOpt)
1276 return false;
1277
Evan Cheng8016fb82008-09-07 09:09:33 +00001278 // Let SDISel handle vararg functions.
1279 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1280 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1281 if (FTy->isVarArg())
1282 return false;
1283
1284 // Handle *simple* calls for now.
1285 const Type *RetTy = CS.getType();
Owen Andersonac9de032009-08-10 22:56:29 +00001286 EVT RetVT;
Chris Lattner82cdc062009-10-05 05:54:46 +00001287 if (RetTy->isVoidTy())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001288 RetVT = MVT::isVoid;
Chris Lattnerffba2be2008-10-15 05:07:36 +00001289 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Cheng8016fb82008-09-07 09:09:33 +00001290 return false;
1291
Dan Gohman49e98b52008-09-17 21:18:49 +00001292 // Materialize callee address in a register. FIXME: GV address can be
1293 // handled with a CALLpcrel32 instead.
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001294 X86AddressMode CalleeAM;
Chris Lattner4ba2d032009-07-10 05:33:42 +00001295 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001296 return false;
Dan Gohman49e98b52008-09-17 21:18:49 +00001297 unsigned CalleeOp = 0;
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001298 GlobalValue *GV = 0;
Chris Lattner09a79a62009-06-27 04:50:14 +00001299 if (CalleeAM.GV != 0) {
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001300 GV = CalleeAM.GV;
Chris Lattner09a79a62009-06-27 04:50:14 +00001301 } else if (CalleeAM.Base.Reg != 0) {
1302 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001303 } else
1304 return false;
Dan Gohman49e98b52008-09-17 21:18:49 +00001305
Evan Cheng52001032008-09-08 17:15:42 +00001306 // Allow calls which produce i1 results.
1307 bool AndToI1 = false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001308 if (RetVT == MVT::i1) {
1309 RetVT = MVT::i8;
Evan Cheng52001032008-09-08 17:15:42 +00001310 AndToI1 = true;
1311 }
1312
Evan Cheng8016fb82008-09-07 09:09:33 +00001313 // Deal with call operands first.
Chris Lattnerd03ed8f2008-10-15 05:38:32 +00001314 SmallVector<Value*, 8> ArgVals;
1315 SmallVector<unsigned, 8> Args;
Owen Andersonac9de032009-08-10 22:56:29 +00001316 SmallVector<EVT, 8> ArgVTs;
Chris Lattnerd03ed8f2008-10-15 05:38:32 +00001317 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Cheng8016fb82008-09-07 09:09:33 +00001318 Args.reserve(CS.arg_size());
Chris Lattnerd03ed8f2008-10-15 05:38:32 +00001319 ArgVals.reserve(CS.arg_size());
Evan Cheng8016fb82008-09-07 09:09:33 +00001320 ArgVTs.reserve(CS.arg_size());
1321 ArgFlags.reserve(CS.arg_size());
1322 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1323 i != e; ++i) {
1324 unsigned Arg = getRegForValue(*i);
1325 if (Arg == 0)
1326 return false;
1327 ISD::ArgFlagsTy Flags;
1328 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Pateld222f862008-09-25 21:00:45 +00001329 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng8016fb82008-09-07 09:09:33 +00001330 Flags.setSExt();
Devang Pateld222f862008-09-25 21:00:45 +00001331 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng8016fb82008-09-07 09:09:33 +00001332 Flags.setZExt();
1333
1334 // FIXME: Only handle *easy* calls for now.
Devang Pateld222f862008-09-25 21:00:45 +00001335 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1336 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1337 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1338 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Cheng8016fb82008-09-07 09:09:33 +00001339 return false;
1340
1341 const Type *ArgTy = (*i)->getType();
Owen Andersonac9de032009-08-10 22:56:29 +00001342 EVT ArgVT;
Chris Lattnerffba2be2008-10-15 05:07:36 +00001343 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng8016fb82008-09-07 09:09:33 +00001344 return false;
1345 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1346 Flags.setOrigAlign(OriginalAlignment);
1347
1348 Args.push_back(Arg);
Chris Lattnerd03ed8f2008-10-15 05:38:32 +00001349 ArgVals.push_back(*i);
Evan Cheng8016fb82008-09-07 09:09:33 +00001350 ArgVTs.push_back(ArgVT);
1351 ArgFlags.push_back(Flags);
1352 }
1353
1354 // Analyze operands of the call, assigning locations to each operand.
1355 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersona0167022009-07-09 17:57:24 +00001356 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Evan Cheng8016fb82008-09-07 09:09:33 +00001357 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1358
1359 // Get a count of how many bytes are to be pushed on the stack.
1360 unsigned NumBytes = CCInfo.getNextStackOffset();
1361
1362 // Issue CALLSEQ_START
Dan Gohman01c9f772008-10-01 18:28:06 +00001363 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001364 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Cheng8016fb82008-09-07 09:09:33 +00001365
Chris Lattner65839062008-10-15 05:30:52 +00001366 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng8016fb82008-09-07 09:09:33 +00001367 // copies / loads.
1368 SmallVector<unsigned, 4> RegArgs;
1369 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1370 CCValAssign &VA = ArgLocs[i];
1371 unsigned Arg = Args[VA.getValNo()];
Owen Andersonac9de032009-08-10 22:56:29 +00001372 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Cheng8016fb82008-09-07 09:09:33 +00001373
1374 // Promote the value if needed.
1375 switch (VA.getLocInfo()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001376 default: llvm_unreachable("Unknown loc info!");
Evan Cheng8016fb82008-09-07 09:09:33 +00001377 case CCValAssign::Full: break;
Evan Cheng2a719f82008-09-08 06:35:17 +00001378 case CCValAssign::SExt: {
1379 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1380 Arg, ArgVT, Arg);
Chris Lattnerdf6f56d2008-12-19 17:03:38 +00001381 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelc57694a2008-12-23 21:56:28 +00001382 Emitted = true;
Evan Cheng2a719f82008-09-08 06:35:17 +00001383 ArgVT = VA.getLocVT();
Evan Cheng8016fb82008-09-07 09:09:33 +00001384 break;
Evan Cheng2a719f82008-09-08 06:35:17 +00001385 }
1386 case CCValAssign::ZExt: {
1387 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1388 Arg, ArgVT, Arg);
Chris Lattnerdf6f56d2008-12-19 17:03:38 +00001389 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelc57694a2008-12-23 21:56:28 +00001390 Emitted = true;
Evan Cheng2a719f82008-09-08 06:35:17 +00001391 ArgVT = VA.getLocVT();
Evan Cheng8016fb82008-09-07 09:09:33 +00001392 break;
Evan Cheng2a719f82008-09-08 06:35:17 +00001393 }
1394 case CCValAssign::AExt: {
1395 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1396 Arg, ArgVT, Arg);
Owen Anderson307fded2008-09-11 02:41:37 +00001397 if (!Emitted)
1398 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnerffba2be2008-10-15 05:07:36 +00001399 Arg, ArgVT, Arg);
Owen Anderson307fded2008-09-11 02:41:37 +00001400 if (!Emitted)
1401 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1402 Arg, ArgVT, Arg);
1403
Chris Lattnerdf6f56d2008-12-19 17:03:38 +00001404 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng2a719f82008-09-08 06:35:17 +00001405 ArgVT = VA.getLocVT();
Evan Cheng8016fb82008-09-07 09:09:33 +00001406 break;
1407 }
Dan Gohman4f0b1fe2009-08-05 05:33:42 +00001408 case CCValAssign::BCvt: {
1409 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
1410 ISD::BIT_CONVERT, Arg);
1411 assert(BC != 0 && "Failed to emit a bitcast!");
1412 Arg = BC;
1413 ArgVT = VA.getLocVT();
1414 break;
1415 }
Evan Cheng2a719f82008-09-08 06:35:17 +00001416 }
Evan Cheng8016fb82008-09-07 09:09:33 +00001417
1418 if (VA.isRegLoc()) {
1419 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1420 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1421 Arg, RC, RC);
Chris Lattnerdf6f56d2008-12-19 17:03:38 +00001422 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelc57694a2008-12-23 21:56:28 +00001423 Emitted = true;
Evan Cheng8016fb82008-09-07 09:09:33 +00001424 RegArgs.push_back(VA.getLocReg());
1425 } else {
1426 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohmand6211a72008-09-10 20:11:02 +00001427 X86AddressMode AM;
1428 AM.Base.Reg = StackPtr;
1429 AM.Disp = LocMemOffset;
Chris Lattnerd03ed8f2008-10-15 05:38:32 +00001430 Value *ArgVal = ArgVals[VA.getValNo()];
1431
1432 // If this is a really simple value, emit this with the Value* version of
1433 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1434 // can cause us to reevaluate the argument.
1435 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1436 X86FastEmitStore(ArgVT, ArgVal, AM);
1437 else
1438 X86FastEmitStore(ArgVT, Arg, AM);
Evan Cheng8016fb82008-09-07 09:09:33 +00001439 }
1440 }
1441
Dan Gohman9039d6b2008-09-25 15:24:26 +00001442 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1443 // GOT pointer.
Chris Lattner5d1f2572009-07-09 04:39:06 +00001444 if (Subtarget->isPICStyleGOT()) {
Dan Gohman9039d6b2008-09-25 15:24:26 +00001445 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman882ab732008-09-30 00:58:23 +00001446 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman9039d6b2008-09-25 15:24:26 +00001447 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
Chris Lattnerdf6f56d2008-12-19 17:03:38 +00001448 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelc57694a2008-12-23 21:56:28 +00001449 Emitted = true;
Dan Gohman9039d6b2008-09-25 15:24:26 +00001450 }
Chris Lattnerc9ddbc22009-07-09 06:34:26 +00001451
Evan Cheng8016fb82008-09-07 09:09:33 +00001452 // Issue the call.
Chris Lattnerc9ddbc22009-07-09 06:34:26 +00001453 MachineInstrBuilder MIB;
1454 if (CalleeOp) {
1455 // Register-indirect call.
1456 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
1457 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
1458
1459 } else {
1460 // Direct call.
1461 assert(GV && "Not a direct call");
1462 unsigned CallOpc =
1463 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1464
1465 // See if we need any target-specific flags on the GV operand.
1466 unsigned char OpFlags = 0;
1467
1468 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1469 // external symbols most go through the PLT in PIC mode. If the symbol
1470 // has hidden or protected visibility, or if it is static or local, then
1471 // we don't need to use the PLT - we can directly call it.
1472 if (Subtarget->isTargetELF() &&
1473 TM.getRelocationModel() == Reloc::PIC_ &&
1474 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1475 OpFlags = X86II::MO_PLT;
Chris Lattner4a948932009-07-10 20:47:30 +00001476 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc9ddbc22009-07-09 06:34:26 +00001477 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1478 Subtarget->getDarwinVers() < 9) {
1479 // PC-relative references to external symbols should go through $stub,
1480 // unless we're building with the leopard linker or later, which
1481 // automatically synthesizes these stubs.
1482 OpFlags = X86II::MO_DARWIN_STUB;
1483 }
1484
1485
1486 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
1487 }
Dan Gohman9039d6b2008-09-25 15:24:26 +00001488
1489 // Add an implicit use GOT pointer in EBX.
Chris Lattner5d1f2572009-07-09 04:39:06 +00001490 if (Subtarget->isPICStyleGOT())
Dan Gohman9039d6b2008-09-25 15:24:26 +00001491 MIB.addReg(X86::EBX);
1492
Evan Cheng8016fb82008-09-07 09:09:33 +00001493 // Add implicit physical register uses to the call.
Dan Gohman831db402008-10-07 22:10:33 +00001494 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1495 MIB.addReg(RegArgs[i]);
Evan Cheng8016fb82008-09-07 09:09:33 +00001496
1497 // Issue CALLSEQ_END
Dan Gohman01c9f772008-10-01 18:28:06 +00001498 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001499 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Cheng8016fb82008-09-07 09:09:33 +00001500
1501 // Now handle call return value (if any).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001502 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Cheng8016fb82008-09-07 09:09:33 +00001503 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersona0167022009-07-09 17:57:24 +00001504 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Cheng8016fb82008-09-07 09:09:33 +00001505 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1506
1507 // Copy all of the result registers out of their specified physreg.
1508 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersonac9de032009-08-10 22:56:29 +00001509 EVT CopyVT = RVLocs[0].getValVT();
Evan Cheng8016fb82008-09-07 09:09:33 +00001510 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1511 TargetRegisterClass *SrcRC = DstRC;
1512
1513 // If this is a call to a function that returns an fp value on the x87 fp
1514 // stack, but where we prefer to use the value in xmm registers, copy it
1515 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1516 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1517 RVLocs[0].getLocReg() == X86::ST1) &&
1518 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001519 CopyVT = MVT::f80;
Evan Cheng8016fb82008-09-07 09:09:33 +00001520 SrcRC = X86::RSTRegisterClass;
1521 DstRC = X86::RFP80RegisterClass;
1522 }
1523
1524 unsigned ResultReg = createResultReg(DstRC);
1525 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1526 RVLocs[0].getLocReg(), DstRC, SrcRC);
Chris Lattnerdf6f56d2008-12-19 17:03:38 +00001527 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelc57694a2008-12-23 21:56:28 +00001528 Emitted = true;
Evan Cheng8016fb82008-09-07 09:09:33 +00001529 if (CopyVT != RVLocs[0].getValVT()) {
1530 // Round the F80 the right size, which also moves to the appropriate xmm
1531 // register. This is accomplished by storing the F80 value in memory and
1532 // then loading it back. Ewww...
Owen Andersonac9de032009-08-10 22:56:29 +00001533 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001534 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng8016fb82008-09-07 09:09:33 +00001535 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene6424ab92009-11-12 20:49:22 +00001536 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001537 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001538 DstRC = ResVT == MVT::f32
Evan Cheng8016fb82008-09-07 09:09:33 +00001539 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001540 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Cheng8016fb82008-09-07 09:09:33 +00001541 ResultReg = createResultReg(DstRC);
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001542 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Cheng8016fb82008-09-07 09:09:33 +00001543 }
1544
Evan Cheng52001032008-09-08 17:15:42 +00001545 if (AndToI1) {
1546 // Mask out all but lowest bit for some call which produces an i1.
1547 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dale Johannesen960bfbd2009-02-13 02:33:27 +00001548 BuildMI(MBB, DL,
1549 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Cheng52001032008-09-08 17:15:42 +00001550 ResultReg = AndResult;
1551 }
1552
Evan Cheng8016fb82008-09-07 09:09:33 +00001553 UpdateValueMap(I, ResultReg);
1554 }
1555
1556 return true;
1557}
1558
1559
Dan Gohmancb9b4d32008-08-28 23:21:34 +00001560bool
Dan Gohmanca4857a2008-09-03 23:12:08 +00001561X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohmancb9b4d32008-08-28 23:21:34 +00001562 switch (I->getOpcode()) {
1563 default: break;
Evan Cheng8700bb92008-09-03 06:44:39 +00001564 case Instruction::Load:
Dan Gohmanca4857a2008-09-03 23:12:08 +00001565 return X86SelectLoad(I);
Owen Andersona2a90a02008-09-04 16:48:33 +00001566 case Instruction::Store:
1567 return X86SelectStore(I);
Dan Gohmane1cdaa62008-09-04 23:26:51 +00001568 case Instruction::ICmp:
1569 case Instruction::FCmp:
1570 return X86SelectCmp(I);
Dan Gohman4a7d4d62008-09-05 01:06:14 +00001571 case Instruction::ZExt:
1572 return X86SelectZExt(I);
1573 case Instruction::Br:
1574 return X86SelectBranch(I);
Evan Cheng8016fb82008-09-07 09:09:33 +00001575 case Instruction::Call:
1576 return X86SelectCall(I);
Evan Chengb10ba152010-01-11 22:59:27 +00001577 case Instruction::Or:
1578 return X86SelectOR(I);
Dan Gohman9ba59f82008-09-05 18:30:08 +00001579 case Instruction::LShr:
1580 case Instruction::AShr:
1581 case Instruction::Shl:
1582 return X86SelectShift(I);
1583 case Instruction::Select:
1584 return X86SelectSelect(I);
Evan Cheng303530d2008-09-07 08:47:42 +00001585 case Instruction::Trunc:
1586 return X86SelectTrunc(I);
Dan Gohman658637e2008-09-10 21:02:08 +00001587 case Instruction::FPExt:
1588 return X86SelectFPExt(I);
1589 case Instruction::FPTrunc:
1590 return X86SelectFPTrunc(I);
Bill Wendling33fe51e2008-12-09 02:42:50 +00001591 case Instruction::ExtractValue:
1592 return X86SelectExtractValue(I);
Dan Gohman94fc47a2009-03-13 23:53:06 +00001593 case Instruction::IntToPtr: // Deliberate fall-through.
1594 case Instruction::PtrToInt: {
Owen Andersonac9de032009-08-10 22:56:29 +00001595 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1596 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman94fc47a2009-03-13 23:53:06 +00001597 if (DstVT.bitsGT(SrcVT))
1598 return X86SelectZExt(I);
1599 if (DstVT.bitsLT(SrcVT))
1600 return X86SelectTrunc(I);
1601 unsigned Reg = getRegForValue(I->getOperand(0));
1602 if (Reg == 0) return false;
1603 UpdateValueMap(I, Reg);
1604 return true;
1605 }
Dan Gohmancb9b4d32008-08-28 23:21:34 +00001606 }
1607
1608 return false;
1609}
1610
Dan Gohmand6211a72008-09-10 20:11:02 +00001611unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Andersonac9de032009-08-10 22:56:29 +00001612 EVT VT;
Chris Lattnerffba2be2008-10-15 05:07:36 +00001613 if (!isTypeLegal(C->getType(), VT))
Owen Anderson51f958e2008-09-05 00:06:23 +00001614 return false;
1615
1616 // Get opcode and regclass of the output for the given load instruction.
1617 unsigned Opc = 0;
1618 const TargetRegisterClass *RC = NULL;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001619 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson51f958e2008-09-05 00:06:23 +00001620 default: return false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001621 case MVT::i8:
Owen Anderson51f958e2008-09-05 00:06:23 +00001622 Opc = X86::MOV8rm;
1623 RC = X86::GR8RegisterClass;
1624 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001625 case MVT::i16:
Owen Anderson51f958e2008-09-05 00:06:23 +00001626 Opc = X86::MOV16rm;
1627 RC = X86::GR16RegisterClass;
1628 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001629 case MVT::i32:
Owen Anderson51f958e2008-09-05 00:06:23 +00001630 Opc = X86::MOV32rm;
1631 RC = X86::GR32RegisterClass;
1632 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001633 case MVT::i64:
Owen Anderson51f958e2008-09-05 00:06:23 +00001634 // Must be in x86-64 mode.
1635 Opc = X86::MOV64rm;
1636 RC = X86::GR64RegisterClass;
1637 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001638 case MVT::f32:
Owen Anderson51f958e2008-09-05 00:06:23 +00001639 if (Subtarget->hasSSE1()) {
1640 Opc = X86::MOVSSrm;
1641 RC = X86::FR32RegisterClass;
1642 } else {
1643 Opc = X86::LD_Fp32m;
1644 RC = X86::RFP32RegisterClass;
1645 }
1646 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001647 case MVT::f64:
Owen Anderson51f958e2008-09-05 00:06:23 +00001648 if (Subtarget->hasSSE2()) {
1649 Opc = X86::MOVSDrm;
1650 RC = X86::FR64RegisterClass;
1651 } else {
1652 Opc = X86::LD_Fp64m;
1653 RC = X86::RFP64RegisterClass;
1654 }
1655 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001656 case MVT::f80:
Dan Gohman17a47142008-09-26 01:39:32 +00001657 // No f80 support yet.
1658 return false;
Owen Anderson51f958e2008-09-05 00:06:23 +00001659 }
1660
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001661 // Materialize addresses with LEA instructions.
Owen Anderson51f958e2008-09-05 00:06:23 +00001662 if (isa<GlobalValue>(C)) {
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001663 X86AddressMode AM;
Chris Lattner4ba2d032009-07-10 05:33:42 +00001664 if (X86SelectAddress(C, AM)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001665 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001666 Opc = X86::LEA32r;
1667 else
1668 Opc = X86::LEA64r;
1669 unsigned ResultReg = createResultReg(RC);
Rafael Espindolabca99f72009-04-08 21:14:34 +00001670 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson51f958e2008-09-05 00:06:23 +00001671 return ResultReg;
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001672 }
Evan Chengcb5422c2008-09-05 21:00:03 +00001673 return 0;
Owen Anderson51f958e2008-09-05 00:06:23 +00001674 }
1675
Owen Anderson64284aa2008-09-06 01:11:01 +00001676 // MachineConstantPool wants an explicit alignment.
Evan Cheng68c18682009-03-13 07:51:59 +00001677 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson64284aa2008-09-06 01:11:01 +00001678 if (Align == 0) {
1679 // Alignment of vector types. FIXME!
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001680 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson64284aa2008-09-06 01:11:01 +00001681 }
Owen Anderson51f958e2008-09-05 00:06:23 +00001682
Dan Gohmanf644a762008-09-30 01:21:32 +00001683 // x86-32 PIC requires a PIC base register for constant pools.
1684 unsigned PICBase = 0;
Chris Lattner83707ca2009-06-27 01:31:51 +00001685 unsigned char OpFlag = 0;
Chris Lattner2e9393c2009-07-10 21:00:45 +00001686 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner5d1f2572009-07-09 04:39:06 +00001687 OpFlag = X86II::MO_PIC_BASE_OFFSET;
1688 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1689 } else if (Subtarget->isPICStyleGOT()) {
1690 OpFlag = X86II::MO_GOTOFF;
1691 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1692 } else if (Subtarget->isPICStyleRIPRel() &&
1693 TM.getCodeModel() == CodeModel::Small) {
1694 PICBase = X86::RIP;
Chris Lattner83707ca2009-06-27 01:31:51 +00001695 }
Dan Gohmanf644a762008-09-30 01:21:32 +00001696
1697 // Create the load from the constant pool.
Dan Gohmand6211a72008-09-10 20:11:02 +00001698 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman0dd5fd92008-09-19 22:16:54 +00001699 unsigned ResultReg = createResultReg(RC);
Chris Lattner83707ca2009-06-27 01:31:51 +00001700 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
1701 MCPOffset, PICBase, OpFlag);
Dan Gohmanf644a762008-09-30 01:21:32 +00001702
Owen Anderson51f958e2008-09-05 00:06:23 +00001703 return ResultReg;
1704}
1705
Dan Gohmand6211a72008-09-10 20:11:02 +00001706unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman70b46c82008-10-03 01:27:49 +00001707 // Fail on dynamic allocas. At this point, getRegForValue has already
1708 // checked its CSE maps, so if we're here trying to handle a dynamic
1709 // alloca, we're not going to succeed. X86SelectAddress has a
1710 // check for dynamic allocas, because it's called directly from
1711 // various places, but TargetMaterializeAlloca also needs a check
1712 // in order to avoid recursion between getRegForValue,
1713 // X86SelectAddrss, and TargetMaterializeAlloca.
1714 if (!StaticAllocaMap.count(C))
1715 return 0;
1716
Dan Gohmand6211a72008-09-10 20:11:02 +00001717 X86AddressMode AM;
Chris Lattner4ba2d032009-07-10 05:33:42 +00001718 if (!X86SelectAddress(C, AM))
Dan Gohmand6211a72008-09-10 20:11:02 +00001719 return 0;
1720 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1721 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1722 unsigned ResultReg = createResultReg(RC);
Rafael Espindolabca99f72009-04-08 21:14:34 +00001723 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohmand6211a72008-09-10 20:11:02 +00001724 return ResultReg;
1725}
1726
Evan Cheng5a0f5912008-09-03 00:03:49 +00001727namespace llvm {
Dan Gohmanca4857a2008-09-03 23:12:08 +00001728 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001729 MachineModuleInfo *mmi,
Devang Patelfcf1c752009-01-13 00:35:13 +00001730 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001731 DenseMap<const Value *, unsigned> &vm,
Dan Gohmand6211a72008-09-10 20:11:02 +00001732 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001733 DenseMap<const AllocaInst *, int> &am
1734#ifndef NDEBUG
1735 , SmallSet<Instruction*, 8> &cil
1736#endif
1737 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00001738 return new X86FastISel(mf, mmi, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00001739#ifndef NDEBUG
1740 , cil
1741#endif
1742 );
Evan Cheng5a0f5912008-09-03 00:03:49 +00001743 }
Dan Gohmancb9b4d32008-08-28 23:21:34 +00001744}