blob: 6e96638c605fcecc76cd5017971be072d03c75c0 [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"
Jay Foad562b84b2011-04-11 09:35:34 +000026#include "llvm/Operator.h"
Dan Gohman84023e02010-07-10 09:00:22 +000027#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000028#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000029#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000030#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000033#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000035#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000036#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000037using namespace llvm;
38
Chris Lattner087fcf32009-03-08 18:44:31 +000039namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000040
Evan Chengc3f44b02008-09-03 00:03:49 +000041class X86FastISel : public FastISel {
42 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
43 /// make the right decision when generating code for different targets.
44 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000045
46 /// StackPtr - Register used as the stack pointer.
47 ///
48 unsigned StackPtr;
49
Wesley Peckbf17cfa2010-11-23 03:31:01 +000050 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000051 /// floating point ops.
52 /// When SSE is available, use it for f32 operations.
53 /// When SSE2 is available, use it for f64 operations.
54 bool X86ScalarSSEf64;
55 bool X86ScalarSSEf32;
56
Evan Cheng8b19e562008-09-03 06:44:39 +000057public:
Dan Gohmana4160c32010-07-07 16:29:44 +000058 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000059 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000060 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
61 X86ScalarSSEf64 = Subtarget->hasSSE2();
62 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000063 }
Evan Chengc3f44b02008-09-03 00:03:49 +000064
Dan Gohman46510a72010-04-15 01:51:59 +000065 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000066
Chris Lattnerbeac75d2010-09-05 02:18:34 +000067 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
68 /// vreg is being provided by the specified load instruction. If possible,
69 /// try to fold the load as an operand to the instruction, returning true if
70 /// possible.
71 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
72 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000073
Dan Gohman1adf1b02008-08-19 21:45:35 +000074#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000075
76private:
Dan Gohman46510a72010-04-15 01:51:59 +000077 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000078
Owen Andersone50ed302009-08-10 22:56:29 +000079 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Chris Lattnerb44101c2011-04-19 05:09:50 +000081 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
82 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000083
Owen Andersone50ed302009-08-10 22:56:29 +000084 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000085 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000086
Dan Gohman46510a72010-04-15 01:51:59 +000087 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
88 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000089
Dan Gohman46510a72010-04-15 01:51:59 +000090 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000093
Dan Gohman84023e02010-07-10 09:00:22 +000094 bool X86SelectRet(const Instruction *I);
95
Dan Gohman46510a72010-04-15 01:51:59 +000096 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000097
Dan Gohman46510a72010-04-15 01:51:59 +000098 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000099
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000105
Dan Gohman46510a72010-04-15 01:51:59 +0000106 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000107
Dan Gohman46510a72010-04-15 01:51:59 +0000108 bool X86SelectFPExt(const Instruction *I);
109 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000110
Dan Gohman46510a72010-04-15 01:51:59 +0000111 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
112 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000113
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000114 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000115 return getTargetMachine()->getInstrInfo();
116 }
117 const X86TargetMachine *getTargetMachine() const {
118 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000119 }
120
Dan Gohman46510a72010-04-15 01:51:59 +0000121 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000122
Dan Gohman46510a72010-04-15 01:51:59 +0000123 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000124
Eli Friedman2790ba82011-04-27 22:41:55 +0000125 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
126
Evan Chengf3d4efe2008-09-07 09:09:33 +0000127 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
128 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000129 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
131 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000132 }
133
Duncan Sands1440e8b2010-11-03 11:35:31 +0000134 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000135
136 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
137 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000138};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000139
Chris Lattner087fcf32009-03-08 18:44:31 +0000140} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000141
Duncan Sands1440e8b2010-11-03 11:35:31 +0000142bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
143 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
144 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000145 // Unhandled type. Halt "fast" selection and bail.
146 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000147
148 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000149 // For now, require SSE/SSE2 for performing floating-point operations,
150 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000152 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000154 return false;
155 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000157 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000158 // We only handle legal types. For example, on x86-32 the instruction
159 // selector contains all of the 64-bit instructions from x86-64,
160 // under the assumption that i64 won't be used if the target doesn't
161 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000163}
164
165#include "X86GenCallingConv.inc"
166
Evan Cheng0de588f2008-09-05 21:00:03 +0000167/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000168/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000169/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000170bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000171 unsigned &ResultReg) {
172 // Get opcode and regclass of the output for the given load instruction.
173 unsigned Opc = 0;
174 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000176 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000177 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000179 Opc = X86::MOV8rm;
180 RC = X86::GR8RegisterClass;
181 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000183 Opc = X86::MOV16rm;
184 RC = X86::GR16RegisterClass;
185 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000187 Opc = X86::MOV32rm;
188 RC = X86::GR32RegisterClass;
189 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000191 // Must be in x86-64 mode.
192 Opc = X86::MOV64rm;
193 RC = X86::GR64RegisterClass;
194 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000196 if (Subtarget->hasSSE1()) {
197 Opc = X86::MOVSSrm;
198 RC = X86::FR32RegisterClass;
199 } else {
200 Opc = X86::LD_Fp32m;
201 RC = X86::RFP32RegisterClass;
202 }
203 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000205 if (Subtarget->hasSSE2()) {
206 Opc = X86::MOVSDrm;
207 RC = X86::FR64RegisterClass;
208 } else {
209 Opc = X86::LD_Fp64m;
210 RC = X86::RFP64RegisterClass;
211 }
212 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000214 // No f80 support yet.
215 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000216 }
217
218 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000219 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
220 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000221 return true;
222}
223
Evan Chengf3d4efe2008-09-07 09:09:33 +0000224/// X86FastEmitStore - Emit a machine instruction to store a value Val of
225/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
226/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000227/// i.e. V. Return true if it is possible.
228bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000229X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000230 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000231 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 switch (VT.getSimpleVT().SimpleTy) {
233 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000234 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000235 case MVT::i1: {
236 // Mask out all but lowest bit.
237 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000239 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
240 Val = AndResult;
241 }
242 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 case MVT::i8: Opc = X86::MOV8mr; break;
244 case MVT::i16: Opc = X86::MOV16mr; break;
245 case MVT::i32: Opc = X86::MOV32mr; break;
246 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
247 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000248 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000249 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000251 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000252 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000253 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000254
Dan Gohman84023e02010-07-10 09:00:22 +0000255 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
256 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000257 return true;
258}
259
Dan Gohman46510a72010-04-15 01:51:59 +0000260bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000261 const X86AddressMode &AM) {
262 // Handle 'null' like i32/i64 0.
263 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000264 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000265
Chris Lattner438949a2008-10-15 05:30:52 +0000266 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000267 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000268 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000269 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000271 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000272 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 case MVT::i8: Opc = X86::MOV8mi; break;
274 case MVT::i16: Opc = X86::MOV16mi; break;
275 case MVT::i32: Opc = X86::MOV32mi; break;
276 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000277 // Must be a 32-bit sign extended value.
278 if ((int)CI->getSExtValue() == CI->getSExtValue())
279 Opc = X86::MOV64mi32;
280 break;
281 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000282
Chris Lattner438949a2008-10-15 05:30:52 +0000283 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000284 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
285 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000286 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000287 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000288 return true;
289 }
290 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000291
Chris Lattner438949a2008-10-15 05:30:52 +0000292 unsigned ValReg = getRegForValue(Val);
293 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000294 return false;
295
Chris Lattner438949a2008-10-15 05:30:52 +0000296 return X86FastEmitStore(VT, ValReg, AM);
297}
298
Evan Cheng24e3a902008-09-08 06:35:17 +0000299/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
300/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
301/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000302bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
303 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000304 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000305 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
306 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000307
Owen Andersonac34a002008-09-11 19:44:55 +0000308 if (RR != 0) {
309 ResultReg = RR;
310 return true;
311 } else
312 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000313}
314
Dan Gohman0586d912008-09-10 20:11:02 +0000315/// X86SelectAddress - Attempt to fill in an address from the given value.
316///
Dan Gohman46510a72010-04-15 01:51:59 +0000317bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
318 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000319 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000320 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000321 // Don't walk into other basic blocks; it's possible we haven't
322 // visited them yet, so the instructions may not yet be assigned
323 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000324 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
325 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
326 Opcode = I->getOpcode();
327 U = I;
328 }
Dan Gohman46510a72010-04-15 01:51:59 +0000329 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000330 Opcode = C->getOpcode();
331 U = C;
332 }
Dan Gohman0586d912008-09-10 20:11:02 +0000333
Chris Lattner868ee942010-06-15 19:08:40 +0000334 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
335 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000336 // Fast instruction selection doesn't support the special
337 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000338 return false;
339
Dan Gohman35893082008-09-18 23:23:44 +0000340 switch (Opcode) {
341 default: break;
342 case Instruction::BitCast:
343 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000344 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000345
346 case Instruction::IntToPtr:
347 // Look past no-op inttoptrs.
348 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000349 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000350 break;
Dan Gohman35893082008-09-18 23:23:44 +0000351
352 case Instruction::PtrToInt:
353 // Look past no-op ptrtoints.
354 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000355 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000356 break;
Dan Gohman35893082008-09-18 23:23:44 +0000357
358 case Instruction::Alloca: {
359 // Do static allocas.
360 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000361 DenseMap<const AllocaInst*, int>::iterator SI =
362 FuncInfo.StaticAllocaMap.find(A);
363 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000364 AM.BaseType = X86AddressMode::FrameIndexBase;
365 AM.Base.FrameIndex = SI->second;
366 return true;
367 }
368 break;
Dan Gohman35893082008-09-18 23:23:44 +0000369 }
370
371 case Instruction::Add: {
372 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000373 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000374 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
375 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000376 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000377 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000378 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000379 }
Dan Gohman0586d912008-09-10 20:11:02 +0000380 }
Dan Gohman35893082008-09-18 23:23:44 +0000381 break;
382 }
383
384 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000385 X86AddressMode SavedAM = AM;
386
Dan Gohman35893082008-09-18 23:23:44 +0000387 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000388 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000389 unsigned IndexReg = AM.IndexReg;
390 unsigned Scale = AM.Scale;
391 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000392 // Iterate through the indices, folding what we can. Constants can be
393 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000394 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000395 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000396 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000397 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
398 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000399 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
400 continue;
401 }
402
403 // A array/variable index is always of the form i*S where S is the
404 // constant scale size. See if we can push the scale into immediates.
405 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
406 for (;;) {
407 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
408 // Constant-offset addressing.
409 Disp += CI->getSExtValue() * S;
410 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000411 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000412 if (isa<AddOperator>(Op) &&
413 (!isa<Instruction>(Op) ||
414 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
415 == FuncInfo.MBB) &&
416 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
417 // An add (in the same block) with a constant operand. Fold the
418 // constant.
419 ConstantInt *CI =
420 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
421 Disp += CI->getSExtValue() * S;
422 // Iterate on the other operand.
423 Op = cast<AddOperator>(Op)->getOperand(0);
424 continue;
425 }
426 if (IndexReg == 0 &&
427 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
428 (S == 1 || S == 2 || S == 4 || S == 8)) {
429 // Scaled-index addressing.
430 Scale = S;
431 IndexReg = getRegForGEPIndex(Op).first;
432 if (IndexReg == 0)
433 return false;
434 break;
435 }
436 // Unsupported.
437 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000438 }
439 }
Dan Gohman09aae462008-09-26 20:04:15 +0000440 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000441 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000442 break;
Dan Gohman35893082008-09-18 23:23:44 +0000443 // Ok, the GEP indices were covered by constant-offset and scaled-index
444 // addressing. Update the address state and move on to examining the base.
445 AM.IndexReg = IndexReg;
446 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000447 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000448 if (X86SelectAddress(U->getOperand(0), AM))
449 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000450
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000451 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000452 // our address and just match the value instead of completely failing.
453 AM = SavedAM;
454 break;
Dan Gohman35893082008-09-18 23:23:44 +0000455 unsupported_gep:
456 // Ok, the GEP indices weren't all covered.
457 break;
458 }
459 }
460
461 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000462 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0a1c9972011-04-17 17:47:38 +0000463 // Can't handle alternate code models or TLS yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000464 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000465 return false;
466
Dan Gohman46510a72010-04-15 01:51:59 +0000467 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000468 if (GVar->isThreadLocal())
469 return false;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000470
471 // RIP-relative addresses can't have additional register operands, so if
472 // we've already folded stuff into the addressing mode, just force the
473 // global value into its own register, which we can use as the basereg.
474 if (!Subtarget->isPICStyleRIPRel() ||
475 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
476 // Okay, we've committed to selecting this global. Set up the address.
477 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000478
Chris Lattner0a1c9972011-04-17 17:47:38 +0000479 // Allow the subtarget to classify the global.
480 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000481
Chris Lattner0a1c9972011-04-17 17:47:38 +0000482 // If this reference is relative to the pic base, set it now.
483 if (isGlobalRelativeToPICBase(GVFlags)) {
484 // FIXME: How do we know Base.Reg is free??
485 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000486 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000487
488 // Unless the ABI requires an extra load, return a direct reference to
489 // the global.
490 if (!isGlobalStubReference(GVFlags)) {
491 if (Subtarget->isPICStyleRIPRel()) {
492 // Use rip-relative addressing if we can. Above we verified that the
493 // base and index registers are unused.
494 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
495 AM.Base.Reg = X86::RIP;
496 }
497 AM.GVOpFlags = GVFlags;
498 return true;
499 }
500
501 // Ok, we need to do a load from a stub. If we've already loaded from
502 // this stub, reuse the loaded pointer, otherwise emit the load now.
503 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
504 unsigned LoadReg;
505 if (I != LocalValueMap.end() && I->second != 0) {
506 LoadReg = I->second;
507 } else {
508 // Issue load from stub.
509 unsigned Opc = 0;
510 const TargetRegisterClass *RC = NULL;
511 X86AddressMode StubAM;
512 StubAM.Base.Reg = AM.Base.Reg;
513 StubAM.GV = GV;
514 StubAM.GVOpFlags = GVFlags;
515
516 // Prepare for inserting code in the local-value area.
517 SavePoint SaveInsertPt = enterLocalValueArea();
518
519 if (TLI.getPointerTy() == MVT::i64) {
520 Opc = X86::MOV64rm;
521 RC = X86::GR64RegisterClass;
522
523 if (Subtarget->isPICStyleRIPRel())
524 StubAM.Base.Reg = X86::RIP;
525 } else {
526 Opc = X86::MOV32rm;
527 RC = X86::GR32RegisterClass;
528 }
529
530 LoadReg = createResultReg(RC);
531 MachineInstrBuilder LoadMI =
532 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
533 addFullAddress(LoadMI, StubAM);
534
535 // Ok, back to normal mode.
536 leaveLocalValueArea(SaveInsertPt);
537
538 // Prevent loading GV stub multiple times in same MBB.
539 LocalValueMap[V] = LoadReg;
540 }
541
542 // Now construct the final address. Note that the Disp, Scale,
543 // and Index values may already be set here.
544 AM.Base.Reg = LoadReg;
545 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000546 return true;
547 }
Dan Gohman0586d912008-09-10 20:11:02 +0000548 }
549
Dan Gohman97135e12008-09-26 19:15:30 +0000550 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000551 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000552 if (AM.Base.Reg == 0) {
553 AM.Base.Reg = getRegForValue(V);
554 return AM.Base.Reg != 0;
555 }
556 if (AM.IndexReg == 0) {
557 assert(AM.Scale == 1 && "Scale with no index!");
558 AM.IndexReg = getRegForValue(V);
559 return AM.IndexReg != 0;
560 }
561 }
562
563 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000564}
565
Chris Lattner0aa43de2009-07-10 05:33:42 +0000566/// X86SelectCallAddress - Attempt to fill in an address from the given value.
567///
Dan Gohman46510a72010-04-15 01:51:59 +0000568bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
569 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000570 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000571 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000572 Opcode = I->getOpcode();
573 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000574 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000575 Opcode = C->getOpcode();
576 U = C;
577 }
578
579 switch (Opcode) {
580 default: break;
581 case Instruction::BitCast:
582 // Look past bitcasts.
583 return X86SelectCallAddress(U->getOperand(0), AM);
584
585 case Instruction::IntToPtr:
586 // Look past no-op inttoptrs.
587 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
588 return X86SelectCallAddress(U->getOperand(0), AM);
589 break;
590
591 case Instruction::PtrToInt:
592 // Look past no-op ptrtoints.
593 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
594 return X86SelectCallAddress(U->getOperand(0), AM);
595 break;
596 }
597
598 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000599 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000600 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000601 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000602 return false;
603
604 // RIP-relative addresses can't have additional register operands.
605 if (Subtarget->isPICStyleRIPRel() &&
606 (AM.Base.Reg != 0 || AM.IndexReg != 0))
607 return false;
608
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000609 // Can't handle DLLImport.
610 if (GV->hasDLLImportLinkage())
611 return false;
612
613 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000614 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000615 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000616 return false;
617
618 // Okay, we've committed to selecting this global. Set up the basic address.
619 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000620
Chris Lattnere6c07b52009-07-10 05:45:15 +0000621 // No ABI requires an extra load for anything other than DLLImport, which
622 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000623 if (Subtarget->isPICStyleRIPRel()) {
624 // Use rip-relative addressing if we can. Above we verified that the
625 // base and index registers are unused.
626 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
627 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000628 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000629 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
630 } else if (Subtarget->isPICStyleGOT()) {
631 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000632 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000633
Chris Lattner0aa43de2009-07-10 05:33:42 +0000634 return true;
635 }
636
637 // If all else fails, try to materialize the value in a register.
638 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
639 if (AM.Base.Reg == 0) {
640 AM.Base.Reg = getRegForValue(V);
641 return AM.Base.Reg != 0;
642 }
643 if (AM.IndexReg == 0) {
644 assert(AM.Scale == 1 && "Scale with no index!");
645 AM.IndexReg = getRegForValue(V);
646 return AM.IndexReg != 0;
647 }
648 }
649
650 return false;
651}
652
653
Owen Andersona3971df2008-09-04 07:08:58 +0000654/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000655bool X86FastISel::X86SelectStore(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000656 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000657 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000658 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000659
Dan Gohman0586d912008-09-10 20:11:02 +0000660 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000661 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000662 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000663
Chris Lattner438949a2008-10-15 05:30:52 +0000664 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000665}
666
Dan Gohman84023e02010-07-10 09:00:22 +0000667/// X86SelectRet - Select and emit code to implement ret instructions.
668bool X86FastISel::X86SelectRet(const Instruction *I) {
669 const ReturnInst *Ret = cast<ReturnInst>(I);
670 const Function &F = *I->getParent()->getParent();
671
672 if (!FuncInfo.CanLowerReturn)
673 return false;
674
675 CallingConv::ID CC = F.getCallingConv();
676 if (CC != CallingConv::C &&
677 CC != CallingConv::Fast &&
678 CC != CallingConv::X86_FastCall)
679 return false;
680
681 if (Subtarget->isTargetWin64())
682 return false;
683
684 // Don't handle popping bytes on return for now.
685 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
686 ->getBytesToPopOnReturn() != 0)
687 return 0;
688
689 // fastcc with -tailcallopt is intended to provide a guaranteed
690 // tail call optimization. Fastisel doesn't know how to do that.
691 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
692 return false;
693
694 // Let SDISel handle vararg functions.
695 if (F.isVarArg())
696 return false;
697
698 if (Ret->getNumOperands() > 0) {
699 SmallVector<ISD::OutputArg, 4> Outs;
700 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
701 Outs, TLI);
702
703 // Analyze operands of the call, assigning locations to each operand.
704 SmallVector<CCValAssign, 16> ValLocs;
705 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000706 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000707
708 const Value *RV = Ret->getOperand(0);
709 unsigned Reg = getRegForValue(RV);
710 if (Reg == 0)
711 return false;
712
713 // Only handle a single return value for now.
714 if (ValLocs.size() != 1)
715 return false;
716
717 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000718
Dan Gohman84023e02010-07-10 09:00:22 +0000719 // Don't bother handling odd stuff for now.
720 if (VA.getLocInfo() != CCValAssign::Full)
721 return false;
722 // Only handle register returns for now.
723 if (!VA.isRegLoc())
724 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000725
726 // The calling-convention tables for x87 returns don't tell
727 // the whole story.
728 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
729 return false;
730
Eli Friedman22486c92011-05-18 23:13:10 +0000731 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000732 EVT SrcVT = TLI.getValueType(RV->getType());
733 EVT DstVT = VA.getValVT();
734 // Special handling for extended integers.
735 if (SrcVT != DstVT) {
736 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
737 return false;
738
739 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
740 return false;
741
742 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
743
744 if (SrcVT == MVT::i1) {
745 if (Outs[0].Flags.isSExt())
746 return false;
747 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
748 SrcVT = MVT::i8;
749 }
750 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
751 ISD::SIGN_EXTEND;
752 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
753 SrcReg, /*TODO: Kill=*/false);
754 }
755
756 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000757 unsigned DstReg = VA.getLocReg();
758 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000759 // Avoid a cross-class copy. This is very unlikely.
760 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000761 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000762 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
763 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000764
765 // Mark the register as live out of the function.
766 MRI.addLiveOut(VA.getLocReg());
767 }
768
769 // Now emit the RET.
770 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
771 return true;
772}
773
Evan Cheng8b19e562008-09-03 06:44:39 +0000774/// X86SelectLoad - Select and emit code to implement load instructions.
775///
Dan Gohman46510a72010-04-15 01:51:59 +0000776bool X86FastISel::X86SelectLoad(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000777 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000778 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000779 return false;
780
Dan Gohman0586d912008-09-10 20:11:02 +0000781 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000782 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000783 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000784
Evan Cheng0de588f2008-09-05 21:00:03 +0000785 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000786 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000787 UpdateValueMap(I, ResultReg);
788 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000789 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000790 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000791}
792
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000793static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000795 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 case MVT::i8: return X86::CMP8rr;
797 case MVT::i16: return X86::CMP16rr;
798 case MVT::i32: return X86::CMP32rr;
799 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000800 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
801 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000802 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000803}
804
Chris Lattner0e13c782008-10-15 04:13:29 +0000805/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
806/// of the comparison, return an opcode that works for the compare (e.g.
807/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000808static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000810 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000811 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000812 case MVT::i8: return X86::CMP8ri;
813 case MVT::i16: return X86::CMP16ri;
814 case MVT::i32: return X86::CMP32ri;
815 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000816 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
817 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000818 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000819 return X86::CMP64ri32;
820 return 0;
821 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000822}
823
Dan Gohman46510a72010-04-15 01:51:59 +0000824bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
825 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000826 unsigned Op0Reg = getRegForValue(Op0);
827 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000828
Chris Lattnerd53886b2008-10-15 05:18:04 +0000829 // Handle 'null' like i32/i64 0.
830 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000831 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000832
Chris Lattner9a08a612008-10-15 04:26:38 +0000833 // We have two options: compare with register or immediate. If the RHS of
834 // the compare is an immediate that we can fold into this compare, use
835 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000836 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000837 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000838 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
839 .addReg(Op0Reg)
840 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000841 return true;
842 }
843 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000844
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000845 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000846 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000847
Chris Lattner9a08a612008-10-15 04:26:38 +0000848 unsigned Op1Reg = getRegForValue(Op1);
849 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000850 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
851 .addReg(Op0Reg)
852 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000853
Chris Lattner9a08a612008-10-15 04:26:38 +0000854 return true;
855}
856
Dan Gohman46510a72010-04-15 01:51:59 +0000857bool X86FastISel::X86SelectCmp(const Instruction *I) {
858 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000859
Duncan Sands1440e8b2010-11-03 11:35:31 +0000860 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000861 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000862 return false;
863
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000864 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000865 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000866 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000867 switch (CI->getPredicate()) {
868 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000869 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
870 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000871
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000872 unsigned EReg = createResultReg(&X86::GR8RegClass);
873 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000874 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
875 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
876 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000877 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000878 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000879 UpdateValueMap(I, ResultReg);
880 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000881 }
882 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000883 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
884 return false;
885
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000886 unsigned NEReg = createResultReg(&X86::GR8RegClass);
887 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000888 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
890 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000891 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000892 UpdateValueMap(I, ResultReg);
893 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000894 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000895 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
896 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
897 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
898 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
899 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
900 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
901 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
902 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
903 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
904 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
905 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
906 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000907
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000908 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
909 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
910 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
911 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
912 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
913 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
914 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
915 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
916 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
917 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000918 default:
919 return false;
920 }
921
Dan Gohman46510a72010-04-15 01:51:59 +0000922 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000923 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000924 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000925
Chris Lattner9a08a612008-10-15 04:26:38 +0000926 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000927 if (!X86FastEmitCompare(Op0, Op1, VT))
928 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000929
Dan Gohman84023e02010-07-10 09:00:22 +0000930 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000931 UpdateValueMap(I, ResultReg);
932 return true;
933}
Evan Cheng8b19e562008-09-03 06:44:39 +0000934
Dan Gohman46510a72010-04-15 01:51:59 +0000935bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000936 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000937 if (I->getType()->isIntegerTy(8) &&
938 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000939 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000940 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000941 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000942 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000943 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000944 UpdateValueMap(I, ResultReg);
945 return true;
946 }
947
948 return false;
949}
950
Chris Lattner9a08a612008-10-15 04:26:38 +0000951
Dan Gohman46510a72010-04-15 01:51:59 +0000952bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000953 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000954 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000955 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000956 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
957 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000958
Dan Gohman8bef7442010-08-21 02:32:36 +0000959 // Fold the common case of a conditional branch with a comparison
960 // in the same block (values defined on other blocks may not have
961 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000962 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000963 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000964 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000965
Dan Gohmand98d6202008-10-02 22:15:21 +0000966 // Try to take advantage of fallthrough opportunities.
967 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000968 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000969 std::swap(TrueMBB, FalseMBB);
970 Predicate = CmpInst::getInversePredicate(Predicate);
971 }
972
Chris Lattner871d2462008-10-15 03:58:05 +0000973 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
974 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
975
Dan Gohmand98d6202008-10-02 22:15:21 +0000976 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000977 case CmpInst::FCMP_OEQ:
978 std::swap(TrueMBB, FalseMBB);
979 Predicate = CmpInst::FCMP_UNE;
980 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000981 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
982 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
983 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
984 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
985 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
986 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
987 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
988 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
989 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
990 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
991 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
992 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
993 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000994
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000995 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
996 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
997 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
998 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
999 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1000 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1001 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1002 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1003 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1004 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001005 default:
1006 return false;
1007 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001008
Dan Gohman46510a72010-04-15 01:51:59 +00001009 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001010 if (SwapArgs)
1011 std::swap(Op0, Op1);
1012
Chris Lattner9a08a612008-10-15 04:26:38 +00001013 // Emit a compare of the LHS and RHS, setting the flags.
1014 if (!X86FastEmitCompare(Op0, Op1, VT))
1015 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001016
Dan Gohman84023e02010-07-10 09:00:22 +00001017 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1018 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001019
1020 if (Predicate == CmpInst::FCMP_UNE) {
1021 // X86 requires a second branch to handle UNE (and OEQ,
1022 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001023 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1024 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001025 }
1026
Stuart Hastings3bf91252010-06-17 22:43:56 +00001027 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001028 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001029 return true;
1030 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001031 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1032 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1033 // typically happen for _Bool and C++ bools.
1034 MVT SourceVT;
1035 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1036 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1037 unsigned TestOpc = 0;
1038 switch (SourceVT.SimpleTy) {
1039 default: break;
1040 case MVT::i8: TestOpc = X86::TEST8ri; break;
1041 case MVT::i16: TestOpc = X86::TEST16ri; break;
1042 case MVT::i32: TestOpc = X86::TEST32ri; break;
1043 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1044 }
1045 if (TestOpc) {
1046 unsigned OpReg = getRegForValue(TI->getOperand(0));
1047 if (OpReg == 0) return false;
1048 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1049 .addReg(OpReg).addImm(1);
Chris Lattnerc76d1212011-04-19 04:26:32 +00001050
1051 unsigned JmpOpc = X86::JNE_4;
1052 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1053 std::swap(TrueMBB, FalseMBB);
1054 JmpOpc = X86::JE_4;
1055 }
1056
1057 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001058 .addMBB(TrueMBB);
1059 FastEmitBranch(FalseMBB, DL);
1060 FuncInfo.MBB->addSuccessor(TrueMBB);
1061 return true;
1062 }
1063 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001064 }
1065
1066 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001067 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1068 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001069 unsigned OpReg = getRegForValue(BI->getCondition());
1070 if (OpReg == 0) return false;
1071
Eli Friedman547eb4f2011-04-27 01:34:27 +00001072 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1073 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001074 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1075 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001076 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001077 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001078 return true;
1079}
1080
Dan Gohman46510a72010-04-15 01:51:59 +00001081bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001082 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001083 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001084 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001085 CReg = X86::CL;
1086 RC = &X86::GR8RegClass;
1087 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001088 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1089 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1090 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001091 default: return false;
1092 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001093 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001094 CReg = X86::CX;
1095 RC = &X86::GR16RegClass;
1096 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001097 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1098 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1099 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001100 default: return false;
1101 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001102 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001103 CReg = X86::ECX;
1104 RC = &X86::GR32RegClass;
1105 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001106 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1107 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1108 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001109 default: return false;
1110 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001111 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001112 CReg = X86::RCX;
1113 RC = &X86::GR64RegClass;
1114 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001115 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1116 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1117 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001118 default: return false;
1119 }
1120 } else {
1121 return false;
1122 }
1123
Duncan Sands1440e8b2010-11-03 11:35:31 +00001124 MVT VT;
1125 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001126 return false;
1127
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001128 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1129 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001130
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001131 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1132 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001133 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1134 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001135
1136 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001137 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001138 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001139 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1140 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001141 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001142
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001143 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1145 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001146 UpdateValueMap(I, ResultReg);
1147 return true;
1148}
1149
Dan Gohman46510a72010-04-15 01:51:59 +00001150bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001151 MVT VT;
1152 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001153 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001154
Eric Christophere487b012010-09-29 23:00:29 +00001155 // We only use cmov here, if we don't have a cmov instruction bail.
1156 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001157
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001158 unsigned Opc = 0;
1159 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001160 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001161 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001162 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001163 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001164 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001165 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001166 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001167 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001168 RC = &X86::GR64RegClass;
1169 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001170 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001171 }
1172
1173 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1174 if (Op0Reg == 0) return false;
1175 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1176 if (Op1Reg == 0) return false;
1177 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1178 if (Op2Reg == 0) return false;
1179
Dan Gohman84023e02010-07-10 09:00:22 +00001180 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1181 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001182 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001183 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1184 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001185 UpdateValueMap(I, ResultReg);
1186 return true;
1187}
1188
Dan Gohman46510a72010-04-15 01:51:59 +00001189bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001190 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001191 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001192 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001193 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001194 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001195 unsigned OpReg = getRegForValue(V);
1196 if (OpReg == 0) return false;
1197 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001198 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1199 TII.get(X86::CVTSS2SDrr), ResultReg)
1200 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001201 UpdateValueMap(I, ResultReg);
1202 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001203 }
1204 }
1205
1206 return false;
1207}
1208
Dan Gohman46510a72010-04-15 01:51:59 +00001209bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001210 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001211 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001212 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001213 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001214 unsigned OpReg = getRegForValue(V);
1215 if (OpReg == 0) return false;
1216 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001217 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1218 TII.get(X86::CVTSD2SSrr), ResultReg)
1219 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001220 UpdateValueMap(I, ResultReg);
1221 return true;
1222 }
1223 }
1224 }
1225
1226 return false;
1227}
1228
Dan Gohman46510a72010-04-15 01:51:59 +00001229bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001230 if (Subtarget->is64Bit())
1231 // All other cases should be handled by the tblgen generated code.
1232 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001233 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1234 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001235
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001236 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001238 // All other cases should be handled by the tblgen generated code.
1239 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001241 // All other cases should be handled by the tblgen generated code.
1242 return false;
1243
1244 unsigned InputReg = getRegForValue(I->getOperand(0));
1245 if (!InputReg)
1246 // Unhandled operand. Halt "fast" selection and bail.
1247 return false;
1248
Dan Gohman62417622009-04-27 16:33:14 +00001249 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001250 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001251 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001252 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1254 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001255
1256 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001258 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001259 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001260 if (!ResultReg)
1261 return false;
1262
1263 UpdateValueMap(I, ResultReg);
1264 return true;
1265}
1266
Eli Friedmand5089a92011-04-27 01:45:07 +00001267bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1268 X86AddressMode SrcAM, uint64_t Len) {
1269 // Make sure we don't bloat code by inlining very large memcpy's.
1270 bool i64Legal = TLI.isTypeLegal(MVT::i64);
1271 if (Len > (i64Legal ? 32 : 16)) return false;
1272
1273 // We don't care about alignment here since we just emit integer accesses.
1274 while (Len) {
1275 MVT VT;
1276 if (Len >= 8 && i64Legal)
1277 VT = MVT::i64;
1278 else if (Len >= 4)
1279 VT = MVT::i32;
1280 else if (Len >= 2)
1281 VT = MVT::i16;
1282 else {
1283 assert(Len == 1);
1284 VT = MVT::i8;
1285 }
1286
1287 unsigned Reg;
1288 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1289 RV &= X86FastEmitStore(VT, Reg, DestAM);
1290 assert(RV && "Failed to emit load or store??");
1291
1292 unsigned Size = VT.getSizeInBits()/8;
1293 Len -= Size;
1294 DestAM.Disp += Size;
1295 SrcAM.Disp += Size;
1296 }
1297
1298 return true;
1299}
1300
Dan Gohman46510a72010-04-15 01:51:59 +00001301bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001302 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001303 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001304 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001305 case Intrinsic::memcpy: {
1306 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1307 // Don't handle volatile or variable length memcpys.
1308 if (MCI.isVolatile() || !isa<ConstantInt>(MCI.getLength()))
1309 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001310
Chris Lattner832e4942011-04-19 05:52:03 +00001311 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
Chris Lattner832e4942011-04-19 05:52:03 +00001312
1313 // Get the address of the dest and source addresses.
1314 X86AddressMode DestAM, SrcAM;
1315 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1316 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1317 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001318
1319 return TryEmitSmallMemcpy(DestAM, SrcAM, Len);
Chris Lattner832e4942011-04-19 05:52:03 +00001320 }
1321
Eric Christopher07754c22010-03-18 20:27:26 +00001322 case Intrinsic::stackprotector: {
1323 // Emit code inline code to store the stack guard onto the stack.
1324 EVT PtrTy = TLI.getPointerTy();
1325
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001326 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1327 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001328
1329 // Grab the frame index.
1330 X86AddressMode AM;
1331 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001332 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001333 return true;
1334 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001335 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001336 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001337 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001338 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001339 if (!X86SelectAddress(DI->getAddress(), AM))
1340 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001341 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001342 // FIXME may need to add RegState::Debug to any registers produced,
1343 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001344 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1345 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001346 return true;
1347 }
Eric Christopher77f79892010-01-18 22:11:29 +00001348 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001350 return true;
1351 }
Bill Wendling52370a12008-12-09 02:42:50 +00001352 case Intrinsic::sadd_with_overflow:
1353 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001354 // FIXME: Should fold immediates.
1355
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001356 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001357 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001358 const Function *Callee = I.getCalledFunction();
1359 const Type *RetTy =
1360 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1361
Duncan Sands1440e8b2010-11-03 11:35:31 +00001362 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001363 if (!isTypeLegal(RetTy, VT))
1364 return false;
1365
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001366 const Value *Op1 = I.getArgOperand(0);
1367 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001368 unsigned Reg1 = getRegForValue(Op1);
1369 unsigned Reg2 = getRegForValue(Op2);
1370
1371 if (Reg1 == 0 || Reg2 == 0)
1372 // FIXME: Handle values *not* in registers.
1373 return false;
1374
1375 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001376 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001377 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001378 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001379 OpC = X86::ADD64rr;
1380 else
1381 return false;
1382
Eli Friedman482feb32011-05-16 21:06:17 +00001383 // The call to CreateRegs builds two sequential registers, to store the
1384 // both the the returned values.
1385 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001386 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1387 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001388
Chris Lattnera9a42252009-04-12 07:36:01 +00001389 unsigned Opc = X86::SETBr;
1390 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1391 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001392 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1393
1394 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001395 return true;
1396 }
1397 }
1398}
1399
Dan Gohman46510a72010-04-15 01:51:59 +00001400bool X86FastISel::X86SelectCall(const Instruction *I) {
1401 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001402 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001403
1404 // Can't handle inline asm yet.
1405 if (isa<InlineAsm>(Callee))
1406 return false;
1407
Bill Wendling52370a12008-12-09 02:42:50 +00001408 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001409 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001410 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001411
Evan Chengf3d4efe2008-09-07 09:09:33 +00001412 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001413 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001414 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001415 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001416 CC != CallingConv::X86_FastCall)
1417 return false;
1418
Evan Cheng381993f2010-01-27 00:00:57 +00001419 // fastcc with -tailcallopt is intended to provide a guaranteed
1420 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001421 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001422 return false;
1423
Evan Chengf3d4efe2008-09-07 09:09:33 +00001424 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1425 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001426 bool isVarArg = FTy->isVarArg();
1427
1428 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1429 // x86-32. Special handling for x86-64 is implemented.
1430 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001431 return false;
1432
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001433 // Fast-isel doesn't know about callee-pop yet.
Eli Friedman37620462011-04-19 17:22:22 +00001434 if (Subtarget->IsCalleePop(isVarArg, CC))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001435 return false;
1436
Eli Friedman19515b42011-05-17 18:29:03 +00001437 // Check whether the function can return without sret-demotion.
1438 SmallVector<ISD::OutputArg, 4> Outs;
1439 SmallVector<uint64_t, 4> Offsets;
1440 GetReturnInfo(I->getType(), CS.getAttributes().getRetAttributes(),
1441 Outs, TLI, &Offsets);
1442 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
1443 FTy->isVarArg(), Outs, FTy->getContext());
1444 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001445 return false;
1446
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001447 // Materialize callee address in a register. FIXME: GV address can be
1448 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001449 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001450 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001451 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001452 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001453 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001454 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001455 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001456 } else if (CalleeAM.Base.Reg != 0) {
1457 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001458 } else
1459 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001460
Evan Chengf3d4efe2008-09-07 09:09:33 +00001461 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001462 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001463 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001464 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001465 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001466 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001467 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001468 ArgVTs.reserve(CS.arg_size());
1469 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001470 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001471 i != e; ++i) {
Chris Lattnere03b8d32011-04-19 04:42:38 +00001472 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001473 ISD::ArgFlagsTy Flags;
1474 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001475 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001476 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001477 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001478 Flags.setZExt();
1479
Chris Lattnere03b8d32011-04-19 04:42:38 +00001480 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1481 // instruction. This is safe because it is common to all fastisel supported
1482 // calling conventions on x86.
1483 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1484 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1485 CI->getBitWidth() == 16) {
1486 if (Flags.isSExt())
1487 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1488 else
1489 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1490 }
1491 }
1492
Chris Lattnerb44101c2011-04-19 05:09:50 +00001493 unsigned ArgReg;
Chris Lattnerff009ad2011-04-19 05:15:59 +00001494
1495 // Passing bools around ends up doing a trunc to i1 and passing it.
1496 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001497 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1498 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1499 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001500 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1501 ArgReg = getRegForValue(ArgVal);
1502 if (ArgReg == 0) return false;
1503
1504 MVT ArgVT;
1505 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
1506
1507 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1508 ArgVal->hasOneUse(), 1);
1509 } else {
1510 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001511 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001512
Chris Lattnerff009ad2011-04-19 05:15:59 +00001513 if (ArgReg == 0) return false;
1514
Evan Chengf3d4efe2008-09-07 09:09:33 +00001515 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001516 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
Devang Patel05988662008-09-25 21:00:45 +00001517 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1518 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001519 return false;
1520
Chris Lattnere03b8d32011-04-19 04:42:38 +00001521 const Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001522 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001523 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001524 return false;
1525 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1526 Flags.setOrigAlign(OriginalAlignment);
1527
Chris Lattnerb44101c2011-04-19 05:09:50 +00001528 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001529 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001530 ArgVTs.push_back(ArgVT);
1531 ArgFlags.push_back(Flags);
1532 }
1533
1534 // Analyze operands of the call, assigning locations to each operand.
1535 SmallVector<CCValAssign, 16> ArgLocs;
Eli Friedman37620462011-04-19 17:22:22 +00001536 CCState CCInfo(CC, isVarArg, TM, ArgLocs, I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001537
Dan Gohmand8acddd2010-06-01 21:09:47 +00001538 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001539 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001540 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001541
Duncan Sands45907662010-10-31 13:21:44 +00001542 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001543
1544 // Get a count of how many bytes are to be pushed on the stack.
1545 unsigned NumBytes = CCInfo.getNextStackOffset();
1546
1547 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001548 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001549 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1550 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001551
Chris Lattner438949a2008-10-15 05:30:52 +00001552 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001553 // copies / loads.
1554 SmallVector<unsigned, 4> RegArgs;
1555 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1556 CCValAssign &VA = ArgLocs[i];
1557 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001558 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001559
Evan Chengf3d4efe2008-09-07 09:09:33 +00001560 // Promote the value if needed.
1561 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001562 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001563 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001564 case CCValAssign::SExt: {
1565 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1566 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001567 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001568 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001569 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001570 }
1571 case CCValAssign::ZExt: {
1572 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1573 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001574 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001575 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001576 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001577 }
1578 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001579 // We don't handle MMX parameters yet.
1580 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1581 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001582 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1583 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001584 if (!Emitted)
1585 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001586 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001587 if (!Emitted)
1588 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1589 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001590
Chris Lattnerc46ec642011-01-05 22:26:52 +00001591 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001592 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001593 break;
1594 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001595 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001596 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001597 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001598 assert(BC != 0 && "Failed to emit a bitcast!");
1599 Arg = BC;
1600 ArgVT = VA.getLocVT();
1601 break;
1602 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001603 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001604
Evan Chengf3d4efe2008-09-07 09:09:33 +00001605 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001606 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1607 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001608 RegArgs.push_back(VA.getLocReg());
1609 } else {
1610 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001611 X86AddressMode AM;
1612 AM.Base.Reg = StackPtr;
1613 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001614 const Value *ArgVal = ArgVals[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001615
Chris Lattner241ab472008-10-15 05:38:32 +00001616 // If this is a really simple value, emit this with the Value* version of
1617 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1618 // can cause us to reevaluate the argument.
1619 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1620 X86FastEmitStore(ArgVT, ArgVal, AM);
1621 else
1622 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001623 }
1624 }
1625
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001626 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001627 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001628 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001629 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001630 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1631 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001632 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001633
Eli Friedman37620462011-04-19 17:22:22 +00001634 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1635 // Count the number of XMM registers allocated.
1636 static const unsigned XMMArgRegs[] = {
1637 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1638 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1639 };
1640 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1641 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1642 X86::AL).addImm(NumXMMRegs);
1643 }
1644
Evan Chengf3d4efe2008-09-07 09:09:33 +00001645 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001646 MachineInstrBuilder MIB;
1647 if (CalleeOp) {
1648 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001649 unsigned CallOpc;
1650 if (Subtarget->isTargetWin64())
1651 CallOpc = X86::WINCALL64r;
1652 else if (Subtarget->is64Bit())
1653 CallOpc = X86::CALL64r;
1654 else
1655 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001656 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1657 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001658
Chris Lattner51e8eab2009-07-09 06:34:26 +00001659 } else {
1660 // Direct call.
1661 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001662 unsigned CallOpc;
1663 if (Subtarget->isTargetWin64())
1664 CallOpc = X86::WINCALL64pcrel32;
1665 else if (Subtarget->is64Bit())
1666 CallOpc = X86::CALL64pcrel32;
1667 else
1668 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001669
Chris Lattner51e8eab2009-07-09 06:34:26 +00001670 // See if we need any target-specific flags on the GV operand.
1671 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001672
Chris Lattner51e8eab2009-07-09 06:34:26 +00001673 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1674 // external symbols most go through the PLT in PIC mode. If the symbol
1675 // has hidden or protected visibility, or if it is static or local, then
1676 // we don't need to use the PLT - we can directly call it.
1677 if (Subtarget->isTargetELF() &&
1678 TM.getRelocationModel() == Reloc::PIC_ &&
1679 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1680 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001681 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001682 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001683 (!Subtarget->getTargetTriple().isMacOSX() ||
1684 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001685 // PC-relative references to external symbols should go through $stub,
1686 // unless we're building with the leopard linker or later, which
1687 // automatically synthesizes these stubs.
1688 OpFlags = X86II::MO_DARWIN_STUB;
1689 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001690
1691
Dan Gohman84023e02010-07-10 09:00:22 +00001692 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1693 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001694 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001695
1696 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001697 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001698 MIB.addReg(X86::EBX);
1699
Eli Friedman37620462011-04-19 17:22:22 +00001700 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1701 MIB.addReg(X86::AL);
1702
Evan Chengf3d4efe2008-09-07 09:09:33 +00001703 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001704 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1705 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001706
1707 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001708 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eli Friedmand227eed2011-04-28 20:19:12 +00001709 unsigned NumBytesCallee = 0;
1710 if (!Subtarget->is64Bit() && CS.paramHasAttr(1, Attribute::StructRet))
1711 NumBytesCallee = 4;
Dan Gohman84023e02010-07-10 09:00:22 +00001712 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001713 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001714
Eli Friedman19515b42011-05-17 18:29:03 +00001715 // Build info for return calling conv lowering code.
1716 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1717 SmallVector<ISD::InputArg, 32> Ins;
1718 SmallVector<EVT, 4> RetTys;
1719 ComputeValueVTs(TLI, I->getType(), RetTys);
1720 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1721 EVT VT = RetTys[i];
1722 EVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
1723 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1724 for (unsigned j = 0; j != NumRegs; ++j) {
1725 ISD::InputArg MyFlags;
1726 MyFlags.VT = RegisterVT.getSimpleVT();
1727 MyFlags.Used = !CS.getInstruction()->use_empty();
1728 if (CS.paramHasAttr(0, Attribute::SExt))
1729 MyFlags.Flags.setSExt();
1730 if (CS.paramHasAttr(0, Attribute::ZExt))
1731 MyFlags.Flags.setZExt();
1732 if (CS.paramHasAttr(0, Attribute::InReg))
1733 MyFlags.Flags.setInReg();
1734 Ins.push_back(MyFlags);
1735 }
1736 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001737
Eli Friedman19515b42011-05-17 18:29:03 +00001738 // Now handle call return values.
1739 SmallVector<unsigned, 4> UsedRegs;
1740 SmallVector<CCValAssign, 16> RVLocs;
1741 CCState CCRetInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
1742 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1743 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1744 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1745 EVT CopyVT = RVLocs[i].getValVT();
1746 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001747
Evan Chengf3d4efe2008-09-07 09:09:33 +00001748 // If this is a call to a function that returns an fp value on the x87 fp
1749 // stack, but where we prefer to use the value in xmm registers, copy it
1750 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00001751 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1752 RVLocs[i].getLocReg() == X86::ST1) &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001753 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 CopyVT = MVT::f80;
Eli Friedman19515b42011-05-17 18:29:03 +00001755 CopyReg = createResultReg(X86::RFP80RegisterClass);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001756 }
1757
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001758 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eli Friedman19515b42011-05-17 18:29:03 +00001759 CopyReg).addReg(RVLocs[i].getLocReg());
1760 UsedRegs.push_back(RVLocs[i].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001761
Eli Friedman19515b42011-05-17 18:29:03 +00001762 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001763 // Round the F80 the right size, which also moves to the appropriate xmm
1764 // register. This is accomplished by storing the F80 value in memory and
1765 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00001766 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001768 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001769 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001770 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1771 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00001772 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001773 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00001774 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00001775 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001776 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001777 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00001778
Eli Friedman19515b42011-05-17 18:29:03 +00001779 if (RVLocs.size())
1780 UpdateValueMap(I, ResultReg, RVLocs.size());
1781
Dan Gohmandb497122010-06-18 23:28:01 +00001782 // Set all unused physreg defs as dead.
1783 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1784
Evan Chengf3d4efe2008-09-07 09:09:33 +00001785 return true;
1786}
1787
1788
Dan Gohman99b21822008-08-28 23:21:34 +00001789bool
Dan Gohman46510a72010-04-15 01:51:59 +00001790X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001791 switch (I->getOpcode()) {
1792 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001793 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001794 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001795 case Instruction::Store:
1796 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001797 case Instruction::Ret:
1798 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001799 case Instruction::ICmp:
1800 case Instruction::FCmp:
1801 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001802 case Instruction::ZExt:
1803 return X86SelectZExt(I);
1804 case Instruction::Br:
1805 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001806 case Instruction::Call:
1807 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001808 case Instruction::LShr:
1809 case Instruction::AShr:
1810 case Instruction::Shl:
1811 return X86SelectShift(I);
1812 case Instruction::Select:
1813 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001814 case Instruction::Trunc:
1815 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001816 case Instruction::FPExt:
1817 return X86SelectFPExt(I);
1818 case Instruction::FPTrunc:
1819 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001820 case Instruction::IntToPtr: // Deliberate fall-through.
1821 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001822 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1823 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001824 if (DstVT.bitsGT(SrcVT))
1825 return X86SelectZExt(I);
1826 if (DstVT.bitsLT(SrcVT))
1827 return X86SelectTrunc(I);
1828 unsigned Reg = getRegForValue(I->getOperand(0));
1829 if (Reg == 0) return false;
1830 UpdateValueMap(I, Reg);
1831 return true;
1832 }
Dan Gohman99b21822008-08-28 23:21:34 +00001833 }
1834
1835 return false;
1836}
1837
Dan Gohman46510a72010-04-15 01:51:59 +00001838unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001839 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001840 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001841 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001842
Owen Anderson95267a12008-09-05 00:06:23 +00001843 // Get opcode and regclass of the output for the given load instruction.
1844 unsigned Opc = 0;
1845 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001846 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001847 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001849 Opc = X86::MOV8rm;
1850 RC = X86::GR8RegisterClass;
1851 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001852 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001853 Opc = X86::MOV16rm;
1854 RC = X86::GR16RegisterClass;
1855 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001857 Opc = X86::MOV32rm;
1858 RC = X86::GR32RegisterClass;
1859 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001861 // Must be in x86-64 mode.
1862 Opc = X86::MOV64rm;
1863 RC = X86::GR64RegisterClass;
1864 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001865 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001866 if (Subtarget->hasSSE1()) {
1867 Opc = X86::MOVSSrm;
1868 RC = X86::FR32RegisterClass;
1869 } else {
1870 Opc = X86::LD_Fp32m;
1871 RC = X86::RFP32RegisterClass;
1872 }
1873 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001875 if (Subtarget->hasSSE2()) {
1876 Opc = X86::MOVSDrm;
1877 RC = X86::FR64RegisterClass;
1878 } else {
1879 Opc = X86::LD_Fp64m;
1880 RC = X86::RFP64RegisterClass;
1881 }
1882 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001884 // No f80 support yet.
1885 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001886 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001887
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001888 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001889 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001890 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001891 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00001892 // If the expression is just a basereg, then we're done, otherwise we need
1893 // to emit an LEA.
1894 if (AM.BaseType == X86AddressMode::RegBase &&
1895 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
1896 return AM.Base.Reg;
1897
1898 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001899 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001900 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1901 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001902 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001903 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001904 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001905 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001906
Owen Anderson3b217c62008-09-06 01:11:01 +00001907 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001908 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001909 if (Align == 0) {
1910 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001911 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001912 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001913
Dan Gohman5396c992008-09-30 01:21:32 +00001914 // x86-32 PIC requires a PIC base register for constant pools.
1915 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001916 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001917 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001918 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001919 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001920 } else if (Subtarget->isPICStyleGOT()) {
1921 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001922 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001923 } else if (Subtarget->isPICStyleRIPRel() &&
1924 TM.getCodeModel() == CodeModel::Small) {
1925 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001926 }
Dan Gohman5396c992008-09-30 01:21:32 +00001927
1928 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001929 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001930 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001931 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1932 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001933 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001934
Owen Anderson95267a12008-09-05 00:06:23 +00001935 return ResultReg;
1936}
1937
Dan Gohman46510a72010-04-15 01:51:59 +00001938unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001939 // Fail on dynamic allocas. At this point, getRegForValue has already
1940 // checked its CSE maps, so if we're here trying to handle a dynamic
1941 // alloca, we're not going to succeed. X86SelectAddress has a
1942 // check for dynamic allocas, because it's called directly from
1943 // various places, but TargetMaterializeAlloca also needs a check
1944 // in order to avoid recursion between getRegForValue,
1945 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001946 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001947 return 0;
1948
Dan Gohman0586d912008-09-10 20:11:02 +00001949 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001950 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001951 return 0;
1952 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1953 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1954 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001955 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1956 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001957 return ResultReg;
1958}
1959
Eli Friedman2790ba82011-04-27 22:41:55 +00001960unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
1961 MVT VT;
1962 if (!isTypeLegal(CF->getType(), VT))
1963 return false;
1964
1965 // Get opcode and regclass for the given zero.
1966 unsigned Opc = 0;
1967 const TargetRegisterClass *RC = NULL;
1968 switch (VT.SimpleTy) {
1969 default: return false;
1970 case MVT::f32:
1971 if (Subtarget->hasSSE1()) {
1972 Opc = X86::FsFLD0SS;
1973 RC = X86::FR32RegisterClass;
1974 } else {
1975 Opc = X86::LD_Fp032;
1976 RC = X86::RFP32RegisterClass;
1977 }
1978 break;
1979 case MVT::f64:
1980 if (Subtarget->hasSSE2()) {
1981 Opc = X86::FsFLD0SD;
1982 RC = X86::FR64RegisterClass;
1983 } else {
1984 Opc = X86::LD_Fp064;
1985 RC = X86::RFP64RegisterClass;
1986 }
1987 break;
1988 case MVT::f80:
1989 // No f80 support yet.
1990 return false;
1991 }
1992
1993 unsigned ResultReg = createResultReg(RC);
1994 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
1995 return ResultReg;
1996}
1997
1998
Chris Lattnerbeac75d2010-09-05 02:18:34 +00001999/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2000/// vreg is being provided by the specified load instruction. If possible,
2001/// try to fold the load as an operand to the instruction, returning true if
2002/// possible.
2003bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2004 const LoadInst *LI) {
2005 X86AddressMode AM;
2006 if (!X86SelectAddress(LI->getOperand(0), AM))
2007 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002008
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002009 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002010
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002011 unsigned Size = TD.getTypeAllocSize(LI->getType());
2012 unsigned Alignment = LI->getAlignment();
2013
2014 SmallVector<MachineOperand, 8> AddrOps;
2015 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002016
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002017 MachineInstr *Result =
2018 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2019 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002020
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002021 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002022 MI->eraseFromParent();
2023 return true;
2024}
2025
2026
Evan Chengc3f44b02008-09-03 00:03:49 +00002027namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00002028 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2029 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002030 }
Dan Gohman99b21822008-08-28 23:21:34 +00002031}