blob: cbbc2fdc759fde32ec87b966d94a0a1a65a05f6b [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
Chris Lattnerbeac75d2010-09-05 02:18:34 +000066 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
67 /// vreg is being provided by the specified load instruction. If possible,
68 /// try to fold the load as an operand to the instruction, returning true if
69 /// possible.
70 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
71 const LoadInst *LI);
72
Dan Gohman1adf1b02008-08-19 21:45:35 +000073#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000074
75private:
Dan Gohman46510a72010-04-15 01:51:59 +000076 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000077
Owen Andersone50ed302009-08-10 22:56:29 +000078 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000079
Dan Gohman46510a72010-04-15 01:51:59 +000080 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000081 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000082 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000083 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000084
Owen Andersone50ed302009-08-10 22:56:29 +000085 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000086 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000087
Dan Gohman46510a72010-04-15 01:51:59 +000088 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
89 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000090
Dan Gohman46510a72010-04-15 01:51:59 +000091 bool X86SelectLoad(const Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000092
Dan Gohman46510a72010-04-15 01:51:59 +000093 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000094
Dan Gohman84023e02010-07-10 09:00:22 +000095 bool X86SelectRet(const Instruction *I);
96
Dan Gohman46510a72010-04-15 01:51:59 +000097 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000098
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000100
Dan Gohman46510a72010-04-15 01:51:59 +0000101 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000102
Dan Gohman46510a72010-04-15 01:51:59 +0000103 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000104
Dan Gohman46510a72010-04-15 01:51:59 +0000105 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 bool X86SelectTrunc(const Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000108
Dan Gohman46510a72010-04-15 01:51:59 +0000109 bool X86SelectFPExt(const Instruction *I);
110 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000111
Dan Gohman46510a72010-04-15 01:51:59 +0000112 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000113
Dan Gohman46510a72010-04-15 01:51:59 +0000114 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
115 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000116
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000117 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000118
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000119 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000120 return getTargetMachine()->getInstrInfo();
121 }
122 const X86TargetMachine *getTargetMachine() const {
123 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000124 }
125
Dan Gohman46510a72010-04-15 01:51:59 +0000126 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000127
Dan Gohman46510a72010-04-15 01:51:59 +0000128 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000129
130 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
131 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000132 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
134 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000135 }
136
Owen Andersone50ed302009-08-10 22:56:29 +0000137 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000138};
Chris Lattner087fcf32009-03-08 18:44:31 +0000139
140} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000141
Owen Andersone50ed302009-08-10 22:56:29 +0000142bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000143 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000145 // Unhandled type. Halt "fast" selection and bail.
146 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000147
Dan Gohman9b66d732008-09-30 00:48:39 +0000148 // For now, require SSE/SSE2 for performing floating-point operations,
149 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000151 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000153 return false;
154 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000156 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000157 // We only handle legal types. For example, on x86-32 the instruction
158 // selector contains all of the 64-bit instructions from x86-64,
159 // under the assumption that i64 won't be used if the target doesn't
160 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000162}
163
164#include "X86GenCallingConv.inc"
165
166/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
167/// convention.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000168CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
169 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000170 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +0000171 if (CC == CallingConv::GHC)
172 return CC_X86_64_GHC;
173 else if (Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000174 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000175 else
176 return CC_X86_64_C;
177 }
178
179 if (CC == CallingConv::X86_FastCall)
180 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000181 else if (CC == CallingConv::X86_ThisCall)
182 return CC_X86_32_ThisCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000183 else if (CC == CallingConv::Fast)
184 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +0000185 else if (CC == CallingConv::GHC)
186 return CC_X86_32_GHC;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000187 else
188 return CC_X86_32_C;
189}
190
Evan Cheng0de588f2008-09-05 21:00:03 +0000191/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000192/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000193/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000194bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000195 unsigned &ResultReg) {
196 // Get opcode and regclass of the output for the given load instruction.
197 unsigned Opc = 0;
198 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000200 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000201 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 Opc = X86::MOV8rm;
204 RC = X86::GR8RegisterClass;
205 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000207 Opc = X86::MOV16rm;
208 RC = X86::GR16RegisterClass;
209 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000211 Opc = X86::MOV32rm;
212 RC = X86::GR32RegisterClass;
213 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000215 // Must be in x86-64 mode.
216 Opc = X86::MOV64rm;
217 RC = X86::GR64RegisterClass;
218 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 if (Subtarget->hasSSE1()) {
221 Opc = X86::MOVSSrm;
222 RC = X86::FR32RegisterClass;
223 } else {
224 Opc = X86::LD_Fp32m;
225 RC = X86::RFP32RegisterClass;
226 }
227 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000229 if (Subtarget->hasSSE2()) {
230 Opc = X86::MOVSDrm;
231 RC = X86::FR64RegisterClass;
232 } else {
233 Opc = X86::LD_Fp64m;
234 RC = X86::RFP64RegisterClass;
235 }
236 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000238 // No f80 support yet.
239 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000240 }
241
242 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000243 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
244 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000245 return true;
246}
247
Evan Chengf3d4efe2008-09-07 09:09:33 +0000248/// X86FastEmitStore - Emit a machine instruction to store a value Val of
249/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
250/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000251/// i.e. V. Return true if it is possible.
252bool
Owen Andersone50ed302009-08-10 22:56:29 +0000253X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000254 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000255 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000256 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 switch (VT.getSimpleVT().SimpleTy) {
258 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000259 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000260 case MVT::i1: {
261 // Mask out all but lowest bit.
262 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000263 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000264 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
265 Val = AndResult;
266 }
267 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 case MVT::i8: Opc = X86::MOV8mr; break;
269 case MVT::i16: Opc = X86::MOV16mr; break;
270 case MVT::i32: Opc = X86::MOV32mr; break;
271 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
272 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000273 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000274 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000276 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000277 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000278 }
Chris Lattner438949a2008-10-15 05:30:52 +0000279
Dan Gohman84023e02010-07-10 09:00:22 +0000280 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
281 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000282 return true;
283}
284
Dan Gohman46510a72010-04-15 01:51:59 +0000285bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000286 const X86AddressMode &AM) {
287 // Handle 'null' like i32/i64 0.
288 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000289 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000290
291 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000292 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000293 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000294 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000296 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000297 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 case MVT::i8: Opc = X86::MOV8mi; break;
299 case MVT::i16: Opc = X86::MOV16mi; break;
300 case MVT::i32: Opc = X86::MOV32mi; break;
301 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000302 // Must be a 32-bit sign extended value.
303 if ((int)CI->getSExtValue() == CI->getSExtValue())
304 Opc = X86::MOV64mi32;
305 break;
306 }
307
308 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000309 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
310 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000311 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000312 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000313 return true;
314 }
315 }
316
317 unsigned ValReg = getRegForValue(Val);
318 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000319 return false;
320
321 return X86FastEmitStore(VT, ValReg, AM);
322}
323
Evan Cheng24e3a902008-09-08 06:35:17 +0000324/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
325/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
326/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000327bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
328 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000329 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000330 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
331 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000332
333 if (RR != 0) {
334 ResultReg = RR;
335 return true;
336 } else
337 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000338}
339
Dan Gohman0586d912008-09-10 20:11:02 +0000340/// X86SelectAddress - Attempt to fill in an address from the given value.
341///
Dan Gohman46510a72010-04-15 01:51:59 +0000342bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
343 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000344 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000345 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000346 // Don't walk into other basic blocks; it's possible we haven't
347 // visited them yet, so the instructions may not yet be assigned
348 // virtual registers.
Dan Gohman84023e02010-07-10 09:00:22 +0000349 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
Dan Gohmanea9f1512010-06-18 20:44:47 +0000350 return false;
351
Dan Gohman35893082008-09-18 23:23:44 +0000352 Opcode = I->getOpcode();
353 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000354 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000355 Opcode = C->getOpcode();
356 U = C;
357 }
Dan Gohman0586d912008-09-10 20:11:02 +0000358
Chris Lattner868ee942010-06-15 19:08:40 +0000359 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
360 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000361 // Fast instruction selection doesn't support the special
362 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000363 return false;
364
Dan Gohman35893082008-09-18 23:23:44 +0000365 switch (Opcode) {
366 default: break;
367 case Instruction::BitCast:
368 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000369 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000370
371 case Instruction::IntToPtr:
372 // Look past no-op inttoptrs.
373 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000374 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000375 break;
Dan Gohman35893082008-09-18 23:23:44 +0000376
377 case Instruction::PtrToInt:
378 // Look past no-op ptrtoints.
379 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000380 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000381 break;
Dan Gohman35893082008-09-18 23:23:44 +0000382
383 case Instruction::Alloca: {
384 // Do static allocas.
385 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000386 DenseMap<const AllocaInst*, int>::iterator SI =
387 FuncInfo.StaticAllocaMap.find(A);
388 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000389 AM.BaseType = X86AddressMode::FrameIndexBase;
390 AM.Base.FrameIndex = SI->second;
391 return true;
392 }
393 break;
Dan Gohman35893082008-09-18 23:23:44 +0000394 }
395
396 case Instruction::Add: {
397 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000398 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000399 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
400 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000401 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000402 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000403 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000404 }
Dan Gohman0586d912008-09-10 20:11:02 +0000405 }
Dan Gohman35893082008-09-18 23:23:44 +0000406 break;
407 }
408
409 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000410 X86AddressMode SavedAM = AM;
411
Dan Gohman35893082008-09-18 23:23:44 +0000412 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000413 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000414 unsigned IndexReg = AM.IndexReg;
415 unsigned Scale = AM.Scale;
416 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000417 // Iterate through the indices, folding what we can. Constants can be
418 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000419 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000420 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000421 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000422 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
423 const StructLayout *SL = TD.getStructLayout(STy);
424 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
425 Disp += SL->getElementOffset(Idx);
426 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000427 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman5c87bf62010-07-01 02:27:15 +0000428 SmallVector<const Value *, 4> Worklist;
429 Worklist.push_back(Op);
430 do {
431 Op = Worklist.pop_back_val();
432 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
433 // Constant-offset addressing.
434 Disp += CI->getSExtValue() * S;
Dan Gohmanabd1d852010-07-01 02:58:21 +0000435 } else if (isa<AddOperator>(Op) &&
436 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
437 // An add with a constant operand. Fold the constant.
438 ConstantInt *CI =
439 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
440 Disp += CI->getSExtValue() * S;
441 // Add the other operand back to the work list.
442 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
Dan Gohman5c87bf62010-07-01 02:27:15 +0000443 } else if (IndexReg == 0 &&
444 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
445 (S == 1 || S == 2 || S == 4 || S == 8)) {
446 // Scaled-index addressing.
447 Scale = S;
448 IndexReg = getRegForGEPIndex(Op).first;
449 if (IndexReg == 0)
450 return false;
Dan Gohman5c87bf62010-07-01 02:27:15 +0000451 } else
452 // Unsupported.
453 goto unsupported_gep;
454 } while (!Worklist.empty());
Dan Gohman35893082008-09-18 23:23:44 +0000455 }
456 }
Dan Gohman09aae462008-09-26 20:04:15 +0000457 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000458 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000459 break;
Dan Gohman35893082008-09-18 23:23:44 +0000460 // Ok, the GEP indices were covered by constant-offset and scaled-index
461 // addressing. Update the address state and move on to examining the base.
462 AM.IndexReg = IndexReg;
463 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000464 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000465 if (X86SelectAddress(U->getOperand(0), AM))
466 return true;
467
468 // If we couldn't merge the sub value into this addr mode, revert back to
469 // our address and just match the value instead of completely failing.
470 AM = SavedAM;
471 break;
Dan Gohman35893082008-09-18 23:23:44 +0000472 unsupported_gep:
473 // Ok, the GEP indices weren't all covered.
474 break;
475 }
476 }
477
478 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000479 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000480 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000481 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000482 return false;
483
Dan Gohman97135e12008-09-26 19:15:30 +0000484 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000485 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000486 (AM.Base.Reg != 0 || AM.IndexReg != 0))
487 return false;
488
Dan Gohmane9865942009-02-23 22:03:08 +0000489 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000490 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000491 if (GVar->isThreadLocal())
492 return false;
493
Chris Lattnerff7727f2009-07-09 06:41:35 +0000494 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000495 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000496
Chris Lattner0d786dd2009-07-10 07:48:51 +0000497 // Allow the subtarget to classify the global.
498 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
499
500 // If this reference is relative to the pic base, set it now.
501 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000502 // FIXME: How do we know Base.Reg is free??
Dan Gohmana4160c32010-07-07 16:29:44 +0000503 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000504 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000505
506 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000507 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000508 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000509 if (Subtarget->isPICStyleRIPRel()) {
510 // Use rip-relative addressing if we can. Above we verified that the
511 // base and index registers are unused.
512 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
513 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000514 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000515 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000516 return true;
517 }
518
Chris Lattner0d786dd2009-07-10 07:48:51 +0000519 // Ok, we need to do a load from a stub. If we've already loaded from this
520 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000521 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
522 unsigned LoadReg;
523 if (I != LocalValueMap.end() && I->second != 0) {
524 LoadReg = I->second;
525 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000526 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000527 unsigned Opc = 0;
528 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000529 X86AddressMode StubAM;
530 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000531 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000532 StubAM.GVOpFlags = GVFlags;
533
Dan Gohman84023e02010-07-10 09:00:22 +0000534 // Prepare for inserting code in the local-value area.
Dan Gohmana10b8492010-07-14 01:07:44 +0000535 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000536
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000538 Opc = X86::MOV64rm;
539 RC = X86::GR64RegisterClass;
540
Chris Lattner0d786dd2009-07-10 07:48:51 +0000541 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000542 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000543 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000544 Opc = X86::MOV32rm;
545 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000546 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000547
548 LoadReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000549 MachineInstrBuilder LoadMI =
550 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
551 addFullAddress(LoadMI, StubAM);
552
553 // Ok, back to normal mode.
554 leaveLocalValueArea(SaveInsertPt);
555
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000556 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000557 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000558 }
Chris Lattner18c59872009-06-27 04:16:01 +0000559
Chris Lattnerff7727f2009-07-09 06:41:35 +0000560 // Now construct the final address. Note that the Disp, Scale,
561 // and Index values may already be set here.
562 AM.Base.Reg = LoadReg;
563 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000564 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000565 }
566
Dan Gohman97135e12008-09-26 19:15:30 +0000567 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000568 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000569 if (AM.Base.Reg == 0) {
570 AM.Base.Reg = getRegForValue(V);
571 return AM.Base.Reg != 0;
572 }
573 if (AM.IndexReg == 0) {
574 assert(AM.Scale == 1 && "Scale with no index!");
575 AM.IndexReg = getRegForValue(V);
576 return AM.IndexReg != 0;
577 }
578 }
579
580 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000581}
582
Chris Lattner0aa43de2009-07-10 05:33:42 +0000583/// X86SelectCallAddress - Attempt to fill in an address from the given value.
584///
Dan Gohman46510a72010-04-15 01:51:59 +0000585bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
586 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000587 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000588 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000589 Opcode = I->getOpcode();
590 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000591 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000592 Opcode = C->getOpcode();
593 U = C;
594 }
595
596 switch (Opcode) {
597 default: break;
598 case Instruction::BitCast:
599 // Look past bitcasts.
600 return X86SelectCallAddress(U->getOperand(0), AM);
601
602 case Instruction::IntToPtr:
603 // Look past no-op inttoptrs.
604 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
605 return X86SelectCallAddress(U->getOperand(0), AM);
606 break;
607
608 case Instruction::PtrToInt:
609 // Look past no-op ptrtoints.
610 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
611 return X86SelectCallAddress(U->getOperand(0), AM);
612 break;
613 }
614
615 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000616 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000617 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000618 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000619 return false;
620
621 // RIP-relative addresses can't have additional register operands.
622 if (Subtarget->isPICStyleRIPRel() &&
623 (AM.Base.Reg != 0 || AM.IndexReg != 0))
624 return false;
625
Chris Lattner754b7652009-07-10 05:48:03 +0000626 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000627 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000628 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000629 return false;
630
631 // Okay, we've committed to selecting this global. Set up the basic address.
632 AM.GV = GV;
633
Chris Lattnere6c07b52009-07-10 05:45:15 +0000634 // No ABI requires an extra load for anything other than DLLImport, which
635 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000636 if (Subtarget->isPICStyleRIPRel()) {
637 // Use rip-relative addressing if we can. Above we verified that the
638 // base and index registers are unused.
639 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
640 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000641 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000642 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
643 } else if (Subtarget->isPICStyleGOT()) {
644 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000645 }
646
Chris Lattner0aa43de2009-07-10 05:33:42 +0000647 return true;
648 }
649
650 // If all else fails, try to materialize the value in a register.
651 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
652 if (AM.Base.Reg == 0) {
653 AM.Base.Reg = getRegForValue(V);
654 return AM.Base.Reg != 0;
655 }
656 if (AM.IndexReg == 0) {
657 assert(AM.Scale == 1 && "Scale with no index!");
658 AM.IndexReg = getRegForValue(V);
659 return AM.IndexReg != 0;
660 }
661 }
662
663 return false;
664}
665
666
Owen Andersona3971df2008-09-04 07:08:58 +0000667/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000668bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000669 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000670 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000671 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000672
Dan Gohman0586d912008-09-10 20:11:02 +0000673 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000674 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000675 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000676
Chris Lattner438949a2008-10-15 05:30:52 +0000677 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000678}
679
Dan Gohman84023e02010-07-10 09:00:22 +0000680/// X86SelectRet - Select and emit code to implement ret instructions.
681bool X86FastISel::X86SelectRet(const Instruction *I) {
682 const ReturnInst *Ret = cast<ReturnInst>(I);
683 const Function &F = *I->getParent()->getParent();
684
685 if (!FuncInfo.CanLowerReturn)
686 return false;
687
688 CallingConv::ID CC = F.getCallingConv();
689 if (CC != CallingConv::C &&
690 CC != CallingConv::Fast &&
691 CC != CallingConv::X86_FastCall)
692 return false;
693
694 if (Subtarget->isTargetWin64())
695 return false;
696
697 // Don't handle popping bytes on return for now.
698 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
699 ->getBytesToPopOnReturn() != 0)
700 return 0;
701
702 // fastcc with -tailcallopt is intended to provide a guaranteed
703 // tail call optimization. Fastisel doesn't know how to do that.
704 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
705 return false;
706
707 // Let SDISel handle vararg functions.
708 if (F.isVarArg())
709 return false;
710
711 if (Ret->getNumOperands() > 0) {
712 SmallVector<ISD::OutputArg, 4> Outs;
713 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
714 Outs, TLI);
715
716 // Analyze operands of the call, assigning locations to each operand.
717 SmallVector<CCValAssign, 16> ValLocs;
718 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000719 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000720
721 const Value *RV = Ret->getOperand(0);
722 unsigned Reg = getRegForValue(RV);
723 if (Reg == 0)
724 return false;
725
726 // Only handle a single return value for now.
727 if (ValLocs.size() != 1)
728 return false;
729
730 CCValAssign &VA = ValLocs[0];
731
732 // Don't bother handling odd stuff for now.
733 if (VA.getLocInfo() != CCValAssign::Full)
734 return false;
735 // Only handle register returns for now.
736 if (!VA.isRegLoc())
737 return false;
738 // TODO: For now, don't try to handle cases where getLocInfo()
739 // says Full but the types don't match.
740 if (VA.getValVT() != TLI.getValueType(RV->getType()))
741 return false;
742
743 // The calling-convention tables for x87 returns don't tell
744 // the whole story.
745 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
746 return false;
747
748 // Make the copy.
749 unsigned SrcReg = Reg + VA.getValNo();
750 unsigned DstReg = VA.getLocReg();
751 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000752 // Avoid a cross-class copy. This is very unlikely.
753 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000754 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000755 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
756 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000757
758 // Mark the register as live out of the function.
759 MRI.addLiveOut(VA.getLocReg());
760 }
761
762 // Now emit the RET.
763 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
764 return true;
765}
766
Evan Cheng8b19e562008-09-03 06:44:39 +0000767/// X86SelectLoad - Select and emit code to implement load instructions.
768///
Dan Gohman46510a72010-04-15 01:51:59 +0000769bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000770 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000771 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000772 return false;
773
Dan Gohman0586d912008-09-10 20:11:02 +0000774 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000775 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000776 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000777
Evan Cheng0de588f2008-09-05 21:00:03 +0000778 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000779 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000780 UpdateValueMap(I, ResultReg);
781 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000782 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000783 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000784}
785
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000786static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000788 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 case MVT::i8: return X86::CMP8rr;
790 case MVT::i16: return X86::CMP16rr;
791 case MVT::i32: return X86::CMP32rr;
792 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000793 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
794 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000795 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000796}
797
Chris Lattner0e13c782008-10-15 04:13:29 +0000798/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
799/// of the comparison, return an opcode that works for the compare (e.g.
800/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000801static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000803 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000804 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 case MVT::i8: return X86::CMP8ri;
806 case MVT::i16: return X86::CMP16ri;
807 case MVT::i32: return X86::CMP32ri;
808 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000809 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
810 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000811 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000812 return X86::CMP64ri32;
813 return 0;
814 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000815}
816
Dan Gohman46510a72010-04-15 01:51:59 +0000817bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
818 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000819 unsigned Op0Reg = getRegForValue(Op0);
820 if (Op0Reg == 0) return false;
821
Chris Lattnerd53886b2008-10-15 05:18:04 +0000822 // Handle 'null' like i32/i64 0.
823 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000824 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000825
Chris Lattner9a08a612008-10-15 04:26:38 +0000826 // We have two options: compare with register or immediate. If the RHS of
827 // the compare is an immediate that we can fold into this compare, use
828 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000829 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000830 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000831 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
832 .addReg(Op0Reg)
833 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000834 return true;
835 }
836 }
837
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000838 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000839 if (CompareOpc == 0) return false;
840
841 unsigned Op1Reg = getRegForValue(Op1);
842 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000843 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
844 .addReg(Op0Reg)
845 .addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000846
847 return true;
848}
849
Dan Gohman46510a72010-04-15 01:51:59 +0000850bool X86FastISel::X86SelectCmp(const Instruction *I) {
851 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000852
Owen Andersone50ed302009-08-10 22:56:29 +0000853 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000854 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000855 return false;
856
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000857 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000858 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000859 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000860 switch (CI->getPredicate()) {
861 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000862 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
863 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000864
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000865 unsigned EReg = createResultReg(&X86::GR8RegClass);
866 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000867 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
868 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
869 TII.get(X86::SETNPr), NPReg);
870 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000871 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000872 UpdateValueMap(I, ResultReg);
873 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000874 }
875 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000876 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
877 return false;
878
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000879 unsigned NEReg = createResultReg(&X86::GR8RegClass);
880 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000881 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
882 TII.get(X86::SETNEr), NEReg);
883 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
884 TII.get(X86::SETPr), PReg);
885 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
886 TII.get(X86::OR8rr), ResultReg)
887 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000888 UpdateValueMap(I, ResultReg);
889 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000890 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000891 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
892 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
893 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
894 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
895 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
896 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
897 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
898 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
899 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
900 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
901 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
902 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
903
904 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
905 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
906 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
907 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
908 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
909 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
910 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
911 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
912 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
913 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000914 default:
915 return false;
916 }
917
Dan Gohman46510a72010-04-15 01:51:59 +0000918 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000919 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000920 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000921
Chris Lattner9a08a612008-10-15 04:26:38 +0000922 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000923 if (!X86FastEmitCompare(Op0, Op1, VT))
924 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000925
Dan Gohman84023e02010-07-10 09:00:22 +0000926 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000927 UpdateValueMap(I, ResultReg);
928 return true;
929}
Evan Cheng8b19e562008-09-03 06:44:39 +0000930
Dan Gohman46510a72010-04-15 01:51:59 +0000931bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000932 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000933 if (I->getType()->isIntegerTy(8) &&
934 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000935 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000936 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000937 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000938 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000939 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000940 UpdateValueMap(I, ResultReg);
941 return true;
942 }
943
944 return false;
945}
946
Chris Lattner9a08a612008-10-15 04:26:38 +0000947
Dan Gohman46510a72010-04-15 01:51:59 +0000948bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000949 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000950 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000951 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000952 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
953 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000954
Dan Gohman8bef7442010-08-21 02:32:36 +0000955 // Fold the common case of a conditional branch with a comparison
956 // in the same block (values defined on other blocks may not have
957 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000958 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000959 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000960 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000961
Dan Gohmand98d6202008-10-02 22:15:21 +0000962 // Try to take advantage of fallthrough opportunities.
963 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000964 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000965 std::swap(TrueMBB, FalseMBB);
966 Predicate = CmpInst::getInversePredicate(Predicate);
967 }
968
Chris Lattner871d2462008-10-15 03:58:05 +0000969 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
970 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
971
Dan Gohmand98d6202008-10-02 22:15:21 +0000972 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000973 case CmpInst::FCMP_OEQ:
974 std::swap(TrueMBB, FalseMBB);
975 Predicate = CmpInst::FCMP_UNE;
976 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000977 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
978 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
979 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
980 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
981 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
982 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
983 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
984 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
985 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
986 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
987 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
988 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
989 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000990
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000991 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
992 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
993 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
994 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
995 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
996 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
997 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
998 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
999 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1000 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001001 default:
1002 return false;
1003 }
Chris Lattner54aebde2008-10-15 03:47:17 +00001004
Dan Gohman46510a72010-04-15 01:51:59 +00001005 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001006 if (SwapArgs)
1007 std::swap(Op0, Op1);
1008
Chris Lattner9a08a612008-10-15 04:26:38 +00001009 // Emit a compare of the LHS and RHS, setting the flags.
1010 if (!X86FastEmitCompare(Op0, Op1, VT))
1011 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +00001012
Dan Gohman84023e02010-07-10 09:00:22 +00001013 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1014 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001015
1016 if (Predicate == CmpInst::FCMP_UNE) {
1017 // X86 requires a second branch to handle UNE (and OEQ,
1018 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001019 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1020 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001021 }
1022
Stuart Hastings3bf91252010-06-17 22:43:56 +00001023 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001024 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001025 return true;
1026 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001027 } else if (ExtractValueInst *EI =
1028 dyn_cast<ExtractValueInst>(BI->getCondition())) {
1029 // Check to see if the branch instruction is from an "arithmetic with
1030 // overflow" intrinsic. The main way these intrinsics are used is:
1031 //
1032 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1033 // %sum = extractvalue { i32, i1 } %t, 0
1034 // %obit = extractvalue { i32, i1 } %t, 1
1035 // br i1 %obit, label %overflow, label %normal
1036 //
Dan Gohman653456c2009-01-07 00:15:08 +00001037 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +00001038 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +00001039 // looking for the SETO/SETB instruction. If an instruction modifies the
1040 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1041 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001042 if (const IntrinsicInst *CI =
1043 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001044 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1045 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1046 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001047 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001048
Chris Lattnera9a42252009-04-12 07:36:01 +00001049 for (MachineBasicBlock::const_reverse_iterator
Dan Gohman84023e02010-07-10 09:00:22 +00001050 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1051 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001052 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001053
Evan Cheng1015ba72010-05-21 20:53:24 +00001054 if (MI.definesRegister(Reg)) {
Jakob Stoklund Olesen84d499a2010-07-16 22:35:34 +00001055 if (MI.isCopy()) {
1056 Reg = MI.getOperand(1).getReg();
Chris Lattnera9a42252009-04-12 07:36:01 +00001057 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001058 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001059
Chris Lattnera9a42252009-04-12 07:36:01 +00001060 SetMI = &MI;
1061 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001062 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001063
Chris Lattnera9a42252009-04-12 07:36:01 +00001064 const TargetInstrDesc &TID = MI.getDesc();
1065 if (TID.hasUnmodeledSideEffects() ||
1066 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
1067 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001068 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001069
1070 if (SetMI) {
1071 unsigned OpCode = SetMI->getOpcode();
1072
1073 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohman84023e02010-07-10 09:00:22 +00001074 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1075 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001076 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001077 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001078 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001079 return true;
1080 }
Bill Wendling9a901322008-12-10 19:44:24 +00001081 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001082 }
1083 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001084 }
1085
1086 // Otherwise do a clumsy setcc and re-test it.
1087 unsigned OpReg = getRegForValue(BI->getCondition());
1088 if (OpReg == 0) return false;
1089
Dan Gohman84023e02010-07-10 09:00:22 +00001090 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1091 .addReg(OpReg).addReg(OpReg);
1092 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1093 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001094 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001095 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001096 return true;
1097}
1098
Dan Gohman46510a72010-04-15 01:51:59 +00001099bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +00001100 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001101 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001102 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001103 CReg = X86::CL;
1104 RC = &X86::GR8RegClass;
1105 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001106 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
1107 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
1108 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001109 default: return false;
1110 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001111 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001112 CReg = X86::CX;
1113 RC = &X86::GR16RegClass;
1114 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001115 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1116 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1117 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001118 default: return false;
1119 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001120 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001121 CReg = X86::ECX;
1122 RC = &X86::GR32RegClass;
1123 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001124 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1125 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1126 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001127 default: return false;
1128 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001129 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001130 CReg = X86::RCX;
1131 RC = &X86::GR64RegClass;
1132 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001133 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1134 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1135 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001136 default: return false;
1137 }
1138 } else {
1139 return false;
1140 }
1141
Owen Andersone50ed302009-08-10 22:56:29 +00001142 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001144 return false;
1145
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001146 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1147 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001148
1149 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001150 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001151 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001152 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001153 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001154 UpdateValueMap(I, ResultReg);
1155 return true;
1156 }
1157
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001158 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1159 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001160 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1161 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001162
1163 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001164 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001165 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001166 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1167 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001168 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001169
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001170 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001171 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1172 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001173 UpdateValueMap(I, ResultReg);
1174 return true;
1175}
1176
Dan Gohman46510a72010-04-15 01:51:59 +00001177bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001178 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001180 return false;
1181
Eric Christophere487b012010-09-29 23:00:29 +00001182 // We only use cmov here, if we don't have a cmov instruction bail.
1183 if (!Subtarget->hasCMov()) return false;
1184
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001185 unsigned Opc = 0;
1186 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001187 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001188 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001189 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001190 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001191 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001192 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001193 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001194 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001195 RC = &X86::GR64RegClass;
1196 } else {
1197 return false;
1198 }
1199
1200 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1201 if (Op0Reg == 0) return false;
1202 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1203 if (Op1Reg == 0) return false;
1204 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1205 if (Op2Reg == 0) return false;
1206
Dan Gohman84023e02010-07-10 09:00:22 +00001207 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1208 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001209 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001210 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1211 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001212 UpdateValueMap(I, ResultReg);
1213 return true;
1214}
1215
Dan Gohman46510a72010-04-15 01:51:59 +00001216bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001217 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001218 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001219 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001220 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001221 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001222 unsigned OpReg = getRegForValue(V);
1223 if (OpReg == 0) return false;
1224 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1226 TII.get(X86::CVTSS2SDrr), ResultReg)
1227 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001228 UpdateValueMap(I, ResultReg);
1229 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001230 }
1231 }
1232
1233 return false;
1234}
1235
Dan Gohman46510a72010-04-15 01:51:59 +00001236bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001237 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001238 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001239 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001240 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001241 unsigned OpReg = getRegForValue(V);
1242 if (OpReg == 0) return false;
1243 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1245 TII.get(X86::CVTSD2SSrr), ResultReg)
1246 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001247 UpdateValueMap(I, ResultReg);
1248 return true;
1249 }
1250 }
1251 }
1252
1253 return false;
1254}
1255
Dan Gohman46510a72010-04-15 01:51:59 +00001256bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001257 if (Subtarget->is64Bit())
1258 // All other cases should be handled by the tblgen generated code.
1259 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001260 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1261 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001262
1263 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001265 // All other cases should be handled by the tblgen generated code.
1266 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001268 // All other cases should be handled by the tblgen generated code.
1269 return false;
1270
1271 unsigned InputReg = getRegForValue(I->getOperand(0));
1272 if (!InputReg)
1273 // Unhandled operand. Halt "fast" selection and bail.
1274 return false;
1275
Dan Gohman62417622009-04-27 16:33:14 +00001276 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001277 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001278 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001279 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001280 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1281 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001282
1283 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001284 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001285 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001286 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001287 if (!ResultReg)
1288 return false;
1289
1290 UpdateValueMap(I, ResultReg);
1291 return true;
1292}
1293
Dan Gohman46510a72010-04-15 01:51:59 +00001294bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1295 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1296 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001297
Dan Gohman46510a72010-04-15 01:51:59 +00001298 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001299 switch (CI->getIntrinsicID()) {
1300 default: break;
1301 case Intrinsic::sadd_with_overflow:
Dan Gohman84023e02010-07-10 09:00:22 +00001302 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001303 // Cheat a little. We know that the registers for "add" and "seto" are
1304 // allocated sequentially. However, we only keep track of the register
1305 // for "add" in the value map. Use extractvalue's index to get the
1306 // correct register for "seto".
Dan Gohman84023e02010-07-10 09:00:22 +00001307 unsigned OpReg = getRegForValue(Agg);
1308 if (OpReg == 0)
1309 return false;
1310 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001311 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001312 }
Dan Gohman84023e02010-07-10 09:00:22 +00001313 }
Bill Wendling52370a12008-12-09 02:42:50 +00001314 }
1315
1316 return false;
1317}
1318
Dan Gohman46510a72010-04-15 01:51:59 +00001319bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001320 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001321 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001322 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001323 case Intrinsic::stackprotector: {
1324 // Emit code inline code to store the stack guard onto the stack.
1325 EVT PtrTy = TLI.getPointerTy();
1326
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001327 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1328 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001329
1330 // Grab the frame index.
1331 X86AddressMode AM;
1332 if (!X86SelectAddress(Slot, AM)) return false;
1333
Eric Christopher88dee302010-03-18 21:58:33 +00001334 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1335
Eric Christopher07754c22010-03-18 20:27:26 +00001336 return true;
1337 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001338 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001339 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001340 const Type *Ty = I.getCalledFunction()->getReturnType();
1341
1342 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1343
1344 EVT VT;
1345 if (!isTypeLegal(Ty, VT))
1346 return false;
1347
1348 unsigned OpC = 0;
1349 if (VT == MVT::i32)
1350 OpC = X86::MOV32ri;
1351 else if (VT == MVT::i64)
1352 OpC = X86::MOV64ri;
1353 else
1354 return false;
1355
1356 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001357 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001358 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001359 UpdateValueMap(&I, ResultReg);
1360 return true;
1361 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001362 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001363 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001364 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001365 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001366 if (!X86SelectAddress(DI->getAddress(), AM))
1367 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001368 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001369 // FIXME may need to add RegState::Debug to any registers produced,
1370 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001371 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1372 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001373 return true;
1374 }
Eric Christopher77f79892010-01-18 22:11:29 +00001375 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001376 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001377 return true;
1378 }
Bill Wendling52370a12008-12-09 02:42:50 +00001379 case Intrinsic::sadd_with_overflow:
1380 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001381 // Replace "add with overflow" intrinsics with an "add" instruction followed
1382 // by a seto/setc instruction. Later on, when the "extractvalue"
1383 // instructions are encountered, we use the fact that two registers were
1384 // created sequentially to get the correct registers for the "sum" and the
1385 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001386 const Function *Callee = I.getCalledFunction();
1387 const Type *RetTy =
1388 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1389
Owen Andersone50ed302009-08-10 22:56:29 +00001390 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001391 if (!isTypeLegal(RetTy, VT))
1392 return false;
1393
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001394 const Value *Op1 = I.getArgOperand(0);
1395 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001396 unsigned Reg1 = getRegForValue(Op1);
1397 unsigned Reg2 = getRegForValue(Op2);
1398
1399 if (Reg1 == 0 || Reg2 == 0)
1400 // FIXME: Handle values *not* in registers.
1401 return false;
1402
1403 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001404 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001405 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001406 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001407 OpC = X86::ADD64rr;
1408 else
1409 return false;
1410
1411 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001412 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1413 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001414 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001415
Chris Lattner8d57b772009-04-12 07:51:14 +00001416 // If the add with overflow is an intra-block value then we just want to
1417 // create temporaries for it like normal. If it is a cross-block value then
1418 // UpdateValueMap will return the cross-block register used. Since we
1419 // *really* want the value to be live in the register pair known by
1420 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1421 // the cross block case. In the non-cross-block case, we should just make
1422 // another register for the value.
1423 if (DestReg1 != ResultReg)
1424 ResultReg = DestReg1+1;
1425 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001427
Chris Lattnera9a42252009-04-12 07:36:01 +00001428 unsigned Opc = X86::SETBr;
1429 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1430 Opc = X86::SETOr;
Dan Gohman84023e02010-07-10 09:00:22 +00001431 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001432 return true;
1433 }
1434 }
1435}
1436
Dan Gohman46510a72010-04-15 01:51:59 +00001437bool X86FastISel::X86SelectCall(const Instruction *I) {
1438 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001439 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001440
1441 // Can't handle inline asm yet.
1442 if (isa<InlineAsm>(Callee))
1443 return false;
1444
Bill Wendling52370a12008-12-09 02:42:50 +00001445 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001446 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001447 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001448
Evan Chengf3d4efe2008-09-07 09:09:33 +00001449 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001450 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001451 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001452 if (CC != CallingConv::C &&
1453 CC != CallingConv::Fast &&
1454 CC != CallingConv::X86_FastCall)
1455 return false;
1456
Evan Cheng381993f2010-01-27 00:00:57 +00001457 // fastcc with -tailcallopt is intended to provide a guaranteed
1458 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001459 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001460 return false;
1461
Evan Chengf3d4efe2008-09-07 09:09:33 +00001462 // Let SDISel handle vararg functions.
1463 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1464 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1465 if (FTy->isVarArg())
1466 return false;
1467
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001468 // Fast-isel doesn't know about callee-pop yet.
1469 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1470 return false;
1471
Evan Chengf3d4efe2008-09-07 09:09:33 +00001472 // Handle *simple* calls for now.
1473 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001474 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001475 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001476 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001477 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001478 return false;
1479
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001480 // Materialize callee address in a register. FIXME: GV address can be
1481 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001482 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001483 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001484 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001485 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001486 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001487 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001488 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001489 } else if (CalleeAM.Base.Reg != 0) {
1490 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001491 } else
1492 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001493
Evan Chengdebdea02008-09-08 17:15:42 +00001494 // Allow calls which produce i1 results.
1495 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001496 if (RetVT == MVT::i1) {
1497 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001498 AndToI1 = true;
1499 }
1500
Evan Chengf3d4efe2008-09-07 09:09:33 +00001501 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001502 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001503 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001504 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001505 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001506 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001507 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001508 ArgVTs.reserve(CS.arg_size());
1509 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001510 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001511 i != e; ++i) {
1512 unsigned Arg = getRegForValue(*i);
1513 if (Arg == 0)
1514 return false;
1515 ISD::ArgFlagsTy Flags;
1516 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001517 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001518 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001519 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001520 Flags.setZExt();
1521
1522 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001523 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1524 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1525 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1526 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001527 return false;
1528
1529 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001530 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001531 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001532 return false;
1533 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1534 Flags.setOrigAlign(OriginalAlignment);
1535
1536 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001537 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001538 ArgVTs.push_back(ArgVT);
1539 ArgFlags.push_back(Flags);
1540 }
1541
1542 // Analyze operands of the call, assigning locations to each operand.
1543 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001544 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Dan Gohmand8acddd2010-06-01 21:09:47 +00001545
1546 // Allocate shadow area for Win64
1547 if (Subtarget->isTargetWin64()) {
1548 CCInfo.AllocateStack(32, 8);
1549 }
1550
Evan Chengf3d4efe2008-09-07 09:09:33 +00001551 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1552
1553 // Get a count of how many bytes are to be pushed on the stack.
1554 unsigned NumBytes = CCInfo.getNextStackOffset();
1555
1556 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001557 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001558 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1559 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001560
Chris Lattner438949a2008-10-15 05:30:52 +00001561 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001562 // copies / loads.
1563 SmallVector<unsigned, 4> RegArgs;
1564 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1565 CCValAssign &VA = ArgLocs[i];
1566 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001567 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001568
1569 // Promote the value if needed.
1570 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001571 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001572 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001573 case CCValAssign::SExt: {
1574 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1575 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001576 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001577 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001578 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001579 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001580 }
1581 case CCValAssign::ZExt: {
1582 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1583 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001584 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001585 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001586 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001587 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001588 }
1589 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001590 // We don't handle MMX parameters yet.
1591 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1592 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001593 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1594 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001595 if (!Emitted)
1596 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001597 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001598 if (!Emitted)
1599 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1600 Arg, ArgVT, Arg);
1601
Chris Lattnera33649e2008-12-19 17:03:38 +00001602 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001603 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001604 break;
1605 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001606 case CCValAssign::BCvt: {
1607 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001608 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001609 assert(BC != 0 && "Failed to emit a bitcast!");
1610 Arg = BC;
1611 ArgVT = VA.getLocVT();
1612 break;
1613 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001614 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001615
1616 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001617 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1618 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001619 RegArgs.push_back(VA.getLocReg());
1620 } else {
1621 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001622 X86AddressMode AM;
1623 AM.Base.Reg = StackPtr;
1624 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001625 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001626
1627 // If this is a really simple value, emit this with the Value* version of
1628 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1629 // can cause us to reevaluate the argument.
1630 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1631 X86FastEmitStore(ArgVT, ArgVal, AM);
1632 else
1633 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001634 }
1635 }
1636
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001637 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1638 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001639 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001640 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001641 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1642 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001643 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001644
Evan Chengf3d4efe2008-09-07 09:09:33 +00001645 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001646 MachineInstrBuilder MIB;
1647 if (CalleeOp) {
1648 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001649 unsigned CallOpc;
1650 if (Subtarget->isTargetWin64())
1651 CallOpc = X86::WINCALL64r;
1652 else if (Subtarget->is64Bit())
1653 CallOpc = X86::CALL64r;
1654 else
1655 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001656 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1657 .addReg(CalleeOp);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001658
1659 } else {
1660 // Direct call.
1661 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001662 unsigned CallOpc;
1663 if (Subtarget->isTargetWin64())
1664 CallOpc = X86::WINCALL64pcrel32;
1665 else if (Subtarget->is64Bit())
1666 CallOpc = X86::CALL64pcrel32;
1667 else
1668 CallOpc = X86::CALLpcrel32;
Chris Lattner51e8eab2009-07-09 06:34:26 +00001669
1670 // See if we need any target-specific flags on the GV operand.
1671 unsigned char OpFlags = 0;
1672
1673 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1674 // external symbols most go through the PLT in PIC mode. If the symbol
1675 // has hidden or protected visibility, or if it is static or local, then
1676 // we don't need to use the PLT - we can directly call it.
1677 if (Subtarget->isTargetELF() &&
1678 TM.getRelocationModel() == Reloc::PIC_ &&
1679 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1680 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001681 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001682 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1683 Subtarget->getDarwinVers() < 9) {
1684 // PC-relative references to external symbols should go through $stub,
1685 // unless we're building with the leopard linker or later, which
1686 // automatically synthesizes these stubs.
1687 OpFlags = X86II::MO_DARWIN_STUB;
1688 }
1689
1690
Dan Gohman84023e02010-07-10 09:00:22 +00001691 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1692 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001693 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001694
1695 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001696 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001697 MIB.addReg(X86::EBX);
1698
Evan Chengf3d4efe2008-09-07 09:09:33 +00001699 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001700 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1701 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001702
1703 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001704 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001705 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1706 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001707
1708 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001709 SmallVector<unsigned, 4> UsedRegs;
Owen Anderson825b72b2009-08-11 20:47:22 +00001710 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001711 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001712 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001713 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1714
1715 // Copy all of the result registers out of their specified physreg.
1716 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001717 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001718 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001719
1720 // If this is a call to a function that returns an fp value on the x87 fp
1721 // stack, but where we prefer to use the value in xmm registers, copy it
1722 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1723 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1724 RVLocs[0].getLocReg() == X86::ST1) &&
1725 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001726 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001727 DstRC = X86::RFP80RegisterClass;
1728 }
1729
1730 unsigned ResultReg = createResultReg(DstRC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001731 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1732 ResultReg).addReg(RVLocs[0].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001733 UsedRegs.push_back(RVLocs[0].getLocReg());
1734
Evan Chengf3d4efe2008-09-07 09:09:33 +00001735 if (CopyVT != RVLocs[0].getValVT()) {
1736 // Round the F80 the right size, which also moves to the appropriate xmm
1737 // register. This is accomplished by storing the F80 value in memory and
1738 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001739 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001740 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001741 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001742 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001743 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1744 TII.get(Opc)), FI)
1745 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001746 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001747 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001748 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001749 ResultReg = createResultReg(DstRC);
Dan Gohman84023e02010-07-10 09:00:22 +00001750 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1751 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001752 }
1753
Evan Chengdebdea02008-09-08 17:15:42 +00001754 if (AndToI1) {
1755 // Mask out all but lowest bit for some call which produces an i1.
1756 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001757 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001758 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001759 ResultReg = AndResult;
1760 }
1761
Evan Chengf3d4efe2008-09-07 09:09:33 +00001762 UpdateValueMap(I, ResultReg);
1763 }
1764
Dan Gohmandb497122010-06-18 23:28:01 +00001765 // Set all unused physreg defs as dead.
1766 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1767
Evan Chengf3d4efe2008-09-07 09:09:33 +00001768 return true;
1769}
1770
1771
Dan Gohman99b21822008-08-28 23:21:34 +00001772bool
Dan Gohman46510a72010-04-15 01:51:59 +00001773X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001774 switch (I->getOpcode()) {
1775 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001776 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001777 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001778 case Instruction::Store:
1779 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001780 case Instruction::Ret:
1781 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001782 case Instruction::ICmp:
1783 case Instruction::FCmp:
1784 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001785 case Instruction::ZExt:
1786 return X86SelectZExt(I);
1787 case Instruction::Br:
1788 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001789 case Instruction::Call:
1790 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001791 case Instruction::LShr:
1792 case Instruction::AShr:
1793 case Instruction::Shl:
1794 return X86SelectShift(I);
1795 case Instruction::Select:
1796 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001797 case Instruction::Trunc:
1798 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001799 case Instruction::FPExt:
1800 return X86SelectFPExt(I);
1801 case Instruction::FPTrunc:
1802 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001803 case Instruction::ExtractValue:
1804 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001805 case Instruction::IntToPtr: // Deliberate fall-through.
1806 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001807 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1808 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001809 if (DstVT.bitsGT(SrcVT))
1810 return X86SelectZExt(I);
1811 if (DstVT.bitsLT(SrcVT))
1812 return X86SelectTrunc(I);
1813 unsigned Reg = getRegForValue(I->getOperand(0));
1814 if (Reg == 0) return false;
1815 UpdateValueMap(I, Reg);
1816 return true;
1817 }
Dan Gohman99b21822008-08-28 23:21:34 +00001818 }
1819
1820 return false;
1821}
1822
Dan Gohman46510a72010-04-15 01:51:59 +00001823unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001824 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001825 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001826 return false;
1827
1828 // Get opcode and regclass of the output for the given load instruction.
1829 unsigned Opc = 0;
1830 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001831 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001832 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001834 Opc = X86::MOV8rm;
1835 RC = X86::GR8RegisterClass;
1836 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001837 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001838 Opc = X86::MOV16rm;
1839 RC = X86::GR16RegisterClass;
1840 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001842 Opc = X86::MOV32rm;
1843 RC = X86::GR32RegisterClass;
1844 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001845 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001846 // Must be in x86-64 mode.
1847 Opc = X86::MOV64rm;
1848 RC = X86::GR64RegisterClass;
1849 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001851 if (Subtarget->hasSSE1()) {
1852 Opc = X86::MOVSSrm;
1853 RC = X86::FR32RegisterClass;
1854 } else {
1855 Opc = X86::LD_Fp32m;
1856 RC = X86::RFP32RegisterClass;
1857 }
1858 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001860 if (Subtarget->hasSSE2()) {
1861 Opc = X86::MOVSDrm;
1862 RC = X86::FR64RegisterClass;
1863 } else {
1864 Opc = X86::LD_Fp64m;
1865 RC = X86::RFP64RegisterClass;
1866 }
1867 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001869 // No f80 support yet.
1870 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001871 }
1872
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001873 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001874 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001875 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001876 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001878 Opc = X86::LEA32r;
1879 else
1880 Opc = X86::LEA64r;
1881 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001882 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1883 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001884 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001885 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001886 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001887 }
1888
Owen Anderson3b217c62008-09-06 01:11:01 +00001889 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001890 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001891 if (Align == 0) {
1892 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001893 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001894 }
Owen Anderson95267a12008-09-05 00:06:23 +00001895
Dan Gohman5396c992008-09-30 01:21:32 +00001896 // x86-32 PIC requires a PIC base register for constant pools.
1897 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001898 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001899 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001900 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001901 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001902 } else if (Subtarget->isPICStyleGOT()) {
1903 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001904 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001905 } else if (Subtarget->isPICStyleRIPRel() &&
1906 TM.getCodeModel() == CodeModel::Small) {
1907 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001908 }
Dan Gohman5396c992008-09-30 01:21:32 +00001909
1910 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001911 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001912 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001913 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1914 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001915 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001916
Owen Anderson95267a12008-09-05 00:06:23 +00001917 return ResultReg;
1918}
1919
Dan Gohman46510a72010-04-15 01:51:59 +00001920unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001921 // Fail on dynamic allocas. At this point, getRegForValue has already
1922 // checked its CSE maps, so if we're here trying to handle a dynamic
1923 // alloca, we're not going to succeed. X86SelectAddress has a
1924 // check for dynamic allocas, because it's called directly from
1925 // various places, but TargetMaterializeAlloca also needs a check
1926 // in order to avoid recursion between getRegForValue,
1927 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001928 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001929 return 0;
1930
Dan Gohman0586d912008-09-10 20:11:02 +00001931 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001932 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001933 return 0;
1934 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1935 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1936 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001937 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1938 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001939 return ResultReg;
1940}
1941
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001942/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
1943/// vreg is being provided by the specified load instruction. If possible,
1944/// try to fold the load as an operand to the instruction, returning true if
1945/// possible.
1946bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
1947 const LoadInst *LI) {
1948 X86AddressMode AM;
1949 if (!X86SelectAddress(LI->getOperand(0), AM))
1950 return false;
1951
1952 X86InstrInfo &XII = (X86InstrInfo&)TII;
1953
1954 unsigned Size = TD.getTypeAllocSize(LI->getType());
1955 unsigned Alignment = LI->getAlignment();
1956
1957 SmallVector<MachineOperand, 8> AddrOps;
1958 AM.getFullAddress(AddrOps);
1959
1960 MachineInstr *Result =
1961 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
1962 if (Result == 0) return false;
1963
1964 MI->getParent()->insert(MI, Result);
1965 MI->eraseFromParent();
1966 return true;
1967}
1968
1969
Evan Chengc3f44b02008-09-03 00:03:49 +00001970namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001971 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1972 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001973 }
Dan Gohman99b21822008-08-28 23:21:34 +00001974}