blob: 153edea4f49c184382a8553fddc9f86be9bcadad [file] [log] [blame]
Dan Gohman1adf1b02008-08-19 21:45:35 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
Evan Cheng8b19e562008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Evan Cheng88e30412008-09-03 01:04:47 +000018#include "X86RegisterInfo.h"
19#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000020#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000021#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000022#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000023#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000025#include "llvm/IntrinsicInst.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000026#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000027#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000031#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000033#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000034#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000035using namespace llvm;
36
Chris Lattner087fcf32009-03-08 18:44:31 +000037namespace {
38
Evan Chengc3f44b02008-09-03 00:03:49 +000039class X86FastISel : public FastISel {
40 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
41 /// make the right decision when generating code for different targets.
42 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000043
44 /// StackPtr - Register used as the stack pointer.
45 ///
46 unsigned StackPtr;
47
48 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
49 /// floating point ops.
50 /// When SSE is available, use it for f32 operations.
51 /// When SSE2 is available, use it for f64 operations.
52 bool X86ScalarSSEf64;
53 bool X86ScalarSSEf32;
54
Evan Cheng8b19e562008-09-03 06:44:39 +000055public:
Dan Gohmana4160c32010-07-07 16:29:44 +000056 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000057 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000058 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
59 X86ScalarSSEf64 = Subtarget->hasSSE2();
60 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000061 }
Evan Chengc3f44b02008-09-03 00:03:49 +000062
Dan Gohman46510a72010-04-15 01:51:59 +000063 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000064
Dan Gohman1adf1b02008-08-19 21:45:35 +000065#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000066
67private:
Dan Gohman46510a72010-04-15 01:51:59 +000068 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000069
Owen Andersone50ed302009-08-10 22:56:29 +000070 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000071
Dan Gohman46510a72010-04-15 01:51:59 +000072 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000073 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000074 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000075 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000076
Owen Andersone50ed302009-08-10 22:56:29 +000077 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000078 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000079
Dan Gohman46510a72010-04-15 01:51:59 +000080 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
81 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000082
Dan Gohman46510a72010-04-15 01:51:59 +000083 bool X86SelectLoad(const Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000084
Dan Gohman46510a72010-04-15 01:51:59 +000085 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000086
Dan Gohman46510a72010-04-15 01:51:59 +000087 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000088
Dan Gohman46510a72010-04-15 01:51:59 +000089 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000090
Dan Gohman46510a72010-04-15 01:51:59 +000091 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000092
Dan Gohman46510a72010-04-15 01:51:59 +000093 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000094
Dan Gohman46510a72010-04-15 01:51:59 +000095 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000096
Dan Gohman46510a72010-04-15 01:51:59 +000097 bool X86SelectTrunc(const Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +000098
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectFPExt(const Instruction *I);
100 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
105 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000106
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000107 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000108
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000109 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000110 return getTargetMachine()->getInstrInfo();
111 }
112 const X86TargetMachine *getTargetMachine() const {
113 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000114 }
115
Dan Gohman46510a72010-04-15 01:51:59 +0000116 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000117
Dan Gohman46510a72010-04-15 01:51:59 +0000118 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000119
120 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
121 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000122 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
124 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000125 }
126
Owen Andersone50ed302009-08-10 22:56:29 +0000127 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000128};
Chris Lattner087fcf32009-03-08 18:44:31 +0000129
130} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000131
Owen Andersone50ed302009-08-10 22:56:29 +0000132bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000133 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000135 // Unhandled type. Halt "fast" selection and bail.
136 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000137
Dan Gohman9b66d732008-09-30 00:48:39 +0000138 // For now, require SSE/SSE2 for performing floating-point operations,
139 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000141 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000143 return false;
144 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000146 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000147 // We only handle legal types. For example, on x86-32 the instruction
148 // selector contains all of the 64-bit instructions from x86-64,
149 // under the assumption that i64 won't be used if the target doesn't
150 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000152}
153
154#include "X86GenCallingConv.inc"
155
156/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
157/// convention.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000158CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
159 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000160 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +0000161 if (CC == CallingConv::GHC)
162 return CC_X86_64_GHC;
163 else if (Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000164 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000165 else
166 return CC_X86_64_C;
167 }
168
169 if (CC == CallingConv::X86_FastCall)
170 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000171 else if (CC == CallingConv::X86_ThisCall)
172 return CC_X86_32_ThisCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000173 else if (CC == CallingConv::Fast)
174 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +0000175 else if (CC == CallingConv::GHC)
176 return CC_X86_32_GHC;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177 else
178 return CC_X86_32_C;
179}
180
Evan Cheng0de588f2008-09-05 21:00:03 +0000181/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000182/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000183/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000184bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000185 unsigned &ResultReg) {
186 // Get opcode and regclass of the output for the given load instruction.
187 unsigned Opc = 0;
188 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000190 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000191 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 Opc = X86::MOV8rm;
194 RC = X86::GR8RegisterClass;
195 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 Opc = X86::MOV16rm;
198 RC = X86::GR16RegisterClass;
199 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000201 Opc = X86::MOV32rm;
202 RC = X86::GR32RegisterClass;
203 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000205 // Must be in x86-64 mode.
206 Opc = X86::MOV64rm;
207 RC = X86::GR64RegisterClass;
208 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000210 if (Subtarget->hasSSE1()) {
211 Opc = X86::MOVSSrm;
212 RC = X86::FR32RegisterClass;
213 } else {
214 Opc = X86::LD_Fp32m;
215 RC = X86::RFP32RegisterClass;
216 }
217 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000219 if (Subtarget->hasSSE2()) {
220 Opc = X86::MOVSDrm;
221 RC = X86::FR64RegisterClass;
222 } else {
223 Opc = X86::LD_Fp64m;
224 RC = X86::RFP64RegisterClass;
225 }
226 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000228 // No f80 support yet.
229 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000230 }
231
232 ResultReg = createResultReg(RC);
Dan Gohmanf5951412010-07-08 01:00:56 +0000233 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000234 return true;
235}
236
Evan Chengf3d4efe2008-09-07 09:09:33 +0000237/// X86FastEmitStore - Emit a machine instruction to store a value Val of
238/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
239/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000240/// i.e. V. Return true if it is possible.
241bool
Owen Andersone50ed302009-08-10 22:56:29 +0000242X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000243 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000244 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000245 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 switch (VT.getSimpleVT().SimpleTy) {
247 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000248 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000249 case MVT::i1: {
250 // Mask out all but lowest bit.
251 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohmanf5951412010-07-08 01:00:56 +0000252 BuildMI(MBB, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000253 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
254 Val = AndResult;
255 }
256 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 case MVT::i8: Opc = X86::MOV8mr; break;
258 case MVT::i16: Opc = X86::MOV16mr; break;
259 case MVT::i32: Opc = X86::MOV32mr; break;
260 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
261 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000262 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000263 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000265 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000266 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000267 }
Chris Lattner438949a2008-10-15 05:30:52 +0000268
Dan Gohmanf5951412010-07-08 01:00:56 +0000269 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000270 return true;
271}
272
Dan Gohman46510a72010-04-15 01:51:59 +0000273bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000274 const X86AddressMode &AM) {
275 // Handle 'null' like i32/i64 0.
276 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000277 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000278
279 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000280 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000281 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000282 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000284 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000285 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 case MVT::i8: Opc = X86::MOV8mi; break;
287 case MVT::i16: Opc = X86::MOV16mi; break;
288 case MVT::i32: Opc = X86::MOV32mi; break;
289 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000290 // Must be a 32-bit sign extended value.
291 if ((int)CI->getSExtValue() == CI->getSExtValue())
292 Opc = X86::MOV64mi32;
293 break;
294 }
295
296 if (Opc) {
Dan Gohmanf5951412010-07-08 01:00:56 +0000297 addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000298 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000299 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000300 return true;
301 }
302 }
303
304 unsigned ValReg = getRegForValue(Val);
305 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000306 return false;
307
308 return X86FastEmitStore(VT, ValReg, AM);
309}
310
Evan Cheng24e3a902008-09-08 06:35:17 +0000311/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
312/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
313/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000314bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
315 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000316 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000317 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
318 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000319
320 if (RR != 0) {
321 ResultReg = RR;
322 return true;
323 } else
324 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000325}
326
Dan Gohman0586d912008-09-10 20:11:02 +0000327/// X86SelectAddress - Attempt to fill in an address from the given value.
328///
Dan Gohman46510a72010-04-15 01:51:59 +0000329bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
330 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000331 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000332 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000333 // Don't walk into other basic blocks; it's possible we haven't
334 // visited them yet, so the instructions may not yet be assigned
335 // virtual registers.
Dan Gohmanf5951412010-07-08 01:00:56 +0000336 if (FuncInfo.MBBMap[I->getParent()] != MBB)
Dan Gohmanea9f1512010-06-18 20:44:47 +0000337 return false;
338
Dan Gohman35893082008-09-18 23:23:44 +0000339 Opcode = I->getOpcode();
340 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000341 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000342 Opcode = C->getOpcode();
343 U = C;
344 }
Dan Gohman0586d912008-09-10 20:11:02 +0000345
Chris Lattner868ee942010-06-15 19:08:40 +0000346 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
347 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000348 // Fast instruction selection doesn't support the special
349 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000350 return false;
351
Dan Gohman35893082008-09-18 23:23:44 +0000352 switch (Opcode) {
353 default: break;
354 case Instruction::BitCast:
355 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000356 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000357
358 case Instruction::IntToPtr:
359 // Look past no-op inttoptrs.
360 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000361 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000362 break;
Dan Gohman35893082008-09-18 23:23:44 +0000363
364 case Instruction::PtrToInt:
365 // Look past no-op ptrtoints.
366 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000367 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000368 break;
Dan Gohman35893082008-09-18 23:23:44 +0000369
370 case Instruction::Alloca: {
371 // Do static allocas.
372 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000373 DenseMap<const AllocaInst*, int>::iterator SI =
374 FuncInfo.StaticAllocaMap.find(A);
375 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000376 AM.BaseType = X86AddressMode::FrameIndexBase;
377 AM.Base.FrameIndex = SI->second;
378 return true;
379 }
380 break;
Dan Gohman35893082008-09-18 23:23:44 +0000381 }
382
383 case Instruction::Add: {
384 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000385 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000386 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
387 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000388 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000389 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000390 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000391 }
Dan Gohman0586d912008-09-10 20:11:02 +0000392 }
Dan Gohman35893082008-09-18 23:23:44 +0000393 break;
394 }
395
396 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000397 X86AddressMode SavedAM = AM;
398
Dan Gohman35893082008-09-18 23:23:44 +0000399 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000400 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000401 unsigned IndexReg = AM.IndexReg;
402 unsigned Scale = AM.Scale;
403 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000404 // Iterate through the indices, folding what we can. Constants can be
405 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000406 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000407 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000408 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000409 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
410 const StructLayout *SL = TD.getStructLayout(STy);
411 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
412 Disp += SL->getElementOffset(Idx);
413 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000414 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman5c87bf62010-07-01 02:27:15 +0000415 SmallVector<const Value *, 4> Worklist;
416 Worklist.push_back(Op);
417 do {
418 Op = Worklist.pop_back_val();
419 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
420 // Constant-offset addressing.
421 Disp += CI->getSExtValue() * S;
Dan Gohmanabd1d852010-07-01 02:58:21 +0000422 } else if (isa<AddOperator>(Op) &&
423 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
424 // An add with a constant operand. Fold the constant.
425 ConstantInt *CI =
426 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
427 Disp += CI->getSExtValue() * S;
428 // Add the other operand back to the work list.
429 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
Dan Gohman5c87bf62010-07-01 02:27:15 +0000430 } else if (IndexReg == 0 &&
431 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
432 (S == 1 || S == 2 || S == 4 || S == 8)) {
433 // Scaled-index addressing.
434 Scale = S;
435 IndexReg = getRegForGEPIndex(Op).first;
436 if (IndexReg == 0)
437 return false;
Dan Gohman5c87bf62010-07-01 02:27:15 +0000438 } else
439 // Unsupported.
440 goto unsupported_gep;
441 } while (!Worklist.empty());
Dan Gohman35893082008-09-18 23:23:44 +0000442 }
443 }
Dan Gohman09aae462008-09-26 20:04:15 +0000444 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000445 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000446 break;
Dan Gohman35893082008-09-18 23:23:44 +0000447 // Ok, the GEP indices were covered by constant-offset and scaled-index
448 // addressing. Update the address state and move on to examining the base.
449 AM.IndexReg = IndexReg;
450 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000451 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000452 if (X86SelectAddress(U->getOperand(0), AM))
453 return true;
454
455 // If we couldn't merge the sub value into this addr mode, revert back to
456 // our address and just match the value instead of completely failing.
457 AM = SavedAM;
458 break;
Dan Gohman35893082008-09-18 23:23:44 +0000459 unsupported_gep:
460 // Ok, the GEP indices weren't all covered.
461 break;
462 }
463 }
464
465 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000466 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000467 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000468 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000469 return false;
470
Dan Gohman97135e12008-09-26 19:15:30 +0000471 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000472 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000473 (AM.Base.Reg != 0 || AM.IndexReg != 0))
474 return false;
475
Dan Gohmane9865942009-02-23 22:03:08 +0000476 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000477 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000478 if (GVar->isThreadLocal())
479 return false;
480
Chris Lattnerff7727f2009-07-09 06:41:35 +0000481 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000482 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000483
Chris Lattner0d786dd2009-07-10 07:48:51 +0000484 // Allow the subtarget to classify the global.
485 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
486
487 // If this reference is relative to the pic base, set it now.
488 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000489 // FIXME: How do we know Base.Reg is free??
Dan Gohmana4160c32010-07-07 16:29:44 +0000490 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000491 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000492
493 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000494 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000495 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000496 if (Subtarget->isPICStyleRIPRel()) {
497 // Use rip-relative addressing if we can. Above we verified that the
498 // base and index registers are unused.
499 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
500 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000501 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000502 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000503 return true;
504 }
505
Chris Lattner0d786dd2009-07-10 07:48:51 +0000506 // Ok, we need to do a load from a stub. If we've already loaded from this
507 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000508 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
509 unsigned LoadReg;
510 if (I != LocalValueMap.end() && I->second != 0) {
511 LoadReg = I->second;
512 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000513 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000514 unsigned Opc = 0;
515 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000516 X86AddressMode StubAM;
517 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000518 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000519 StubAM.GVOpFlags = GVFlags;
520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000522 Opc = X86::MOV64rm;
523 RC = X86::GR64RegisterClass;
524
Chris Lattner0d786dd2009-07-10 07:48:51 +0000525 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000526 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000527 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000528 Opc = X86::MOV32rm;
529 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000530 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000531
532 LoadReg = createResultReg(RC);
Dan Gohmanf5951412010-07-08 01:00:56 +0000533 addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
Chris Lattnerff7727f2009-07-09 06:41:35 +0000534
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000535 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000536 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000537 }
Chris Lattner18c59872009-06-27 04:16:01 +0000538
Chris Lattnerff7727f2009-07-09 06:41:35 +0000539 // Now construct the final address. Note that the Disp, Scale,
540 // and Index values may already be set here.
541 AM.Base.Reg = LoadReg;
542 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000543 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000544 }
545
Dan Gohman97135e12008-09-26 19:15:30 +0000546 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000547 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000548 if (AM.Base.Reg == 0) {
549 AM.Base.Reg = getRegForValue(V);
550 return AM.Base.Reg != 0;
551 }
552 if (AM.IndexReg == 0) {
553 assert(AM.Scale == 1 && "Scale with no index!");
554 AM.IndexReg = getRegForValue(V);
555 return AM.IndexReg != 0;
556 }
557 }
558
559 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000560}
561
Chris Lattner0aa43de2009-07-10 05:33:42 +0000562/// X86SelectCallAddress - Attempt to fill in an address from the given value.
563///
Dan Gohman46510a72010-04-15 01:51:59 +0000564bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
565 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000566 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000567 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000568 Opcode = I->getOpcode();
569 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000570 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000571 Opcode = C->getOpcode();
572 U = C;
573 }
574
575 switch (Opcode) {
576 default: break;
577 case Instruction::BitCast:
578 // Look past bitcasts.
579 return X86SelectCallAddress(U->getOperand(0), AM);
580
581 case Instruction::IntToPtr:
582 // Look past no-op inttoptrs.
583 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
584 return X86SelectCallAddress(U->getOperand(0), AM);
585 break;
586
587 case Instruction::PtrToInt:
588 // Look past no-op ptrtoints.
589 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
590 return X86SelectCallAddress(U->getOperand(0), AM);
591 break;
592 }
593
594 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000595 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000596 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000597 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000598 return false;
599
600 // RIP-relative addresses can't have additional register operands.
601 if (Subtarget->isPICStyleRIPRel() &&
602 (AM.Base.Reg != 0 || AM.IndexReg != 0))
603 return false;
604
Chris Lattner754b7652009-07-10 05:48:03 +0000605 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000606 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000607 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000608 return false;
609
610 // Okay, we've committed to selecting this global. Set up the basic address.
611 AM.GV = GV;
612
Chris Lattnere6c07b52009-07-10 05:45:15 +0000613 // No ABI requires an extra load for anything other than DLLImport, which
614 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000615 if (Subtarget->isPICStyleRIPRel()) {
616 // Use rip-relative addressing if we can. Above we verified that the
617 // base and index registers are unused.
618 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
619 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000620 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000621 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
622 } else if (Subtarget->isPICStyleGOT()) {
623 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000624 }
625
Chris Lattner0aa43de2009-07-10 05:33:42 +0000626 return true;
627 }
628
629 // If all else fails, try to materialize the value in a register.
630 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
631 if (AM.Base.Reg == 0) {
632 AM.Base.Reg = getRegForValue(V);
633 return AM.Base.Reg != 0;
634 }
635 if (AM.IndexReg == 0) {
636 assert(AM.Scale == 1 && "Scale with no index!");
637 AM.IndexReg = getRegForValue(V);
638 return AM.IndexReg != 0;
639 }
640 }
641
642 return false;
643}
644
645
Owen Andersona3971df2008-09-04 07:08:58 +0000646/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000647bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000648 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000649 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000650 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000651
Dan Gohman0586d912008-09-10 20:11:02 +0000652 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000653 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000654 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000655
Chris Lattner438949a2008-10-15 05:30:52 +0000656 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000657}
658
Evan Cheng8b19e562008-09-03 06:44:39 +0000659/// X86SelectLoad - Select and emit code to implement load instructions.
660///
Dan Gohman46510a72010-04-15 01:51:59 +0000661bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000662 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000663 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000664 return false;
665
Dan Gohman0586d912008-09-10 20:11:02 +0000666 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000667 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000668 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000669
Evan Cheng0de588f2008-09-05 21:00:03 +0000670 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000671 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000672 UpdateValueMap(I, ResultReg);
673 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000674 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000675 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000676}
677
Owen Andersone50ed302009-08-10 22:56:29 +0000678static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000680 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 case MVT::i8: return X86::CMP8rr;
682 case MVT::i16: return X86::CMP16rr;
683 case MVT::i32: return X86::CMP32rr;
684 case MVT::i64: return X86::CMP64rr;
685 case MVT::f32: return X86::UCOMISSrr;
686 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000687 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000688}
689
Chris Lattner0e13c782008-10-15 04:13:29 +0000690/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
691/// of the comparison, return an opcode that works for the compare (e.g.
692/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000693static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000695 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000696 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 case MVT::i8: return X86::CMP8ri;
698 case MVT::i16: return X86::CMP16ri;
699 case MVT::i32: return X86::CMP32ri;
700 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000701 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
702 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000703 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000704 return X86::CMP64ri32;
705 return 0;
706 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000707}
708
Dan Gohman46510a72010-04-15 01:51:59 +0000709bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
710 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000711 unsigned Op0Reg = getRegForValue(Op0);
712 if (Op0Reg == 0) return false;
713
Chris Lattnerd53886b2008-10-15 05:18:04 +0000714 // Handle 'null' like i32/i64 0.
715 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000716 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000717
Chris Lattner9a08a612008-10-15 04:26:38 +0000718 // We have two options: compare with register or immediate. If the RHS of
719 // the compare is an immediate that we can fold into this compare, use
720 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000721 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000722 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohmanf5951412010-07-08 01:00:56 +0000723 BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
724 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000725 return true;
726 }
727 }
728
729 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
730 if (CompareOpc == 0) return false;
731
732 unsigned Op1Reg = getRegForValue(Op1);
733 if (Op1Reg == 0) return false;
Dan Gohmanf5951412010-07-08 01:00:56 +0000734 BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000735
736 return true;
737}
738
Dan Gohman46510a72010-04-15 01:51:59 +0000739bool X86FastISel::X86SelectCmp(const Instruction *I) {
740 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000741
Owen Andersone50ed302009-08-10 22:56:29 +0000742 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000743 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000744 return false;
745
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000746 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000747 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000748 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000749 switch (CI->getPredicate()) {
750 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000751 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
752 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000753
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000754 unsigned EReg = createResultReg(&X86::GR8RegClass);
755 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohmanf5951412010-07-08 01:00:56 +0000756 BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
757 BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
758 BuildMI(MBB, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000759 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000760 UpdateValueMap(I, ResultReg);
761 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000762 }
763 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000764 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
765 return false;
766
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000767 unsigned NEReg = createResultReg(&X86::GR8RegClass);
768 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohmanf5951412010-07-08 01:00:56 +0000769 BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
770 BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
771 BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000772 UpdateValueMap(I, ResultReg);
773 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000774 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000775 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
776 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
777 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
778 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
779 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
780 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
781 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
782 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
783 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
784 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
785 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
786 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
787
788 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
789 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
790 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
791 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
792 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
793 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
794 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
795 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
796 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
797 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000798 default:
799 return false;
800 }
801
Dan Gohman46510a72010-04-15 01:51:59 +0000802 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000803 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000804 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000805
Chris Lattner9a08a612008-10-15 04:26:38 +0000806 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000807 if (!X86FastEmitCompare(Op0, Op1, VT))
808 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000809
Dan Gohmanf5951412010-07-08 01:00:56 +0000810 BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000811 UpdateValueMap(I, ResultReg);
812 return true;
813}
Evan Cheng8b19e562008-09-03 06:44:39 +0000814
Dan Gohman46510a72010-04-15 01:51:59 +0000815bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000816 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000817 if (I->getType()->isIntegerTy(8) &&
818 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000819 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000820 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000821 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000822 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000823 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000824 UpdateValueMap(I, ResultReg);
825 return true;
826 }
827
828 return false;
829}
830
Chris Lattner9a08a612008-10-15 04:26:38 +0000831
Dan Gohman46510a72010-04-15 01:51:59 +0000832bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000833 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000834 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000835 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000836 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
837 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000838
Dan Gohmand98d6202008-10-02 22:15:21 +0000839 // Fold the common case of a conditional branch with a comparison.
Dan Gohman46510a72010-04-15 01:51:59 +0000840 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000841 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000842 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000843
Dan Gohmand98d6202008-10-02 22:15:21 +0000844 // Try to take advantage of fallthrough opportunities.
845 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmanf5951412010-07-08 01:00:56 +0000846 if (MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000847 std::swap(TrueMBB, FalseMBB);
848 Predicate = CmpInst::getInversePredicate(Predicate);
849 }
850
Chris Lattner871d2462008-10-15 03:58:05 +0000851 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
852 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
853
Dan Gohmand98d6202008-10-02 22:15:21 +0000854 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000855 case CmpInst::FCMP_OEQ:
856 std::swap(TrueMBB, FalseMBB);
857 Predicate = CmpInst::FCMP_UNE;
858 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000859 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
860 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
861 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
862 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
863 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
864 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
865 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
866 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
867 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
868 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
869 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
870 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
871 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000872
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000873 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
874 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
875 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
876 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
877 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
878 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
879 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
880 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
881 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
882 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000883 default:
884 return false;
885 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000886
Dan Gohman46510a72010-04-15 01:51:59 +0000887 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000888 if (SwapArgs)
889 std::swap(Op0, Op1);
890
Chris Lattner9a08a612008-10-15 04:26:38 +0000891 // Emit a compare of the LHS and RHS, setting the flags.
892 if (!X86FastEmitCompare(Op0, Op1, VT))
893 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000894
Dan Gohmanf5951412010-07-08 01:00:56 +0000895 BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000896
897 if (Predicate == CmpInst::FCMP_UNE) {
898 // X86 requires a second branch to handle UNE (and OEQ,
899 // which is mapped to UNE above).
Dan Gohmanf5951412010-07-08 01:00:56 +0000900 BuildMI(MBB, DL, TII.get(X86::JP_4)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000901 }
902
Stuart Hastings3bf91252010-06-17 22:43:56 +0000903 FastEmitBranch(FalseMBB, DL);
Dan Gohmanf5951412010-07-08 01:00:56 +0000904 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000905 return true;
906 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000907 } else if (ExtractValueInst *EI =
908 dyn_cast<ExtractValueInst>(BI->getCondition())) {
909 // Check to see if the branch instruction is from an "arithmetic with
910 // overflow" intrinsic. The main way these intrinsics are used is:
911 //
912 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
913 // %sum = extractvalue { i32, i1 } %t, 0
914 // %obit = extractvalue { i32, i1 } %t, 1
915 // br i1 %obit, label %overflow, label %normal
916 //
Dan Gohman653456c2009-01-07 00:15:08 +0000917 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000918 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000919 // looking for the SETO/SETB instruction. If an instruction modifies the
920 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
921 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +0000922 if (const IntrinsicInst *CI =
923 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +0000924 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
925 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
926 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +0000927 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +0000928
Chris Lattnera9a42252009-04-12 07:36:01 +0000929 for (MachineBasicBlock::const_reverse_iterator
Dan Gohmanf5951412010-07-08 01:00:56 +0000930 RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +0000931 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +0000932
Evan Cheng1015ba72010-05-21 20:53:24 +0000933 if (MI.definesRegister(Reg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +0000934 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +0000935
Chris Lattnera9a42252009-04-12 07:36:01 +0000936 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
937 Reg = Src;
938 continue;
Bill Wendling9a901322008-12-10 19:44:24 +0000939 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000940
Chris Lattnera9a42252009-04-12 07:36:01 +0000941 SetMI = &MI;
942 break;
Bill Wendling30a64a72008-12-09 23:19:12 +0000943 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000944
Chris Lattnera9a42252009-04-12 07:36:01 +0000945 const TargetInstrDesc &TID = MI.getDesc();
946 if (TID.hasUnmodeledSideEffects() ||
947 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
948 break;
Bill Wendling9a901322008-12-10 19:44:24 +0000949 }
Chris Lattnera9a42252009-04-12 07:36:01 +0000950
951 if (SetMI) {
952 unsigned OpCode = SetMI->getOpcode();
953
954 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohmanf5951412010-07-08 01:00:56 +0000955 BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ?
956 X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +0000957 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000958 FastEmitBranch(FalseMBB, DL);
Dan Gohmanf5951412010-07-08 01:00:56 +0000959 MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +0000960 return true;
961 }
Bill Wendling9a901322008-12-10 19:44:24 +0000962 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000963 }
964 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000965 }
966
967 // Otherwise do a clumsy setcc and re-test it.
968 unsigned OpReg = getRegForValue(BI->getCondition());
969 if (OpReg == 0) return false;
970
Dan Gohmanf5951412010-07-08 01:00:56 +0000971 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
972 BuildMI(MBB, DL, TII.get(X86::JNE_4)).addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000973 FastEmitBranch(FalseMBB, DL);
Dan Gohmanf5951412010-07-08 01:00:56 +0000974 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000975 return true;
976}
977
Dan Gohman46510a72010-04-15 01:51:59 +0000978bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000979 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000980 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000981 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000982 CReg = X86::CL;
983 RC = &X86::GR8RegClass;
984 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000985 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
986 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
987 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000988 default: return false;
989 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000990 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000991 CReg = X86::CX;
992 RC = &X86::GR16RegClass;
993 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000994 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
995 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
996 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000997 default: return false;
998 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000999 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001000 CReg = X86::ECX;
1001 RC = &X86::GR32RegClass;
1002 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001003 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1004 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1005 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001006 default: return false;
1007 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001008 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001009 CReg = X86::RCX;
1010 RC = &X86::GR64RegClass;
1011 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001012 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1013 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1014 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001015 default: return false;
1016 }
1017 } else {
1018 return false;
1019 }
1020
Owen Andersone50ed302009-08-10 22:56:29 +00001021 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001022 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001023 return false;
1024
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001025 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1026 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001027
1028 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001029 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001030 unsigned ResultReg = createResultReg(RC);
Dan Gohmanf5951412010-07-08 01:00:56 +00001031 BuildMI(MBB, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001032 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001033 UpdateValueMap(I, ResultReg);
1034 return true;
1035 }
1036
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001037 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1038 if (Op1Reg == 0) return false;
Dan Gohmanf5951412010-07-08 01:00:56 +00001039 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC, DL);
Dan Gohman145b8282008-10-07 21:50:36 +00001040
1041 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001042 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001043 if (CReg != X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001044 BuildMI(MBB, DL, TII.get(TargetOpcode::KILL), X86::CL)
1045 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001046
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001047 unsigned ResultReg = createResultReg(RC);
Dan Gohmanf5951412010-07-08 01:00:56 +00001048 BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001049 UpdateValueMap(I, ResultReg);
1050 return true;
1051}
1052
Dan Gohman46510a72010-04-15 01:51:59 +00001053bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001054 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001055 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001056 return false;
1057
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001058 unsigned Opc = 0;
1059 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001061 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001062 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001064 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001065 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001066 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001067 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001068 RC = &X86::GR64RegClass;
1069 } else {
1070 return false;
1071 }
1072
1073 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1074 if (Op0Reg == 0) return false;
1075 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1076 if (Op1Reg == 0) return false;
1077 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1078 if (Op2Reg == 0) return false;
1079
Dan Gohmanf5951412010-07-08 01:00:56 +00001080 BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001081 unsigned ResultReg = createResultReg(RC);
Dan Gohmanf5951412010-07-08 01:00:56 +00001082 BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001083 UpdateValueMap(I, ResultReg);
1084 return true;
1085}
1086
Dan Gohman46510a72010-04-15 01:51:59 +00001087bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001088 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001089 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001090 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001091 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001092 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001093 unsigned OpReg = getRegForValue(V);
1094 if (OpReg == 0) return false;
1095 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohmanf5951412010-07-08 01:00:56 +00001096 BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001097 UpdateValueMap(I, ResultReg);
1098 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001099 }
1100 }
1101
1102 return false;
1103}
1104
Dan Gohman46510a72010-04-15 01:51:59 +00001105bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001106 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001107 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001108 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001109 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001110 unsigned OpReg = getRegForValue(V);
1111 if (OpReg == 0) return false;
1112 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohmanf5951412010-07-08 01:00:56 +00001113 BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001114 UpdateValueMap(I, ResultReg);
1115 return true;
1116 }
1117 }
1118 }
1119
1120 return false;
1121}
1122
Dan Gohman46510a72010-04-15 01:51:59 +00001123bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001124 if (Subtarget->is64Bit())
1125 // All other cases should be handled by the tblgen generated code.
1126 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001127 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1128 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001129
1130 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001131 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001132 // All other cases should be handled by the tblgen generated code.
1133 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001135 // All other cases should be handled by the tblgen generated code.
1136 return false;
1137
1138 unsigned InputReg = getRegForValue(I->getOperand(0));
1139 if (!InputReg)
1140 // Unhandled operand. Halt "fast" selection and bail.
1141 return false;
1142
Dan Gohman62417622009-04-27 16:33:14 +00001143 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001144 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1145 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001146 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001147 unsigned CopyReg = createResultReg(CopyRC);
Dan Gohmanf5951412010-07-08 01:00:56 +00001148 BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001149
1150 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001151 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001152 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001153 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001154 if (!ResultReg)
1155 return false;
1156
1157 UpdateValueMap(I, ResultReg);
1158 return true;
1159}
1160
Dan Gohman46510a72010-04-15 01:51:59 +00001161bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1162 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1163 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001164
Dan Gohman46510a72010-04-15 01:51:59 +00001165 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001166 switch (CI->getIntrinsicID()) {
1167 default: break;
1168 case Intrinsic::sadd_with_overflow:
Dan Gohmanf5951412010-07-08 01:00:56 +00001169 case Intrinsic::uadd_with_overflow:
Chris Lattnera9a42252009-04-12 07:36:01 +00001170 // Cheat a little. We know that the registers for "add" and "seto" are
1171 // allocated sequentially. However, we only keep track of the register
1172 // for "add" in the value map. Use extractvalue's index to get the
1173 // correct register for "seto".
Dan Gohmanf5951412010-07-08 01:00:56 +00001174 UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001175 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001176 }
1177 }
1178
1179 return false;
1180}
1181
Dan Gohman46510a72010-04-15 01:51:59 +00001182bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001183 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001184 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001185 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001186 case Intrinsic::stackprotector: {
1187 // Emit code inline code to store the stack guard onto the stack.
1188 EVT PtrTy = TLI.getPointerTy();
1189
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001190 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1191 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001192
1193 // Grab the frame index.
1194 X86AddressMode AM;
1195 if (!X86SelectAddress(Slot, AM)) return false;
1196
Eric Christopher88dee302010-03-18 21:58:33 +00001197 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1198
Eric Christopher07754c22010-03-18 20:27:26 +00001199 return true;
1200 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001201 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001202 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001203 const Type *Ty = I.getCalledFunction()->getReturnType();
1204
1205 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1206
1207 EVT VT;
1208 if (!isTypeLegal(Ty, VT))
1209 return false;
1210
1211 unsigned OpC = 0;
1212 if (VT == MVT::i32)
1213 OpC = X86::MOV32ri;
1214 else if (VT == MVT::i64)
1215 OpC = X86::MOV64ri;
1216 else
1217 return false;
1218
1219 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohmanf5951412010-07-08 01:00:56 +00001220 BuildMI(MBB, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001221 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001222 UpdateValueMap(&I, ResultReg);
1223 return true;
1224 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001225 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001226 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001227 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001228 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001229 if (!X86SelectAddress(DI->getAddress(), AM))
1230 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001231 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001232 // FIXME may need to add RegState::Debug to any registers produced,
1233 // although ESP/EBP should be the only ones at the moment.
Dan Gohmanf5951412010-07-08 01:00:56 +00001234 addFullAddress(BuildMI(MBB, DL, II), AM).addImm(0).
1235 addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001236 return true;
1237 }
Eric Christopher77f79892010-01-18 22:11:29 +00001238 case Intrinsic::trap: {
Dan Gohmanf5951412010-07-08 01:00:56 +00001239 BuildMI(MBB, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001240 return true;
1241 }
Bill Wendling52370a12008-12-09 02:42:50 +00001242 case Intrinsic::sadd_with_overflow:
1243 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001244 // Replace "add with overflow" intrinsics with an "add" instruction followed
1245 // by a seto/setc instruction. Later on, when the "extractvalue"
1246 // instructions are encountered, we use the fact that two registers were
1247 // created sequentially to get the correct registers for the "sum" and the
1248 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001249 const Function *Callee = I.getCalledFunction();
1250 const Type *RetTy =
1251 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1252
Owen Andersone50ed302009-08-10 22:56:29 +00001253 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001254 if (!isTypeLegal(RetTy, VT))
1255 return false;
1256
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001257 const Value *Op1 = I.getArgOperand(0);
1258 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001259 unsigned Reg1 = getRegForValue(Op1);
1260 unsigned Reg2 = getRegForValue(Op2);
1261
1262 if (Reg1 == 0 || Reg2 == 0)
1263 // FIXME: Handle values *not* in registers.
1264 return false;
1265
1266 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001268 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001270 OpC = X86::ADD64rr;
1271 else
1272 return false;
1273
1274 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohmanf5951412010-07-08 01:00:56 +00001275 BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001276 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001277
Chris Lattner8d57b772009-04-12 07:51:14 +00001278 // If the add with overflow is an intra-block value then we just want to
1279 // create temporaries for it like normal. If it is a cross-block value then
1280 // UpdateValueMap will return the cross-block register used. Since we
1281 // *really* want the value to be live in the register pair known by
1282 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1283 // the cross block case. In the non-cross-block case, we should just make
1284 // another register for the value.
1285 if (DestReg1 != ResultReg)
1286 ResultReg = DestReg1+1;
1287 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001288 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001289
Chris Lattnera9a42252009-04-12 07:36:01 +00001290 unsigned Opc = X86::SETBr;
1291 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1292 Opc = X86::SETOr;
Dan Gohmanf5951412010-07-08 01:00:56 +00001293 BuildMI(MBB, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001294 return true;
1295 }
1296 }
1297}
1298
Dan Gohman46510a72010-04-15 01:51:59 +00001299bool X86FastISel::X86SelectCall(const Instruction *I) {
1300 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001301 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001302
1303 // Can't handle inline asm yet.
1304 if (isa<InlineAsm>(Callee))
1305 return false;
1306
Bill Wendling52370a12008-12-09 02:42:50 +00001307 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001308 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001309 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001310
Evan Chengf3d4efe2008-09-07 09:09:33 +00001311 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001312 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001313 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001314 if (CC != CallingConv::C &&
1315 CC != CallingConv::Fast &&
1316 CC != CallingConv::X86_FastCall)
1317 return false;
1318
Evan Cheng381993f2010-01-27 00:00:57 +00001319 // fastcc with -tailcallopt is intended to provide a guaranteed
1320 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001321 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001322 return false;
1323
Evan Chengf3d4efe2008-09-07 09:09:33 +00001324 // Let SDISel handle vararg functions.
1325 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1326 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1327 if (FTy->isVarArg())
1328 return false;
1329
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001330 // Fast-isel doesn't know about callee-pop yet.
1331 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1332 return false;
1333
Evan Chengf3d4efe2008-09-07 09:09:33 +00001334 // Handle *simple* calls for now.
1335 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001336 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001337 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001338 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001339 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001340 return false;
1341
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001342 // Materialize callee address in a register. FIXME: GV address can be
1343 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001344 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001345 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001346 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001347 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001348 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001349 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001350 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001351 } else if (CalleeAM.Base.Reg != 0) {
1352 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001353 } else
1354 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001355
Evan Chengdebdea02008-09-08 17:15:42 +00001356 // Allow calls which produce i1 results.
1357 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001358 if (RetVT == MVT::i1) {
1359 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001360 AndToI1 = true;
1361 }
1362
Evan Chengf3d4efe2008-09-07 09:09:33 +00001363 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001364 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001365 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001366 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001367 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001368 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001369 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001370 ArgVTs.reserve(CS.arg_size());
1371 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001372 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001373 i != e; ++i) {
1374 unsigned Arg = getRegForValue(*i);
1375 if (Arg == 0)
1376 return false;
1377 ISD::ArgFlagsTy Flags;
1378 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001379 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001380 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001381 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001382 Flags.setZExt();
1383
1384 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001385 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1386 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1387 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1388 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001389 return false;
1390
1391 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001392 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001393 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001394 return false;
1395 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1396 Flags.setOrigAlign(OriginalAlignment);
1397
1398 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001399 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001400 ArgVTs.push_back(ArgVT);
1401 ArgFlags.push_back(Flags);
1402 }
1403
1404 // Analyze operands of the call, assigning locations to each operand.
1405 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001406 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Dan Gohmand8acddd2010-06-01 21:09:47 +00001407
1408 // Allocate shadow area for Win64
1409 if (Subtarget->isTargetWin64()) {
1410 CCInfo.AllocateStack(32, 8);
1411 }
1412
Evan Chengf3d4efe2008-09-07 09:09:33 +00001413 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1414
1415 // Get a count of how many bytes are to be pushed on the stack.
1416 unsigned NumBytes = CCInfo.getNextStackOffset();
1417
1418 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001419 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohmanf5951412010-07-08 01:00:56 +00001420 BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001421
Chris Lattner438949a2008-10-15 05:30:52 +00001422 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001423 // copies / loads.
1424 SmallVector<unsigned, 4> RegArgs;
1425 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1426 CCValAssign &VA = ArgLocs[i];
1427 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001428 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001429
1430 // Promote the value if needed.
1431 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001432 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001433 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001434 case CCValAssign::SExt: {
1435 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1436 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001437 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001438 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001439 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001440 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001441 }
1442 case CCValAssign::ZExt: {
1443 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1444 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001445 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001446 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001447 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001448 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001449 }
1450 case CCValAssign::AExt: {
1451 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1452 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001453 if (!Emitted)
1454 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001455 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001456 if (!Emitted)
1457 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1458 Arg, ArgVT, Arg);
1459
Chris Lattnera33649e2008-12-19 17:03:38 +00001460 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001461 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001462 break;
1463 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001464 case CCValAssign::BCvt: {
1465 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001466 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001467 assert(BC != 0 && "Failed to emit a bitcast!");
1468 Arg = BC;
1469 ArgVT = VA.getLocVT();
1470 break;
1471 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001472 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001473
1474 if (VA.isRegLoc()) {
1475 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
Dan Gohmanf5951412010-07-08 01:00:56 +00001476 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1477 Arg, RC, RC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001478 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001479 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001480 RegArgs.push_back(VA.getLocReg());
1481 } else {
1482 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001483 X86AddressMode AM;
1484 AM.Base.Reg = StackPtr;
1485 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001486 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001487
1488 // If this is a really simple value, emit this with the Value* version of
1489 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1490 // can cause us to reevaluate the argument.
1491 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1492 X86FastEmitStore(ArgVT, ArgVal, AM);
1493 else
1494 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001495 }
1496 }
1497
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001498 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1499 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001500 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001501 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohmana4160c32010-07-07 16:29:44 +00001502 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohmanf5951412010-07-08 01:00:56 +00001503 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC,
1504 DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001505 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001506 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001507 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001508
Evan Chengf3d4efe2008-09-07 09:09:33 +00001509 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001510 MachineInstrBuilder MIB;
1511 if (CalleeOp) {
1512 // Register-indirect call.
1513 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
Dan Gohmanf5951412010-07-08 01:00:56 +00001514 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001515
1516 } else {
1517 // Direct call.
1518 assert(GV && "Not a direct call");
1519 unsigned CallOpc =
1520 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1521
1522 // See if we need any target-specific flags on the GV operand.
1523 unsigned char OpFlags = 0;
1524
1525 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1526 // external symbols most go through the PLT in PIC mode. If the symbol
1527 // has hidden or protected visibility, or if it is static or local, then
1528 // we don't need to use the PLT - we can directly call it.
1529 if (Subtarget->isTargetELF() &&
1530 TM.getRelocationModel() == Reloc::PIC_ &&
1531 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1532 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001533 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001534 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1535 Subtarget->getDarwinVers() < 9) {
1536 // PC-relative references to external symbols should go through $stub,
1537 // unless we're building with the leopard linker or later, which
1538 // automatically synthesizes these stubs.
1539 OpFlags = X86II::MO_DARWIN_STUB;
1540 }
1541
1542
Dan Gohmanf5951412010-07-08 01:00:56 +00001543 MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001544 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001545
1546 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001547 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001548 MIB.addReg(X86::EBX);
1549
Evan Chengf3d4efe2008-09-07 09:09:33 +00001550 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001551 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1552 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001553
1554 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001555 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohmanf5951412010-07-08 01:00:56 +00001556 BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001557
1558 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001559 SmallVector<unsigned, 4> UsedRegs;
Owen Anderson825b72b2009-08-11 20:47:22 +00001560 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001561 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001562 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001563 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1564
1565 // Copy all of the result registers out of their specified physreg.
1566 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001567 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001568 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1569 TargetRegisterClass *SrcRC = DstRC;
1570
1571 // If this is a call to a function that returns an fp value on the x87 fp
1572 // stack, but where we prefer to use the value in xmm registers, copy it
1573 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1574 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1575 RVLocs[0].getLocReg() == X86::ST1) &&
1576 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001577 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001578 SrcRC = X86::RSTRegisterClass;
1579 DstRC = X86::RFP80RegisterClass;
1580 }
1581
1582 unsigned ResultReg = createResultReg(DstRC);
Dan Gohmanf5951412010-07-08 01:00:56 +00001583 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001584 RVLocs[0].getLocReg(), DstRC, SrcRC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001585 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001586 Emitted = true;
Dan Gohmandb497122010-06-18 23:28:01 +00001587 UsedRegs.push_back(RVLocs[0].getLocReg());
1588
Evan Chengf3d4efe2008-09-07 09:09:33 +00001589 if (CopyVT != RVLocs[0].getValVT()) {
1590 // Round the F80 the right size, which also moves to the appropriate xmm
1591 // register. This is accomplished by storing the F80 value in memory and
1592 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001593 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001594 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001595 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001596 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohmanf5951412010-07-08 01:00:56 +00001597 addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001598 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001599 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001600 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001601 ResultReg = createResultReg(DstRC);
Dan Gohmanf5951412010-07-08 01:00:56 +00001602 addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001603 }
1604
Evan Chengdebdea02008-09-08 17:15:42 +00001605 if (AndToI1) {
1606 // Mask out all but lowest bit for some call which produces an i1.
1607 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohmanf5951412010-07-08 01:00:56 +00001608 BuildMI(MBB, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001609 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001610 ResultReg = AndResult;
1611 }
1612
Evan Chengf3d4efe2008-09-07 09:09:33 +00001613 UpdateValueMap(I, ResultReg);
1614 }
1615
Dan Gohmandb497122010-06-18 23:28:01 +00001616 // Set all unused physreg defs as dead.
1617 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1618
Evan Chengf3d4efe2008-09-07 09:09:33 +00001619 return true;
1620}
1621
1622
Dan Gohman99b21822008-08-28 23:21:34 +00001623bool
Dan Gohman46510a72010-04-15 01:51:59 +00001624X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001625 switch (I->getOpcode()) {
1626 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001627 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001628 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001629 case Instruction::Store:
1630 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001631 case Instruction::ICmp:
1632 case Instruction::FCmp:
1633 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001634 case Instruction::ZExt:
1635 return X86SelectZExt(I);
1636 case Instruction::Br:
1637 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001638 case Instruction::Call:
1639 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001640 case Instruction::LShr:
1641 case Instruction::AShr:
1642 case Instruction::Shl:
1643 return X86SelectShift(I);
1644 case Instruction::Select:
1645 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001646 case Instruction::Trunc:
1647 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001648 case Instruction::FPExt:
1649 return X86SelectFPExt(I);
1650 case Instruction::FPTrunc:
1651 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001652 case Instruction::ExtractValue:
1653 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001654 case Instruction::IntToPtr: // Deliberate fall-through.
1655 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001656 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1657 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001658 if (DstVT.bitsGT(SrcVT))
1659 return X86SelectZExt(I);
1660 if (DstVT.bitsLT(SrcVT))
1661 return X86SelectTrunc(I);
1662 unsigned Reg = getRegForValue(I->getOperand(0));
1663 if (Reg == 0) return false;
1664 UpdateValueMap(I, Reg);
1665 return true;
1666 }
Dan Gohman99b21822008-08-28 23:21:34 +00001667 }
1668
1669 return false;
1670}
1671
Dan Gohman46510a72010-04-15 01:51:59 +00001672unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001673 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001674 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001675 return false;
1676
1677 // Get opcode and regclass of the output for the given load instruction.
1678 unsigned Opc = 0;
1679 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001680 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001681 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001682 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001683 Opc = X86::MOV8rm;
1684 RC = X86::GR8RegisterClass;
1685 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001686 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001687 Opc = X86::MOV16rm;
1688 RC = X86::GR16RegisterClass;
1689 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001690 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001691 Opc = X86::MOV32rm;
1692 RC = X86::GR32RegisterClass;
1693 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001695 // Must be in x86-64 mode.
1696 Opc = X86::MOV64rm;
1697 RC = X86::GR64RegisterClass;
1698 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001700 if (Subtarget->hasSSE1()) {
1701 Opc = X86::MOVSSrm;
1702 RC = X86::FR32RegisterClass;
1703 } else {
1704 Opc = X86::LD_Fp32m;
1705 RC = X86::RFP32RegisterClass;
1706 }
1707 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001709 if (Subtarget->hasSSE2()) {
1710 Opc = X86::MOVSDrm;
1711 RC = X86::FR64RegisterClass;
1712 } else {
1713 Opc = X86::LD_Fp64m;
1714 RC = X86::RFP64RegisterClass;
1715 }
1716 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001717 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001718 // No f80 support yet.
1719 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001720 }
1721
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001722 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001723 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001724 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001725 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001726 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001727 Opc = X86::LEA32r;
1728 else
1729 Opc = X86::LEA64r;
1730 unsigned ResultReg = createResultReg(RC);
Dan Gohmanf5951412010-07-08 01:00:56 +00001731 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001732 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001733 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001734 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001735 }
1736
Owen Anderson3b217c62008-09-06 01:11:01 +00001737 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001738 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001739 if (Align == 0) {
1740 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001741 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001742 }
Owen Anderson95267a12008-09-05 00:06:23 +00001743
Dan Gohman5396c992008-09-30 01:21:32 +00001744 // x86-32 PIC requires a PIC base register for constant pools.
1745 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001746 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001747 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001748 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001749 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001750 } else if (Subtarget->isPICStyleGOT()) {
1751 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001752 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001753 } else if (Subtarget->isPICStyleRIPRel() &&
1754 TM.getCodeModel() == CodeModel::Small) {
1755 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001756 }
Dan Gohman5396c992008-09-30 01:21:32 +00001757
1758 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001759 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001760 unsigned ResultReg = createResultReg(RC);
Dan Gohmanf5951412010-07-08 01:00:56 +00001761 addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001762 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001763
Owen Anderson95267a12008-09-05 00:06:23 +00001764 return ResultReg;
1765}
1766
Dan Gohman46510a72010-04-15 01:51:59 +00001767unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001768 // Fail on dynamic allocas. At this point, getRegForValue has already
1769 // checked its CSE maps, so if we're here trying to handle a dynamic
1770 // alloca, we're not going to succeed. X86SelectAddress has a
1771 // check for dynamic allocas, because it's called directly from
1772 // various places, but TargetMaterializeAlloca also needs a check
1773 // in order to avoid recursion between getRegForValue,
1774 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001775 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001776 return 0;
1777
Dan Gohman0586d912008-09-10 20:11:02 +00001778 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001779 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001780 return 0;
1781 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1782 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1783 unsigned ResultReg = createResultReg(RC);
Dan Gohmanf5951412010-07-08 01:00:56 +00001784 addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001785 return ResultReg;
1786}
1787
Evan Chengc3f44b02008-09-03 00:03:49 +00001788namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001789 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1790 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001791 }
Dan Gohman99b21822008-08-28 23:21:34 +00001792}