blob: 7f230ff571845fa1c24b3a4870680e2928d5969f [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 Chengef41ff62011-06-23 17:54:54 +000017#include "X86ISelLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "X86InstrBuilder.h"
Evan Cheng88e30412008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Dan Gohman84023e02010-07-10 09:00:22 +000022#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000023#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000024#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/GlobalAlias.h"
31#include "llvm/IR/GlobalVariable.h"
32#include "llvm/IR/Instructions.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/Operator.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000035#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000037#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000038#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000039using namespace llvm;
40
Chris Lattner087fcf32009-03-08 18:44:31 +000041namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000042
Evan Chengc3f44b02008-09-03 00:03:49 +000043class X86FastISel : public FastISel {
44 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
45 /// make the right decision when generating code for different targets.
46 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000047
Michael Liaof0e06e82012-11-01 03:47:50 +000048 /// RegInfo - X86 register info.
Evan Chengf3d4efe2008-09-07 09:09:33 +000049 ///
Michael Liaof0e06e82012-11-01 03:47:50 +000050 const X86RegisterInfo *RegInfo;
Evan Chengf3d4efe2008-09-07 09:09:33 +000051
Wesley Peckbf17cfa2010-11-23 03:31:01 +000052 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000053 /// floating point ops.
54 /// When SSE is available, use it for f32 operations.
55 /// When SSE2 is available, use it for f64 operations.
56 bool X86ScalarSSEf64;
57 bool X86ScalarSSEf32;
58
Evan Cheng8b19e562008-09-03 06:44:39 +000059public:
Bob Wilsond49edb72012-08-03 04:06:28 +000060 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
61 const TargetLibraryInfo *libInfo)
62 : FastISel(funcInfo, libInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000063 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +000064 X86ScalarSSEf64 = Subtarget->hasSSE2();
65 X86ScalarSSEf32 = Subtarget->hasSSE1();
Michael Liaof0e06e82012-11-01 03:47:50 +000066 RegInfo = static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Evan Cheng88e30412008-09-03 01:04:47 +000067 }
Evan Chengc3f44b02008-09-03 00:03:49 +000068
Dan Gohman46510a72010-04-15 01:51:59 +000069 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000070
Chris Lattnerbeac75d2010-09-05 02:18:34 +000071 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
72 /// vreg is being provided by the specified load instruction. If possible,
73 /// try to fold the load as an operand to the instruction, returning true if
74 /// possible.
75 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
76 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000077
Dan Gohman1adf1b02008-08-19 21:45:35 +000078#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000079
80private:
Dan Gohman46510a72010-04-15 01:51:59 +000081 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000082
Owen Andersone50ed302009-08-10 22:56:29 +000083 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000084
Chris Lattnerb44101c2011-04-19 05:09:50 +000085 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
86 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000087
Owen Andersone50ed302009-08-10 22:56:29 +000088 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000089 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000090
Dan Gohman46510a72010-04-15 01:51:59 +000091 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
92 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000093
Dan Gohman46510a72010-04-15 01:51:59 +000094 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000095
Dan Gohman46510a72010-04-15 01:51:59 +000096 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000097
Dan Gohman84023e02010-07-10 09:00:22 +000098 bool X86SelectRet(const Instruction *I);
99
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000105
Dan Gohman46510a72010-04-15 01:51:59 +0000106 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000107
Dan Gohman46510a72010-04-15 01:51:59 +0000108 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000109
Dan Gohman46510a72010-04-15 01:51:59 +0000110 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000111
Dan Gohman46510a72010-04-15 01:51:59 +0000112 bool X86SelectFPExt(const Instruction *I);
113 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000114
Dan Gohman46510a72010-04-15 01:51:59 +0000115 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
116 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000117
Eli Friedman25255cb2011-06-10 23:39:36 +0000118 bool DoSelectCall(const Instruction *I, const char *MemIntName);
119
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000120 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000121 return getTargetMachine()->getInstrInfo();
122 }
123 const X86TargetMachine *getTargetMachine() const {
124 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000125 }
126
Dan Gohman46510a72010-04-15 01:51:59 +0000127 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000128
Dan Gohman46510a72010-04-15 01:51:59 +0000129 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000130
Eli Friedman2790ba82011-04-27 22:41:55 +0000131 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
132
Evan Chengf3d4efe2008-09-07 09:09:33 +0000133 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
134 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000135 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
137 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000138 }
139
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000140 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000141
Eli Friedmanc0883452011-05-20 22:21:04 +0000142 bool IsMemcpySmall(uint64_t Len);
143
Eli Friedmand5089a92011-04-27 01:45:07 +0000144 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
145 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000146};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000147
Chris Lattner087fcf32009-03-08 18:44:31 +0000148} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000149
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000150bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000151 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
152 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000153 // Unhandled type. Halt "fast" selection and bail.
154 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000155
156 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000157 // For now, require SSE/SSE2 for performing floating-point operations,
158 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topperf4cfc442012-08-11 17:53:00 +0000160 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topperf4cfc442012-08-11 17:53:00 +0000162 return false;
Dan Gohman9b66d732008-09-30 00:48:39 +0000163 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000165 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000166 // We only handle legal types. For example, on x86-32 the instruction
167 // selector contains all of the 64-bit instructions from x86-64,
168 // under the assumption that i64 won't be used if the target doesn't
169 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000171}
172
173#include "X86GenCallingConv.inc"
174
Evan Cheng0de588f2008-09-05 21:00:03 +0000175/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000176/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000177/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000178bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000179 unsigned &ResultReg) {
180 // Get opcode and regclass of the output for the given load instruction.
181 unsigned Opc = 0;
182 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000184 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000185 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000187 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +0000188 RC = &X86::GR8RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000189 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000191 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +0000192 RC = &X86::GR16RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000195 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000196 RC = &X86::GR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000199 // Must be in x86-64 mode.
200 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000201 RC = &X86::GR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000202 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000204 if (X86ScalarSSEf32) {
205 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +0000206 RC = &X86::FR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000207 } else {
208 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +0000209 RC = &X86::RFP32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000210 }
211 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000213 if (X86ScalarSSEf64) {
214 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +0000215 RC = &X86::FR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000216 } else {
217 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +0000218 RC = &X86::RFP64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000219 }
220 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000222 // No f80 support yet.
223 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000224 }
225
226 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000227 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
228 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000229 return true;
230}
231
Evan Chengf3d4efe2008-09-07 09:09:33 +0000232/// X86FastEmitStore - Emit a machine instruction to store a value Val of
233/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
234/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000235/// i.e. V. Return true if it is possible.
236bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000237X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000238 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000239 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 switch (VT.getSimpleVT().SimpleTy) {
241 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000242 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000243 case MVT::i1: {
244 // Mask out all but lowest bit.
Craig Topperc9099502012-04-20 06:31:50 +0000245 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000247 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
248 Val = AndResult;
249 }
250 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 case MVT::i8: Opc = X86::MOV8mr; break;
252 case MVT::i16: Opc = X86::MOV16mr; break;
253 case MVT::i32: Opc = X86::MOV32mr; break;
254 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
255 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000256 Opc = X86ScalarSSEf32 ?
257 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000258 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000260 Opc = X86ScalarSSEf64 ?
261 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000262 break;
Lang Hamese4824712011-10-18 22:11:33 +0000263 case MVT::v4f32:
264 Opc = X86::MOVAPSmr;
265 break;
266 case MVT::v2f64:
267 Opc = X86::MOVAPDmr;
268 break;
269 case MVT::v4i32:
270 case MVT::v2i64:
271 case MVT::v8i16:
272 case MVT::v16i8:
273 Opc = X86::MOVDQAmr;
274 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000275 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000276
Dan Gohman84023e02010-07-10 09:00:22 +0000277 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
278 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000279 return true;
280}
281
Dan Gohman46510a72010-04-15 01:51:59 +0000282bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000283 const X86AddressMode &AM) {
284 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000285 if (isa<ConstantPointerNull>(Val))
286 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000287
Chris Lattner438949a2008-10-15 05:30:52 +0000288 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000289 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000290 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000291 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000293 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000294 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 case MVT::i8: Opc = X86::MOV8mi; break;
296 case MVT::i16: Opc = X86::MOV16mi; break;
297 case MVT::i32: Opc = X86::MOV32mi; break;
298 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000299 // Must be a 32-bit sign extended value.
Jakub Staszakeaf77252012-11-15 19:05:23 +0000300 if (isInt<32>(CI->getSExtValue()))
Chris Lattner438949a2008-10-15 05:30:52 +0000301 Opc = X86::MOV64mi32;
302 break;
303 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000304
Chris Lattner438949a2008-10-15 05:30:52 +0000305 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000306 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
307 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000308 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000309 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000310 return true;
311 }
312 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000313
Chris Lattner438949a2008-10-15 05:30:52 +0000314 unsigned ValReg = getRegForValue(Val);
315 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000316 return false;
317
Chris Lattner438949a2008-10-15 05:30:52 +0000318 return X86FastEmitStore(VT, ValReg, AM);
319}
320
Evan Cheng24e3a902008-09-08 06:35:17 +0000321/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
322/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
323/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000324bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
325 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000326 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000327 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
328 Src, /*TODO: Kill=*/false);
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000329 if (RR == 0)
Owen Andersonac34a002008-09-11 19:44:55 +0000330 return false;
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000331
332 ResultReg = RR;
333 return true;
Evan Cheng24e3a902008-09-08 06:35:17 +0000334}
335
Dan Gohman0586d912008-09-10 20:11:02 +0000336/// X86SelectAddress - Attempt to fill in an address from the given value.
337///
Dan Gohman46510a72010-04-15 01:51:59 +0000338bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
339 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000340 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000341 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000342 // Don't walk into other basic blocks; it's possible we haven't
343 // visited them yet, so the instructions may not yet be assigned
344 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000345 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
346 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
347 Opcode = I->getOpcode();
348 U = I;
349 }
Dan Gohman46510a72010-04-15 01:51:59 +0000350 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000351 Opcode = C->getOpcode();
352 U = C;
353 }
Dan Gohman0586d912008-09-10 20:11:02 +0000354
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000355 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000356 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000357 // Fast instruction selection doesn't support the special
358 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000359 return false;
360
Dan Gohman35893082008-09-18 23:23:44 +0000361 switch (Opcode) {
362 default: break;
363 case Instruction::BitCast:
364 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000365 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000366
367 case Instruction::IntToPtr:
368 // Look past no-op inttoptrs.
369 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000370 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000371 break;
Dan Gohman35893082008-09-18 23:23:44 +0000372
373 case Instruction::PtrToInt:
374 // Look past no-op ptrtoints.
375 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000376 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000377 break;
Dan Gohman35893082008-09-18 23:23:44 +0000378
379 case Instruction::Alloca: {
380 // Do static allocas.
381 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000382 DenseMap<const AllocaInst*, int>::iterator SI =
383 FuncInfo.StaticAllocaMap.find(A);
384 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000385 AM.BaseType = X86AddressMode::FrameIndexBase;
386 AM.Base.FrameIndex = SI->second;
387 return true;
388 }
389 break;
Dan Gohman35893082008-09-18 23:23:44 +0000390 }
391
392 case Instruction::Add: {
393 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000394 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000395 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
396 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000397 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000398 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000399 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000400 }
Dan Gohman0586d912008-09-10 20:11:02 +0000401 }
Dan Gohman35893082008-09-18 23:23:44 +0000402 break;
403 }
404
405 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000406 X86AddressMode SavedAM = AM;
407
Dan Gohman35893082008-09-18 23:23:44 +0000408 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000409 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000410 unsigned IndexReg = AM.IndexReg;
411 unsigned Scale = AM.Scale;
412 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000413 // Iterate through the indices, folding what we can. Constants can be
414 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000415 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000416 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000417 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000418 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000419 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000420 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
421 continue;
422 }
Eric Christopher471e4222011-06-08 23:55:35 +0000423
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000424 // A array/variable index is always of the form i*S where S is the
425 // constant scale size. See if we can push the scale into immediates.
426 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
427 for (;;) {
428 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
429 // Constant-offset addressing.
430 Disp += CI->getSExtValue() * S;
431 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000432 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000433 if (isa<AddOperator>(Op) &&
434 (!isa<Instruction>(Op) ||
435 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
436 == FuncInfo.MBB) &&
437 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
438 // An add (in the same block) with a constant operand. Fold the
439 // constant.
440 ConstantInt *CI =
441 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
442 Disp += CI->getSExtValue() * S;
443 // Iterate on the other operand.
444 Op = cast<AddOperator>(Op)->getOperand(0);
445 continue;
446 }
447 if (IndexReg == 0 &&
448 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
449 (S == 1 || S == 2 || S == 4 || S == 8)) {
450 // Scaled-index addressing.
451 Scale = S;
452 IndexReg = getRegForGEPIndex(Op).first;
453 if (IndexReg == 0)
454 return false;
455 break;
456 }
457 // Unsupported.
458 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000459 }
460 }
Dan Gohman09aae462008-09-26 20:04:15 +0000461 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000462 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000463 break;
Dan Gohman35893082008-09-18 23:23:44 +0000464 // Ok, the GEP indices were covered by constant-offset and scaled-index
465 // addressing. Update the address state and move on to examining the base.
466 AM.IndexReg = IndexReg;
467 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000468 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000469 if (X86SelectAddress(U->getOperand(0), AM))
470 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000471
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000472 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000473 // our address and just match the value instead of completely failing.
474 AM = SavedAM;
475 break;
Dan Gohman35893082008-09-18 23:23:44 +0000476 unsupported_gep:
477 // Ok, the GEP indices weren't all covered.
478 break;
479 }
480 }
481
482 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000483 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedmana6176ad2011-09-22 23:41:28 +0000484 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000485 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000486 return false;
487
Eli Friedmana6176ad2011-09-22 23:41:28 +0000488 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000489 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000490 if (GVar->isThreadLocal())
491 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000492
Eli Friedmana6176ad2011-09-22 23:41:28 +0000493 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
494 // it works...).
495 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
496 if (const GlobalVariable *GVar =
497 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
498 if (GVar->isThreadLocal())
499 return false;
500
Chris Lattner0a1c9972011-04-17 17:47:38 +0000501 // RIP-relative addresses can't have additional register operands, so if
502 // we've already folded stuff into the addressing mode, just force the
503 // global value into its own register, which we can use as the basereg.
504 if (!Subtarget->isPICStyleRIPRel() ||
505 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
506 // Okay, we've committed to selecting this global. Set up the address.
507 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000508
Chris Lattner0a1c9972011-04-17 17:47:38 +0000509 // Allow the subtarget to classify the global.
510 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000511
Chris Lattner0a1c9972011-04-17 17:47:38 +0000512 // If this reference is relative to the pic base, set it now.
513 if (isGlobalRelativeToPICBase(GVFlags)) {
514 // FIXME: How do we know Base.Reg is free??
515 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000516 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000517
518 // Unless the ABI requires an extra load, return a direct reference to
519 // the global.
520 if (!isGlobalStubReference(GVFlags)) {
521 if (Subtarget->isPICStyleRIPRel()) {
522 // Use rip-relative addressing if we can. Above we verified that the
523 // base and index registers are unused.
524 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
525 AM.Base.Reg = X86::RIP;
526 }
527 AM.GVOpFlags = GVFlags;
528 return true;
529 }
530
531 // Ok, we need to do a load from a stub. If we've already loaded from
532 // this stub, reuse the loaded pointer, otherwise emit the load now.
533 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
534 unsigned LoadReg;
535 if (I != LocalValueMap.end() && I->second != 0) {
536 LoadReg = I->second;
537 } else {
538 // Issue load from stub.
539 unsigned Opc = 0;
540 const TargetRegisterClass *RC = NULL;
541 X86AddressMode StubAM;
542 StubAM.Base.Reg = AM.Base.Reg;
543 StubAM.GV = GV;
544 StubAM.GVOpFlags = GVFlags;
545
546 // Prepare for inserting code in the local-value area.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000547 SavePoint SaveInsertPt = enterLocalValueArea();
Chris Lattner0a1c9972011-04-17 17:47:38 +0000548
549 if (TLI.getPointerTy() == MVT::i64) {
550 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000551 RC = &X86::GR64RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000552
553 if (Subtarget->isPICStyleRIPRel())
554 StubAM.Base.Reg = X86::RIP;
555 } else {
556 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000557 RC = &X86::GR32RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000558 }
559
560 LoadReg = createResultReg(RC);
561 MachineInstrBuilder LoadMI =
562 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
563 addFullAddress(LoadMI, StubAM);
564
565 // Ok, back to normal mode.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000566 leaveLocalValueArea(SaveInsertPt);
Chris Lattner0a1c9972011-04-17 17:47:38 +0000567
568 // Prevent loading GV stub multiple times in same MBB.
569 LocalValueMap[V] = LoadReg;
570 }
571
572 // Now construct the final address. Note that the Disp, Scale,
573 // and Index values may already be set here.
574 AM.Base.Reg = LoadReg;
575 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000576 return true;
577 }
Dan Gohman0586d912008-09-10 20:11:02 +0000578 }
579
Dan Gohman97135e12008-09-26 19:15:30 +0000580 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000581 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000582 if (AM.Base.Reg == 0) {
583 AM.Base.Reg = getRegForValue(V);
584 return AM.Base.Reg != 0;
585 }
586 if (AM.IndexReg == 0) {
587 assert(AM.Scale == 1 && "Scale with no index!");
588 AM.IndexReg = getRegForValue(V);
589 return AM.IndexReg != 0;
590 }
591 }
592
593 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000594}
595
Chris Lattner0aa43de2009-07-10 05:33:42 +0000596/// X86SelectCallAddress - Attempt to fill in an address from the given value.
597///
Dan Gohman46510a72010-04-15 01:51:59 +0000598bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
599 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000600 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000601 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000602 Opcode = I->getOpcode();
603 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000604 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000605 Opcode = C->getOpcode();
606 U = C;
607 }
608
609 switch (Opcode) {
610 default: break;
611 case Instruction::BitCast:
612 // Look past bitcasts.
613 return X86SelectCallAddress(U->getOperand(0), AM);
614
615 case Instruction::IntToPtr:
616 // Look past no-op inttoptrs.
617 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
618 return X86SelectCallAddress(U->getOperand(0), AM);
619 break;
620
621 case Instruction::PtrToInt:
622 // Look past no-op ptrtoints.
623 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
624 return X86SelectCallAddress(U->getOperand(0), AM);
625 break;
626 }
627
628 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000629 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000630 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000631 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000632 return false;
633
634 // RIP-relative addresses can't have additional register operands.
635 if (Subtarget->isPICStyleRIPRel() &&
636 (AM.Base.Reg != 0 || AM.IndexReg != 0))
637 return false;
638
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000639 // Can't handle DLLImport.
640 if (GV->hasDLLImportLinkage())
641 return false;
642
643 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000644 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000645 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000646 return false;
647
648 // Okay, we've committed to selecting this global. Set up the basic address.
649 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000650
Chris Lattnere6c07b52009-07-10 05:45:15 +0000651 // No ABI requires an extra load for anything other than DLLImport, which
652 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000653 if (Subtarget->isPICStyleRIPRel()) {
654 // Use rip-relative addressing if we can. Above we verified that the
655 // base and index registers are unused.
656 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
657 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000658 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000659 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
660 } else if (Subtarget->isPICStyleGOT()) {
661 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000662 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000663
Chris Lattner0aa43de2009-07-10 05:33:42 +0000664 return true;
665 }
666
667 // If all else fails, try to materialize the value in a register.
668 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
669 if (AM.Base.Reg == 0) {
670 AM.Base.Reg = getRegForValue(V);
671 return AM.Base.Reg != 0;
672 }
673 if (AM.IndexReg == 0) {
674 assert(AM.Scale == 1 && "Scale with no index!");
675 AM.IndexReg = getRegForValue(V);
676 return AM.IndexReg != 0;
677 }
678 }
679
680 return false;
681}
682
683
Owen Andersona3971df2008-09-04 07:08:58 +0000684/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000685bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000686 // Atomic stores need special handling.
Lang Hamese4824712011-10-18 22:11:33 +0000687 const StoreInst *S = cast<StoreInst>(I);
688
689 if (S->isAtomic())
690 return false;
691
692 unsigned SABIAlignment =
693 TD.getABITypeAlignment(S->getValueOperand()->getType());
694 if (S->getAlignment() != 0 && S->getAlignment() < SABIAlignment)
Eli Friedman4136d232011-09-02 22:33:24 +0000695 return false;
696
Duncan Sands1440e8b2010-11-03 11:35:31 +0000697 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000698 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000699 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000700
Dan Gohman0586d912008-09-10 20:11:02 +0000701 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000702 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000703 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000704
Chris Lattner438949a2008-10-15 05:30:52 +0000705 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000706}
707
Dan Gohman84023e02010-07-10 09:00:22 +0000708/// X86SelectRet - Select and emit code to implement ret instructions.
709bool X86FastISel::X86SelectRet(const Instruction *I) {
710 const ReturnInst *Ret = cast<ReturnInst>(I);
711 const Function &F = *I->getParent()->getParent();
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000712 const X86MachineFunctionInfo *X86MFInfo =
713 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohman84023e02010-07-10 09:00:22 +0000714
715 if (!FuncInfo.CanLowerReturn)
716 return false;
717
718 CallingConv::ID CC = F.getCallingConv();
719 if (CC != CallingConv::C &&
720 CC != CallingConv::Fast &&
721 CC != CallingConv::X86_FastCall)
722 return false;
723
724 if (Subtarget->isTargetWin64())
725 return false;
726
727 // Don't handle popping bytes on return for now.
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000728 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000729 return 0;
730
731 // fastcc with -tailcallopt is intended to provide a guaranteed
732 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000733 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohman84023e02010-07-10 09:00:22 +0000734 return false;
735
736 // Let SDISel handle vararg functions.
737 if (F.isVarArg())
738 return false;
739
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000740 // Build a list of return value registers.
741 SmallVector<unsigned, 4> RetRegs;
742
Dan Gohman84023e02010-07-10 09:00:22 +0000743 if (Ret->getNumOperands() > 0) {
744 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +0000745 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohman84023e02010-07-10 09:00:22 +0000746
747 // Analyze operands of the call, assigning locations to each operand.
748 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000749 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +0000750 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000751 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000752
753 const Value *RV = Ret->getOperand(0);
754 unsigned Reg = getRegForValue(RV);
755 if (Reg == 0)
756 return false;
757
758 // Only handle a single return value for now.
759 if (ValLocs.size() != 1)
760 return false;
761
762 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000763
Dan Gohman84023e02010-07-10 09:00:22 +0000764 // Don't bother handling odd stuff for now.
765 if (VA.getLocInfo() != CCValAssign::Full)
766 return false;
767 // Only handle register returns for now.
768 if (!VA.isRegLoc())
769 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000770
771 // The calling-convention tables for x87 returns don't tell
772 // the whole story.
773 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
774 return false;
775
Eli Friedman22486c92011-05-18 23:13:10 +0000776 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000777 EVT SrcVT = TLI.getValueType(RV->getType());
778 EVT DstVT = VA.getValVT();
779 // Special handling for extended integers.
780 if (SrcVT != DstVT) {
781 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
782 return false;
783
784 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
785 return false;
786
787 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
788
789 if (SrcVT == MVT::i1) {
790 if (Outs[0].Flags.isSExt())
791 return false;
792 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
793 SrcVT = MVT::i8;
794 }
795 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
796 ISD::SIGN_EXTEND;
797 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
798 SrcReg, /*TODO: Kill=*/false);
799 }
800
801 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000802 unsigned DstReg = VA.getLocReg();
803 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000804 // Avoid a cross-class copy. This is very unlikely.
805 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000806 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000807 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
808 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000809
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000810 // Add register to return instruction.
811 RetRegs.push_back(VA.getLocReg());
Dan Gohman84023e02010-07-10 09:00:22 +0000812 }
813
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000814 // The x86-64 ABI for returning structs by value requires that we copy
815 // the sret argument into %rax for the return. We saved the argument into
816 // a virtual register in the entry block, so now we copy the value out
817 // and into %rax.
818 if (Subtarget->is64Bit() && F.hasStructRetAttr()) {
819 unsigned Reg = X86MFInfo->getSRetReturnReg();
820 assert(Reg &&
821 "SRetReturnReg should have been set in LowerFormalArguments()!");
822 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
823 X86::RAX).addReg(Reg);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000824 RetRegs.push_back(X86::RAX);
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000825 }
826
Dan Gohman84023e02010-07-10 09:00:22 +0000827 // Now emit the RET.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000828 MachineInstrBuilder MIB =
829 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
830 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
831 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohman84023e02010-07-10 09:00:22 +0000832 return true;
833}
834
Evan Cheng8b19e562008-09-03 06:44:39 +0000835/// X86SelectLoad - Select and emit code to implement load instructions.
836///
Dan Gohman46510a72010-04-15 01:51:59 +0000837bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000838 // Atomic loads need special handling.
839 if (cast<LoadInst>(I)->isAtomic())
840 return false;
841
Duncan Sands1440e8b2010-11-03 11:35:31 +0000842 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000843 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000844 return false;
845
Dan Gohman0586d912008-09-10 20:11:02 +0000846 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000847 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000848 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000849
Evan Cheng0de588f2008-09-05 21:00:03 +0000850 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000851 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000852 UpdateValueMap(I, ResultReg);
853 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000854 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000855 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000856}
857
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000858static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000859 bool HasAVX = Subtarget->hasAVX();
Craig Topper1accb7e2012-01-10 06:54:16 +0000860 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
861 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000862
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000864 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 case MVT::i8: return X86::CMP8rr;
866 case MVT::i16: return X86::CMP16rr;
867 case MVT::i32: return X86::CMP32rr;
868 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000869 case MVT::f32:
870 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
871 case MVT::f64:
872 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000873 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000874}
875
Chris Lattner0e13c782008-10-15 04:13:29 +0000876/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
877/// of the comparison, return an opcode that works for the compare (e.g.
878/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000879static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000881 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000882 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 case MVT::i8: return X86::CMP8ri;
884 case MVT::i16: return X86::CMP16ri;
885 case MVT::i32: return X86::CMP32ri;
886 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000887 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
888 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000889 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000890 return X86::CMP64ri32;
891 return 0;
892 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000893}
894
Dan Gohman46510a72010-04-15 01:51:59 +0000895bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
896 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000897 unsigned Op0Reg = getRegForValue(Op0);
898 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000899
Chris Lattnerd53886b2008-10-15 05:18:04 +0000900 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000901 if (isa<ConstantPointerNull>(Op1))
902 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000903
Chris Lattner9a08a612008-10-15 04:26:38 +0000904 // We have two options: compare with register or immediate. If the RHS of
905 // the compare is an immediate that we can fold into this compare, use
906 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000907 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000908 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000909 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
910 .addReg(Op0Reg)
911 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000912 return true;
913 }
914 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000915
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000916 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000917 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000918
Chris Lattner9a08a612008-10-15 04:26:38 +0000919 unsigned Op1Reg = getRegForValue(Op1);
920 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000921 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
922 .addReg(Op0Reg)
923 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000924
Chris Lattner9a08a612008-10-15 04:26:38 +0000925 return true;
926}
927
Dan Gohman46510a72010-04-15 01:51:59 +0000928bool X86FastISel::X86SelectCmp(const Instruction *I) {
929 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000930
Duncan Sands1440e8b2010-11-03 11:35:31 +0000931 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000932 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000933 return false;
934
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000935 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000936 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000937 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000938 switch (CI->getPredicate()) {
939 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000940 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
941 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000942
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000943 unsigned EReg = createResultReg(&X86::GR8RegClass);
944 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000945 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
946 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
947 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000948 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000949 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000950 UpdateValueMap(I, ResultReg);
951 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000952 }
953 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000954 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
955 return false;
956
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000957 unsigned NEReg = createResultReg(&X86::GR8RegClass);
958 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000959 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
960 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000962 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000963 UpdateValueMap(I, ResultReg);
964 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000965 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000966 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
967 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
968 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
969 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
970 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
971 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
972 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
973 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
974 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
975 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
976 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
977 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000978
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000979 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
980 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
981 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
982 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
983 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
984 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
985 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
986 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
987 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
988 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000989 default:
990 return false;
991 }
992
Dan Gohman46510a72010-04-15 01:51:59 +0000993 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000994 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000995 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000996
Chris Lattner9a08a612008-10-15 04:26:38 +0000997 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000998 if (!X86FastEmitCompare(Op0, Op1, VT))
999 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001000
Dan Gohman84023e02010-07-10 09:00:22 +00001001 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001002 UpdateValueMap(I, ResultReg);
1003 return true;
1004}
Evan Cheng8b19e562008-09-03 06:44:39 +00001005
Dan Gohman46510a72010-04-15 01:51:59 +00001006bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001007 // Handle zero-extension from i1 to i8, which is common.
Eric Christopher471e4222011-06-08 23:55:35 +00001008 if (!I->getOperand(0)->getType()->isIntegerTy(1))
Eli Friedman76927d732011-05-25 23:49:02 +00001009 return false;
1010
1011 EVT DstVT = TLI.getValueType(I->getType());
1012 if (!TLI.isTypeLegal(DstVT))
1013 return false;
1014
1015 unsigned ResultReg = getRegForValue(I->getOperand(0));
1016 if (ResultReg == 0)
1017 return false;
1018
1019 // Set the high bits to zero.
1020 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1021 if (ResultReg == 0)
1022 return false;
1023
1024 if (DstVT != MVT::i8) {
1025 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1026 ResultReg, /*Kill=*/true);
1027 if (ResultReg == 0)
1028 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +00001029 }
1030
Eli Friedman76927d732011-05-25 23:49:02 +00001031 UpdateValueMap(I, ResultReg);
1032 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +00001033}
1034
Chris Lattner9a08a612008-10-15 04:26:38 +00001035
Dan Gohman46510a72010-04-15 01:51:59 +00001036bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +00001037 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +00001038 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001039 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +00001040 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1041 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +00001042
Dan Gohman8bef7442010-08-21 02:32:36 +00001043 // Fold the common case of a conditional branch with a comparison
1044 // in the same block (values defined on other blocks may not have
1045 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +00001046 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +00001047 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001048 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001049
Dan Gohmand98d6202008-10-02 22:15:21 +00001050 // Try to take advantage of fallthrough opportunities.
1051 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001052 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001053 std::swap(TrueMBB, FalseMBB);
1054 Predicate = CmpInst::getInversePredicate(Predicate);
1055 }
1056
Chris Lattner871d2462008-10-15 03:58:05 +00001057 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1058 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1059
Dan Gohmand98d6202008-10-02 22:15:21 +00001060 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001061 case CmpInst::FCMP_OEQ:
1062 std::swap(TrueMBB, FalseMBB);
1063 Predicate = CmpInst::FCMP_UNE;
1064 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001065 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1066 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1067 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1068 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1069 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1070 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1071 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1072 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1073 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1074 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1075 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1076 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1077 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001078
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001079 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1080 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1081 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1082 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1083 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1084 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1085 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1086 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1087 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1088 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001089 default:
1090 return false;
1091 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001092
Dan Gohman46510a72010-04-15 01:51:59 +00001093 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001094 if (SwapArgs)
1095 std::swap(Op0, Op1);
1096
Chris Lattner9a08a612008-10-15 04:26:38 +00001097 // Emit a compare of the LHS and RHS, setting the flags.
1098 if (!X86FastEmitCompare(Op0, Op1, VT))
1099 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001100
Dan Gohman84023e02010-07-10 09:00:22 +00001101 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1102 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001103
1104 if (Predicate == CmpInst::FCMP_UNE) {
1105 // X86 requires a second branch to handle UNE (and OEQ,
1106 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001107 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1108 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001109 }
1110
Stuart Hastings3bf91252010-06-17 22:43:56 +00001111 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001112 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001113 return true;
1114 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001115 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1116 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1117 // typically happen for _Bool and C++ bools.
1118 MVT SourceVT;
1119 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1120 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1121 unsigned TestOpc = 0;
1122 switch (SourceVT.SimpleTy) {
1123 default: break;
1124 case MVT::i8: TestOpc = X86::TEST8ri; break;
1125 case MVT::i16: TestOpc = X86::TEST16ri; break;
1126 case MVT::i32: TestOpc = X86::TEST32ri; break;
1127 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1128 }
1129 if (TestOpc) {
1130 unsigned OpReg = getRegForValue(TI->getOperand(0));
1131 if (OpReg == 0) return false;
1132 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1133 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001134
Chris Lattnerc76d1212011-04-19 04:26:32 +00001135 unsigned JmpOpc = X86::JNE_4;
1136 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1137 std::swap(TrueMBB, FalseMBB);
1138 JmpOpc = X86::JE_4;
1139 }
Eric Christopher471e4222011-06-08 23:55:35 +00001140
Chris Lattnerc76d1212011-04-19 04:26:32 +00001141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001142 .addMBB(TrueMBB);
1143 FastEmitBranch(FalseMBB, DL);
1144 FuncInfo.MBB->addSuccessor(TrueMBB);
1145 return true;
1146 }
1147 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001148 }
1149
1150 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001151 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1152 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001153 unsigned OpReg = getRegForValue(BI->getCondition());
1154 if (OpReg == 0) return false;
1155
Eli Friedman547eb4f2011-04-27 01:34:27 +00001156 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1157 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001158 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1159 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001160 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001161 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001162 return true;
1163}
1164
Dan Gohman46510a72010-04-15 01:51:59 +00001165bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001166 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001167 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001168 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001169 CReg = X86::CL;
1170 RC = &X86::GR8RegClass;
1171 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001172 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1173 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1174 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001175 default: return false;
1176 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001177 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001178 CReg = X86::CX;
1179 RC = &X86::GR16RegClass;
1180 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001181 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1182 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1183 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001184 default: return false;
1185 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001186 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001187 CReg = X86::ECX;
1188 RC = &X86::GR32RegClass;
1189 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001190 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1191 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1192 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001193 default: return false;
1194 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001195 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001196 CReg = X86::RCX;
1197 RC = &X86::GR64RegClass;
1198 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001199 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1200 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1201 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001202 default: return false;
1203 }
1204 } else {
1205 return false;
1206 }
1207
Duncan Sands1440e8b2010-11-03 11:35:31 +00001208 MVT VT;
1209 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001210 return false;
1211
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001212 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1213 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001214
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001215 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1216 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001217 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1218 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001219
1220 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001221 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001222 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001223 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1224 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001225 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001226
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001227 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001228 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1229 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001230 UpdateValueMap(I, ResultReg);
1231 return true;
1232}
1233
Dan Gohman46510a72010-04-15 01:51:59 +00001234bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001235 MVT VT;
1236 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001237 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001238
Eric Christophere487b012010-09-29 23:00:29 +00001239 // We only use cmov here, if we don't have a cmov instruction bail.
1240 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001241
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001242 unsigned Opc = 0;
1243 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001244 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001245 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001246 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001247 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001248 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001249 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001250 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001251 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001252 RC = &X86::GR64RegClass;
1253 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001254 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001255 }
1256
1257 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1258 if (Op0Reg == 0) return false;
1259 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1260 if (Op1Reg == 0) return false;
1261 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1262 if (Op2Reg == 0) return false;
1263
Dan Gohman84023e02010-07-10 09:00:22 +00001264 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1265 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001266 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001267 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1268 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001269 UpdateValueMap(I, ResultReg);
1270 return true;
1271}
1272
Dan Gohman46510a72010-04-15 01:51:59 +00001273bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001274 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001275 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001276 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001277 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001278 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001279 unsigned OpReg = getRegForValue(V);
1280 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001281 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001282 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1283 TII.get(X86::CVTSS2SDrr), ResultReg)
1284 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001285 UpdateValueMap(I, ResultReg);
1286 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001287 }
1288 }
1289
1290 return false;
1291}
1292
Dan Gohman46510a72010-04-15 01:51:59 +00001293bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001294 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001295 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001296 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001297 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001298 unsigned OpReg = getRegForValue(V);
1299 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001300 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001301 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1302 TII.get(X86::CVTSD2SSrr), ResultReg)
1303 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001304 UpdateValueMap(I, ResultReg);
1305 return true;
1306 }
1307 }
1308 }
1309
1310 return false;
1311}
1312
Dan Gohman46510a72010-04-15 01:51:59 +00001313bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001314 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1315 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001316
Eli Friedman76927d732011-05-25 23:49:02 +00001317 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001319 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001320 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001321 return false;
1322
1323 unsigned InputReg = getRegForValue(I->getOperand(0));
1324 if (!InputReg)
1325 // Unhandled operand. Halt "fast" selection and bail.
1326 return false;
1327
Eli Friedman76927d732011-05-25 23:49:02 +00001328 if (SrcVT == MVT::i8) {
1329 // Truncate from i8 to i1; no code needed.
1330 UpdateValueMap(I, InputReg);
1331 return true;
1332 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001333
Eli Friedman76927d732011-05-25 23:49:02 +00001334 if (!Subtarget->is64Bit()) {
1335 // If we're on x86-32; we can't extract an i8 from a general register.
1336 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperc9099502012-04-20 06:31:50 +00001337 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1338 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1339 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedman76927d732011-05-25 23:49:02 +00001340 unsigned CopyReg = createResultReg(CopyRC);
1341 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1342 CopyReg).addReg(InputReg);
1343 InputReg = CopyReg;
1344 }
1345
1346 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001347 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001348 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001349 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001350 if (!ResultReg)
1351 return false;
1352
1353 UpdateValueMap(I, ResultReg);
1354 return true;
1355}
1356
Eli Friedmanc0883452011-05-20 22:21:04 +00001357bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1358 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1359}
1360
Eli Friedmand5089a92011-04-27 01:45:07 +00001361bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1362 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001363
Eli Friedmand5089a92011-04-27 01:45:07 +00001364 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001365 if (!IsMemcpySmall(Len))
1366 return false;
1367
1368 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001369
1370 // We don't care about alignment here since we just emit integer accesses.
1371 while (Len) {
1372 MVT VT;
1373 if (Len >= 8 && i64Legal)
1374 VT = MVT::i64;
1375 else if (Len >= 4)
1376 VT = MVT::i32;
1377 else if (Len >= 2)
1378 VT = MVT::i16;
1379 else {
1380 assert(Len == 1);
1381 VT = MVT::i8;
1382 }
1383
1384 unsigned Reg;
1385 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1386 RV &= X86FastEmitStore(VT, Reg, DestAM);
1387 assert(RV && "Failed to emit load or store??");
1388
1389 unsigned Size = VT.getSizeInBits()/8;
1390 Len -= Size;
1391 DestAM.Disp += Size;
1392 SrcAM.Disp += Size;
1393 }
1394
1395 return true;
1396}
1397
Dan Gohman46510a72010-04-15 01:51:59 +00001398bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001399 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001400 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001401 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001402 case Intrinsic::memcpy: {
1403 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1404 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001405 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001406 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001407
Eli Friedman25255cb2011-06-10 23:39:36 +00001408 if (isa<ConstantInt>(MCI.getLength())) {
1409 // Small memcpy's are common enough that we want to do them
1410 // without a call if possible.
1411 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1412 if (IsMemcpySmall(Len)) {
1413 X86AddressMode DestAM, SrcAM;
1414 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1415 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1416 return false;
1417 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1418 return true;
1419 }
1420 }
Eric Christopher471e4222011-06-08 23:55:35 +00001421
Eli Friedman25255cb2011-06-10 23:39:36 +00001422 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1423 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001424 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001425
Eli Friedman25255cb2011-06-10 23:39:36 +00001426 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1427 return false;
1428
1429 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001430 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001431 case Intrinsic::memset: {
1432 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001433
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001434 if (MSI.isVolatile())
1435 return false;
1436
Eli Friedman25255cb2011-06-10 23:39:36 +00001437 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1438 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1439 return false;
1440
1441 if (MSI.getDestAddressSpace() > 255)
1442 return false;
1443
1444 return DoSelectCall(&I, "memset");
1445 }
Eric Christopher07754c22010-03-18 20:27:26 +00001446 case Intrinsic::stackprotector: {
Chad Rosiere1093e52012-05-11 19:43:29 +00001447 // Emit code to store the stack guard onto the stack.
Eric Christopher07754c22010-03-18 20:27:26 +00001448 EVT PtrTy = TLI.getPointerTy();
1449
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001450 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1451 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001452
1453 // Grab the frame index.
1454 X86AddressMode AM;
1455 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001456 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001457 return true;
1458 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001459 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001460 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001461 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001462 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001463 if (!X86SelectAddress(DI->getAddress(), AM))
1464 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001465 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001466 // FIXME may need to add RegState::Debug to any registers produced,
1467 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001468 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1469 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001470 return true;
1471 }
Eric Christopher77f79892010-01-18 22:11:29 +00001472 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001473 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001474 return true;
1475 }
Bill Wendling52370a12008-12-09 02:42:50 +00001476 case Intrinsic::sadd_with_overflow:
1477 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001478 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001479
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001480 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001481 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001482 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001483 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001484 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1485
Duncan Sands1440e8b2010-11-03 11:35:31 +00001486 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001487 if (!isTypeLegal(RetTy, VT))
1488 return false;
1489
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001490 const Value *Op1 = I.getArgOperand(0);
1491 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001492 unsigned Reg1 = getRegForValue(Op1);
1493 unsigned Reg2 = getRegForValue(Op2);
1494
1495 if (Reg1 == 0 || Reg2 == 0)
1496 // FIXME: Handle values *not* in registers.
1497 return false;
1498
1499 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001500 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001501 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001502 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001503 OpC = X86::ADD64rr;
1504 else
1505 return false;
1506
Eli Friedman482feb32011-05-16 21:06:17 +00001507 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001508 // both the returned values.
Eli Friedman482feb32011-05-16 21:06:17 +00001509 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001510 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1511 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001512
Chris Lattnera9a42252009-04-12 07:36:01 +00001513 unsigned Opc = X86::SETBr;
1514 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1515 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1517
1518 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001519 return true;
1520 }
1521 }
1522}
1523
Dan Gohman46510a72010-04-15 01:51:59 +00001524bool X86FastISel::X86SelectCall(const Instruction *I) {
1525 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001526 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001527
1528 // Can't handle inline asm yet.
1529 if (isa<InlineAsm>(Callee))
1530 return false;
1531
Bill Wendling52370a12008-12-09 02:42:50 +00001532 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001533 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001534 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001535
Chad Rosier425e9512012-12-11 00:18:02 +00001536 // Allow SelectionDAG isel to handle tail calls.
1537 if (cast<CallInst>(I)->isTailCall())
1538 return false;
1539
Eli Friedman25255cb2011-06-10 23:39:36 +00001540 return DoSelectCall(I, 0);
1541}
1542
Rafael Espindolac338fe02012-07-25 15:42:45 +00001543static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1544 const ImmutableCallSite &CS) {
Rafael Espindola742f2c92012-07-25 13:35:45 +00001545 if (Subtarget.is64Bit())
1546 return 0;
1547 if (Subtarget.isTargetWindows())
1548 return 0;
1549 CallingConv::ID CC = CS.getCallingConv();
1550 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1551 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001552 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola742f2c92012-07-25 13:35:45 +00001553 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001554 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola1cee7102012-07-25 13:41:10 +00001555 return 0;
Rafael Espindola742f2c92012-07-25 13:35:45 +00001556 return 4;
1557}
1558
Eli Friedman25255cb2011-06-10 23:39:36 +00001559// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1560bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1561 const CallInst *CI = cast<CallInst>(I);
1562 const Value *Callee = CI->getCalledValue();
1563
Evan Chengf3d4efe2008-09-07 09:09:33 +00001564 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001565 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001566 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001567 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001568 CC != CallingConv::X86_FastCall)
1569 return false;
1570
Evan Cheng381993f2010-01-27 00:00:57 +00001571 // fastcc with -tailcallopt is intended to provide a guaranteed
1572 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001573 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001574 return false;
1575
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001576 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1577 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001578 bool isVarArg = FTy->isVarArg();
1579
1580 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1581 // x86-32. Special handling for x86-64 is implemented.
1582 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001583 return false;
1584
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001585 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001586 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001587 TM.Options.GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001588 return false;
1589
Eli Friedman19515b42011-05-17 18:29:03 +00001590 // Check whether the function can return without sret-demotion.
1591 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001592 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman19515b42011-05-17 18:29:03 +00001593 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001594 *FuncInfo.MF, FTy->isVarArg(),
1595 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001596 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001597 return false;
1598
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001599 // Materialize callee address in a register. FIXME: GV address can be
1600 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001601 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001602 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001603 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001604 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001605 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001606 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001607 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001608 } else if (CalleeAM.Base.Reg != 0) {
1609 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001610 } else
1611 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001612
Evan Chengf3d4efe2008-09-07 09:09:33 +00001613 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001614 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001615 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001616 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001617 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosier15b44972012-02-15 00:36:26 +00001618 unsigned arg_size = CS.arg_size();
1619 Args.reserve(arg_size);
1620 ArgVals.reserve(arg_size);
1621 ArgVTs.reserve(arg_size);
1622 ArgFlags.reserve(arg_size);
Dan Gohman46510a72010-04-15 01:51:59 +00001623 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001624 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001625 // If we're lowering a mem intrinsic instead of a regular call, skip the
1626 // last two arguments, which should not passed to the underlying functions.
1627 if (MemIntName && e-i <= 2)
1628 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001629 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001630 ISD::ArgFlagsTy Flags;
1631 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling034b94b2012-12-19 07:18:57 +00001632 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001633 Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00001634 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001635 Flags.setZExt();
1636
Bill Wendling034b94b2012-12-19 07:18:57 +00001637 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001638 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1639 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001640 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1641 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1642 if (!FrameAlign)
1643 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1644 Flags.setByVal();
1645 Flags.setByValSize(FrameSize);
1646 Flags.setByValAlign(FrameAlign);
1647 if (!IsMemcpySmall(FrameSize))
1648 return false;
1649 }
1650
Bill Wendling034b94b2012-12-19 07:18:57 +00001651 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedmanc0883452011-05-20 22:21:04 +00001652 Flags.setInReg();
Bill Wendling034b94b2012-12-19 07:18:57 +00001653 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedmanc0883452011-05-20 22:21:04 +00001654 Flags.setNest();
1655
Chris Lattnere03b8d32011-04-19 04:42:38 +00001656 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1657 // instruction. This is safe because it is common to all fastisel supported
1658 // calling conventions on x86.
1659 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1660 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1661 CI->getBitWidth() == 16) {
1662 if (Flags.isSExt())
1663 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1664 else
1665 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1666 }
1667 }
Eric Christopher471e4222011-06-08 23:55:35 +00001668
Chris Lattnerb44101c2011-04-19 05:09:50 +00001669 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001670
Chris Lattnerff009ad2011-04-19 05:15:59 +00001671 // Passing bools around ends up doing a trunc to i1 and passing it.
1672 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001673 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1674 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1675 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001676 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1677 ArgReg = getRegForValue(ArgVal);
1678 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001679
Chris Lattnerb44101c2011-04-19 05:09:50 +00001680 MVT ArgVT;
1681 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001682
Chris Lattnerb44101c2011-04-19 05:09:50 +00001683 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1684 ArgVal->hasOneUse(), 1);
1685 } else {
1686 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001687 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001688
Chris Lattnerff009ad2011-04-19 05:15:59 +00001689 if (ArgReg == 0) return false;
1690
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001691 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001692 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001693 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001694 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001695 if (ArgVT == MVT::x86mmx)
1696 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001697 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1698 Flags.setOrigAlign(OriginalAlignment);
1699
Chris Lattnerb44101c2011-04-19 05:09:50 +00001700 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001701 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001702 ArgVTs.push_back(ArgVT);
1703 ArgFlags.push_back(Flags);
1704 }
1705
1706 // Analyze operands of the call, assigning locations to each operand.
1707 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001708 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00001709 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001710
Dan Gohmand8acddd2010-06-01 21:09:47 +00001711 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001712 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001713 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001714
Duncan Sands45907662010-10-31 13:21:44 +00001715 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001716
1717 // Get a count of how many bytes are to be pushed on the stack.
1718 unsigned NumBytes = CCInfo.getNextStackOffset();
1719
1720 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001721 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001722 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1723 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001724
Chris Lattner438949a2008-10-15 05:30:52 +00001725 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001726 // copies / loads.
1727 SmallVector<unsigned, 4> RegArgs;
1728 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1729 CCValAssign &VA = ArgLocs[i];
1730 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001731 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001732
Evan Chengf3d4efe2008-09-07 09:09:33 +00001733 // Promote the value if needed.
1734 switch (VA.getLocInfo()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001735 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001736 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001737 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1738 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001739 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1740 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001741 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001742 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001743 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001744 }
1745 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001746 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1747 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001748 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1749 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001750 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001751 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001752 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001753 }
1754 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001755 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1756 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001757 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1758 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001759 if (!Emitted)
1760 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001761 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001762 if (!Emitted)
1763 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1764 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001765
Chris Lattnerc46ec642011-01-05 22:26:52 +00001766 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001767 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001768 break;
1769 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001770 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001771 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001772 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001773 assert(BC != 0 && "Failed to emit a bitcast!");
1774 Arg = BC;
1775 ArgVT = VA.getLocVT();
1776 break;
1777 }
Chad Rosier36ec0ca2012-07-11 19:58:38 +00001778 case CCValAssign::VExt:
1779 // VExt has not been implemented, so this should be impossible to reach
1780 // for now. However, fallback to Selection DAG isel once implemented.
1781 return false;
1782 case CCValAssign::Indirect:
1783 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
1784 // support this.
1785 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001786 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001787
Evan Chengf3d4efe2008-09-07 09:09:33 +00001788 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001789 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1790 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001791 RegArgs.push_back(VA.getLocReg());
1792 } else {
1793 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001794 X86AddressMode AM;
Michael Liaof0e06e82012-11-01 03:47:50 +00001795 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman0586d912008-09-10 20:11:02 +00001796 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001797 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00001798 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001799
Eli Friedmanc0883452011-05-20 22:21:04 +00001800 if (Flags.isByVal()) {
1801 X86AddressMode SrcAM;
1802 SrcAM.Base.Reg = Arg;
1803 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
1804 assert(Res && "memcpy length already checked!"); (void)Res;
1805 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
1806 // If this is a really simple value, emit this with the Value* version
Nick Lewycky1f9c6862011-10-12 00:14:12 +00001807 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedmanc0883452011-05-20 22:21:04 +00001808 // as it can cause us to reevaluate the argument.
Lang Hamese4824712011-10-18 22:11:33 +00001809 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
1810 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001811 } else {
Lang Hamese4824712011-10-18 22:11:33 +00001812 if (!X86FastEmitStore(ArgVT, Arg, AM))
1813 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001814 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001815 }
1816 }
1817
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001818 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001819 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001820 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001821 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001822 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1823 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001824 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001825
Eli Friedman37620462011-04-19 17:22:22 +00001826 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1827 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00001828 static const uint16_t XMMArgRegs[] = {
Eli Friedman37620462011-04-19 17:22:22 +00001829 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1830 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1831 };
1832 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1833 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1834 X86::AL).addImm(NumXMMRegs);
1835 }
1836
Evan Chengf3d4efe2008-09-07 09:09:33 +00001837 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001838 MachineInstrBuilder MIB;
1839 if (CalleeOp) {
1840 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001841 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00001842 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00001843 CallOpc = X86::CALL64r;
1844 else
1845 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001846 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1847 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001848
Chris Lattner51e8eab2009-07-09 06:34:26 +00001849 } else {
1850 // Direct call.
1851 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001852 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00001853 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00001854 CallOpc = X86::CALL64pcrel32;
1855 else
1856 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001857
Chris Lattner51e8eab2009-07-09 06:34:26 +00001858 // See if we need any target-specific flags on the GV operand.
1859 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001860
Chris Lattner51e8eab2009-07-09 06:34:26 +00001861 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1862 // external symbols most go through the PLT in PIC mode. If the symbol
1863 // has hidden or protected visibility, or if it is static or local, then
1864 // we don't need to use the PLT - we can directly call it.
1865 if (Subtarget->isTargetELF() &&
1866 TM.getRelocationModel() == Reloc::PIC_ &&
1867 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1868 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001869 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001870 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001871 (!Subtarget->getTargetTriple().isMacOSX() ||
1872 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001873 // PC-relative references to external symbols should go through $stub,
1874 // unless we're building with the leopard linker or later, which
1875 // automatically synthesizes these stubs.
1876 OpFlags = X86II::MO_DARWIN_STUB;
1877 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001878
1879
Eli Friedman25255cb2011-06-10 23:39:36 +00001880 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
1881 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00001882 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00001883 else
1884 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001885 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001886
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00001887 // Add a register mask with the call-preserved registers.
1888 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1889 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
1890
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00001891 // Add an implicit use GOT pointer in EBX.
1892 if (Subtarget->isPICStyleGOT())
1893 MIB.addReg(X86::EBX, RegState::Implicit);
1894
1895 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1896 MIB.addReg(X86::AL, RegState::Implicit);
1897
1898 // Add implicit physical register uses to the call.
1899 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1900 MIB.addReg(RegArgs[i], RegState::Implicit);
1901
Evan Chengf3d4efe2008-09-07 09:09:33 +00001902 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001903 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolac338fe02012-07-25 15:42:45 +00001904 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohman84023e02010-07-10 09:00:22 +00001905 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001906 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001907
Eli Friedman19515b42011-05-17 18:29:03 +00001908 // Build info for return calling conv lowering code.
1909 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1910 SmallVector<ISD::InputArg, 32> Ins;
1911 SmallVector<EVT, 4> RetTys;
1912 ComputeValueVTs(TLI, I->getType(), RetTys);
1913 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1914 EVT VT = RetTys[i];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00001915 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman19515b42011-05-17 18:29:03 +00001916 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1917 for (unsigned j = 0; j != NumRegs; ++j) {
1918 ISD::InputArg MyFlags;
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00001919 MyFlags.VT = RegisterVT;
Eli Friedman19515b42011-05-17 18:29:03 +00001920 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling034b94b2012-12-19 07:18:57 +00001921 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman19515b42011-05-17 18:29:03 +00001922 MyFlags.Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00001923 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman19515b42011-05-17 18:29:03 +00001924 MyFlags.Flags.setZExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00001925 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman19515b42011-05-17 18:29:03 +00001926 MyFlags.Flags.setInReg();
1927 Ins.push_back(MyFlags);
1928 }
1929 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001930
Eli Friedman19515b42011-05-17 18:29:03 +00001931 // Now handle call return values.
1932 SmallVector<unsigned, 4> UsedRegs;
1933 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001934 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00001935 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001936 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1937 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1938 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1939 EVT CopyVT = RVLocs[i].getValVT();
1940 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001941
Evan Chengf3d4efe2008-09-07 09:09:33 +00001942 // If this is a call to a function that returns an fp value on the x87 fp
1943 // stack, but where we prefer to use the value in xmm registers, copy it
1944 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00001945 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001946 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001947 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001948 CopyVT = MVT::f80;
Craig Topperc9099502012-04-20 06:31:50 +00001949 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001950 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001951 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
1952 CopyReg);
1953 } else {
1954 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1955 CopyReg).addReg(RVLocs[i].getLocReg());
1956 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001957 }
1958
Eli Friedman19515b42011-05-17 18:29:03 +00001959 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001960 // Round the F80 the right size, which also moves to the appropriate xmm
1961 // register. This is accomplished by storing the F80 value in memory and
1962 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00001963 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001965 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001966 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001967 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1968 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00001969 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001970 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00001971 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00001972 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001973 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001974 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00001975
Eli Friedman19515b42011-05-17 18:29:03 +00001976 if (RVLocs.size())
1977 UpdateValueMap(I, ResultReg, RVLocs.size());
1978
Dan Gohmandb497122010-06-18 23:28:01 +00001979 // Set all unused physreg defs as dead.
1980 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1981
Evan Chengf3d4efe2008-09-07 09:09:33 +00001982 return true;
1983}
1984
1985
Dan Gohman99b21822008-08-28 23:21:34 +00001986bool
Dan Gohman46510a72010-04-15 01:51:59 +00001987X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001988 switch (I->getOpcode()) {
1989 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001990 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001991 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001992 case Instruction::Store:
1993 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001994 case Instruction::Ret:
1995 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001996 case Instruction::ICmp:
1997 case Instruction::FCmp:
1998 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001999 case Instruction::ZExt:
2000 return X86SelectZExt(I);
2001 case Instruction::Br:
2002 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002003 case Instruction::Call:
2004 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002005 case Instruction::LShr:
2006 case Instruction::AShr:
2007 case Instruction::Shl:
2008 return X86SelectShift(I);
2009 case Instruction::Select:
2010 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00002011 case Instruction::Trunc:
2012 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00002013 case Instruction::FPExt:
2014 return X86SelectFPExt(I);
2015 case Instruction::FPTrunc:
2016 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00002017 case Instruction::IntToPtr: // Deliberate fall-through.
2018 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00002019 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2020 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00002021 if (DstVT.bitsGT(SrcVT))
2022 return X86SelectZExt(I);
2023 if (DstVT.bitsLT(SrcVT))
2024 return X86SelectTrunc(I);
2025 unsigned Reg = getRegForValue(I->getOperand(0));
2026 if (Reg == 0) return false;
2027 UpdateValueMap(I, Reg);
2028 return true;
2029 }
Dan Gohman99b21822008-08-28 23:21:34 +00002030 }
2031
2032 return false;
2033}
2034
Dan Gohman46510a72010-04-15 01:51:59 +00002035unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002036 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00002037 if (!isTypeLegal(C->getType(), VT))
Michael Liaofaa11592012-08-30 00:30:16 +00002038 return 0;
2039
2040 // Can't handle alternate code models yet.
2041 if (TM.getCodeModel() != CodeModel::Small)
2042 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002043
Owen Anderson95267a12008-09-05 00:06:23 +00002044 // Get opcode and regclass of the output for the given load instruction.
2045 unsigned Opc = 0;
2046 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002047 switch (VT.SimpleTy) {
Michael Liaofaa11592012-08-30 00:30:16 +00002048 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002049 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00002050 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +00002051 RC = &X86::GR8RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002052 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00002054 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +00002055 RC = &X86::GR16RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002056 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002057 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00002058 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +00002059 RC = &X86::GR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002060 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002061 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00002062 // Must be in x86-64 mode.
2063 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +00002064 RC = &X86::GR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002065 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002067 if (X86ScalarSSEf32) {
2068 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +00002069 RC = &X86::FR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002070 } else {
2071 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +00002072 RC = &X86::RFP32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002073 }
2074 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002075 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002076 if (X86ScalarSSEf64) {
2077 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +00002078 RC = &X86::FR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002079 } else {
2080 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +00002081 RC = &X86::RFP64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002082 }
2083 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002084 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002085 // No f80 support yet.
Michael Liaofaa11592012-08-30 00:30:16 +00002086 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002087 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002088
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002089 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002090 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002091 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002092 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002093 // If the expression is just a basereg, then we're done, otherwise we need
2094 // to emit an LEA.
2095 if (AM.BaseType == X86AddressMode::RegBase &&
2096 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2097 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002098
Chris Lattner685090f2011-04-17 17:12:08 +00002099 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002100 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002101 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2102 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002103 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002104 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002105 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002106 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002107
Owen Anderson3b217c62008-09-06 01:11:01 +00002108 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002109 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002110 if (Align == 0) {
2111 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002112 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002113 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002114
Dan Gohman5396c992008-09-30 01:21:32 +00002115 // x86-32 PIC requires a PIC base register for constant pools.
2116 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002117 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002118 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002119 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002120 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002121 } else if (Subtarget->isPICStyleGOT()) {
2122 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002123 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002124 } else if (Subtarget->isPICStyleRIPRel() &&
2125 TM.getCodeModel() == CodeModel::Small) {
2126 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002127 }
Dan Gohman5396c992008-09-30 01:21:32 +00002128
2129 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002130 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002131 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002132 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2133 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002134 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002135
Owen Anderson95267a12008-09-05 00:06:23 +00002136 return ResultReg;
2137}
2138
Dan Gohman46510a72010-04-15 01:51:59 +00002139unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002140 // Fail on dynamic allocas. At this point, getRegForValue has already
2141 // checked its CSE maps, so if we're here trying to handle a dynamic
2142 // alloca, we're not going to succeed. X86SelectAddress has a
2143 // check for dynamic allocas, because it's called directly from
2144 // various places, but TargetMaterializeAlloca also needs a check
2145 // in order to avoid recursion between getRegForValue,
2146 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002147 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002148 return 0;
2149
Dan Gohman0586d912008-09-10 20:11:02 +00002150 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002151 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002152 return 0;
2153 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper44d23822012-02-22 05:59:10 +00002154 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman0586d912008-09-10 20:11:02 +00002155 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002156 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2157 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002158 return ResultReg;
2159}
2160
Eli Friedman2790ba82011-04-27 22:41:55 +00002161unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2162 MVT VT;
2163 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002164 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002165
2166 // Get opcode and regclass for the given zero.
2167 unsigned Opc = 0;
2168 const TargetRegisterClass *RC = NULL;
2169 switch (VT.SimpleTy) {
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002170 default: return 0;
Craig Topperf4cfc442012-08-11 17:53:00 +00002171 case MVT::f32:
2172 if (X86ScalarSSEf32) {
2173 Opc = X86::FsFLD0SS;
2174 RC = &X86::FR32RegClass;
2175 } else {
2176 Opc = X86::LD_Fp032;
2177 RC = &X86::RFP32RegClass;
2178 }
2179 break;
2180 case MVT::f64:
2181 if (X86ScalarSSEf64) {
2182 Opc = X86::FsFLD0SD;
2183 RC = &X86::FR64RegClass;
2184 } else {
2185 Opc = X86::LD_Fp064;
2186 RC = &X86::RFP64RegClass;
2187 }
2188 break;
2189 case MVT::f80:
2190 // No f80 support yet.
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002191 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002192 }
2193
2194 unsigned ResultReg = createResultReg(RC);
2195 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2196 return ResultReg;
2197}
2198
2199
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002200/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2201/// vreg is being provided by the specified load instruction. If possible,
2202/// try to fold the load as an operand to the instruction, returning true if
2203/// possible.
2204bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2205 const LoadInst *LI) {
2206 X86AddressMode AM;
2207 if (!X86SelectAddress(LI->getOperand(0), AM))
2208 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002209
Craig Topperdca72542012-08-11 17:46:16 +00002210 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002211
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002212 unsigned Size = TD.getTypeAllocSize(LI->getType());
2213 unsigned Alignment = LI->getAlignment();
2214
2215 SmallVector<MachineOperand, 8> AddrOps;
2216 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002217
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002218 MachineInstr *Result =
2219 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2220 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002221
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002222 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002223 MI->eraseFromParent();
2224 return true;
2225}
2226
2227
Evan Chengc3f44b02008-09-03 00:03:49 +00002228namespace llvm {
Bob Wilsond49edb72012-08-03 04:06:28 +00002229 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2230 const TargetLibraryInfo *libInfo) {
2231 return new X86FastISel(funcInfo, libInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002232 }
Dan Gohman99b21822008-08-28 23:21:34 +00002233}