blob: c5da0b9b164f6140f3d8c5b099bbef0686b4acb1 [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
Chad Rosierfd3417d2013-02-25 21:59:35 +000078 virtual bool FastLowerArguments();
79
Dan Gohman1adf1b02008-08-19 21:45:35 +000080#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000081
82private:
Dan Gohman46510a72010-04-15 01:51:59 +000083 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000084
Owen Andersone50ed302009-08-10 22:56:29 +000085 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000086
Chris Lattnerb44101c2011-04-19 05:09:50 +000087 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
88 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000089
Owen Andersone50ed302009-08-10 22:56:29 +000090 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000091 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000092
Dan Gohman46510a72010-04-15 01:51:59 +000093 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
94 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000095
Dan Gohman46510a72010-04-15 01:51:59 +000096 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000097
Dan Gohman46510a72010-04-15 01:51:59 +000098 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000099
Dan Gohman84023e02010-07-10 09:00:22 +0000100 bool X86SelectRet(const Instruction *I);
101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000105
Dan Gohman46510a72010-04-15 01:51:59 +0000106 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000107
Dan Gohman46510a72010-04-15 01:51:59 +0000108 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000109
Dan Gohman46510a72010-04-15 01:51:59 +0000110 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000111
Dan Gohman46510a72010-04-15 01:51:59 +0000112 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000113
Dan Gohman46510a72010-04-15 01:51:59 +0000114 bool X86SelectFPExt(const Instruction *I);
115 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000116
Dan Gohman46510a72010-04-15 01:51:59 +0000117 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
118 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000119
Eli Friedman25255cb2011-06-10 23:39:36 +0000120 bool DoSelectCall(const Instruction *I, const char *MemIntName);
121
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000122 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000123 return getTargetMachine()->getInstrInfo();
124 }
125 const X86TargetMachine *getTargetMachine() const {
126 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000127 }
128
Dan Gohman46510a72010-04-15 01:51:59 +0000129 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000130
Dan Gohman46510a72010-04-15 01:51:59 +0000131 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000132
Eli Friedman2790ba82011-04-27 22:41:55 +0000133 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
134
Evan Chengf3d4efe2008-09-07 09:09:33 +0000135 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
136 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000137 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
139 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000140 }
141
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000142 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000143
Eli Friedmanc0883452011-05-20 22:21:04 +0000144 bool IsMemcpySmall(uint64_t Len);
145
Eli Friedmand5089a92011-04-27 01:45:07 +0000146 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
147 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000148};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000149
Chris Lattner087fcf32009-03-08 18:44:31 +0000150} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000151
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000152bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000153 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
154 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000155 // Unhandled type. Halt "fast" selection and bail.
156 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000157
158 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000159 // For now, require SSE/SSE2 for performing floating-point operations,
160 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topperf4cfc442012-08-11 17:53:00 +0000162 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topperf4cfc442012-08-11 17:53:00 +0000164 return false;
Dan Gohman9b66d732008-09-30 00:48:39 +0000165 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000167 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000168 // We only handle legal types. For example, on x86-32 the instruction
169 // selector contains all of the 64-bit instructions from x86-64,
170 // under the assumption that i64 won't be used if the target doesn't
171 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000173}
174
175#include "X86GenCallingConv.inc"
176
Evan Cheng0de588f2008-09-05 21:00:03 +0000177/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000178/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000179/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000180bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000181 unsigned &ResultReg) {
182 // Get opcode and regclass of the output for the given load instruction.
183 unsigned Opc = 0;
184 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000185 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000186 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000187 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000189 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +0000190 RC = &X86::GR8RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000191 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +0000194 RC = &X86::GR16RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000195 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000198 RC = &X86::GR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000199 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000201 // Must be in x86-64 mode.
202 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000203 RC = &X86::GR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000204 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000206 if (X86ScalarSSEf32) {
207 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +0000208 RC = &X86::FR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000209 } else {
210 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +0000211 RC = &X86::RFP32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000212 }
213 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000215 if (X86ScalarSSEf64) {
216 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +0000217 RC = &X86::FR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000218 } else {
219 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +0000220 RC = &X86::RFP64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000221 }
222 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000224 // No f80 support yet.
225 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000226 }
227
228 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000229 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
230 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000231 return true;
232}
233
Evan Chengf3d4efe2008-09-07 09:09:33 +0000234/// X86FastEmitStore - Emit a machine instruction to store a value Val of
235/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
236/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000237/// i.e. V. Return true if it is possible.
238bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000239X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000240 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000241 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 switch (VT.getSimpleVT().SimpleTy) {
243 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000244 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000245 case MVT::i1: {
246 // Mask out all but lowest bit.
Craig Topperc9099502012-04-20 06:31:50 +0000247 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000249 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
250 Val = AndResult;
251 }
252 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 case MVT::i8: Opc = X86::MOV8mr; break;
254 case MVT::i16: Opc = X86::MOV16mr; break;
255 case MVT::i32: Opc = X86::MOV32mr; break;
256 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
257 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000258 Opc = X86ScalarSSEf32 ?
259 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000260 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000262 Opc = X86ScalarSSEf64 ?
263 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000264 break;
Lang Hamese4824712011-10-18 22:11:33 +0000265 case MVT::v4f32:
266 Opc = X86::MOVAPSmr;
267 break;
268 case MVT::v2f64:
269 Opc = X86::MOVAPDmr;
270 break;
271 case MVT::v4i32:
272 case MVT::v2i64:
273 case MVT::v8i16:
274 case MVT::v16i8:
275 Opc = X86::MOVDQAmr;
276 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000277 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000278
Dan Gohman84023e02010-07-10 09:00:22 +0000279 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
280 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000281 return true;
282}
283
Dan Gohman46510a72010-04-15 01:51:59 +0000284bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000285 const X86AddressMode &AM) {
286 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000287 if (isa<ConstantPointerNull>(Val))
288 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000289
Chris Lattner438949a2008-10-15 05:30:52 +0000290 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000291 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000292 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000293 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000295 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000296 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 case MVT::i8: Opc = X86::MOV8mi; break;
298 case MVT::i16: Opc = X86::MOV16mi; break;
299 case MVT::i32: Opc = X86::MOV32mi; break;
300 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000301 // Must be a 32-bit sign extended value.
Jakub Staszakeaf77252012-11-15 19:05:23 +0000302 if (isInt<32>(CI->getSExtValue()))
Chris Lattner438949a2008-10-15 05:30:52 +0000303 Opc = X86::MOV64mi32;
304 break;
305 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000306
Chris Lattner438949a2008-10-15 05:30:52 +0000307 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000308 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
309 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000310 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000311 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000312 return true;
313 }
314 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000315
Chris Lattner438949a2008-10-15 05:30:52 +0000316 unsigned ValReg = getRegForValue(Val);
317 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000318 return false;
319
Chris Lattner438949a2008-10-15 05:30:52 +0000320 return X86FastEmitStore(VT, ValReg, AM);
321}
322
Evan Cheng24e3a902008-09-08 06:35:17 +0000323/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
324/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
325/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000326bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
327 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000328 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000329 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
330 Src, /*TODO: Kill=*/false);
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000331 if (RR == 0)
Owen Andersonac34a002008-09-11 19:44:55 +0000332 return false;
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000333
334 ResultReg = RR;
335 return true;
Evan Cheng24e3a902008-09-08 06:35:17 +0000336}
337
Dan Gohman0586d912008-09-10 20:11:02 +0000338/// X86SelectAddress - Attempt to fill in an address from the given value.
339///
Dan Gohman46510a72010-04-15 01:51:59 +0000340bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
341 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000342 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000343 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000344 // Don't walk into other basic blocks; it's possible we haven't
345 // visited them yet, so the instructions may not yet be assigned
346 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000347 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
348 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
349 Opcode = I->getOpcode();
350 U = I;
351 }
Dan Gohman46510a72010-04-15 01:51:59 +0000352 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000353 Opcode = C->getOpcode();
354 U = C;
355 }
Dan Gohman0586d912008-09-10 20:11:02 +0000356
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000357 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000358 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000359 // Fast instruction selection doesn't support the special
360 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000361 return false;
362
Dan Gohman35893082008-09-18 23:23:44 +0000363 switch (Opcode) {
364 default: break;
365 case Instruction::BitCast:
366 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000367 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000368
369 case Instruction::IntToPtr:
370 // Look past no-op inttoptrs.
371 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000372 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000373 break;
Dan Gohman35893082008-09-18 23:23:44 +0000374
375 case Instruction::PtrToInt:
376 // Look past no-op ptrtoints.
377 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000378 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000379 break;
Dan Gohman35893082008-09-18 23:23:44 +0000380
381 case Instruction::Alloca: {
382 // Do static allocas.
383 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000384 DenseMap<const AllocaInst*, int>::iterator SI =
385 FuncInfo.StaticAllocaMap.find(A);
386 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000387 AM.BaseType = X86AddressMode::FrameIndexBase;
388 AM.Base.FrameIndex = SI->second;
389 return true;
390 }
391 break;
Dan Gohman35893082008-09-18 23:23:44 +0000392 }
393
394 case Instruction::Add: {
395 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000396 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000397 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
398 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000399 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000400 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000401 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000402 }
Dan Gohman0586d912008-09-10 20:11:02 +0000403 }
Dan Gohman35893082008-09-18 23:23:44 +0000404 break;
405 }
406
407 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000408 X86AddressMode SavedAM = AM;
409
Dan Gohman35893082008-09-18 23:23:44 +0000410 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000411 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000412 unsigned IndexReg = AM.IndexReg;
413 unsigned Scale = AM.Scale;
414 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000415 // Iterate through the indices, folding what we can. Constants can be
416 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000417 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000418 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000419 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000420 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000421 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000422 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
423 continue;
424 }
Eric Christopher471e4222011-06-08 23:55:35 +0000425
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000426 // A array/variable index is always of the form i*S where S is the
427 // constant scale size. See if we can push the scale into immediates.
428 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
429 for (;;) {
430 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
431 // Constant-offset addressing.
432 Disp += CI->getSExtValue() * S;
433 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000434 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000435 if (isa<AddOperator>(Op) &&
436 (!isa<Instruction>(Op) ||
437 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
438 == FuncInfo.MBB) &&
439 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
440 // An add (in the same block) with a constant operand. Fold the
441 // constant.
442 ConstantInt *CI =
443 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
444 Disp += CI->getSExtValue() * S;
445 // Iterate on the other operand.
446 Op = cast<AddOperator>(Op)->getOperand(0);
447 continue;
448 }
449 if (IndexReg == 0 &&
450 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
451 (S == 1 || S == 2 || S == 4 || S == 8)) {
452 // Scaled-index addressing.
453 Scale = S;
454 IndexReg = getRegForGEPIndex(Op).first;
455 if (IndexReg == 0)
456 return false;
457 break;
458 }
459 // Unsupported.
460 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000461 }
462 }
Dan Gohman09aae462008-09-26 20:04:15 +0000463 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000464 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000465 break;
Dan Gohman35893082008-09-18 23:23:44 +0000466 // Ok, the GEP indices were covered by constant-offset and scaled-index
467 // addressing. Update the address state and move on to examining the base.
468 AM.IndexReg = IndexReg;
469 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000470 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000471 if (X86SelectAddress(U->getOperand(0), AM))
472 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000473
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000474 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000475 // our address and just match the value instead of completely failing.
476 AM = SavedAM;
477 break;
Dan Gohman35893082008-09-18 23:23:44 +0000478 unsupported_gep:
479 // Ok, the GEP indices weren't all covered.
480 break;
481 }
482 }
483
484 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000485 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedmana6176ad2011-09-22 23:41:28 +0000486 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000487 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000488 return false;
489
Eli Friedmana6176ad2011-09-22 23:41:28 +0000490 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000491 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000492 if (GVar->isThreadLocal())
493 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000494
Eli Friedmana6176ad2011-09-22 23:41:28 +0000495 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
496 // it works...).
497 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
498 if (const GlobalVariable *GVar =
499 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
500 if (GVar->isThreadLocal())
501 return false;
502
Chris Lattner0a1c9972011-04-17 17:47:38 +0000503 // RIP-relative addresses can't have additional register operands, so if
504 // we've already folded stuff into the addressing mode, just force the
505 // global value into its own register, which we can use as the basereg.
506 if (!Subtarget->isPICStyleRIPRel() ||
507 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
508 // Okay, we've committed to selecting this global. Set up the address.
509 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000510
Chris Lattner0a1c9972011-04-17 17:47:38 +0000511 // Allow the subtarget to classify the global.
512 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000513
Chris Lattner0a1c9972011-04-17 17:47:38 +0000514 // If this reference is relative to the pic base, set it now.
515 if (isGlobalRelativeToPICBase(GVFlags)) {
516 // FIXME: How do we know Base.Reg is free??
517 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000518 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000519
520 // Unless the ABI requires an extra load, return a direct reference to
521 // the global.
522 if (!isGlobalStubReference(GVFlags)) {
523 if (Subtarget->isPICStyleRIPRel()) {
524 // Use rip-relative addressing if we can. Above we verified that the
525 // base and index registers are unused.
526 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
527 AM.Base.Reg = X86::RIP;
528 }
529 AM.GVOpFlags = GVFlags;
530 return true;
531 }
532
533 // Ok, we need to do a load from a stub. If we've already loaded from
534 // this stub, reuse the loaded pointer, otherwise emit the load now.
535 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
536 unsigned LoadReg;
537 if (I != LocalValueMap.end() && I->second != 0) {
538 LoadReg = I->second;
539 } else {
540 // Issue load from stub.
541 unsigned Opc = 0;
542 const TargetRegisterClass *RC = NULL;
543 X86AddressMode StubAM;
544 StubAM.Base.Reg = AM.Base.Reg;
545 StubAM.GV = GV;
546 StubAM.GVOpFlags = GVFlags;
547
548 // Prepare for inserting code in the local-value area.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000549 SavePoint SaveInsertPt = enterLocalValueArea();
Chris Lattner0a1c9972011-04-17 17:47:38 +0000550
551 if (TLI.getPointerTy() == MVT::i64) {
552 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000553 RC = &X86::GR64RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000554
555 if (Subtarget->isPICStyleRIPRel())
556 StubAM.Base.Reg = X86::RIP;
557 } else {
558 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000559 RC = &X86::GR32RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000560 }
561
562 LoadReg = createResultReg(RC);
563 MachineInstrBuilder LoadMI =
564 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
565 addFullAddress(LoadMI, StubAM);
566
567 // Ok, back to normal mode.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000568 leaveLocalValueArea(SaveInsertPt);
Chris Lattner0a1c9972011-04-17 17:47:38 +0000569
570 // Prevent loading GV stub multiple times in same MBB.
571 LocalValueMap[V] = LoadReg;
572 }
573
574 // Now construct the final address. Note that the Disp, Scale,
575 // and Index values may already be set here.
576 AM.Base.Reg = LoadReg;
577 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000578 return true;
579 }
Dan Gohman0586d912008-09-10 20:11:02 +0000580 }
581
Dan Gohman97135e12008-09-26 19:15:30 +0000582 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000583 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000584 if (AM.Base.Reg == 0) {
585 AM.Base.Reg = getRegForValue(V);
586 return AM.Base.Reg != 0;
587 }
588 if (AM.IndexReg == 0) {
589 assert(AM.Scale == 1 && "Scale with no index!");
590 AM.IndexReg = getRegForValue(V);
591 return AM.IndexReg != 0;
592 }
593 }
594
595 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000596}
597
Chris Lattner0aa43de2009-07-10 05:33:42 +0000598/// X86SelectCallAddress - Attempt to fill in an address from the given value.
599///
Dan Gohman46510a72010-04-15 01:51:59 +0000600bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
601 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000602 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000603 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000604 Opcode = I->getOpcode();
605 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000606 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000607 Opcode = C->getOpcode();
608 U = C;
609 }
610
611 switch (Opcode) {
612 default: break;
613 case Instruction::BitCast:
614 // Look past bitcasts.
615 return X86SelectCallAddress(U->getOperand(0), AM);
616
617 case Instruction::IntToPtr:
618 // Look past no-op inttoptrs.
619 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
620 return X86SelectCallAddress(U->getOperand(0), AM);
621 break;
622
623 case Instruction::PtrToInt:
624 // Look past no-op ptrtoints.
625 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
626 return X86SelectCallAddress(U->getOperand(0), AM);
627 break;
628 }
629
630 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000631 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000632 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000633 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000634 return false;
635
636 // RIP-relative addresses can't have additional register operands.
637 if (Subtarget->isPICStyleRIPRel() &&
638 (AM.Base.Reg != 0 || AM.IndexReg != 0))
639 return false;
640
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000641 // Can't handle DLLImport.
642 if (GV->hasDLLImportLinkage())
643 return false;
644
645 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000646 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000647 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000648 return false;
649
650 // Okay, we've committed to selecting this global. Set up the basic address.
651 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000652
Chris Lattnere6c07b52009-07-10 05:45:15 +0000653 // No ABI requires an extra load for anything other than DLLImport, which
654 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000655 if (Subtarget->isPICStyleRIPRel()) {
656 // Use rip-relative addressing if we can. Above we verified that the
657 // base and index registers are unused.
658 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
659 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000660 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000661 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
662 } else if (Subtarget->isPICStyleGOT()) {
663 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000664 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000665
Chris Lattner0aa43de2009-07-10 05:33:42 +0000666 return true;
667 }
668
669 // If all else fails, try to materialize the value in a register.
670 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
671 if (AM.Base.Reg == 0) {
672 AM.Base.Reg = getRegForValue(V);
673 return AM.Base.Reg != 0;
674 }
675 if (AM.IndexReg == 0) {
676 assert(AM.Scale == 1 && "Scale with no index!");
677 AM.IndexReg = getRegForValue(V);
678 return AM.IndexReg != 0;
679 }
680 }
681
682 return false;
683}
684
685
Owen Andersona3971df2008-09-04 07:08:58 +0000686/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000687bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000688 // Atomic stores need special handling.
Lang Hamese4824712011-10-18 22:11:33 +0000689 const StoreInst *S = cast<StoreInst>(I);
690
691 if (S->isAtomic())
692 return false;
693
694 unsigned SABIAlignment =
695 TD.getABITypeAlignment(S->getValueOperand()->getType());
696 if (S->getAlignment() != 0 && S->getAlignment() < SABIAlignment)
Eli Friedman4136d232011-09-02 22:33:24 +0000697 return false;
698
Duncan Sands1440e8b2010-11-03 11:35:31 +0000699 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000700 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000701 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000702
Dan Gohman0586d912008-09-10 20:11:02 +0000703 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000704 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000705 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000706
Chris Lattner438949a2008-10-15 05:30:52 +0000707 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000708}
709
Dan Gohman84023e02010-07-10 09:00:22 +0000710/// X86SelectRet - Select and emit code to implement ret instructions.
711bool X86FastISel::X86SelectRet(const Instruction *I) {
712 const ReturnInst *Ret = cast<ReturnInst>(I);
713 const Function &F = *I->getParent()->getParent();
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000714 const X86MachineFunctionInfo *X86MFInfo =
715 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohman84023e02010-07-10 09:00:22 +0000716
717 if (!FuncInfo.CanLowerReturn)
718 return false;
719
720 CallingConv::ID CC = F.getCallingConv();
721 if (CC != CallingConv::C &&
722 CC != CallingConv::Fast &&
723 CC != CallingConv::X86_FastCall)
724 return false;
725
726 if (Subtarget->isTargetWin64())
727 return false;
728
729 // Don't handle popping bytes on return for now.
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000730 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszakd61932b2013-02-17 18:35:25 +0000731 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000732
733 // fastcc with -tailcallopt is intended to provide a guaranteed
734 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000735 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohman84023e02010-07-10 09:00:22 +0000736 return false;
737
738 // Let SDISel handle vararg functions.
739 if (F.isVarArg())
740 return false;
741
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000742 // Build a list of return value registers.
743 SmallVector<unsigned, 4> RetRegs;
744
Dan Gohman84023e02010-07-10 09:00:22 +0000745 if (Ret->getNumOperands() > 0) {
746 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +0000747 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohman84023e02010-07-10 09:00:22 +0000748
749 // Analyze operands of the call, assigning locations to each operand.
750 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000751 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +0000752 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000753 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000754
755 const Value *RV = Ret->getOperand(0);
756 unsigned Reg = getRegForValue(RV);
757 if (Reg == 0)
758 return false;
759
760 // Only handle a single return value for now.
761 if (ValLocs.size() != 1)
762 return false;
763
764 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000765
Dan Gohman84023e02010-07-10 09:00:22 +0000766 // Don't bother handling odd stuff for now.
767 if (VA.getLocInfo() != CCValAssign::Full)
768 return false;
769 // Only handle register returns for now.
770 if (!VA.isRegLoc())
771 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000772
773 // The calling-convention tables for x87 returns don't tell
774 // the whole story.
775 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
776 return false;
777
Eli Friedman22486c92011-05-18 23:13:10 +0000778 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000779 EVT SrcVT = TLI.getValueType(RV->getType());
780 EVT DstVT = VA.getValVT();
781 // Special handling for extended integers.
782 if (SrcVT != DstVT) {
783 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
784 return false;
785
786 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
787 return false;
788
789 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
790
791 if (SrcVT == MVT::i1) {
792 if (Outs[0].Flags.isSExt())
793 return false;
794 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
795 SrcVT = MVT::i8;
796 }
797 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
798 ISD::SIGN_EXTEND;
799 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
800 SrcReg, /*TODO: Kill=*/false);
801 }
802
803 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000804 unsigned DstReg = VA.getLocReg();
805 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000806 // Avoid a cross-class copy. This is very unlikely.
807 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000808 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000809 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
810 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000811
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000812 // Add register to return instruction.
813 RetRegs.push_back(VA.getLocReg());
Dan Gohman84023e02010-07-10 09:00:22 +0000814 }
815
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000816 // The x86-64 ABI for returning structs by value requires that we copy
817 // the sret argument into %rax for the return. We saved the argument into
818 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000819 // and into %rax. We also do the same with %eax for Win32.
820 if (F.hasStructRetAttr() &&
821 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000822 unsigned Reg = X86MFInfo->getSRetReturnReg();
823 assert(Reg &&
824 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000825 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000826 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000827 RetReg).addReg(Reg);
828 RetRegs.push_back(RetReg);
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000829 }
830
Dan Gohman84023e02010-07-10 09:00:22 +0000831 // Now emit the RET.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000832 MachineInstrBuilder MIB =
833 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
834 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
835 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohman84023e02010-07-10 09:00:22 +0000836 return true;
837}
838
Evan Cheng8b19e562008-09-03 06:44:39 +0000839/// X86SelectLoad - Select and emit code to implement load instructions.
840///
Dan Gohman46510a72010-04-15 01:51:59 +0000841bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000842 // Atomic loads need special handling.
843 if (cast<LoadInst>(I)->isAtomic())
844 return false;
845
Duncan Sands1440e8b2010-11-03 11:35:31 +0000846 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000847 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000848 return false;
849
Dan Gohman0586d912008-09-10 20:11:02 +0000850 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000851 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000852 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000853
Evan Cheng0de588f2008-09-05 21:00:03 +0000854 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000855 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000856 UpdateValueMap(I, ResultReg);
857 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000858 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000859 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000860}
861
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000862static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000863 bool HasAVX = Subtarget->hasAVX();
Craig Topper1accb7e2012-01-10 06:54:16 +0000864 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
865 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000868 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 case MVT::i8: return X86::CMP8rr;
870 case MVT::i16: return X86::CMP16rr;
871 case MVT::i32: return X86::CMP32rr;
872 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000873 case MVT::f32:
874 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
875 case MVT::f64:
876 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000877 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000878}
879
Chris Lattner0e13c782008-10-15 04:13:29 +0000880/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
881/// of the comparison, return an opcode that works for the compare (e.g.
882/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000883static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000885 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000886 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 case MVT::i8: return X86::CMP8ri;
888 case MVT::i16: return X86::CMP16ri;
889 case MVT::i32: return X86::CMP32ri;
890 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000891 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
892 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000893 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000894 return X86::CMP64ri32;
895 return 0;
896 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000897}
898
Dan Gohman46510a72010-04-15 01:51:59 +0000899bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
900 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000901 unsigned Op0Reg = getRegForValue(Op0);
902 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000903
Chris Lattnerd53886b2008-10-15 05:18:04 +0000904 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000905 if (isa<ConstantPointerNull>(Op1))
906 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000907
Chris Lattner9a08a612008-10-15 04:26:38 +0000908 // We have two options: compare with register or immediate. If the RHS of
909 // the compare is an immediate that we can fold into this compare, use
910 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000911 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000912 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000913 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
914 .addReg(Op0Reg)
915 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000916 return true;
917 }
918 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000919
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000920 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000921 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000922
Chris Lattner9a08a612008-10-15 04:26:38 +0000923 unsigned Op1Reg = getRegForValue(Op1);
924 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000925 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
926 .addReg(Op0Reg)
927 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000928
Chris Lattner9a08a612008-10-15 04:26:38 +0000929 return true;
930}
931
Dan Gohman46510a72010-04-15 01:51:59 +0000932bool X86FastISel::X86SelectCmp(const Instruction *I) {
933 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000934
Duncan Sands1440e8b2010-11-03 11:35:31 +0000935 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000936 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000937 return false;
938
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000939 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000940 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000941 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000942 switch (CI->getPredicate()) {
943 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000944 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
945 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000946
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000947 unsigned EReg = createResultReg(&X86::GR8RegClass);
948 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000949 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
950 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
951 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000952 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000953 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000954 UpdateValueMap(I, ResultReg);
955 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000956 }
957 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000958 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
959 return false;
960
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000961 unsigned NEReg = createResultReg(&X86::GR8RegClass);
962 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000963 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
964 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
965 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000966 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000967 UpdateValueMap(I, ResultReg);
968 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000969 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000970 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
971 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
972 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
973 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
974 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
975 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
976 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
977 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
978 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
979 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
980 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
981 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000982
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000983 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
984 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
985 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
986 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
987 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
988 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
989 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
990 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
991 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
992 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000993 default:
994 return false;
995 }
996
Dan Gohman46510a72010-04-15 01:51:59 +0000997 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000998 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000999 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +00001000
Chris Lattner9a08a612008-10-15 04:26:38 +00001001 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +00001002 if (!X86FastEmitCompare(Op0, Op1, VT))
1003 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001004
Dan Gohman84023e02010-07-10 09:00:22 +00001005 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001006 UpdateValueMap(I, ResultReg);
1007 return true;
1008}
Evan Cheng8b19e562008-09-03 06:44:39 +00001009
Dan Gohman46510a72010-04-15 01:51:59 +00001010bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001011 // Handle zero-extension from i1 to i8, which is common.
Eric Christopher471e4222011-06-08 23:55:35 +00001012 if (!I->getOperand(0)->getType()->isIntegerTy(1))
Eli Friedman76927d732011-05-25 23:49:02 +00001013 return false;
1014
1015 EVT DstVT = TLI.getValueType(I->getType());
1016 if (!TLI.isTypeLegal(DstVT))
1017 return false;
1018
1019 unsigned ResultReg = getRegForValue(I->getOperand(0));
1020 if (ResultReg == 0)
1021 return false;
1022
1023 // Set the high bits to zero.
1024 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1025 if (ResultReg == 0)
1026 return false;
1027
1028 if (DstVT != MVT::i8) {
1029 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1030 ResultReg, /*Kill=*/true);
1031 if (ResultReg == 0)
1032 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +00001033 }
1034
Eli Friedman76927d732011-05-25 23:49:02 +00001035 UpdateValueMap(I, ResultReg);
1036 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +00001037}
1038
Chris Lattner9a08a612008-10-15 04:26:38 +00001039
Dan Gohman46510a72010-04-15 01:51:59 +00001040bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +00001041 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +00001042 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001043 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +00001044 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1045 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +00001046
Dan Gohman8bef7442010-08-21 02:32:36 +00001047 // Fold the common case of a conditional branch with a comparison
1048 // in the same block (values defined on other blocks may not have
1049 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +00001050 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +00001051 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001052 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001053
Dan Gohmand98d6202008-10-02 22:15:21 +00001054 // Try to take advantage of fallthrough opportunities.
1055 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001056 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001057 std::swap(TrueMBB, FalseMBB);
1058 Predicate = CmpInst::getInversePredicate(Predicate);
1059 }
1060
Chris Lattner871d2462008-10-15 03:58:05 +00001061 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1062 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1063
Dan Gohmand98d6202008-10-02 22:15:21 +00001064 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001065 case CmpInst::FCMP_OEQ:
1066 std::swap(TrueMBB, FalseMBB);
1067 Predicate = CmpInst::FCMP_UNE;
1068 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001069 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1070 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1071 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1072 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1073 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1074 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1075 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1076 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1077 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1078 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1079 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1080 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1081 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001082
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001083 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1084 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1085 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1086 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1087 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1088 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1089 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1090 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1091 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1092 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001093 default:
1094 return false;
1095 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001096
Dan Gohman46510a72010-04-15 01:51:59 +00001097 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001098 if (SwapArgs)
1099 std::swap(Op0, Op1);
1100
Chris Lattner9a08a612008-10-15 04:26:38 +00001101 // Emit a compare of the LHS and RHS, setting the flags.
1102 if (!X86FastEmitCompare(Op0, Op1, VT))
1103 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001104
Dan Gohman84023e02010-07-10 09:00:22 +00001105 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1106 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001107
1108 if (Predicate == CmpInst::FCMP_UNE) {
1109 // X86 requires a second branch to handle UNE (and OEQ,
1110 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001111 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1112 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001113 }
1114
Stuart Hastings3bf91252010-06-17 22:43:56 +00001115 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001116 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001117 return true;
1118 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001119 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1120 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1121 // typically happen for _Bool and C++ bools.
1122 MVT SourceVT;
1123 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1124 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1125 unsigned TestOpc = 0;
1126 switch (SourceVT.SimpleTy) {
1127 default: break;
1128 case MVT::i8: TestOpc = X86::TEST8ri; break;
1129 case MVT::i16: TestOpc = X86::TEST16ri; break;
1130 case MVT::i32: TestOpc = X86::TEST32ri; break;
1131 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1132 }
1133 if (TestOpc) {
1134 unsigned OpReg = getRegForValue(TI->getOperand(0));
1135 if (OpReg == 0) return false;
1136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1137 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001138
Chris Lattnerc76d1212011-04-19 04:26:32 +00001139 unsigned JmpOpc = X86::JNE_4;
1140 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1141 std::swap(TrueMBB, FalseMBB);
1142 JmpOpc = X86::JE_4;
1143 }
Eric Christopher471e4222011-06-08 23:55:35 +00001144
Chris Lattnerc76d1212011-04-19 04:26:32 +00001145 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001146 .addMBB(TrueMBB);
1147 FastEmitBranch(FalseMBB, DL);
1148 FuncInfo.MBB->addSuccessor(TrueMBB);
1149 return true;
1150 }
1151 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001152 }
1153
1154 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001155 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1156 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001157 unsigned OpReg = getRegForValue(BI->getCondition());
1158 if (OpReg == 0) return false;
1159
Eli Friedman547eb4f2011-04-27 01:34:27 +00001160 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1161 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001162 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1163 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001164 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001165 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001166 return true;
1167}
1168
Dan Gohman46510a72010-04-15 01:51:59 +00001169bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001170 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001171 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001172 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001173 CReg = X86::CL;
1174 RC = &X86::GR8RegClass;
1175 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001176 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1177 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1178 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001179 default: return false;
1180 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001181 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001182 CReg = X86::CX;
1183 RC = &X86::GR16RegClass;
1184 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001185 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1186 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1187 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001188 default: return false;
1189 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001190 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001191 CReg = X86::ECX;
1192 RC = &X86::GR32RegClass;
1193 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001194 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1195 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1196 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001197 default: return false;
1198 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001199 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001200 CReg = X86::RCX;
1201 RC = &X86::GR64RegClass;
1202 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001203 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1204 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1205 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001206 default: return false;
1207 }
1208 } else {
1209 return false;
1210 }
1211
Duncan Sands1440e8b2010-11-03 11:35:31 +00001212 MVT VT;
1213 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001214 return false;
1215
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001216 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1217 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001218
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001219 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1220 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001221 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1222 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001223
1224 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001225 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001226 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001227 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1228 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001229 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001230
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001231 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1233 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001234 UpdateValueMap(I, ResultReg);
1235 return true;
1236}
1237
Dan Gohman46510a72010-04-15 01:51:59 +00001238bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001239 MVT VT;
1240 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001241 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001242
Eric Christophere487b012010-09-29 23:00:29 +00001243 // We only use cmov here, if we don't have a cmov instruction bail.
1244 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001245
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001246 unsigned Opc = 0;
1247 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001248 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001249 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001250 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001251 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001252 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001253 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001254 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001255 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001256 RC = &X86::GR64RegClass;
1257 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001258 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001259 }
1260
1261 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1262 if (Op0Reg == 0) return false;
1263 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1264 if (Op1Reg == 0) return false;
1265 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1266 if (Op2Reg == 0) return false;
1267
Dan Gohman84023e02010-07-10 09:00:22 +00001268 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1269 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001270 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001271 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1272 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001273 UpdateValueMap(I, ResultReg);
1274 return true;
1275}
1276
Dan Gohman46510a72010-04-15 01:51:59 +00001277bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001278 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001279 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001280 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001281 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001282 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001283 unsigned OpReg = getRegForValue(V);
1284 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001285 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001286 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1287 TII.get(X86::CVTSS2SDrr), ResultReg)
1288 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001289 UpdateValueMap(I, ResultReg);
1290 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001291 }
1292 }
1293
1294 return false;
1295}
1296
Dan Gohman46510a72010-04-15 01:51:59 +00001297bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001298 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001299 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001300 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001301 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001302 unsigned OpReg = getRegForValue(V);
1303 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001304 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001305 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1306 TII.get(X86::CVTSD2SSrr), ResultReg)
1307 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001308 UpdateValueMap(I, ResultReg);
1309 return true;
1310 }
1311 }
1312 }
1313
1314 return false;
1315}
1316
Dan Gohman46510a72010-04-15 01:51:59 +00001317bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001318 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1319 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001320
Eli Friedman76927d732011-05-25 23:49:02 +00001321 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001322 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001323 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001324 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001325 return false;
1326
1327 unsigned InputReg = getRegForValue(I->getOperand(0));
1328 if (!InputReg)
1329 // Unhandled operand. Halt "fast" selection and bail.
1330 return false;
1331
Eli Friedman76927d732011-05-25 23:49:02 +00001332 if (SrcVT == MVT::i8) {
1333 // Truncate from i8 to i1; no code needed.
1334 UpdateValueMap(I, InputReg);
1335 return true;
1336 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001337
Eli Friedman76927d732011-05-25 23:49:02 +00001338 if (!Subtarget->is64Bit()) {
1339 // If we're on x86-32; we can't extract an i8 from a general register.
1340 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperc9099502012-04-20 06:31:50 +00001341 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1342 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1343 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedman76927d732011-05-25 23:49:02 +00001344 unsigned CopyReg = createResultReg(CopyRC);
1345 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1346 CopyReg).addReg(InputReg);
1347 InputReg = CopyReg;
1348 }
1349
1350 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001351 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001352 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001353 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001354 if (!ResultReg)
1355 return false;
1356
1357 UpdateValueMap(I, ResultReg);
1358 return true;
1359}
1360
Eli Friedmanc0883452011-05-20 22:21:04 +00001361bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1362 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1363}
1364
Eli Friedmand5089a92011-04-27 01:45:07 +00001365bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1366 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001367
Eli Friedmand5089a92011-04-27 01:45:07 +00001368 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001369 if (!IsMemcpySmall(Len))
1370 return false;
1371
1372 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001373
1374 // We don't care about alignment here since we just emit integer accesses.
1375 while (Len) {
1376 MVT VT;
1377 if (Len >= 8 && i64Legal)
1378 VT = MVT::i64;
1379 else if (Len >= 4)
1380 VT = MVT::i32;
1381 else if (Len >= 2)
1382 VT = MVT::i16;
1383 else {
Eli Friedmand5089a92011-04-27 01:45:07 +00001384 VT = MVT::i8;
1385 }
1386
1387 unsigned Reg;
1388 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1389 RV &= X86FastEmitStore(VT, Reg, DestAM);
1390 assert(RV && "Failed to emit load or store??");
1391
1392 unsigned Size = VT.getSizeInBits()/8;
1393 Len -= Size;
1394 DestAM.Disp += Size;
1395 SrcAM.Disp += Size;
1396 }
1397
1398 return true;
1399}
1400
Dan Gohman46510a72010-04-15 01:51:59 +00001401bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001402 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001403 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001404 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001405 case Intrinsic::memcpy: {
1406 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1407 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001408 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001409 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001410
Eli Friedman25255cb2011-06-10 23:39:36 +00001411 if (isa<ConstantInt>(MCI.getLength())) {
1412 // Small memcpy's are common enough that we want to do them
1413 // without a call if possible.
1414 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1415 if (IsMemcpySmall(Len)) {
1416 X86AddressMode DestAM, SrcAM;
1417 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1418 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1419 return false;
1420 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1421 return true;
1422 }
1423 }
Eric Christopher471e4222011-06-08 23:55:35 +00001424
Eli Friedman25255cb2011-06-10 23:39:36 +00001425 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1426 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001427 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001428
Eli Friedman25255cb2011-06-10 23:39:36 +00001429 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1430 return false;
1431
1432 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001433 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001434 case Intrinsic::memset: {
1435 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001436
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001437 if (MSI.isVolatile())
1438 return false;
1439
Eli Friedman25255cb2011-06-10 23:39:36 +00001440 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1441 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1442 return false;
1443
1444 if (MSI.getDestAddressSpace() > 255)
1445 return false;
1446
1447 return DoSelectCall(&I, "memset");
1448 }
Eric Christopher07754c22010-03-18 20:27:26 +00001449 case Intrinsic::stackprotector: {
Chad Rosiere1093e52012-05-11 19:43:29 +00001450 // Emit code to store the stack guard onto the stack.
Eric Christopher07754c22010-03-18 20:27:26 +00001451 EVT PtrTy = TLI.getPointerTy();
1452
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001453 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1454 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001455
1456 // Grab the frame index.
1457 X86AddressMode AM;
1458 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001459 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001460 return true;
1461 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001462 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001463 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001464 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001465 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001466 if (!X86SelectAddress(DI->getAddress(), AM))
1467 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001468 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001469 // FIXME may need to add RegState::Debug to any registers produced,
1470 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001471 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1472 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001473 return true;
1474 }
Eric Christopher77f79892010-01-18 22:11:29 +00001475 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001476 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001477 return true;
1478 }
Bill Wendling52370a12008-12-09 02:42:50 +00001479 case Intrinsic::sadd_with_overflow:
1480 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001481 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001482
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001483 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001484 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001485 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001486 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001487 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1488
Duncan Sands1440e8b2010-11-03 11:35:31 +00001489 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001490 if (!isTypeLegal(RetTy, VT))
1491 return false;
1492
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001493 const Value *Op1 = I.getArgOperand(0);
1494 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001495 unsigned Reg1 = getRegForValue(Op1);
1496 unsigned Reg2 = getRegForValue(Op2);
1497
1498 if (Reg1 == 0 || Reg2 == 0)
1499 // FIXME: Handle values *not* in registers.
1500 return false;
1501
1502 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001503 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001504 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001505 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001506 OpC = X86::ADD64rr;
1507 else
1508 return false;
1509
Eli Friedman482feb32011-05-16 21:06:17 +00001510 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001511 // both the returned values.
Eli Friedman482feb32011-05-16 21:06:17 +00001512 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001513 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1514 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001515
Chris Lattnera9a42252009-04-12 07:36:01 +00001516 unsigned Opc = X86::SETBr;
1517 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1518 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001519 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1520
1521 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001522 return true;
1523 }
1524 }
1525}
1526
Chad Rosierfd3417d2013-02-25 21:59:35 +00001527bool X86FastISel::FastLowerArguments() {
1528 if (!FuncInfo.CanLowerReturn)
1529 return false;
1530
Chad Rosierd9b306a2013-03-14 21:25:04 +00001531 if (Subtarget->isTargetWindows())
1532 return false;
1533
Chad Rosierfd3417d2013-02-25 21:59:35 +00001534 const Function *F = FuncInfo.Fn;
1535 if (F->isVarArg())
1536 return false;
1537
1538 CallingConv::ID CC = F->getCallingConv();
1539 if (CC != CallingConv::C)
1540 return false;
1541
1542 if (!Subtarget->is64Bit())
1543 return false;
1544
1545 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1546 unsigned Idx = 1;
1547 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1548 I != E; ++I, ++Idx) {
1549 if (Idx > 6)
1550 return false;
1551
1552 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1553 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1554 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1555 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1556 return false;
1557
1558 Type *ArgTy = I->getType();
1559 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1560 return false;
1561
1562 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosierfe88aa02013-02-26 01:05:31 +00001563 if (!ArgVT.isSimple()) return false;
Chad Rosierfd3417d2013-02-25 21:59:35 +00001564 switch (ArgVT.getSimpleVT().SimpleTy) {
1565 case MVT::i32:
1566 case MVT::i64:
1567 break;
1568 default:
1569 return false;
1570 }
1571 }
1572
1573 static const uint16_t GPR32ArgRegs[] = {
1574 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1575 };
1576 static const uint16_t GPR64ArgRegs[] = {
1577 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1578 };
1579
1580 Idx = 0;
1581 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1582 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1583 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1584 I != E; ++I, ++Idx) {
1585 if (I->use_empty())
1586 continue;
1587 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1588 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1589 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1590 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1591 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1592 // Without this, EmitLiveInCopies may eliminate the livein if its only
1593 // use is a bitcast (which isn't turned into an instruction).
1594 unsigned ResultReg = createResultReg(RC);
1595 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1596 ResultReg).addReg(DstReg, getKillRegState(true));
1597 UpdateValueMap(I, ResultReg);
1598 }
1599 return true;
1600}
1601
Dan Gohman46510a72010-04-15 01:51:59 +00001602bool X86FastISel::X86SelectCall(const Instruction *I) {
1603 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001604 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001605
1606 // Can't handle inline asm yet.
1607 if (isa<InlineAsm>(Callee))
1608 return false;
1609
Bill Wendling52370a12008-12-09 02:42:50 +00001610 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001611 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001612 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001613
Chad Rosier425e9512012-12-11 00:18:02 +00001614 // Allow SelectionDAG isel to handle tail calls.
1615 if (cast<CallInst>(I)->isTailCall())
1616 return false;
1617
Eli Friedman25255cb2011-06-10 23:39:36 +00001618 return DoSelectCall(I, 0);
1619}
1620
Rafael Espindolac338fe02012-07-25 15:42:45 +00001621static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1622 const ImmutableCallSite &CS) {
Rafael Espindola742f2c92012-07-25 13:35:45 +00001623 if (Subtarget.is64Bit())
1624 return 0;
1625 if (Subtarget.isTargetWindows())
1626 return 0;
1627 CallingConv::ID CC = CS.getCallingConv();
1628 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1629 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001630 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola742f2c92012-07-25 13:35:45 +00001631 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001632 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola1cee7102012-07-25 13:41:10 +00001633 return 0;
Rafael Espindola742f2c92012-07-25 13:35:45 +00001634 return 4;
1635}
1636
Eli Friedman25255cb2011-06-10 23:39:36 +00001637// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1638bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1639 const CallInst *CI = cast<CallInst>(I);
1640 const Value *Callee = CI->getCalledValue();
1641
Evan Chengf3d4efe2008-09-07 09:09:33 +00001642 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001643 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001644 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001645 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001646 CC != CallingConv::X86_FastCall)
1647 return false;
1648
Evan Cheng381993f2010-01-27 00:00:57 +00001649 // fastcc with -tailcallopt is intended to provide a guaranteed
1650 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001651 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001652 return false;
1653
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001654 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1655 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001656 bool isVarArg = FTy->isVarArg();
1657
1658 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1659 // x86-32. Special handling for x86-64 is implemented.
1660 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001661 return false;
1662
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001663 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001664 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001665 TM.Options.GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001666 return false;
1667
Eli Friedman19515b42011-05-17 18:29:03 +00001668 // Check whether the function can return without sret-demotion.
1669 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001670 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman19515b42011-05-17 18:29:03 +00001671 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001672 *FuncInfo.MF, FTy->isVarArg(),
1673 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001674 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001675 return false;
1676
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001677 // Materialize callee address in a register. FIXME: GV address can be
1678 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001679 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001680 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001681 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001682 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001683 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001684 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001685 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001686 } else if (CalleeAM.Base.Reg != 0) {
1687 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001688 } else
1689 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001690
Evan Chengf3d4efe2008-09-07 09:09:33 +00001691 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001692 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001693 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001694 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001695 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosier15b44972012-02-15 00:36:26 +00001696 unsigned arg_size = CS.arg_size();
1697 Args.reserve(arg_size);
1698 ArgVals.reserve(arg_size);
1699 ArgVTs.reserve(arg_size);
1700 ArgFlags.reserve(arg_size);
Dan Gohman46510a72010-04-15 01:51:59 +00001701 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001702 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001703 // If we're lowering a mem intrinsic instead of a regular call, skip the
1704 // last two arguments, which should not passed to the underlying functions.
1705 if (MemIntName && e-i <= 2)
1706 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001707 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001708 ISD::ArgFlagsTy Flags;
1709 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling034b94b2012-12-19 07:18:57 +00001710 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001711 Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00001712 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001713 Flags.setZExt();
1714
Bill Wendling034b94b2012-12-19 07:18:57 +00001715 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001716 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1717 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001718 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1719 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1720 if (!FrameAlign)
1721 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1722 Flags.setByVal();
1723 Flags.setByValSize(FrameSize);
1724 Flags.setByValAlign(FrameAlign);
1725 if (!IsMemcpySmall(FrameSize))
1726 return false;
1727 }
1728
Bill Wendling034b94b2012-12-19 07:18:57 +00001729 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedmanc0883452011-05-20 22:21:04 +00001730 Flags.setInReg();
Bill Wendling034b94b2012-12-19 07:18:57 +00001731 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedmanc0883452011-05-20 22:21:04 +00001732 Flags.setNest();
1733
Chris Lattnere03b8d32011-04-19 04:42:38 +00001734 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1735 // instruction. This is safe because it is common to all fastisel supported
1736 // calling conventions on x86.
1737 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1738 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1739 CI->getBitWidth() == 16) {
1740 if (Flags.isSExt())
1741 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1742 else
1743 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1744 }
1745 }
Eric Christopher471e4222011-06-08 23:55:35 +00001746
Chris Lattnerb44101c2011-04-19 05:09:50 +00001747 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001748
Chris Lattnerff009ad2011-04-19 05:15:59 +00001749 // Passing bools around ends up doing a trunc to i1 and passing it.
1750 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001751 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1752 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1753 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001754 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1755 ArgReg = getRegForValue(ArgVal);
1756 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001757
Chris Lattnerb44101c2011-04-19 05:09:50 +00001758 MVT ArgVT;
1759 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001760
Chris Lattnerb44101c2011-04-19 05:09:50 +00001761 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1762 ArgVal->hasOneUse(), 1);
1763 } else {
1764 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001765 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001766
Chris Lattnerff009ad2011-04-19 05:15:59 +00001767 if (ArgReg == 0) return false;
1768
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001769 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001770 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001771 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001772 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001773 if (ArgVT == MVT::x86mmx)
1774 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001775 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1776 Flags.setOrigAlign(OriginalAlignment);
1777
Chris Lattnerb44101c2011-04-19 05:09:50 +00001778 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001779 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001780 ArgVTs.push_back(ArgVT);
1781 ArgFlags.push_back(Flags);
1782 }
1783
1784 // Analyze operands of the call, assigning locations to each operand.
1785 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001786 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00001787 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001788
Dan Gohmand8acddd2010-06-01 21:09:47 +00001789 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001790 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001791 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001792
Duncan Sands45907662010-10-31 13:21:44 +00001793 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001794
1795 // Get a count of how many bytes are to be pushed on the stack.
1796 unsigned NumBytes = CCInfo.getNextStackOffset();
1797
1798 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001799 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001800 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1801 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001802
Chris Lattner438949a2008-10-15 05:30:52 +00001803 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001804 // copies / loads.
1805 SmallVector<unsigned, 4> RegArgs;
1806 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1807 CCValAssign &VA = ArgLocs[i];
1808 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001809 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001810
Evan Chengf3d4efe2008-09-07 09:09:33 +00001811 // Promote the value if needed.
1812 switch (VA.getLocInfo()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001813 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001814 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001815 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1816 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001817 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1818 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001819 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001820 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001821 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001822 }
1823 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001824 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1825 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001826 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1827 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001828 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001829 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001830 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001831 }
1832 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001833 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1834 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001835 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1836 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001837 if (!Emitted)
1838 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001839 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001840 if (!Emitted)
1841 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1842 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001843
Chris Lattnerc46ec642011-01-05 22:26:52 +00001844 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001845 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001846 break;
1847 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001848 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001849 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001850 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001851 assert(BC != 0 && "Failed to emit a bitcast!");
1852 Arg = BC;
1853 ArgVT = VA.getLocVT();
1854 break;
1855 }
Chad Rosier36ec0ca2012-07-11 19:58:38 +00001856 case CCValAssign::VExt:
1857 // VExt has not been implemented, so this should be impossible to reach
1858 // for now. However, fallback to Selection DAG isel once implemented.
1859 return false;
1860 case CCValAssign::Indirect:
1861 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
1862 // support this.
1863 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001864 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001865
Evan Chengf3d4efe2008-09-07 09:09:33 +00001866 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001867 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1868 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001869 RegArgs.push_back(VA.getLocReg());
1870 } else {
1871 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001872 X86AddressMode AM;
Michael Liaof0e06e82012-11-01 03:47:50 +00001873 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman0586d912008-09-10 20:11:02 +00001874 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001875 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00001876 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001877
Eli Friedmanc0883452011-05-20 22:21:04 +00001878 if (Flags.isByVal()) {
1879 X86AddressMode SrcAM;
1880 SrcAM.Base.Reg = Arg;
1881 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
1882 assert(Res && "memcpy length already checked!"); (void)Res;
1883 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
1884 // If this is a really simple value, emit this with the Value* version
Nick Lewycky1f9c6862011-10-12 00:14:12 +00001885 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedmanc0883452011-05-20 22:21:04 +00001886 // as it can cause us to reevaluate the argument.
Lang Hamese4824712011-10-18 22:11:33 +00001887 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
1888 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001889 } else {
Lang Hamese4824712011-10-18 22:11:33 +00001890 if (!X86FastEmitStore(ArgVT, Arg, AM))
1891 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001892 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001893 }
1894 }
1895
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001896 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001897 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001898 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001899 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001900 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1901 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001902 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001903
Eli Friedman37620462011-04-19 17:22:22 +00001904 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1905 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00001906 static const uint16_t XMMArgRegs[] = {
Eli Friedman37620462011-04-19 17:22:22 +00001907 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1908 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1909 };
1910 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1911 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1912 X86::AL).addImm(NumXMMRegs);
1913 }
1914
Evan Chengf3d4efe2008-09-07 09:09:33 +00001915 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001916 MachineInstrBuilder MIB;
1917 if (CalleeOp) {
1918 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001919 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00001920 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00001921 CallOpc = X86::CALL64r;
1922 else
1923 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001924 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1925 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001926
Chris Lattner51e8eab2009-07-09 06:34:26 +00001927 } else {
1928 // Direct call.
1929 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001930 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00001931 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00001932 CallOpc = X86::CALL64pcrel32;
1933 else
1934 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001935
Chris Lattner51e8eab2009-07-09 06:34:26 +00001936 // See if we need any target-specific flags on the GV operand.
1937 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001938
Chris Lattner51e8eab2009-07-09 06:34:26 +00001939 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1940 // external symbols most go through the PLT in PIC mode. If the symbol
1941 // has hidden or protected visibility, or if it is static or local, then
1942 // we don't need to use the PLT - we can directly call it.
1943 if (Subtarget->isTargetELF() &&
1944 TM.getRelocationModel() == Reloc::PIC_ &&
1945 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1946 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001947 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001948 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001949 (!Subtarget->getTargetTriple().isMacOSX() ||
1950 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001951 // PC-relative references to external symbols should go through $stub,
1952 // unless we're building with the leopard linker or later, which
1953 // automatically synthesizes these stubs.
1954 OpFlags = X86II::MO_DARWIN_STUB;
1955 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001956
1957
Eli Friedman25255cb2011-06-10 23:39:36 +00001958 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
1959 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00001960 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00001961 else
1962 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001963 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001964
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00001965 // Add a register mask with the call-preserved registers.
1966 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1967 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
1968
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00001969 // Add an implicit use GOT pointer in EBX.
1970 if (Subtarget->isPICStyleGOT())
1971 MIB.addReg(X86::EBX, RegState::Implicit);
1972
1973 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1974 MIB.addReg(X86::AL, RegState::Implicit);
1975
1976 // Add implicit physical register uses to the call.
1977 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1978 MIB.addReg(RegArgs[i], RegState::Implicit);
1979
Evan Chengf3d4efe2008-09-07 09:09:33 +00001980 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001981 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolac338fe02012-07-25 15:42:45 +00001982 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohman84023e02010-07-10 09:00:22 +00001983 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001984 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001985
Eli Friedman19515b42011-05-17 18:29:03 +00001986 // Build info for return calling conv lowering code.
1987 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1988 SmallVector<ISD::InputArg, 32> Ins;
1989 SmallVector<EVT, 4> RetTys;
1990 ComputeValueVTs(TLI, I->getType(), RetTys);
1991 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1992 EVT VT = RetTys[i];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00001993 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman19515b42011-05-17 18:29:03 +00001994 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1995 for (unsigned j = 0; j != NumRegs; ++j) {
1996 ISD::InputArg MyFlags;
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00001997 MyFlags.VT = RegisterVT;
Eli Friedman19515b42011-05-17 18:29:03 +00001998 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling034b94b2012-12-19 07:18:57 +00001999 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002000 MyFlags.Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002001 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002002 MyFlags.Flags.setZExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002003 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman19515b42011-05-17 18:29:03 +00002004 MyFlags.Flags.setInReg();
2005 Ins.push_back(MyFlags);
2006 }
2007 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002008
Eli Friedman19515b42011-05-17 18:29:03 +00002009 // Now handle call return values.
2010 SmallVector<unsigned, 4> UsedRegs;
2011 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002012 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00002013 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00002014 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2015 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2016 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2017 EVT CopyVT = RVLocs[i].getValVT();
2018 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002019
Evan Chengf3d4efe2008-09-07 09:09:33 +00002020 // If this is a call to a function that returns an fp value on the x87 fp
2021 // stack, but where we prefer to use the value in xmm registers, copy it
2022 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00002023 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002024 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002025 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002026 CopyVT = MVT::f80;
Craig Topperc9099502012-04-20 06:31:50 +00002027 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002028 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002029 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2030 CopyReg);
2031 } else {
2032 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2033 CopyReg).addReg(RVLocs[i].getLocReg());
2034 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00002035 }
2036
Eli Friedman19515b42011-05-17 18:29:03 +00002037 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00002038 // Round the F80 the right size, which also moves to the appropriate xmm
2039 // register. This is accomplished by storing the F80 value in memory and
2040 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00002041 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00002043 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002044 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00002045 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2046 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00002047 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00002049 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00002050 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002051 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002052 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00002053
Eli Friedman19515b42011-05-17 18:29:03 +00002054 if (RVLocs.size())
2055 UpdateValueMap(I, ResultReg, RVLocs.size());
2056
Dan Gohmandb497122010-06-18 23:28:01 +00002057 // Set all unused physreg defs as dead.
2058 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2059
Evan Chengf3d4efe2008-09-07 09:09:33 +00002060 return true;
2061}
2062
2063
Dan Gohman99b21822008-08-28 23:21:34 +00002064bool
Dan Gohman46510a72010-04-15 01:51:59 +00002065X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00002066 switch (I->getOpcode()) {
2067 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00002068 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00002069 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00002070 case Instruction::Store:
2071 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00002072 case Instruction::Ret:
2073 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00002074 case Instruction::ICmp:
2075 case Instruction::FCmp:
2076 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00002077 case Instruction::ZExt:
2078 return X86SelectZExt(I);
2079 case Instruction::Br:
2080 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002081 case Instruction::Call:
2082 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002083 case Instruction::LShr:
2084 case Instruction::AShr:
2085 case Instruction::Shl:
2086 return X86SelectShift(I);
2087 case Instruction::Select:
2088 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00002089 case Instruction::Trunc:
2090 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00002091 case Instruction::FPExt:
2092 return X86SelectFPExt(I);
2093 case Instruction::FPTrunc:
2094 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00002095 case Instruction::IntToPtr: // Deliberate fall-through.
2096 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00002097 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2098 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00002099 if (DstVT.bitsGT(SrcVT))
2100 return X86SelectZExt(I);
2101 if (DstVT.bitsLT(SrcVT))
2102 return X86SelectTrunc(I);
2103 unsigned Reg = getRegForValue(I->getOperand(0));
2104 if (Reg == 0) return false;
2105 UpdateValueMap(I, Reg);
2106 return true;
2107 }
Dan Gohman99b21822008-08-28 23:21:34 +00002108 }
2109
2110 return false;
2111}
2112
Dan Gohman46510a72010-04-15 01:51:59 +00002113unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002114 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00002115 if (!isTypeLegal(C->getType(), VT))
Michael Liaofaa11592012-08-30 00:30:16 +00002116 return 0;
2117
2118 // Can't handle alternate code models yet.
2119 if (TM.getCodeModel() != CodeModel::Small)
2120 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002121
Owen Anderson95267a12008-09-05 00:06:23 +00002122 // Get opcode and regclass of the output for the given load instruction.
2123 unsigned Opc = 0;
2124 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002125 switch (VT.SimpleTy) {
Michael Liaofaa11592012-08-30 00:30:16 +00002126 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002127 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00002128 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +00002129 RC = &X86::GR8RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002130 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002131 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00002132 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +00002133 RC = &X86::GR16RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002134 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00002136 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +00002137 RC = &X86::GR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002138 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002139 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00002140 // Must be in x86-64 mode.
2141 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +00002142 RC = &X86::GR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002143 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002144 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002145 if (X86ScalarSSEf32) {
2146 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +00002147 RC = &X86::FR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002148 } else {
2149 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +00002150 RC = &X86::RFP32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002151 }
2152 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002153 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002154 if (X86ScalarSSEf64) {
2155 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +00002156 RC = &X86::FR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002157 } else {
2158 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +00002159 RC = &X86::RFP64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002160 }
2161 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002162 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002163 // No f80 support yet.
Michael Liaofaa11592012-08-30 00:30:16 +00002164 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002165 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002166
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002167 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002168 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002169 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002170 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002171 // If the expression is just a basereg, then we're done, otherwise we need
2172 // to emit an LEA.
2173 if (AM.BaseType == X86AddressMode::RegBase &&
2174 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2175 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002176
Chris Lattner685090f2011-04-17 17:12:08 +00002177 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002178 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002179 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2180 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002181 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002182 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002183 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002184 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002185
Owen Anderson3b217c62008-09-06 01:11:01 +00002186 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002187 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002188 if (Align == 0) {
2189 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002190 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002191 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002192
Dan Gohman5396c992008-09-30 01:21:32 +00002193 // x86-32 PIC requires a PIC base register for constant pools.
2194 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002195 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002196 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002197 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002198 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002199 } else if (Subtarget->isPICStyleGOT()) {
2200 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002201 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002202 } else if (Subtarget->isPICStyleRIPRel() &&
2203 TM.getCodeModel() == CodeModel::Small) {
2204 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002205 }
Dan Gohman5396c992008-09-30 01:21:32 +00002206
2207 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002208 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002209 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002210 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2211 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002212 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002213
Owen Anderson95267a12008-09-05 00:06:23 +00002214 return ResultReg;
2215}
2216
Dan Gohman46510a72010-04-15 01:51:59 +00002217unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002218 // Fail on dynamic allocas. At this point, getRegForValue has already
2219 // checked its CSE maps, so if we're here trying to handle a dynamic
2220 // alloca, we're not going to succeed. X86SelectAddress has a
2221 // check for dynamic allocas, because it's called directly from
2222 // various places, but TargetMaterializeAlloca also needs a check
2223 // in order to avoid recursion between getRegForValue,
2224 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002225 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002226 return 0;
2227
Dan Gohman0586d912008-09-10 20:11:02 +00002228 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002229 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002230 return 0;
2231 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper44d23822012-02-22 05:59:10 +00002232 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman0586d912008-09-10 20:11:02 +00002233 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002234 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2235 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002236 return ResultReg;
2237}
2238
Eli Friedman2790ba82011-04-27 22:41:55 +00002239unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2240 MVT VT;
2241 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002242 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002243
2244 // Get opcode and regclass for the given zero.
2245 unsigned Opc = 0;
2246 const TargetRegisterClass *RC = NULL;
2247 switch (VT.SimpleTy) {
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002248 default: return 0;
Craig Topperf4cfc442012-08-11 17:53:00 +00002249 case MVT::f32:
2250 if (X86ScalarSSEf32) {
2251 Opc = X86::FsFLD0SS;
2252 RC = &X86::FR32RegClass;
2253 } else {
2254 Opc = X86::LD_Fp032;
2255 RC = &X86::RFP32RegClass;
2256 }
2257 break;
2258 case MVT::f64:
2259 if (X86ScalarSSEf64) {
2260 Opc = X86::FsFLD0SD;
2261 RC = &X86::FR64RegClass;
2262 } else {
2263 Opc = X86::LD_Fp064;
2264 RC = &X86::RFP64RegClass;
2265 }
2266 break;
2267 case MVT::f80:
2268 // No f80 support yet.
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002269 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002270 }
2271
2272 unsigned ResultReg = createResultReg(RC);
2273 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2274 return ResultReg;
2275}
2276
2277
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002278/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2279/// vreg is being provided by the specified load instruction. If possible,
2280/// try to fold the load as an operand to the instruction, returning true if
2281/// possible.
2282bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2283 const LoadInst *LI) {
2284 X86AddressMode AM;
2285 if (!X86SelectAddress(LI->getOperand(0), AM))
2286 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002287
Craig Topperdca72542012-08-11 17:46:16 +00002288 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002289
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002290 unsigned Size = TD.getTypeAllocSize(LI->getType());
2291 unsigned Alignment = LI->getAlignment();
2292
2293 SmallVector<MachineOperand, 8> AddrOps;
2294 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002295
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002296 MachineInstr *Result =
2297 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2298 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002299
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002300 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002301 MI->eraseFromParent();
2302 return true;
2303}
2304
2305
Evan Chengc3f44b02008-09-03 00:03:49 +00002306namespace llvm {
Bob Wilsond49edb72012-08-03 04:06:28 +00002307 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2308 const TargetLibraryInfo *libInfo) {
2309 return new X86FastISel(funcInfo, libInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002310 }
Dan Gohman99b21822008-08-28 23:21:34 +00002311}