blob: ce1370763b77fb1d300edb5c41e4d4615824b802 [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"
Dan Gohman84023e02010-07-10 09:00:22 +000026#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000027#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000028#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000029#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000032#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000034#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000035#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000036using namespace llvm;
37
Chris Lattner087fcf32009-03-08 18:44:31 +000038namespace {
39
Evan Chengc3f44b02008-09-03 00:03:49 +000040class X86FastISel : public FastISel {
41 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
42 /// make the right decision when generating code for different targets.
43 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000044
45 /// StackPtr - Register used as the stack pointer.
46 ///
47 unsigned StackPtr;
48
49 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
50 /// floating point ops.
51 /// When SSE is available, use it for f32 operations.
52 /// When SSE2 is available, use it for f64 operations.
53 bool X86ScalarSSEf64;
54 bool X86ScalarSSEf32;
55
Evan Cheng8b19e562008-09-03 06:44:39 +000056public:
Dan Gohmana4160c32010-07-07 16:29:44 +000057 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000058 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000059 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
60 X86ScalarSSEf64 = Subtarget->hasSSE2();
61 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000062 }
Evan Chengc3f44b02008-09-03 00:03:49 +000063
Dan Gohman46510a72010-04-15 01:51:59 +000064 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000065
Dan Gohman1adf1b02008-08-19 21:45:35 +000066#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000067
68private:
Dan Gohman46510a72010-04-15 01:51:59 +000069 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000070
Owen Andersone50ed302009-08-10 22:56:29 +000071 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000072
Dan Gohman46510a72010-04-15 01:51:59 +000073 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000074 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000075 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000076 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000077
Owen Andersone50ed302009-08-10 22:56:29 +000078 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000079 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Dan Gohman46510a72010-04-15 01:51:59 +000081 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
82 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000083
Dan Gohman46510a72010-04-15 01:51:59 +000084 bool X86SelectLoad(const Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000085
Dan Gohman46510a72010-04-15 01:51:59 +000086 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000087
Dan Gohman84023e02010-07-10 09:00:22 +000088 bool X86SelectRet(const Instruction *I);
89
Dan Gohman46510a72010-04-15 01:51:59 +000090 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000093
Dan Gohman46510a72010-04-15 01:51:59 +000094 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000095
Dan Gohman46510a72010-04-15 01:51:59 +000096 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000097
Dan Gohman46510a72010-04-15 01:51:59 +000098 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000099
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectTrunc(const Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectFPExt(const Instruction *I);
103 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000104
Dan Gohman46510a72010-04-15 01:51:59 +0000105 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
108 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000109
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000110 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Dan Gohman84023e02010-07-10 09:00:22 +0000111 CCAssignFn *CCAssignFnForRet(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000112
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000113 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000114 return getTargetMachine()->getInstrInfo();
115 }
116 const X86TargetMachine *getTargetMachine() const {
117 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000118 }
119
Dan Gohman46510a72010-04-15 01:51:59 +0000120 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000121
Dan Gohman46510a72010-04-15 01:51:59 +0000122 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000123
124 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
125 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000126 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
128 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000129 }
130
Owen Andersone50ed302009-08-10 22:56:29 +0000131 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000132};
Chris Lattner087fcf32009-03-08 18:44:31 +0000133
134} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000135
Owen Andersone50ed302009-08-10 22:56:29 +0000136bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000137 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000139 // Unhandled type. Halt "fast" selection and bail.
140 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000141
Dan Gohman9b66d732008-09-30 00:48:39 +0000142 // For now, require SSE/SSE2 for performing floating-point operations,
143 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000145 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000147 return false;
148 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000150 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000151 // We only handle legal types. For example, on x86-32 the instruction
152 // selector contains all of the 64-bit instructions from x86-64,
153 // under the assumption that i64 won't be used if the target doesn't
154 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000156}
157
158#include "X86GenCallingConv.inc"
159
160/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
161/// convention.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000162CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
163 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000164 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +0000165 if (CC == CallingConv::GHC)
166 return CC_X86_64_GHC;
167 else if (Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000168 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000169 else
170 return CC_X86_64_C;
171 }
172
173 if (CC == CallingConv::X86_FastCall)
174 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000175 else if (CC == CallingConv::X86_ThisCall)
176 return CC_X86_32_ThisCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177 else if (CC == CallingConv::Fast)
178 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +0000179 else if (CC == CallingConv::GHC)
180 return CC_X86_32_GHC;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000181 else
182 return CC_X86_32_C;
183}
184
Dan Gohman84023e02010-07-10 09:00:22 +0000185/// CCAssignFnForRet - Selects the correct CCAssignFn for a given calling
186/// convention.
187CCAssignFn *X86FastISel::CCAssignFnForRet(CallingConv::ID CC,
188 bool isTaillCall) {
189 if (Subtarget->is64Bit()) {
190 if (Subtarget->isTargetWin64())
191 return RetCC_X86_Win64_C;
192 else
193 return RetCC_X86_64_C;
194 }
195
196 return RetCC_X86_32_C;
197}
198
Evan Cheng0de588f2008-09-05 21:00:03 +0000199/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000200/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000201/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000202bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 unsigned &ResultReg) {
204 // Get opcode and regclass of the output for the given load instruction.
205 unsigned Opc = 0;
206 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000208 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000209 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000211 Opc = X86::MOV8rm;
212 RC = X86::GR8RegisterClass;
213 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000215 Opc = X86::MOV16rm;
216 RC = X86::GR16RegisterClass;
217 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000219 Opc = X86::MOV32rm;
220 RC = X86::GR32RegisterClass;
221 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000223 // Must be in x86-64 mode.
224 Opc = X86::MOV64rm;
225 RC = X86::GR64RegisterClass;
226 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000228 if (Subtarget->hasSSE1()) {
229 Opc = X86::MOVSSrm;
230 RC = X86::FR32RegisterClass;
231 } else {
232 Opc = X86::LD_Fp32m;
233 RC = X86::RFP32RegisterClass;
234 }
235 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000237 if (Subtarget->hasSSE2()) {
238 Opc = X86::MOVSDrm;
239 RC = X86::FR64RegisterClass;
240 } else {
241 Opc = X86::LD_Fp64m;
242 RC = X86::RFP64RegisterClass;
243 }
244 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000246 // No f80 support yet.
247 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000248 }
249
250 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000251 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
252 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000253 return true;
254}
255
Evan Chengf3d4efe2008-09-07 09:09:33 +0000256/// X86FastEmitStore - Emit a machine instruction to store a value Val of
257/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
258/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000259/// i.e. V. Return true if it is possible.
260bool
Owen Andersone50ed302009-08-10 22:56:29 +0000261X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000262 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000263 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000264 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 switch (VT.getSimpleVT().SimpleTy) {
266 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000267 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000268 case MVT::i1: {
269 // Mask out all but lowest bit.
270 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000271 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000272 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
273 Val = AndResult;
274 }
275 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 case MVT::i8: Opc = X86::MOV8mr; break;
277 case MVT::i16: Opc = X86::MOV16mr; break;
278 case MVT::i32: Opc = X86::MOV32mr; break;
279 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
280 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000281 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000282 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000284 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000285 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000286 }
Chris Lattner438949a2008-10-15 05:30:52 +0000287
Dan Gohman84023e02010-07-10 09:00:22 +0000288 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
289 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000290 return true;
291}
292
Dan Gohman46510a72010-04-15 01:51:59 +0000293bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000294 const X86AddressMode &AM) {
295 // Handle 'null' like i32/i64 0.
296 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000297 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000298
299 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000300 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000301 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000302 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000304 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000305 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 case MVT::i8: Opc = X86::MOV8mi; break;
307 case MVT::i16: Opc = X86::MOV16mi; break;
308 case MVT::i32: Opc = X86::MOV32mi; break;
309 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000310 // Must be a 32-bit sign extended value.
311 if ((int)CI->getSExtValue() == CI->getSExtValue())
312 Opc = X86::MOV64mi32;
313 break;
314 }
315
316 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000317 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
318 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000319 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000320 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000321 return true;
322 }
323 }
324
325 unsigned ValReg = getRegForValue(Val);
326 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000327 return false;
328
329 return X86FastEmitStore(VT, ValReg, AM);
330}
331
Evan Cheng24e3a902008-09-08 06:35:17 +0000332/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
333/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
334/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000335bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
336 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000337 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000338 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
339 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000340
341 if (RR != 0) {
342 ResultReg = RR;
343 return true;
344 } else
345 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000346}
347
Dan Gohman0586d912008-09-10 20:11:02 +0000348/// X86SelectAddress - Attempt to fill in an address from the given value.
349///
Dan Gohman46510a72010-04-15 01:51:59 +0000350bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
351 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000352 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000353 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000354 // Don't walk into other basic blocks; it's possible we haven't
355 // visited them yet, so the instructions may not yet be assigned
356 // virtual registers.
Dan Gohman84023e02010-07-10 09:00:22 +0000357 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
Dan Gohmanea9f1512010-06-18 20:44:47 +0000358 return false;
359
Dan Gohman35893082008-09-18 23:23:44 +0000360 Opcode = I->getOpcode();
361 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000362 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000363 Opcode = C->getOpcode();
364 U = C;
365 }
Dan Gohman0586d912008-09-10 20:11:02 +0000366
Chris Lattner868ee942010-06-15 19:08:40 +0000367 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
368 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000369 // Fast instruction selection doesn't support the special
370 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000371 return false;
372
Dan Gohman35893082008-09-18 23:23:44 +0000373 switch (Opcode) {
374 default: break;
375 case Instruction::BitCast:
376 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000377 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000378
379 case Instruction::IntToPtr:
380 // Look past no-op inttoptrs.
381 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000382 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000383 break;
Dan Gohman35893082008-09-18 23:23:44 +0000384
385 case Instruction::PtrToInt:
386 // Look past no-op ptrtoints.
387 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000388 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000389 break;
Dan Gohman35893082008-09-18 23:23:44 +0000390
391 case Instruction::Alloca: {
392 // Do static allocas.
393 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000394 DenseMap<const AllocaInst*, int>::iterator SI =
395 FuncInfo.StaticAllocaMap.find(A);
396 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000397 AM.BaseType = X86AddressMode::FrameIndexBase;
398 AM.Base.FrameIndex = SI->second;
399 return true;
400 }
401 break;
Dan Gohman35893082008-09-18 23:23:44 +0000402 }
403
404 case Instruction::Add: {
405 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000406 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000407 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
408 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000409 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000410 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000411 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000412 }
Dan Gohman0586d912008-09-10 20:11:02 +0000413 }
Dan Gohman35893082008-09-18 23:23:44 +0000414 break;
415 }
416
417 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000418 X86AddressMode SavedAM = AM;
419
Dan Gohman35893082008-09-18 23:23:44 +0000420 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000421 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000422 unsigned IndexReg = AM.IndexReg;
423 unsigned Scale = AM.Scale;
424 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000425 // Iterate through the indices, folding what we can. Constants can be
426 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000427 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000428 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000429 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000430 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
431 const StructLayout *SL = TD.getStructLayout(STy);
432 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
433 Disp += SL->getElementOffset(Idx);
434 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000435 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman5c87bf62010-07-01 02:27:15 +0000436 SmallVector<const Value *, 4> Worklist;
437 Worklist.push_back(Op);
438 do {
439 Op = Worklist.pop_back_val();
440 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
441 // Constant-offset addressing.
442 Disp += CI->getSExtValue() * S;
Dan Gohmanabd1d852010-07-01 02:58:21 +0000443 } else if (isa<AddOperator>(Op) &&
444 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
445 // An add with a constant operand. Fold the constant.
446 ConstantInt *CI =
447 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
448 Disp += CI->getSExtValue() * S;
449 // Add the other operand back to the work list.
450 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
Dan Gohman5c87bf62010-07-01 02:27:15 +0000451 } else if (IndexReg == 0 &&
452 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
453 (S == 1 || S == 2 || S == 4 || S == 8)) {
454 // Scaled-index addressing.
455 Scale = S;
456 IndexReg = getRegForGEPIndex(Op).first;
457 if (IndexReg == 0)
458 return false;
Dan Gohman5c87bf62010-07-01 02:27:15 +0000459 } else
460 // Unsupported.
461 goto unsupported_gep;
462 } while (!Worklist.empty());
Dan Gohman35893082008-09-18 23:23:44 +0000463 }
464 }
Dan Gohman09aae462008-09-26 20:04:15 +0000465 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000466 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000467 break;
Dan Gohman35893082008-09-18 23:23:44 +0000468 // Ok, the GEP indices were covered by constant-offset and scaled-index
469 // addressing. Update the address state and move on to examining the base.
470 AM.IndexReg = IndexReg;
471 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000472 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000473 if (X86SelectAddress(U->getOperand(0), AM))
474 return true;
475
476 // If we couldn't merge the sub value into this addr mode, revert back to
477 // our address and just match the value instead of completely failing.
478 AM = SavedAM;
479 break;
Dan Gohman35893082008-09-18 23:23:44 +0000480 unsupported_gep:
481 // Ok, the GEP indices weren't all covered.
482 break;
483 }
484 }
485
486 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000487 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000488 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000489 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000490 return false;
491
Dan Gohman97135e12008-09-26 19:15:30 +0000492 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000493 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000494 (AM.Base.Reg != 0 || AM.IndexReg != 0))
495 return false;
496
Dan Gohmane9865942009-02-23 22:03:08 +0000497 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000498 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000499 if (GVar->isThreadLocal())
500 return false;
501
Chris Lattnerff7727f2009-07-09 06:41:35 +0000502 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000503 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000504
Chris Lattner0d786dd2009-07-10 07:48:51 +0000505 // Allow the subtarget to classify the global.
506 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
507
508 // If this reference is relative to the pic base, set it now.
509 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000510 // FIXME: How do we know Base.Reg is free??
Dan Gohmana4160c32010-07-07 16:29:44 +0000511 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000512 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000513
514 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000515 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000516 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000517 if (Subtarget->isPICStyleRIPRel()) {
518 // Use rip-relative addressing if we can. Above we verified that the
519 // base and index registers are unused.
520 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
521 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000522 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000523 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000524 return true;
525 }
526
Chris Lattner0d786dd2009-07-10 07:48:51 +0000527 // Ok, we need to do a load from a stub. If we've already loaded from this
528 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000529 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
530 unsigned LoadReg;
531 if (I != LocalValueMap.end() && I->second != 0) {
532 LoadReg = I->second;
533 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000534 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000535 unsigned Opc = 0;
536 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000537 X86AddressMode StubAM;
538 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000539 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000540 StubAM.GVOpFlags = GVFlags;
541
Dan Gohman84023e02010-07-10 09:00:22 +0000542 // Prepare for inserting code in the local-value area.
Dan Gohmana10b8492010-07-14 01:07:44 +0000543 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000544
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000546 Opc = X86::MOV64rm;
547 RC = X86::GR64RegisterClass;
548
Chris Lattner0d786dd2009-07-10 07:48:51 +0000549 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000550 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000551 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000552 Opc = X86::MOV32rm;
553 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000554 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000555
556 LoadReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000557 MachineInstrBuilder LoadMI =
558 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
559 addFullAddress(LoadMI, StubAM);
560
561 // Ok, back to normal mode.
562 leaveLocalValueArea(SaveInsertPt);
563
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000564 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000565 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000566 }
Chris Lattner18c59872009-06-27 04:16:01 +0000567
Chris Lattnerff7727f2009-07-09 06:41:35 +0000568 // Now construct the final address. Note that the Disp, Scale,
569 // and Index values may already be set here.
570 AM.Base.Reg = LoadReg;
571 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000572 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000573 }
574
Dan Gohman97135e12008-09-26 19:15:30 +0000575 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000576 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000577 if (AM.Base.Reg == 0) {
578 AM.Base.Reg = getRegForValue(V);
579 return AM.Base.Reg != 0;
580 }
581 if (AM.IndexReg == 0) {
582 assert(AM.Scale == 1 && "Scale with no index!");
583 AM.IndexReg = getRegForValue(V);
584 return AM.IndexReg != 0;
585 }
586 }
587
588 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000589}
590
Chris Lattner0aa43de2009-07-10 05:33:42 +0000591/// X86SelectCallAddress - Attempt to fill in an address from the given value.
592///
Dan Gohman46510a72010-04-15 01:51:59 +0000593bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
594 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000595 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000596 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000597 Opcode = I->getOpcode();
598 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000599 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000600 Opcode = C->getOpcode();
601 U = C;
602 }
603
604 switch (Opcode) {
605 default: break;
606 case Instruction::BitCast:
607 // Look past bitcasts.
608 return X86SelectCallAddress(U->getOperand(0), AM);
609
610 case Instruction::IntToPtr:
611 // Look past no-op inttoptrs.
612 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
613 return X86SelectCallAddress(U->getOperand(0), AM);
614 break;
615
616 case Instruction::PtrToInt:
617 // Look past no-op ptrtoints.
618 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
619 return X86SelectCallAddress(U->getOperand(0), AM);
620 break;
621 }
622
623 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000624 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000625 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000626 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000627 return false;
628
629 // RIP-relative addresses can't have additional register operands.
630 if (Subtarget->isPICStyleRIPRel() &&
631 (AM.Base.Reg != 0 || AM.IndexReg != 0))
632 return false;
633
Chris Lattner754b7652009-07-10 05:48:03 +0000634 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000635 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000636 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000637 return false;
638
639 // Okay, we've committed to selecting this global. Set up the basic address.
640 AM.GV = GV;
641
Chris Lattnere6c07b52009-07-10 05:45:15 +0000642 // No ABI requires an extra load for anything other than DLLImport, which
643 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000644 if (Subtarget->isPICStyleRIPRel()) {
645 // Use rip-relative addressing if we can. Above we verified that the
646 // base and index registers are unused.
647 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
648 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000649 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000650 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
651 } else if (Subtarget->isPICStyleGOT()) {
652 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000653 }
654
Chris Lattner0aa43de2009-07-10 05:33:42 +0000655 return true;
656 }
657
658 // If all else fails, try to materialize the value in a register.
659 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
660 if (AM.Base.Reg == 0) {
661 AM.Base.Reg = getRegForValue(V);
662 return AM.Base.Reg != 0;
663 }
664 if (AM.IndexReg == 0) {
665 assert(AM.Scale == 1 && "Scale with no index!");
666 AM.IndexReg = getRegForValue(V);
667 return AM.IndexReg != 0;
668 }
669 }
670
671 return false;
672}
673
674
Owen Andersona3971df2008-09-04 07:08:58 +0000675/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000676bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000677 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000678 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000679 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000680
Dan Gohman0586d912008-09-10 20:11:02 +0000681 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000682 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000683 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000684
Chris Lattner438949a2008-10-15 05:30:52 +0000685 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000686}
687
Dan Gohman84023e02010-07-10 09:00:22 +0000688/// X86SelectRet - Select and emit code to implement ret instructions.
689bool X86FastISel::X86SelectRet(const Instruction *I) {
690 const ReturnInst *Ret = cast<ReturnInst>(I);
691 const Function &F = *I->getParent()->getParent();
692
693 if (!FuncInfo.CanLowerReturn)
694 return false;
695
696 CallingConv::ID CC = F.getCallingConv();
697 if (CC != CallingConv::C &&
698 CC != CallingConv::Fast &&
699 CC != CallingConv::X86_FastCall)
700 return false;
701
702 if (Subtarget->isTargetWin64())
703 return false;
704
705 // Don't handle popping bytes on return for now.
706 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
707 ->getBytesToPopOnReturn() != 0)
708 return 0;
709
710 // fastcc with -tailcallopt is intended to provide a guaranteed
711 // tail call optimization. Fastisel doesn't know how to do that.
712 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
713 return false;
714
715 // Let SDISel handle vararg functions.
716 if (F.isVarArg())
717 return false;
718
719 if (Ret->getNumOperands() > 0) {
720 SmallVector<ISD::OutputArg, 4> Outs;
721 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
722 Outs, TLI);
723
724 // Analyze operands of the call, assigning locations to each operand.
725 SmallVector<CCValAssign, 16> ValLocs;
726 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
727 CCInfo.AnalyzeReturn(Outs, CCAssignFnForRet(CC));
728
729 const Value *RV = Ret->getOperand(0);
730 unsigned Reg = getRegForValue(RV);
731 if (Reg == 0)
732 return false;
733
734 // Only handle a single return value for now.
735 if (ValLocs.size() != 1)
736 return false;
737
738 CCValAssign &VA = ValLocs[0];
739
740 // Don't bother handling odd stuff for now.
741 if (VA.getLocInfo() != CCValAssign::Full)
742 return false;
743 // Only handle register returns for now.
744 if (!VA.isRegLoc())
745 return false;
746 // TODO: For now, don't try to handle cases where getLocInfo()
747 // says Full but the types don't match.
748 if (VA.getValVT() != TLI.getValueType(RV->getType()))
749 return false;
750
751 // The calling-convention tables for x87 returns don't tell
752 // the whole story.
753 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
754 return false;
755
756 // Make the copy.
757 unsigned SrcReg = Reg + VA.getValNo();
758 unsigned DstReg = VA.getLocReg();
759 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000760 // Avoid a cross-class copy. This is very unlikely.
761 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000762 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000763 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
764 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000765
766 // Mark the register as live out of the function.
767 MRI.addLiveOut(VA.getLocReg());
768 }
769
770 // Now emit the RET.
771 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
772 return true;
773}
774
Evan Cheng8b19e562008-09-03 06:44:39 +0000775/// X86SelectLoad - Select and emit code to implement load instructions.
776///
Dan Gohman46510a72010-04-15 01:51:59 +0000777bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000778 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000779 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000780 return false;
781
Dan Gohman0586d912008-09-10 20:11:02 +0000782 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000783 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000784 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000785
Evan Cheng0de588f2008-09-05 21:00:03 +0000786 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000787 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000788 UpdateValueMap(I, ResultReg);
789 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000790 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000791 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000792}
793
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000794static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000796 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 case MVT::i8: return X86::CMP8rr;
798 case MVT::i16: return X86::CMP16rr;
799 case MVT::i32: return X86::CMP32rr;
800 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000801 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
802 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000803 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000804}
805
Chris Lattner0e13c782008-10-15 04:13:29 +0000806/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
807/// of the comparison, return an opcode that works for the compare (e.g.
808/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000809static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000811 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000812 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 case MVT::i8: return X86::CMP8ri;
814 case MVT::i16: return X86::CMP16ri;
815 case MVT::i32: return X86::CMP32ri;
816 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000817 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
818 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000819 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000820 return X86::CMP64ri32;
821 return 0;
822 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000823}
824
Dan Gohman46510a72010-04-15 01:51:59 +0000825bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
826 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000827 unsigned Op0Reg = getRegForValue(Op0);
828 if (Op0Reg == 0) return false;
829
Chris Lattnerd53886b2008-10-15 05:18:04 +0000830 // Handle 'null' like i32/i64 0.
831 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000832 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000833
Chris Lattner9a08a612008-10-15 04:26:38 +0000834 // We have two options: compare with register or immediate. If the RHS of
835 // the compare is an immediate that we can fold into this compare, use
836 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000837 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000838 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000839 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
840 .addReg(Op0Reg)
841 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000842 return true;
843 }
844 }
845
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000846 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000847 if (CompareOpc == 0) return false;
848
849 unsigned Op1Reg = getRegForValue(Op1);
850 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000851 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
852 .addReg(Op0Reg)
853 .addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000854
855 return true;
856}
857
Dan Gohman46510a72010-04-15 01:51:59 +0000858bool X86FastISel::X86SelectCmp(const Instruction *I) {
859 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000860
Owen Andersone50ed302009-08-10 22:56:29 +0000861 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000862 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000863 return false;
864
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000865 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000866 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000867 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000868 switch (CI->getPredicate()) {
869 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000870 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
871 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000872
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000873 unsigned EReg = createResultReg(&X86::GR8RegClass);
874 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000875 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
876 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
877 TII.get(X86::SETNPr), NPReg);
878 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000879 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000880 UpdateValueMap(I, ResultReg);
881 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000882 }
883 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000884 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
885 return false;
886
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000887 unsigned NEReg = createResultReg(&X86::GR8RegClass);
888 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
890 TII.get(X86::SETNEr), NEReg);
891 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
892 TII.get(X86::SETPr), PReg);
893 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
894 TII.get(X86::OR8rr), ResultReg)
895 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000896 UpdateValueMap(I, ResultReg);
897 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000898 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000899 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
900 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
901 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
902 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
903 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
904 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
905 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
906 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
907 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
908 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
909 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
910 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
911
912 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
913 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
914 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
915 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
916 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
917 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
918 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
919 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
920 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
921 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000922 default:
923 return false;
924 }
925
Dan Gohman46510a72010-04-15 01:51:59 +0000926 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000927 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000928 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000929
Chris Lattner9a08a612008-10-15 04:26:38 +0000930 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000931 if (!X86FastEmitCompare(Op0, Op1, VT))
932 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000933
Dan Gohman84023e02010-07-10 09:00:22 +0000934 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000935 UpdateValueMap(I, ResultReg);
936 return true;
937}
Evan Cheng8b19e562008-09-03 06:44:39 +0000938
Dan Gohman46510a72010-04-15 01:51:59 +0000939bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000940 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000941 if (I->getType()->isIntegerTy(8) &&
942 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000943 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000944 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000945 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000946 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000947 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000948 UpdateValueMap(I, ResultReg);
949 return true;
950 }
951
952 return false;
953}
954
Chris Lattner9a08a612008-10-15 04:26:38 +0000955
Dan Gohman46510a72010-04-15 01:51:59 +0000956bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000957 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000958 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000959 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000960 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
961 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000962
Dan Gohmand98d6202008-10-02 22:15:21 +0000963 // Fold the common case of a conditional branch with a comparison.
Dan Gohman46510a72010-04-15 01:51:59 +0000964 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000965 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000966 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000967
Dan Gohmand98d6202008-10-02 22:15:21 +0000968 // Try to take advantage of fallthrough opportunities.
969 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000970 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000971 std::swap(TrueMBB, FalseMBB);
972 Predicate = CmpInst::getInversePredicate(Predicate);
973 }
974
Chris Lattner871d2462008-10-15 03:58:05 +0000975 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
976 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
977
Dan Gohmand98d6202008-10-02 22:15:21 +0000978 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000979 case CmpInst::FCMP_OEQ:
980 std::swap(TrueMBB, FalseMBB);
981 Predicate = CmpInst::FCMP_UNE;
982 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000983 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
984 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
985 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
986 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
987 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
988 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
989 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
990 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
991 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
992 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
993 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
994 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
995 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000996
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000997 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
998 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
999 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1000 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1001 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1002 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1003 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1004 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1005 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1006 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001007 default:
1008 return false;
1009 }
Chris Lattner54aebde2008-10-15 03:47:17 +00001010
Dan Gohman46510a72010-04-15 01:51:59 +00001011 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001012 if (SwapArgs)
1013 std::swap(Op0, Op1);
1014
Chris Lattner9a08a612008-10-15 04:26:38 +00001015 // Emit a compare of the LHS and RHS, setting the flags.
1016 if (!X86FastEmitCompare(Op0, Op1, VT))
1017 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +00001018
Dan Gohman84023e02010-07-10 09:00:22 +00001019 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1020 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001021
1022 if (Predicate == CmpInst::FCMP_UNE) {
1023 // X86 requires a second branch to handle UNE (and OEQ,
1024 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001025 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1026 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001027 }
1028
Stuart Hastings3bf91252010-06-17 22:43:56 +00001029 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001030 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001031 return true;
1032 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001033 } else if (ExtractValueInst *EI =
1034 dyn_cast<ExtractValueInst>(BI->getCondition())) {
1035 // Check to see if the branch instruction is from an "arithmetic with
1036 // overflow" intrinsic. The main way these intrinsics are used is:
1037 //
1038 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1039 // %sum = extractvalue { i32, i1 } %t, 0
1040 // %obit = extractvalue { i32, i1 } %t, 1
1041 // br i1 %obit, label %overflow, label %normal
1042 //
Dan Gohman653456c2009-01-07 00:15:08 +00001043 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +00001044 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +00001045 // looking for the SETO/SETB instruction. If an instruction modifies the
1046 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1047 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001048 if (const IntrinsicInst *CI =
1049 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001050 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1051 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1052 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001053 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001054
Chris Lattnera9a42252009-04-12 07:36:01 +00001055 for (MachineBasicBlock::const_reverse_iterator
Dan Gohman84023e02010-07-10 09:00:22 +00001056 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1057 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001058 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001059
Evan Cheng1015ba72010-05-21 20:53:24 +00001060 if (MI.definesRegister(Reg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001061 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +00001062
Chris Lattnera9a42252009-04-12 07:36:01 +00001063 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
1064 Reg = Src;
1065 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001066 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001067
Chris Lattnera9a42252009-04-12 07:36:01 +00001068 SetMI = &MI;
1069 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001070 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001071
Chris Lattnera9a42252009-04-12 07:36:01 +00001072 const TargetInstrDesc &TID = MI.getDesc();
1073 if (TID.hasUnmodeledSideEffects() ||
1074 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
1075 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001076 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001077
1078 if (SetMI) {
1079 unsigned OpCode = SetMI->getOpcode();
1080
1081 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohman84023e02010-07-10 09:00:22 +00001082 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1083 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001084 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001085 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001086 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001087 return true;
1088 }
Bill Wendling9a901322008-12-10 19:44:24 +00001089 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001090 }
1091 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001092 }
1093
1094 // Otherwise do a clumsy setcc and re-test it.
1095 unsigned OpReg = getRegForValue(BI->getCondition());
1096 if (OpReg == 0) return false;
1097
Dan Gohman84023e02010-07-10 09:00:22 +00001098 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1099 .addReg(OpReg).addReg(OpReg);
1100 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1101 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001102 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001103 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001104 return true;
1105}
1106
Dan Gohman46510a72010-04-15 01:51:59 +00001107bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +00001108 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001109 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001110 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001111 CReg = X86::CL;
1112 RC = &X86::GR8RegClass;
1113 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001114 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
1115 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
1116 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001117 default: return false;
1118 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001119 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001120 CReg = X86::CX;
1121 RC = &X86::GR16RegClass;
1122 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001123 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1124 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1125 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001126 default: return false;
1127 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001128 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001129 CReg = X86::ECX;
1130 RC = &X86::GR32RegClass;
1131 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001132 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1133 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1134 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001135 default: return false;
1136 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001137 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001138 CReg = X86::RCX;
1139 RC = &X86::GR64RegClass;
1140 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001141 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1142 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1143 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001144 default: return false;
1145 }
1146 } else {
1147 return false;
1148 }
1149
Owen Andersone50ed302009-08-10 22:56:29 +00001150 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001151 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001152 return false;
1153
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001154 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1155 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001156
1157 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001158 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001159 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001160 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001161 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001162 UpdateValueMap(I, ResultReg);
1163 return true;
1164 }
1165
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001166 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1167 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001168 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1169 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001170
1171 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001172 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001173 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001174 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1175 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001176 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001177
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001178 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001179 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1180 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001181 UpdateValueMap(I, ResultReg);
1182 return true;
1183}
1184
Dan Gohman46510a72010-04-15 01:51:59 +00001185bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001186 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001187 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001188 return false;
1189
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001190 unsigned Opc = 0;
1191 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001192 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001193 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001194 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001195 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001196 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001197 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001198 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001199 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001200 RC = &X86::GR64RegClass;
1201 } else {
1202 return false;
1203 }
1204
1205 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1206 if (Op0Reg == 0) return false;
1207 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1208 if (Op1Reg == 0) return false;
1209 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1210 if (Op2Reg == 0) return false;
1211
Dan Gohman84023e02010-07-10 09:00:22 +00001212 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1213 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001214 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001215 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1216 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001217 UpdateValueMap(I, ResultReg);
1218 return true;
1219}
1220
Dan Gohman46510a72010-04-15 01:51:59 +00001221bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001222 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001223 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001224 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001225 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001226 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001227 unsigned OpReg = getRegForValue(V);
1228 if (OpReg == 0) return false;
1229 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001230 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1231 TII.get(X86::CVTSS2SDrr), ResultReg)
1232 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001233 UpdateValueMap(I, ResultReg);
1234 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001235 }
1236 }
1237
1238 return false;
1239}
1240
Dan Gohman46510a72010-04-15 01:51:59 +00001241bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001242 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001243 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001244 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001245 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001246 unsigned OpReg = getRegForValue(V);
1247 if (OpReg == 0) return false;
1248 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001249 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1250 TII.get(X86::CVTSD2SSrr), ResultReg)
1251 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001252 UpdateValueMap(I, ResultReg);
1253 return true;
1254 }
1255 }
1256 }
1257
1258 return false;
1259}
1260
Dan Gohman46510a72010-04-15 01:51:59 +00001261bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001262 if (Subtarget->is64Bit())
1263 // All other cases should be handled by the tblgen generated code.
1264 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001265 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1266 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001267
1268 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001270 // All other cases should be handled by the tblgen generated code.
1271 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001273 // All other cases should be handled by the tblgen generated code.
1274 return false;
1275
1276 unsigned InputReg = getRegForValue(I->getOperand(0));
1277 if (!InputReg)
1278 // Unhandled operand. Halt "fast" selection and bail.
1279 return false;
1280
Dan Gohman62417622009-04-27 16:33:14 +00001281 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001282 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001283 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001284 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001285 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1286 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001287
1288 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001289 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001290 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001291 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001292 if (!ResultReg)
1293 return false;
1294
1295 UpdateValueMap(I, ResultReg);
1296 return true;
1297}
1298
Dan Gohman46510a72010-04-15 01:51:59 +00001299bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1300 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1301 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001302
Dan Gohman46510a72010-04-15 01:51:59 +00001303 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001304 switch (CI->getIntrinsicID()) {
1305 default: break;
1306 case Intrinsic::sadd_with_overflow:
Dan Gohman84023e02010-07-10 09:00:22 +00001307 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001308 // Cheat a little. We know that the registers for "add" and "seto" are
1309 // allocated sequentially. However, we only keep track of the register
1310 // for "add" in the value map. Use extractvalue's index to get the
1311 // correct register for "seto".
Dan Gohman84023e02010-07-10 09:00:22 +00001312 unsigned OpReg = getRegForValue(Agg);
1313 if (OpReg == 0)
1314 return false;
1315 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001316 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001317 }
Dan Gohman84023e02010-07-10 09:00:22 +00001318 }
Bill Wendling52370a12008-12-09 02:42:50 +00001319 }
1320
1321 return false;
1322}
1323
Dan Gohman46510a72010-04-15 01:51:59 +00001324bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001325 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001326 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001327 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001328 case Intrinsic::stackprotector: {
1329 // Emit code inline code to store the stack guard onto the stack.
1330 EVT PtrTy = TLI.getPointerTy();
1331
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001332 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1333 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001334
1335 // Grab the frame index.
1336 X86AddressMode AM;
1337 if (!X86SelectAddress(Slot, AM)) return false;
1338
Eric Christopher88dee302010-03-18 21:58:33 +00001339 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1340
Eric Christopher07754c22010-03-18 20:27:26 +00001341 return true;
1342 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001343 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001344 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001345 const Type *Ty = I.getCalledFunction()->getReturnType();
1346
1347 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1348
1349 EVT VT;
1350 if (!isTypeLegal(Ty, VT))
1351 return false;
1352
1353 unsigned OpC = 0;
1354 if (VT == MVT::i32)
1355 OpC = X86::MOV32ri;
1356 else if (VT == MVT::i64)
1357 OpC = X86::MOV64ri;
1358 else
1359 return false;
1360
1361 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001362 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001363 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001364 UpdateValueMap(&I, ResultReg);
1365 return true;
1366 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001367 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001368 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001369 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001370 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001371 if (!X86SelectAddress(DI->getAddress(), AM))
1372 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001373 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001374 // FIXME may need to add RegState::Debug to any registers produced,
1375 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001376 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1377 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001378 return true;
1379 }
Eric Christopher77f79892010-01-18 22:11:29 +00001380 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001382 return true;
1383 }
Bill Wendling52370a12008-12-09 02:42:50 +00001384 case Intrinsic::sadd_with_overflow:
1385 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001386 // Replace "add with overflow" intrinsics with an "add" instruction followed
1387 // by a seto/setc instruction. Later on, when the "extractvalue"
1388 // instructions are encountered, we use the fact that two registers were
1389 // created sequentially to get the correct registers for the "sum" and the
1390 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001391 const Function *Callee = I.getCalledFunction();
1392 const Type *RetTy =
1393 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1394
Owen Andersone50ed302009-08-10 22:56:29 +00001395 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001396 if (!isTypeLegal(RetTy, VT))
1397 return false;
1398
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001399 const Value *Op1 = I.getArgOperand(0);
1400 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001401 unsigned Reg1 = getRegForValue(Op1);
1402 unsigned Reg2 = getRegForValue(Op2);
1403
1404 if (Reg1 == 0 || Reg2 == 0)
1405 // FIXME: Handle values *not* in registers.
1406 return false;
1407
1408 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001410 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001412 OpC = X86::ADD64rr;
1413 else
1414 return false;
1415
1416 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001417 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1418 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001419 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001420
Chris Lattner8d57b772009-04-12 07:51:14 +00001421 // If the add with overflow is an intra-block value then we just want to
1422 // create temporaries for it like normal. If it is a cross-block value then
1423 // UpdateValueMap will return the cross-block register used. Since we
1424 // *really* want the value to be live in the register pair known by
1425 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1426 // the cross block case. In the non-cross-block case, we should just make
1427 // another register for the value.
1428 if (DestReg1 != ResultReg)
1429 ResultReg = DestReg1+1;
1430 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001431 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001432
Chris Lattnera9a42252009-04-12 07:36:01 +00001433 unsigned Opc = X86::SETBr;
1434 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1435 Opc = X86::SETOr;
Dan Gohman84023e02010-07-10 09:00:22 +00001436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001437 return true;
1438 }
1439 }
1440}
1441
Dan Gohman46510a72010-04-15 01:51:59 +00001442bool X86FastISel::X86SelectCall(const Instruction *I) {
1443 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001444 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001445
1446 // Can't handle inline asm yet.
1447 if (isa<InlineAsm>(Callee))
1448 return false;
1449
Bill Wendling52370a12008-12-09 02:42:50 +00001450 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001451 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001452 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001453
Evan Chengf3d4efe2008-09-07 09:09:33 +00001454 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001455 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001456 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001457 if (CC != CallingConv::C &&
1458 CC != CallingConv::Fast &&
1459 CC != CallingConv::X86_FastCall)
1460 return false;
1461
Evan Cheng381993f2010-01-27 00:00:57 +00001462 // fastcc with -tailcallopt is intended to provide a guaranteed
1463 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001464 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001465 return false;
1466
Evan Chengf3d4efe2008-09-07 09:09:33 +00001467 // Let SDISel handle vararg functions.
1468 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1469 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1470 if (FTy->isVarArg())
1471 return false;
1472
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001473 // Fast-isel doesn't know about callee-pop yet.
1474 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1475 return false;
1476
Evan Chengf3d4efe2008-09-07 09:09:33 +00001477 // Handle *simple* calls for now.
1478 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001479 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001480 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001481 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001482 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001483 return false;
1484
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001485 // Materialize callee address in a register. FIXME: GV address can be
1486 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001487 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001488 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001489 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001490 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001491 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001492 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001493 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001494 } else if (CalleeAM.Base.Reg != 0) {
1495 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001496 } else
1497 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001498
Evan Chengdebdea02008-09-08 17:15:42 +00001499 // Allow calls which produce i1 results.
1500 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001501 if (RetVT == MVT::i1) {
1502 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001503 AndToI1 = true;
1504 }
1505
Evan Chengf3d4efe2008-09-07 09:09:33 +00001506 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001507 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001508 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001509 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001510 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001511 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001512 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001513 ArgVTs.reserve(CS.arg_size());
1514 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001515 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001516 i != e; ++i) {
1517 unsigned Arg = getRegForValue(*i);
1518 if (Arg == 0)
1519 return false;
1520 ISD::ArgFlagsTy Flags;
1521 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001522 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001523 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001524 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001525 Flags.setZExt();
1526
1527 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001528 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1529 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1530 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1531 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001532 return false;
1533
1534 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001535 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001536 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001537 return false;
1538 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1539 Flags.setOrigAlign(OriginalAlignment);
1540
1541 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001542 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001543 ArgVTs.push_back(ArgVT);
1544 ArgFlags.push_back(Flags);
1545 }
1546
1547 // Analyze operands of the call, assigning locations to each operand.
1548 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001549 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Dan Gohmand8acddd2010-06-01 21:09:47 +00001550
1551 // Allocate shadow area for Win64
1552 if (Subtarget->isTargetWin64()) {
1553 CCInfo.AllocateStack(32, 8);
1554 }
1555
Evan Chengf3d4efe2008-09-07 09:09:33 +00001556 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1557
1558 // Get a count of how many bytes are to be pushed on the stack.
1559 unsigned NumBytes = CCInfo.getNextStackOffset();
1560
1561 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001562 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001563 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1564 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001565
Chris Lattner438949a2008-10-15 05:30:52 +00001566 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001567 // copies / loads.
1568 SmallVector<unsigned, 4> RegArgs;
1569 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1570 CCValAssign &VA = ArgLocs[i];
1571 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001572 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001573
1574 // Promote the value if needed.
1575 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001576 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001577 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001578 case CCValAssign::SExt: {
1579 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1580 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001581 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001582 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001583 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001584 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001585 }
1586 case CCValAssign::ZExt: {
1587 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1588 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001589 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001590 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001591 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001592 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001593 }
1594 case CCValAssign::AExt: {
1595 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1596 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001597 if (!Emitted)
1598 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001599 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001600 if (!Emitted)
1601 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1602 Arg, ArgVT, Arg);
1603
Chris Lattnera33649e2008-12-19 17:03:38 +00001604 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001605 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001606 break;
1607 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001608 case CCValAssign::BCvt: {
1609 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001610 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001611 assert(BC != 0 && "Failed to emit a bitcast!");
1612 Arg = BC;
1613 ArgVT = VA.getLocVT();
1614 break;
1615 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001616 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001617
1618 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001619 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1620 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001621 RegArgs.push_back(VA.getLocReg());
1622 } else {
1623 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001624 X86AddressMode AM;
1625 AM.Base.Reg = StackPtr;
1626 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001627 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001628
1629 // If this is a really simple value, emit this with the Value* version of
1630 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1631 // can cause us to reevaluate the argument.
1632 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1633 X86FastEmitStore(ArgVT, ArgVal, AM);
1634 else
1635 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001636 }
1637 }
1638
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001639 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1640 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001641 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001642 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001643 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1644 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001645 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001646
Evan Chengf3d4efe2008-09-07 09:09:33 +00001647 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001648 MachineInstrBuilder MIB;
1649 if (CalleeOp) {
1650 // Register-indirect call.
1651 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001652 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1653 .addReg(CalleeOp);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001654
1655 } else {
1656 // Direct call.
1657 assert(GV && "Not a direct call");
1658 unsigned CallOpc =
1659 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1660
1661 // See if we need any target-specific flags on the GV operand.
1662 unsigned char OpFlags = 0;
1663
1664 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1665 // external symbols most go through the PLT in PIC mode. If the symbol
1666 // has hidden or protected visibility, or if it is static or local, then
1667 // we don't need to use the PLT - we can directly call it.
1668 if (Subtarget->isTargetELF() &&
1669 TM.getRelocationModel() == Reloc::PIC_ &&
1670 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1671 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001672 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001673 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1674 Subtarget->getDarwinVers() < 9) {
1675 // PC-relative references to external symbols should go through $stub,
1676 // unless we're building with the leopard linker or later, which
1677 // automatically synthesizes these stubs.
1678 OpFlags = X86II::MO_DARWIN_STUB;
1679 }
1680
1681
Dan Gohman84023e02010-07-10 09:00:22 +00001682 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1683 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001684 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001685
1686 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001687 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001688 MIB.addReg(X86::EBX);
1689
Evan Chengf3d4efe2008-09-07 09:09:33 +00001690 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001691 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1692 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001693
1694 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001695 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001696 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1697 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001698
1699 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001700 SmallVector<unsigned, 4> UsedRegs;
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001702 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001703 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001704 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1705
1706 // Copy all of the result registers out of their specified physreg.
1707 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001708 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001709 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001710
1711 // If this is a call to a function that returns an fp value on the x87 fp
1712 // stack, but where we prefer to use the value in xmm registers, copy it
1713 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1714 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1715 RVLocs[0].getLocReg() == X86::ST1) &&
1716 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001717 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001718 DstRC = X86::RFP80RegisterClass;
1719 }
1720
1721 unsigned ResultReg = createResultReg(DstRC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001722 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1723 ResultReg).addReg(RVLocs[0].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001724 UsedRegs.push_back(RVLocs[0].getLocReg());
1725
Evan Chengf3d4efe2008-09-07 09:09:33 +00001726 if (CopyVT != RVLocs[0].getValVT()) {
1727 // Round the F80 the right size, which also moves to the appropriate xmm
1728 // register. This is accomplished by storing the F80 value in memory and
1729 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001730 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001731 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001732 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001733 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001734 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1735 TII.get(Opc)), FI)
1736 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001737 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001738 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001739 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001740 ResultReg = createResultReg(DstRC);
Dan Gohman84023e02010-07-10 09:00:22 +00001741 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1742 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001743 }
1744
Evan Chengdebdea02008-09-08 17:15:42 +00001745 if (AndToI1) {
1746 // Mask out all but lowest bit for some call which produces an i1.
1747 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001748 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001749 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001750 ResultReg = AndResult;
1751 }
1752
Evan Chengf3d4efe2008-09-07 09:09:33 +00001753 UpdateValueMap(I, ResultReg);
1754 }
1755
Dan Gohmandb497122010-06-18 23:28:01 +00001756 // Set all unused physreg defs as dead.
1757 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1758
Evan Chengf3d4efe2008-09-07 09:09:33 +00001759 return true;
1760}
1761
1762
Dan Gohman99b21822008-08-28 23:21:34 +00001763bool
Dan Gohman46510a72010-04-15 01:51:59 +00001764X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001765 switch (I->getOpcode()) {
1766 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001767 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001768 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001769 case Instruction::Store:
1770 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001771 case Instruction::Ret:
1772 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001773 case Instruction::ICmp:
1774 case Instruction::FCmp:
1775 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001776 case Instruction::ZExt:
1777 return X86SelectZExt(I);
1778 case Instruction::Br:
1779 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001780 case Instruction::Call:
1781 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001782 case Instruction::LShr:
1783 case Instruction::AShr:
1784 case Instruction::Shl:
1785 return X86SelectShift(I);
1786 case Instruction::Select:
1787 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001788 case Instruction::Trunc:
1789 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001790 case Instruction::FPExt:
1791 return X86SelectFPExt(I);
1792 case Instruction::FPTrunc:
1793 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001794 case Instruction::ExtractValue:
1795 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001796 case Instruction::IntToPtr: // Deliberate fall-through.
1797 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001798 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1799 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001800 if (DstVT.bitsGT(SrcVT))
1801 return X86SelectZExt(I);
1802 if (DstVT.bitsLT(SrcVT))
1803 return X86SelectTrunc(I);
1804 unsigned Reg = getRegForValue(I->getOperand(0));
1805 if (Reg == 0) return false;
1806 UpdateValueMap(I, Reg);
1807 return true;
1808 }
Dan Gohman99b21822008-08-28 23:21:34 +00001809 }
1810
1811 return false;
1812}
1813
Dan Gohman46510a72010-04-15 01:51:59 +00001814unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001815 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001816 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001817 return false;
1818
1819 // Get opcode and regclass of the output for the given load instruction.
1820 unsigned Opc = 0;
1821 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001823 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001825 Opc = X86::MOV8rm;
1826 RC = X86::GR8RegisterClass;
1827 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001829 Opc = X86::MOV16rm;
1830 RC = X86::GR16RegisterClass;
1831 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001832 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001833 Opc = X86::MOV32rm;
1834 RC = X86::GR32RegisterClass;
1835 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001836 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001837 // Must be in x86-64 mode.
1838 Opc = X86::MOV64rm;
1839 RC = X86::GR64RegisterClass;
1840 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001842 if (Subtarget->hasSSE1()) {
1843 Opc = X86::MOVSSrm;
1844 RC = X86::FR32RegisterClass;
1845 } else {
1846 Opc = X86::LD_Fp32m;
1847 RC = X86::RFP32RegisterClass;
1848 }
1849 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001851 if (Subtarget->hasSSE2()) {
1852 Opc = X86::MOVSDrm;
1853 RC = X86::FR64RegisterClass;
1854 } else {
1855 Opc = X86::LD_Fp64m;
1856 RC = X86::RFP64RegisterClass;
1857 }
1858 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001860 // No f80 support yet.
1861 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001862 }
1863
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001864 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001865 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001866 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001867 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001869 Opc = X86::LEA32r;
1870 else
1871 Opc = X86::LEA64r;
1872 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001873 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1874 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001875 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001876 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001877 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001878 }
1879
Owen Anderson3b217c62008-09-06 01:11:01 +00001880 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001881 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001882 if (Align == 0) {
1883 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001884 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001885 }
Owen Anderson95267a12008-09-05 00:06:23 +00001886
Dan Gohman5396c992008-09-30 01:21:32 +00001887 // x86-32 PIC requires a PIC base register for constant pools.
1888 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001889 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001890 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001891 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001892 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001893 } else if (Subtarget->isPICStyleGOT()) {
1894 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001895 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001896 } else if (Subtarget->isPICStyleRIPRel() &&
1897 TM.getCodeModel() == CodeModel::Small) {
1898 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001899 }
Dan Gohman5396c992008-09-30 01:21:32 +00001900
1901 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001902 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001903 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001904 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1905 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001906 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001907
Owen Anderson95267a12008-09-05 00:06:23 +00001908 return ResultReg;
1909}
1910
Dan Gohman46510a72010-04-15 01:51:59 +00001911unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001912 // Fail on dynamic allocas. At this point, getRegForValue has already
1913 // checked its CSE maps, so if we're here trying to handle a dynamic
1914 // alloca, we're not going to succeed. X86SelectAddress has a
1915 // check for dynamic allocas, because it's called directly from
1916 // various places, but TargetMaterializeAlloca also needs a check
1917 // in order to avoid recursion between getRegForValue,
1918 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001919 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001920 return 0;
1921
Dan Gohman0586d912008-09-10 20:11:02 +00001922 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001923 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001924 return 0;
1925 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1926 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1927 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001928 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1929 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001930 return ResultReg;
1931}
1932
Evan Chengc3f44b02008-09-03 00:03:49 +00001933namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001934 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1935 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001936 }
Dan Gohman99b21822008-08-28 23:21:34 +00001937}