blob: e11d312815902fd4d2855977d484b9dbf6ca116a [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
Dan Gohman84023e02010-07-10 09:00:22 +0000731 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman107ffd52011-05-18 23:11:30 +0000732 EVT SrcVT = TLI.getValueType(RV->getType());
733 EVT DstVT = VA.getValVT();
734 // Special handling for extended integers.
735 if (SrcVT != DstVT) {
736 return false;
737 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
738 return false;
739
740 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
741 return false;
742
743 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
744
745 if (SrcVT == MVT::i1) {
746 if (Outs[0].Flags.isSExt())
747 return false;
748 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
749 SrcVT = MVT::i8;
750 }
751 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
752 ISD::SIGN_EXTEND;
753 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
754 SrcReg, /*TODO: Kill=*/false);
755 }
756
757 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000758 unsigned DstReg = VA.getLocReg();
759 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000760 // Avoid a cross-class copy. This is very unlikely.
761 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000762 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000763 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
764 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000765
766 // Mark the register as live out of the function.
767 MRI.addLiveOut(VA.getLocReg());
768 }
769
770 // Now emit the RET.
771 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
772 return true;
773}
774
Evan Cheng8b19e562008-09-03 06:44:39 +0000775/// X86SelectLoad - Select and emit code to implement load instructions.
776///
Dan Gohman46510a72010-04-15 01:51:59 +0000777bool X86FastISel::X86SelectLoad(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000778 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000779 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000780 return false;
781
Dan Gohman0586d912008-09-10 20:11:02 +0000782 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000783 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000784 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000785
Evan Cheng0de588f2008-09-05 21:00:03 +0000786 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000787 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000788 UpdateValueMap(I, ResultReg);
789 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000790 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000791 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000792}
793
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000794static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000796 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 case MVT::i8: return X86::CMP8rr;
798 case MVT::i16: return X86::CMP16rr;
799 case MVT::i32: return X86::CMP32rr;
800 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000801 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
802 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000803 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000804}
805
Chris Lattner0e13c782008-10-15 04:13:29 +0000806/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
807/// of the comparison, return an opcode that works for the compare (e.g.
808/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000809static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000811 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000812 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 case MVT::i8: return X86::CMP8ri;
814 case MVT::i16: return X86::CMP16ri;
815 case MVT::i32: return X86::CMP32ri;
816 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000817 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
818 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000819 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000820 return X86::CMP64ri32;
821 return 0;
822 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000823}
824
Dan Gohman46510a72010-04-15 01:51:59 +0000825bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
826 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000827 unsigned Op0Reg = getRegForValue(Op0);
828 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000829
Chris Lattnerd53886b2008-10-15 05:18:04 +0000830 // Handle 'null' like i32/i64 0.
831 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000832 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000833
Chris Lattner9a08a612008-10-15 04:26:38 +0000834 // We have two options: compare with register or immediate. If the RHS of
835 // the compare is an immediate that we can fold into this compare, use
836 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000837 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000838 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000839 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
840 .addReg(Op0Reg)
841 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000842 return true;
843 }
844 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000845
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000846 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000847 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000848
Chris Lattner9a08a612008-10-15 04:26:38 +0000849 unsigned Op1Reg = getRegForValue(Op1);
850 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000851 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
852 .addReg(Op0Reg)
853 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000854
Chris Lattner9a08a612008-10-15 04:26:38 +0000855 return true;
856}
857
Dan Gohman46510a72010-04-15 01:51:59 +0000858bool X86FastISel::X86SelectCmp(const Instruction *I) {
859 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000860
Duncan Sands1440e8b2010-11-03 11:35:31 +0000861 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000862 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000863 return false;
864
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000865 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000866 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000867 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000868 switch (CI->getPredicate()) {
869 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000870 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
871 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000872
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000873 unsigned EReg = createResultReg(&X86::GR8RegClass);
874 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000875 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
876 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
877 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000878 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000879 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000880 UpdateValueMap(I, ResultReg);
881 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000882 }
883 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000884 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
885 return false;
886
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000887 unsigned NEReg = createResultReg(&X86::GR8RegClass);
888 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
890 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
891 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000892 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000893 UpdateValueMap(I, ResultReg);
894 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000895 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000896 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
897 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
898 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
899 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
900 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
901 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
902 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
903 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
904 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
905 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
906 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
907 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000908
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000909 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
910 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
911 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
912 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
913 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
914 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
915 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
916 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
917 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
918 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000919 default:
920 return false;
921 }
922
Dan Gohman46510a72010-04-15 01:51:59 +0000923 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000924 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000925 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000926
Chris Lattner9a08a612008-10-15 04:26:38 +0000927 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000928 if (!X86FastEmitCompare(Op0, Op1, VT))
929 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000930
Dan Gohman84023e02010-07-10 09:00:22 +0000931 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000932 UpdateValueMap(I, ResultReg);
933 return true;
934}
Evan Cheng8b19e562008-09-03 06:44:39 +0000935
Dan Gohman46510a72010-04-15 01:51:59 +0000936bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000937 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000938 if (I->getType()->isIntegerTy(8) &&
939 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000940 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000941 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000942 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000943 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000944 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000945 UpdateValueMap(I, ResultReg);
946 return true;
947 }
948
949 return false;
950}
951
Chris Lattner9a08a612008-10-15 04:26:38 +0000952
Dan Gohman46510a72010-04-15 01:51:59 +0000953bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000954 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000955 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000956 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000957 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
958 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000959
Dan Gohman8bef7442010-08-21 02:32:36 +0000960 // Fold the common case of a conditional branch with a comparison
961 // in the same block (values defined on other blocks may not have
962 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000963 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000964 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000965 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000966
Dan Gohmand98d6202008-10-02 22:15:21 +0000967 // Try to take advantage of fallthrough opportunities.
968 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000969 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000970 std::swap(TrueMBB, FalseMBB);
971 Predicate = CmpInst::getInversePredicate(Predicate);
972 }
973
Chris Lattner871d2462008-10-15 03:58:05 +0000974 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
975 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
976
Dan Gohmand98d6202008-10-02 22:15:21 +0000977 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000978 case CmpInst::FCMP_OEQ:
979 std::swap(TrueMBB, FalseMBB);
980 Predicate = CmpInst::FCMP_UNE;
981 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000982 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
983 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
984 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
985 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
986 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
987 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
988 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
989 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
990 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
991 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
992 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
993 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
994 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000995
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000996 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
997 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
998 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
999 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1000 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1001 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1002 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1003 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1004 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1005 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001006 default:
1007 return false;
1008 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001009
Dan Gohman46510a72010-04-15 01:51:59 +00001010 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001011 if (SwapArgs)
1012 std::swap(Op0, Op1);
1013
Chris Lattner9a08a612008-10-15 04:26:38 +00001014 // Emit a compare of the LHS and RHS, setting the flags.
1015 if (!X86FastEmitCompare(Op0, Op1, VT))
1016 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001017
Dan Gohman84023e02010-07-10 09:00:22 +00001018 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1019 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001020
1021 if (Predicate == CmpInst::FCMP_UNE) {
1022 // X86 requires a second branch to handle UNE (and OEQ,
1023 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001024 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1025 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001026 }
1027
Stuart Hastings3bf91252010-06-17 22:43:56 +00001028 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001029 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001030 return true;
1031 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001032 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1033 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1034 // typically happen for _Bool and C++ bools.
1035 MVT SourceVT;
1036 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1037 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1038 unsigned TestOpc = 0;
1039 switch (SourceVT.SimpleTy) {
1040 default: break;
1041 case MVT::i8: TestOpc = X86::TEST8ri; break;
1042 case MVT::i16: TestOpc = X86::TEST16ri; break;
1043 case MVT::i32: TestOpc = X86::TEST32ri; break;
1044 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1045 }
1046 if (TestOpc) {
1047 unsigned OpReg = getRegForValue(TI->getOperand(0));
1048 if (OpReg == 0) return false;
1049 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1050 .addReg(OpReg).addImm(1);
Chris Lattnerc76d1212011-04-19 04:26:32 +00001051
1052 unsigned JmpOpc = X86::JNE_4;
1053 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1054 std::swap(TrueMBB, FalseMBB);
1055 JmpOpc = X86::JE_4;
1056 }
1057
1058 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001059 .addMBB(TrueMBB);
1060 FastEmitBranch(FalseMBB, DL);
1061 FuncInfo.MBB->addSuccessor(TrueMBB);
1062 return true;
1063 }
1064 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001065 }
1066
1067 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001068 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1069 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001070 unsigned OpReg = getRegForValue(BI->getCondition());
1071 if (OpReg == 0) return false;
1072
Eli Friedman547eb4f2011-04-27 01:34:27 +00001073 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1074 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1076 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001077 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001078 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001079 return true;
1080}
1081
Dan Gohman46510a72010-04-15 01:51:59 +00001082bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001083 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001084 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001085 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001086 CReg = X86::CL;
1087 RC = &X86::GR8RegClass;
1088 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001089 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1090 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1091 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001092 default: return false;
1093 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001094 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001095 CReg = X86::CX;
1096 RC = &X86::GR16RegClass;
1097 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001098 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1099 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1100 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001101 default: return false;
1102 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001103 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001104 CReg = X86::ECX;
1105 RC = &X86::GR32RegClass;
1106 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001107 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1108 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1109 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001110 default: return false;
1111 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001112 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001113 CReg = X86::RCX;
1114 RC = &X86::GR64RegClass;
1115 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001116 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1117 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1118 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001119 default: return false;
1120 }
1121 } else {
1122 return false;
1123 }
1124
Duncan Sands1440e8b2010-11-03 11:35:31 +00001125 MVT VT;
1126 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001127 return false;
1128
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001129 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1130 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001131
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001132 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1133 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001134 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1135 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001136
1137 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001138 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001139 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001140 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1141 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001142 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001143
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001144 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001145 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1146 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001147 UpdateValueMap(I, ResultReg);
1148 return true;
1149}
1150
Dan Gohman46510a72010-04-15 01:51:59 +00001151bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001152 MVT VT;
1153 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001154 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001155
Eric Christophere487b012010-09-29 23:00:29 +00001156 // We only use cmov here, if we don't have a cmov instruction bail.
1157 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001158
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001159 unsigned Opc = 0;
1160 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001161 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001162 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001163 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001164 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001165 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001166 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001167 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001168 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001169 RC = &X86::GR64RegClass;
1170 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001171 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001172 }
1173
1174 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1175 if (Op0Reg == 0) return false;
1176 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1177 if (Op1Reg == 0) return false;
1178 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1179 if (Op2Reg == 0) return false;
1180
Dan Gohman84023e02010-07-10 09:00:22 +00001181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1182 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001183 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001184 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1185 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001186 UpdateValueMap(I, ResultReg);
1187 return true;
1188}
1189
Dan Gohman46510a72010-04-15 01:51:59 +00001190bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001191 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001192 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001193 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001194 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001195 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001196 unsigned OpReg = getRegForValue(V);
1197 if (OpReg == 0) return false;
1198 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001199 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1200 TII.get(X86::CVTSS2SDrr), ResultReg)
1201 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001202 UpdateValueMap(I, ResultReg);
1203 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001204 }
1205 }
1206
1207 return false;
1208}
1209
Dan Gohman46510a72010-04-15 01:51:59 +00001210bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001211 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001212 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001213 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001214 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001215 unsigned OpReg = getRegForValue(V);
1216 if (OpReg == 0) return false;
1217 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001218 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1219 TII.get(X86::CVTSD2SSrr), ResultReg)
1220 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001221 UpdateValueMap(I, ResultReg);
1222 return true;
1223 }
1224 }
1225 }
1226
1227 return false;
1228}
1229
Dan Gohman46510a72010-04-15 01:51:59 +00001230bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001231 if (Subtarget->is64Bit())
1232 // All other cases should be handled by the tblgen generated code.
1233 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001234 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1235 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001236
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001237 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001238 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001239 // All other cases should be handled by the tblgen generated code.
1240 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001241 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001242 // All other cases should be handled by the tblgen generated code.
1243 return false;
1244
1245 unsigned InputReg = getRegForValue(I->getOperand(0));
1246 if (!InputReg)
1247 // Unhandled operand. Halt "fast" selection and bail.
1248 return false;
1249
Dan Gohman62417622009-04-27 16:33:14 +00001250 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001252 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001253 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1255 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001256
1257 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001258 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001259 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001260 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001261 if (!ResultReg)
1262 return false;
1263
1264 UpdateValueMap(I, ResultReg);
1265 return true;
1266}
1267
Eli Friedmand5089a92011-04-27 01:45:07 +00001268bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1269 X86AddressMode SrcAM, uint64_t Len) {
1270 // Make sure we don't bloat code by inlining very large memcpy's.
1271 bool i64Legal = TLI.isTypeLegal(MVT::i64);
1272 if (Len > (i64Legal ? 32 : 16)) return false;
1273
1274 // We don't care about alignment here since we just emit integer accesses.
1275 while (Len) {
1276 MVT VT;
1277 if (Len >= 8 && i64Legal)
1278 VT = MVT::i64;
1279 else if (Len >= 4)
1280 VT = MVT::i32;
1281 else if (Len >= 2)
1282 VT = MVT::i16;
1283 else {
1284 assert(Len == 1);
1285 VT = MVT::i8;
1286 }
1287
1288 unsigned Reg;
1289 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1290 RV &= X86FastEmitStore(VT, Reg, DestAM);
1291 assert(RV && "Failed to emit load or store??");
1292
1293 unsigned Size = VT.getSizeInBits()/8;
1294 Len -= Size;
1295 DestAM.Disp += Size;
1296 SrcAM.Disp += Size;
1297 }
1298
1299 return true;
1300}
1301
Dan Gohman46510a72010-04-15 01:51:59 +00001302bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001303 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001304 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001305 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001306 case Intrinsic::memcpy: {
1307 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1308 // Don't handle volatile or variable length memcpys.
1309 if (MCI.isVolatile() || !isa<ConstantInt>(MCI.getLength()))
1310 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001311
Chris Lattner832e4942011-04-19 05:52:03 +00001312 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
Chris Lattner832e4942011-04-19 05:52:03 +00001313
1314 // Get the address of the dest and source addresses.
1315 X86AddressMode DestAM, SrcAM;
1316 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1317 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1318 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001319
1320 return TryEmitSmallMemcpy(DestAM, SrcAM, Len);
Chris Lattner832e4942011-04-19 05:52:03 +00001321 }
1322
Eric Christopher07754c22010-03-18 20:27:26 +00001323 case Intrinsic::stackprotector: {
1324 // Emit code inline code to store the stack guard onto the stack.
1325 EVT PtrTy = TLI.getPointerTy();
1326
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001327 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1328 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001329
1330 // Grab the frame index.
1331 X86AddressMode AM;
1332 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001333 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001334 return true;
1335 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001336 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001337 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001338 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001339 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001340 if (!X86SelectAddress(DI->getAddress(), AM))
1341 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001342 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001343 // FIXME may need to add RegState::Debug to any registers produced,
1344 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001345 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1346 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001347 return true;
1348 }
Eric Christopher77f79892010-01-18 22:11:29 +00001349 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001350 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001351 return true;
1352 }
Bill Wendling52370a12008-12-09 02:42:50 +00001353 case Intrinsic::sadd_with_overflow:
1354 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001355 // FIXME: Should fold immediates.
1356
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001357 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001358 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001359 const Function *Callee = I.getCalledFunction();
1360 const Type *RetTy =
1361 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1362
Duncan Sands1440e8b2010-11-03 11:35:31 +00001363 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001364 if (!isTypeLegal(RetTy, VT))
1365 return false;
1366
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001367 const Value *Op1 = I.getArgOperand(0);
1368 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001369 unsigned Reg1 = getRegForValue(Op1);
1370 unsigned Reg2 = getRegForValue(Op2);
1371
1372 if (Reg1 == 0 || Reg2 == 0)
1373 // FIXME: Handle values *not* in registers.
1374 return false;
1375
1376 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001377 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001378 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001379 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001380 OpC = X86::ADD64rr;
1381 else
1382 return false;
1383
Eli Friedman482feb32011-05-16 21:06:17 +00001384 // The call to CreateRegs builds two sequential registers, to store the
1385 // both the the returned values.
1386 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001387 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1388 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001389
Chris Lattnera9a42252009-04-12 07:36:01 +00001390 unsigned Opc = X86::SETBr;
1391 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1392 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001393 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1394
1395 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001396 return true;
1397 }
1398 }
1399}
1400
Dan Gohman46510a72010-04-15 01:51:59 +00001401bool X86FastISel::X86SelectCall(const Instruction *I) {
1402 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001403 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001404
1405 // Can't handle inline asm yet.
1406 if (isa<InlineAsm>(Callee))
1407 return false;
1408
Bill Wendling52370a12008-12-09 02:42:50 +00001409 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001410 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001411 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001412
Evan Chengf3d4efe2008-09-07 09:09:33 +00001413 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001414 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001415 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001416 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001417 CC != CallingConv::X86_FastCall)
1418 return false;
1419
Evan Cheng381993f2010-01-27 00:00:57 +00001420 // fastcc with -tailcallopt is intended to provide a guaranteed
1421 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001422 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001423 return false;
1424
Evan Chengf3d4efe2008-09-07 09:09:33 +00001425 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1426 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001427 bool isVarArg = FTy->isVarArg();
1428
1429 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1430 // x86-32. Special handling for x86-64 is implemented.
1431 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001432 return false;
1433
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001434 // Fast-isel doesn't know about callee-pop yet.
Eli Friedman37620462011-04-19 17:22:22 +00001435 if (Subtarget->IsCalleePop(isVarArg, CC))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001436 return false;
1437
Eli Friedman19515b42011-05-17 18:29:03 +00001438 // Check whether the function can return without sret-demotion.
1439 SmallVector<ISD::OutputArg, 4> Outs;
1440 SmallVector<uint64_t, 4> Offsets;
1441 GetReturnInfo(I->getType(), CS.getAttributes().getRetAttributes(),
1442 Outs, TLI, &Offsets);
1443 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
1444 FTy->isVarArg(), Outs, FTy->getContext());
1445 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001446 return false;
1447
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001448 // Materialize callee address in a register. FIXME: GV address can be
1449 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001450 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001451 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001452 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001453 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001454 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001455 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001456 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001457 } else if (CalleeAM.Base.Reg != 0) {
1458 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001459 } else
1460 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001461
Evan Chengf3d4efe2008-09-07 09:09:33 +00001462 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001463 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001464 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001465 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001466 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001467 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001468 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001469 ArgVTs.reserve(CS.arg_size());
1470 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001471 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001472 i != e; ++i) {
Chris Lattnere03b8d32011-04-19 04:42:38 +00001473 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001474 ISD::ArgFlagsTy Flags;
1475 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001476 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001477 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001478 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001479 Flags.setZExt();
1480
Chris Lattnere03b8d32011-04-19 04:42:38 +00001481 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1482 // instruction. This is safe because it is common to all fastisel supported
1483 // calling conventions on x86.
1484 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1485 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1486 CI->getBitWidth() == 16) {
1487 if (Flags.isSExt())
1488 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1489 else
1490 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1491 }
1492 }
1493
Chris Lattnerb44101c2011-04-19 05:09:50 +00001494 unsigned ArgReg;
Chris Lattnerff009ad2011-04-19 05:15:59 +00001495
1496 // Passing bools around ends up doing a trunc to i1 and passing it.
1497 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001498 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1499 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1500 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001501 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1502 ArgReg = getRegForValue(ArgVal);
1503 if (ArgReg == 0) return false;
1504
1505 MVT ArgVT;
1506 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
1507
1508 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1509 ArgVal->hasOneUse(), 1);
1510 } else {
1511 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001512 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001513
Chris Lattnerff009ad2011-04-19 05:15:59 +00001514 if (ArgReg == 0) return false;
1515
Evan Chengf3d4efe2008-09-07 09:09:33 +00001516 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001517 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
Devang Patel05988662008-09-25 21:00:45 +00001518 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1519 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001520 return false;
1521
Chris Lattnere03b8d32011-04-19 04:42:38 +00001522 const Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001523 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001524 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001525 return false;
1526 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1527 Flags.setOrigAlign(OriginalAlignment);
1528
Chris Lattnerb44101c2011-04-19 05:09:50 +00001529 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001530 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001531 ArgVTs.push_back(ArgVT);
1532 ArgFlags.push_back(Flags);
1533 }
1534
1535 // Analyze operands of the call, assigning locations to each operand.
1536 SmallVector<CCValAssign, 16> ArgLocs;
Eli Friedman37620462011-04-19 17:22:22 +00001537 CCState CCInfo(CC, isVarArg, TM, ArgLocs, I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001538
Dan Gohmand8acddd2010-06-01 21:09:47 +00001539 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001540 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001541 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001542
Duncan Sands45907662010-10-31 13:21:44 +00001543 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001544
1545 // Get a count of how many bytes are to be pushed on the stack.
1546 unsigned NumBytes = CCInfo.getNextStackOffset();
1547
1548 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001549 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001550 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1551 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001552
Chris Lattner438949a2008-10-15 05:30:52 +00001553 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001554 // copies / loads.
1555 SmallVector<unsigned, 4> RegArgs;
1556 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1557 CCValAssign &VA = ArgLocs[i];
1558 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001559 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001560
Evan Chengf3d4efe2008-09-07 09:09:33 +00001561 // Promote the value if needed.
1562 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001563 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001564 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001565 case CCValAssign::SExt: {
1566 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1567 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001568 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001569 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001570 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001571 }
1572 case CCValAssign::ZExt: {
1573 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1574 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001575 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001576 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001577 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001578 }
1579 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001580 // We don't handle MMX parameters yet.
1581 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1582 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001583 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1584 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001585 if (!Emitted)
1586 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001587 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001588 if (!Emitted)
1589 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1590 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001591
Chris Lattnerc46ec642011-01-05 22:26:52 +00001592 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001593 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001594 break;
1595 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001596 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001597 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001598 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001599 assert(BC != 0 && "Failed to emit a bitcast!");
1600 Arg = BC;
1601 ArgVT = VA.getLocVT();
1602 break;
1603 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001604 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001605
Evan Chengf3d4efe2008-09-07 09:09:33 +00001606 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001607 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1608 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001609 RegArgs.push_back(VA.getLocReg());
1610 } else {
1611 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001612 X86AddressMode AM;
1613 AM.Base.Reg = StackPtr;
1614 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001615 const Value *ArgVal = ArgVals[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001616
Chris Lattner241ab472008-10-15 05:38:32 +00001617 // If this is a really simple value, emit this with the Value* version of
1618 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1619 // can cause us to reevaluate the argument.
1620 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1621 X86FastEmitStore(ArgVT, ArgVal, AM);
1622 else
1623 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001624 }
1625 }
1626
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001627 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001628 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001629 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001630 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001631 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1632 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001633 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001634
Eli Friedman37620462011-04-19 17:22:22 +00001635 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1636 // Count the number of XMM registers allocated.
1637 static const unsigned XMMArgRegs[] = {
1638 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1639 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1640 };
1641 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1642 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1643 X86::AL).addImm(NumXMMRegs);
1644 }
1645
Evan Chengf3d4efe2008-09-07 09:09:33 +00001646 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001647 MachineInstrBuilder MIB;
1648 if (CalleeOp) {
1649 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001650 unsigned CallOpc;
1651 if (Subtarget->isTargetWin64())
1652 CallOpc = X86::WINCALL64r;
1653 else if (Subtarget->is64Bit())
1654 CallOpc = X86::CALL64r;
1655 else
1656 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001657 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1658 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001659
Chris Lattner51e8eab2009-07-09 06:34:26 +00001660 } else {
1661 // Direct call.
1662 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001663 unsigned CallOpc;
1664 if (Subtarget->isTargetWin64())
1665 CallOpc = X86::WINCALL64pcrel32;
1666 else if (Subtarget->is64Bit())
1667 CallOpc = X86::CALL64pcrel32;
1668 else
1669 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001670
Chris Lattner51e8eab2009-07-09 06:34:26 +00001671 // See if we need any target-specific flags on the GV operand.
1672 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001673
Chris Lattner51e8eab2009-07-09 06:34:26 +00001674 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1675 // external symbols most go through the PLT in PIC mode. If the symbol
1676 // has hidden or protected visibility, or if it is static or local, then
1677 // we don't need to use the PLT - we can directly call it.
1678 if (Subtarget->isTargetELF() &&
1679 TM.getRelocationModel() == Reloc::PIC_ &&
1680 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1681 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001682 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001683 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001684 (!Subtarget->getTargetTriple().isMacOSX() ||
1685 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001686 // PC-relative references to external symbols should go through $stub,
1687 // unless we're building with the leopard linker or later, which
1688 // automatically synthesizes these stubs.
1689 OpFlags = X86II::MO_DARWIN_STUB;
1690 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001691
1692
Dan Gohman84023e02010-07-10 09:00:22 +00001693 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1694 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001695 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001696
1697 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001698 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001699 MIB.addReg(X86::EBX);
1700
Eli Friedman37620462011-04-19 17:22:22 +00001701 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1702 MIB.addReg(X86::AL);
1703
Evan Chengf3d4efe2008-09-07 09:09:33 +00001704 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001705 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1706 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001707
1708 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001709 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eli Friedmand227eed2011-04-28 20:19:12 +00001710 unsigned NumBytesCallee = 0;
1711 if (!Subtarget->is64Bit() && CS.paramHasAttr(1, Attribute::StructRet))
1712 NumBytesCallee = 4;
Dan Gohman84023e02010-07-10 09:00:22 +00001713 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001714 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001715
Eli Friedman19515b42011-05-17 18:29:03 +00001716 // Build info for return calling conv lowering code.
1717 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1718 SmallVector<ISD::InputArg, 32> Ins;
1719 SmallVector<EVT, 4> RetTys;
1720 ComputeValueVTs(TLI, I->getType(), RetTys);
1721 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1722 EVT VT = RetTys[i];
1723 EVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
1724 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1725 for (unsigned j = 0; j != NumRegs; ++j) {
1726 ISD::InputArg MyFlags;
1727 MyFlags.VT = RegisterVT.getSimpleVT();
1728 MyFlags.Used = !CS.getInstruction()->use_empty();
1729 if (CS.paramHasAttr(0, Attribute::SExt))
1730 MyFlags.Flags.setSExt();
1731 if (CS.paramHasAttr(0, Attribute::ZExt))
1732 MyFlags.Flags.setZExt();
1733 if (CS.paramHasAttr(0, Attribute::InReg))
1734 MyFlags.Flags.setInReg();
1735 Ins.push_back(MyFlags);
1736 }
1737 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001738
Eli Friedman19515b42011-05-17 18:29:03 +00001739 // Now handle call return values.
1740 SmallVector<unsigned, 4> UsedRegs;
1741 SmallVector<CCValAssign, 16> RVLocs;
1742 CCState CCRetInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
1743 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1744 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1745 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1746 EVT CopyVT = RVLocs[i].getValVT();
1747 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001748
Evan Chengf3d4efe2008-09-07 09:09:33 +00001749 // If this is a call to a function that returns an fp value on the x87 fp
1750 // stack, but where we prefer to use the value in xmm registers, copy it
1751 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00001752 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1753 RVLocs[i].getLocReg() == X86::ST1) &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001754 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001755 CopyVT = MVT::f80;
Eli Friedman19515b42011-05-17 18:29:03 +00001756 CopyReg = createResultReg(X86::RFP80RegisterClass);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001757 }
1758
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001759 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eli Friedman19515b42011-05-17 18:29:03 +00001760 CopyReg).addReg(RVLocs[i].getLocReg());
1761 UsedRegs.push_back(RVLocs[i].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001762
Eli Friedman19515b42011-05-17 18:29:03 +00001763 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001764 // Round the F80 the right size, which also moves to the appropriate xmm
1765 // register. This is accomplished by storing the F80 value in memory and
1766 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00001767 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001769 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001770 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001771 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1772 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00001773 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001774 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00001775 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00001776 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001777 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001778 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00001779
Eli Friedman19515b42011-05-17 18:29:03 +00001780 if (RVLocs.size())
1781 UpdateValueMap(I, ResultReg, RVLocs.size());
1782
Dan Gohmandb497122010-06-18 23:28:01 +00001783 // Set all unused physreg defs as dead.
1784 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1785
Evan Chengf3d4efe2008-09-07 09:09:33 +00001786 return true;
1787}
1788
1789
Dan Gohman99b21822008-08-28 23:21:34 +00001790bool
Dan Gohman46510a72010-04-15 01:51:59 +00001791X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001792 switch (I->getOpcode()) {
1793 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001794 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001795 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001796 case Instruction::Store:
1797 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001798 case Instruction::Ret:
1799 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001800 case Instruction::ICmp:
1801 case Instruction::FCmp:
1802 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001803 case Instruction::ZExt:
1804 return X86SelectZExt(I);
1805 case Instruction::Br:
1806 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001807 case Instruction::Call:
1808 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001809 case Instruction::LShr:
1810 case Instruction::AShr:
1811 case Instruction::Shl:
1812 return X86SelectShift(I);
1813 case Instruction::Select:
1814 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001815 case Instruction::Trunc:
1816 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001817 case Instruction::FPExt:
1818 return X86SelectFPExt(I);
1819 case Instruction::FPTrunc:
1820 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001821 case Instruction::IntToPtr: // Deliberate fall-through.
1822 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001823 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1824 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001825 if (DstVT.bitsGT(SrcVT))
1826 return X86SelectZExt(I);
1827 if (DstVT.bitsLT(SrcVT))
1828 return X86SelectTrunc(I);
1829 unsigned Reg = getRegForValue(I->getOperand(0));
1830 if (Reg == 0) return false;
1831 UpdateValueMap(I, Reg);
1832 return true;
1833 }
Dan Gohman99b21822008-08-28 23:21:34 +00001834 }
1835
1836 return false;
1837}
1838
Dan Gohman46510a72010-04-15 01:51:59 +00001839unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001840 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001841 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001842 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001843
Owen Anderson95267a12008-09-05 00:06:23 +00001844 // Get opcode and regclass of the output for the given load instruction.
1845 unsigned Opc = 0;
1846 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001847 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001848 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001849 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001850 Opc = X86::MOV8rm;
1851 RC = X86::GR8RegisterClass;
1852 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001854 Opc = X86::MOV16rm;
1855 RC = X86::GR16RegisterClass;
1856 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001858 Opc = X86::MOV32rm;
1859 RC = X86::GR32RegisterClass;
1860 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001861 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001862 // Must be in x86-64 mode.
1863 Opc = X86::MOV64rm;
1864 RC = X86::GR64RegisterClass;
1865 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001867 if (Subtarget->hasSSE1()) {
1868 Opc = X86::MOVSSrm;
1869 RC = X86::FR32RegisterClass;
1870 } else {
1871 Opc = X86::LD_Fp32m;
1872 RC = X86::RFP32RegisterClass;
1873 }
1874 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001875 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001876 if (Subtarget->hasSSE2()) {
1877 Opc = X86::MOVSDrm;
1878 RC = X86::FR64RegisterClass;
1879 } else {
1880 Opc = X86::LD_Fp64m;
1881 RC = X86::RFP64RegisterClass;
1882 }
1883 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001884 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001885 // No f80 support yet.
1886 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001887 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001888
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001889 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001890 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001891 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001892 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00001893 // If the expression is just a basereg, then we're done, otherwise we need
1894 // to emit an LEA.
1895 if (AM.BaseType == X86AddressMode::RegBase &&
1896 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
1897 return AM.Base.Reg;
1898
1899 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001900 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001901 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1902 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001903 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001904 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001905 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001906 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001907
Owen Anderson3b217c62008-09-06 01:11:01 +00001908 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00001909 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001910 if (Align == 0) {
1911 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00001912 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001913 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001914
Dan Gohman5396c992008-09-30 01:21:32 +00001915 // x86-32 PIC requires a PIC base register for constant pools.
1916 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00001917 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00001918 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00001919 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00001920 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001921 } else if (Subtarget->isPICStyleGOT()) {
1922 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00001923 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00001924 } else if (Subtarget->isPICStyleRIPRel() &&
1925 TM.getCodeModel() == CodeModel::Small) {
1926 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00001927 }
Dan Gohman5396c992008-09-30 01:21:32 +00001928
1929 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001930 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001931 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001932 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1933 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00001934 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00001935
Owen Anderson95267a12008-09-05 00:06:23 +00001936 return ResultReg;
1937}
1938
Dan Gohman46510a72010-04-15 01:51:59 +00001939unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001940 // Fail on dynamic allocas. At this point, getRegForValue has already
1941 // checked its CSE maps, so if we're here trying to handle a dynamic
1942 // alloca, we're not going to succeed. X86SelectAddress has a
1943 // check for dynamic allocas, because it's called directly from
1944 // various places, but TargetMaterializeAlloca also needs a check
1945 // in order to avoid recursion between getRegForValue,
1946 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00001947 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001948 return 0;
1949
Dan Gohman0586d912008-09-10 20:11:02 +00001950 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001951 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00001952 return 0;
1953 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1954 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1955 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001956 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1957 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00001958 return ResultReg;
1959}
1960
Eli Friedman2790ba82011-04-27 22:41:55 +00001961unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
1962 MVT VT;
1963 if (!isTypeLegal(CF->getType(), VT))
1964 return false;
1965
1966 // Get opcode and regclass for the given zero.
1967 unsigned Opc = 0;
1968 const TargetRegisterClass *RC = NULL;
1969 switch (VT.SimpleTy) {
1970 default: return false;
1971 case MVT::f32:
1972 if (Subtarget->hasSSE1()) {
1973 Opc = X86::FsFLD0SS;
1974 RC = X86::FR32RegisterClass;
1975 } else {
1976 Opc = X86::LD_Fp032;
1977 RC = X86::RFP32RegisterClass;
1978 }
1979 break;
1980 case MVT::f64:
1981 if (Subtarget->hasSSE2()) {
1982 Opc = X86::FsFLD0SD;
1983 RC = X86::FR64RegisterClass;
1984 } else {
1985 Opc = X86::LD_Fp064;
1986 RC = X86::RFP64RegisterClass;
1987 }
1988 break;
1989 case MVT::f80:
1990 // No f80 support yet.
1991 return false;
1992 }
1993
1994 unsigned ResultReg = createResultReg(RC);
1995 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
1996 return ResultReg;
1997}
1998
1999
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002000/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2001/// vreg is being provided by the specified load instruction. If possible,
2002/// try to fold the load as an operand to the instruction, returning true if
2003/// possible.
2004bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2005 const LoadInst *LI) {
2006 X86AddressMode AM;
2007 if (!X86SelectAddress(LI->getOperand(0), AM))
2008 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002009
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002010 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002011
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002012 unsigned Size = TD.getTypeAllocSize(LI->getType());
2013 unsigned Alignment = LI->getAlignment();
2014
2015 SmallVector<MachineOperand, 8> AddrOps;
2016 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002017
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002018 MachineInstr *Result =
2019 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2020 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002021
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002022 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002023 MI->eraseFromParent();
2024 return true;
2025}
2026
2027
Evan Chengc3f44b02008-09-03 00:03:49 +00002028namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00002029 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2030 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002031 }
Dan Gohman99b21822008-08-28 23:21:34 +00002032}