blob: 594ca4b11281c25c59168f9b213c6714ac672380 [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 Gohmanf423a692010-07-07 18:32:53 +000026#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000027#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000028#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000029#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000032#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000034#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000035#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000036using namespace llvm;
37
Chris Lattner087fcf32009-03-08 18:44:31 +000038namespace {
39
Evan Chengc3f44b02008-09-03 00:03:49 +000040class X86FastISel : public FastISel {
41 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
42 /// make the right decision when generating code for different targets.
43 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000044
45 /// StackPtr - Register used as the stack pointer.
46 ///
47 unsigned StackPtr;
48
49 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
50 /// floating point ops.
51 /// When SSE is available, use it for f32 operations.
52 /// When SSE2 is available, use it for f64 operations.
53 bool X86ScalarSSEf64;
54 bool X86ScalarSSEf32;
55
Evan Cheng8b19e562008-09-03 06:44:39 +000056public:
Dan Gohmana4160c32010-07-07 16:29:44 +000057 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000058 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000059 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
60 X86ScalarSSEf64 = Subtarget->hasSSE2();
61 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000062 }
Evan Chengc3f44b02008-09-03 00:03:49 +000063
Dan Gohman46510a72010-04-15 01:51:59 +000064 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000065
Dan Gohman1adf1b02008-08-19 21:45:35 +000066#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000067
68private:
Dan Gohman46510a72010-04-15 01:51:59 +000069 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Chris Lattner9a08a612008-10-15 04:26:38 +000070
Owen Andersone50ed302009-08-10 22:56:29 +000071 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000072
Dan Gohman46510a72010-04-15 01:51:59 +000073 bool X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +000074 const X86AddressMode &AM);
Owen Andersone50ed302009-08-10 22:56:29 +000075 bool X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000076 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000077
Owen Andersone50ed302009-08-10 22:56:29 +000078 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000079 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Dan Gohman46510a72010-04-15 01:51:59 +000081 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
82 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000083
Dan Gohman46510a72010-04-15 01:51:59 +000084 bool X86SelectLoad(const Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000085
Dan Gohman46510a72010-04-15 01:51:59 +000086 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000087
Dan Gohmanf423a692010-07-07 18:32:53 +000088 bool X86SelectRet(const Instruction *I);
89
Dan Gohman46510a72010-04-15 01:51:59 +000090 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000093
Dan Gohman46510a72010-04-15 01:51:59 +000094 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000095
Dan Gohman46510a72010-04-15 01:51:59 +000096 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000097
Dan Gohman46510a72010-04-15 01:51:59 +000098 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000099
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectTrunc(const Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectFPExt(const Instruction *I);
103 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000104
Dan Gohman46510a72010-04-15 01:51:59 +0000105 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
108 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000109
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000110 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isTailCall = false);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000111
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000112 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000113 return getTargetMachine()->getInstrInfo();
114 }
115 const X86TargetMachine *getTargetMachine() const {
116 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000117 }
118
Dan Gohman46510a72010-04-15 01:51:59 +0000119 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000120
Dan Gohman46510a72010-04-15 01:51:59 +0000121 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000122
123 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
124 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000125 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
127 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000128 }
129
Owen Andersone50ed302009-08-10 22:56:29 +0000130 bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000131};
Chris Lattner087fcf32009-03-08 18:44:31 +0000132
133} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000134
Owen Andersone50ed302009-08-10 22:56:29 +0000135bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000136 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 if (VT == MVT::Other || !VT.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000138 // Unhandled type. Halt "fast" selection and bail.
139 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000140
Dan Gohman9b66d732008-09-30 00:48:39 +0000141 // For now, require SSE/SSE2 for performing floating-point operations,
142 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000144 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000146 return false;
147 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000149 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000150 // We only handle legal types. For example, on x86-32 the instruction
151 // selector contains all of the 64-bit instructions from x86-64,
152 // under the assumption that i64 won't be used if the target doesn't
153 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000155}
156
157#include "X86GenCallingConv.inc"
158
159/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
160/// convention.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000161CCAssignFn *X86FastISel::CCAssignFnForCall(CallingConv::ID CC,
162 bool isTaillCall) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000163 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +0000164 if (CC == CallingConv::GHC)
165 return CC_X86_64_GHC;
166 else if (Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000167 return CC_X86_Win64_C;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000168 else
169 return CC_X86_64_C;
170 }
171
172 if (CC == CallingConv::X86_FastCall)
173 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000174 else if (CC == CallingConv::X86_ThisCall)
175 return CC_X86_32_ThisCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000176 else if (CC == CallingConv::Fast)
177 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +0000178 else if (CC == CallingConv::GHC)
179 return CC_X86_32_GHC;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000180 else
181 return CC_X86_32_C;
182}
183
Evan Cheng0de588f2008-09-05 21:00:03 +0000184/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000185/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000186/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000187bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000188 unsigned &ResultReg) {
189 // Get opcode and regclass of the output for the given load instruction.
190 unsigned Opc = 0;
191 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000194 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000196 Opc = X86::MOV8rm;
197 RC = X86::GR8RegisterClass;
198 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000200 Opc = X86::MOV16rm;
201 RC = X86::GR16RegisterClass;
202 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000204 Opc = X86::MOV32rm;
205 RC = X86::GR32RegisterClass;
206 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000208 // Must be in x86-64 mode.
209 Opc = X86::MOV64rm;
210 RC = X86::GR64RegisterClass;
211 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000213 if (Subtarget->hasSSE1()) {
214 Opc = X86::MOVSSrm;
215 RC = X86::FR32RegisterClass;
216 } else {
217 Opc = X86::LD_Fp32m;
218 RC = X86::RFP32RegisterClass;
219 }
220 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000222 if (Subtarget->hasSSE2()) {
223 Opc = X86::MOVSDrm;
224 RC = X86::FR64RegisterClass;
225 } else {
226 Opc = X86::LD_Fp64m;
227 RC = X86::RFP64RegisterClass;
228 }
229 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000231 // No f80 support yet.
232 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000233 }
234
235 ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000236 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
237 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000238 return true;
239}
240
Evan Chengf3d4efe2008-09-07 09:09:33 +0000241/// X86FastEmitStore - Emit a machine instruction to store a value Val of
242/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
243/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000244/// i.e. V. Return true if it is possible.
245bool
Owen Andersone50ed302009-08-10 22:56:29 +0000246X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000247 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000248 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000249 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 switch (VT.getSimpleVT().SimpleTy) {
251 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000252 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000253 case MVT::i1: {
254 // Mask out all but lowest bit.
255 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000256 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000257 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
258 Val = AndResult;
259 }
260 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 case MVT::i8: Opc = X86::MOV8mr; break;
262 case MVT::i16: Opc = X86::MOV16mr; break;
263 case MVT::i32: Opc = X86::MOV32mr; break;
264 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
265 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000266 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000267 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000269 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000270 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000271 }
Chris Lattner438949a2008-10-15 05:30:52 +0000272
Dan Gohmaneabaed22010-07-07 16:47:08 +0000273 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
274 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000275 return true;
276}
277
Dan Gohman46510a72010-04-15 01:51:59 +0000278bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000279 const X86AddressMode &AM) {
280 // Handle 'null' like i32/i64 0.
281 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000282 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Chris Lattner438949a2008-10-15 05:30:52 +0000283
284 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000285 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000286 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000287 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000289 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000290 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 case MVT::i8: Opc = X86::MOV8mi; break;
292 case MVT::i16: Opc = X86::MOV16mi; break;
293 case MVT::i32: Opc = X86::MOV32mi; break;
294 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000295 // Must be a 32-bit sign extended value.
296 if ((int)CI->getSExtValue() == CI->getSExtValue())
297 Opc = X86::MOV64mi32;
298 break;
299 }
300
301 if (Opc) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000302 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
303 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000304 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000305 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000306 return true;
307 }
308 }
309
310 unsigned ValReg = getRegForValue(Val);
311 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000312 return false;
313
314 return X86FastEmitStore(VT, ValReg, AM);
315}
316
Evan Cheng24e3a902008-09-08 06:35:17 +0000317/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
318/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
319/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000320bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
321 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000322 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000323 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
324 Src, /*TODO: Kill=*/false);
Owen Andersonac34a002008-09-11 19:44:55 +0000325
326 if (RR != 0) {
327 ResultReg = RR;
328 return true;
329 } else
330 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000331}
332
Dan Gohman0586d912008-09-10 20:11:02 +0000333/// X86SelectAddress - Attempt to fill in an address from the given value.
334///
Dan Gohman46510a72010-04-15 01:51:59 +0000335bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
336 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000337 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000338 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000339 // Don't walk into other basic blocks; it's possible we haven't
340 // visited them yet, so the instructions may not yet be assigned
341 // virtual registers.
Dan Gohmaneabaed22010-07-07 16:47:08 +0000342 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
Dan Gohmanea9f1512010-06-18 20:44:47 +0000343 return false;
344
Dan Gohman35893082008-09-18 23:23:44 +0000345 Opcode = I->getOpcode();
346 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000347 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000348 Opcode = C->getOpcode();
349 U = C;
350 }
Dan Gohman0586d912008-09-10 20:11:02 +0000351
Chris Lattner868ee942010-06-15 19:08:40 +0000352 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
353 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000354 // Fast instruction selection doesn't support the special
355 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000356 return false;
357
Dan Gohman35893082008-09-18 23:23:44 +0000358 switch (Opcode) {
359 default: break;
360 case Instruction::BitCast:
361 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000362 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000363
364 case Instruction::IntToPtr:
365 // Look past no-op inttoptrs.
366 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000367 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000368 break;
Dan Gohman35893082008-09-18 23:23:44 +0000369
370 case Instruction::PtrToInt:
371 // Look past no-op ptrtoints.
372 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000373 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000374 break;
Dan Gohman35893082008-09-18 23:23:44 +0000375
376 case Instruction::Alloca: {
377 // Do static allocas.
378 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000379 DenseMap<const AllocaInst*, int>::iterator SI =
380 FuncInfo.StaticAllocaMap.find(A);
381 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000382 AM.BaseType = X86AddressMode::FrameIndexBase;
383 AM.Base.FrameIndex = SI->second;
384 return true;
385 }
386 break;
Dan Gohman35893082008-09-18 23:23:44 +0000387 }
388
389 case Instruction::Add: {
390 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000391 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000392 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
393 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000394 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000395 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000396 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000397 }
Dan Gohman0586d912008-09-10 20:11:02 +0000398 }
Dan Gohman35893082008-09-18 23:23:44 +0000399 break;
400 }
401
402 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000403 X86AddressMode SavedAM = AM;
404
Dan Gohman35893082008-09-18 23:23:44 +0000405 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000406 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000407 unsigned IndexReg = AM.IndexReg;
408 unsigned Scale = AM.Scale;
409 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000410 // Iterate through the indices, folding what we can. Constants can be
411 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000412 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000413 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000414 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000415 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
416 const StructLayout *SL = TD.getStructLayout(STy);
417 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
418 Disp += SL->getElementOffset(Idx);
419 } else {
Duncan Sands777d2302009-05-09 07:06:46 +0000420 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Dan Gohman5c87bf62010-07-01 02:27:15 +0000421 SmallVector<const Value *, 4> Worklist;
422 Worklist.push_back(Op);
423 do {
424 Op = Worklist.pop_back_val();
425 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
426 // Constant-offset addressing.
427 Disp += CI->getSExtValue() * S;
Dan Gohmanabd1d852010-07-01 02:58:21 +0000428 } else if (isa<AddOperator>(Op) &&
429 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
430 // An add with a constant operand. Fold the constant.
431 ConstantInt *CI =
432 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
433 Disp += CI->getSExtValue() * S;
434 // Add the other operand back to the work list.
435 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
Dan Gohman5c87bf62010-07-01 02:27:15 +0000436 } else if (IndexReg == 0 &&
437 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
438 (S == 1 || S == 2 || S == 4 || S == 8)) {
439 // Scaled-index addressing.
440 Scale = S;
441 IndexReg = getRegForGEPIndex(Op).first;
442 if (IndexReg == 0)
443 return false;
Dan Gohman5c87bf62010-07-01 02:27:15 +0000444 } else
445 // Unsupported.
446 goto unsupported_gep;
447 } while (!Worklist.empty());
Dan Gohman35893082008-09-18 23:23:44 +0000448 }
449 }
Dan Gohman09aae462008-09-26 20:04:15 +0000450 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000451 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000452 break;
Dan Gohman35893082008-09-18 23:23:44 +0000453 // Ok, the GEP indices were covered by constant-offset and scaled-index
454 // addressing. Update the address state and move on to examining the base.
455 AM.IndexReg = IndexReg;
456 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000457 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000458 if (X86SelectAddress(U->getOperand(0), AM))
459 return true;
460
461 // If we couldn't merge the sub value into this addr mode, revert back to
462 // our address and just match the value instead of completely failing.
463 AM = SavedAM;
464 break;
Dan Gohman35893082008-09-18 23:23:44 +0000465 unsupported_gep:
466 // Ok, the GEP indices weren't all covered.
467 break;
468 }
469 }
470
471 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000472 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000473 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000474 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000475 return false;
476
Dan Gohman97135e12008-09-26 19:15:30 +0000477 // RIP-relative addresses can't have additional register operands.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000478 if (Subtarget->isPICStyleRIPRel() &&
Dan Gohman97135e12008-09-26 19:15:30 +0000479 (AM.Base.Reg != 0 || AM.IndexReg != 0))
480 return false;
481
Dan Gohmane9865942009-02-23 22:03:08 +0000482 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000483 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000484 if (GVar->isThreadLocal())
485 return false;
486
Chris Lattnerff7727f2009-07-09 06:41:35 +0000487 // Okay, we've committed to selecting this global. Set up the basic address.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000488 AM.GV = GV;
Chris Lattner18c59872009-06-27 04:16:01 +0000489
Chris Lattner0d786dd2009-07-10 07:48:51 +0000490 // Allow the subtarget to classify the global.
491 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
492
493 // If this reference is relative to the pic base, set it now.
494 if (isGlobalRelativeToPICBase(GVFlags)) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000495 // FIXME: How do we know Base.Reg is free??
Dan Gohmana4160c32010-07-07 16:29:44 +0000496 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner75cdf272009-07-09 06:59:17 +0000497 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000498
499 // Unless the ABI requires an extra load, return a direct reference to
Chris Lattnerff7727f2009-07-09 06:41:35 +0000500 // the global.
Chris Lattner0d786dd2009-07-10 07:48:51 +0000501 if (!isGlobalStubReference(GVFlags)) {
Chris Lattnerff7727f2009-07-09 06:41:35 +0000502 if (Subtarget->isPICStyleRIPRel()) {
503 // Use rip-relative addressing if we can. Above we verified that the
504 // base and index registers are unused.
505 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
506 AM.Base.Reg = X86::RIP;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000507 }
Chris Lattner0d786dd2009-07-10 07:48:51 +0000508 AM.GVOpFlags = GVFlags;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000509 return true;
510 }
511
Chris Lattner0d786dd2009-07-10 07:48:51 +0000512 // Ok, we need to do a load from a stub. If we've already loaded from this
513 // stub, reuse the loaded pointer, otherwise emit the load now.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000514 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
515 unsigned LoadReg;
516 if (I != LocalValueMap.end() && I->second != 0) {
517 LoadReg = I->second;
518 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000519 // Issue load from stub.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000520 unsigned Opc = 0;
521 const TargetRegisterClass *RC = NULL;
Dan Gohman789ce772008-09-25 23:34:02 +0000522 X86AddressMode StubAM;
523 StubAM.Base.Reg = AM.Base.Reg;
Chris Lattner75cdf272009-07-09 06:59:17 +0000524 StubAM.GV = GV;
Chris Lattner0d786dd2009-07-10 07:48:51 +0000525 StubAM.GVOpFlags = GVFlags;
526
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 if (TLI.getPointerTy() == MVT::i64) {
Chris Lattner75cdf272009-07-09 06:59:17 +0000528 Opc = X86::MOV64rm;
529 RC = X86::GR64RegisterClass;
530
Chris Lattner0d786dd2009-07-10 07:48:51 +0000531 if (Subtarget->isPICStyleRIPRel())
Chris Lattner75cdf272009-07-09 06:59:17 +0000532 StubAM.Base.Reg = X86::RIP;
Chris Lattner75cdf272009-07-09 06:59:17 +0000533 } else {
Chris Lattner35c28ec2009-07-01 03:27:19 +0000534 Opc = X86::MOV32rm;
535 RC = X86::GR32RegisterClass;
Chris Lattner35c28ec2009-07-01 03:27:19 +0000536 }
Chris Lattnerff7727f2009-07-09 06:41:35 +0000537
538 LoadReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000539 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
540 DL, TII.get(Opc), LoadReg), StubAM);
Chris Lattnerff7727f2009-07-09 06:41:35 +0000541
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000542 // Prevent loading GV stub multiple times in same MBB.
Chris Lattnerff7727f2009-07-09 06:41:35 +0000543 LocalValueMap[V] = LoadReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000544 }
Chris Lattner18c59872009-06-27 04:16:01 +0000545
Chris Lattnerff7727f2009-07-09 06:41:35 +0000546 // Now construct the final address. Note that the Disp, Scale,
547 // and Index values may already be set here.
548 AM.Base.Reg = LoadReg;
549 AM.GV = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000550 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000551 }
552
Dan Gohman97135e12008-09-26 19:15:30 +0000553 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000554 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000555 if (AM.Base.Reg == 0) {
556 AM.Base.Reg = getRegForValue(V);
557 return AM.Base.Reg != 0;
558 }
559 if (AM.IndexReg == 0) {
560 assert(AM.Scale == 1 && "Scale with no index!");
561 AM.IndexReg = getRegForValue(V);
562 return AM.IndexReg != 0;
563 }
564 }
565
566 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000567}
568
Chris Lattner0aa43de2009-07-10 05:33:42 +0000569/// X86SelectCallAddress - Attempt to fill in an address from the given value.
570///
Dan Gohman46510a72010-04-15 01:51:59 +0000571bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
572 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000573 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000574 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000575 Opcode = I->getOpcode();
576 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000577 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000578 Opcode = C->getOpcode();
579 U = C;
580 }
581
582 switch (Opcode) {
583 default: break;
584 case Instruction::BitCast:
585 // Look past bitcasts.
586 return X86SelectCallAddress(U->getOperand(0), AM);
587
588 case Instruction::IntToPtr:
589 // Look past no-op inttoptrs.
590 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
591 return X86SelectCallAddress(U->getOperand(0), AM);
592 break;
593
594 case Instruction::PtrToInt:
595 // Look past no-op ptrtoints.
596 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
597 return X86SelectCallAddress(U->getOperand(0), AM);
598 break;
599 }
600
601 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000602 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000603 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000604 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000605 return false;
606
607 // RIP-relative addresses can't have additional register operands.
608 if (Subtarget->isPICStyleRIPRel() &&
609 (AM.Base.Reg != 0 || AM.IndexReg != 0))
610 return false;
611
Chris Lattner754b7652009-07-10 05:48:03 +0000612 // Can't handle TLS or DLLImport.
Dan Gohman46510a72010-04-15 01:51:59 +0000613 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Chris Lattnere6c07b52009-07-10 05:45:15 +0000614 if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000615 return false;
616
617 // Okay, we've committed to selecting this global. Set up the basic address.
618 AM.GV = GV;
619
Chris Lattnere6c07b52009-07-10 05:45:15 +0000620 // No ABI requires an extra load for anything other than DLLImport, which
621 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000622 if (Subtarget->isPICStyleRIPRel()) {
623 // Use rip-relative addressing if we can. Above we verified that the
624 // base and index registers are unused.
625 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
626 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000627 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000628 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
629 } else if (Subtarget->isPICStyleGOT()) {
630 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000631 }
632
Chris Lattner0aa43de2009-07-10 05:33:42 +0000633 return true;
634 }
635
636 // If all else fails, try to materialize the value in a register.
637 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
638 if (AM.Base.Reg == 0) {
639 AM.Base.Reg = getRegForValue(V);
640 return AM.Base.Reg != 0;
641 }
642 if (AM.IndexReg == 0) {
643 assert(AM.Scale == 1 && "Scale with no index!");
644 AM.IndexReg = getRegForValue(V);
645 return AM.IndexReg != 0;
646 }
647 }
648
649 return false;
650}
651
652
Owen Andersona3971df2008-09-04 07:08:58 +0000653/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000654bool X86FastISel::X86SelectStore(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000655 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000656 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000657 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000658
Dan Gohman0586d912008-09-10 20:11:02 +0000659 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000660 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000661 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000662
Chris Lattner438949a2008-10-15 05:30:52 +0000663 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000664}
665
Dan Gohmanf423a692010-07-07 18:32:53 +0000666/// X86SelectRet - Select and emit code to implement ret instructions.
667bool X86FastISel::X86SelectRet(const Instruction *I) {
668 const ReturnInst *Ret = cast<ReturnInst>(I);
669 const Function &F = *I->getParent()->getParent();
670
671 if (!FuncInfo.CanLowerReturn)
672 return false;
673
674 CallingConv::ID CC = F.getCallingConv();
675 if (CC != CallingConv::C &&
676 CC != CallingConv::Fast &&
677 CC != CallingConv::X86_FastCall)
678 return false;
679
680 if (Subtarget->isTargetWin64())
681 return false;
682
683 // fastcc with -tailcallopt is intended to provide a guaranteed
684 // tail call optimization. Fastisel doesn't know how to do that.
685 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
686 return false;
687
688 // Let SDISel handle vararg functions.
689 if (F.isVarArg())
690 return false;
691
692 SmallVector<ISD::OutputArg, 4> Outs;
693 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
694 Outs, TLI);
695
696 // Analyze operands of the call, assigning locations to each operand.
697 SmallVector<CCValAssign, 16> ValLocs;
698 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
699 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC));
700
701 // Copy the return value into registers.
702 for (unsigned i = 0, e = ValLocs.size(); i != e; ++i) {
703 CCValAssign &VA = ValLocs[i];
704
705 // Don't bother handling odd stuff for now.
706 if (VA.getLocInfo() != CCValAssign::Full)
707 return false;
708 if (!VA.isRegLoc())
709 return false;
710
711 const Value *RV = Ret->getOperand(VA.getValNo());
712 unsigned Reg = getRegForValue(RV);
713
714 TargetRegisterClass* RC = TLI.getRegClassFor(VA.getValVT());
715 bool Emitted = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
716 VA.getLocReg(), Reg, RC, RC, DL);
717 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
718
719 MRI.addLiveOut(X86::XMM0);
720 }
721
722 // Now emit the RET.
723 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
724 return true;
725}
726
Evan Cheng8b19e562008-09-03 06:44:39 +0000727/// X86SelectLoad - Select and emit code to implement load instructions.
728///
Dan Gohman46510a72010-04-15 01:51:59 +0000729bool X86FastISel::X86SelectLoad(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +0000730 EVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000731 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000732 return false;
733
Dan Gohman0586d912008-09-10 20:11:02 +0000734 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000735 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000736 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000737
Evan Cheng0de588f2008-09-05 21:00:03 +0000738 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000739 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000740 UpdateValueMap(I, ResultReg);
741 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000742 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000743 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000744}
745
Owen Andersone50ed302009-08-10 22:56:29 +0000746static unsigned X86ChooseCmpOpcode(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000748 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 case MVT::i8: return X86::CMP8rr;
750 case MVT::i16: return X86::CMP16rr;
751 case MVT::i32: return X86::CMP32rr;
752 case MVT::i64: return X86::CMP64rr;
753 case MVT::f32: return X86::UCOMISSrr;
754 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000755 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000756}
757
Chris Lattner0e13c782008-10-15 04:13:29 +0000758/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
759/// of the comparison, return an opcode that works for the compare (e.g.
760/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000761static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000763 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000764 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 case MVT::i8: return X86::CMP8ri;
766 case MVT::i16: return X86::CMP16ri;
767 case MVT::i32: return X86::CMP32ri;
768 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000769 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
770 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000771 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000772 return X86::CMP64ri32;
773 return 0;
774 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000775}
776
Dan Gohman46510a72010-04-15 01:51:59 +0000777bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
778 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000779 unsigned Op0Reg = getRegForValue(Op0);
780 if (Op0Reg == 0) return false;
781
Chris Lattnerd53886b2008-10-15 05:18:04 +0000782 // Handle 'null' like i32/i64 0.
783 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000784 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Chris Lattnerd53886b2008-10-15 05:18:04 +0000785
Chris Lattner9a08a612008-10-15 04:26:38 +0000786 // We have two options: compare with register or immediate. If the RHS of
787 // the compare is an immediate that we can fold into this compare, use
788 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000789 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000790 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000791 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
792 .addReg(Op0Reg)
793 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000794 return true;
795 }
796 }
797
798 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
799 if (CompareOpc == 0) return false;
800
801 unsigned Op1Reg = getRegForValue(Op1);
802 if (Op1Reg == 0) return false;
Dan Gohmaneabaed22010-07-07 16:47:08 +0000803 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
804 .addReg(Op0Reg)
805 .addReg(Op1Reg);
Chris Lattner9a08a612008-10-15 04:26:38 +0000806
807 return true;
808}
809
Dan Gohman46510a72010-04-15 01:51:59 +0000810bool X86FastISel::X86SelectCmp(const Instruction *I) {
811 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000812
Owen Andersone50ed302009-08-10 22:56:29 +0000813 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000814 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000815 return false;
816
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000817 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000818 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000819 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000820 switch (CI->getPredicate()) {
821 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000822 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
823 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000824
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000825 unsigned EReg = createResultReg(&X86::GR8RegClass);
826 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000827 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
828 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
829 TII.get(X86::SETNPr), NPReg);
830 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000831 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000832 UpdateValueMap(I, ResultReg);
833 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000834 }
835 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000836 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
837 return false;
838
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000839 unsigned NEReg = createResultReg(&X86::GR8RegClass);
840 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000841 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
842 TII.get(X86::SETNEr), NEReg);
843 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
844 TII.get(X86::SETPr), PReg);
845 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
846 TII.get(X86::OR8rr), ResultReg)
847 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000848 UpdateValueMap(I, ResultReg);
849 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000850 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000851 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
852 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
853 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
854 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
855 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
856 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
857 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
858 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
859 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
860 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
861 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
862 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
863
864 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
865 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
866 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
867 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
868 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
869 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
870 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
871 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
872 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
873 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000874 default:
875 return false;
876 }
877
Dan Gohman46510a72010-04-15 01:51:59 +0000878 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000879 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000880 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000881
Chris Lattner9a08a612008-10-15 04:26:38 +0000882 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000883 if (!X86FastEmitCompare(Op0, Op1, VT))
884 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000885
Dan Gohmaneabaed22010-07-07 16:47:08 +0000886 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000887 UpdateValueMap(I, ResultReg);
888 return true;
889}
Evan Cheng8b19e562008-09-03 06:44:39 +0000890
Dan Gohman46510a72010-04-15 01:51:59 +0000891bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000892 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000893 if (I->getType()->isIntegerTy(8) &&
894 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000895 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000896 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000897 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000898 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000899 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000900 UpdateValueMap(I, ResultReg);
901 return true;
902 }
903
904 return false;
905}
906
Chris Lattner9a08a612008-10-15 04:26:38 +0000907
Dan Gohman46510a72010-04-15 01:51:59 +0000908bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000909 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000910 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000911 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000912 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
913 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000914
Dan Gohmand98d6202008-10-02 22:15:21 +0000915 // Fold the common case of a conditional branch with a comparison.
Dan Gohman46510a72010-04-15 01:51:59 +0000916 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000917 if (CI->hasOneUse()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000918 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000919
Dan Gohmand98d6202008-10-02 22:15:21 +0000920 // Try to take advantage of fallthrough opportunities.
921 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmaneabaed22010-07-07 16:47:08 +0000922 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000923 std::swap(TrueMBB, FalseMBB);
924 Predicate = CmpInst::getInversePredicate(Predicate);
925 }
926
Chris Lattner871d2462008-10-15 03:58:05 +0000927 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
928 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
929
Dan Gohmand98d6202008-10-02 22:15:21 +0000930 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000931 case CmpInst::FCMP_OEQ:
932 std::swap(TrueMBB, FalseMBB);
933 Predicate = CmpInst::FCMP_UNE;
934 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000935 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
936 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
937 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
938 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
939 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
940 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
941 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
942 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
943 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
944 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
945 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
946 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
947 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000948
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000949 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
950 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
951 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
952 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
953 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
954 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
955 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
956 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
957 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
958 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000959 default:
960 return false;
961 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000962
Dan Gohman46510a72010-04-15 01:51:59 +0000963 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000964 if (SwapArgs)
965 std::swap(Op0, Op1);
966
Chris Lattner9a08a612008-10-15 04:26:38 +0000967 // Emit a compare of the LHS and RHS, setting the flags.
968 if (!X86FastEmitCompare(Op0, Op1, VT))
969 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000970
Dan Gohmaneabaed22010-07-07 16:47:08 +0000971 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
972 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000973
974 if (Predicate == CmpInst::FCMP_UNE) {
975 // X86 requires a second branch to handle UNE (and OEQ,
976 // which is mapped to UNE above).
Dan Gohmaneabaed22010-07-07 16:47:08 +0000977 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
978 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000979 }
980
Stuart Hastings3bf91252010-06-17 22:43:56 +0000981 FastEmitBranch(FalseMBB, DL);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000982 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000983 return true;
984 }
Bill Wendling30a64a72008-12-09 23:19:12 +0000985 } else if (ExtractValueInst *EI =
986 dyn_cast<ExtractValueInst>(BI->getCondition())) {
987 // Check to see if the branch instruction is from an "arithmetic with
988 // overflow" intrinsic. The main way these intrinsics are used is:
989 //
990 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
991 // %sum = extractvalue { i32, i1 } %t, 0
992 // %obit = extractvalue { i32, i1 } %t, 1
993 // br i1 %obit, label %overflow, label %normal
994 //
Dan Gohman653456c2009-01-07 00:15:08 +0000995 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +0000996 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +0000997 // looking for the SETO/SETB instruction. If an instruction modifies the
998 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
999 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001000 if (const IntrinsicInst *CI =
1001 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001002 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1003 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1004 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001005 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001006
Chris Lattnera9a42252009-04-12 07:36:01 +00001007 for (MachineBasicBlock::const_reverse_iterator
Dan Gohmaneabaed22010-07-07 16:47:08 +00001008 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1009 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001010 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001011
Evan Cheng1015ba72010-05-21 20:53:24 +00001012 if (MI.definesRegister(Reg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001013 unsigned Src, Dst, SrcSR, DstSR;
Bill Wendling30a64a72008-12-09 23:19:12 +00001014
Chris Lattnera9a42252009-04-12 07:36:01 +00001015 if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
1016 Reg = Src;
1017 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001018 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001019
Chris Lattnera9a42252009-04-12 07:36:01 +00001020 SetMI = &MI;
1021 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001022 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001023
Chris Lattnera9a42252009-04-12 07:36:01 +00001024 const TargetInstrDesc &TID = MI.getDesc();
1025 if (TID.hasUnmodeledSideEffects() ||
1026 TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
1027 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001028 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001029
1030 if (SetMI) {
1031 unsigned OpCode = SetMI->getOpcode();
1032
1033 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001034 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1035 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001036 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001037 FastEmitBranch(FalseMBB, DL);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001038 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001039 return true;
1040 }
Bill Wendling9a901322008-12-10 19:44:24 +00001041 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001042 }
1043 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001044 }
1045
1046 // Otherwise do a clumsy setcc and re-test it.
1047 unsigned OpReg = getRegForValue(BI->getCondition());
1048 if (OpReg == 0) return false;
1049
Dan Gohmaneabaed22010-07-07 16:47:08 +00001050 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1051 .addReg(OpReg).addReg(OpReg);
1052 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1053 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001054 FastEmitBranch(FalseMBB, DL);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001055 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001056 return true;
1057}
1058
Dan Gohman46510a72010-04-15 01:51:59 +00001059bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +00001060 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001061 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001062 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001063 CReg = X86::CL;
1064 RC = &X86::GR8RegClass;
1065 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001066 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
1067 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
1068 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001069 default: return false;
1070 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001071 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001072 CReg = X86::CX;
1073 RC = &X86::GR16RegClass;
1074 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001075 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
1076 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
1077 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001078 default: return false;
1079 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001080 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001081 CReg = X86::ECX;
1082 RC = &X86::GR32RegClass;
1083 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001084 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
1085 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
1086 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001087 default: return false;
1088 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001089 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001090 CReg = X86::RCX;
1091 RC = &X86::GR64RegClass;
1092 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +00001093 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
1094 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
1095 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001096 default: return false;
1097 }
1098 } else {
1099 return false;
1100 }
1101
Owen Andersone50ed302009-08-10 22:56:29 +00001102 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001103 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001104 return false;
1105
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001106 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1107 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +00001108
1109 // Fold immediate in shl(x,3).
Dan Gohman46510a72010-04-15 01:51:59 +00001110 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner743922e2008-09-21 21:44:29 +00001111 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001112 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpImm),
Dan Gohmanb12b1a22008-12-20 17:19:40 +00001113 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
Chris Lattner743922e2008-09-21 21:44:29 +00001114 UpdateValueMap(I, ResultReg);
1115 return true;
1116 }
1117
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001118 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1119 if (Op1Reg == 0) return false;
Dan Gohmaneabaed22010-07-07 16:47:08 +00001120 TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1121 CReg, Op1Reg, RC, RC, DL);
Dan Gohman145b8282008-10-07 21:50:36 +00001122
1123 // The shift instruction uses X86::CL. If we defined a super-register
1124 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1125 // we're doing here.
1126 if (CReg != X86::CL)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001127 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1128 TII.get(TargetOpcode::EXTRACT_SUBREG), X86::CL)
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001129 .addReg(CReg).addImm(X86::sub_8bit);
Dan Gohman145b8282008-10-07 21:50:36 +00001130
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001131 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001132 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1133 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001134 UpdateValueMap(I, ResultReg);
1135 return true;
1136}
1137
Dan Gohman46510a72010-04-15 01:51:59 +00001138bool X86FastISel::X86SelectSelect(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001139 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00001140 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001141 return false;
1142
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001143 unsigned Opc = 0;
1144 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001145 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001146 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001147 RC = &X86::GR16RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001148 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001149 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001150 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001151 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001152 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001153 RC = &X86::GR64RegClass;
1154 } else {
1155 return false;
1156 }
1157
1158 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1159 if (Op0Reg == 0) return false;
1160 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1161 if (Op1Reg == 0) return false;
1162 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1163 if (Op2Reg == 0) return false;
1164
Dan Gohmaneabaed22010-07-07 16:47:08 +00001165 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1166 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001167 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001168 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1169 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001170 UpdateValueMap(I, ResultReg);
1171 return true;
1172}
1173
Dan Gohman46510a72010-04-15 01:51:59 +00001174bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001175 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001176 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001177 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001178 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001179 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001180 unsigned OpReg = getRegForValue(V);
1181 if (OpReg == 0) return false;
1182 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001183 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1184 TII.get(X86::CVTSS2SDrr), ResultReg)
1185 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001186 UpdateValueMap(I, ResultReg);
1187 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001188 }
1189 }
1190
1191 return false;
1192}
1193
Dan Gohman46510a72010-04-15 01:51:59 +00001194bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001195 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001196 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001197 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001198 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001199 unsigned OpReg = getRegForValue(V);
1200 if (OpReg == 0) return false;
1201 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001202 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1203 TII.get(X86::CVTSD2SSrr), ResultReg)
1204 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001205 UpdateValueMap(I, ResultReg);
1206 return true;
1207 }
1208 }
1209 }
1210
1211 return false;
1212}
1213
Dan Gohman46510a72010-04-15 01:51:59 +00001214bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001215 if (Subtarget->is64Bit())
1216 // All other cases should be handled by the tblgen generated code.
1217 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001218 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1219 EVT DstVT = TLI.getValueType(I->getType());
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001220
1221 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001222 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001223 // All other cases should be handled by the tblgen generated code.
1224 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001226 // All other cases should be handled by the tblgen generated code.
1227 return false;
1228
1229 unsigned InputReg = getRegForValue(I->getOperand(0));
1230 if (!InputReg)
1231 // Unhandled operand. Halt "fast" selection and bail.
1232 return false;
1233
Dan Gohman62417622009-04-27 16:33:14 +00001234 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001235 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
1236 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001237 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001238 unsigned CopyReg = createResultReg(CopyRC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001239 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CopyOpc), CopyReg)
1240 .addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001241
1242 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001244 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001245 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001246 if (!ResultReg)
1247 return false;
1248
1249 UpdateValueMap(I, ResultReg);
1250 return true;
1251}
1252
Dan Gohman46510a72010-04-15 01:51:59 +00001253bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1254 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1255 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001256
Dan Gohman46510a72010-04-15 01:51:59 +00001257 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001258 switch (CI->getIntrinsicID()) {
1259 default: break;
1260 case Intrinsic::sadd_with_overflow:
Dan Gohmanf423a692010-07-07 18:32:53 +00001261 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001262 // Cheat a little. We know that the registers for "add" and "seto" are
1263 // allocated sequentially. However, we only keep track of the register
1264 // for "add" in the value map. Use extractvalue's index to get the
1265 // correct register for "seto".
Dan Gohmanf423a692010-07-07 18:32:53 +00001266 unsigned OpReg = getRegForValue(Agg);
1267 if (OpReg == 0)
1268 return false;
1269 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001270 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001271 }
Dan Gohmanf423a692010-07-07 18:32:53 +00001272 }
Bill Wendling52370a12008-12-09 02:42:50 +00001273 }
1274
1275 return false;
1276}
1277
Dan Gohman46510a72010-04-15 01:51:59 +00001278bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001279 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001280 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001281 default: return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001282 case Intrinsic::stackprotector: {
1283 // Emit code inline code to store the stack guard onto the stack.
1284 EVT PtrTy = TLI.getPointerTy();
1285
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001286 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1287 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001288
1289 // Grab the frame index.
1290 X86AddressMode AM;
1291 if (!X86SelectAddress(Slot, AM)) return false;
1292
Eric Christopher88dee302010-03-18 21:58:33 +00001293 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1294
Eric Christopher07754c22010-03-18 20:27:26 +00001295 return true;
1296 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001297 case Intrinsic::objectsize: {
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001298 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001299 const Type *Ty = I.getCalledFunction()->getReturnType();
1300
1301 assert(CI && "Non-constant type in Intrinsic::objectsize?");
1302
1303 EVT VT;
1304 if (!isTypeLegal(Ty, VT))
1305 return false;
1306
1307 unsigned OpC = 0;
1308 if (VT == MVT::i32)
1309 OpC = X86::MOV32ri;
1310 else if (VT == MVT::i64)
1311 OpC = X86::MOV64ri;
1312 else
1313 return false;
1314
1315 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohmaneabaed22010-07-07 16:47:08 +00001316 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001317 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001318 UpdateValueMap(&I, ResultReg);
1319 return true;
1320 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001321 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001322 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001323 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001324 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001325 if (!X86SelectAddress(DI->getAddress(), AM))
1326 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001327 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001328 // FIXME may need to add RegState::Debug to any registers produced,
1329 // although ESP/EBP should be the only ones at the moment.
Dan Gohmaneabaed22010-07-07 16:47:08 +00001330 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1331 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001332 return true;
1333 }
Eric Christopher77f79892010-01-18 22:11:29 +00001334 case Intrinsic::trap: {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001335 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001336 return true;
1337 }
Bill Wendling52370a12008-12-09 02:42:50 +00001338 case Intrinsic::sadd_with_overflow:
1339 case Intrinsic::uadd_with_overflow: {
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001340 // Replace "add with overflow" intrinsics with an "add" instruction followed
1341 // by a seto/setc instruction. Later on, when the "extractvalue"
1342 // instructions are encountered, we use the fact that two registers were
1343 // created sequentially to get the correct registers for the "sum" and the
1344 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001345 const Function *Callee = I.getCalledFunction();
1346 const Type *RetTy =
1347 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1348
Owen Andersone50ed302009-08-10 22:56:29 +00001349 EVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001350 if (!isTypeLegal(RetTy, VT))
1351 return false;
1352
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001353 const Value *Op1 = I.getArgOperand(0);
1354 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001355 unsigned Reg1 = getRegForValue(Op1);
1356 unsigned Reg2 = getRegForValue(Op2);
1357
1358 if (Reg1 == 0 || Reg2 == 0)
1359 // FIXME: Handle values *not* in registers.
1360 return false;
1361
1362 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001363 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001364 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001365 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001366 OpC = X86::ADD64rr;
1367 else
1368 return false;
1369
1370 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohmaneabaed22010-07-07 16:47:08 +00001371 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1372 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001373 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001374
Chris Lattner8d57b772009-04-12 07:51:14 +00001375 // If the add with overflow is an intra-block value then we just want to
1376 // create temporaries for it like normal. If it is a cross-block value then
1377 // UpdateValueMap will return the cross-block register used. Since we
1378 // *really* want the value to be live in the register pair known by
1379 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1380 // the cross block case. In the non-cross-block case, we should just make
1381 // another register for the value.
1382 if (DestReg1 != ResultReg)
1383 ResultReg = DestReg1+1;
1384 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001385 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Chris Lattner8d57b772009-04-12 07:51:14 +00001386
Chris Lattnera9a42252009-04-12 07:36:01 +00001387 unsigned Opc = X86::SETBr;
1388 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1389 Opc = X86::SETOr;
Dan Gohmaneabaed22010-07-07 16:47:08 +00001390 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001391 return true;
1392 }
1393 }
1394}
1395
Dan Gohman46510a72010-04-15 01:51:59 +00001396bool X86FastISel::X86SelectCall(const Instruction *I) {
1397 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001398 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001399
1400 // Can't handle inline asm yet.
1401 if (isa<InlineAsm>(Callee))
1402 return false;
1403
Bill Wendling52370a12008-12-09 02:42:50 +00001404 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001405 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001406 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001407
Evan Chengf3d4efe2008-09-07 09:09:33 +00001408 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001409 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001410 CallingConv::ID CC = CS.getCallingConv();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001411 if (CC != CallingConv::C &&
1412 CC != CallingConv::Fast &&
1413 CC != CallingConv::X86_FastCall)
1414 return false;
1415
Evan Cheng381993f2010-01-27 00:00:57 +00001416 // fastcc with -tailcallopt is intended to provide a guaranteed
1417 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001418 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001419 return false;
1420
Evan Chengf3d4efe2008-09-07 09:09:33 +00001421 // Let SDISel handle vararg functions.
1422 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1423 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1424 if (FTy->isVarArg())
1425 return false;
1426
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001427 // Fast-isel doesn't know about callee-pop yet.
1428 if (Subtarget->IsCalleePop(FTy->isVarArg(), CC))
1429 return false;
1430
Evan Chengf3d4efe2008-09-07 09:09:33 +00001431 // Handle *simple* calls for now.
1432 const Type *RetTy = CS.getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001433 EVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001434 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001435 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001436 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001437 return false;
1438
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001439 // Materialize callee address in a register. FIXME: GV address can be
1440 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001441 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001442 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001443 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001444 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001445 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001446 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001447 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001448 } else if (CalleeAM.Base.Reg != 0) {
1449 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001450 } else
1451 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001452
Evan Chengdebdea02008-09-08 17:15:42 +00001453 // Allow calls which produce i1 results.
1454 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001455 if (RetVT == MVT::i1) {
1456 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001457 AndToI1 = true;
1458 }
1459
Evan Chengf3d4efe2008-09-07 09:09:33 +00001460 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001461 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001462 SmallVector<unsigned, 8> Args;
Owen Andersone50ed302009-08-10 22:56:29 +00001463 SmallVector<EVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001464 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001465 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001466 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001467 ArgVTs.reserve(CS.arg_size());
1468 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001469 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001470 i != e; ++i) {
1471 unsigned Arg = getRegForValue(*i);
1472 if (Arg == 0)
1473 return false;
1474 ISD::ArgFlagsTy Flags;
1475 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001476 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001477 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001478 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001479 Flags.setZExt();
1480
1481 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001482 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1483 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1484 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1485 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001486 return false;
1487
1488 const Type *ArgTy = (*i)->getType();
Owen Andersone50ed302009-08-10 22:56:29 +00001489 EVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001490 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001491 return false;
1492 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1493 Flags.setOrigAlign(OriginalAlignment);
1494
1495 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001496 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001497 ArgVTs.push_back(ArgVT);
1498 ArgFlags.push_back(Flags);
1499 }
1500
1501 // Analyze operands of the call, assigning locations to each operand.
1502 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001503 CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
Dan Gohmand8acddd2010-06-01 21:09:47 +00001504
1505 // Allocate shadow area for Win64
1506 if (Subtarget->isTargetWin64()) {
1507 CCInfo.AllocateStack(32, 8);
1508 }
1509
Evan Chengf3d4efe2008-09-07 09:09:33 +00001510 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1511
1512 // Get a count of how many bytes are to be pushed on the stack.
1513 unsigned NumBytes = CCInfo.getNextStackOffset();
1514
1515 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001516 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohmaneabaed22010-07-07 16:47:08 +00001517 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1518 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001519
Chris Lattner438949a2008-10-15 05:30:52 +00001520 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001521 // copies / loads.
1522 SmallVector<unsigned, 4> RegArgs;
1523 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1524 CCValAssign &VA = ArgLocs[i];
1525 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001526 EVT ArgVT = ArgVTs[VA.getValNo()];
Evan Chengf3d4efe2008-09-07 09:09:33 +00001527
1528 // Promote the value if needed.
1529 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001530 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001531 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001532 case CCValAssign::SExt: {
1533 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1534 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001535 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001536 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001537 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001538 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001539 }
1540 case CCValAssign::ZExt: {
1541 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1542 Arg, ArgVT, Arg);
Chris Lattnera33649e2008-12-19 17:03:38 +00001543 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001544 Emitted = true;
Evan Cheng24e3a902008-09-08 06:35:17 +00001545 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001546 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001547 }
1548 case CCValAssign::AExt: {
1549 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1550 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001551 if (!Emitted)
1552 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001553 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001554 if (!Emitted)
1555 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1556 Arg, ArgVT, Arg);
1557
Chris Lattnera33649e2008-12-19 17:03:38 +00001558 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001559 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001560 break;
1561 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001562 case CCValAssign::BCvt: {
1563 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001564 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001565 assert(BC != 0 && "Failed to emit a bitcast!");
1566 Arg = BC;
1567 ArgVT = VA.getLocVT();
1568 break;
1569 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001570 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001571
1572 if (VA.isRegLoc()) {
1573 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001574 bool Emitted = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1575 VA.getLocReg(), Arg, RC, RC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001576 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001577 Emitted = true;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001578 RegArgs.push_back(VA.getLocReg());
1579 } else {
1580 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001581 X86AddressMode AM;
1582 AM.Base.Reg = StackPtr;
1583 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001584 const Value *ArgVal = ArgVals[VA.getValNo()];
Chris Lattner241ab472008-10-15 05:38:32 +00001585
1586 // If this is a really simple value, emit this with the Value* version of
1587 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1588 // can cause us to reevaluate the argument.
1589 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1590 X86FastEmitStore(ArgVT, ArgVal, AM);
1591 else
1592 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001593 }
1594 }
1595
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001596 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1597 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001598 if (Subtarget->isPICStyleGOT()) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001599 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohmana4160c32010-07-07 16:29:44 +00001600 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001601 bool Emitted = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1602 X86::EBX, Base, RC, RC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001603 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001604 Emitted = true;
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001605 }
Chris Lattner51e8eab2009-07-09 06:34:26 +00001606
Evan Chengf3d4efe2008-09-07 09:09:33 +00001607 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001608 MachineInstrBuilder MIB;
1609 if (CalleeOp) {
1610 // Register-indirect call.
1611 unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
Dan Gohmaneabaed22010-07-07 16:47:08 +00001612 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1613 .addReg(CalleeOp);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001614
1615 } else {
1616 // Direct call.
1617 assert(GV && "Not a direct call");
1618 unsigned CallOpc =
1619 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
1620
1621 // See if we need any target-specific flags on the GV operand.
1622 unsigned char OpFlags = 0;
1623
1624 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1625 // external symbols most go through the PLT in PIC mode. If the symbol
1626 // has hidden or protected visibility, or if it is static or local, then
1627 // we don't need to use the PLT - we can directly call it.
1628 if (Subtarget->isTargetELF() &&
1629 TM.getRelocationModel() == Reloc::PIC_ &&
1630 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1631 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001632 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001633 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1634 Subtarget->getDarwinVers() < 9) {
1635 // PC-relative references to external symbols should go through $stub,
1636 // unless we're building with the leopard linker or later, which
1637 // automatically synthesizes these stubs.
1638 OpFlags = X86II::MO_DARWIN_STUB;
1639 }
1640
1641
Dan Gohmaneabaed22010-07-07 16:47:08 +00001642 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1643 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001644 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001645
1646 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001647 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001648 MIB.addReg(X86::EBX);
1649
Evan Chengf3d4efe2008-09-07 09:09:33 +00001650 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001651 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1652 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001653
1654 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001655 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Dan Gohmaneabaed22010-07-07 16:47:08 +00001656 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1657 .addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001658
1659 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001660 SmallVector<unsigned, 4> UsedRegs;
Owen Anderson825b72b2009-08-11 20:47:22 +00001661 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001662 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001663 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001664 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1665
1666 // Copy all of the result registers out of their specified physreg.
1667 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001668 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001669 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1670 TargetRegisterClass *SrcRC = DstRC;
1671
1672 // If this is a call to a function that returns an fp value on the x87 fp
1673 // stack, but where we prefer to use the value in xmm registers, copy it
1674 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1675 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1676 RVLocs[0].getLocReg() == X86::ST1) &&
1677 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001678 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001679 SrcRC = X86::RSTRegisterClass;
1680 DstRC = X86::RFP80RegisterClass;
1681 }
1682
1683 unsigned ResultReg = createResultReg(DstRC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001684 bool Emitted = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt, ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001685 RVLocs[0].getLocReg(), DstRC, SrcRC, DL);
Chris Lattnera33649e2008-12-19 17:03:38 +00001686 assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
Devang Patelfd1c6c32008-12-23 21:56:28 +00001687 Emitted = true;
Dan Gohmandb497122010-06-18 23:28:01 +00001688 UsedRegs.push_back(RVLocs[0].getLocReg());
1689
Evan Chengf3d4efe2008-09-07 09:09:33 +00001690 if (CopyVT != RVLocs[0].getValVT()) {
1691 // Round the F80 the right size, which also moves to the appropriate xmm
1692 // register. This is accomplished by storing the F80 value in memory and
1693 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001694 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001696 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001697 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001698 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1699 TII.get(Opc)), FI)
1700 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001702 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001704 ResultReg = createResultReg(DstRC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001705 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1706 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001707 }
1708
Evan Chengdebdea02008-09-08 17:15:42 +00001709 if (AndToI1) {
1710 // Mask out all but lowest bit for some call which produces an i1.
1711 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001712 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001713 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001714 ResultReg = AndResult;
1715 }
1716
Evan Chengf3d4efe2008-09-07 09:09:33 +00001717 UpdateValueMap(I, ResultReg);
1718 }
1719
Dan Gohmandb497122010-06-18 23:28:01 +00001720 // Set all unused physreg defs as dead.
1721 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1722
Evan Chengf3d4efe2008-09-07 09:09:33 +00001723 return true;
1724}
1725
1726
Dan Gohman99b21822008-08-28 23:21:34 +00001727bool
Dan Gohman46510a72010-04-15 01:51:59 +00001728X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001729 switch (I->getOpcode()) {
1730 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001731 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001732 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001733 case Instruction::Store:
1734 return X86SelectStore(I);
Dan Gohmanf423a692010-07-07 18:32:53 +00001735 case Instruction::Ret:
1736 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001737 case Instruction::ICmp:
1738 case Instruction::FCmp:
1739 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001740 case Instruction::ZExt:
1741 return X86SelectZExt(I);
1742 case Instruction::Br:
1743 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001744 case Instruction::Call:
1745 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001746 case Instruction::LShr:
1747 case Instruction::AShr:
1748 case Instruction::Shl:
1749 return X86SelectShift(I);
1750 case Instruction::Select:
1751 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001752 case Instruction::Trunc:
1753 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001754 case Instruction::FPExt:
1755 return X86SelectFPExt(I);
1756 case Instruction::FPTrunc:
1757 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001758 case Instruction::ExtractValue:
1759 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001760 case Instruction::IntToPtr: // Deliberate fall-through.
1761 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001762 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1763 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001764 if (DstVT.bitsGT(SrcVT))
1765 return X86SelectZExt(I);
1766 if (DstVT.bitsLT(SrcVT))
1767 return X86SelectTrunc(I);
1768 unsigned Reg = getRegForValue(I->getOperand(0));
1769 if (Reg == 0) return false;
1770 UpdateValueMap(I, Reg);
1771 return true;
1772 }
Dan Gohman99b21822008-08-28 23:21:34 +00001773 }
1774
1775 return false;
1776}
1777
Dan Gohman46510a72010-04-15 01:51:59 +00001778unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Owen Andersone50ed302009-08-10 22:56:29 +00001779 EVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001780 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001781 return false;
1782
1783 // Get opcode and regclass of the output for the given load instruction.
1784 unsigned Opc = 0;
1785 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001786 switch (VT.getSimpleVT().SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001787 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001788 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001789 Opc = X86::MOV8rm;
1790 RC = X86::GR8RegisterClass;
1791 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001792 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001793 Opc = X86::MOV16rm;
1794 RC = X86::GR16RegisterClass;
1795 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001797 Opc = X86::MOV32rm;
1798 RC = X86::GR32RegisterClass;
1799 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001800 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001801 // Must be in x86-64 mode.
1802 Opc = X86::MOV64rm;
1803 RC = X86::GR64RegisterClass;
1804 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001806 if (Subtarget->hasSSE1()) {
1807 Opc = X86::MOVSSrm;
1808 RC = X86::FR32RegisterClass;
1809 } else {
1810 Opc = X86::LD_Fp32m;
1811 RC = X86::RFP32RegisterClass;
1812 }
1813 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001814 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001815 if (Subtarget->hasSSE2()) {
1816 Opc = X86::MOVSDrm;
1817 RC = X86::FR64RegisterClass;
1818 } else {
1819 Opc = X86::LD_Fp64m;
1820 RC = X86::RFP64RegisterClass;
1821 }
1822 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001823 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001824 // No f80 support yet.
1825 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001826 }
1827
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001828 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001829 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001830 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001831 if (X86SelectAddress(C, AM)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001832 if (TLI.getPointerTy() == MVT::i32)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001833 Opc = X86::LEA32r;
1834 else
1835 Opc = X86::LEA64r;
1836 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001837 addLeaAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1838 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001839 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001840 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001841 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001842 }
1843
Owen Anderson3b217c62008-09-06 01:11:01 +00001844 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001845 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001846 if (Align == 0) {
1847 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001848 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001849 }
Owen Anderson95267a12008-09-05 00:06:23 +00001850
Dan Gohman5396c992008-09-30 01:21:32 +00001851 // x86-32 PIC requires a PIC base register for constant pools.
1852 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001853 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001854 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001855 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001856 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001857 } else if (Subtarget->isPICStyleGOT()) {
1858 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001859 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001860 } else if (Subtarget->isPICStyleRIPRel() &&
1861 TM.getCodeModel() == CodeModel::Small) {
1862 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001863 }
Dan Gohman5396c992008-09-30 01:21:32 +00001864
1865 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001866 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001867 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001868 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1869 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001870 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001871
Owen Anderson95267a12008-09-05 00:06:23 +00001872 return ResultReg;
1873}
1874
Dan Gohman46510a72010-04-15 01:51:59 +00001875unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001876 // Fail on dynamic allocas. At this point, getRegForValue has already
1877 // checked its CSE maps, so if we're here trying to handle a dynamic
1878 // alloca, we're not going to succeed. X86SelectAddress has a
1879 // check for dynamic allocas, because it's called directly from
1880 // various places, but TargetMaterializeAlloca also needs a check
1881 // in order to avoid recursion between getRegForValue,
1882 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001883 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001884 return 0;
1885
Dan Gohman0586d912008-09-10 20:11:02 +00001886 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001887 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001888 return 0;
1889 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1890 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1891 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001892 addLeaAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1893 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001894 return ResultReg;
1895}
1896
Evan Chengc3f44b02008-09-03 00:03:49 +00001897namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00001898 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
1899 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00001900 }
Dan Gohman99b21822008-08-28 23:21:34 +00001901}