blob: e7813716e47fd1258208fa60faf80689e61b943b [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);
Dan Gohman84023e02010-07-10 09:00:22 +0000118 CCAssignFn *CCAssignFnForRet(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000119
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000120 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000121 return getTargetMachine()->getInstrInfo();
122 }
123 const X86TargetMachine *getTargetMachine() const {
124 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000125 }
126
Dan Gohman46510a72010-04-15 01:51:59 +0000127 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000128
Dan Gohman46510a72010-04-15 01:51:59 +0000129 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000130
131 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
132 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000133 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
135 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000136 }
137
Owen Andersone50ed302009-08-10 22:56:29 +0000138 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000139};
Chris Lattner087fcf32009-03-08 18:44:31 +0000140
141} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000142
Owen Andersone50ed302009-08-10 22:56:29 +0000143bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000144 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000146 // Unhandled type. Halt "fast" selection and bail.
147 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000148
Dan Gohman9b66d732008-09-30 00:48:39 +0000149 // For now, require SSE/SSE2 for performing floating-point operations,
150 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000152 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000154 return false;
155 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000157 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000158 // We only handle legal types. For example, on x86-32 the instruction
159 // selector contains all of the 64-bit instructions from x86-64,
160 // under the assumption that i64 won't be used if the target doesn't
161 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000163}
164
165#include "X86GenCallingConv.inc"
166
167/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
168/// convention.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000169CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
170 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000171 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +0000172 if (CC == CallingConv::GHC)
173 return CC_X86_64_GHC;
174 else if (Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000175 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000176 else
177 return CC_X86_64_C;
178 }
179
180 if (CC == CallingConv::X86_FastCall)
181 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000182 else if (CC == CallingConv::X86_ThisCall)
183 return CC_X86_32_ThisCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000184 else if (CC == CallingConv::Fast)
185 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +0000186 else if (CC == CallingConv::GHC)
187 return CC_X86_32_GHC;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000188 else
189 return CC_X86_32_C;
190}
191
Dan Gohman84023e02010-07-10 09:00:22 +0000192/// CCAssignFnForRet - Selects the correct CCAssignFn for a given calling
193/// convention.
194CCAssignFn *X86FastISel::CCAssignFnForRet(CallingConv::ID CC,
195 bool isTaillCall) {
196 if (Subtarget->is64Bit()) {
197 if (Subtarget->isTargetWin64())
198 return RetCC_X86_Win64_C;
199 else
200 return RetCC_X86_64_C;
201 }
202
203 return RetCC_X86_32_C;
204}
205
Evan Cheng0de588f2008-09-05 21:00:03 +0000206/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000207/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000208/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000209bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000210 unsigned &ResultReg) {
211 // Get opcode and regclass of the output for the given load instruction.
212 unsigned Opc = 0;
213 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000215 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000216 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000218 Opc = X86::MOV8rm;
219 RC = X86::GR8RegisterClass;
220 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000222 Opc = X86::MOV16rm;
223 RC = X86::GR16RegisterClass;
224 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000226 Opc = X86::MOV32rm;
227 RC = X86::GR32RegisterClass;
228 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000230 // Must be in x86-64 mode.
231 Opc = X86::MOV64rm;
232 RC = X86::GR64RegisterClass;
233 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000235 if (Subtarget->hasSSE1()) {
236 Opc = X86::MOVSSrm;
237 RC = X86::FR32RegisterClass;
238 } else {
239 Opc = X86::LD_Fp32m;
240 RC = X86::RFP32RegisterClass;
241 }
242 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000244 if (Subtarget->hasSSE2()) {
245 Opc = X86::MOVSDrm;
246 RC = X86::FR64RegisterClass;
247 } else {
248 Opc = X86::LD_Fp64m;
249 RC = X86::RFP64RegisterClass;
250 }
251 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000253 // No f80 support yet.
254 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000255 }
256
257 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000258 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
259 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000260 return true;
261}
262
Evan Chengf3d4efe2008-09-07 09:09:33 +0000263/// X86FastEmitStore - Emit a machine instruction to store a value Val of
264/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
265/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000266/// i.e. V. Return true if it is possible.
267bool
Owen Andersone50ed302009-08-10 22:56:29 +0000268X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000269 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000270 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000271 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 switch (VT.getSimpleVT().SimpleTy) {
273 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000274 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000275 case MVT::i1: {
276 // Mask out all but lowest bit.
277 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000279 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
280 Val = AndResult;
281 }
282 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 case MVT::i8: Opc = X86::MOV8mr; break;
284 case MVT::i16: Opc = X86::MOV16mr; break;
285 case MVT::i32: Opc = X86::MOV32mr; break;
286 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
287 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000288 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000289 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000291 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000292 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000293 }
Chris Lattner438949a2008-10-15 05:30:52 +0000294
Dan Gohman84023e02010-07-10 09:00:22 +0000295 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
296 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000297 return true;
298}
299
Dan Gohman46510a72010-04-15 01:51:59 +0000300bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000301 const X86AddressMode &AM) {
302 // Handle 'null' like i32/i64 0.
303 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000304 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000305
306 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000307 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000308 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000309 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000311 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000312 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 case MVT::i8: Opc = X86::MOV8mi; break;
314 case MVT::i16: Opc = X86::MOV16mi; break;
315 case MVT::i32: Opc = X86::MOV32mi; break;
316 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000317 // Must be a 32-bit sign extended value.
318 if ((int)CI->getSExtValue() == CI->getSExtValue())
319 Opc = X86::MOV64mi32;
320 break;
321 }
322
323 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000324 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
325 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000326 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000327 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000328 return true;
329 }
330 }
331
332 unsigned ValReg = getRegForValue(Val);
333 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000334 return false;
335
336 return X86FastEmitStore(VT, ValReg, AM);
337}
338
Evan Cheng24e3a902008-09-08 06:35:17 +0000339/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
340/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
341/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000342bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
343 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000344 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000345 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
346 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000347
348 if (RR != 0) {
349 ResultReg = RR;
350 return true;
351 } else
352 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000353}
354
Dan Gohman0586d912008-09-10 20:11:02 +0000355/// X86SelectAddress - Attempt to fill in an address from the given value.
356///
Dan Gohman46510a72010-04-15 01:51:59 +0000357bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
358 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000359 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000360 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000361 // Don't walk into other basic blocks; it's possible we haven't
362 // visited them yet, so the instructions may not yet be assigned
363 // virtual registers.
Dan Gohman84023e02010-07-10 09:00:22 +0000364 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
Dan Gohmanea9f1512010-06-18 20:44:47 +0000365 return false;
366
Dan Gohman35893082008-09-18 23:23:44 +0000367 Opcode = I->getOpcode();
368 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000369 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000370 Opcode = C->getOpcode();
371 U = C;
372 }
Dan Gohman0586d912008-09-10 20:11:02 +0000373
Chris Lattner868ee942010-06-15 19:08:40 +0000374 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
375 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000376 // Fast instruction selection doesn't support the special
377 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000378 return false;
379
Dan Gohman35893082008-09-18 23:23:44 +0000380 switch (Opcode) {
381 default: break;
382 case Instruction::BitCast:
383 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000384 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000385
386 case Instruction::IntToPtr:
387 // Look past no-op inttoptrs.
388 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000389 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000390 break;
Dan Gohman35893082008-09-18 23:23:44 +0000391
392 case Instruction::PtrToInt:
393 // Look past no-op ptrtoints.
394 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000395 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000396 break;
Dan Gohman35893082008-09-18 23:23:44 +0000397
398 case Instruction::Alloca: {
399 // Do static allocas.
400 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000401 DenseMap<const AllocaInst*, int>::iterator SI =
402 FuncInfo.StaticAllocaMap.find(A);
403 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000404 AM.BaseType = X86AddressMode::FrameIndexBase;
405 AM.Base.FrameIndex = SI->second;
406 return true;
407 }
408 break;
Dan Gohman35893082008-09-18 23:23:44 +0000409 }
410
411 case Instruction::Add: {
412 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000413 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000414 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
415 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000416 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000417 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000418 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000419 }
Dan Gohman0586d912008-09-10 20:11:02 +0000420 }
Dan Gohman35893082008-09-18 23:23:44 +0000421 break;
422 }
423
424 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000425 X86AddressMode SavedAM = AM;
426
Dan Gohman35893082008-09-18 23:23:44 +0000427 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000428 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000429 unsigned IndexReg = AM.IndexReg;
430 unsigned Scale = AM.Scale;
431 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000432 // Iterate through the indices, folding what we can. Constants can be
433 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000434 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000435 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000436 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000437 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
438 const StructLayout *SL = TD.getStructLayout(STy);
439 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
440 Disp += SL->getElementOffset(Idx);
441 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000442 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman5c87bf62010-07-01 02:27:15 +0000443 SmallVector<const Value *, 4> Worklist;
444 Worklist.push_back(Op);
445 do {
446 Op = Worklist.pop_back_val();
447 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
448 // Constant-offset addressing.
449 Disp += CI->getSExtValue() * S;
Dan Gohmanabd1d852010-07-01 02:58:21 +0000450 } else if (isa<AddOperator>(Op) &&
451 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
452 // An add with a constant operand. Fold the constant.
453 ConstantInt *CI =
454 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
455 Disp += CI->getSExtValue() * S;
456 // Add the other operand back to the work list.
457 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
Dan Gohman5c87bf62010-07-01 02:27:15 +0000458 } else if (IndexReg == 0 &&
459 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
460 (S == 1 || S == 2 || S == 4 || S == 8)) {
461 // Scaled-index addressing.
462 Scale = S;
463 IndexReg = getRegForGEPIndex(Op).first;
464 if (IndexReg == 0)
465 return false;
Dan Gohman5c87bf62010-07-01 02:27:15 +0000466 } else
467 // Unsupported.
468 goto unsupported_gep;
469 } while (!Worklist.empty());
Dan Gohman35893082008-09-18 23:23:44 +0000470 }
471 }
Dan Gohman09aae462008-09-26 20:04:15 +0000472 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000473 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000474 break;
Dan Gohman35893082008-09-18 23:23:44 +0000475 // Ok, the GEP indices were covered by constant-offset and scaled-index
476 // addressing. Update the address state and move on to examining the base.
477 AM.IndexReg = IndexReg;
478 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000479 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000480 if (X86SelectAddress(U->getOperand(0), AM))
481 return true;
482
483 // If we couldn't merge the sub value into this addr mode, revert back to
484 // our address and just match the value instead of completely failing.
485 AM = SavedAM;
486 break;
Dan Gohman35893082008-09-18 23:23:44 +0000487 unsupported_gep:
488 // Ok, the GEP indices weren't all covered.
489 break;
490 }
491 }
492
493 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000494 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000495 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000496 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000497 return false;
498
Dan Gohman97135e12008-09-26 19:15:30 +0000499 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000500 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000501 (AM.Base.Reg != 0 || AM.IndexReg != 0))
502 return false;
503
Dan Gohmane9865942009-02-23 22:03:08 +0000504 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000505 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000506 if (GVar->isThreadLocal())
507 return false;
508
Chris Lattnerff7727f2009-07-09 06:41:35 +0000509 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000510 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000511
Chris Lattner0d786dd2009-07-10 07:48:51 +0000512 // Allow the subtarget to classify the global.
513 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
514
515 // If this reference is relative to the pic base, set it now.
516 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000517 // FIXME: How do we know Base.Reg is free??
Dan Gohmana4160c32010-07-07 16:29:44 +0000518 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000519 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000520
521 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000522 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000523 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000524 if (Subtarget->isPICStyleRIPRel()) {
525 // Use rip-relative addressing if we can. Above we verified that the
526 // base and index registers are unused.
527 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
528 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000529 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000530 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000531 return true;
532 }
533
Chris Lattner0d786dd2009-07-10 07:48:51 +0000534 // Ok, we need to do a load from a stub. If we've already loaded from this
535 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000536 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
537 unsigned LoadReg;
538 if (I != LocalValueMap.end() && I->second != 0) {
539 LoadReg = I->second;
540 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000541 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000542 unsigned Opc = 0;
543 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000544 X86AddressMode StubAM;
545 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000546 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000547 StubAM.GVOpFlags = GVFlags;
548
Dan Gohman84023e02010-07-10 09:00:22 +0000549 // Prepare for inserting code in the local-value area.
Dan Gohmana10b8492010-07-14 01:07:44 +0000550 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000551
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000553 Opc = X86::MOV64rm;
554 RC = X86::GR64RegisterClass;
555
Chris Lattner0d786dd2009-07-10 07:48:51 +0000556 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000557 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000558 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000559 Opc = X86::MOV32rm;
560 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000561 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000562
563 LoadReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000564 MachineInstrBuilder LoadMI =
565 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
566 addFullAddress(LoadMI, StubAM);
567
568 // Ok, back to normal mode.
569 leaveLocalValueArea(SaveInsertPt);
570
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000571 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000572 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000573 }
Chris Lattner18c59872009-06-27 04:16:01 +0000574
Chris Lattnerff7727f2009-07-09 06:41:35 +0000575 // Now construct the final address. Note that the Disp, Scale,
576 // and Index values may already be set here.
577 AM.Base.Reg = LoadReg;
578 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000579 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000580 }
581
Dan Gohman97135e12008-09-26 19:15:30 +0000582 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000583 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000584 if (AM.Base.Reg == 0) {
585 AM.Base.Reg = getRegForValue(V);
586 return AM.Base.Reg != 0;
587 }
588 if (AM.IndexReg == 0) {
589 assert(AM.Scale == 1 && "Scale with no index!");
590 AM.IndexReg = getRegForValue(V);
591 return AM.IndexReg != 0;
592 }
593 }
594
595 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000596}
597
Chris Lattner0aa43de2009-07-10 05:33:42 +0000598/// X86SelectCallAddress - Attempt to fill in an address from the given value.
599///
Dan Gohman46510a72010-04-15 01:51:59 +0000600bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
601 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000602 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000603 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000604 Opcode = I->getOpcode();
605 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000606 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000607 Opcode = C->getOpcode();
608 U = C;
609 }
610
611 switch (Opcode) {
612 default: break;
613 case Instruction::BitCast:
614 // Look past bitcasts.
615 return X86SelectCallAddress(U->getOperand(0), AM);
616
617 case Instruction::IntToPtr:
618 // Look past no-op inttoptrs.
619 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
620 return X86SelectCallAddress(U->getOperand(0), AM);
621 break;
622
623 case Instruction::PtrToInt:
624 // Look past no-op ptrtoints.
625 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
626 return X86SelectCallAddress(U->getOperand(0), AM);
627 break;
628 }
629
630 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000631 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000632 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000633 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000634 return false;
635
636 // RIP-relative addresses can't have additional register operands.
637 if (Subtarget->isPICStyleRIPRel() &&
638 (AM.Base.Reg != 0 || AM.IndexReg != 0))
639 return false;
640
Chris Lattner754b7652009-07-10 05:48:03 +0000641 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000642 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000643 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000644 return false;
645
646 // Okay, we've committed to selecting this global. Set up the basic address.
647 AM.GV = GV;
648
Chris Lattnere6c07b52009-07-10 05:45:15 +0000649 // No ABI requires an extra load for anything other than DLLImport, which
650 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000651 if (Subtarget->isPICStyleRIPRel()) {
652 // Use rip-relative addressing if we can. Above we verified that the
653 // base and index registers are unused.
654 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
655 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000656 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000657 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
658 } else if (Subtarget->isPICStyleGOT()) {
659 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000660 }
661
Chris Lattner0aa43de2009-07-10 05:33:42 +0000662 return true;
663 }
664
665 // If all else fails, try to materialize the value in a register.
666 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
667 if (AM.Base.Reg == 0) {
668 AM.Base.Reg = getRegForValue(V);
669 return AM.Base.Reg != 0;
670 }
671 if (AM.IndexReg == 0) {
672 assert(AM.Scale == 1 && "Scale with no index!");
673 AM.IndexReg = getRegForValue(V);
674 return AM.IndexReg != 0;
675 }
676 }
677
678 return false;
679}
680
681
Owen Andersona3971df2008-09-04 07:08:58 +0000682/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000683bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000684 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000685 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000686 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000687
Dan Gohman0586d912008-09-10 20:11:02 +0000688 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000689 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000690 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000691
Chris Lattner438949a2008-10-15 05:30:52 +0000692 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000693}
694
Dan Gohman84023e02010-07-10 09:00:22 +0000695/// X86SelectRet - Select and emit code to implement ret instructions.
696bool X86FastISel::X86SelectRet(const Instruction *I) {
697 const ReturnInst *Ret = cast<ReturnInst>(I);
698 const Function &F = *I->getParent()->getParent();
699
700 if (!FuncInfo.CanLowerReturn)
701 return false;
702
703 CallingConv::ID CC = F.getCallingConv();
704 if (CC != CallingConv::C &&
705 CC != CallingConv::Fast &&
706 CC != CallingConv::X86_FastCall)
707 return false;
708
709 if (Subtarget->isTargetWin64())
710 return false;
711
712 // Don't handle popping bytes on return for now.
713 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
714 ->getBytesToPopOnReturn() != 0)
715 return 0;
716
717 // fastcc with -tailcallopt is intended to provide a guaranteed
718 // tail call optimization. Fastisel doesn't know how to do that.
719 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
720 return false;
721
722 // Let SDISel handle vararg functions.
723 if (F.isVarArg())
724 return false;
725
726 if (Ret->getNumOperands() > 0) {
727 SmallVector<ISD::OutputArg, 4> Outs;
728 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
729 Outs, TLI);
730
731 // Analyze operands of the call, assigning locations to each operand.
732 SmallVector<CCValAssign, 16> ValLocs;
733 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
734 CCInfo.AnalyzeReturn(Outs, CCAssignFnForRet(CC));
735
736 const Value *RV = Ret->getOperand(0);
737 unsigned Reg = getRegForValue(RV);
738 if (Reg == 0)
739 return false;
740
741 // Only handle a single return value for now.
742 if (ValLocs.size() != 1)
743 return false;
744
745 CCValAssign &VA = ValLocs[0];
746
747 // Don't bother handling odd stuff for now.
748 if (VA.getLocInfo() != CCValAssign::Full)
749 return false;
750 // Only handle register returns for now.
751 if (!VA.isRegLoc())
752 return false;
753 // TODO: For now, don't try to handle cases where getLocInfo()
754 // says Full but the types don't match.
755 if (VA.getValVT() != TLI.getValueType(RV->getType()))
756 return false;
757
758 // The calling-convention tables for x87 returns don't tell
759 // the whole story.
760 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
761 return false;
762
763 // Make the copy.
764 unsigned SrcReg = Reg + VA.getValNo();
765 unsigned DstReg = VA.getLocReg();
766 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000767 // Avoid a cross-class copy. This is very unlikely.
768 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000769 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000770 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
771 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000772
773 // Mark the register as live out of the function.
774 MRI.addLiveOut(VA.getLocReg());
775 }
776
777 // Now emit the RET.
778 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
779 return true;
780}
781
Evan Cheng8b19e562008-09-03 06:44:39 +0000782/// X86SelectLoad - Select and emit code to implement load instructions.
783///
Dan Gohman46510a72010-04-15 01:51:59 +0000784bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000785 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000786 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000787 return false;
788
Dan Gohman0586d912008-09-10 20:11:02 +0000789 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000790 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000791 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000792
Evan Cheng0de588f2008-09-05 21:00:03 +0000793 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000794 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000795 UpdateValueMap(I, ResultReg);
796 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000797 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000798 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000799}
800
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000801static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000803 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 case MVT::i8: return X86::CMP8rr;
805 case MVT::i16: return X86::CMP16rr;
806 case MVT::i32: return X86::CMP32rr;
807 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000808 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
809 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000810 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000811}
812
Chris Lattner0e13c782008-10-15 04:13:29 +0000813/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
814/// of the comparison, return an opcode that works for the compare (e.g.
815/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000816static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000818 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000819 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 case MVT::i8: return X86::CMP8ri;
821 case MVT::i16: return X86::CMP16ri;
822 case MVT::i32: return X86::CMP32ri;
823 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000824 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
825 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000826 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000827 return X86::CMP64ri32;
828 return 0;
829 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000830}
831
Dan Gohman46510a72010-04-15 01:51:59 +0000832bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
833 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000834 unsigned Op0Reg = getRegForValue(Op0);
835 if (Op0Reg == 0) return false;
836
Chris Lattnerd53886b2008-10-15 05:18:04 +0000837 // Handle 'null' like i32/i64 0.
838 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000839 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000840
Chris Lattner9a08a612008-10-15 04:26:38 +0000841 // We have two options: compare with register or immediate. If the RHS of
842 // the compare is an immediate that we can fold into this compare, use
843 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000844 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000845 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000846 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
847 .addReg(Op0Reg)
848 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000849 return true;
850 }
851 }
852
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000853 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000854 if (CompareOpc == 0) return false;
855
856 unsigned Op1Reg = getRegForValue(Op1);
857 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000858 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
859 .addReg(Op0Reg)
860 .addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000861
862 return true;
863}
864
Dan Gohman46510a72010-04-15 01:51:59 +0000865bool X86FastISel::X86SelectCmp(const Instruction *I) {
866 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000867
Owen Andersone50ed302009-08-10 22:56:29 +0000868 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000869 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000870 return false;
871
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000872 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000873 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000874 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000875 switch (CI->getPredicate()) {
876 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000877 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
878 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000879
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000880 unsigned EReg = createResultReg(&X86::GR8RegClass);
881 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000882 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
883 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
884 TII.get(X86::SETNPr), NPReg);
885 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000886 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000887 UpdateValueMap(I, ResultReg);
888 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000889 }
890 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000891 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
892 return false;
893
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000894 unsigned NEReg = createResultReg(&X86::GR8RegClass);
895 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000896 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
897 TII.get(X86::SETNEr), NEReg);
898 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
899 TII.get(X86::SETPr), PReg);
900 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
901 TII.get(X86::OR8rr), ResultReg)
902 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000903 UpdateValueMap(I, ResultReg);
904 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000905 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000906 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
907 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
908 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
909 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
910 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
911 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
912 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
913 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
914 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
915 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
916 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
917 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
918
919 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
920 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
921 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
922 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
923 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
924 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
925 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
926 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
927 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
928 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000929 default:
930 return false;
931 }
932
Dan Gohman46510a72010-04-15 01:51:59 +0000933 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000934 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000935 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000936
Chris Lattner9a08a612008-10-15 04:26:38 +0000937 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000938 if (!X86FastEmitCompare(Op0, Op1, VT))
939 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000940
Dan Gohman84023e02010-07-10 09:00:22 +0000941 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000942 UpdateValueMap(I, ResultReg);
943 return true;
944}
Evan Cheng8b19e562008-09-03 06:44:39 +0000945
Dan Gohman46510a72010-04-15 01:51:59 +0000946bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000947 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000948 if (I->getType()->isIntegerTy(8) &&
949 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000950 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000951 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000952 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000953 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000954 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000955 UpdateValueMap(I, ResultReg);
956 return true;
957 }
958
959 return false;
960}
961
Chris Lattner9a08a612008-10-15 04:26:38 +0000962
Dan Gohman46510a72010-04-15 01:51:59 +0000963bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000964 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000965 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000966 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000967 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
968 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000969
Dan Gohman8bef7442010-08-21 02:32:36 +0000970 // Fold the common case of a conditional branch with a comparison
971 // in the same block (values defined on other blocks may not have
972 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000973 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000974 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000975 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000976
Dan Gohmand98d6202008-10-02 22:15:21 +0000977 // Try to take advantage of fallthrough opportunities.
978 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000979 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000980 std::swap(TrueMBB, FalseMBB);
981 Predicate = CmpInst::getInversePredicate(Predicate);
982 }
983
Chris Lattner871d2462008-10-15 03:58:05 +0000984 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
985 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
986
Dan Gohmand98d6202008-10-02 22:15:21 +0000987 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000988 case CmpInst::FCMP_OEQ:
989 std::swap(TrueMBB, FalseMBB);
990 Predicate = CmpInst::FCMP_UNE;
991 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000992 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
993 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
994 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
995 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
996 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
997 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
998 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
999 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1000 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1001 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1002 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1003 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1004 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +00001005
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001006 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1007 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1008 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1009 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1010 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1011 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1012 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1013 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1014 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1015 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001016 default:
1017 return false;
1018 }
Chris Lattner54aebde2008-10-15 03:47:17 +00001019
Dan Gohman46510a72010-04-15 01:51:59 +00001020 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001021 if (SwapArgs)
1022 std::swap(Op0, Op1);
1023
Chris Lattner9a08a612008-10-15 04:26:38 +00001024 // Emit a compare of the LHS and RHS, setting the flags.
1025 if (!X86FastEmitCompare(Op0, Op1, VT))
1026 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +00001027
Dan Gohman84023e02010-07-10 09:00:22 +00001028 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1029 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001030
1031 if (Predicate == CmpInst::FCMP_UNE) {
1032 // X86 requires a second branch to handle UNE (and OEQ,
1033 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001034 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1035 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001036 }
1037
Stuart Hastings3bf91252010-06-17 22:43:56 +00001038 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001039 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001040 return true;
1041 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001042 } else if (ExtractValueInst *EI =
1043 dyn_cast<ExtractValueInst>(BI->getCondition())) {
1044 // Check to see if the branch instruction is from an "arithmetic with
1045 // overflow" intrinsic. The main way these intrinsics are used is:
1046 //
1047 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1048 // %sum = extractvalue { i32, i1 } %t, 0
1049 // %obit = extractvalue { i32, i1 } %t, 1
1050 // br i1 %obit, label %overflow, label %normal
1051 //
Dan Gohman653456c2009-01-07 00:15:08 +00001052 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +00001053 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +00001054 // looking for the SETO/SETB instruction. If an instruction modifies the
1055 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1056 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001057 if (const IntrinsicInst *CI =
1058 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001059 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1060 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1061 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001062 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001063
Chris Lattnera9a42252009-04-12 07:36:01 +00001064 for (MachineBasicBlock::const_reverse_iterator
Dan Gohman84023e02010-07-10 09:00:22 +00001065 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1066 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001067 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001068
Evan Cheng1015ba72010-05-21 20:53:24 +00001069 if (MI.definesRegister(Reg)) {
Jakob Stoklund Olesen84d499a2010-07-16 22:35:34 +00001070 if (MI.isCopy()) {
1071 Reg = MI.getOperand(1).getReg();
Chris Lattnera9a42252009-04-12 07:36:01 +00001072 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001073 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001074
Chris Lattnera9a42252009-04-12 07:36:01 +00001075 SetMI = &MI;
1076 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001077 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001078
Chris Lattnera9a42252009-04-12 07:36:01 +00001079 const TargetInstrDesc &TID = MI.getDesc();
1080 if (TID.hasUnmodeledSideEffects() ||
1081 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
1082 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001083 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001084
1085 if (SetMI) {
1086 unsigned OpCode = SetMI->getOpcode();
1087
1088 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohman84023e02010-07-10 09:00:22 +00001089 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1090 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001091 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001092 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001093 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001094 return true;
1095 }
Bill Wendling9a901322008-12-10 19:44:24 +00001096 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001097 }
1098 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001099 }
1100
1101 // Otherwise do a clumsy setcc and re-test it.
1102 unsigned OpReg = getRegForValue(BI->getCondition());
1103 if (OpReg == 0) return false;
1104
Dan Gohman84023e02010-07-10 09:00:22 +00001105 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1106 .addReg(OpReg).addReg(OpReg);
1107 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1108 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001109 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001110 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001111 return true;
1112}
1113
Dan Gohman46510a72010-04-15 01:51:59 +00001114bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +00001115 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001116 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001117 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001118 CReg = X86::CL;
1119 RC = &X86::GR8RegClass;
1120 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001121 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
1122 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
1123 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001124 default: return false;
1125 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001126 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001127 CReg = X86::CX;
1128 RC = &X86::GR16RegClass;
1129 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001130 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1131 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1132 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001133 default: return false;
1134 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001135 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001136 CReg = X86::ECX;
1137 RC = &X86::GR32RegClass;
1138 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001139 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1140 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1141 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001142 default: return false;
1143 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001144 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001145 CReg = X86::RCX;
1146 RC = &X86::GR64RegClass;
1147 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001148 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1149 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1150 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001151 default: return false;
1152 }
1153 } else {
1154 return false;
1155 }
1156
Owen Andersone50ed302009-08-10 22:56:29 +00001157 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001158 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001159 return false;
1160
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001161 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1162 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001163
1164 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001165 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001166 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001167 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001168 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001169 UpdateValueMap(I, ResultReg);
1170 return true;
1171 }
1172
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001173 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1174 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001175 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1176 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001177
1178 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001179 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001180 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1182 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001183 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001184
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001185 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001186 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1187 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001188 UpdateValueMap(I, ResultReg);
1189 return true;
1190}
1191
Dan Gohman46510a72010-04-15 01:51:59 +00001192bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001193 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001194 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001195 return false;
1196
Eric Christophere487b012010-09-29 23:00:29 +00001197 // We only use cmov here, if we don't have a cmov instruction bail.
1198 if (!Subtarget->hasCMov()) return false;
1199
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001200 unsigned Opc = 0;
1201 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001202 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001203 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001204 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001205 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001206 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001207 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001208 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001209 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001210 RC = &X86::GR64RegClass;
1211 } else {
1212 return false;
1213 }
1214
1215 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1216 if (Op0Reg == 0) return false;
1217 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1218 if (Op1Reg == 0) return false;
1219 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1220 if (Op2Reg == 0) return false;
1221
Dan Gohman84023e02010-07-10 09:00:22 +00001222 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1223 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001224 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1226 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001227 UpdateValueMap(I, ResultReg);
1228 return true;
1229}
1230
Dan Gohman46510a72010-04-15 01:51:59 +00001231bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001232 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001233 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001234 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001235 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001236 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001237 unsigned OpReg = getRegForValue(V);
1238 if (OpReg == 0) return false;
1239 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1241 TII.get(X86::CVTSS2SDrr), ResultReg)
1242 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001243 UpdateValueMap(I, ResultReg);
1244 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001245 }
1246 }
1247
1248 return false;
1249}
1250
Dan Gohman46510a72010-04-15 01:51:59 +00001251bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001252 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001253 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001254 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001255 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001256 unsigned OpReg = getRegForValue(V);
1257 if (OpReg == 0) return false;
1258 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001259 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1260 TII.get(X86::CVTSD2SSrr), ResultReg)
1261 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001262 UpdateValueMap(I, ResultReg);
1263 return true;
1264 }
1265 }
1266 }
1267
1268 return false;
1269}
1270
Dan Gohman46510a72010-04-15 01:51:59 +00001271bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001272 if (Subtarget->is64Bit())
1273 // All other cases should be handled by the tblgen generated code.
1274 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001275 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1276 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001277
1278 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001279 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001280 // All other cases should be handled by the tblgen generated code.
1281 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001282 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001283 // All other cases should be handled by the tblgen generated code.
1284 return false;
1285
1286 unsigned InputReg = getRegForValue(I->getOperand(0));
1287 if (!InputReg)
1288 // Unhandled operand. Halt "fast" selection and bail.
1289 return false;
1290
Dan Gohman62417622009-04-27 16:33:14 +00001291 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001293 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001294 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001295 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1296 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001297
1298 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001299 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001300 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001301 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001302 if (!ResultReg)
1303 return false;
1304
1305 UpdateValueMap(I, ResultReg);
1306 return true;
1307}
1308
Dan Gohman46510a72010-04-15 01:51:59 +00001309bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1310 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1311 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001312
Dan Gohman46510a72010-04-15 01:51:59 +00001313 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001314 switch (CI->getIntrinsicID()) {
1315 default: break;
1316 case Intrinsic::sadd_with_overflow:
Dan Gohman84023e02010-07-10 09:00:22 +00001317 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001318 // Cheat a little. We know that the registers for "add" and "seto" are
1319 // allocated sequentially. However, we only keep track of the register
1320 // for "add" in the value map. Use extractvalue's index to get the
1321 // correct register for "seto".
Dan Gohman84023e02010-07-10 09:00:22 +00001322 unsigned OpReg = getRegForValue(Agg);
1323 if (OpReg == 0)
1324 return false;
1325 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001326 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001327 }
Dan Gohman84023e02010-07-10 09:00:22 +00001328 }
Bill Wendling52370a12008-12-09 02:42:50 +00001329 }
1330
1331 return false;
1332}
1333
Dan Gohman46510a72010-04-15 01:51:59 +00001334bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001335 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001336 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001337 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001338 case Intrinsic::stackprotector: {
1339 // Emit code inline code to store the stack guard onto the stack.
1340 EVT PtrTy = TLI.getPointerTy();
1341
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001342 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1343 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001344
1345 // Grab the frame index.
1346 X86AddressMode AM;
1347 if (!X86SelectAddress(Slot, AM)) return false;
1348
Eric Christopher88dee302010-03-18 21:58:33 +00001349 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1350
Eric Christopher07754c22010-03-18 20:27:26 +00001351 return true;
1352 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001353 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001354 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001355 const Type *Ty = I.getCalledFunction()->getReturnType();
1356
1357 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1358
1359 EVT VT;
1360 if (!isTypeLegal(Ty, VT))
1361 return false;
1362
1363 unsigned OpC = 0;
1364 if (VT == MVT::i32)
1365 OpC = X86::MOV32ri;
1366 else if (VT == MVT::i64)
1367 OpC = X86::MOV64ri;
1368 else
1369 return false;
1370
1371 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001372 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001373 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001374 UpdateValueMap(&I, ResultReg);
1375 return true;
1376 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001377 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001378 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001379 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001380 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001381 if (!X86SelectAddress(DI->getAddress(), AM))
1382 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001383 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001384 // FIXME may need to add RegState::Debug to any registers produced,
1385 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001386 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1387 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001388 return true;
1389 }
Eric Christopher77f79892010-01-18 22:11:29 +00001390 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001391 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001392 return true;
1393 }
Bill Wendling52370a12008-12-09 02:42:50 +00001394 case Intrinsic::sadd_with_overflow:
1395 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001396 // Replace "add with overflow" intrinsics with an "add" instruction followed
1397 // by a seto/setc instruction. Later on, when the "extractvalue"
1398 // instructions are encountered, we use the fact that two registers were
1399 // created sequentially to get the correct registers for the "sum" and the
1400 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001401 const Function *Callee = I.getCalledFunction();
1402 const Type *RetTy =
1403 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1404
Owen Andersone50ed302009-08-10 22:56:29 +00001405 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001406 if (!isTypeLegal(RetTy, VT))
1407 return false;
1408
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001409 const Value *Op1 = I.getArgOperand(0);
1410 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001411 unsigned Reg1 = getRegForValue(Op1);
1412 unsigned Reg2 = getRegForValue(Op2);
1413
1414 if (Reg1 == 0 || Reg2 == 0)
1415 // FIXME: Handle values *not* in registers.
1416 return false;
1417
1418 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001419 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001420 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001421 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001422 OpC = X86::ADD64rr;
1423 else
1424 return false;
1425
1426 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001427 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1428 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001429 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001430
Chris Lattner8d57b772009-04-12 07:51:14 +00001431 // If the add with overflow is an intra-block value then we just want to
1432 // create temporaries for it like normal. If it is a cross-block value then
1433 // UpdateValueMap will return the cross-block register used. Since we
1434 // *really* want the value to be live in the register pair known by
1435 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1436 // the cross block case. In the non-cross-block case, we should just make
1437 // another register for the value.
1438 if (DestReg1 != ResultReg)
1439 ResultReg = DestReg1+1;
1440 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001442
Chris Lattnera9a42252009-04-12 07:36:01 +00001443 unsigned Opc = X86::SETBr;
1444 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1445 Opc = X86::SETOr;
Dan Gohman84023e02010-07-10 09:00:22 +00001446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001447 return true;
1448 }
1449 }
1450}
1451
Dan Gohman46510a72010-04-15 01:51:59 +00001452bool X86FastISel::X86SelectCall(const Instruction *I) {
1453 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001454 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001455
1456 // Can't handle inline asm yet.
1457 if (isa<InlineAsm>(Callee))
1458 return false;
1459
Bill Wendling52370a12008-12-09 02:42:50 +00001460 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001461 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001462 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001463
Evan Chengf3d4efe2008-09-07 09:09:33 +00001464 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001465 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001466 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001467 if (CC != CallingConv::C &&
1468 CC != CallingConv::Fast &&
1469 CC != CallingConv::X86_FastCall)
1470 return false;
1471
Evan Cheng381993f2010-01-27 00:00:57 +00001472 // fastcc with -tailcallopt is intended to provide a guaranteed
1473 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001474 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001475 return false;
1476
Evan Chengf3d4efe2008-09-07 09:09:33 +00001477 // Let SDISel handle vararg functions.
1478 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1479 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1480 if (FTy->isVarArg())
1481 return false;
1482
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001483 // Fast-isel doesn't know about callee-pop yet.
1484 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1485 return false;
1486
Evan Chengf3d4efe2008-09-07 09:09:33 +00001487 // Handle *simple* calls for now.
1488 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001489 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001490 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001491 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001492 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001493 return false;
1494
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001495 // Materialize callee address in a register. FIXME: GV address can be
1496 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001497 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001498 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001499 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001500 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001501 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001502 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001503 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001504 } else if (CalleeAM.Base.Reg != 0) {
1505 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001506 } else
1507 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001508
Evan Chengdebdea02008-09-08 17:15:42 +00001509 // Allow calls which produce i1 results.
1510 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001511 if (RetVT == MVT::i1) {
1512 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001513 AndToI1 = true;
1514 }
1515
Evan Chengf3d4efe2008-09-07 09:09:33 +00001516 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001517 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001518 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001519 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001520 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001521 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001522 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001523 ArgVTs.reserve(CS.arg_size());
1524 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001525 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001526 i != e; ++i) {
1527 unsigned Arg = getRegForValue(*i);
1528 if (Arg == 0)
1529 return false;
1530 ISD::ArgFlagsTy Flags;
1531 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001532 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001533 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001534 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001535 Flags.setZExt();
1536
1537 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001538 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1539 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1540 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1541 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001542 return false;
1543
1544 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001545 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001546 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001547 return false;
1548 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1549 Flags.setOrigAlign(OriginalAlignment);
1550
1551 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001552 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001553 ArgVTs.push_back(ArgVT);
1554 ArgFlags.push_back(Flags);
1555 }
1556
1557 // Analyze operands of the call, assigning locations to each operand.
1558 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001559 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Dan Gohmand8acddd2010-06-01 21:09:47 +00001560
1561 // Allocate shadow area for Win64
1562 if (Subtarget->isTargetWin64()) {
1563 CCInfo.AllocateStack(32, 8);
1564 }
1565
Evan Chengf3d4efe2008-09-07 09:09:33 +00001566 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1567
1568 // Get a count of how many bytes are to be pushed on the stack.
1569 unsigned NumBytes = CCInfo.getNextStackOffset();
1570
1571 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001572 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001573 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1574 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001575
Chris Lattner438949a2008-10-15 05:30:52 +00001576 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001577 // copies / loads.
1578 SmallVector<unsigned, 4> RegArgs;
1579 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1580 CCValAssign &VA = ArgLocs[i];
1581 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001582 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001583
1584 // Promote the value if needed.
1585 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001586 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001587 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001588 case CCValAssign::SExt: {
1589 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1590 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001591 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001592 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001593 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001594 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001595 }
1596 case CCValAssign::ZExt: {
1597 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1598 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001599 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001600 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001601 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001602 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001603 }
1604 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001605 // We don't handle MMX parameters yet.
1606 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1607 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001608 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1609 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001610 if (!Emitted)
1611 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001612 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001613 if (!Emitted)
1614 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1615 Arg, ArgVT, Arg);
1616
Chris Lattnera33649e2008-12-19 17:03:38 +00001617 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001618 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001619 break;
1620 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001621 case CCValAssign::BCvt: {
1622 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001623 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001624 assert(BC != 0 && "Failed to emit a bitcast!");
1625 Arg = BC;
1626 ArgVT = VA.getLocVT();
1627 break;
1628 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001629 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001630
1631 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001632 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1633 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001634 RegArgs.push_back(VA.getLocReg());
1635 } else {
1636 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001637 X86AddressMode AM;
1638 AM.Base.Reg = StackPtr;
1639 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001640 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001641
1642 // If this is a really simple value, emit this with the Value* version of
1643 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1644 // can cause us to reevaluate the argument.
1645 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1646 X86FastEmitStore(ArgVT, ArgVal, AM);
1647 else
1648 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001649 }
1650 }
1651
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001652 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1653 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001654 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001655 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001656 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1657 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001658 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001659
Evan Chengf3d4efe2008-09-07 09:09:33 +00001660 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001661 MachineInstrBuilder MIB;
1662 if (CalleeOp) {
1663 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001664 unsigned CallOpc;
1665 if (Subtarget->isTargetWin64())
1666 CallOpc = X86::WINCALL64r;
1667 else if (Subtarget->is64Bit())
1668 CallOpc = X86::CALL64r;
1669 else
1670 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001671 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1672 .addReg(CalleeOp);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001673
1674 } else {
1675 // Direct call.
1676 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001677 unsigned CallOpc;
1678 if (Subtarget->isTargetWin64())
1679 CallOpc = X86::WINCALL64pcrel32;
1680 else if (Subtarget->is64Bit())
1681 CallOpc = X86::CALL64pcrel32;
1682 else
1683 CallOpc = X86::CALLpcrel32;
Chris Lattner51e8eab2009-07-09 06:34:26 +00001684
1685 // See if we need any target-specific flags on the GV operand.
1686 unsigned char OpFlags = 0;
1687
1688 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1689 // external symbols most go through the PLT in PIC mode. If the symbol
1690 // has hidden or protected visibility, or if it is static or local, then
1691 // we don't need to use the PLT - we can directly call it.
1692 if (Subtarget->isTargetELF() &&
1693 TM.getRelocationModel() == Reloc::PIC_ &&
1694 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1695 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001696 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001697 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1698 Subtarget->getDarwinVers() < 9) {
1699 // PC-relative references to external symbols should go through $stub,
1700 // unless we're building with the leopard linker or later, which
1701 // automatically synthesizes these stubs.
1702 OpFlags = X86II::MO_DARWIN_STUB;
1703 }
1704
1705
Dan Gohman84023e02010-07-10 09:00:22 +00001706 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1707 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001708 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001709
1710 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001711 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001712 MIB.addReg(X86::EBX);
1713
Evan Chengf3d4efe2008-09-07 09:09:33 +00001714 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001715 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1716 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001717
1718 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001719 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001720 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1721 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001722
1723 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001724 SmallVector<unsigned, 4> UsedRegs;
Owen Anderson825b72b2009-08-11 20:47:22 +00001725 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001726 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001727 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001728 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1729
1730 // Copy all of the result registers out of their specified physreg.
1731 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001732 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001733 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001734
1735 // If this is a call to a function that returns an fp value on the x87 fp
1736 // stack, but where we prefer to use the value in xmm registers, copy it
1737 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1738 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1739 RVLocs[0].getLocReg() == X86::ST1) &&
1740 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001741 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001742 DstRC = X86::RFP80RegisterClass;
1743 }
1744
1745 unsigned ResultReg = createResultReg(DstRC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001746 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1747 ResultReg).addReg(RVLocs[0].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001748 UsedRegs.push_back(RVLocs[0].getLocReg());
1749
Evan Chengf3d4efe2008-09-07 09:09:33 +00001750 if (CopyVT != RVLocs[0].getValVT()) {
1751 // Round the F80 the right size, which also moves to the appropriate xmm
1752 // register. This is accomplished by storing the F80 value in memory and
1753 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001754 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001755 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001756 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001757 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001758 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1759 TII.get(Opc)), FI)
1760 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001762 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001763 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001764 ResultReg = createResultReg(DstRC);
Dan Gohman84023e02010-07-10 09:00:22 +00001765 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1766 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001767 }
1768
Evan Chengdebdea02008-09-08 17:15:42 +00001769 if (AndToI1) {
1770 // Mask out all but lowest bit for some call which produces an i1.
1771 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001772 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001773 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001774 ResultReg = AndResult;
1775 }
1776
Evan Chengf3d4efe2008-09-07 09:09:33 +00001777 UpdateValueMap(I, ResultReg);
1778 }
1779
Dan Gohmandb497122010-06-18 23:28:01 +00001780 // Set all unused physreg defs as dead.
1781 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1782
Evan Chengf3d4efe2008-09-07 09:09:33 +00001783 return true;
1784}
1785
1786
Dan Gohman99b21822008-08-28 23:21:34 +00001787bool
Dan Gohman46510a72010-04-15 01:51:59 +00001788X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001789 switch (I->getOpcode()) {
1790 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001791 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001792 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001793 case Instruction::Store:
1794 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001795 case Instruction::Ret:
1796 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001797 case Instruction::ICmp:
1798 case Instruction::FCmp:
1799 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001800 case Instruction::ZExt:
1801 return X86SelectZExt(I);
1802 case Instruction::Br:
1803 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001804 case Instruction::Call:
1805 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001806 case Instruction::LShr:
1807 case Instruction::AShr:
1808 case Instruction::Shl:
1809 return X86SelectShift(I);
1810 case Instruction::Select:
1811 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001812 case Instruction::Trunc:
1813 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001814 case Instruction::FPExt:
1815 return X86SelectFPExt(I);
1816 case Instruction::FPTrunc:
1817 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001818 case Instruction::ExtractValue:
1819 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001820 case Instruction::IntToPtr: // Deliberate fall-through.
1821 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001822 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1823 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001824 if (DstVT.bitsGT(SrcVT))
1825 return X86SelectZExt(I);
1826 if (DstVT.bitsLT(SrcVT))
1827 return X86SelectTrunc(I);
1828 unsigned Reg = getRegForValue(I->getOperand(0));
1829 if (Reg == 0) return false;
1830 UpdateValueMap(I, Reg);
1831 return true;
1832 }
Dan Gohman99b21822008-08-28 23:21:34 +00001833 }
1834
1835 return false;
1836}
1837
Dan Gohman46510a72010-04-15 01:51:59 +00001838unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001839 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001840 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001841 return false;
1842
1843 // Get opcode and regclass of the output for the given load instruction.
1844 unsigned Opc = 0;
1845 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001846 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001847 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001849 Opc = X86::MOV8rm;
1850 RC = X86::GR8RegisterClass;
1851 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001852 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001853 Opc = X86::MOV16rm;
1854 RC = X86::GR16RegisterClass;
1855 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001857 Opc = X86::MOV32rm;
1858 RC = X86::GR32RegisterClass;
1859 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001861 // Must be in x86-64 mode.
1862 Opc = X86::MOV64rm;
1863 RC = X86::GR64RegisterClass;
1864 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001865 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001866 if (Subtarget->hasSSE1()) {
1867 Opc = X86::MOVSSrm;
1868 RC = X86::FR32RegisterClass;
1869 } else {
1870 Opc = X86::LD_Fp32m;
1871 RC = X86::RFP32RegisterClass;
1872 }
1873 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001875 if (Subtarget->hasSSE2()) {
1876 Opc = X86::MOVSDrm;
1877 RC = X86::FR64RegisterClass;
1878 } else {
1879 Opc = X86::LD_Fp64m;
1880 RC = X86::RFP64RegisterClass;
1881 }
1882 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001884 // No f80 support yet.
1885 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001886 }
1887
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001888 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001889 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001890 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001891 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001893 Opc = X86::LEA32r;
1894 else
1895 Opc = X86::LEA64r;
1896 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001897 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1898 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001899 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001900 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001901 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001902 }
1903
Owen Anderson3b217c62008-09-06 01:11:01 +00001904 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001905 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001906 if (Align == 0) {
1907 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001908 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001909 }
Owen Anderson95267a12008-09-05 00:06:23 +00001910
Dan Gohman5396c992008-09-30 01:21:32 +00001911 // x86-32 PIC requires a PIC base register for constant pools.
1912 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001913 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001914 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001915 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001916 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001917 } else if (Subtarget->isPICStyleGOT()) {
1918 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001919 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001920 } else if (Subtarget->isPICStyleRIPRel() &&
1921 TM.getCodeModel() == CodeModel::Small) {
1922 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001923 }
Dan Gohman5396c992008-09-30 01:21:32 +00001924
1925 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001926 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001927 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001928 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1929 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001930 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001931
Owen Anderson95267a12008-09-05 00:06:23 +00001932 return ResultReg;
1933}
1934
Dan Gohman46510a72010-04-15 01:51:59 +00001935unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001936 // Fail on dynamic allocas. At this point, getRegForValue has already
1937 // checked its CSE maps, so if we're here trying to handle a dynamic
1938 // alloca, we're not going to succeed. X86SelectAddress has a
1939 // check for dynamic allocas, because it's called directly from
1940 // various places, but TargetMaterializeAlloca also needs a check
1941 // in order to avoid recursion between getRegForValue,
1942 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001943 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001944 return 0;
1945
Dan Gohman0586d912008-09-10 20:11:02 +00001946 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001947 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001948 return 0;
1949 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1950 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1951 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001952 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1953 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001954 return ResultReg;
1955}
1956
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001957/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
1958/// vreg is being provided by the specified load instruction. If possible,
1959/// try to fold the load as an operand to the instruction, returning true if
1960/// possible.
1961bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
1962 const LoadInst *LI) {
1963 X86AddressMode AM;
1964 if (!X86SelectAddress(LI->getOperand(0), AM))
1965 return false;
1966
1967 X86InstrInfo &XII = (X86InstrInfo&)TII;
1968
1969 unsigned Size = TD.getTypeAllocSize(LI->getType());
1970 unsigned Alignment = LI->getAlignment();
1971
1972 SmallVector<MachineOperand, 8> AddrOps;
1973 AM.getFullAddress(AddrOps);
1974
1975 MachineInstr *Result =
1976 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
1977 if (Result == 0) return false;
1978
1979 MI->getParent()->insert(MI, Result);
1980 MI->eraseFromParent();
1981 return true;
1982}
1983
1984
Evan Chengc3f44b02008-09-03 00:03:49 +00001985namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001986 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1987 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001988 }
Dan Gohman99b21822008-08-28 23:21:34 +00001989}